linking xtext editor support with external ANTLR parser

时光怂恿深爱的人放手 提交于 2019-12-24 12:01:49

问题


My current project (name it IoTSuite) takes high-level specifications, parses them, and generates code in Java and Android. In this project, I have written ANTLR grammar to parse the high-level specification and I have used StringTemplate for the code generator.

However, due to nice editor support and syntax coloring features, I have used the xtext grammar(same as the ANTLR grammar, but it has been written in xText). Now, I perform the following three steps:

Step 1: I have written xtext grammar as show in the following image.

Step 2: Now, I generate the supporting artifacts from MyDsl.xtext grammar and then run the whole project as Eclipse Application, as shown in the following image.

Step 3: After this, Eclipse application runs that allows me to write high-level specifications, as shown in the following image.

Now, my requirement is - I would like to parse the high-level specification, as shown in the step3, using the ANTLR grammar written in IoTSuite project. In brief, my requirement is to connect the high-level specification written in xtext with the ANTLR parser. How could I do it? Please let me know in case you need more clarity for the solution.


回答1:


Xtext generates its own ANTLR grammar. This grammar is also instrumented with code to instantiate the AST for the grammar. The produced parser cannot be exchanged.

What you likely want to do is to invoke your own ANTLR parser when saving a resource. The IGenerator hook could be used to invoke your parser and generator.

Please consider also to use Xtend for code generation, that's much nicer than StringTemplate and integrates smoothly with Xtext infrastructure. Xtext's Statemachine Example shipped with the SDK shows how to do this (and to use the IGenerator interface).



来源:https://stackoverflow.com/questions/33684328/linking-xtext-editor-support-with-external-antlr-parser

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