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

Implement an easy way to get the matched char in a wildcard rule #10

Open
osa1 opened this issue Jun 19, 2021 · 1 comment
Open

Implement an easy way to get the matched char in a wildcard rule #10

osa1 opened this issue Jun 19, 2021 · 1 comment
Labels
feature New feature or request

Comments

@osa1
Copy link
Owner

osa1 commented Jun 19, 2021

Currently getting the matched character in a wildcard is quite verbose (and probably also inefficient):

_ => |mut lexer| {
    let char = lexer.match_().chars().next_back().unwrap();
    ...
}

One easy fix would be to add a char method to lexer that returns the last matched character.

Alternatively with #9 we could allow <char:_> => ... syntax.

@osa1 osa1 added the feature New feature or request label Jun 19, 2021
@osa1
Copy link
Owner Author

osa1 commented Oct 26, 2021

Another example:

let whitespace =
    ['\t' '\n' '\u{B}' '\u{C}' '\r' ' ' '\u{85}' '\u{200E}' '\u{200F}' '\u{2028}' '\u{2029}'];

rule DecInt {
    ($dec_digit | '_')* $int_suffix?,

    $ => |lexer| {
        let match_ = lexer.match_();
        lexer.return_(Token::Lit(Lit::Int(match_)))
    },

    $whitespace => |lexer| {
        let match_ = lexer.match_();
        // TODO: Rust whitespace characters 1, 2, or 3 bytes long
        lexer.return_(Token::Lit(Lit::Int(&match_[..match_.len() - match_.chars().last().unwrap().len_utf8()])))
    },
}

In the last rule we want to exclude the trailing whitespace. We can't just drop the last byte as the allowed whitespace characters can be 1, 2, or 3 bytes long. If we could bind the whitespace character we could do match_[..match.len() - whitespace_clar.len_utf8()].

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

No branches or pull requests

1 participant