[antlr-interest] overriding CommonTree.dupNode()

Andy Tripp antlr at jazillian.com
Tue Aug 7 15:10:25 PDT 2007


I just spent a lot of time tracking down why I was getting a few 
CommonTree objects
in my AST even though I'm using my own FancyTree class and a 
FancyTreeAdaptor.
As I'm sure Terrence would guess, I didn't realize I had to override 
CommonTree.dupNode().

I see CommonTreeAdaptor.dupNode() apologizes for not doing better, 
saying in a comment that it could
use reflection, but that would be slow. Well, I grudgingly accept the 
apology, but still would still
like to see a little more effort :) How about adding this to 
CommonTreeAdaptor.dupNode():

if (treeNode instanceof CommonNode) {
    return create(((CommonNode) treeNode).getToken());
}

In this way, one isn't forced to override CommonTree.dupNode() in 
addition to CommonTree.create().

If not, at least change the book (p170 of the PDF version) to say that 
you need to override both
dupNode and create.

Andy


More information about the antlr-interest mailing list