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

Scannerless parsing #23

Merged
merged 12 commits into from
Oct 5, 2023
Prev Previous commit
Next Next commit
Extend the test that validates token priority is driven by parsers
  • Loading branch information
alllex committed Oct 5, 2023
commit 30669a60af56e3e2b0a23101a8842f27e604eeaa
20 changes: 19 additions & 1 deletion src/commonTest/kotlin/me/alllex/parsus/TokenTests.kt
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package me.alllex.parsus

import assertk.assertions.isEqualTo
import me.alllex.parsus.parser.*
import me.alllex.parsus.token.EofToken
import me.alllex.parsus.token.TokenMatch
import me.alllex.parsus.token.literalToken
import me.alllex.parsus.token.regexToken
@@ -25,14 +26,31 @@ class TokenTests {
@Test
fun tokenPriorityIsDrivenByParser() {
object : Grammar<TokenMatch>() {
val single by literalToken("<")
// double declared first
val double by literalToken("<<")
val single by literalToken("<")
override val root by double or single
}.run {
assertParsed("<<").isEqualTo(TokenMatch(double, 0, 2))
}

object : Grammar<TokenMatch>() {
val single by literalToken("<")
val double by literalToken("<<")
// even though single token is declared first, it is not matched first
override val root by double or single
}.run {
assertParsed("<<").isEqualTo(TokenMatch(double, 0, 2))
}

object : Grammar<TokenMatch>() {
val single by literalToken("<")
val double by literalToken("<<")
// if the order in the parser is "wrong", then the parsing will fail too
override val root by single or double
}.run {
assertNotParsed("<<").failedWithUnmatchedToken(EofToken, 1)
}
}

@Test
Loading