The string quotations in Stratego would be better if any line that contains only whitespace and placeholders does not appear in the output at all if and only if all placeholders on that line expand to the empty string.

Motivation:

Without this often some ‘hacks’ are needed to get rid of spurious empty lines at places where e.g. a list of items (statements) is allowed, but no items are present at this location.

Example:

$[if [cond] then
    [statements]
  [elsepart]
  end]

This would often result in one or two empty lines. If lines with only whitespace and empty placeholders would be skipped, the end result would usually be better.

There is currently no way to get rid of these empty lines except by either splitting up the template and messing with concat-strings and separate-by again (to which these templates are intended to be superior, right?), or by using conditionals and extra versions of the template without some placeholders (which sucks because of the duplication…), or by post-processing the string in some (ugly) way.

For the use case where the empty line is desired, one could simply make the placeholder expand to some whitespace instead of the empty string.

The suggested behaviour is also how e.g. StringTemplate behaves by default.

Submitted by Tobi Vollebregt on 18 April 2011 at 14:35

On 18 April 2011 at 15:49 Lennart Kats commented:

I can’t say I’ve seen those before myself. The only case where placeholders are empty in code I’ve ran into so far are list placeholders a la statement* in


$[ void x {
[statement*]
}

]

So your use case may be special since you’re generating string quotations with optional parts straight form the syntax definition? In the example above the blank line certainly isn’t harmful. So should all lines with empty placeholders be stripped from the output? And what about lines without any placeholders, like the empty line at the end of my example? That’s a common (anti-?)pattern for separating generated code with an empty line.


On 18 April 2011 at 15:50 Lennart Kats tagged rfc

On 18 April 2011 at 16:22 Tobi Vollebregt commented:

Yeah, both optional placeholders and list placeholders exhibit this behaviour.

So your use case may be special since you’re generating string quotations with optional parts straight form the syntax definition?

My use case is kind of special in that I’m considering to use this for pretty printing (it’s a lot closer to my SDF-with-templates than Box), in which case the empty lines (sometimes) aren’t pretty :-)
(So yeah, I would generate the string quotations from the syntax definition.)

For code generation I agree that the empty lines generally aren’t harmful.

And what about lines without any placeholders, like the empty line at the end of my example?

Those would be treated like they are now, i.e. always in the output. The suggested change would really only apply to lines that have whitespace, at least one placeholder, and nothing else.

Log in to post comments