Conditionals in Antlr String Templates

Posted by Pat Long - Munkii Yebee on Stack Overflow See other posts from Stack Overflow or by Pat Long - Munkii Yebee
Published on 2009-10-09T16:55:23Z Indexed on 2011/01/04 20:53 UTC
Read the original article Hit count: 385

Filed under:
|

We are using Antlr StringTemplates to give control over how a Entity's Name is output.

The basic Stringtemplate is

$FirstName$ $Initial$ $LastName$, $Suffix$, $Degree$

I want to add some smarts to that template so that the commas are only output when necessary i.e. The first comma is only output when there is a Suffix or Degree and the second commas is only output if there is a suffix.

I tried the following template string bit it does not work. I guess I have misunderstood

$FirstName$ $Initial$ $LastName$ <if(Suffix|Degree)>,<endif>, $Suffix$ <if(Suffix)>,<endif> $Degree$

If it helps we process the templates using this C#

StringTemplate stringtemplate = new Antlr.StringTemplate.StringTemplate(template.Data);

foreach (Pair<string, string> pair in dictionary)
{
    if (pair.First != null && pair.Second != null)
    {
        stringtemplate.SetAttribute(pair.First, pair.Second);
    }
}

return stringtemplate.ToString();

© Stack Overflow or respective owner

Related posts about antlr

Related posts about stringtemplate