diff --git a/src/lib.rs b/src/lib.rs index c457ce2..73f1fa3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,8 +9,16 @@ use codespan_reporting::{ }; use lalrpop_util::{lexer::Token, ParseError}; -pub fn report_error( - tree: Result>, +pub trait SpannedErr { + // TODO: Give these a default, so that &str can be used as default. + fn lo(&self) -> usize; + fn hi(&self) -> usize; + // TODO: Allow more controll + fn message(&self) -> String; +} + +pub fn report_error( + tree: Result>, file_name: &str, file_contents: &str, ) -> anyhow::Result { @@ -39,7 +47,9 @@ pub fn report_error( ParseError::ExtraToken { token } => Diagnostic::error() .with_message("Extra token") .with_labels(vec![Label::primary(main_file_id, token.0..token.2)]), - ParseError::User { error } => bail!("User parse error: {}", error), + ParseError::User { error } => Diagnostic::error() + .with_message(error.message()) + .with_labels(vec![Label::primary(main_file_id, error.lo()..error.hi())]), }; emit_err(&err)?;