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

Terence Parr parrt at cs.usfca.edu
Sat Feb 5 11:10:53 PST 2011


Oh, i'm an idiot. it should obviously just create same kind of writer as outer templ is using.

			StringWriter sw = new StringWriter();
			STWriter stw = null;
			try {
				Class writerClass = out.getClass();
				Constructor ctor =
					writerClass.getConstructor(new Class[] {Writer.class});
				stw = (STWriter)ctor.newInstance(sw);
			}
			catch (Exception e) {
				stw = new AutoIndentWriter(sw);
				errMgr.runTimeError(self, current_ip, ErrorType.WRITER_CTOR_ISSUE, out.getClass().getSimpleName());
			}
			writeObjectNoOptions(stw, self, value);

Unit tests:

	/** (...) forces early eval to string. early eval <(x)> using new
	 *  STWriter derived from type of current STWriter. e.g., AutoIndentWriter.
	 */
	@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\n" +
			"  abc\n" +
			"    abc\n" +
			"    abc";
		assertEquals(expected, result);
	}

	@Test public void testEarlyEvalNoIndent() 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");
		StringWriter sw = new StringWriter();
		NoIndentWriter w = new NoIndentWriter(sw);
		st.write(w);
		String result = sw.toString();
		String expected =
			"abc\n" +
			"abc\n" +
			"abc\n" +
			"abc";
		assertEquals(expected, result);
	}

On Jan 31, 2011, at 2:11 PM, Udo Borkowski wrote:

> 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
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110205/a7cfeebe/attachment-0001.html 


More information about the stringtemplate-interest mailing list