feat(migrate): M7 迁移与版本门 — per-file markup + migrate labels + 全仓迁移

原计划的"翻转默认 + 旧形硬报错"不可行:`[键](集合)` 与 `[描述](实现)` 外形相同,
翻转会让 506 处取元静默变义,硬报错又拿不到证据。改为**按文件声明**。

一、per-file markup
- frontmatter `markup: occlusion`(或 `标记: 遮蔽`)决定该文件模式
- load.rs 先取模式,用 parse::expr::with_markup(线程局部压栈)包住解析
- 同一进程可同时加载两种模式的文件(imports);MARQDO_MARKUP 仍作进程默认
- 好处:迁移逐文件、可增量、可回滚,不存在半迁移的静默错义

二、`marqdo migrate labels [--write|--json]`
改写规则:
  [键](集合)      → [键](access(集合, "键"))
  [`名`](集合)    → [名](access(集合, `名`))
  - [项](集合)    → - [项](遍历 集合 为 项)

安全模型:改写 → 重解析 → 证明等价 → 才写
- 原文件按 classic 解析得 AST-A;改写后按 occlusion 解析得 AST-B
- 反归一化 B(标签与键同名且 payload 为 access 的遮蔽还原成 Index)
- Debug(A) == Debug(反归一化(B))(抹掉行号)才写入,否则原文件一字不动

只改代码行(LineKind::Code);表格行只改 **…** 单元格,散文 markdown 链接不碰;
链式索引由内向外改写,已迁移的有守卫不二次改。

三、access 提升为宿主函数 HostFn::Access(u16 id 212)
与旧取元逐字一致(Map 按键 / List 1-based / 缺键 missing map key);
进 HostFn 是为了两个后端共用同一实现,interp 里的特例已删除。

四、顺带修的两处真实语义缺陷
1. payload 继承外层"文本模式":`f text=[k](access(c,k))` 中 access 不被当调用
   → `()` 内永远允许 callee(args)
2. 遮蔽形状判定过宽:`[问候] ([格式化] "Bo")` 误判为遮蔽
   → 要求 `]` 后紧跟 `(`,否则落回方括号调用

五、全仓迁移结果
migrate labels: 256 index, 23 loop, 80 file written;0 文件被等价性拒绝;
迁移后 cargo test --release → 604 passed / 0 failed;再跑迁移幂等。

等价性检查先后抓出 4 类改写 bug(表格散文链接、payload 文本模式、链式索引、
遮蔽与方括号调用混淆),每类都是先拦下再修工具,未污染文件。

测试:6 个单测 + 3 个集成(干跑不写盘 / 写入后运行输出不变且幂等 /
等价失败文件不动);全量 604 passed / 0 failed(M3 后 595 + 9);双后端一致。

文档:doc/design/change-m7-migration.md
This commit is contained in:
cflmy committed 2026-10-22 15:33:31 +08:00
1 parent 9dbcc728b6
commit 4488a3cb70
98 files changed
+1354 -278

No files matched your search

+61 -5
View File
@@ -33,7 +33,26 @@ pub fn set_markup_default(m: Markup) {
);
}
thread_local! {
/// Per-module override (`markup:` in frontmatter) — wins over the process default.
static MARKUP_OVERRIDE: std::cell::Cell<Option<Markup>> = const { std::cell::Cell::new(None) };
}
/// Run `f` with `markup` as the effective mode for everything parsed inside.
///
/// Frontmatter `markup: occlusion` uses this so that one process can load files
/// in different modes (imports!). `<TAB>` M7
pub fn with_markup<T>(markup: Markup, f: impl FnOnce() -> T) -> T {
let saved = MARKUP_OVERRIDE.with(|c| c.replace(Some(markup)));
let out = f();
MARKUP_OVERRIDE.with(|c| c.set(saved));
out
}
pub fn markup_default() -> Markup {
if let Some(m) = MARKUP_OVERRIDE.with(|c| c.get()) {
return m;
}
match MARKUP.load(std::sync::atomic::Ordering::Relaxed) {
1 => Markup::Occlusion,
_ => Markup::Classic,
@@ -517,7 +536,7 @@ impl<'a> Parser<'a> {
// Link-shaped index `[key](collection)` vs bracket-marked call `[callee] …`
// vs JSON-ish `[…]` (arg text fallback when expr parse fails).
if self.starts_with("[") && !self.starts_with("[^") {
if self.markup == Markup::Occlusion {
if self.markup == Markup::Occlusion && occlusion_shape_ahead(self.rest()) {
return self.parse_occluded();
}
if link_index_shape_ahead(self.rest()) {
@@ -722,7 +741,13 @@ impl<'a> Parser<'a> {
payload: None,
});
}
let inner = self.parse_or()?;
// `()` is machine code: the payload always allows `callee(args)` even
// when the occlusion sits inside a text-like argument value. `<TAB>` M7
let saved_allow = self.allow_paren_call;
self.allow_paren_call = true;
let inner = self.parse_or();
self.allow_paren_call = saved_allow;
let inner = inner?;
self.skip_ws();
if !self.eat(")") {
bail!("expected ')' to close occlusion payload");
@@ -1432,6 +1457,33 @@ fn split_first_token(s: &str, paren_mode: bool) -> (&str, &str) {
}
/// `[key](…)` with no whitespace between `]` and `(`.
/// True when `[…]` is immediately followed by `(` — i.e. an occlusion shape.
/// `[问候] "Ada"` (bracket-marked call) has no `(`, so it keeps its meaning. `<TAB>` M7
fn occlusion_shape_ahead(s: &str) -> bool {
let mut chars = s.char_indices();
let Some((_, '[')) = chars.next() else {
return false;
};
let mut escaped = false;
for (i, c) in chars {
if escaped {
escaped = false;
continue;
}
match c {
'\\' => escaped = true,
']' => {
// Strict: `(` must follow immediately. `[问候] ([…])` is a
// bracket-marked call with a parenthesised argument, not an occlusion.
return s[i + 1..].starts_with('(');
}
'\n' => return false,
_ => {}
}
}
false
}
fn link_index_shape_ahead(s: &str) -> bool {
let s = s.trim_start();
if !s.starts_with('[') || s.starts_with("[^") {
@@ -2124,9 +2176,13 @@ mod bracket_call_tests {
}
#[test]
fn occlusion_requires_paren() {
let err = parse_expr_markup("[只有描述]", false, Markup::Occlusion).unwrap_err();
assert!(err.to_string().contains("followed by"), "{err}");
fn occlusion_requires_immediate_paren() {
// No `(` after `]` → falls back to the bracket-marked call form.
let e = parse_expr_markup("[只有描述]", false, Markup::Occlusion).unwrap();
assert!(matches!(e, Expr::Call(_)), "got {e:?}");
// A space before `(` also means "not an occlusion".
let e = parse_expr_markup("[问候] (1)", false, Markup::Occlusion).unwrap();
assert!(matches!(e, Expr::Call(_)), "got {e:?}");
}
#[test]