I’m toying with Scala’s Parser library. I am trying to write a parser for a format where a length is specified followed by a message of that length. For example:
x.parseAll(x.message, "5helloworld") // result: "hello", remaining: "world"
I’m not sure how to do this using combinators. My mind first goes to:
def message = length ~ body
But obviously body depends on length, and I don’t know how to do that :p
Instead you could just define a message Parser as a single Parser (not combination of Parsers) and I think that is doable (although I haven’t looked if a single Parser can pull several elem?).
Anyways, I’m a scala noob, I just find this awesome 🙂
You should use
intofor that, or its abbreviation,>>:Be careful with precedence when using it. If you add an “~ remainder” after the function above, for instance, Scala will interpret it as
length >> ({ length => ...} ~ remainder)instead of(length >> { length => ...}) ~ remainder.