Question

So, I am trying to learn me a bit of ruby, a bit of TDD and a bit of Treetop.

I have the following grammar for parsing string literals:

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

And the following test method:

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

The third test (the one with the backslashes) does not pass (the string is not parsed): why?

Thanks!

Was it helpful?

Solution

You need to swap the order of the escaped-quote check:

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

As another example, your grammar would also match this:

"he said, \"

Flipping the check correctly fails that as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top