[antlr-interest] Generating text from models

Terence Parr parrt at cs.usfca.edu
Fri Dec 5 12:33:42 PST 2008


ha!  I knew I have done this:

http://www.codegeneration.net/tiki-read_article.php?articleId=65

IGenerate Java and XML code from the reflection API.

>
> thanks a lot. For a full model-to-text transformation you can then  
> merge the model's object tree and the corresponding template at the  
> top of the hierarchy, and the latter then pulls in further more fine- 
> grained templates as it is proceeds. And this is exactly what XPAND  
> or much rather oAW does, where you also (in their workflow XML)  
> associate model root and template root. I know (or better I have  
> read a bit) that oAW can do a lot of very advanced things, but I  
> currently feel that ST is much easier to get to. Needless to say  
> that I am living in .NET country most of the time... :-) So thanks  
> for this tool.

my pleasure

> Still have an issue left though: on a different project I am using  
> Microsoft's T4 code generator from their DSL tools. If you don't  
> know anything about it just think of ASP/JSP generators or JET in  
> Eclipse, templates look like "<% code %> literal text <%= value %>"  
> and so forth. The ability to put in code does allow for bad style,  
> but:
>
> In addition to just querying attributes from the context, you can  
> also pass parameters to (query) functions. And this is mandatory in  
> our case. Our input data is XML and the templates emit portions from  
> the XML by referring to XML nodes through XPATH. So in a template  
> you might find something like
>
> DoIt() first parameter: <%= Value( "//Method[@name='DoIt']/ 
> Parameters[0]" ) %>.
>
> Where Value( string xpathExpr ) is such a query operation to get the  
> referenced data from the XML. The problem is that we have a lot of  
> templates that all need different data portions so hardcoding those  
> queries in form of getters is not an option.
>
> Can ST do this as well?

Well, it depends on what the arguments are.  if it's an attribute or  
string of some kind, you can use the format option and a renderer.

Any time I need to pass an argument back into the model from the  
template, I ask myself whether I am allowing the model to leak into  
the template. If it looks the program, it probably is. That is not  
something you want to happen to your template. In your case, It's hard  
to tell. But, xpath stuff to pull specific nodes from a data structure  
makes me a bit nervous unless you are just descending through the  
various nodes. One could argue that the template should simply be  
displaying data, not encoding how to jump around and ask about tree.  
One test is to ask whether or not you could use that template with  
another data structure. just something to think about.

Ter


More information about the antlr-interest mailing list