You've asked Treetop to parse "num", which is a
string of digits starting with 1-9. The "space" rule
can never get called, so it's irrelevant. When you
call the parser like this:
> p parser.parse("123")
You get this:
> SyntaxNode+Num1+Num0 offset=0, "123" (value):
> SyntaxNode offset=0, "1"
> SyntaxNode offset=1, "23":
> SyntaxNode offset=1, "2"
> SyntaxNode offset=2, "3"
which is exactly what's expected. The returned top
node from the parse has your value method, but you
haven't called that. Check it:
t = parser.parse("123")
puts t.value
When you call the parser like this:
> p parser.parse(" \n\t\r")
> puts parser.failure_reason
I'm surprised that you get no failure reason. I suspect
that's a bug - but in any case, the parse will fail, because
Treetop is looking for "num" and the input doesn't match
that.
Clifford Heath.
The parser only parses things that match the top (first) rule. You
never call the space rule, so it is never used. Perhaps you wanted
something structured more like this (semantic defs omitted for clarity)?
grammar Arithmetic
rule space_or_num
space / num
end
rule num
[1-9] [0-9]*
end
rule space
[ \t\n\r]+
end
end
-- MarkusQ
> grammar Arithmetic
> rule space_or_num
> space / num
> end
> rule num
> [1-9] [0-9]*
> end
> rule space
> [ \t\n\r]+
> end
> end
>
> -- MarkusQ
Or:
rule num_list
(space / num)+
end
?
Denis
------
la vita e estrany
> Now I come to know that a top rule must include all the sub-rules
> below.
Rather:
It must describe (the pattern of) the whole of what you intend to parse. Further rules define sub-patterns of subsets of this whole.
denis