[antlr-interest] Strategy for mapping output to line numbers from a tree walker

Jim Idle jimi at temporal-wave.com
Fri Aug 21 15:37:19 PDT 2009


Gavin Lambert wrote:
> At 08:47 22/08/2009, Stanislav Sokorac wrote:
>> What is the best way to handle this problem when the children of a 
>> node are coming from different CharStreams (include files, macros, 
>> what have you...), and you could expect to have the first or last 
>> token be from another stream?
> [...]
>> It seems to me that the tree walker has no way of determining the 
>> location of the first character in PROGRAM without us tracking the 
>> locations of char stream switches during lexing, but that creates a 
>> special case to be checked for every one of the nice and simple 
>> methods below. Is there a more elegant solution available?
>
> You're going to want to track that information anyway, in order to 
> provide good error reporting (people will want to know that an error 
> is on line 16 of include file B, not on line 152 of the input after 
> preprocessing).
>
> ANTLR already includes a line number with each token, and the filename 
> is in the char stream.  You just need to link the token with the char 
> stream it came from (which might even already be done; I haven't 
> checked) and you should be good to go.

Yes it is already done, there is a getInputStream() (or similar sounding 
method) that gives you this, which will then yield all the information 
you need.

Jim


More information about the antlr-interest mailing list