Having trouble with Scala's “repsep” as seen in parser combinators

久未见 提交于 2019-12-01 11:02:38

After a quick glance, three suggestions:

  • using RegexParsers instead of JavaTokenParsers should be enough if you don't use any of the parsers inside of JavaTokenParser
  • RegexParsers per default skip whitespace; override skipWhitespace to change that behaviour
  • for another option, have a look at parboiled, it has a similar syntax to parser combinators and is quite well documented but IMO is more production ready, has better performance and error reporting (disclaimer: I work with the guy behind parboiled)

As jrudolph said, RegexParsers (and its subclass JavaTokenParsers) skip whitespace by default. You have the option of telling it not to skip whitespace, by overriding skipWhitespace, and you also have the option of telling it what you consider to be whitespace, by overriding the protected val whiteSpace: Regex.

The problem comes from this:

def nameValuePairs:Parser[List[(String, String)]] = repsep(nameValuePair, "\r\n")

Here, \r\n is being skipped automatically, so it is never found. Once you changed skipWhitespace, you got errors because there's an extra \r\n at the end of the file, so it expects to see another nameValuePair.

You might have better luck with this:

def nameValuePairs:Parser[List[(String, String)]] = rep(nameValuePair <~ "\r\n")

Alternatively, you might remove \r\n altogether and let the parser skip whitespace.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!