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

后端 未结 2 1592
情话喂你
情话喂你 2021-01-14 20:06

Please help! I am trying to build a parser to parse SSDP messages as defined in the UPnP protocol. (see \"Discovery\" section)

Basically it\'s a header of HTTP OK,

相关标签:
2条回答
  • 2021-01-14 20:14

    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.

    0 讨论(0)
  • 2021-01-14 20:36

    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)
    0 讨论(0)
提交回复
热议问题