Scala partner parser, what does >> mean?

I am a little confused about the “→” in scala. Daniel said in Scala xml parser? that it can be used to parameterize the parsing database as a result of the previous analyzer. Can someone give me an example / hint? I already read scaladoc, but still do not understand.

thank

+5
source share
2 answers

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))

, , , , " ", - .: -)

+16

>> , . , .

n + 1 . n . , , n .

, parseInt: Parser[Int]. n, >> n , . , n ( ).

def intLine: Parser[Seq[Int]] = parseInt >> (n => repN(n,parseInt))

1 42
3 1 2 3
0

0 1
1
3 42 42
+5

All Articles