2011-07-27 40 views
0

我剛開始使用外部DSL,但遇到了問題。我寫了一個非常簡單的測試,我用斯卡拉2.9.0-1和scalatest 1.6.1:在Scala中使用JavaTokenParsers時,簡單字符串匹配失敗

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

任何想法我做錯了什麼?我基本上遵循了Wampler的書(http://ofps.oreilly.com/titles/9780596155957/DomainSpecificLanguages.html)。

回答

2

一個stringLiteral是這樣的:

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

如果聲明輸入類似下面,它應該工作:

val input = "\"ABC\"" 

話又說回來,有一個錯誤在正確的情況下:

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

應該寫成

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

非常感謝!現在當你告訴我這很明顯。 –

相關問題