[stringtemplate-interest] Problem with null actual parameters (in anonymous templates)

Iztok Kavkler iztok.kavkler at fmf.uni-lj.si
Wed Mar 18 09:10:12 PDT 2009


If an actual parameter to a template without formal attributes is
specified but its value is null, the attribute will be looked-up in
enclosing instances. This will almost always break recursive invocations
of templates (they are common in grammars with output=template), because
it creates cycles in the list of enclosing instances.

For example, the following snippet should parse and print back nested
functions with any number of parameters, like " f1(f2(), f3()) ", but it
actually dies with stack owerflow (or it diagnoses the cycle when lint
is on):

expr: id=IDENTIFIER '(' (pl+=expr (',' pl+=expr)*) ')'
->	template(id={$id.text}, par={$pl})
	<< <id>(<par; separator=", ">) >> ;

The solution is simple: when checking for attributes th function
StringTemplate.get should return null if the attribute is present but
its value is null (some modifications to setAttribute are also
necessary). The following patch to StringTemplate.java does the trick:

486c486
< 		if ( value==null || name==null ) {
---
> 		if ( name==null ) {
510a511,516
> 		
> 		// null value should be added only if the attribute does not yet exist
> 		if ( value==null ) {
> 		   return;
> 		}
> 		
772a779,783
> 		// there is an actual argument, but it is null
> 		if ( o==null && self.attributes!=null &&
self.attributes.containsKey(attribute) ) {
> 		   return o;
> 		}
>


More information about the stringtemplate-interest mailing list