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

后端 未结 2 1616
情话喂你
情话喂你 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.

提交回复
热议问题