我想使用Parboiled来解析一个字符串,该字符串应该可以将相似的源代码转换为不同的类型。
具体地说,我试图将输入的由相同分隔符分隔的单词解析为等价的(List[String], String),其中最后一个单词是元组的第二个元素。
例如,应该将"a.bb.ccc.dd.e"解析为(["a", "bb", "ccc", "dd"], "e")。
我的代码的简化版本如下:
case class Foo(s: String)
case class Bar(fs: List[Foo], f: Foo)
object FooBarParser extends Parser {
val SEPARATOR = "."
def letter: Rule0 = rule { "a" - "z" }
def word: Rule1[String] = rule { oneOrMore(letter) ~> identity }
def foo = rule { word ~~> Foo }
def foos = rule { zeroOrMore(foo, separator = SEPARATOR) }
def bar = foos ~ SEPARATOR ~ foo ~~> Bar
}
object TestParser extends App {
val source = "aaa.bbb.ccc"
val parseResult = ReportingParseRunner(FooBarParser.bar).run(source)
println(parseResult.result)
}这打印了None,很明显,我做错了什么。Parboiled能够解析这个吗?
发布于 2015-09-08 07:00:44
请将您的所有规则包含在'rule‘块中。它在某些情况下是有帮助的
https://stackoverflow.com/questions/26768863
复制相似问题