Files
marqdo/src/ast.rs
T
cflmy deac53d0f5 refactor(view): AST 遍历器合流 — walk_stmts / stmt_line 收进 ast
三处各自实现的「深度优先遍历函数体」合并为一个:
- ast::walk_stmts(fun, &mut |stmt| ...):body + 分支臂 + 循环体 + 子函数
- ast::stmt_line(stmt):语句行号(原 view::document 私有副本)

改造:view/annot.rs::fun_stmt_lines、view/document.rs::index_stmt_lines
(annot 的 collect_name_sites 保留自己的收集逻辑,只借遍历)

cargo build 零警告;cargo test --release 609 passed / 0 failed。
view/annot.rs -30 行、view/document.rs -41 行。
2026-10-22 15:44:20 +08:00

449 lines
13 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Marqdo AST (Phase I).
use std::collections::HashMap;
use crate::diagnostics::Span;
use crate::formula::Expr as FormulaExpr;
use crate::value::CodeBlock;
/// Frontmatter file import: `import bind:path.mq.md` (see `doc/design/module-namespace.md`).
#[derive(Debug, Clone)]
pub struct Import {
pub path: String,
pub bind: String,
}
/// Frontmatter short-name bind: `import bind:lib.member` (same keyword as file import).
#[derive(Debug, Clone)]
pub struct Use {
pub path: Vec<String>,
pub bind: String,
}
#[derive(Debug, Clone)]
pub struct Module {
pub imports: Vec<Import>,
pub uses: Vec<Use>,
pub functions: Vec<Function>,
/// Populated by the loader: bind name → imported module (not flattened).
pub import_modules: HashMap<String, Module>,
/// Unresolved Artifact Metadata (from frontmatter); filled by parse.
pub metadata_raw: Vec<(String, crate::binding::MetaValue)>,
/// Bound metadata after load-time Binding resolve (insertion order).
pub metadata: Vec<(String, crate::value::Value)>,
}
impl Default for Module {
fn default() -> Self {
Self {
imports: Vec::new(),
uses: Vec::new(),
functions: Vec::new(),
import_modules: HashMap::new(),
metadata_raw: Vec::new(),
metadata: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct Param {
pub name: String,
pub default: Option<Expr>,
/// True when collected by v0.3 body/prose inference (not an explicit `+` line).
pub inferred: bool,
}
#[derive(Debug, Clone)]
pub struct Function {
pub name: String,
/// Heading depth 1–6. Level 1 = object/type; level ≥ 2 = function/method.
pub level: u8,
pub span: Span,
pub params: Vec<Param>,
pub body: Vec<Stmt>,
pub children: Vec<Function>,
/// Base type name for `# Child = > Parent` (objects only).
pub base: Option<String>,
/// Prose `` `名` `` never read/assigned on the executable surface (not params).
pub dead_binds: Vec<String>,
/// 渐进式契约(T2.1):形参/返回/对象字段契约(附着在文档表上,升参推断前提取)。
pub contract: Option<crate::contract::Contract>,
/// 集合契约(T2.1):变量名 → 字段契约(紧邻 `` `名` = `` 绑定之前的文档表)。
pub var_contracts: HashMap<String, crate::contract::Contract>,
}
impl Function {
/// `# Name` — object / type (constructor body).
pub fn is_object(&self) -> bool {
self.level == 1
}
}
/// Source line of a statement (every variant carries a span).
pub fn stmt_line(st: &Stmt) -> u32 {
match st {
Stmt::Assign { span, .. }
| Stmt::Return { span, .. }
| Stmt::Call { span, .. }
| Stmt::Expr { span, .. }
| Stmt::Branch { span, .. }
| Stmt::While { span, .. }
| Stmt::ForEach { span, .. } => span.line,
}
}
/// Depth-first walk over every statement of a function: its body, nested block
/// bodies (branch arms / loops) and child functions.
///
/// `<TAB>` slimming: three view modules each re-implemented this recursion; the
/// traversal lives here so collectors only supply the per-statement logic.
pub fn walk_stmts(fun: &Function, f: &mut impl FnMut(&Stmt)) {
fn go(st: &Stmt, f: &mut impl FnMut(&Stmt)) {
f(st);
match st {
Stmt::Branch { arms, .. } => {
for arm in arms {
for b in &arm.body {
go(b, f);
}
}
}
Stmt::While { body, .. } | Stmt::ForEach { body, .. } => {
for b in body {
go(b, f);
}
}
_ => {}
}
}
for st in &fun.body {
go(st, f);
}
for c in &fun.children {
walk_stmts(c, f);
}
}
#[derive(Debug, Clone)]
pub enum Stmt {
Assign {
name: String,
value: Expr,
span: Span,
/// Inclusive last source line (covers following `$$` fence or table rows).
end_line: u32,
},
Return {
value: Expr,
span: Span,
},
/// Statement-form call (`> …`); return value discarded.
Call {
call: CallExpr,
span: Span,
},
/// Bold expression statement (`**n + 1**`); value evaluated and discarded.
Expr {
value: Expr,
span: Span,
},
Branch {
arms: Vec<BranchArm>,
span: Span,
},
While {
condition: Expr,
body: Vec<Stmt>,
span: Span,
},
ForEach {
item: String,
collection: String,
/// `<TAB>` M3: occlusion form `- [描述](遍历 集合 为 变量)` carries prose.
label: Option<String>,
body: Vec<Stmt>,
span: Span,
},
}
#[derive(Debug, Clone)]
pub struct BranchArm {
pub condition: Option<Expr>, // None = else
pub body: Vec<Stmt>,
}
#[derive(Debug, Clone)]
pub struct CallExpr {
pub callee: String,
/// Bare library path segments, e.g. `["time","parse"]` for `time.parse`.
/// Mutually exclusive with `receiver` (instance method).
pub path: Option<Vec<String>>,
/// When set, `` `recv`.callee `` method call; receiver is a variable name.
pub receiver: Option<String>,
pub args: Vec<Arg>,
/// Pre-bracket modifiers: `礼貌 [问候] x` → `["礼貌"]`, expanded to `礼貌=True` at eval.
/// See [bracket-call-modifiers.md](../../doc/design/bracket-call-modifiers.md).
pub pre_modifiers: Vec<String>,
/// True when the call was authored with `[callee]` (even with no modifiers).
/// View prefers `修饰 [callee] …` / `[callee] …` over `> callee …` when set.
pub bracket_marked: bool,
}
impl CallExpr {
/// Fold `pre_modifiers` into leading `name=True` named args (conflicts → Err).
pub fn with_modifiers_expanded(&self) -> Result<CallExpr, String> {
let mut call = self.clone();
for m in &call.pre_modifiers {
if call
.args
.iter()
.any(|a| matches!(a, Arg::Named { name, .. } if name == m))
{
return Err(format!(
"pre-bracket modifier `{m}` conflicts with named argument `{m}`"
));
}
}
if call.pre_modifiers.is_empty() {
return Ok(call);
}
let mut args = Vec::with_capacity(call.pre_modifiers.len() + call.args.len());
for m in &call.pre_modifiers {
args.push(Arg::Named {
name: m.clone(),
value: Expr::Literal(Literal::Bool(true)),
});
}
args.append(&mut call.args);
call.args = args;
call.pre_modifiers.clear();
Ok(call)
}
}
#[derive(Debug, Clone)]
pub enum Arg {
Positional(Expr),
Named { name: String, value: Expr },
}
#[derive(Debug, Clone)]
pub enum Expr {
Literal(Literal),
Var(String),
/// Text with embedded `` `var` `` segments.
Interp(Vec<InterpPart>),
Unary {
op: UnaryOp,
expr: Box<Expr>,
},
Binary {
op: BinaryOp,
left: Box<Expr>,
right: Box<Expr>,
},
Call(CallExpr),
List(Vec<Expr>),
/// Object literal / horizontal table (`map` value).
Map(Vec<(String, Expr)>),
/// Collection index: `` [拿铁](菜单) `` / `` [`名`](菜单) `` (preferred), or
/// legacy footnote `` `菜单`[^拿铁] `` / `` `菜单`[^`名`] ``.
Index {
base: Box<Expr>,
/// Key or 1-based list index; see [`IndexKey`].
label: IndexKey,
},
/// Parsed `$$…$$` formula tree (from assignment RHS).
Formula(FormulaExpr),
/// Bound ```lang … ``` fence (`code` value).
Code(CodeBlock),
/// `[描述](实现)` — **occlusion**: `label` is inert prose (never executed,
/// only scanned for keywords and checked for staleness), `payload` is the
/// only thing that runs. `payload: None` means the empty-payload stub
/// (`[某某功能开发中]()`), which evaluates to `None`. `<TAB>` M2
Occluded {
label: String,
payload: Option<Box<Expr>>,
},
}
#[derive(Debug, Clone)]
pub enum InterpPart {
Lit(String),
Var(String),
/// `` `base`[^label] `` — variable followed by footnote index inside interpolated
/// text (chained labels fold into one part, e.g. `` `x`[^a][^b] `` → two labels).
Index { base: String, labels: Vec<String> },
}
/// Map / list index key (`[key](coll)` or legacy `[^…]`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IndexKey {
/// Static text: `[拿铁](…)` / `[^拿铁]` / `[^1]`.
Lit(String),
/// Dynamic: `` [`品名`](…) `` / `` [^`品名`] `` — evaluate variable, then stringify as key.
Var(String),
}
#[derive(Debug, Clone)]
pub enum Literal {
None,
Bool(bool),
Int(i64),
/// Decimal literal (`0.85`, `3.14`).
Num(f64),
Text(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
Not,
Neg,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
Add,
Sub,
Mul,
Div,
Eq,
Ne,
Lt,
Le,
Gt,
Ge,
And,
Or,
}
/// Pretty-print AST for `--dump-ast`.
pub fn format_ast_dump(path: &str, module: &Module) -> String {
let mut out = String::new();
out.push_str(&format!("=== marqdo: ast ({path}) ===\n"));
if !module.imports.is_empty() {
let specs: Vec<String> = module
.imports
.iter()
.map(|i| format!("import {}:{}", i.bind, i.path))
.collect();
out.push_str(&format!("(imports {:?})\n", specs));
}
for fun in &module.functions {
dump_fun(&mut out, fun, 0);
}
out.push_str("=== marqdo: end ast ===\n");
out
}
fn dump_fun(out: &mut String, fun: &Function, depth: usize) {
let pad = " ".repeat(depth);
let kind = if fun.is_object() { "object" } else { "fun" };
let params = format_params(&fun.params);
let dead = if fun.dead_binds.is_empty() {
String::new()
} else {
format!(" dead={:?}", fun.dead_binds)
};
match &fun.base {
Some(base) => out.push_str(&format!(
"{pad}({kind} level={} {:?} extends={base:?} params={params}{dead} @{}\n",
fun.level, fun.name, fun.span
)),
None => out.push_str(&format!(
"{pad}({kind} level={} {:?} params={params}{dead} @{}\n",
fun.level, fun.name, fun.span
)),
}
for stmt in &fun.body {
dump_stmt(out, stmt, depth + 1);
}
for child in &fun.children {
dump_fun(out, child, depth + 1);
}
out.push_str(&format!("{pad})\n"));
}
fn dump_stmt(out: &mut String, stmt: &Stmt, depth: usize) {
let pad = " ".repeat(depth);
match stmt {
Stmt::Assign {
name,
value,
span,
end_line,
} => {
out.push_str(&format!(
"{pad}(assign {name:?} {value:?} @{span}..{end_line})\n"
));
}
Stmt::Return { value, span } => {
out.push_str(&format!("{pad}(return {value:?} @{span})\n"));
}
Stmt::Call { call, span } => {
out.push_str(&format!(
"{pad}(call recv={:?} {:?} {:?} @{span}\n",
call.receiver, call.callee, call.args
));
}
Stmt::Expr { value, span } => {
out.push_str(&format!("{pad}(expr {value:?} @{span})\n"));
}
Stmt::Branch { arms, span } => {
out.push_str(&format!("{pad}(branch @{span}\n"));
for arm in arms {
out.push_str(&format!("{pad} (arm cond={:?}\n", arm.condition));
for s in &arm.body {
dump_stmt(out, s, depth + 2);
}
out.push_str(&format!("{pad} )\n"));
}
out.push_str(&format!("{pad})\n"));
}
Stmt::While {
condition,
body,
span,
} => {
out.push_str(&format!("{pad}(while {condition:?} @{span}\n"));
for s in body {
dump_stmt(out, s, depth + 1);
}
out.push_str(&format!("{pad})\n"));
}
Stmt::ForEach {
item,
collection,
label: _,
body,
span,
} => {
out.push_str(&format!(
"{pad}(foreach {item:?} in {collection:?} @{span}\n"
));
for s in body {
dump_stmt(out, s, depth + 1);
}
out.push_str(&format!("{pad})\n"));
}
}
}
fn format_params(params: &[Param]) -> String {
let parts: Vec<String> = params
.iter()
.map(|p| {
let mut s = p.name.clone();
if p.default.is_some() {
s.push('?');
}
if p.inferred {
s.push_str(" inferred");
}
s
})
.collect();
format!("[{}]", parts.join(", "))
}