[stringtemplate-interest] Using the "format" option with a template calls

Terence Parr parrt at cs.usfca.edu
Sun Jan 30 11:47:21 PST 2011


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110130/5fe4c023/attachment-0001.html 


More information about the stringtemplate-interest mailing list