[stringtemplate-interest] summarizing white space and indentation

Terence Parr parrt at cs.usfca.edu
Sun Nov 8 10:40:19 PST 2009


Verrrrrrry interesting.  Perhaps this gives an opport. to format  
templates w/o messing up output.

<if(x)>
foo
<endif>

would give "foo\n" by default if x.  It would give "" if !x.  Wait,  
how to remove \n from after foo?

<if(x)>
foo
<-endif>

?? probably not.

Let's use a real example where I have a huge single template line to  
obtain a single output line (it might wrap in your emailer:

public <returnType()> <ruleDescriptor.name> 
(<ruleDescriptor.parameterScope:parameterScope(scope=it)>) throws  
RecognitionException \{ <if(ruleDescriptor.hasReturnValue)>return  
<endif><ruleDescriptor.grammar:delegateName()>.<ruleDescriptor.name> 
(<ruleDescriptor.parameterScope.attributes:{a|<a.name>}; separator=",  
">); \}}; separator="\n">

Here we have exprs and IF stuff and {...} stuff with separator  
option.  What I'd like is to add some formatting:

public <returnType()> <ruleDescriptor.name>(
     <ruleDescriptor.parameterScope:parameterScope(scope=it)>
) throws RecognitionException {
     <if(ruleDescriptor.hasReturnValue)>return <endif>
     <ruleDescriptor.grammar:delegateName()>.<ruleDescriptor.name>(
         <ruleDescriptor.parameterScope.attributes:{a|<a.name>};  
separator=", ">
     );
\}}; separator="\n">

Or something like that.  BUT, I don't want any newlines in output.  
I.e., I want newlines to format template itself not output. In 3.2.1 I  
added <\\> (though I think it's broken).

public <returnType()> <ruleDescriptor.name>(<\\>
     <ruleDescriptor.parameterScope:parameterScope(scope=it)><\\>
) throws RecognitionException {<\\>
     <if(ruleDescriptor.hasReturnValue)>return <endif><\\>
     <ruleDescriptor.grammar:delegateName()>.<ruleDescriptor.name>(<\\>
         <ruleDescriptor.parameterScope.attributes:{a|<a.name>};  
separator=", "><\\>
     );<\\>
\}}; separator="\n">

I guess that works. The <\\> would scarf \n followed by whitespace.   
Hmm....seems ok.

I like the '-' idea so we could indent IFs:

<if(x)>
     <-name>  <! don't indent; I'm just formatting template !>
<endif>

OTOH, that makes it harder to read templates. have to read carefully  
to figure out indentation.

If we wanted to use multi-line IF to mean single line we can do:

<if(x)>
foo<\\>
<endif>

yields foo if x.

Ter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20091108/13b99ee8/attachment.html 


More information about the stringtemplate-interest mailing list