As I said, it serves to parameterize the parser, but let it go through an example to make it understandable.
Let's start with a simple parser that parses a number, the following word:
def numberAndWord = number ~ word
def number = "\\d+".r
def word = "\\w+".r
RegexParsers , "3 ".
, , "n ". , "3 : , , ". , , .
-, "N"? , repN:
def threeThings = repN(3, word)
" ", ", , ". . repsep, , , . , :
def threeThings = word ~ repN(2, "," ~> word)
, . , , :
def listOfThings = "3" ~ word ~ ":" ~ threeThings
def word = "\\w+".r
def threeThings = word ~ repN(2, "," ~> word)
, , "N" 3. , , . , >>, into (, , flatMap Parser), . threeThings:
def things(n: Int) = n match {
case 1 => word ^^ (List(_))
case x if x > 1 => word ~ repN(x - 1, "," ~> word) ^^ { case w ~ l => w :: l }
case x => err("Invalid repetitions: "+x)
}
, , Parser[List[String]]. ? , :
def listOfThings = number ~ word ~ ":" ~ things(/* what do I put here?*/)
:
def listOfThings = (number ~ word <~ ":") >> {
case n ~ what => things(n.toInt)
}
, , n what: " (, , )", , . :
def listOfThings = (number ~ word <~ ":") >> {
case n ~ what => things(n.toInt) ^^ { list => new ~(n.toInt, new ~(what, list)) }
}
def number = "\\d+".r
def word = "\\w+".r
def things(n: Int) = n match {
case 1 => word ^^ (List(_))
case x if x > 1 => word ~ repN(x - 1, "," ~> word) ^^ { case w ~ l => w :: l }
case x => err("Invalid repetitions: "+x)
}
. , : " flatMap? /? , , !:-) listOfThings:
def listOfThings = for {
nOfWhat <- number ~ word <~ ":"
n ~ what = nOfWhat
list <- things(n.toInt)
} yield new ~(n.toInt, new ~(what, list))
n ~ what <- number ~ word <~ ":", filter withFilter Scala, Parsers. , , :
def listOfThings = for {
n <- number
what <- word
_ <- ":" : Parser[String]
list <- things(n.toInt)
} yield new ~(n.toInt, new ~(what, list))
, , , , " ", - .: -)