开发者

Simple stringLiteral matching fails when using JavaTokenParsers in Scala

开发者 https://www.devze.com 2023-03-23 02:47 出处:网络
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:

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, _) => Ri开发者_如何学JAVAght(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).


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)""""
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号