[stringtemplate-interest] Changing separators in recursive templates
Terence Parr
parrt at cs.usfca.edu
Fri Aug 15 11:40:05 PDT 2008
On Aug 14, 2008, at 10:36 AM, asword at telus.net wrote:
> Hello,
>
> I recently started using StringTemplate, and haven't found my
> around all the docs and mailing lists yet. I am using the C#
> version 3.1beta (since it adds features I need). I've run into a
> few problems here and there, particularly with trying to use
> recursive templates. The latest one seemed like a flat out bug so I
> thought I would inquire about it.
> The situation is that I have a graph of objects that represent a C+
> + boolean expression that I want to output. My initial attempt at
> this was roughly this (I don't have the original code as I've worked
> around the problem by taking a different strategy, I would just like
> to know why it didn't work):
> BinaryOp(node, op) ::=
> "$node.SubExpressions:EvalElement(elem=it);separator=op$"
> EvalElement(elem) ::= <<
>
> $if(elem.IsBinaryOp)$
>
> ($BinaryOp(node=elem,op=node.Op)$)
>
> $else$
>
> $elem.Condition$
>
> $endif$
>
> >>
>
>
> This almost worked. The problem arose when it had recursed a level
> or two and the op changed from (for example) "&&" to "||" and back
> again. The "||" never appeared, instead the separator was always
> "&&" at all levels.
>
That does seem strange. Perhaps you could simply do separator=node.Op
to avoid Anything weird with the parameters.
> Also, are there any hints to debugging templates more effectively?
> I'm finding that it is mostly guesswork and that I get NoViableAlt
> exceptions which don't provide any clues as to their source. Many
> of these are caught internally by StringTemplate, and if I have the
> debugger set to stop at the exception then I have to step through
> potentially dozens of them until I get to the exception that I
> really want to look at.
>
NoViable alt errors are parse errors...often the line numbers are not
exactly helpful because the errors are within an action. I'm not
familiar enough with C# implementation to know what the real issues are.
Ter
More information about the stringtemplate-interest
mailing list