[antlr-interest] Proposal: Change Java templates to eliminate redundant casts based on labelType

Austin Hastings Austin_Hastings at Yahoo.com
Fri Oct 19 09:20:56 PDT 2007


I'm not sure who is maintaining the Java codegen templates, so this goes 
to everybody.

I'd like to eliminate the redundant cast warnings in the java templates.

Currently there is a labelType parameter, declared: labelType = "Token" 
which I think specifies a default value.

The labelType gets used in two ways. It can be a type in its own right, 
or a type name in a cast:

<labelType> next_token = (<labelType>) input.LT(1);

I think the type cast can be replaced by a 'function' that returns a 
cast if the labelType is set:

<if(labelType)>(<labelType>)<endif>

Similarly, the declaration side could be handled by replacing the 
"default value" by a condition:

<if(labelType)><labelType><else>Token<endif>

I believe this would clean up the redundant casts of LT(1) to (Token) 
that appear in nearly every generated rule.

=Austin



More information about the antlr-interest mailing list