在 Scala 中使用 JavaTokenParsers 时,简单的 stringLiteral 匹配失败
我刚刚开始研究外部 DSL,但是遇到了一个问题。我编写了一个非常简单的测试,我使用 Scala 2.9.0-1 和 scalatest 1.6.1:
class DSLTest extends FlatSpec with ShouldMatchers {
object DSL extends JavaTokenParsers {
def test = stringLiteral
def apply(s: String): Either[String, String] = parseAll(test, s) match {
case Success(tree, _) => Right(tree.toString)
case NoSuccess(msg, _) => Left("Bad syntax: " + msg)
}
}
"DSL" should "parse ABC" in {
val input = "ABC"
DSL(input) match {
case Right(r) =>
r should be === """(ABC)""""
case Left(msg) =>
fail(msg)
}
}
}
如果我运行它,它会在解析过程中失败并返回:
Bad syntax: string matching regex `"([^"\p{Cntrl}\\]|\\[\\/bfnrt]|\\u[a-fA-F0-9]{4})*"' expected but `A' found
你知道我做错了什么吗?我基本上遵循 Dean Wampler 的书(http://ofps.oreilly.com/titles/9780596155957/DomainSpecificLanguages.html)。
I've just started working on an external DSL, however I've run into an issue. I've written a very simple test, I use Scala 2.9.0-1 and scalatest 1.6.1:
class DSLTest extends FlatSpec with ShouldMatchers {
object DSL extends JavaTokenParsers {
def test = stringLiteral
def apply(s: String): Either[String, String] = parseAll(test, s) match {
case Success(tree, _) => Right(tree.toString)
case NoSuccess(msg, _) => Left("Bad syntax: " + msg)
}
}
"DSL" should "parse ABC" in {
val input = "ABC"
DSL(input) match {
case Right(r) =>
r should be === """(ABC)""""
case Left(msg) =>
fail(msg)
}
}
}
If I run it, it fails during parsing and returns:
Bad syntax: string matching regex `"([^"\p{Cntrl}\\]|\\[\\/bfnrt]|\\u[a-fA-F0-9]{4})*"' expected but `A' found
Any ideas what I did wrong? I basically followed Dean Wampler's book (http://ofps.oreilly.com/titles/9780596155957/DomainSpecificLanguages.html).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
stringLiteral
是这样的:如果你像下面这样声明输入,它应该可以工作:
然后,在正确的情况下又出现错误:
应该被写入
A
stringLiteral
is something like this:If you declare input like below, it should work:
Then again, there's an error in the right case:
should have been written