logos
logos copied to clipboard
No way to propagate error from callback
Consider this example:
fn parse_int(lex: &Lexer<Token>) -> ??? {
lex.slice().parse::<i32>()
}
enum Token {
#[error]
Error,
#[regex("[0-9]+"), parse_int]
Int(i32),
#[regex(...), parse_string_literal]
StringLiteral,
}
Parse errors are converted to Token::Error
which contains no information. There's no way (to my knowledge) to propagate parse_int
or parse_string_literal
error to the caller.
Wanted to second this. I am trying to propagate an error out from one of my token parsers, but it appears there is no way to map the Error token to something else?
As a workaround, you can store extra error information inside the extras
field of the lexer
fn parse_int(lex: &mut Lexer<Token>) -> Result<i32, ()> {
match lex.slice().parse::<i32>() {
Ok(value) => Ok(value),
Err(err) => {
lex.extras.errors.push(err.into());
Err(())
}
}
#[derive(Default)]
struct TokenExtras {
errors: Vec<MyErrorType>,
}
#[derive(Logos)]
#[logos(extras = TokenExtras)]
enum Token {
#[error]
Error,
#[regex("[0-9]+"), parse_int]
Int(i32),
}