Show source names in diagnostics
This commit is contained in:
@@ -87,6 +87,17 @@ impl<L: SourceLoader> Engine<L> {
|
||||
self.materialize_with_path(value, &mut Vec::new())
|
||||
}
|
||||
|
||||
pub fn source_name(&self, source: SourceId) -> Option<&str> {
|
||||
self.modules
|
||||
.iter()
|
||||
.find(|module| module.source == source)
|
||||
.map(|module| module.name.as_str())
|
||||
}
|
||||
|
||||
pub fn format_diagnostic(&self, diagnostic: &Diagnostic) -> String {
|
||||
format_diagnostic_with(diagnostic, |source| self.source_name(source))
|
||||
}
|
||||
|
||||
fn materialize_with_path(
|
||||
&mut self,
|
||||
value: &RuntimeValue,
|
||||
@@ -1163,6 +1174,39 @@ fn primitive_type(name: &str) -> Option<PrimitiveType> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_diagnostic_with<'a>(
|
||||
diagnostic: &Diagnostic,
|
||||
mut source_name: impl FnMut(SourceId) -> Option<&'a str>,
|
||||
) -> String {
|
||||
let mut out = format!(
|
||||
"{:?} at {}:{}..{}: {}",
|
||||
diagnostic.kind,
|
||||
format_source(diagnostic.span.source, &mut source_name),
|
||||
diagnostic.span.start,
|
||||
diagnostic.span.end,
|
||||
diagnostic.message,
|
||||
);
|
||||
for label in &diagnostic.labels {
|
||||
out.push_str(&format!(
|
||||
"\nnote at {}:{}..{}: {}",
|
||||
format_source(label.span.source, &mut source_name),
|
||||
label.span.start,
|
||||
label.span.end,
|
||||
label.message,
|
||||
));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn format_source<'a>(
|
||||
source: SourceId,
|
||||
source_name: &mut impl FnMut(SourceId) -> Option<&'a str>,
|
||||
) -> String {
|
||||
source_name(source)
|
||||
.map(String::from)
|
||||
.unwrap_or_else(|| format!("source {}", source.0))
|
||||
}
|
||||
|
||||
fn constraint_primary_span(constraints: &[ConstraintEntry]) -> Span {
|
||||
constraints
|
||||
.first()
|
||||
|
||||
@@ -17,7 +17,7 @@ pub use ast::{Ast, BinaryOp, CompareOp, Expr, ExprId, Field, Literal, Param};
|
||||
pub use constraints::normalize_constraints;
|
||||
pub use diagnostic::{Diagnostic, DiagnosticKind, Result};
|
||||
pub use embedding::{HostField, HostValue};
|
||||
pub use eval::Engine;
|
||||
pub use eval::{Engine, format_diagnostic_with};
|
||||
pub use lexer::{Lexer, Token, TokenKind};
|
||||
pub use module::{EmptyLoader, LoadedSource, Module, SourceLoader};
|
||||
pub use parser::{ParseOutput, Parser, SourceForm, parse_source, parse_source_with_source_id};
|
||||
|
||||
Reference in New Issue
Block a user