You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Following up from discussion in #440:
The current implementation of a token's Error type makes use of the Default constructor. This works fine as a default, but you run into issues if you want the error to reflect the current span of the Lexer, for example, to provide users of a lexer an indication of where exactly there was an error. It's not impossible - currently, a solution is to provide an arbitrary token variant with an attribute like the following:
use logos::Logos;#[derive(Logos)]#[logos(error = String)]enumToken{#[token("a", priority = 1)]A,#[token("b", priority = 1)]#[regex(".", callback = |lex| {Err::<(), String>(format!("Syntax error at {:?}: unrecognised character '{}'", lex.span(), lex.slice()))})]B,}
Note that you have to add priority = 1 to both A and B because "." also matches "a" and "b", and you also have to specify the associated type of the result because Rust cannot infer it.
A solution is to allow users to provide a default constructor for Error, to be used instead of Default::default() with an attribute like #[logos(error_callback = ...)], or something similar. The previous example with this would look like this:
use logos::Logos;#[derive(Logos)]#[logos(error = String)]#[logos(error_callback = |lex| { format!("Syntax error at {:?}: unrecognised character '{}'", lex.span(), lex.slice())})]enumToken{#[token("a")]A,#[token("b")]B,}
The text was updated successfully, but these errors were encountered:
Following up from discussion in #440:
The current implementation of a token's Error type makes use of the Default constructor. This works fine as a default, but you run into issues if you want the error to reflect the current span of the Lexer, for example, to provide users of a lexer an indication of where exactly there was an error. It's not impossible - currently, a solution is to provide an arbitrary token variant with an attribute like the following:
Note that you have to add priority = 1 to both A and B because "." also matches "a" and "b", and you also have to specify the associated type of the result because Rust cannot infer it.
A solution is to allow users to provide a default constructor for Error, to be used instead of
Default::default()
with an attribute like#[logos(error_callback = ...)]
, or something similar. The previous example with this would look like this:The text was updated successfully, but these errors were encountered: