[antlr-interest] Minor suggestion re. Java output

Guntis Ozols guntiso at latnet.lv
Sun Feb 3 13:37:50 PST 2008


> A tree is not guaranteed to be any particular type or interface as the
> tree adaptor can produce anything it likes. Hence it must be an object.
> This is explained in the code etc.
>
> Jim

Casting tree once is not that big of a deal, but tons of casting could be
eliminated, plus there is no need for an option, if CommonTree.getChild()
returned CommonTree.

Covariant return types are available since java 1.5 (Sept 2004).

Also, how about using enum for tokens? It would free client code of prefixes in
switch statements, plus toString() would be available and more.

Guntis

> > To make things a little nicer in client code,
> > ANTLR could covariantly specialize return types
> > in return classes. E.g., when you have
> >
> > options {
> >          output = AST;
> >          ASTLabelType=CommonTree;
> > }
> >
> > in the grammar the xxx_return classes in the
> > Parser could have a method
> >
> >     public CommonTree getTree() { return tree; }
> >
> > instead of
> >
> >     public Object getTree() { return tree; }
> >
> > That way we wouldn't need to cast. Since one
> > goal of ANTLR is to emit code that doesn't look
> > too wildly different from what one might have
> > written by hand I think it makes sense to try
> > to make the code more idiomatic.
> >
> > Regards,
> >
> > -- O.L.



More information about the antlr-interest mailing list