-
-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add callback to skip #440
Comments
As you said, this is totally possible to do this, but you need to do that with an enum variant. If you have some time, you can try adding support for a callback for All you need (I think), is to modify the parser to also read optional callbacks logos/logos-codegen/src/parser/mod.rs Lines 135 to 143 in 727becf
You can take inspiration for what is done for parsing token / regex definitions: logos/logos-codegen/src/parser/mod.rs Lines 211 to 254 in 727becf
|
I've had a crack at it here |
Looking at erroring, it seems a bit odd that there isn't a callback for the case when no match was found. I think it just uses |
I've done an implementation of the above here |
This is a branch, do you have a concrete example I could read? :-) |
Sure, this is an example a possible error syntax: fn main() {
let input = "1 * 2 - 8 % 4";
for res in Token::lexer(input) {
match res {
Ok(_) => (),
Err(e) => eprintln!("{e}"),
}
}
}
use std::{fmt::Display, ops::Range};
use logos::Logos;
#[derive(Debug, Clone, Default, PartialEq)]
struct Error {
message: String,
index: Range<usize>,
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!{f,
"Error at {:?}: {}",
self.index,
self.message,
}
}
}
#[derive(Logos, Debug)]
#[logos(error = Error)]
#[logos(error_callback = |lex| Error {
message: format!("Invalid character '{}'", lex.slice()),
index: lex.span(),
})]
#[logos(skip(r" "))]
enum Token {
#[regex(r"[\*/+-]")]
Operator,
#[regex("[0-9]+")]
Number
} which prints That example without the enhancement, while not impossible to accomplish, is significantly more clunky - you have to call |
Ok this makes sense, I think this would be a great new feature! |
Should I make a new issue/pull request for this? |
Yes please :-) |
Is it possible to add a callback to skip?
This would be useful if you are trying to count newlines, and you also want to skip them:
I know you could add another variant like
Token::Ignored
, or attach the newline regex to another variant, but both those solutions feel messy, and it'd be nice to have all the skip logic in one place.The text was updated successfully, but these errors were encountered: