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 case-insensitive literal tokens #13

Merged
merged 2 commits into from
Jul 26, 2023
Merged

Support case-insensitive literal tokens #13

merged 2 commits into from
Jul 26, 2023

Conversation

alllex
Copy link
Owner

@alllex alllex commented Jul 4, 2023

Fixes #9

@alllex alllex mentioned this pull request Jul 4, 2023
Also, support case-insensitive Regex tokens
@alllex
Copy link
Owner Author

alllex commented Jul 26, 2023

I realized that it's possible to use receiver properties as default values for extension functions parameters. This way, the switch to enable case-insensitivity can be located in a single place -- object MyGrammar : Grammar<T>(ignoreCase = true) and functions like Grammar<*>.literalToken would just pick it up.

You can see an example of this in a test:

fun ignoreCaseGrammar() {
object : Grammar<SyntaxTree>(ignoreCase = true) {
val lit by literalToken("a")
val reLit by regexToken("[bc]")
val re by regexToken(Regex("[de]"))
val lam by token { s, i -> if (s[i] == 'f' || (ignoreCase && s[i] == 'F')) 1 else 0 }
val lamStrict by token { s, i -> if (s[i] == 'g') 1 else 0 }
override val root by parser { lexeme(lit) } or parser { lexeme(reLit) } or parser { lexeme(re) } or parser { lexeme(lam) } or parser { lexeme(lamStrict) }
}.run {
assertParsed("a").isEqualTo(lit.lex("a"))
assertParsed("A").isEqualTo(lit.lex("A"))
assertParsed("b").isEqualTo(reLit.lex("b"))
assertParsed("C").isEqualTo(reLit.lex("C"))
assertParsed("D").isEqualTo(re.lex("D"))
assertParsed("e").isEqualTo(re.lex("e"))
assertParsed("f").isEqualTo(lam.lex("f"))
assertParsed("F").isEqualTo(lam.lex("F"))
assertParsed("g").isEqualTo(lamStrict.lex("g"))
assertThat(parseEntire("G")).failedWith(NoMatchingToken(0))
}
}

@alllex alllex merged commit caa9c11 into main Jul 26, 2023
3 checks passed
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

Successfully merging this pull request may close these issues.

Case insensitive matching
1 participant