[stringtemplate-interest] Bug/Problem passing parameters into iteration

Terence Parr parrt at cs.usfca.edu
Fri Nov 5 14:21:31 PDT 2010


On Nov 3, 2010, at 3:42 PM, Mike Goodwin wrote:

>>> No that is not the expectation.
>>> 
>>> In the expression:         indent=indent+"  "
>>> 
>>> My understanding is that StringTemplate is functional so reassignment
>>> would not be a legal action. I expect that the left hand side 'indent'
>>> is the name of the value being passed to the next call and so does not
>>> refer to the indent in the calling scope.
>> 
>> Oh. actually it does lazy eval in case you ref another parameter.
>> 
> 
> I'm not sure I entirely follow. Which other parameter could change?
> Surely this would only matter if the parameter was one which depended
> on the iteration, such as $i0$, is that even in scope? We may be
> talking at cross-purposes.


Sorry for delay. ok, i've got v3 and v4 in my head, dang it.  v3 comment on eval args:

	/** Evaluate an argument list within the context of the enclosing
	 *  template but store the values in the context of self, the
	 *  new embedded template.  For example, bold(item=item) means
	 *  that bold.item should get the value of enclosing.item.
	 */

foo(a=b) looks to create an 'a' in the arg map of foo with value of outer context's b value.

In ASTExpr.write, I see comment

	 *  Compute separator and wrap expressions, save as strings so we don't
	 *  recompute for each value in a multi-valued attribute or expression.

so iteration value is computed once, but args should be created/eval'd every invocation.

anyway, looking more closely [sorry, i'm a one track mind and on something else], I see:

x=x+"  "

The "+" forces (early) eval which has to eval x. x is eval'd with x.toString().  wait. why is x a model?

Hmm...that does seem wrong...i think "it" is becoming confused with sole arg.  Anyway, here is the fix

element(el,x)::=<<
$x$$el.name$<br>
$x$$el.children:element(el=it,x=x+" ")$
>>

tree(root)::=<<
<html>
        <body>
                <code>
$root:element(el=it, x="")$
                </code>
        </body>
</html>
>>

Note 1 less template
Ter


More information about the stringtemplate-interest mailing list