[antlr-interest] Preserving Comments

Jim Idle jimi at temporal-wave.com
Mon Jul 14 07:54:30 PDT 2008


On Mon, 2008-07-14 at 12:43 +0530, nilesh.kapile at tcs.com wrote:

> 
> Hello, 
> 
> I need to preserve comments and want to collect it in some data
> structure.  How can we do that in ANTLR? 
> 
> Currently, I've following rule for comments: 
> 
> LINE_COMMENT 
>     :  '//' ~('\n'|'\r')* '\r'? '\n'   {$channel=HIDDEN;} 
>     ; 

The easiest way os to use your own channel, say 2, which means the
parser will ignore them but they are preserved in the input stream
(actually they are when HIDDEN but that really means 'anything you want
to hide' and you specifically want to inspect comments. Then, when you
walk your tree (assuming you are using a tree but that is best), at any
point where the comments are required, you can search within the start
and stop tokens for the AST rule and find anything on channel 2, doing
with it as you will. You can also do this from the parser of course.

The other option is to pass the COMMENT token through as a normal token,
and create parser rules to assemble them at various points. The problem
here comes when the COMMENT can be anywhere, such as in the middle of
expressions, so the parser ends up having the COMMENT token everywhere
and complicates your grammar enormously.

Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080714/2b80d3c4/attachment.html 


More information about the antlr-interest mailing list