[stringtemplate-interest] leading/trailing whitespace trimmed
Terence Parr
parrt at cs.usfca.edu
Thu Apr 17 11:12:34 PDT 2008
On Apr 16, 2008, at 10:34 AM, Zenaan Harkness wrote:
>> What I find frustrating is that it seems as though it can trim one OR
>> two newlines.
>
> I've just done some experimenting, and the problem is this. Here's my
> test case, with three failed examples:
>
> 0) ----
> # This is my getter template, the main parent template, which, when
> # a.customGetter is true (as is in my case), then the subtemplate
> # "getBody" is called.
> # If the middle line wraps, pretend the following is three lines only:
> attGet(a) ::= <<
> public <a.type> get<a.name> ()
> {<if(a.customGetter)><getBody(a)><else>return <a.name>;<endif>}
>>>
>
>
> 1) ----
> # Here's my first attempt to produce a nice (custom) getter method
> body.
> # Clearly this example does not work, because there is no blank line
> # at start or end of method:
> getBody(a) ::= <<
> return _param != null ? _param
> : _returns != null ? _returns
> : null;
>>>
>
> # As expected, the consumed blank lines show (by their lack) in my
> # generated output; a nice template layout (just above), but an ugly
> # result:
> public String getParam () { return _param != null ? _param
> : _returns != null ? _returns
> : null;}
Yes,Because you said no \n after the { in the enclosing template. that
is as I intend.
> 2) ----
> # Take 2, my second attempt, following the ST docs, I insert a "<\n>":
> getBody(a) ::= <<
> <\n>
> return _param != null ? _param
> : _returns != null ? _returns
> : null;
> <\n>
>>>
>
> # Here's what my 2nd generated output looks like, template a bit ugly,
> # reasonable layout (above), but a silly result:
> public String getParam () {
>
> return _param != null ? _param
> : _returns != null ? _returns
> : null;
>
> }
Again, this is as I intend. <\n> Followed by \n use 2 newlines, not
one. Try
<< <\n> return _param...
> 3) ----
> # So, I must have too many newlines. Easy! compress the <\n>s:
> getBody(a) ::= << <\n>
> return _param != null ? _param
> : _returns != null ? _returns
> : null;
> <\n> >>
oh, right... you saw that.
> # This produces almost the same result as 2) above, except there are
> # extra space characters lurking around (just after the first open
> # brace, and just before the last open brace):
> public String getParam () {
>
> return _param != null ? _param
> : _returns != null ? _returns
> : null;
>
> }
Yeah, that is ugly I guess... I think you need to think about this a
different way. A template specifies content but the surrounding
template specifies a whitespace around that template. Instead of your
public <a.type> get<a.name> () {<if(a.customGetter)>
do this
public <a.type> get<a.name> () {
<if(a.customGetter)>
and then all of a sudden you get exactly what you want and it looks
great.
> 4) ----
> # So, on to solution 4), where I take advantage of the first and
> # last \n getting consumed, and have my template like so:
> getBody(a) ::= <<
> <\n> return _param != null ? _param
> : _returns != null ? _returns
> : null;<\n>
>>>
>
> # Finally, I have produced the result, character for character,
> # that I wanted, albeit with a template that's a bit ugly:
> public String getParam () {
> return _param != null ? _param
> : _returns != null ? _returns
> : null;
> }
That also works but is not as clean as what I just mentioned.
> So in conclusion, there are 5 ways to get the job done, 3 of which
> simply don't work, and two of which work.
I only use one way and it seems to work great ;)
:)
Ter
More information about the stringtemplate-interest
mailing list