Scala: Can I nudge a combinator parser to be locally greedy?

余生颓废 提交于 2019-12-05 02:23:21

Use |||:

object Example extends JavaTokenParsers {
  def obj: Parser[Any] = (shortchain ||| longchain) ~ anyrep

  def longchain: Parser[Any] = zero~zero~one~one
  def shortchain: Parser[Any] = zero~zero

  def anyrep: Parser[Any] = rep(any)
  def any: Parser[Any] = zero | one
  def zero: Parser[Any] = "0"
  def one: Parser[Any] = "1"
  def main(args: Array[String]) {
    println(parseAll(obj, args(0) ))
  }
}

scala> Example.main(Array("001111"))
[1.7] parsed: ((((0~0)~1)~1)~List(1, 1))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!