Pass analyzer tests
This commit is contained in:
parent
e84bb2ea70
commit
de3c83a6f5
File diff suppressed because it is too large
Load Diff
@ -13,7 +13,7 @@ pub enum DustError<'src> {
|
|||||||
source: &'src str,
|
source: &'src str,
|
||||||
},
|
},
|
||||||
Analysis {
|
Analysis {
|
||||||
analysis_error: AnalysisError,
|
analysis_errors: Vec<AnalysisError>,
|
||||||
source: &'src str,
|
source: &'src str,
|
||||||
},
|
},
|
||||||
Parse {
|
Parse {
|
||||||
@ -34,9 +34,9 @@ impl<'src> DustError<'src> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn analysis(analysis_error: AnalysisError, source: &'src str) -> Self {
|
pub fn analysis(analysis_errors: Vec<AnalysisError>, source: &'src str) -> Self {
|
||||||
DustError::Analysis {
|
DustError::Analysis {
|
||||||
analysis_error,
|
analysis_errors,
|
||||||
source,
|
source,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,15 +61,6 @@ impl<'src> DustError<'src> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn position(&self) -> Span {
|
|
||||||
match self {
|
|
||||||
DustError::Runtime { runtime_error, .. } => runtime_error.position(),
|
|
||||||
DustError::Analysis { analysis_error, .. } => analysis_error.position(),
|
|
||||||
DustError::Parse { parse_error, .. } => parse_error.position(),
|
|
||||||
DustError::Lex { lex_error, .. } => lex_error.position(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn source(&self) -> &'src str {
|
pub fn source(&self) -> &'src str {
|
||||||
match self {
|
match self {
|
||||||
DustError::Runtime { source, .. } => source,
|
DustError::Runtime { source, .. } => source,
|
||||||
@ -79,23 +70,27 @@ impl<'src> DustError<'src> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn primary_error_data(&self) -> (&'static str, Span, String) {
|
pub fn error_data(&self) -> Vec<(&'static str, Span, String)> {
|
||||||
(self.title(), self.position(), self.to_string())
|
match self {
|
||||||
}
|
DustError::Runtime { runtime_error, .. } => vec![(
|
||||||
|
"Runtime error",
|
||||||
pub fn secondary_error_data(&self) -> Option<(&'static str, Span, String)> {
|
runtime_error.position(),
|
||||||
if let DustError::Runtime { runtime_error, .. } = self {
|
runtime_error.to_string(),
|
||||||
match runtime_error {
|
)],
|
||||||
RuntimeError::Expression { error, .. } => {
|
DustError::Analysis {
|
||||||
Some(("Expression error", error.position(), error.to_string()))
|
analysis_errors, ..
|
||||||
}
|
} => analysis_errors
|
||||||
RuntimeError::Statement { error, .. } => {
|
.iter()
|
||||||
Some(("Statement error", error.position(), error.to_string()))
|
.map(|error| ("Analysis error", error.position(), error.to_string()))
|
||||||
}
|
.collect(),
|
||||||
_ => None,
|
DustError::Parse { parse_error, .. } => vec![(
|
||||||
|
"Parse error",
|
||||||
|
parse_error.position(),
|
||||||
|
parse_error.to_string(),
|
||||||
|
)],
|
||||||
|
DustError::Lex { lex_error, .. } => {
|
||||||
|
vec![("Lex error", lex_error.position(), lex_error.to_string())]
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,16 +98,7 @@ impl<'src> DustError<'src> {
|
|||||||
let mut report = String::new();
|
let mut report = String::new();
|
||||||
let renderer = Renderer::styled();
|
let renderer = Renderer::styled();
|
||||||
|
|
||||||
let (title, span, label) = self.primary_error_data();
|
for (title, span, label) in self.error_data() {
|
||||||
|
|
||||||
let message = Level::Error.title(title).snippet(
|
|
||||||
Snippet::source(self.source())
|
|
||||||
.annotation(Level::Info.span(span.0..span.1).label(&label)),
|
|
||||||
);
|
|
||||||
|
|
||||||
report.push_str(&format!("{}", renderer.render(message)));
|
|
||||||
|
|
||||||
if let Some((title, span, label)) = self.secondary_error_data() {
|
|
||||||
let message = Level::Error.title(title).snippet(
|
let message = Level::Error.title(title).snippet(
|
||||||
Snippet::source(self.source())
|
Snippet::source(self.source())
|
||||||
.annotation(Level::Info.span(span.0..span.1).label(&label)),
|
.annotation(Level::Info.span(span.0..span.1).label(&label)),
|
||||||
@ -129,7 +115,15 @@ impl Display for DustError<'_> {
|
|||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
DustError::Runtime { runtime_error, .. } => write!(f, "{runtime_error}"),
|
DustError::Runtime { runtime_error, .. } => write!(f, "{runtime_error}"),
|
||||||
DustError::Analysis { analysis_error, .. } => write!(f, "{analysis_error}"),
|
DustError::Analysis {
|
||||||
|
analysis_errors, ..
|
||||||
|
} => {
|
||||||
|
for error in analysis_errors {
|
||||||
|
write!(f, "{error} ")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
DustError::Parse { parse_error, .. } => write!(f, "{parse_error}"),
|
DustError::Parse { parse_error, .. } => write!(f, "{parse_error}"),
|
||||||
DustError::Lex { lex_error, .. } => write!(f, "{lex_error}"),
|
DustError::Lex { lex_error, .. } => write!(f, "{lex_error}"),
|
||||||
}
|
}
|
||||||
|
@ -57,14 +57,16 @@ pub fn run(source: &str) -> Result<Option<Value>, DustError> {
|
|||||||
/// ```
|
/// ```
|
||||||
pub fn run_with_context(source: &str, context: Context) -> Result<Option<Value>, DustError> {
|
pub fn run_with_context(source: &str, context: Context) -> Result<Option<Value>, DustError> {
|
||||||
let abstract_syntax_tree = parse(source)?;
|
let abstract_syntax_tree = parse(source)?;
|
||||||
let analyzer = Analyzer::new(&abstract_syntax_tree, context.clone());
|
let mut analyzer = Analyzer::new(&abstract_syntax_tree, context.clone());
|
||||||
|
|
||||||
analyzer
|
analyzer.analyze();
|
||||||
.analyze()
|
|
||||||
.map_err(|analysis_error| DustError::Analysis {
|
if !analyzer.errors.is_empty() {
|
||||||
analysis_error,
|
return Err(DustError::Analysis {
|
||||||
|
analysis_errors: analyzer.errors,
|
||||||
source,
|
source,
|
||||||
})?;
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let mut vm = Vm::new(abstract_syntax_tree, context);
|
let mut vm = Vm::new(abstract_syntax_tree, context);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user