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

查看:25
本文介绍了将 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 应用程序运行,如下图所示.

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 语法解析高级规范,如步骤 3 所示.简而言之,我的要求是将用 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天全站免登陆