[stringtemplate-interest] Using the "format" option with a template calls
Udo Borkowski
ub at abego-software.de
Mon Jan 31 14:11:57 PST 2011
Hi Ter,
> The early eval is really meant for small chunks.
For the property name case and possibly the option value case this assumption is fine. However for the default value this seems to be quite restrictive. Why should default values always be small chunks?
Anyhow, I guess we could go on with this discussion for quite a while. What about shortcutting this?
As you may tell from my postings I would like to use an AutoIndentWriter instead of the NoIndentWriter for the "toString"/"early eval" case. What about making this configurable? I.e. by default the NoIndentWriter is used but one may use a different one, if desired.
One way to achieve this: instead of calling "new NoIndentWriter(sw)" directly in Interpreter#toString(…) ask a factory object to return the "early eval" writer. By default this is a NoIndentWriter.
E.g.:
Add new interface:
public interface EarlyEvalWriterFactory {
/**
* Returns a newly created STWriter used to write the given value as a
* String with the StringWriter out.
*
* @param out
* @param template
* @param value
* @return
*/
STWriter createEarlyEvalWriter(StringWriter out, ST template, Object value);
}
In Interpreter add:
public static EarlyEvalWriterFactory earlyEvalWriterFactory = new EarlyEvalWriterFactory() {
@Override
public STWriter createEarlyEvalWriter(StringWriter sw, ST template,
Object value) {
return new NoIndentWriter(sw);
}
};
In Interpreter#toString(ST self, Object value) use
STWriter out = earlyEvalWriterFactory.createEarlyEvalWriter(sw,
self, value);
writeObjectNoOptions(out, self, value);
return sw.toString();
This way one could replace the earlyEvalWriterFactory if a different STWriter than the NoIndentWriter should be used for early string evaluation.
I would really appreciate this extension.
Udo
On 30.01.2011, at 20:47, Terence Parr wrote:
>
> On Jan 28, 2011, at 4:45 PM, Udo Borkowski wrote:
>> For me "the outer expression" is the invocation in the body of the main template. We agree this does not affect the outcome of "<(t())>". BTW: the invocation is not indented in main().
>
> oh! yeah, missed that.
>
>> So "<(t())>" is evaluated in the context of t. As you will recall, t is defined as
>>
>> t() ::= <<
>> abc
>> >>
>
> yeah, <(t())> it should be " abc" no matter what I think. hmm...
>
>> And here we have the indentation, that is part of the definition of t. Maybe this is more obvious when I write the template definition in this equivalent way:
>>
>> t() ::= " abc"
>>
>> For me <(t())> mean "evaluate the template t()" (to a string). And evaluating " abc" to a string results in " abc" (with the spaces).
>
>
>> Could you please have another look at the issue? I think this could be a quite important detail we should have a clear understanding of.
>
> indeed.
>
>> Udo
>>
>> P.S.: If it is not too much trouble, could you please explain why you are using the NoIndentWriter in Interpreter#toString(ST self, Object value) in the first place? As the NoIndentWriter is only used in this one method it was probably created just for this case. But I don't see why we need this. Could you give me an example when the AutoIndentWriter would not work?
>
> Yeah, this is a tough one. to force eval of a template expr, we need an STWriter so I must pick one. toString(...) is used to ensure b is property name in <a.b>. It's used to eval default args (usually strings). It's use to eval option values (usually strings). So in general no-indent is fine. Now, if I used indent-writer, it would mostly work too. What about <(t())> when t() is huge and indented but you had called render() with a no-indent-writer? now *part* your input is indented!
>
> The early eval is really meant for small chunks.
>
> added this test:
>
> @Test public void testEarlyEvalIndent() throws Exception {
> String templates =
> "t() ::= << abc>>\n" +
> "main() ::= <<\n" +
> "<t()>\n" +
> "<(t())>\n" + // early eval ignores indents; mostly for simply strings
> " <t()>\n" +
> " <(t())>\n" +
> ">>\n";
>
> writeFile(tmpdir, "t.stg", templates);
> STGroup group = new STGroupFile(tmpdir+"/"+"t.stg");
> ST st = group.getInstanceOf("main");
> String result = st.render();
> String expected =
> " abc" +newline+
> "abc"+newline+
> " abc" +newline+
> " abc";
> assertEquals(expected, result);
> }
>
> Ter
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110131/48488ebd/attachment-0001.html
More information about the stringtemplate-interest
mailing list