-
-
Notifications
You must be signed in to change notification settings - Fork 118
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
Assign any other text to a "default" token. #325
Comments
Hello! This used to be the case with prior version of Logos, but now the lexer returns an error With the current Logos version, you could do something like this (not tested): #[derive(Default, Debug, Clone, PartialEq)]
enum LexingError {
/* Add any other variant */
#[default]
InvalidToken,
}
#[derive(Debug, Logos, PartialEq)]
#[logos(error = LexingError)]
#[logos(skip r"[ \t\n\f]+")]
enum Token {
#[token("fast")]
Fast,
#[token(".")]
Period,
Text,
}
fn main {
let mut lex = Token::lexer("Some random fast sentence.");
lex.map(|res| {
if Err(LexingError::InvalidToken) == res {
return Ok(Token::Text);
}
res
}).for_each(|res| { /* do stuff */ });
} |
Could there be an option in the future where you can set a default/fallback? |
Yes I think so :) If you know how to create derive macros, I think you could easily implement this |
I'm actually still a bit of a beginner to rust, and I'm using logos for a project of mine (It was recommended to me by someone on a discord server) But other than this bit this it's very nice, just a little confusing at times since there aren't many examples to follow for certain things. |
Did you check the handbook? I tried to make the documentation a bit more comprehensive :) |
It's much better, but the problem is when you try to use the |
What you did in the json example however is something I hadn't thought of: while let Some(token) = lexer.next() and while it isn't as readable as a simple Adding these examples really helped, thank you. :) |
@jeertmans Would it be possible in the future to create a new macro where you have an option to set a default/fallback? |
I tried this out of curiousity, and it doesn't work either because it will mark every single character as it's own token. So instead of something like |
Yes, this has to be expected somehow. Maybe you should implement your own iterator that takes a |
Basically, I was wondering if it's possible to have it so that when a piece of text doesn't match any other tokens, it is assigned a default token.
For example:
And any text that isn't
fast
or.
would be assigned toText
. I have tried doing this with a regex but it didn't work, perhaps I'm missing something? I'm new to using logos, so maybe I'm just thinking about this incorrectly, but this library has a shocking lack of examples (eg. I wasn't sure how to use a for loop with the lexer)The text was updated successfully, but these errors were encountered: