[antlr-interest] String template questions.
PSheehan at decaresystems.ie
PSheehan at decaresystems.ie
Mon Apr 7 09:21:28 PDT 2008
I've got a simple grammar up and running which generates Java beans and
Hibernate config files. I have a few questions as I'm not really sure if
I am going about things in the correctly way. My solution uses a lexer
and parser to create an AST which I then pass on to a tree walker for the
generation of the output.
1. How do people generally create multiple files from a single tree
output. For example if the result of the tree needs to create Bean1.java
and Bean2.java. Currently I've created a streamer strategy which my tree
walker calls when it needs be, passing the output of a template to it. The
strategy then knows how to create java files in the correct package
structure. I can then easily change the strategy in order to write
Hibernate config files. Is this a valid approach or should I just get the
tree walker to roll up all the template results and break them up after
the walker has finished.
2. I've created one entry point into my templates from my grammar like
so '%modeltmp($namespace,$name,$fieldDefinations)'. Only one rule in the
tree walker invodes a grammar, the other rules just gather the information
needed to pass to the template. The main reason I did this was so I could
call different templates from within the main template to produce
different output given the same input. For example I could generate a
constructor, bean properties and a 'toString' method all from
$fieldDefinations. Is there a better way of doing this? Should I move
some of this work back into my parser so it creates, in the AST, a
constructor node with field definations as childern or a 'toString' node
with field definations as childern etc.
Here is a snippet of what I'm doing in the tree grammar.
model
scope{
ArrayList fieldDefList;
}
@init{
$model::fieldDefList=new ArrayList();
}
: ^(MODEL TYPE fs+=fielddef+)
{
StringTemplate
temp=%modeltmp(namespace={$stat::namespaceDef},name={$TYPE},fields={$model::fieldDefList});
streamer.write($stat::namespaceDef,$TYPE.text,temp.toString());
}
;
fielddef
: ^(FIELD TYPE ID) {$model::fieldDefList.add(new
SimpleFieldType($TYPE.text,$ID.text));}
;
namespace
: ^(NAMESPACE ^(NAMESPACEMETA (ids+=ID)+))
{$stat::namespaceDef=$ids;}
;
Thanks,
Paul
This e-mail and any files transmitted with it are intended solely for the individual or entity to whom they are addressed. This communication may contain material that is privileged, confidential and protected from disclosure under the law. If you are not the intended recipient, any disclosure, distribution, copying, or use of this information is strictly prohibited and may be unlawful.
If you have received this e-mail in error, please reply immediately to the sender and delete it.
Registered Address:
DeCare Systems Ireland Ltd,
Building 1, University Technology Centre, Curraheen Road, Cork, Ireland.
Company Registration Number: 278462
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080407/358ea24a/attachment-0001.html
More information about the antlr-interest
mailing list