[antlr-interest] Source positions for imaginary tokens

Juancarlo Añez apalala at gmail.com
Tue Sep 11 06:08:44 PDT 2012


On Tue, Sep 11, 2012 at 12:43 AM, Guus Bonnema <gbonnema at xs4all.nl> wrote:

> As far as I know an imaginary token as root will only have position
> information if you copy the information from a real token.
>
> For instance like this: lb=LBRACE block RBRACE ^(DUM[$lb,"SLIST"] block)
>


That's what I had read. But when I started adding the copy constructs to
imaginary tokens, I noted that some of them already had positions in the
AST.

A look at the source code for CommonTree.java reveals the secret:

public int getLine() {
if ( token==null *|| token.getLine()==0* ) {
 if ( getChildCount()>0 ) {
return getChild(0).getLine();
}
 return 0;
}
return token.getLine();
 }

For better or worse, it only works with the first child.

-- 
Juancarlo *Añez*


More information about the antlr-interest mailing list