[antlr-interest] Why does ASTFactory.create(Token tok) creates new token instead of using token passed as a parameter

Andy Tripp antlr at jazillian.com
Thu Mar 23 06:52:24 PST 2006

Vladmir Kvashin wrote:

> Hi,
> Could anybody answer my, why TokenFactory behaves in a following way.
> In method create(Token tok) it creates a new token and then just 
> copies (via initialize() method) values from the token that is passes 
> as a parameter.
>    public AST create(Token tok) {
>        AST t = create(tok.getType());
>        if ( t!=null ) {
>            t.initialize(tok);
>        }
>        return t;
>    }
> My idea is to make my own extension of AstFactory, which would hold 
> the initial token instead of creation new one. Are some drawbacks of 
> such decision? I don't see any.
> If there are no, then probably it's worth to make this behaviour default?
> Thanks,
> Vladimir

My Tokens have extra information. For example, each token knows what 
file it's in. So
reusing a single Token in multiple places won't work for me.

I think it makes more sense to
have create() return a copy, because you can always create a second 
pointer to
the same instance with a simple "AST myCopy = myOriginal;" assignment.


More information about the antlr-interest mailing list