I am little bit confusing about \">>\" in scala. Daniel said in Scala parser combinators parsing xml? that it could be used to parameterize the parser base on result from previo
The method >>
takes a function that is given the result of the parser and uses it to contruct a new parser. As stated, this can be used to parameterize a parser on the result of a previous parser.
The following parser parses a line with n + 1
integer values. The first value n
states the number of values to follow. This first integer is parsed and then the result of this parse is used to construct a parser that parses n
further integers.
The following line assumes, that you can parse an integer with parseInt: Parser[Int]
. It first parses an integer value n
and then uses >>
to parse n
additional integers which form the result of the parser. So the initial n
is not returned by the parser (though it's the size of the returned list).
def intLine: Parser[Seq[Int]] = parseInt >> (n => repN(n,parseInt))
1 42
3 1 2 3
0
0 1
1
3 42 42