Err trait
parent
11133bc7de
commit
e51947e76f
16
src/lib.rs
16
src/lib.rs
|
@ -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)?;
|
||||
|
|
Loading…
Reference in New Issue