[stringtemplate-interest] "rest" and template recursion FIXED

Terence Parr parrt at cs.usfca.edu
Mon Jun 2 19:01:38 PDT 2008


Ok, Fixed. THis test passes:

	public void testReUseOfRestResult() throws Exception {
		String templates =
			"group test;" +newline+
			"a(names) ::= \"<b(rest(names))>\""+newline+
			"b(x) ::= \"<x>, <x>\""+newline
			;
		StringTemplateGroup group =
			new StringTemplateGroup(new StringReader(templates));
		StringTemplate e = group.getInstanceOf("a");
		List names = new ArrayList();
		names.add("Ter");
		names.add("Tom");
		e.setAttribute("names", names);
		String expecting = "Tom, Tom";
		assertEquals(expecting, e.toString());
	}

and this one:

	public void testReUseOfStripResult() throws Exception {
		String templates =
			"group test;" +newline+
			"a(names) ::= \"<b(strip(names))>\""+newline+
			"b(x) ::= \"<x>, <x>\""+newline
			;
		StringTemplateGroup group =
			new StringTemplateGroup(new StringReader(templates));
		StringTemplate e = group.getInstanceOf("a");
		List names = new ArrayList();
		names.add("Ter");
		names.add(null);
		names.add("Tom");
		e.setAttribute("names", names);
		String expecting = "TerTom, TerTom";
		assertEquals(expecting, e.toString());
	}

failed beforehand.

Implemented trunc also:

	public void testTruncOp() throws Exception {
		StringTemplate e = new StringTemplate(
				"$trunc(names); separator=\", \"$"
			);
		e = e.getInstanceOf();
		e.setAttribute("names", "Ter");
		e.setAttribute("names", "Tom");
		e.setAttribute("names", "Sriram");
		String expecting = "Ter, Tom";
		assertEquals(expecting, e.toString());
	}

edit //depot/code/stringtemplate/java/main/CHANGES.txt#15
edit //depot/code/stringtemplate/java/main/src/org/antlr/ 
stringtemplate/language/ASTExpr.java#47
delete //depot/code/stringtemplate/java/main/src/org/antlr/ 
stringtemplate/language/StripIterator.java#3
edit //depot/code/stringtemplate/java/main/src/org/antlr/ 
stringtemplate/test/TestStringTemplate.java#84
Change 4977 submitted.

Ter

On May 31, 2008, at 4:39 PM, Terence Parr wrote:

>
> On May 30, 2008, at 11:30 PM, Zenaan Harkness wrote:
>
>> On Fri, May 30, 2008 at 11:05:22PM -0700, Sasha Crosland wrote:
>>> Thanks for the explanation, Terence.  A side-effect-free rest()
>>> does seem
>>> more in the spirit of StringTemplate, doesn't it?  I vote yes, for
>>> what it's
>>> worth.
>>>
>>> But would you actually need to make a copy of the list data?  If
>>> lists are
>>> immutable within a template, then couldn't you easily return a
>>> lightweight
>>> view of the sublist that reads through to the original list, just
>>> like with
>>> an iterator?
>>
>> Vote thirded.
>>
>> Recursion ought to Just Work (TM).
>>
>> I additionally vote for an addition to first,last,rest to fill the
>> gap,
>> perhaps "front" (all but last).
>
> I think i have trunc for that.
>
>> Lightweight container wrapper is definitely the go here.
>> ArrayList.clone(), or Collection.clone() should do what's needed.
>
> kewl. thanks.
> Ter
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest



More information about the stringtemplate-interest mailing list