将xtext编辑器支持与外部ANTLR解析器链接 [英] linking xtext editor support with external ANTLR parser

查看:162
本文介绍了将xtext编辑器支持与外部ANTLR解析器链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的项目(命名为IoTSuite)采用了高级规范,对其进行了解析,并使用Java和Android生成了代码.在这个项目中,我编写了ANTLR语法来解析高级规范,并使用StringTemplate作为代码生成器.

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.

但是,由于良好的编辑器支持和语法着色功能,我使用了xtext语法(与ANTLR语法相同,但是它是用xText编写的).现在,我执行以下三个步骤:

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:

步骤1:我编写了xtext语法,如下图所示.

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

步骤2:现在,我从MyDsl.xtext语法生成支持工件,然后将整个项目作为Eclipse Application运行,如下图所示.

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.

第3步:此后,运行Eclipse应用程序,使我可以编写高级规范,如下图所示.

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

现在,我的要求是-我想使用IoTSuite项目中编写的ANTLR语法解析高级规范,如step3中所示. 简而言之,我的要求是将以xtext编写的高级规范与ANTLR解析器连接.我该怎么办?如果您需要更清晰的解决方案,请告诉我.

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.

推荐答案

Xtext生成自己的ANTLR语法.该语法还使用代码实例化该语法的AST.产生的解析器无法交换.

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.

您可能想做的是在保存资源时调用自己的ANTLR解析器. IGenerator挂钩可用于调用解析器和生成器.

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.

也请考虑使用Xtend进行代码生成,这比StringTemplate更好,并且可以与Xtext基础结构顺利集成. SDK附带的Xtext状态机示例演示了如何执行此操作(以及使用IGenerator接口).

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).

这篇关于将xtext编辑器支持与外部ANTLR解析器链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆