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

Bug: Lexer returns Err when there is a Token it could have returned #338

Closed
aDotInTheVoid opened this issue Sep 16, 2023 · 3 comments
Closed

Comments

@aDotInTheVoid
Copy link

With logos 0.13.0

use logos::Logos;

#[derive(Debug, PartialEq, Logos)]
pub enum TokenKind {
    #[token(r"0b1")]
    ObiWan,
    #[token(".")]
    Dot,
    #[token("0")]
    Zero,
    #[regex(r"0b[0-1]+\.[0-1]+f")]
    BinFloat,
}

#[test]
fn check() {
    let mut lexer = TokenKind::lexer("0b1.0");
    assert_eq!(lexer.next(), Some(Ok(TokenKind::ObiWan)));
    assert_eq!(lexer.next(), Some(Ok(TokenKind::Dot)));
    assert_eq!(lexer.next(), Some(Ok(TokenKind::Zero)));
    assert_eq!(lexer.next(), None);
}

This should lex 0b1.0 as [ObiWan, Dot, Zero], but instead immedatly errors

thread 'check' panicked at 'assertion failed: `(left == right)`
  left: `Some(Err(()))`,
 right: `Some(Ok(ObiWan))`', src/lib.rs:18:5

Either I don't understand the semantics, or this as a bug somewhere in the derive macro.

@aDotInTheVoid
Copy link
Author

An even simpler case:

use logos::Logos;

#[derive(Debug, PartialEq, Logos)]
pub enum TokenKind {
    #[token(r"AB")]
    AB,
    #[token(r"C")]
    C,
    #[regex(r"A[BC]+D")]
    Regex,
}

#[test]
fn check() {
    let mut lexer = TokenKind::lexer("ABC");
    assert_eq!(lexer.next(), Some(Ok(TokenKind::AB)));
    assert_eq!(lexer.next(), Some(Ok(TokenKind::C)));
    assert_eq!(lexer.next(), None);
}
--- STDERR:              logos-wat check ---
thread 'check' panicked at 'assertion failed: `(left == right)`
  left: `Some(Err(()))`,
 right: `Some(Ok(AB))`', src/lib.rs:16:5

@Skaytacium
Copy link

Same as #315 right? I'm having the same issue.

@aDotInTheVoid
Copy link
Author

Yeah, Closing as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants