[antlr-interest] StringTemplate + preserving comments

Sohail Somani sohail at taggedtype.net
Tue Nov 8 21:35:51 PST 2005


On Tue, 2005-08-11 at 10:04 -0800, Terence Parr wrote:
> On Nov 8, 2005, at 10:00 AM, sohail at taggedtype.net wrote:
> >> Sohail Somani wrote:
> >>
> >>> I think the subject line is basically impossible with antlr
> >>> +stringtemplate, BUT in the off-chance someone has any ideas to  
> >>> do this,
> >>> I would appreciate some pointers.
> >>>
> >>> It would be really nice if one could do it without a TreeParser :)
> >>>
> >> Hi
> >> I don't know much about Stringtemplate, but if you want to preserve
> >> comments,
> >> you could use a tokenstreamhiddentoken filter after pasrsing, see
> >> http://www.antlr.org/doc/streams.html
> >
> > Hi Ed!
> >
> > I am aware of this technique. I would ideally like the separation that
> > StringTemplate provides. This is a bit difficult because (I think) it
> > would require me to insert <comment>'s between every tag in my  
> > template. I
> > should probably sign up for the stringtemplate ml now.
> 
> 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 {

    public MyCommonHiddenStreamToken()
    {
        super();
    }

    public MyCommonHiddenStreamToken(int t, String txt) 
    {
        super(t, txt);
    }

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

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

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

Thanks

Sohail



More information about the antlr-interest mailing list