Treetop string example rule failing.

59 views
Skip to first unread message

David Dai

unread,
Feb 16, 2013, 7:37:39 AM2/16/13
to treet...@googlegroups.com
Hi there, I'm trying to parse the an escapable string like in the example here: http://treetop.rubyforge.org/pitfalls_and_advanced_techniques.html.  But for some reasons, it's not parsing.  Here is my code:

Treetop.load_from_string <<-G
grammar S
  rule string
    '"' ('\"' / !'"' .)* '"'
  end
end
G

SParser.new.parse('"Hi there"') # => nil


Can some explain to me why this isn't working?  I'm running Ruby 1.9.3.

markus

unread,
Feb 16, 2013, 5:47:49 PM2/16/13
to treet...@googlegroups.com
David --

> Treetop.load_from_string <<-G
> grammar S
> rule string
> '"' ('\"' / !'"' .)* '"'
> end
> end
> G

The here-doc is eating one level of escaping, so what treetop sees is:

Treetop.load_from_string <<-G
grammar S
rule string
'"' ('"' / !'"' .)* '"'
end
end
G

SParser.new.parse('"Hi there"')

You need to double the "\" to "\\" so that the string passed to treetop
will contain a single "\" as you want:

Treetop.load_from_string <<-G
grammar S
rule string
'"' ('\\"' / !'"' .)* '"'
end
end
G

SParser.new.parse('"Hi there"')

-- MarkusQ


Reply all
Reply to author
Forward
0 new messages