Skip to content

MismatchedToken error: Token(EOF) is expected, despite requesting the found token of LiteralToken(' ') #10

Description

@aSemy

I would like to parse the following (which is a simplified example of the full text I would like to parse).

Version: 1.2.3
  Features:
  Fixes:

Following Version is a list of category strings. Each category must prefixed with two spaces and suffixed with a colon :.

I'd like to parse it into this class:

data class Demo(
  val version: String,
  val categories: List<String>
)

I have written a parser (see full code below) that takes the leading whitespace into account.

  /** leading category-name whitespace, to be ignored */
  private val categoryNameIndent by literalToken("  ")
  private val categoryNameSuffix by literalToken(":")
  private val categoryName by -categoryNameIndent * text * -categoryNameSuffix

However, I get an error

MismatchedToken(expected=Token(EOF), found=TokenMatch(token=LiteralToken('  '), offset=15, length=2))

This error is very confusing because it seems to have swapped around the expected/found. I didn't expect EOF, while I did expect LiteralToken(' '). And even then, why did the parser not find the literal token? It's hard to figure out, even when debugging, so help would be appreciated.

fun main() {
  val demo = DemoGrammar.parseEntire(
    /* language=text */ """
Version: 1.2.3
  Features:
  Fixes:
""".trimIndent()
  )

  println("parsed demo: $demo")
}


object DemoGrammar : Grammar<Demo>(debugMode = true) {
  private val newline by regexToken("""\n|\r\n|\r""")
  private val text by regexToken(""".+""")

  private val versionTag by literalToken("Version: ")
  private val version by -versionTag * text

  private val categoryNameIndent by literalToken("  ")
  private val categoryNameSuffix by literalToken(":")
  private val categoryName by -categoryNameIndent * text * -categoryNameSuffix

  private val categorySection: Parser<String> by parser {
    println("parsing CategorySection")
    val name = categoryName().text
    println("  name: $name")
    println(newline())
    name
  }

  override val root: Parser<Demo> by parser {
    val version = version().text
    println("  version:$version")
    val categories = repeatZeroOrMore(categorySection)
    repeatZeroOrMore(newline)
    Demo(
      version = version,
      categories = categories,
    )
  }
}

Output:

  version:1.2.3
parsing CategorySection
parsed demo: MismatchedToken(expected=Token(EOF), found=TokenMatch(token=LiteralToken('  '), offset=15, length=2))

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions