backtracking parsers

This may be obvious in retrospect, but I was having some issues with my citation parser – it wasn’t backtracking through disjunctions properly:

lazy val authors: PackratParser[Authors] = (
      author ~ "," ~ authors |
      author ~ "and" ~ author <~ endOfAuthors |
      author ~ "," ~ etal <~ endOfAuthors |
      author ~ etal <~ endOfAuthors

It turns out that it’s a bad idea to try and match together Packrat and normal parsers if you don’t know what you’re doing (aka, if you’re me). I had a parser definition for names that I was using:

lazy val word: Parser[String] = "[p{L}0-9]+".r

Simply forcing this to be a PackratParser cleared things up:

lazy val word: PackratParser[String] = regex("[p{L}0-9]+".r)

Leave a Reply

Your email address will not be published. Required fields are marked *