[antlr-interest] Write comments to templates

Gavin Lambert antlr at mirality.co.nz
Sun Nov 23 00:58:57 PST 2008


At 07:39 23/11/2008, Chris Sekszczynska wrote:
>Now I like to write all comments occurring in the source file to 
>the (nearly) same position in the destination file. So I have to 
>pass the comments to the walker. Of cause, I could simply put the 
>comments into the default token stream and add "Comment?" to 
>every position in every rule. This will make my parser and walker 
>grammars unreadable.
>
>Isn't there a better solution? Is it, for example, possible to 
>scan the token stream parallel while the AST is parsed? So it 
>could be possible to write comments to the StringTemplate output 
>just after they appear in the input stream at the nearly same 
>position (using token.index).

I haven't actually done it myself, so I don't know the 
specifics.  But it's possible to put the comments on a separate 
channel (not the hidden channel) and then either use a separate 
parser "tuned" to that channel (so it only sees the comments) and 
then splice the ASTs together outside of ANTLR, or to scan through 
the token stream in your standard parser between two known 
reference points looking for off-channel tokens to include.



More information about the antlr-interest mailing list