问题
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