[stringtemplate-interest] How does one map properties/methods of an object to ST attributes?

Johannes Luber jaluber at gmx.de
Mon Oct 15 15:57:47 PDT 2007


Hi!

The CreateContent template of the following ST group gives me trouble:
group ANTLR3ToRelaxNG;

/** 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">

# These elements complement the use of tokens which shouldn't be
generated by the program
EpsilonToken = empty
TextContent = text

}
>>

/** This rule generates the content elements. */
CreateContent(element) ::= <<
<if(element.IsRootType)>
<element.XmlName>Content =
	<element.ChildrenArray:{c|<CreateContent(element=c)>}>
<endif>
<if(element.IsAndType)>(<element.ChildrenArray:{c|<CreateContent(element=c)>};
separator=",\n">)<endif>
<if(element.IsOrType)>(<element.ChildrenArray:{c|<CreateContent(element=c)>};
separator="|\n">)<endif><endif>
<if(element.IsOptionalType)><element.ChildrenArray:{c|<CreateContent(element=c)>}>?<endif>
<if(element.IsZeroOrMoreType)><element.ChildrenArray:{c|<CreateContent(element=c)>}>*<endif>
<if(element.IsOneOrMoreType)><element.ChildrenArray:{c|<CreateContent(element=c)>}>+<endif>
<if(element.IsEmptyType)><element.XmlName>Content = empty<endif>
<if(element.IsEmptyType)><element.XmlName>Content = empty<endif>
<if(element.IsTextType)><element.XmlName>Content = text<endif>
<if(element.IsAnyType)><element.XmlName>Content = *<endif>
<if(element.IsLeafType)>element <element.XmlName> {
<element.XmlName>Content }<endif>
>>

element refers to a TöpeInterpreter object (subclassed for every type),
which has properties (can change them into methods, if necessary) named
IsAndType, IsOptionalType, etc. But if I call the template with three
TextInterpreters (XmlNames are "SingleCharacter", "SimpleEscapeSequence"
and "Character") and one RootInterpreter (with XmlName "Data", also
having a ZeroOrMoreInterpreter as a child, which itself has a
LeafInterpreter child with XmlName ) I receive the following output:

# Grammar BackslashBugTest
grammar{
start = element CompilationUnit { CompilationUnitContent }

DataContent =

# These elements complement the use of tokens which shouldn't be
generated by the program
EpsilonToken = empty
TextContent = text

}

As one can see, only the RootInterpreter object shows up on the output.
What I am doing wrong? Considering that I'm using ANTLR I can't assign
other other template arguments automatically - at least not that I know
of. Do I have to create an own tree walker which feeds to adapted templates?

Best regards,
Johannes Luber


More information about the stringtemplate-interest mailing list