Show source names in diagnostics

This commit is contained in:
2026-06-22 18:05:26 +09:00
parent cc5ab63922
commit a2a6dee025
6 changed files with 113 additions and 54 deletions
+24 -26
View File
@@ -4,19 +4,22 @@ use std::{
process::ExitCode,
};
use decodal_core::{Data, Diagnostic, DiagnosticKind, Engine, LoadedSource, SourceLoader, Span};
use decodal_core::{
Data, Diagnostic, DiagnosticKind, Engine, LoadedSource, SourceId, SourceLoader, Span,
format_diagnostic_with,
};
fn main() -> ExitCode {
match run() {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
print_diagnostic(&error);
eprintln!("{error}");
ExitCode::FAILURE
}
}
}
fn run() -> Result<(), Diagnostic> {
fn run() -> Result<(), String> {
let mut args = env::args().skip(1);
let first = args.next().unwrap_or_else(|| String::from("--help"));
match first.as_str() {
@@ -51,12 +54,19 @@ fn run() -> Result<(), Diagnostic> {
}
}
fn materialize_path(path: &str) -> Result<Data, Diagnostic> {
let root = read_root_source(path)?;
fn materialize_path(path: &str) -> Result<Data, String> {
let root = read_root_source(path).map_err(format_raw_diagnostic)?;
let root_name = root.name.clone();
let mut engine = Engine::new(FsLoader);
let module = engine.add_root_source(root.key, root.name, &root.source)?;
let value = engine.eval_module(module)?;
engine.materialize(&value)
let module = engine
.add_root_source(root.key, root.name, &root.source)
.map_err(|error| format_diagnostic_with_root(&error, &root_name))?;
let value = engine
.eval_module(module)
.map_err(|error| engine.format_diagnostic(&error))?;
engine
.materialize(&value)
.map_err(|error| engine.format_diagnostic(&error))
}
fn read_root_source(path: &str) -> Result<LoadedSource, Diagnostic> {
@@ -139,24 +149,12 @@ fn print_help() {
println!(" decodal - Read DCDL source from stdin");
}
fn print_diagnostic(error: &Diagnostic) {
eprintln!(
"error[{kind:?}] {source}:{start}..{end}: {message}",
kind = error.kind,
source = error.span.source.0,
start = error.span.start,
end = error.span.end,
message = error.message,
);
for label in &error.labels {
eprintln!(
" note {source}:{start}..{end}: {message}",
source = label.span.source.0,
start = label.span.start,
end = label.span.end,
message = label.message,
);
}
fn format_diagnostic_with_root(error: &Diagnostic, root_name: &str) -> String {
format_diagnostic_with(error, |source| (source == SourceId(0)).then_some(root_name))
}
fn format_raw_diagnostic(error: Diagnostic) -> String {
format_diagnostic_with(&error, |_| None)
}
fn print_data(data: &Data, indent: usize) {