Skip to content
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

Support callbacks returning Result<Skip> #432

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Logos can handle callbacks with following return types:
| `Option<T>` | `Ok(Token::Value(T))` **or** `Err(<Token as Logos>::Error::default())` |
| `Result<T, E>` | `Ok(Token::Value(T))` **or** `Err(<Token as Logos>::Error::from(err))` |
| [`Skip`](https://docs.rs/logos/latest/logos/struct.Skip.html) | _skips matched input_ |
| `Result<Skip, E>` | _skips matched input_ **or** `Err(<Token as Logos>::Error::from(err))` |
| [`Filter<T>`](https://docs.rs/logos/latest/logos/enum.Filter.html) | `Ok(Token::Value(T))` **or** _skips matched input_ |
| [`FilterResult<T, E>`](https://docs.rs/logos/latest/logos/enum.FilterResult.html) | `Ok(Token::Value(T))` **or** `Err(<Token as Logos>::Error::from(err))` **or** _skips matched input_ |

Expand Down
19 changes: 19 additions & 0 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ impl<'s, T: Logos<'s>> CallbackResult<'s, (), T> for Skip {
}
}

impl<'s, E, T: Logos<'s>> CallbackResult<'s, (), T> for Result<Skip, E>
where
E: Into<T::Error>,
{
#[inline]
fn construct<Constructor>(self, _: Constructor, lex: &mut Lexer<'s, T>)
where
Constructor: Fn(()) -> T,
{
match self {
Ok(_) => {
lex.trivia();
T::lex(lex);
}
Err(err) => lex.set(Err(err.into())),
}
}
}

impl<'s, P, T: Logos<'s>> CallbackResult<'s, P, T> for Filter<P> {
#[inline]
fn construct<Constructor>(self, c: Constructor, lex: &mut Lexer<'s, T>)
Expand Down
46 changes: 45 additions & 1 deletion tests/tests/callbacks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use logos::{Lexer, Logos as _};
use logos::{Lexer, Logos as _, Skip};
use logos_derive::Logos;
use tests::assert_lex;

Expand Down Expand Up @@ -172,3 +172,47 @@ mod any_token_callback {
);
}
}

mod return_result_skip {
use super::*;

#[derive(Debug, Default, PartialEq, Clone)]
enum LexerError {
UnterminatedComment,
#[default]
Other,
}

#[derive(Logos, Debug, PartialEq)]
#[logos(skip r"[ \t\n\f]+")]
#[logos(error = LexerError)]
enum Token<'src> {
#[regex(r"<[a-zA-Z0-9-]+>", |lex| &lex.slice()[1..lex.slice().len()-1])]
Tag(&'src str),

#[token("<!--", skip_comment)]
Comment,
}

fn skip_comment<'src>(lexer: &mut Lexer<'src, Token<'src>>) -> Result<Skip, LexerError> {
let end = lexer
.remainder()
.find("-->")
.ok_or(LexerError::UnterminatedComment)?;
lexer.bump(end + 3);

Ok(Skip)
}

#[test]
fn return_result_skip() {
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"))));
assert_eq!(lexer.next(), Some(Err(LexerError::UnterminatedComment)));
}
}
Loading