Heterogeneous AST node types
I just finished adding heterogeneous AST node types to the tree construction. I did not get a response on the mailing list about what people needed, so I build what I thought I would need. The mechanism is extremely simple. If you want a specific node type for a token type, simply switch on the token type inside the TreeAdapter. When you need to use a specific node type for the same token type depending on grammatical context, then ANTLR needs to let you specify that. Here's a simple example that illustrates how to set the node type for token references and string literals:
a : ID<Var> ID<Method> 'main'<Method> ;
This will also work with the rewrite operator. Any token or string literal on the right-hand side can specify the node type, including arguments if necessary:
// imaginary node m : type ID '(' args ')' body -> ^(METHOD<MethodNode> type ID args body) ;
// real node d : type ID ';' -> ^(DECL type ID<VarNode>) ;
Can use literals too:
a : 'int' -> 'int'<TypeNode> ;
Args just get passed to constructor:
b : ID -> ID<VarNode>[3,4] BLORT<IckNode>["hi"];
Can even do in loops:
g : ID+ -> ID<VarNode>+ ;
Cool, eh? This is all still in my dev branch. My intention is that this will be used in rewrites for tree grammars too, but not on left side of -> in tree grammars.
In terms of implementation, ID<Var> becomes new Var(ID) and ID<Var>[foo] becomes new Var(ID, foo) instead of the usual stream_ID.next() etc...