[antlr-interest] Retaining comments

Gavin Lambert antlr at mirality.co.nz
Wed Feb 27 12:29:59 PST 2008


At 06:06 28/02/2008, Blake Meike wrote:
 >Unfortunately, every call to the lexer action is called
 >before any call to any parser action, as if the entire
 >source file were converted to tokens, before any token
 >is fed to the parser.

Because that's exactly what does happen.

 >Is there a way to keep the, very convenient lexical handling 
of
 >comments, and still, to have the action associated with them 
called
 >in context?

You need to assign the COMMENT tokens to a channel; either the 
default channel (in which case all the parser rules will need to 
specify where comments are permitted) or an alternate channel (eg. 
channel 2).

This will keep the comment tokens in the token stream at the 
appropriate points.  To transfer them you'll have to add some code 
that looks for comment tokens nearby recognised parser constructs 
so you can emit them at the right place in the output.



More information about the antlr-interest mailing list