rule input
'FIRST LINE'
CLRF
'SECOND LINE'
end
rule CLRF
('\r\n' / '\n\r' / '\r' / '\n' / '\0' ) # <-- what to put here ???
end
Now I declare a variable as:
input = <<-INPUT_END
FIRST_LINE
SECOND_LINE
INPUT_END
and test it:
result = parser.parse(input)
but I can't get the CLRF symbol, why? how to indicate it?
Thanks.
PD: Sorry if this question is an off-topic.
--
Iñaki Baz Castillo
<i...@aliax.net>
I don't know if this helps, but I have my whitespace rules as follows.
# optional space
rule space
white*
end
# mandatory space
rule SPACE
white+
end
# whitespace
rule white
[ \r\t\n]
end
The main difference I can see is that I use [ ... ] whereas you use (
../.../... ) ...
Maybe this does the trick?
cheers
Martin
Completely!!!
I've realized that writting:
'\n'
DOESN'T MATCH a line jump, I must use:
[\n]
So if I need to match a CRLF I cannot do:
'\r\n'
and I must do:
[\r] [\n]
Anyway I don't like having to use "[" "]" since they are used to enter
options. Is there no other ellegant way to set \n \t and so?
Thanks a lot for your useful help ;)
I'm always confused by whitespace handling :-) I don't know of any
other way, but let us know if you find one!
Anyway, I can really recommend having a look at all the examples under
treetop-1.2.3/examples along with the tests! They helped clarify a lot
for me!
cheers
Martin
Here are the basic lexical rules I use, with comments defined for a
C++-like grammar. I hope they help you.
rule id
alpha alphanumeric*
end
rule alpha
[A-Za-z_]
end
rule alphanumeric
alpha / [0-9]
end
rule s # Optional space
S?
end
rule S # Mandatory space
(white / comment_to_eol / comment_c_style)+
end
rule white
[ \t\n\r]+
end
rule comment_to_eol
'//' (!"\n" .)+
end
rule comment_c_style
'/*' (!'*/' . )* '*/'
end
Clifford Heath.
> rule comment_to_eol
> '//' (!"\n" .)+
> end
Please, could you explain a little the above expresion? What does the
"." mean in ".)+" ?
Thanks a lot.
The . matches any character, and !"\n" says "as long as we're not
looking
at a newline...". So this matches any number of characters up to the
next
newline (or end of file).
You can use any rule after ! (not just a character literal) so you
can have
unlimited lookahead before deciding to proceed down the current path.
Clifford Heath.
I was wrong when saying it. I don't need to use:
[\n]
I can use:
"\n"
but cannot use:
'\n'
:)
--
Iñaki Baz Castillo
Right, Ruby-style. I introduced rigourous tests and corrected this
behaviour a
few weeks back, after it was pointed out that #{...} could be used
for injection.
All other character escapes except multi-byte Unicode work the same,
including
the weird Ruby-isms like "\C-M-c"
Clifford Heath.