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

Incorrect matched token slice and span when involving a dash. #461

Open
WindSoilder opened this issue Jan 6, 2025 · 2 comments
Open

Incorrect matched token slice and span when involving a dash. #461

WindSoilder opened this issue Jan 6, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@WindSoilder
Copy link

WindSoilder commented Jan 6, 2025

Given the following code:

use logos::Logos;

#[derive(Logos, Debug, Clone, Copy, PartialEq)]
#[logos(skip r"[ \t]+")]
#[logos(source = [u8])]
pub enum Token {
    #[regex("-?(0[xob])?[0-9][0-9_]*")]
    Int,
    #[token("-")]
    Dash,
}

fn main() {
    let mut lex = Token::lexer(b"-0x");
    assert_eq!(lex.next(), Some(Ok(Token::Dash)));
    assert_eq!(lex.slice(), b"-");   // the slice is `-0`
    assert_eq!(lex.span(), 0..1);    // the span is `0..2`
}

And test failed with the following message:

thread 'main' panicked at src/main.rs:16:5:
assertion `left == right` failed
  left: [45, 48]
 right: [45]
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I think the result is not right in the case, since the lexer correctly say the token is a Dash. It's impossible for the slice containing one more byte.

@jeertmans
Copy link
Collaborator

Hi @WindSoilder, what is the faulty line? I don't see any equality test for [45, 48].

Also, can you include the output of debugged graph? See: https://logos.maciej.codes/debugging.html.

@jeertmans jeertmans added the bug Something isn't working label Jan 6, 2025
@WindSoilder
Copy link
Author

Hi, the faulty line is assert_eq!(lex.slice(), b"-");, b"-" is just a vec![45].

Sure here is the graph:

{
    1: ::<skip> (<skip>),
    2: {
        [09] ⇒ 2,
          ⇒ 2,
        _ ⇒ 1,
    },
    4: ::Int,
    5: {
        [0-9] ⇒ 5,
        _ ⇒ 5,
        _ ⇒ 4,
    },
    6: [0-9] ⇒ 5,
    10: ::Dash,
    11: {
        0 ⇒ 13,
        [1-9] ⇒ 5,
        _ ⇒ 10,
    },
    13: {
        [0-9] ⇒ 5,
        _ ⇒ 5,
        b ⇒ 6,
        o ⇒ 6,
        x ⇒ 6,
        _ ⇒ 4,
    },
    14: {
        [09] ⇒ 2,
          ⇒ 2,
        - ⇒ 11,
        0 ⇒ 13,
        [1-9] ⇒ 5,
    },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants