3

I'm trying to try out this example from parboiled2:

scala> class MyParser(val input: org.parboiled2.ParserInput) 
            extends org.parboiled2.Parser { 
                def f = rule { capture("foo" ~ push(42)) 
                } 
        }
defined class MyParser

Then, I create a new MyParser with input of "foo".

scala> new MyParser("foo").f
res11: org.parboiled2.Rule[shapeless.HNil,shapeless.::
            [Int,shapeless.::[String,shapeless.HNil]]] = null

Yet the return value is null.

How can I run this simple f Rule from the REPL?

4

1 回答 1

7

Parboiled 2rule是一个宏,使用定义的方法rule不打算在其他规则的上下文之外引用或调用run(). 因此,如果您有以下情况:

import org.parboiled2._

class MyParser(val input: ParserInput) extends Parser {
  def f = rule { capture("foo" ~ push(42)) } 
}

您可以像这样使用它(为清楚起见,清理了类型):

scala>  new MyParser("foo").f.run()
res0: scala.util.Try[Int :: String :: HNil] = Success(42 :: foo :: HNil)

如果您不想要,Try您可以使用其他交付方案之一。

于 2015-03-24T02:13:48.293 回答