Try changing it to a fully qualified reference:
> grammar D
> rule A
> !E . <C>
!E . <F::C>
> end
> end
or import F above/before the grammar.
-- MarkusQ
> --
> 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.
>
>
I'm not clear on what you are trying to accomplish, but can you just
make C a module (that wouldn't inherit from anything but which would be
used to extend a SyntaxNode, for similar effect)?
If not, and if the reason is that you are trying to make the AST
produced by Treetop "represent" the results by comprising objects of
specific classes, my personal advice would be...turn back. That story
never ends well, and at best you will wind up fighting the tools all the
way to produce something brittle and hard to maintain. Instead, have
the AST produce the results you want directly, without trying to make it
be something it isn't.
-- MarkusQ
Markus is right; let the syntax tree be a syntax tree, don't try to
use it as your semantic model.
However, to explain your original question, when you have a
rule (or an alternate) consisting of exactly one non-terminal,
Treetop uses the object that was created for that non-terminal,
instead of making a new sequence of one element. Consequently
your "B" represents an existing object, not a new one, and you
can't extend a class into that object. If it was a terminal, or if you
had another item in the rule, no problem because that requires
a new node. Sometimes I use an empty string to match nothing,
but make a single term into a sequence, in a situation like this:
rule A
( B "" / C D ) <E>
end
If I didn't add the empty string, the B path would fail.
Clifford Heath.
On 19 July 2011 22:30, Clifford Heath <cliffor...@gmail.com> wrote:
> Frank,
>
> Markus is right; let the syntax tree be a syntax tree, don't try to
> use it as your semantic model.
Ditto what I said to Markus.
> However, to explain your original question, when you have a
> rule (or an alternate) consisting of exactly one non-terminal,
> Treetop uses the object that was created for that non-terminal,
> instead of making a new sequence of one element. Consequently
> your "B" represents an existing object, not a new one, and you
That makes more sense.
> can't extend a class into that object. If it was a terminal, or if you
> had another item in the rule, no problem because that requires
> a new node. Sometimes I use an empty string to match nothing,
> but make a single term into a sequence, in a situation like this:
>
> rule A
> ( B "" / C D ) <E>
> end
>
> If I didn't add the empty string, the B path would fail.
I think You lost Me there. But it also might be either the fact it's
late here or My lack of experience with Treetop. On that note, I know
TT has a reputations for being skimpy on documentation but it really
seems more sparse in explanation than imagined. Is there a /thorough/
tutorial on using Treetop?
Actually I think I lost myself :). When I use an empty string, it's not
so I can use a module, but to regularise access (e.g. elements[0]).
In my defence, I'm at home, too sick to work today.
> On that note, I know
> TT has a reputations for being skimpy on documentation but it really
> seems more sparse in explanation than imagined. Is there a /thorough/
> tutorial on using Treetop?
Only what you see, unfortunately, which is what Nathan has written when
the project was orphaned, as updated from time to time mostly by me.
Pull requests are welcome :).
Clifford Heath.
You can, sometimes, but IHMO you shouldn't.
> That was not the impression I got from
> http://thingsaaronmade.com/blog/a-quick-intro-to-writing-a-parser-using-treetop.html
> which explicitly uses class names.
It does work sometimes, but you have to be careful. The rule used to be
"A module for a singleton node, a class for a compound node" but this
required knowing what type of node would be produced, which has gotten
increasingly tricky. That's why I submitted the patch bach around
1.2.5 (https://github.com/MarkusQ/treetop-patch-proposals) which makes
it always accept modules.
So now I always / only use modules.
It always works, and it's easy to remember. :)
-- M
If I can ever understand TT enough to make some, You just might get some. ;-)
Short form: they are like classes except they don't have super classes
and you can't instantiate them. They are modifiers that you can attach
to a class the way adjectives can be attached to a noun.
For more, check any book on ruby, ask google, or take a look at these:
http://www.rubyfleebie.com/an-introduction-to-modules-part-1/
http://www.rubyist.net/~slagell/ruby/modules.html
-- MarkusQ
You can still walk the AST (though it's preferable for the AST to walk
itself).
The difference is that all nodes will be SyntaxNode (not a subclass
which
you instantiated), but with any additional methods from mix-in modules
added.
This is much preferable to using your own classes. For one thing, in the
case where Treetop re-uses a node instead of creating a unary sequence,
you can mix in multiple modules. The modules can even inherit the
behaviour
of earlier modules by calling super.
If you want to avoid pain, add methods to each node to descend the tree
and build your output by recursive descent. Don't write an external
walker
method that traverses the tree.
Clifford Heath.