2011-12-17 49 views
0

所以,我想學習一點紅寶石,一點TDD和一點Treetop。Treetop:解析字符串文字?

我有用於解析字符串文字以下語法:

grammar Str 
    rule string 
    '"' 
    (
     !'"' ./'\"' 
    )* 
    '"' 
    end 
end 

和以下測試方法:

def test_strings 
    assert @parser.parse('"Hi there!"') 
    assert [email protected]('"This is not" valid') 
    assert @parser.parse('"He said, \"Well done!\""') 
end 

第三測試(一個與反斜槓)未通過(串沒有被解析):爲什麼?

謝謝!

回答

1

您需要交換逃脫引號檢查的順序:

(
    '\"'/!'"' . 
)* 

再舉一個例子,你的語法也符合這樣的:

"he said, \" 

翻轉檢查正確的失敗時,以及。

+0

謝謝,就是這樣!有趣的是,我發佈的例子是官方文檔[here](http://treetop.rubyforge.org/pitfalls_and_advanced_techniques.html)中的功能 – Rom1 2011-12-17 17:57:08

+0

只需注意:該示例現在已在Treetop網站中修復。 – Rom1 2011-12-19 11:06:25