Improve syntax error reports

This commit is contained in:
Jeff 2024-05-18 14:27:42 -04:00
parent 47c1617602
commit 42f48e8d76
2 changed files with 12 additions and 23 deletions

View File

@ -1,6 +1,6 @@
use std::{io, sync::PoisonError};
use chumsky::{prelude::Rich, span::Span};
use chumsky::{container::Container, prelude::Rich, span::Span};
use crate::{
abstract_tree::{SourcePosition, Type},
@ -13,7 +13,7 @@ pub enum Error {
Parse {
expected: String,
span: (usize, usize),
reason: String,
found: Option<String>,
},
Lex {
expected: String,
@ -45,7 +45,7 @@ impl<'src> From<Rich<'_, Token<'src>>> for Error {
Error::Parse {
expected: error.expected().map(|error| error.to_string()).collect(),
span: (error.span().start(), error.span().end()),
reason: error.reason().to_string(),
found: error.found().map(|token| token.to_string()),
}
}
}

View File

@ -14,7 +14,7 @@ use std::{
use abstract_tree::{AbstractTree, Type};
use ariadne::{Color, Fmt, Label, Report, ReportKind};
use chumsky::prelude::*;
use chumsky::{container::Container, prelude::*};
use context::Context;
use error::{Error, RuntimeError, TypeConflict, ValidationError};
use lexer::lex;
@ -112,23 +112,7 @@ impl Interpreter {
.into_result()
.map_err(|errors| InterpreterError {
source_id: source_id.clone(),
errors: errors
.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(),
errors: errors.into_iter().map(Error::from).collect(),
});
let abstract_tree = match parse_result {
Ok(statements) => AbstractTree::new(statements),
@ -202,7 +186,7 @@ impl InterpreterError {
Error::Parse {
expected,
span,
reason,
found,
} => {
let description = if expected.is_empty() {
"Invalid token.".to_string()
@ -210,6 +194,11 @@ impl InterpreterError {
format!("Expected {expected}.")
};
let label_message = format!(
"{} is not valid in this position.",
found.unwrap_or_else(|| String::with_capacity(0))
);
(
Report::build(
ReportKind::Custom("Parsing Error", Color::Yellow),
@ -219,7 +208,7 @@ impl InterpreterError {
.with_message(description)
.with_label(
Label::new((self.source_id.clone(), span.0..span.1))
.with_message(reason)
.with_message(label_message)
.with_color(Color::Red),
),
None,