Add format_errors to Diagnostics
Introduces a new method `format_errors` to the `Diagnostics` struct. This method iterates through all diagnostics, filters for errors, and formats them into a newline-joined string. This simplifies error reporting in compilation results.
This commit is contained in:
@@ -51,4 +51,14 @@ impl Diagnostics {
|
|||||||
identity: id,
|
identity: id,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Formats all error-level diagnostics as a newline-joined string.
|
||||||
|
pub fn format_errors(&self) -> String {
|
||||||
|
self.items
|
||||||
|
.iter()
|
||||||
|
.filter(|d| d.level == DiagnosticLevel::Error)
|
||||||
|
.map(|d| d.message.as_str())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-28
@@ -25,7 +25,7 @@ use crate::ast::types::{
|
|||||||
CommentLine, NativeFunction, NodeIdentity, Purity, SourceLocation, StaticType, Value,
|
CommentLine, NativeFunction, NodeIdentity, Purity, SourceLocation, StaticType, Value,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::ast::diagnostics::{DiagnosticLevel, Diagnostics};
|
use crate::ast::diagnostics::Diagnostics;
|
||||||
pub type PipelineGenerator = Box<dyn FnMut() -> bool>;
|
pub type PipelineGenerator = Box<dyn FnMut() -> bool>;
|
||||||
|
|
||||||
const SYSTEM_LIB_SOURCE: &str = include_str!("rtl/prelude.myc");
|
const SYSTEM_LIB_SOURCE: &str = include_str!("rtl/prelude.myc");
|
||||||
@@ -59,14 +59,7 @@ impl CompilationResult {
|
|||||||
|
|
||||||
pub fn into_result(self) -> Result<TypedNode, String> {
|
pub fn into_result(self) -> Result<TypedNode, String> {
|
||||||
if self.diagnostics.has_errors() {
|
if self.diagnostics.has_errors() {
|
||||||
let errors: Vec<String> = self
|
Err(self.diagnostics.format_errors())
|
||||||
.diagnostics
|
|
||||||
.items
|
|
||||||
.into_iter()
|
|
||||||
.filter(|d| d.level == DiagnosticLevel::Error)
|
|
||||||
.map(|d| d.message)
|
|
||||||
.collect();
|
|
||||||
Err(errors.join("\n"))
|
|
||||||
} else if let Some(ast) = self.ast {
|
} else if let Some(ast) = self.ast {
|
||||||
Ok(ast)
|
Ok(ast)
|
||||||
} else {
|
} else {
|
||||||
@@ -198,12 +191,7 @@ impl MacroEvaluator for RuntimeMacroEvaluator {
|
|||||||
let typed_ast = checker.check(&bound_ast, &[], &mut diag);
|
let typed_ast = checker.check(&bound_ast, &[], &mut diag);
|
||||||
|
|
||||||
if diag.has_errors() {
|
if diag.has_errors() {
|
||||||
return Err(diag
|
return Err(diag.format_errors());
|
||||||
.items
|
|
||||||
.into_iter()
|
|
||||||
.map(|d| d.message)
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let exec_ast = Lowering::lower(Analyzer::analyze(&typed_ast, &self.root_purity.borrow())); // Minimal analysis for macro eval
|
let exec_ast = Lowering::lower(Analyzer::analyze(&typed_ast, &self.root_purity.borrow())); // Minimal analysis for macro eval
|
||||||
@@ -596,12 +584,7 @@ impl Environment {
|
|||||||
let typed_ast_opt = self.compile_pipeline(syntax_ast, &mut diagnostics);
|
let typed_ast_opt = self.compile_pipeline(syntax_ast, &mut diagnostics);
|
||||||
|
|
||||||
if diagnostics.has_errors() || typed_ast_opt.is_none() {
|
if diagnostics.has_errors() || typed_ast_opt.is_none() {
|
||||||
return Err(diagnostics
|
return Err(diagnostics.format_errors());
|
||||||
.items
|
|
||||||
.into_iter()
|
|
||||||
.map(|d| d.message)
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(typed_ast_opt.unwrap())
|
Ok(typed_ast_opt.unwrap())
|
||||||
@@ -839,7 +822,9 @@ impl Environment {
|
|||||||
|
|
||||||
pub fn dump_ast(&self, source: &str) -> Result<String, String> {
|
pub fn dump_ast(&self, source: &str) -> Result<String, String> {
|
||||||
self.preload_dependencies(source, None)?;
|
self.preload_dependencies(source, None)?;
|
||||||
let compiled = self.compile(source).into_result()?;
|
let mut parser = Parser::new(source);
|
||||||
|
let syntax_ast = parser.parse_expression();
|
||||||
|
let compiled = self.compile_syntax(syntax_ast)?;
|
||||||
let linked = self.link(compiled);
|
let linked = self.link(compiled);
|
||||||
Ok(Dumper::dump(&linked))
|
Ok(Dumper::dump(&linked))
|
||||||
}
|
}
|
||||||
@@ -983,12 +968,7 @@ impl Environment {
|
|||||||
let retyped_ast = checker.check_node_as_bound(func_template.ty.original.as_ref(), arg_types, &mut diag);
|
let retyped_ast = checker.check_node_as_bound(func_template.ty.original.as_ref(), arg_types, &mut diag);
|
||||||
|
|
||||||
if diag.has_errors() {
|
if diag.has_errors() {
|
||||||
return Err(diag
|
return Err(diag.format_errors());
|
||||||
.items
|
|
||||||
.into_iter()
|
|
||||||
.map(|d| d.message)
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let analyzed = Analyzer::analyze(&retyped_ast, &root_purity.borrow());
|
let analyzed = Analyzer::analyze(&retyped_ast, &root_purity.borrow());
|
||||||
|
|||||||
Reference in New Issue
Block a user