[stringtemplate-interest] Conditional output of multivalued attributes / How to handle separator problem?

Terence Parr parrt at cs.usfca.edu
Sun Apr 22 17:43:47 PDT 2007


Hi Ivan,

I think that you need to filter the list and then apply your  
template. I'm just thinking off the top of my head here:

$parameters:{ p | $if(p.isSet)$$p$$endif$}:{this.$it.Name$ = $it.Name 
$;}; separator="\r\n"$

  please let me know if that works.  Note that I am using a chained  
map operation to apply a filter to the list of parameters.  The only  
thing that worries me is that the results of the first {..} block  
might yield an empty or null value they get passed to the second {...}.

  If you actually want the \r\n on end of a line instead of as a  
separator, then just add <\r><\n> inside the template application  
{...} and get rid of the separator.
Ter

On Mar 18, 2007, at 12:44 PM, Ivan Vedic Ai wrote:

> Hi all,
>
> I'm having trouble designing a stringtemplate for code of a C#
> constructor with arguments. I would like to construct a template that
> would output items $it$ of a multivalued attribute only if item's
> "$it.IsSet$" boolean property is set to "true". The trouble I'm having
> is with the set 'separator=", "' that is written for all items, even
> those excluded by my condition.
>
> My constructor.st stringtemplate looks like this:
> ------------------------------------------------------
> public $class.Name$($class.Parameters:{$if(it.IsSet)$$it.Type$
> $it.Name$$endif$}; separator=", "$)
> {
>   $class.Parameters:{$if(it.IsSet)$this.$it.Name$ =
> $it.Name$;$endif$}; separator="\r\n"$
> }
> ------------------------------------------------------
>
> Between the brackets I output a multivalued attribute
> $class.Parameters$, but I only output $it.Type$ and $it.Name$ if the
> value of $it.IsSet$ is set to "true".
>
> The output I get is as follows:
> ------------------------------------------------------
> public Miki(string name, , TestEnum enum)
> {
>    this._name = name;
>
>    this._enum = enum;
> }
> ------------------------------------------------------
>
> What I would like to achieve is not output the separator for items
> which do not get written out (notice the ", , " and an extra newline).
>
> Is there a way arround this problem?
> Can someone please tell me how should I design my stringtemplate to
> achieve this functionality?
>
>
>
> Here is the definition of my template setAttribute classes:
> ------------------------------------------------------
> public class TestClass
> {
>     private string _name;
>     private List<ParameterClass> _parameters;
>
>     public string Name
>     {
>         get { return _name; }
>         set { _name = value; }
>     }
>
>     public TestClass()
>     {
>         _parameters = new List<ParameterClass>();
>     }
>
>     public TestClass(string name)
>         : this()
>     {
>         _name = name;
>     }
>
>     public List<ParameterClass> Parameters
>     {
>         get { return _parameters; }
>         set { _parameters = value; }
>     }
> }
>
> public class ParameterClass
> {
>     private string _type;
>     private string _name;
>     private bool _isSet;
>
>     public string Type
>     {
>         get { return _type; }
>         set { _type = value; }
>     }
>
>     public string Name
>     {
>         get { return _name; }
>         set { _name = value; }
>     }
>
>     public bool IsSet
>     {
>         get { return _isSet; }
>         set { _isSet = value; }
>     }
>
>     public ParameterClass(string type, string name, bool isSet)
>     {
>         _type = type;
>         _name = name;
>         _isSet = isSet;
>     }
> }
> ------------------------------------------------------
>
> I initialize a "class" attribute with my TestClass() instance like  
> this:
> ------------------------------------------------------
> TestClass clazz = new TestClass("Miki");
> clazz.Parameters.Add(new ParameterClass("string", "Name", true));
> clazz.Parameters.Add(new ParameterClass("string", "Parameters",  
> false));
> clazz.Parameters.Add(new ParameterClass("TestEnum", "Enum", true));
>
> template.SetAttribute("class", clazz);
> ------------------------------------------------------
> _______________________________________________
> 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