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};
|
use lalrpop_util::{lexer::Token, ParseError};
|
||||||
|
|
||||||
pub fn report_error<T>(
|
pub trait SpannedErr {
|
||||||
tree: Result<T, ParseError<usize, Token, &str>>,
|
// 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_name: &str,
|
||||||
file_contents: &str,
|
file_contents: &str,
|
||||||
) -> anyhow::Result<T> {
|
) -> anyhow::Result<T> {
|
||||||
|
@ -39,7 +47,9 @@ pub fn report_error<T>(
|
||||||
ParseError::ExtraToken { token } => Diagnostic::error()
|
ParseError::ExtraToken { token } => Diagnostic::error()
|
||||||
.with_message("Extra token")
|
.with_message("Extra token")
|
||||||
.with_labels(vec![Label::primary(main_file_id, token.0..token.2)]),
|
.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)?;
|
emit_err(&err)?;
|
||||||
|
|
Loading…
Reference in New Issue