我试图弄清楚如何使用关键字终止单词的重复。一个例子:
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
才能成功解析。
这是你想要的?
import scala.util.parsing.combinator.syntactical._
object CAQuery extends StandardTokenParsers {
lexical.reserved += ("START", "END")
lexical.delimiters += (" ")
def query:Parser[Any]= "START" ~> rep1(ident) <~ "END"
def parse(s:String) = {
val tokens = new lexical.Scanner(s)
phrase(query)(tokens)
}
}
println(CAQuery.parse("""START a END""")) //List(a)
println(CAQuery.parse("""START a b c END""")) //List(a, b, c)
如果您想了解更多详细信息,您可以查看这篇博文
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)