test: validate executable knowledge syntax across libraries
This commit is contained in:
1 parent
b624084745
commit
fc7097bae4
11 files changed
+307
-17
No files matched your search
+110
@@ -367,6 +367,16 @@ enum KnowledgeAction {
|
||||
#[arg(value_name = "SYMBOL")]
|
||||
symbol: String,
|
||||
},
|
||||
/// Resolve a new-style knowledge path in the import scope of a source file
|
||||
Resolve {
|
||||
/// `mq://…` URI or a value-path such as `facts.Person.birth_year`
|
||||
#[arg(value_name = "REFERENCE")]
|
||||
reference: String,
|
||||
|
||||
/// Source file whose frontmatter imports supply the path's first segment
|
||||
#[arg(long, value_name = "FILE")]
|
||||
from: Option<PathBuf>,
|
||||
},
|
||||
/// Run engineering preflight for a task
|
||||
Preflight {
|
||||
#[arg(value_name = "TASK")]
|
||||
@@ -497,6 +507,70 @@ fn main() -> ExitCode {
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve the value-path spelling introduced for executable knowledge without
|
||||
/// evaluating it. The import bind comes from the caller's source file, so
|
||||
/// `math.pi` never degrades into a project-wide name guess.
|
||||
fn resolve_knowledge_reference<'a>(
|
||||
index: &'a marqdo::sema::ProjectIndex,
|
||||
reference: &str,
|
||||
from: Option<&std::path::Path>,
|
||||
) -> Result<&'a marqdo::sema::SymbolDecl> {
|
||||
if reference.starts_with("mq://") {
|
||||
return index
|
||||
.symbols()
|
||||
.iter()
|
||||
.find(|symbol| symbol.id.as_str() == reference)
|
||||
.ok_or_else(|| anyhow::anyhow!("unknown symbol URI `{reference}`"));
|
||||
}
|
||||
|
||||
let expression = marqdo::parse::expr::parse_expr_prefer_var(reference)
|
||||
.map_err(|error| anyhow::anyhow!("invalid knowledge reference `{reference}`: {error}"))?;
|
||||
let marqdo::ast::Expr::Call(call) = expression else {
|
||||
anyhow::bail!("knowledge reference `{reference}` must be a dotted value path or mq:// URI");
|
||||
};
|
||||
if !call.args.is_empty() {
|
||||
anyhow::bail!("knowledge resolve only binds a zero-argument path; `{reference}` has arguments");
|
||||
}
|
||||
let path = call.path.ok_or_else(|| {
|
||||
anyhow::anyhow!("knowledge reference `{reference}` must start with an imported module bind")
|
||||
})?;
|
||||
if path.len() < 2 {
|
||||
anyhow::bail!("knowledge reference `{reference}` needs at least `module.symbol`");
|
||||
}
|
||||
let from = from.ok_or_else(|| {
|
||||
anyhow::anyhow!("`--from FILE` is required to resolve imported path `{reference}`")
|
||||
})?;
|
||||
let module = marqdo::load::load_module(from)?;
|
||||
let imported = module
|
||||
.imports
|
||||
.iter()
|
||||
.find(|item| item.bind == path[0])
|
||||
.ok_or_else(|| anyhow::anyhow!("unknown import bind `{}` in {}", path[0], from.display()))?;
|
||||
let base = from.parent().unwrap_or_else(|| std::path::Path::new("."));
|
||||
let imported_path = marqdo::load::resolve_import(base, &imported.path)?;
|
||||
let imported_path = imported_path.canonicalize().unwrap_or(imported_path);
|
||||
let module_id = index
|
||||
.modules()
|
||||
.iter()
|
||||
.find_map(|(module_id, source)| {
|
||||
(source.canonicalize().unwrap_or_else(|_| source.clone()) == imported_path)
|
||||
.then_some(module_id)
|
||||
})
|
||||
.ok_or_else(|| {
|
||||
anyhow::anyhow!(
|
||||
"import `{}` resolves to {}, which is outside the indexed project",
|
||||
path[0],
|
||||
imported_path.display()
|
||||
)
|
||||
})?;
|
||||
let declaration_path = &path[1..];
|
||||
index
|
||||
.symbols()
|
||||
.iter()
|
||||
.find(|symbol| symbol.module == *module_id && symbol.declaration_path == declaration_path)
|
||||
.ok_or_else(|| anyhow::anyhow!("unknown knowledge path `{reference}`"))
|
||||
}
|
||||
|
||||
/// Recursively collect `*.mq.md` under `path` (file → itself).
|
||||
fn collect_mq_files(path: &std::path::Path) -> Result<Vec<std::path::PathBuf>> {
|
||||
fn walk(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
|
||||
@@ -994,6 +1068,42 @@ fn try_main(cli: Cli) -> Result<i32> {
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
KnowledgeAction::Resolve { reference, from } => {
|
||||
let from = from.as_deref().or_else(|| path.is_file().then_some(path.as_path()));
|
||||
let index = marqdo::sema::ProjectIndex::build(if path.is_dir() {
|
||||
marqdo::project::ProjectRoot::new(&path)?
|
||||
} else {
|
||||
marqdo::project::ProjectRoot::discover(&path)?
|
||||
})?;
|
||||
let symbol = resolve_knowledge_reference(&index, &reference, from)?;
|
||||
let instance_only = symbol.callable
|
||||
== Some(marqdo::sema::CallableKind::InstanceMethod);
|
||||
if json {
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::json!({
|
||||
"reference": reference,
|
||||
"resolution": if instance_only { "instance_only" } else { "symbol" },
|
||||
"symbol": {
|
||||
"id": symbol.id.as_str(),
|
||||
"module": symbol.module.as_str(),
|
||||
"kind": symbol.kind.as_str(),
|
||||
"callable": symbol.callable.map(|kind| kind.as_str()),
|
||||
"declaration_path": symbol.declaration_path,
|
||||
"source": { "file": symbol.source, "line": symbol.line },
|
||||
},
|
||||
})
|
||||
);
|
||||
} else {
|
||||
println!("{}", symbol.id);
|
||||
println!(" {}", symbol.declaration_path.join("."));
|
||||
println!(" {}:{}", symbol.source.display(), symbol.line);
|
||||
if instance_only {
|
||||
println!(" instance_only: call through an instance receiver");
|
||||
}
|
||||
}
|
||||
Ok(if instance_only { 1 } else { 0 })
|
||||
}
|
||||
KnowledgeAction::Preflight {
|
||||
task,
|
||||
min_candidates,
|
||||
|
||||
+27
-10
@@ -686,16 +686,22 @@ impl<'a> Parser<'a> {
|
||||
// A dotted definition path in a value position is a zero-argument
|
||||
// call. This makes a documented fact such as `facts.Person.birth_year`
|
||||
// executable without ceremonial `()`.
|
||||
if let Some(path) = split_lib_path_callee(&word) {
|
||||
let callee = path.last().cloned().unwrap_or_default();
|
||||
return Ok(Expr::Call(CallExpr {
|
||||
callee,
|
||||
path: Some(path),
|
||||
receiver: None,
|
||||
args: Vec::new(),
|
||||
pre_modifiers: Vec::new(),
|
||||
bracket_marked: false,
|
||||
}));
|
||||
// Classic expression contexts intentionally preserve bare dotted
|
||||
// text such as `refunds.md` in branch conditions. The new path
|
||||
// spelling is enabled only on the executable value surface
|
||||
// (`*…*` / `**…**`), where bare words already mean bindings.
|
||||
if self.prefer_var {
|
||||
if let Some(path) = split_lib_path_callee(&word) {
|
||||
let callee = path.last().cloned().unwrap_or_default();
|
||||
return Ok(Expr::Call(CallExpr {
|
||||
callee,
|
||||
path: Some(path),
|
||||
receiver: None,
|
||||
args: Vec::new(),
|
||||
pre_modifiers: Vec::new(),
|
||||
bracket_marked: false,
|
||||
}));
|
||||
}
|
||||
}
|
||||
self.skip_ws();
|
||||
if self.starts_with("[^") {
|
||||
@@ -2104,6 +2110,17 @@ mod bracket_call_tests {
|
||||
assert!(c.args.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classic_dotted_text_is_not_an_implicit_path_call() {
|
||||
// Branch conditions and other classic contexts use bare dotted text
|
||||
// for filenames such as `refunds.md`; new paths live on the
|
||||
// executable value surface only.
|
||||
assert!(matches!(
|
||||
parse_expr("refunds.md").unwrap(),
|
||||
Expr::Literal(Literal::Text(value)) if value == "refunds.md"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paren_call_nested_in_expression() {
|
||||
let c = call_of(parse_expr("加(加(1 2), 3)").unwrap());
|
||||
|
||||
Reference in new issue
Block a user