[antlr-interest] Convert token position from line+column (index in line) to absolute source indexes

Jim Idle jimi at temporal-wave.com
Tue Mar 17 08:16:21 PDT 2009


Gabriel Petrovay wrote:
> Hi all,
>
> Does anybody have any idea, a working solution or an example of how 
> could I convert the token positions from (line, column) pair to 
> absolute indexes in the input stream (indexStart, indexEnd)
>
> Or, are there any ways to do this from the lexer without hacking it 
> (change the Java code generation). I am thinking about helper 
> clases/functions to do this or if I can some tell the Lexer to use 
> some extended token (MyCommonToken) that somehow (I don't know yet 
> how) adds this functionality.
This information is part of the standard CommonToken, but don't forget 
that in the lexer rules you are still forming some of this information, 
so some references are only good for fragment calls and so on. Look at 
the values of $start, $stop, $pos, etc.

For CommonToken itself you can use getStartIndex() and getStopIndex(), 
as you can see from the implementation of getText().

Jim




More information about the antlr-interest mailing list