2014-11-05 35 views
1

我想使用Parboiled來解析應該將一個類似的源變成不同類型的字符串。使用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能夠解析這個?

+0

看起來像你正在使用Parboiled1。你可以使用Parboiled2,它不會解決你的問題,但是..你會獲得更好的工具。 – ppopoff 2015-09-07 22:58:33

回答

0

請將所有規則包裹在'規則'區塊內。它有助於在某些情況下

相關問題