Matching a backslash

30 views
Skip to first unread message

Nickolay Kolev

unread,
Oct 13, 2011, 6:57:59 AM10/13/11
to treet...@googlegroups.com
I have a string like this '\x.\y.\z.' and would like to parse it into an array ['x', 'y', 'z']. Here is my first try:

      rule lambdas
        lambda_var rest:(space? lambda_var)* {
          def value
            [lambda_var.value] + rest.elements.map { |e| e.lambda_var.value }
          end
        }
      end

      rule lambda_var
        '\' var_name:[a-z]+ '.' {
          def value
            var_name.text_value
          end
        }
      end

It fails on the backslash however. If I leave out the '\' at the beginning of the lambda_var rule and parse a string like 'x.y.z.' it works as intended.

Does anyone have a hint on what I am doing wrong?

Many thanks in advance!

markus

unread,
Oct 13, 2011, 10:52:07 AM10/13/11
to treet...@googlegroups.com
Nickolay --

Have you tried:

'\\' var_name:[a-z]+ '.' {

-- Markus


Nickolay Kolev

unread,
Oct 14, 2011, 7:19:05 AM10/14/11
to treet...@googlegroups.com
Ah, I see... '\\' works when the grammar is in a standalone file.

It fails when the grammar is inlined in a HEREdoc and loaded with #load_from_string.

Thanks for the hint!

Nickolay

Clifford Heath

unread,
Oct 14, 2011, 9:04:48 PM10/14/11
to treet...@googlegroups.com
On 14/10/2011, at 10:19 PM, Nickolay Kolev wrote:
> Ah, I see... '\\' works when the grammar is in a standalone file.
>
> It fails when the grammar is inlined in a HEREdoc and loaded with #load_from_string.

It fails when some preprocessing has reduced the double-\ to a single one.
In other words, it depends on the kind of heredoc. Just like in Ruby, imagine that!

Treetop's strings have the same rules for single and double quotes as the same
strings in Ruby code.

Clifford Heath.

Reply all
Reply to author
Forward
0 new messages