Skip to content

Best way to define lexis rules for ASCII-case-insensitive keywords? #17

Answered by Eliah-Lakhin
jerome-trc asked this question in Q&A
Discussion options

You must be logged in to vote

@jerome-trc The feature is available for review in the issue-18-case-insensitive-grammars branch.

The following code:

use lady_deirdre::lexis::{SourceCode, Token, TokenBuffer};

#[derive(Token, Copy, Clone, PartialEq, Eq, Debug)]
#[repr(u8)]
enum Tok {
    EOI = 0,

    Mismatch = 1,

    #[rule("|")]
    Sep,

    #[rule(i("Foo"))] // Case-insensitive
    Foo,

    #[rule(i("Bar"))] // Case-insensitive
    Bar,

    #[rule("baz")] // Case-sensitive
    Baz,
}

let buf = TokenBuffer::<Tok>::from("foo|Bar|BAR|baz|BAZ");

for chunk in buf.chunks(..) {
    println!("{:?}: {:?}", chunk.token, chunk.string);
}

outputs:

Foo: "foo"
Sep: "|"
Bar: "Bar"
Sep: "|"
Bar: "BAR"
Sep: "|"
Baz: "baz"
Sep:…

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by jerome-trc
Comment options

You must be logged in to vote
2 replies
@Eliah-Lakhin
Comment options

@jerome-trc
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants