Err trait

trunk
Alona EM 2022-06-19 21:16:11 +01:00
parent 11133bc7de
commit e51947e76f
1 changed files with 13 additions and 3 deletions

View File

@ -9,8 +9,16 @@ use codespan_reporting::{
};
use lalrpop_util::{lexer::Token, ParseError};
pub fn report_error<T>(
tree: Result<T, ParseError<usize, Token, &str>>,
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<T, E: SpannedErr>(
tree: Result<T, ParseError<usize, Token, E>>,
file_name: &str,
file_contents: &str,
) -> anyhow::Result<T> {
@ -39,7 +47,9 @@ pub fn report_error<T>(
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)?;