解析器组合器:如何终止关键字重复
我试图弄清楚如何使用关键字终止单词的重复。一个例子:
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
成功解析。
I'm trying to figure out how to terminate a repetition of words using a keyword. An example:
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
}
When I execute
val caql = new CAQueryLanguage
caql.parseAll(caql.expression, "START one two END")
It prints words: List(one, two, END)
, indicating the words
parser has consumed the END
keyword in my input, leaving the expression parser unable to match. I would like END
to not be matched by words
, which will allow expression
to successfully parse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您要找的吗?
如果您想了解更多详细信息,可以查看 这篇博文
Is this what you are looking for?
If you would like more details, you can check out this blog post