[stringtemplate-interest] summarizing white space and indentation

Jonathan Buhacoff jonathan at buhacoff.net
Sun Nov 8 12:59:43 PST 2009


On Nov 8, 2009, at 10:40 AM, Terence Parr wrote:

> 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.
>

You could remove the \n from after foo like this:

<if(x)>
foo<empty->    <! where empty is an empty template named "empty" !>
<endif>

or

<if(x)>
foo<""->   <! empty string as literal empty template  !>
<endif>


Maybe <""-> looks rather ugly,  but how nice can a representation of  
nothingness be?  Maybe <\\-> is a little better, but I would expect a  
backslash out of that and not an empty string.  Maybe \e meaning empty  
string could then be used as <\e->.   I like \e because it doesn't  
step on any existing standard that I know of.  ANSI-C  defines only \n  
\r \t \b \\ \? \' and \"


> 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">
>

My attempt, this time trying <\e-> to see how it looks.  Notice also  
the leading/trailing whitespace control on other tags:

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

Or, if the rule for trailing whitespace control is eliminate all  
whitespace up to the next template tag or literal character (like what  
<\\> is currently supposed to do), then this specific example wouldn't  
need to use the leading whitespace controls (but they'd still be  
necessary for other situations):

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

To me the hyphens kind of stand out, but even if they didn't, knowing  
that hyphens control whitespace that would be the first thing I look  
for if I have a whitespace issue.

here's the first example again using \e:

<if(x)>
foo<\e->   <! empty string as literal empty template, with trailing  
whitespace control  !>
<endif>



> 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/faded659/attachment.html 


More information about the stringtemplate-interest mailing list