Improve syntax error reports
This commit is contained in:
parent
47c1617602
commit
42f48e8d76
@ -1,6 +1,6 @@
|
|||||||
use std::{io, sync::PoisonError};
|
use std::{io, sync::PoisonError};
|
||||||
|
|
||||||
use chumsky::{prelude::Rich, span::Span};
|
use chumsky::{container::Container, prelude::Rich, span::Span};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
abstract_tree::{SourcePosition, Type},
|
abstract_tree::{SourcePosition, Type},
|
||||||
@ -13,7 +13,7 @@ pub enum Error {
|
|||||||
Parse {
|
Parse {
|
||||||
expected: String,
|
expected: String,
|
||||||
span: (usize, usize),
|
span: (usize, usize),
|
||||||
reason: String,
|
found: Option<String>,
|
||||||
},
|
},
|
||||||
Lex {
|
Lex {
|
||||||
expected: String,
|
expected: String,
|
||||||
@ -45,7 +45,7 @@ impl<'src> From<Rich<'_, Token<'src>>> for Error {
|
|||||||
Error::Parse {
|
Error::Parse {
|
||||||
expected: error.expected().map(|error| error.to_string()).collect(),
|
expected: error.expected().map(|error| error.to_string()).collect(),
|
||||||
span: (error.span().start(), error.span().end()),
|
span: (error.span().start(), error.span().end()),
|
||||||
reason: error.reason().to_string(),
|
found: error.found().map(|token| token.to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ use std::{
|
|||||||
|
|
||||||
use abstract_tree::{AbstractTree, Type};
|
use abstract_tree::{AbstractTree, Type};
|
||||||
use ariadne::{Color, Fmt, Label, Report, ReportKind};
|
use ariadne::{Color, Fmt, Label, Report, ReportKind};
|
||||||
use chumsky::prelude::*;
|
use chumsky::{container::Container, prelude::*};
|
||||||
use context::Context;
|
use context::Context;
|
||||||
use error::{Error, RuntimeError, TypeConflict, ValidationError};
|
use error::{Error, RuntimeError, TypeConflict, ValidationError};
|
||||||
use lexer::lex;
|
use lexer::lex;
|
||||||
@ -112,23 +112,7 @@ impl Interpreter {
|
|||||||
.into_result()
|
.into_result()
|
||||||
.map_err(|errors| InterpreterError {
|
.map_err(|errors| InterpreterError {
|
||||||
source_id: source_id.clone(),
|
source_id: source_id.clone(),
|
||||||
errors: errors
|
errors: errors.into_iter().map(Error::from).collect(),
|
||||||
.into_iter()
|
|
||||||
.map(|error| {
|
|
||||||
let expected = error
|
|
||||||
.expected()
|
|
||||||
.into_iter()
|
|
||||||
.map(|pattern| pattern.to_string())
|
|
||||||
.collect();
|
|
||||||
let span = error.span();
|
|
||||||
|
|
||||||
Error::Parse {
|
|
||||||
expected,
|
|
||||||
span: (span.start(), span.end()),
|
|
||||||
reason: error.reason().to_string(),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
});
|
});
|
||||||
let abstract_tree = match parse_result {
|
let abstract_tree = match parse_result {
|
||||||
Ok(statements) => AbstractTree::new(statements),
|
Ok(statements) => AbstractTree::new(statements),
|
||||||
@ -202,7 +186,7 @@ impl InterpreterError {
|
|||||||
Error::Parse {
|
Error::Parse {
|
||||||
expected,
|
expected,
|
||||||
span,
|
span,
|
||||||
reason,
|
found,
|
||||||
} => {
|
} => {
|
||||||
let description = if expected.is_empty() {
|
let description = if expected.is_empty() {
|
||||||
"Invalid token.".to_string()
|
"Invalid token.".to_string()
|
||||||
@ -210,6 +194,11 @@ impl InterpreterError {
|
|||||||
format!("Expected {expected}.")
|
format!("Expected {expected}.")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let label_message = format!(
|
||||||
|
"{} is not valid in this position.",
|
||||||
|
found.unwrap_or_else(|| String::with_capacity(0))
|
||||||
|
);
|
||||||
|
|
||||||
(
|
(
|
||||||
Report::build(
|
Report::build(
|
||||||
ReportKind::Custom("Parsing Error", Color::Yellow),
|
ReportKind::Custom("Parsing Error", Color::Yellow),
|
||||||
@ -219,7 +208,7 @@ impl InterpreterError {
|
|||||||
.with_message(description)
|
.with_message(description)
|
||||||
.with_label(
|
.with_label(
|
||||||
Label::new((self.source_id.clone(), span.0..span.1))
|
Label::new((self.source_id.clone(), span.0..span.1))
|
||||||
.with_message(reason)
|
.with_message(label_message)
|
||||||
.with_color(Color::Red),
|
.with_color(Color::Red),
|
||||||
),
|
),
|
||||||
None,
|
None,
|
||||||
|
Loading…
Reference in New Issue
Block a user