-
-
Notifications
You must be signed in to change notification settings - Fork 128
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 callbacks and priority for #[logos(skip)] #441
base: master
Are you sure you want to change the base?
Add callbacks and priority for #[logos(skip)] #441
Conversation
Syntax is same as #[regex] and #[token]: `#[logos(skip(literal, callback = callback, priority = priority))]`
Hi @mysteriouslyseeing, it looks great, thanks for your contribution! Before I review this in depth, could you please:
Thanks! |
Provides type inference for closure argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding tests and book docs!
Before I review other parts of the code, please check my first comments about the failing test :-)
tests/tests/callbacks.rs
Outdated
fn skip_comment<'src>(lexer: &mut Lexer<'src, Token<'src>>) { | ||
let end = lexer | ||
.remainder() | ||
.find("-->") | ||
.unwrap_or(lexer.remainder().len()); | ||
lexer.bump(end + 3); | ||
} | ||
|
||
#[test] | ||
fn skip_callback_function() { | ||
let mut lexer = Token::lexer("<foo> <!-- comment --> <bar>"); | ||
assert_eq!(lexer.next(), Some(Ok(Token::Tag("foo")))); | ||
assert_eq!(lexer.next(), Some(Ok(Token::Tag("bar")))); | ||
assert_eq!(lexer.next(), None); | ||
|
||
let mut lexer = Token::lexer("<foo> <!-- unterminated comment"); | ||
assert_eq!(lexer.next(), Some(Ok(Token::Tag("foo")))); | ||
// Errors not allowed from skips | ||
assert_eq!(lexer.next(), None); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is failing, I think this is because you cannot bump end + 3
if the comment is unterminated. The callback logic should only pay attention to this edge case (which you hopefully tested here :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, I forgot to run cargo test
in ./test
. My bad, I'll fix that now.
tests/tests/callbacks.rs
Outdated
// Errors not allowed from skips | ||
assert_eq!(lexer.next(), None); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it mean the error type is never returned? I am not sure to understand this comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was to do with the other branch, where skips could return errors. I've removed that line
let end = lexer | ||
.remainder() | ||
.find("-->") | ||
.unwrap_or(lexer.remainder().len()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In line with other comment, you can probably fix the text by changing this to .map(|id| id + 3).unwrap_or(lexer.remainer().len());
and bump(end)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
When I run |
To clarify, Clippy complains that pub enum Callback {
Label(TokenStream),
Inline(Box<InlineCallback>),
SkipWithCallback(SkipCallback),
Skip(Span),
} the Now the enum is defined like so: pub enum Callback {
Label(TokenStream),
Inline(Box<InlineCallback>),
Skip(SkipCallback), // note these are swapped
SkipEmpty(Span), // note these are swapped
} which could be confusing to people previously familiar with the codebase. I've committed it like this, but if you'd rather revert to 294a7ab (before any of the switching around of the variants) that's totally fine by me. |
I think this is better to avoid breaking changes when possible (and changing the name of an enum variant is a breaking change), so I prefer reverting to previous name (and IMO it conveys the functionality better), and disable Clippy warning for this variant. |
Closes #440.
Syntax is same as #[regex] and #[token]:
#[logos(skip(literal[, callback[ = callback]][, priority = priority]))]
Also added an example with usage:
A lot of the logic is copied and pasted from Definition - maybe it would be better to refactor them both to use common code? Let me know.