[stringtemplate-interest] wrapping long lines in ST expressions
Terence Parr
parrt at cs.usfca.edu
Sun May 28 11:45:24 PDT 2006
Hi,
I'm in need of proper line wrapping as I'm generating some big static
arrays of ints. We need to come up with a nice mechanism that is
easy and general. The easiest thing would probably be a lineLength
parameter to either toString() or as a property of STs or STGs. ST
would need to insert a \n before spitting out an expression if the
prior output on that line is over the threshold. You want the output
to break in between expressions so that you avoid splitting output
constructs unnecessarily, but the common case is probably a single
attribute expression that has multiple things spit out like an array
of ints.
Can people think of common uses for wrapping other than lists and such?
Do we need an indentation parameter also?
int[] a = { a, b,
c, d };
Those spaces before the "c" (sizeof("int[] a = { ")) are what I'm
talking about. Here is the generating template:
int[] a = { <elements; indent=" ", separator=", "> };
Those option values can actually template expressions and here is a
case where we need length() operator. If the "int" is a variable
<type>[] a = { <elements; indent={<length(type+" ")>},
separator=", "> };
which would mean I would need to interpret numeric strings as how
many spaces? Hmm...not good. Also length should be how many
elements not length in char probably. How do we deal with variable
length indentation? Perhaps we have indent and relativeIndent.
relativeIndent indents from position of expression start on first line.
There is another issue: Where do I store the current char count for
current line? Can't store in the ST objects as they can be contained
in multiple other templates and may get examined multiple times to
display different things. Seems like the actual invocation of the
toString() method needs to track the line count for that particular
execution. I'll have to create a mutable Integer I guess and pass
that printing context around to the write() routines. Perhaps that
will be a useful thing in general. I have trouble removing blank
lines sometimes as I don't know the overall count of char for the
current line. Oh, doh! I have AutoIndentWriter already that knows
about newlines for indentation...I wonder if we can simply handle
this with a parameter to the constructor. Currently, toString() calls:
StringTemplateWriter wr = group.getStringTemplateWriter(out);
which can be set with:
/** Specify a StringTemplateWriter implementing class to use for
* filtering output
*/
public void setStringTemplateWriter(Class c) {
userSpecifiedWriter = c;
}
Revisiting multiple expressions per line:
public void foo(<args; separator=", ">) throws <excepts; separator=", ">
{
<body>
}
where should it break? Presumably in between the expressions rather
than in the middle of one of the expr. That might be too hard. The
output writer sees "public void foo(" then each arg intermixed with
write(", ") then ") throws " then the excepts elements intermixed
with ", ". The write doesn't know anything about literals, intra
expression and inter expression locations. Hmm...can't predict the
future anyway...we don't know that the excepts are coming.
Ok, I think i've convinced myself that a simple char count and
threshold will do the trick. Hooray!
Wait, what if you want lines to wrap more tightly for some
expressions? Do we need an arg on expressions like
int[] a = { <elements; width=40, separator=", "> };
Hmm...I could store the option in the ASTExpr object like I do for
indentation and separator. Well, I don't need it now; agile mindset
says leave til later ;)
Wow...did I *finally* find a need for more options thus validating my
complicated <expr; option=value> notation? ;)
Ter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20060528/2e699181/attachment.html
More information about the stringtemplate-interest
mailing list