树顶:解析字符串文字?

时间:2011-12-17 16:30:10

标签: ruby treetop

所以,我试图向我学习一些红宝石,一点TDD和一点Treetop。

我有以下语法来解析字符串文字:

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

以下测试方法:

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

第三个测试(带有反斜杠的测试)没有通过(字符串没有被解析):为什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要交换转义报价单的顺序:

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

另一个例子,你的语法也会匹配这个:

"he said, \"

正确翻转支票也失败了。

相关问题