diff --git a/dust-lang/src/error.rs b/dust-lang/src/error.rs index 9bb0824..5804faa 100644 --- a/dust-lang/src/error.rs +++ b/dust-lang/src/error.rs @@ -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, }, Lex { expected: String, @@ -45,7 +45,7 @@ impl<'src> From>> 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()), } } } diff --git a/dust-lang/src/lib.rs b/dust-lang/src/lib.rs index befa050..96abe86 100644 --- a/dust-lang/src/lib.rs +++ b/dust-lang/src/lib.rs @@ -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,