3
我想弄清楚如何使用關鍵字終止重複的單詞。舉個例子:解析器組合器:如何終止關鍵字的重複
class CAQueryLanguage extends JavaTokenParsers {
def expression = ("START" ~ words ~ "END") ^^ { x =>
println("expression: " + x);
x
}
def words = rep(word) ^^ { x =>
println("words: " + x)
x
}
def word = """\w+""".r
}
當我執行
val caql = new CAQueryLanguage
caql.parseAll(caql.expression, "START one two END")
它打印words: List(one, two, END)
,說明words
解析器消耗在我輸入關鍵字END
,留下表達式解析器無法匹配。我想END
不匹配words
,這將允許expression
成功解析。
JavaTokenParsers標識符和關鍵字之間沒有區別。我認爲agilefall的答案就是你需要的。 – 2009-10-07 00:53:54