Parboiled2 解析器示例
Parboiled2 Parser Example
我正在尝试尝试来自 parboiled2 的这个示例:
scala> class MyParser(val input: org.parboiled2.ParserInput)
extends org.parboiled2.Parser {
def f = rule { capture("foo" ~ push(42))
}
}
defined class MyParser
然后,我创建一个新的 MyParser
,输入 "foo"
。
scala> new MyParser("foo").f
res11: org.parboiled2.Rule[shapeless.HNil,shapeless.::
[Int,shapeless.::[String,shapeless.HNil]]] = null
然而 return 值为 null
。
我如何运行这个简单的f
Rule来自REPL?
Parboiled 2 的 rule
是一个宏,使用 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
,您可以使用另一个 delivery schemes。
我正在尝试尝试来自 parboiled2 的这个示例:
scala> class MyParser(val input: org.parboiled2.ParserInput)
extends org.parboiled2.Parser {
def f = rule { capture("foo" ~ push(42))
}
}
defined class MyParser
然后,我创建一个新的 MyParser
,输入 "foo"
。
scala> new MyParser("foo").f
res11: org.parboiled2.Rule[shapeless.HNil,shapeless.::
[Int,shapeless.::[String,shapeless.HNil]]] = null
然而 return 值为 null
。
我如何运行这个简单的f
Rule来自REPL?
Parboiled 2 的 rule
是一个宏,使用 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
,您可以使用另一个 delivery schemes。