From da5122358e3c621f4f2bf0a69ade430be144847c Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 6 Mar 2024 16:24:48 -0500 Subject: [PATCH] Add fancy errors --- src/error.rs | 66 ++++++++++++++++++++++++---------------------------- src/main.rs | 10 ++++---- 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/src/error.rs b/src/error.rs index db6295b..20057df 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,53 +1,21 @@ use std::sync::PoisonError; -use ariadne::{Color, Label, Report, ReportKind}; +use ariadne::{Label, Report, ReportKind}; use chumsky::{prelude::Rich, span::SimpleSpan}; use crate::{abstract_tree::Type, lexer::Token}; #[derive(Debug, PartialEq)] pub enum Error { - Parse { - expected: String, - found: Option, - span: SimpleSpan, - }, - Lex { - expected: String, - found: Option, - span: SimpleSpan, - }, + Parse { expected: String, span: SimpleSpan }, + Lex { expected: String, span: SimpleSpan }, Runtime(RuntimeError), } -impl Error { - pub fn report(&self, source: &str) -> Report { - match self { - Error::Parse { - expected, - found, - span, - } => Report::build(ReportKind::Custom("Parsing Error", Color::Red), (), 0).finish(), - Error::Lex { - expected, - found, - span, - } => Report::build(ReportKind::Custom("Lexing Error", Color::Red), (), 0) - .with_label(Label::new(span.start..span.end).with_message(format!( - "Exptected {expected} but found {}.", - found.unwrap_or(' '), - ))) - .finish(), - Error::Runtime(_) => todo!(), - } - } -} - impl From> for Error { fn from(error: Rich<'_, char>) -> Self { Error::Lex { expected: error.expected().map(|error| error.to_string()).collect(), - found: error.reason().found().map(|c| c.clone()), span: error.span().clone(), } } @@ -57,7 +25,6 @@ impl<'src> From>> for Error { fn from(error: Rich<'_, Token<'src>>) -> Self { Error::Parse { expected: error.expected().map(|error| error.to_string()).collect(), - found: error.reason().found().map(|c| c.to_string()), span: error.span().clone(), } } @@ -120,3 +87,30 @@ pub struct TypeCheckError { pub actual: Type, pub expected: Type, } + +pub fn create_report<'a>(errors: &'a [Error]) -> Report<'a> { + let mut report = Report::build(ReportKind::Error, (), 0); + + for error in errors { + match error { + Error::Parse { expected, span } => { + report = report.with_label( + Label::new(span.start..span.end).with_message(format!("Expected {expected}.")), + ); + } + Error::Lex { expected, span } => { + let expected = match expected.as_str() { + "" => "something else", + expected => expected, + }; + + report = report.with_label( + Label::new(span.start..span.end).with_message(format!("Expected {expected}.")), + ); + } + Error::Runtime(_) => todo!(), + } + } + + report.finish() +} diff --git a/src/main.rs b/src/main.rs index 49f9baf..1355189 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use colored::Colorize; use std::{fs::read_to_string, io::Write}; -use dust_lang::{context::Context, Interpreter}; +use dust_lang::{context::Context, error::create_report, Interpreter}; /// Command-line arguments to be parsed. #[derive(Parser, Debug)] @@ -52,10 +52,8 @@ fn main() { println!("{value}") } } - Err(errors) => { - for error in errors { - error.report(&source).eprint(Source::from(&source)).unwrap(); - } - } + Err(errors) => create_report(&errors) + .eprint(Source::from(&source)) + .unwrap(), } }