feat(cli): marqdo init/run/call/check/inspect/fmt/plugin/lock/sync

Composition root wiring the frontend, runtime, host and package layers:

- Project loader: deterministic project-root discovery, explicit front-matter
  imports only (`@dependency` or relative `.mq.md`), recursive module loading
  with cycle diagnostics, resolved import edges and cross-module call checks.
  Loading never executes anything.
- run/call: sandbox mode enforcement (strict refuses when unavailable),
  explicit capability grants, fuel/stack/output/timeout overrides, typed
  `--arg name=value` constants, program stdout separated from diagnostics, and
  machine-readable JSON output that embeds program output without mixing.
- check: static diagnostics only, human-readable locations with source lines
  and stable machine codes in JSON.
- inspect: canonical AST, heading units, signatures (explicit/inferred),
  executable IR, static reference edges (including cross-file links analysed
  without execution) and platform capability report.
- fmt: conservative, AST-preserving normalisation of line endings and trailing
  newlines; --check mode.
- plugin inspect: manifest/ABI/artifact validation and hash verification,
  never loading plugin code.
- init: minimal project (manifest, entry document, .gitignore).
- lock/sync --locked: deterministic path-dependency lockfile, stale lock or
  changed dependency content is an error (no silent upgrades).
- examples: hello, factorial, math formula, Python foreign call, static
  references and a two-module project with an explicit import.
This commit is contained in:
cflmy committed 2026-11-20 10:44:03 +08:00
1 parent 2e49c7b855
commit e1ba630068
18 files changed
+1803 -28

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
//! Diagnostic rendering and JSON envelopes.
use serde_json::{json, Value as Json};
use crate::loader::FileDiagnostic;
/// Severity name.
fn severity_name(diagnostic: &marqdo_value::Diagnostic) -> &'static str {
match diagnostic.severity {
marqdo_value::Severity::Error => "error",
marqdo_value::Severity::Warning => "warning",
marqdo_value::Severity::Info => "info",
}
}
/// Render diagnostics as human readable text (stderr).
pub fn render_text(diagnostics: &[FileDiagnostic]) -> String {
let mut out = String::new();
for item in diagnostics {
let diagnostic = &item.diagnostic;
let location = match item.line_col() {
Some((line, column)) => format!("{}:{line}:{column}", item.path.display()),
None => item.path.display().to_string(),
};
out.push_str(&format!(
"{}[{}]: {}: {}\n",
severity_name(diagnostic),
diagnostic.code,
location,
diagnostic.message
));
if let Some(line_text) = item.line_text() {
let index = marqdo_span::LineIndex::new(&item.source);
if let Some(span) = diagnostic.span {
let position = index.line_col(span.start);
let caret_offset = position.column.saturating_sub(1);
out.push_str(&format!(" {}\n", line_text.trim_end()));
out.push_str(&format!(
" {}^\n",
" ".repeat(caret_offset.min(line_text.len()))
));
}
}
for related in &diagnostic.related {
let mut message = related.message.clone();
if let Some(span) = related.span {
let index = marqdo_span::LineIndex::new(&item.source);
let position = index.line_col(span.start);
message = format!("{}:{}: {}", position.line, position.column, message);
}
out.push_str(&format!(" note: {message}\n"));
}
for hint in &diagnostic.hints {
out.push_str(&format!(" hint: {hint}\n"));
}
}
out
}
/// Serialize one diagnostic with file/line/column metadata.
pub fn diagnostic_json(item: &FileDiagnostic) -> Json {
let diagnostic = &item.diagnostic;
let (line, column) = item
.line_col()
.map(|(line, column)| (Json::from(line), Json::from(column)))
.unwrap_or((Json::Null, Json::Null));
json!({
"file": item.path.display().to_string(),
"module": item.module_id.0,
"line": line,
"column": column,
"severity": severity_name(diagnostic),
"code": diagnostic.code,
"message": diagnostic.message,
"hints": diagnostic.hints,
})
}
/// Serialize diagnostics.
pub fn diagnostics_json(diagnostics: &[FileDiagnostic]) -> Json {
Json::Array(diagnostics.iter().map(diagnostic_json).collect())
}
/// Print a JSON value to stdout.
pub fn print_json(value: &Json) {
println!(
"{}",
serde_json::to_string_pretty(value).unwrap_or_default()
);
}
/// Count errors.
pub fn error_count(diagnostics: &[FileDiagnostic]) -> usize {
diagnostics
.iter()
.filter(|item| item.diagnostic.is_error())
.count()
}