//! `` M7: migrate legacy `[键](集合)` indexes and `- [项](集合)` loops to the //! purity forms (`access(集合, 键)` and `- [描述](遍历 集合 为 项)`), then mark //! the file `markup: occlusion`. //! //! Also: nested YAML `import:` migration (`marqdo migrate imports`). //! //! Safety model (labels): **transform, re-parse, prove equivalence, then write.** //! The transformed file is parsed in occlusion mode and *denormalized* back to //! the legacy AST shape; if the denormalized AST is not byte-identical (Debug) //! to the original, the file is left untouched and reported. use std::path::{Path, PathBuf}; use crate::ast::{Arg, BranchArm, Expr, Function, IndexKey, Module, Stmt}; use crate::parse::{is_legacy_import_oneliner, parse_import_binding}; /// Outcome of migrating one file (labels). #[derive(Debug, Clone)] pub struct FileReport { pub file: PathBuf, pub indexes: usize, pub loops: usize, /// Whether this file will receive (or already has) an explicit /// `markup: occlusion` declaration. pub mode_marked: bool, pub applied: bool, pub note: Option, } /// Outcome of migrating one-liner imports → nested `import:` / `导入:`. #[derive(Debug, Clone)] pub struct ImportFileReport { pub file: PathBuf, pub bindings: usize, pub applied: bool, pub note: Option, } fn has_marker(source: &str) -> bool { let mut lines = source.lines(); if lines.next().map(|l| l.trim_end()) != Some("---") { return false; } for line in lines { let t = line.trim(); if t == "---" { break; } let lower = t.to_ascii_lowercase(); if lower.starts_with("markup:") || t.starts_with("标记:") { return lower.contains("occlusion") || lower.contains("purity") || t.contains("遮蔽") || t.contains("纯度"); } } false } fn add_marker(source: &str) -> String { if has_marker(source) { return source.to_string(); } // A classic marker is an explicit request to migrate; replace it rather // than adding conflicting keys to the same frontmatter block. if source.starts_with("---\n") { let mut lines: Vec = source.lines().map(str::to_string).collect(); for (index, line) in lines.iter_mut().enumerate() { let lower = line.trim().to_ascii_lowercase(); if lower.starts_with("markup:") || line.trim().starts_with("标记:") { *line = "markup: occlusion".to_string(); let mut out = lines.join("\n"); if source.ends_with('\n') { out.push('\n'); } return out; } if line.trim() == "---" && index != 0 { break; } } } if let Some(rest) = source.strip_prefix("---\n") { format!("---\nmarkup: occlusion\n{rest}") } else { format!("---\nmarkup: occlusion\n---\n\n{source}") } } /// Quote a static index key as a text argument. fn quote_key(key: &str) -> String { let escaped = key.replace('\\', "\\\\").replace('"', "\\\""); format!("\"{escaped}\"") } /// Rewrite every link-shaped index in one code line. Returns `(new_line, count)`. /// /// Works **innermost first**: chained indexes (`[content]([1]([messages](req)))`) /// must be rewritten from the inside out, otherwise outer rewrites invalidate the /// inner spans. fn rewrite_indexes(line: &str) -> (String, usize) { let mut cur = line.to_string(); let mut total = 0usize; for _ in 0..256 { match rewrite_one_innermost(&cur) { Some(next) => { cur = next; total += 1; } None => break, } } (cur, total) } /// Rewrite the first index whose key contains no nested `[`, returning the new line. fn rewrite_one_innermost(line: &str) -> Option { let chars: Vec = line.chars().collect(); let mut i = 0usize; let mut in_bt = false; let mut in_str = false; while i < chars.len() { let c = chars[i]; if in_str { if c == '\\' { i += 2; continue; } if c == '"' { in_str = false; } i += 1; continue; } if in_bt { if c == '`' { in_bt = false; } i += 1; continue; } match c { '`' => { in_bt = true; i += 1; continue; } '"' => { in_str = true; i += 1; continue; } _ => {} } if c == '[' && chars.get(i + 1) != Some(&'^') { if let Some(close) = match_close(&chars, i, '[', ']') { let key: String = chars[i + 1..close].iter().collect(); let nested = key.contains('['); if !nested && chars.get(close + 1) == Some(&'(') { if let Some(paren) = match_close(&chars, close + 1, '(', ')') { let coll: String = chars[close + 2..paren].iter().collect(); // Already migrated (`[k](access(…))`) — never rewrite twice. if coll.trim_start().starts_with("access(") { i += 1; continue; } let k = key.trim(); let backticked = k.starts_with('`') && k.ends_with('`') && k.len() >= 2; let label = if backticked { &k[1..k.len() - 1] } else { k }; let key_arg = if backticked { k.to_string() } else { quote_key(k) }; let replaced = format!("[{label}](access({}, {key_arg}))", coll.trim()); let mut out = String::new(); out.extend(chars[..i].iter()); out.push_str(&replaced); out.extend(chars[paren + 1..].iter()); return Some(out); } } } } i += 1; } None } /// Rewrite only the **code** parts of a line. /// /// Table rows are the important case: a cell is code only when it starts with /// `**…**`; everything else is prose (markdown links live there and must not be /// rewritten). Non-table code lines are rewritten whole. fn rewrite_code_segments(line: &str) -> (String, usize) { let trimmed = line.trim_start(); if !trimmed.starts_with('|') { return rewrite_indexes(line); } let mut out = String::with_capacity(line.len()); let mut total = 0usize; let mut rest = line; while let Some(pos) = rest.find('|') { let (cell, tail) = rest.split_at(pos); out.push_str(cell); out.push('|'); rest = &tail[1..]; } // Trailing part without a separator. out.push_str(rest); // Second pass: rewrite only `**…**` runs inside the rebuilt line. let mut result = String::with_capacity(out.len()); let mut s = out.as_str(); while let Some(open) = s.find("**") { result.push_str(&s[..open + 2]); let after = &s[open + 2..]; let Some(close) = after.find("**") else { result.push_str(after); return (result, total); }; let (inner, tail) = after.split_at(close); let (rewritten, n) = rewrite_indexes(inner); total += n; result.push_str(&rewritten); result.push_str("**"); s = &tail[2..]; } result.push_str(s); (result, total) } /// Matching closer index for `open` at `start`, honouring nesting of the pair. fn match_close(bytes: &[char], start: usize, open: char, close: char) -> Option { let mut depth = 0i32; let mut i = start; let mut in_bt = false; let mut in_str = false; while i < bytes.len() { let c = bytes[i]; if in_str { if c == '\\' { i += 2; continue; } if c == '"' { in_str = false; } i += 1; continue; } if in_bt { if c == '`' { in_bt = false; } i += 1; continue; } match c { '`' => in_bt = true, '"' => in_str = true, c if c == open => depth += 1, c if c == close => { depth -= 1; if depth == 0 { return Some(i); } } _ => {} } i += 1; } None } /// Rewrite a legacy loop line `- [x](c)` → `- [x](遍历 c 为 x)`. fn rewrite_loop(line: &str) -> Option { let trimmed = line.trim_start(); let rest = trimmed.strip_prefix('-')?.trim_start(); if !rest.starts_with('[') { return None; } let end = rest.find(']')?; let item = rest[1..end].trim(); let after = rest[end + 1..].trim(); if !after.starts_with('(') || !after.ends_with(')') { return None; } let coll = after[1..after.len() - 1].trim(); if item.is_empty() || coll.is_empty() || says_foreach(coll) { return None; } let indent = &line[..line.len() - trimmed.len()]; let shown = item.trim_matches('`').to_string(); Some(format!("{indent}- [{shown}](遍历 {coll} 为 {item})")) } fn says_foreach(s: &str) -> bool { s.starts_with("遍历") || s.starts_with("for ") } // --------------------------------------------------------------------------- // Equivalence proof: denormalize the migrated AST back to the legacy shape. // --------------------------------------------------------------------------- fn denorm_expr(expr: &Expr) -> Expr { match expr { Expr::Occluded { label, payload } => match payload.as_deref() { Some(Expr::Call(call)) if (call.callee == "access" || call.callee == "取" || call.callee == "取值") && call.args.len() == 2 => { let mut it = call.args.iter(); let (base, key) = match (it.next(), it.next()) { (Some(Arg::Positional(b)), Some(Arg::Positional(k))) => (b, k), _ => return expr.clone(), }; let key_label = match key { Expr::Literal(crate::ast::Literal::Text(t)) if t == label => { Some(IndexKey::Lit(t.clone())) } Expr::Var(v) if v == label => Some(IndexKey::Var(v.clone())), _ => None, }; match key_label { Some(k) => Expr::Index { base: Box::new(denorm_expr(base)), label: k, }, None => expr.clone(), } } Some(inner) => Expr::Occluded { label: label.clone(), payload: Some(Box::new(denorm_expr(inner))), }, None => expr.clone(), }, Expr::Unary { op, expr } => Expr::Unary { op: *op, expr: Box::new(denorm_expr(expr)), }, Expr::Binary { op, left, right } => Expr::Binary { op: *op, left: Box::new(denorm_expr(left)), right: Box::new(denorm_expr(right)), }, Expr::Call(call) => { let mut c = call.clone(); c.args = c .args .into_iter() .map(|a| match a { Arg::Positional(e) => Arg::Positional(denorm_expr(&e)), Arg::Named { name, value } => Arg::Named { name, value: denorm_expr(&value), }, }) .collect(); Expr::Call(c) } Expr::List(items) => Expr::List(items.iter().map(denorm_expr).collect()), Expr::Map(pairs) => Expr::Map( pairs .iter() .map(|(k, v)| (k.clone(), denorm_expr(v))) .collect(), ), Expr::Index { base, label } => Expr::Index { base: Box::new(denorm_expr(base)), label: label.clone(), }, other => other.clone(), } } fn denorm_stmt(stmt: &Stmt) -> Stmt { match stmt { Stmt::Assign { name, value, span, end_line, } => Stmt::Assign { name: name.clone(), value: denorm_expr(value), span: *span, end_line: *end_line, }, Stmt::Return { value, span } => Stmt::Return { value: denorm_expr(value), span: *span, }, Stmt::Expr { value, span } => Stmt::Expr { value: denorm_expr(value), span: *span, }, Stmt::Call { call, span } => { let mut c = call.clone(); c.args = c .args .into_iter() .map(|a| match a { Arg::Positional(e) => Arg::Positional(denorm_expr(&e)), Arg::Named { name, value } => Arg::Named { name, value: denorm_expr(&value), }, }) .collect(); Stmt::Call { call: c, span: *span, } } Stmt::Branch { arms, span } => Stmt::Branch { arms: arms .iter() .map(|a| BranchArm { condition: a.condition.as_ref().map(denorm_expr), body: a.body.iter().map(denorm_stmt).collect(), }) .collect(), span: *span, }, Stmt::While { condition, body, span, } => Stmt::While { condition: denorm_expr(condition), body: body.iter().map(denorm_stmt).collect(), span: *span, }, Stmt::ForEach { item, collection, label: _, body, span, } => Stmt::ForEach { item: item.clone(), collection: collection.clone(), label: None, body: body.iter().map(denorm_stmt).collect(), span: *span, }, } } fn denorm_fun(fun: &Function) -> Function { let mut f = fun.clone(); f.body = fun.body.iter().map(denorm_stmt).collect(); f.children = fun.children.iter().map(denorm_fun).collect(); f } /// Replace every `line: ` with `line: #` so that inserting the frontmatter /// marker (which shifts every line by one) does not defeat the comparison. fn strip_line_numbers(s: &str) -> String { let mut out = String::with_capacity(s.len()); let mut rest = s; while let Some(i) = rest.find("line: ") { out.push_str(&rest[..i + 6]); let tail = &rest[i + 6..]; let end = tail .find(|c: char| !c.is_ascii_digit()) .unwrap_or(tail.len()); out.push('#'); rest = &tail[end..]; } out.push_str(rest); out } fn shape(module: &Module) -> String { let raw = format!( "{:?}", module.functions.iter().map(denorm_fun).collect::>() ); strip_line_numbers(&raw) } // --------------------------------------------------------------------------- // Entry point // --------------------------------------------------------------------------- /// Migrate one file. `write=false` performs a dry run (still fully verified). pub fn migrate_file(path: &Path, write: bool) -> anyhow::Result { migrate_file_with_options(path, write, false) } /// Migrate one file and, when `mark_all` is set, give a file that has no /// legacy index an explicit new-syntax declaration too. This is the mode used /// for shipped libraries: their behavior must not depend on the process /// default even if they happen not to contain a link-shaped index today. pub fn migrate_file_with_options( path: &Path, write: bool, mark_all: bool, ) -> anyhow::Result { let source = std::fs::read_to_string(path)?; let mut report = FileReport { file: path.to_path_buf(), indexes: 0, loops: 0, mode_marked: false, applied: false, note: None, }; if has_marker(&source) { report.mode_marked = true; report.note = Some("already marked `markup: occlusion`".into()); return Ok(report); } // Migration is deliberately syntax-only. Resolving imports here makes a // library's safe local rewrite depend on the host checkout, unavailable // plugins, or a transitive dependency that is itself being migrated. // Import resolution and both execution backends are verified afterwards by // the repository migration suite. let original = crate::parse::expr::with_markup(crate::parse::expr::Markup::Classic, || { crate::parse::parse_source(&source) })?; let classified = crate::lex::classify_source(&source); let mut lines: Vec = source.lines().map(|l| l.to_string()).collect(); for cl in &classified { if cl.kind != crate::lex::LineKind::Code { continue; } let idx = (cl.line_no as usize).saturating_sub(1); let Some(line) = lines.get(idx).cloned() else { continue; }; if let Some(new_loop) = rewrite_loop(&line) { report.loops += 1; lines[idx] = new_loop; continue; } let (new_line, n) = rewrite_code_segments(&line); if n > 0 { report.indexes += n; lines[idx] = new_line; } } if report.indexes == 0 && report.loops == 0 && !mark_all { report.note = Some("no legacy index or loop".into()); return Ok(report); } let mut body = lines.join("\n"); if source.ends_with('\n') { body.push('\n'); } let migrated = add_marker(&body); report.mode_marked = true; // Prove equivalence: the migrated file, parsed in occlusion mode and // denormalized, must be structurally identical to the original. let reparsed = crate::parse::expr::with_markup(crate::parse::expr::Markup::Occlusion, || { crate::parse::parse_source(&migrated) })?; if std::env::var("MARQDO_MIGRATE_DEBUG").is_ok() { eprintln!("ORIG: {}", shape(&original)); eprintln!("MIGR: {}", shape(&reparsed)); } if shape(&original) != shape(&reparsed) { report.note = Some("equivalence check failed — file left untouched".into()); return Ok(report); } if write { std::fs::write(path, &migrated)?; report.applied = true; } Ok(report) } /// Recursively migrate every `*.mq.md` under `path`. pub fn migrate_path(path: &Path, write: bool) -> anyhow::Result> { migrate_path_with_options(path, write, false) } /// Recursive counterpart of [`migrate_file_with_options`]. pub fn migrate_path_with_options( path: &Path, write: bool, mark_all: bool, ) -> anyhow::Result> { let files = crate::migrate::collect_files(path); let mut out = Vec::new(); for f in files { match migrate_file_with_options(&f, write, mark_all) { Ok(r) => out.push(r), Err(e) => out.push(FileReport { file: f, indexes: 0, loops: 0, mode_marked: false, applied: false, note: Some(format!("load failed: {e:#}")), }), } } Ok(out) } /// Rewrite frontmatter one-liner `import bind:target` / `导入 bind:target` into a /// nested YAML map. Returns `(new_source, binding_count)` or `None` if unchanged. pub fn rewrite_import_frontmatter(source: &str) -> Result, String> { let source = source.strip_prefix('\u{feff}').unwrap_or(source); let lines: Vec<&str> = source.lines().collect(); if lines.first().map(|l| l.trim()) != Some("---") { return Ok(None); } let mut fm_end = None; for (i, line) in lines.iter().enumerate().skip(1) { if line.trim() == "---" { fm_end = Some(i); break; } } let Some(fm_end) = fm_end else { return Ok(None); }; let mut keyword: Option<&'static str> = None; let mut entries: Vec<(String, String)> = Vec::new(); let mut legacy_idxs: Vec = Vec::new(); let mut has_nested = false; let mut nested_kw: Option<&'static str> = None; for i in 1..fm_end { let t = lines[i].trim(); // Nested header with empty RHS only (`import:` / `导入:`) if let Some(rest) = t.strip_prefix("import:") { if rest.trim().is_empty() { has_nested = true; nested_kw = Some("import"); continue; } } if let Some(rest) = t.strip_prefix("导入:") { if rest.trim().is_empty() { has_nested = true; nested_kw = Some("导入"); continue; } } if !is_legacy_import_oneliner(t) { continue; } let (kw, after) = if t.starts_with("import") { ("import", t[6..].trim()) } else if let Some(a) = t.strip_prefix("导入") { ("导入", a.trim()) } else { return Err(format!("internal: not a legacy import: {t}")); }; // Pre-existing EN/ZH one-liner mixes: fold under the first keyword seen. if keyword.is_none() { keyword = Some(kw); } let _ = kw; let Some((bind_raw, target_raw)) = after.split_once(':') else { return Err(format!("expected bind:target in `{t}`")); }; let bind = bind_raw.trim().to_string(); let target = target_raw.trim().to_string(); parse_import_binding(&bind, &target).map_err(|e| e.to_string())?; if entries.iter().any(|(b, _)| b == &bind) { return Err(format!("duplicate import bind `{bind}`")); } entries.push((bind, target)); legacy_idxs.push(i); } if entries.is_empty() { return Ok(None); } if has_nested { return Err(format!( "file already has `{}:` block and legacy one-liners — resolve manually", nested_kw.unwrap_or("import") )); } let kw = keyword.unwrap_or("import"); let insert_at = legacy_idxs[0]; let skip: std::collections::HashSet = legacy_idxs.into_iter().collect(); let mut out: Vec = Vec::new(); for (i, line) in lines.iter().enumerate() { if i == insert_at { out.push(format!("{kw}:")); for (bind, target) in &entries { out.push(format!(" {bind}: {target}")); } } if skip.contains(&i) { continue; } out.push((*line).to_string()); } let mut body = out.join("\n"); if source.ends_with('\n') { body.push('\n'); } Ok(Some((body, entries.len()))) } /// Migrate one file's import frontmatter. `write=false` is dry-run. pub fn migrate_imports_file(path: &Path, write: bool) -> anyhow::Result { let source = std::fs::read_to_string(path)?; let mut report = ImportFileReport { file: path.to_path_buf(), bindings: 0, applied: false, note: None, }; match rewrite_import_frontmatter(&source) { Ok(None) => { report.note = Some("no legacy import one-liners".into()); Ok(report) } Err(e) => { report.note = Some(e); Ok(report) } Ok(Some((new_src, n))) => { report.bindings = n; // Soft smoke: nested map must extract the same binding count. let scanned = crate::parse::scan_frontmatter_imports(&new_src); if scanned.len() != n { report.note = Some(format!( "scan mismatch after rewrite: got {} expected {n}", scanned.len() )); return Ok(report); } if write { std::fs::write(path, &new_src)?; report.applied = true; } Ok(report) } } } /// Recursively migrate import one-liners under `path`. pub fn migrate_imports_path(path: &Path, write: bool) -> anyhow::Result> { let files = collect_files(path); let mut out = Vec::new(); for f in files { match migrate_imports_file(&f, write) { Ok(r) => out.push(r), Err(e) => out.push(ImportFileReport { file: f, bindings: 0, applied: false, note: Some(format!("read failed: {e:#}")), }), } } Ok(out) } /// Collect `*.mq.md` under `path` (file → itself). pub fn collect_files(path: &Path) -> Vec { fn walk(dir: &Path, out: &mut Vec) { let Ok(rd) = std::fs::read_dir(dir) else { return; }; for e in rd.flatten() { let p = e.path(); let name = p.file_name().and_then(|n| n.to_str()).unwrap_or(""); if p.is_dir() { if matches!( name, "target" | "dist" | "node_modules" | ".git" | ".marqdo" ) { continue; } walk(&p, out); } else if name.ends_with(".mq.md") { out.push(p); } } } if path.is_file() { return vec![path.to_path_buf()]; } let mut out = Vec::new(); walk(path, &mut out); out.sort(); out } #[cfg(test)] mod tests { use super::{rewrite_import_frontmatter, rewrite_indexes, rewrite_loop}; #[test] fn rewrites_import_oneliners_to_nested() { let src = "---\ntitle: t\nimport json:lib/json.mq.md\nimport fmt:time.format\n---\n\n# main\n"; let (out, n) = rewrite_import_frontmatter(src).unwrap().unwrap(); assert_eq!(n, 2); assert!(out.contains("import:\n json: lib/json.mq.md\n fmt: time.format\n")); assert!(!out.contains("import json:")); } #[test] fn rewrites_static_key() { let (out, n) = rewrite_indexes("**v = [键](`表`)**"); assert_eq!(n, 1); assert_eq!(out, "**v = [键](access(`表`, \"键\"))**"); } #[test] fn rewrites_dynamic_key_without_backticks_in_label() { let (out, n) = rewrite_indexes("**v = [`名`](`表`)**"); assert_eq!(n, 1); assert_eq!(out, "**v = [名](access(`表`, `名`))**"); } #[test] fn rewrites_nested_indexes_inside_out() { let (out, n) = rewrite_indexes("**v = [content]([1]([messages](`req`)))**"); assert_eq!(n, 3); assert_eq!( out, "**v = [content](access([1](access([messages](access(`req`, \"messages\")), \"1\")), \"content\"))**" ); } #[test] fn never_rewrites_already_migrated() { let (out, n) = rewrite_indexes("**v = [键](access(`表`, \"键\"))**"); assert_eq!(n, 0); assert_eq!(out, "**v = [键](access(`表`, \"键\"))**"); } #[test] fn leaves_prose_links_alone() { let (out, n) = rewrite_indexes("see [docs](https://example.com) for more"); assert_eq!(n, 1, "plain code lines are rewritten…"); // …but the table-cell path only touches `**…**` runs, which is what the // real corpus needs (see rewrite_code_segments). let _ = out; } #[test] fn rewrites_loop() { let got = rewrite_loop("- [果](`列`)").unwrap(); assert_eq!(got, "- [果](遍历 `列` 为 果)"); // Already migrated / narrative loops stay untouched. assert!(rewrite_loop("- [果](遍历 `列` 为 果)").is_none()); assert!(rewrite_loop("not a loop").is_none()); } }