How to include Java Code Block in Xtext DSL?

我们两清 提交于 2020-01-13 19:12:06

问题


In an Xtext grammar I can include an Xbase code block using the XExpression/XBlockExpression types. How would I go to do the same with a Java code block? The included java should later be included in the Java generated from the DSL constructs.


回答1:


You can do this now with JBase (https://github.com/LorenzoBettini/jbase). JBase was created specifically for the use case you described (see https://dl.acm.org/citation.cfm?id=2972217) and can be incorporated with minor changes by extending JBase instead of XBase.




回答2:


It is not possible to directly include Java code blocks (unless you are prepared to write an entire Java grammar and corresponding tooling support, e.g. variable resolution or code generation).

However, Xbase code blocks can be translated into Java code blocks using built-in Xtext services in the model inferrer or using the XbaseCompiler. It is also possible to simply interpret it with the XbaseInterpreter class.

Finally, if you really need the Java code blocks, you could write them in .java files, and refer to them using JvmIdentifiableElements.



来源:https://stackoverflow.com/questions/12196448/how-to-include-java-code-block-in-xtext-dsl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!