[antlr-interest] Write comments to templates

Jared Bunting jared.bunting at peachjean.com
Mon Nov 24 10:04:33 PST 2008


Well, I haven't actually used the rewrite features myself, just read the 
section on them in the book :-).  I would suggest that as a starting 
point.  I don't think you have to access the string produced by a 
template, etc.

 From your initial description, it sounded like your parser was 
basically taking some input and modifying the bits that you cared about 
while leaving the bits you don't care about (comments) alone, but 
preserving them in the output.  The rewrite features seem to be designed 
to do exactly this.  From what I understand, the TokenRewriteStream 
basically copies your input to a buffer.  Then, while parsing, each rule 
that is parsed, the output for the template for that rule replaces the 
original text that matched that rule.  Then, you can print out the 
entire buffer from the TokenRewriteStream.

But, like I said, I haven't actually used this functionality, but it 
sounds like it might be a fit for your problem.

-Jared

Chris Sekszczynska wrote:
> Hi Jared,
>
> this could be a good and elegant solution :)
>
> I think you mean the TokenStreamRewriteEngine? There, I can add a 
> String before and after a hidden token. But how can I access the 
> String which is produced by a specific template? And how can I map the 
> template output to the hidden token? I see no way to access the 
> template output within the grammar and no way to access the token 
> within my main() method.
>
> I'm not very familiar with ANTLRs internals, so I don't see any 
> starting point :(
>
> Kind regards,
>
> Chris
>
>
> On 23.11.2008 at 05:34 Jared Bunting wrote:
>
>> Not sure if it will meet your needs or not, but you might want to 
>> look at the rewrite functionality.  It should allow you to use your 
>> source file as a starting point, and replace code in it with your 
>> template output.  By ignoring comments in your parser, I would think 
>> the rewrite would simply leave those where they are.
>


More information about the antlr-interest mailing list