[stringtemplate-interest] Re: [antlr-interest] StringTemplate + preserving comments

Terence Parr parrt at cs.usfca.edu
Tue Nov 8 21:40:24 PST 2005


On Nov 8, 2005, at 9:35 PM, Sohail Somani wrote:
>> Howdy.  For tweaking or augmenting input, ANTLR +
>> TokenStreamRewriteEngine is pretty damn hard to beat. :)  That said,
>> I'm hoping to learn how to build a pretty printer in the new few
>> months as a way to test new v3 functionality.  As you say, preserving
>> the comments in this scenario is pretty tough.  I will have to think
>> about how to solve that.  Naturally ANTLR has all the info, the
>> problem may be simply to assume that any token you insert into the
>> template also includes any hidden tokens before it.  Heh, that might
>> work :)  In v2 or v3, I think you can just alter Token.toString() to
>> print hidden stuff before the token as well.  easy :) Damn!  That
>> might work!
>
> Are you suggesting I do something like:
>
> import antlr.CommonHiddenStreamToken;
>
> public class MyCommonHiddenStreamToken extends  
> CommonHiddenStreamToken {
>

[snip]

>     public String getText()
>     {
>         if(getHiddenBefore()!=null)
>         {
>             return getHiddenBefore().getText() + "\n" +  
> super.getText();
>         }
>         else
>         {
>             return super.getText();
>         }
>     }
>
>     public String toString()
>     {
>         return getText();
>     }
> }

Yes, that should work.

> A problem I'm having is that getHiddenBefore() becomes null when its
> time to print it out (getText()).

really?  Hmm...it should always point at the hidden stuff in front.   
Perhaps you are not setting the right token into the ST attribute.   
For example, if a comment is between a '{' and an ID, then the ID  
must go into the stringtemplate so that it can print itself out with  
the comment before.

> If I'm on the wrong track, please let me know :)

You are on the right track :)

Ter


More information about the antlr-interest mailing list