Skip to content

Commit

Permalink
Extend the test that validates token priority is driven by parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
alllex committed Oct 5, 2023
1 parent 915edc8 commit 30669a6
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/commonTest/kotlin/me/alllex/parsus/TokenTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 30669a6

Please sign in to comment.