如何使用Xtext和Xtend生成AST?
所以我有 xtext 语法的默认示例。
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
greetings+=Greeting*;
Greeting:
'Hello' name=ID '!';
我想查看抽象语法树 - 特别是我想要相应的 xtend 文件,看起来像
package org.xtext.example.mydsl.generator
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
class MyDslGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
//TODO implement me
}
}
每当调用 doGenerate 时打印出 AST 的文本版本。我已经对此进行了大约两天的黑客攻击,我显然错过了一些基本的东西 - 有人可怜我吗? doGenerate 函数中会包含什么?
So I have the default example of a xtext grammar.
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
greetings+=Greeting*;
Greeting:
'Hello' name=ID '!';
I'd like to view the abstract syntax tree - in particular I'd like the corresponding xtend
file, which looks like
package org.xtext.example.mydsl.generator
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
class MyDslGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
//TODO implement me
}
}
To printout a textual version of the AST whenever doGenerate is called. I've been hacking at this for about two days and I'm clearly missing something fundamental - someone take pity on me? What would go in the doGenerate Function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
doGenerate 函数的参数是基于 EMF 的资源(基本上是文本文件的模型表示 - 换句话说,已解析链接的 AST)和用于写入文件系统的帮助程序类。
生成器的任务是创建生成的源代码(通常是 Java,但这不是必需的)。有关示例,请参阅博客文章 http://www.rcp-vision.com/?p =1573 - 最后有一个使用的示例代码生成器。
The parameters of the doGenerate functions are the EMF-based resource (basically a model representation of the textual file - in other words, an AST with the links resolved) and a helper class for writing into the file system.
The generators task is to create the generated source code (typically Java, but that is not required). For an example, see the blog post http://www.rcp-vision.com/?p=1573 - at the end there is a sample code generator used.