使用 Antlr/Stringtemplates 编写翻译器
我想写一个翻译器。这个想法是将特殊形式的 C++ 接口转换为 C++/CLI。我有一个 antlr 语法,可以解析所有内容并生成 AST。现在我想使用这些信息和一些字符串模板来发出源代码。
我的想法是将 AST 转换为某种具有属性的对象层次结构(例如,包含索引属性方法的接口对象,其中包含方法描述对象。 然后向主字符串模板提供根对象,并将属性插入正确的位置或将它们传递给子模板。
现在我的问题是: 如何编写需要调用未定义次数的字符串模板/属性?示例:一个接口包含多个方法。这意味着,方法的子模板需要调用多次,每次都使用不同的属性。我怎样才能把它写成字符串模板和字符串模板的组合?索引财产?
感谢您的帮助 托比亚斯
I want to write a translator. The idea is to translate special formed C++ interfaces to C++/CLI. I have an antlr grammar that parses everything and generates an AST. Now I want to use this information and some string templates to emit source code.
My idea was to transform the AST in some kind of object hierarchy with properties (e.g. an interface object containing the indexed property methods which contains method-description-objects.
The master string template is then fed with the root object and inserts the properties at the correct positions or passes them to sub-templates.
Now my question:
How do I write a string template / property that needs to be called some undefined number of times? Example: an interface contains a number of methods. This means, that the subtemplate for method needs to be called several times, each time with a different property. How can I write this down as a mix of stringtemplate & indexed property?
Thank you for your help
Tobias
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在做一些非常相似的事情。基本思想是您的模型必须公开某个对象的列表,并且您在字符串模板中使用该列表。例如,假设我有一个非常脑残的实现。我将使用 Java,因为这是我最了解的;你应该明白了。
https://gist.github.com/894632
对于我的模板,我可能有以下内容:
关键是我在功能上为我拥有的每个方法对象应用一个模板;这就是
$methods:method()
的作用。如果我有一个空列表,则根本不会调用任何模板。这处理了可变大小的问题。我在方法定义中做了类似的事情;($method.arguments:argument();separator=","$)
。这将在括号之间创建一个以逗号分隔的方法参数列表,就像您所期望的那样。I'm doing something very similar. The basic idea is that your model must expose a list of some object, and you use that list within your string templates. For instance, let's say I have a very braindead implementation. I'm going to use Java because that's what I know best; you should get the idea.
https://gist.github.com/894632
For my template I might have the following:
The key is that I functionally apply a template for each method object I have; that's what
$methods:method()
does. If I had an empty list, no template would be invoked at all. This handles the variable size problem. I do a similar thing within the method definition;($method.arguments:argument(); separator=","$)
. This is going to create a comma separated list of method parameters in between parentheses, just like you'd expect.