[stringtemplate-interest] Attaching model to stringtemplate group

Johannes Luber jaluber at gmx.de
Sun Sep 23 11:09:29 PDT 2007


Terence Parr wrote:
> On Sep 17, 2007, at 8:23 AM, Johannes Luber wrote:
> 
> Hi Johannes,
> 
> I am not sure what you're asking here.  There is no attaching a model  
> to the templates.  You must send in data via setAttribute() to every  
> template and for all of the attributes that you intend to use in the  
> template.
> 
> ST by itself requires you to manually set attributes and so on with  
> code, I'm afraid.  Perhaps that is what you are asking.  ANTLR  
> actions allow you to say %foo() to load template foo and then %x.y =  
> "hi" to set attribute y of template x to "hi".
> 
> Ter

The following is my entire STG-file (only two rules):

group ANTLR3ToRelaxNG;

/*	Can AnyElement match only a single element or as many as wished?
*/

/** The start template. Call only this template from a program. */
Grammar(Name, StartRule, Elements) ::= <<
# Grammar <Name>
grammar{
start = element <StartRule> { <StartRule>Content }

<Elements:{e|<CreateContent(element=e)>}; separator="\n">

EpsilonToken = empty

}
>>

/** This rule generates the content elements. */
CreateContent(element) ::= <<
<if(element.IsRootElement)>
<element.Name>Content =
	<element.Children:{c|<CreateContent(element=c)>}>
<endif>
<if(element.IsAndElement)>(<element.Children:{c|<CreateContent(element=c)>};
separator=",\n">)<endif>
<if(element.IsOrElement)>(<element.Children:{c|<CreateContent(element=c)>};
separator="|\n">)<endif><endif>
<if(element.IsOptionalElement)><element.Children:{c|<CreateContent(element=c)>}>?<endif>
<if(element.IsZeroOrMoreElement)><element.Children:{c|<CreateContent(element=c)>}>*<endif>
<if(element.IsOneOrMoreElement)><element.Children:{c|<CreateContent(element=c)>}>+<endif>
<if(element.IsEmptyElement)><element.Name>Content = empty<endif>
<if(element.IsTextElement)><element.Name>Content = text<endif>
<if(element.IsAnyElement)><element.Name>Content = *<endif>
<if(element.IsLeafElement)>element <element.Name> {
<element.Name>Content }<endif>
>>

As you can see the second rule is to be used recursively. How do I setup
the string template objects with the tree objects? How am I supposed to
set the values correctly? The elements have properties with the same
name as the tested attribute. Do you need any other information?

On another note: Having read your problem with extended typing sessions:
Did you consider already to switch the keyboard layout? I recommend
Colemak on <http://www.colemak.com>. Better than Dvorák and once you get
up to speed, even faster than Dvorák. I'm using it to reduce the strain
on my fingers, too, and have been very satisfied. The only problem is
the speed hit you take. The first week is especially gruesome and it
took a few months to return to the old marks, so the undertaking is not
for the faint of hard. :( But it is better than having constantly pain,
isn't it?

Johannes


More information about the stringtemplate-interest mailing list