[stringtemplate-interest] Treating inner classes differently than outer classes when rendering source code

Terence Parr parrt at cs.usfca.edu
Tue May 3 16:54:06 PDT 2011


Hi Nicholas,My knee-jerk reaction is that you are trying to do too much inside the template. have a code generator decide what inner classes if any are needed. it should create them with getInstanceOf and then inject them as attributes to classDeclaration, which can simply dump them out via

<innerClasses()>

Ter
On Apr 18, 2011, at 11:52 AM, Nicholas Dunn wrote:

> I am using StringTemplate 3.2 in a project to generate sourcecode for serializing/deserializing data adhering to binary packed message specifications (e.g. DIS, STANAG 4586).  The problem I'm running into is as follows.  I structure the templates roughly as follows:
> 
> http://i54.tinypic.com/29wphjl.jpg
> 
> The issue is that not all of my classes that I want to generate use the STANAG4586Java specializations.  There are two exceptions.  One is inner classes; I want all of the templates for the inner classes to use the Java rather than STANAG4586Java templates.  The second is for helper classes that don't actually represent complete messages themselves, but instead represent some data structure that is repeated in multiple messages.  For instance, I have a TimeStamp class that is used in many Messages, yet it is not a message by itself.  I want the Java templates to take over in this case.
> 
> I think the second problem is easy to fix; in my domain object I have a method "isHelperClass" that I can call to determine whether to use the Java templates or the STANAG4586Java specialization templates; for those helper classes I would just use the plain Java.
> 
> The real tricky problem is in the inner classes.  The way I'm doing inner classes is as follows:
> 
> class(name, number, description, classAttributes, innerClasses, classComments, package)::=<<
> $packageDeclaration(package)$
> 
> $imports(...)$
> 
> $header()$
> 
> $classBody(...)$
> >>
> 
> classBody(name, number, description, classAttributes, innerClasses, classComments,innerClass)::=<<
> $startBlockComment()$
> $classComments; format="comment"$
> $endBlockComment()$
> 
> $classDeclaration(className=name, parentClass=parentClass)$ $startBlock()$
> 
>     // snip
> 
>     $innerClasses(...)$
> 
> $endBlock()$
> >>
> 
> where innerClasses is defined as follows:
> 
> innerClasses()::=<<
> $innerClasses:{inner | $classBody(name=inner.name, number="", classAttributes=inner.classAttributes, description=inner.comment, innerClasses=inner.innerClasses, classComments=inner.comment, innerClass=inner.innerClass)$}; separator="\n\n"$
> >>
> 
> In other words, I recursively call the class template, which handles all of the code generation I want to do.  The problem comes from the fact that I want these inner classes to use a different set of templates than the enclosing stanag message.  I'm getting around this by checking in the specialized templates whether the object I'm rendering is in fact a helper class or not:
> 
> declarations(variables)::=<<
> $if(!helperClass)$
> private PresenceVector presenceVector = new PresenceVector();
> 
> $super.declarations(rest(variables))$
> $else$
> $super.declarations(variables)$
> $endif$
> >>
> 
> I really don't like this approach because I'm repeating myself over and over again with the $if(!helperClass)$ conditional template inclusion.
> 
> 
> Am I missing something obvious?  Is there a better way of accomplishing what I'm trying to do?  I would appreciate any help.
> 
> Thanks,
> 
> -- 
> Nicholas Dunn
> 
> 
> 
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110503/298c41ed/attachment.html 


More information about the stringtemplate-interest mailing list