[antlr-interest] adding enum token types output for java target

Terence Parr parrt at cs.usfca.edu
Mon May 26 12:13:19 PDT 2008


RatherThem a separate file, it seems like the Tokens enough and should  
be generated within the class itself like this:

enum Tokens {
    FUNC_DEF(8);
    T__23(23);
    WS(20);
    CHAR(15);
    EQ(11);
    EOF(-1);
...
    public final int tokenNum;
    public Tokens(int tokenNum) { this.tokenNum = tokenNum; }
}

all I did was add

enum Tokens {
    <tokens:{<it.name>(<it.type>);}; separator="\n">
    public final int tokenNum;
    public Tokens(int tokenNum) { this.tokenNum = tokenNum; }
}

to the output template.

The problem is that we have some duplication; I generate the following  
as well:

     public static final int Inputcharacter=31;
     public static final int COMMA=14;
     public static final int MINUS=21;

So really, it should be a replacement not an addition. That  
unfortunately makes it ickier to reference token types. you would have  
to use Tokens.MINUS instead of MINUS within the code and, externally,  
one would have to use MyParser.Tokens.MINUS. well, I guess it reads  
well, but I'm worried about such a widespread ( albeit simple) change  
to the code generation. each reference to a token type would have to  
have the Tokens. prefix on the front.

Ter


More information about the antlr-interest mailing list