This block only applies to comment, not to Instruction. Use parentheses around (Instruction/comment) to make it apply to both.
> def to_array
> return self.elements.to_array
You don't need to say "self".
"elements" is already an array. Did you mean "elements.map{|e| e.to_array}"?
> end
> }
> end
>
> rule Instruction
> Unary / Tri / Binary
Use parentheses.
> {
> def to_array
> return self.elements.map {|e| e.to_array}
> end
> }
>
> end
>
> rule Unary
> UnaryInst space?
>
> def_to_array
> self.elements[0].to_array # I get an error here saying
> self.elements is nil. But my parser successfully parses the
> instruction. I am unable to call the function beyond this point.
I don't understand that. elements[1] might be nil, because it's optional, but not elements[0].
Normally you should use the element names (or tags) to access the elements, so instead of saying "elements[0]" you can just say "UnaryInst".
Try these things and report back.
Clifford Heath.
> end
> end
>
> rule UnaryInst
> UnaryOp sat?
> {
> def to_array()
> return 1
> end
> }
> end
>
>
> Looks like something basic. Thanks in advance for the help.
>
> --
> You received this message because you are subscribed to the Google Groups "Treetop Development" group.
> To post to this group, send email to treet...@googlegroups.com.
> To unsubscribe from this group, send email to treetop-dev...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/treetop-dev?hl=en.
>
rule
A / B / C { def foo; …; end }
end
defines foo only for the C path. You can even do this:
rule
(A / B / C { def foo; …; end }) { def bar; …; end}
end
which defines foo for C, but bar for all paths. Note that
the two modules will be mixed in to the same SyntaxNode
in this case.
> Another issue I had was in accessing methods for optional non-terminals.
This is a known trap. The accessor method for C is defined
only if the rule was matched. I think this is a bug; the method
should be defined regardless, but return nil if C was not
matched. You can work around it using labels and .empty?,
because the label is always defined.
A fix for this would be welcome, if anyone has time.
Clifford Heath.