Writing a translator using Antlr/Stringtemplates -
i want write translator. idea translate special formed c++ interfaces c++/cli. have antlr grammar parses , generates ast. want use information , string templates emit source code.
my idea transform ast in kind of object hierarchy properties (e.g. interface object containing indexed property methods contains method-description-objects. master string template fed root object , inserts properties @ correct positions or passes them sub-templates.
now question: how write string template / property needs called undefined number of times? example: interface contains number of methods. means, subtemplate method needs called several times, each time different property. how can write down mix of stringtemplate & indexed property?
thank tobias
i'm doing similar. basic idea model must expose list of object, , use list within string templates. instance, let's have braindead implementation. i'm going use java because that's know best; should idea.
https://gist.github.com/894632
public class generatedclass { private string accessmodifier; private string name; private string superclass; private list<method> methods; } public class method { private string comments; private string name; private string accessmodifier; private type returntype; private list<argument> arguments; private string body; } public class argument { private type type; private string name; } public class type { private string name; }
for template might have following:
group java; class(accessmodifier, name, superclass, methods)::=<< $accessmodifier$ class $name$ extends $superclass$ { $methods:method(); separator="\n"$ } >> method(method)::=<< /** $method.comments$ */ $method.accessmodifier$ $method.returntype.name$ $name$ ($method.arguments:argument(); separator=","$) { $method.body$ } >> argument(argument)::=<< $argument.type.name$ $argument.name$ >>
the key functionally apply template each method object have; that's $methods:method()
does. if had empty list, no template invoked @ all. handles variable size problem. similar thing within method definition; ($method.arguments:argument(); separator=","$)
. going create comma separated list of method parameters in between parentheses, you'd expect.
Comments
Post a Comment