在 Scala 中使用 JavaTokenParsers 时,简单的 stringLiteral 匹配失败

发布于 2024-11-27 00:39:45 字数 947 浏览 0 评论 0原文

我刚刚开始研究外部 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2024-12-04 00:39:45

stringLiteral 是这样的:

"I am a string literal because I'm between double quotes"

如果你像下面这样声明输入,它应该可以工作:

val input = "\"ABC\""

然后,在正确的情况下又出现错误:

    r should be === """(ABC)""""

应该被写入

    r should be === """"(ABC)""""

A stringLiteral is something like this:

"I am a string literal because I'm between double quotes"

If you declare input like below, it should work:

val input = "\"ABC\""

Then again, there's an error in the right case:

    r should be === """(ABC)""""

should have been written

    r should be === """"(ABC)""""
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文