我使用 Xtext 来定义我的 DSL。 Xtext 生成一个解析器,让我可以遍历输入 DSL 的 EMF 模型。我想将此 EMF 模型转换为其他树。为了进行这个翻译,我需要遍历这棵树。但是,我找不到 Xtext 生成的 EMF 模型的访问者类。我发现的最接近的是访问单个节点的 Switch 类。我可以自己遍历 EMF 模型并在我访问的每个节点上调用 Switch 类。但是,我想知道 Xtext 中是否存在实现模型遍历的访问者功能。
I'm using Xtext to define my DSL. Xtext generates a parser that lets me traverse the EMF model of my input DSL. I'd like to translate this EMF model into some other tree. To do this translation, I need to traverse the tree. But, I couldn't find a visitor class for the EMF model generated by Xtext. The closest thing that I've found is a Switch class that visits a single node. I can traverse the EMF model myself and invoke the Switch class on each node that I visit. But, I wonder if there exists a visitor functionality in Xtext that implements the model traversal.
发布评论
评论(3)
如果您考虑此线程,请切换 (此处使用)几乎是 XText 的官方访问者模式。< br>
您还拥有 自定义验证流程:
但这可能并不完全是您所需要的。
If you consider this thread, Switch (used here for instance) is pretty much the official visitor pattern for XText.
You also have the custom Validation process:
That may be not exactly what you need, though.
我想 EcoreUtil.getAllProperContents(Resource, false) 可能就是您正在寻找的。您将获得一个 TreeIterator 来遍历资源的内容。将 iter.next() 的结果传递到 Switch 中,并根据元素类型执行转换逻辑。
I guess EcoreUtil.getAllProperContents(Resource, false) might be what you are looking for. You'll get a TreeIterator that will traverse the contents of the resource. Pass the result of iter.next() into a Switch and perform the transformation logic depending on the element type.
嗯,使用 EMF API 还不够吗?如果你想转换模型,使用一些像Xtend这样的转换语言是不可能的吗?
Hmm, is it not sufficient to use EMF API? If you want to transform the model, is using some of the transformation langauges like Xtend not possible?