[antlr-interest] Conditional AST tree construction with custom TreeAdaptor?

Darach Ennis darach at gmail.com
Tue Feb 19 13:00:59 PST 2008


Hi guys,

I've hacked up a variant of TreeAdaptor which allows me to conditionally
'grow' ASTs.
This has turned out to be pretty effective for writing my preprocessor but
with a few
caveats, one in particular:

When in 'skipping' mode, I obviously want to be able to create tokens and
pass them
on to the parser but I don't want all of them, necessarily in my resultant
tree...
so my create looks a little like as follows:

    public Object create(Token arg0) {
        if (isProcessing()) {
            return base.create(arg0);
        }
        return arg0;
    }

However, although the tree being constructed and toStringTree() are correct
all of the created
tokens (even those logically being skipped) are present in the resultant
tree. This isn't so bad
as I can filter out those tokens through setting them on a 'SKIP' channel
with a small change to
my create(...) method, as follows:

    public Object create(Token arg0) {
        if (isProcessing()) {
            return base.create(arg0);
        }
        arg0.setChannel(666);
        return arg0;
    }

This works fairly well but some tokens seem to miss their queue and their
channels are thus
set inappropriately. Is there a better point in the construction of the tree
(eg: addChild) or consuming
of the token stream where the channel can be set more effectively?

My current workaround is to resort to the following sledgehammer action:

{ someToken.setChannel(666); }

This is effective at keeping the token range for a given tree consistent
with intent but litters
the grammar with a few setChannel calls which I'd like to remove. Perhaps I
should be
subclassing/customizing CommonTree also? Has anyone on the list modified
TreeAdaptor
successfully to take on extra duties for specific grammars such as this?

Regards,

Darach.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080219/b48cc363/attachment.html 


More information about the antlr-interest mailing list