Xtext:具有重要/语义空白的语言的语法

发布于 2024-12-01 02:23:19 字数 70 浏览 1 评论 0原文

如何使用 Xtext 解析带有语义空白的语言?我正在尝试为 CoffeeScript 编写语法,但找不到任何关于此的好的文档。

How can I use Xtext to parse languages with semantic whitespace? I'm trying to write a grammar for CoffeeScript and I can't find any good documentation on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜警司 2024-12-08 02:23:19

以下是 XText 中空白敏感语言的示例

Here's an example whitespace sensitive language in XText

故事还在继续 2024-12-08 02:23:19

AFAIK,你不能。

在解析类似 Python 的语言时,您需要词法分析器发出 INDENTDEDENT 标记。为此,您需要在词法分析器规则(Xtext 的 terminal 规则)内支持语义谓词,该规则首先检查输入中下一个字符的当前位置是否等于0(行的开头)并且' ''\t'

但是浏览 文档,我看不到这是目前由 Xtext 支持。自 Xtext 2.0 起,已在产生式规则中添加了对语义谓词的支持(请参阅:6.2.8. 句法谓词),但在终端规则中未添加支持。

使用 Xtext 执行此操作的唯一方法是让词法分析器生成终端空格和换行符,但这会使您的生产规则完全混乱。

如果您想使用 Java(以及面向 Java 的解析器生成器)解析此类语言,我建议使用 ANTLR,您可以在其中发出此类 INDENTDEDENT 令牌很容易。但是,如果您热衷于 Eclipse 集成,那么抱歉,我不知道如何使用 Xtext 来做到这一点。

AFAIK, you can't.

In case of parsing Python-like languages, you'd need the lexer to emit INDENT and DEDENT tokens. For that to happen, you'd need semantic predicates to be supported inside lexer rules (Xtext's terminal rules) that would first check if the current-position-in-line of the next character int the input equals 0 (the beginning of the line) and is a ' ' or '\t'.

But browsing through the documentation, I don't see this is supported by Xtext at the moment. Since Xtext 2.0, support has been added for semantic predicates in production rules (see: 6.2.8. Syntactic Predicates), but not in terminal rules.

The only way to do this with Xtext would be to let the lexer produce terminal spaces and line-breaks, but this would make an utter mess of your production rules.

If you want to parse such a language using Java (and a Java oriented parser generator) I'd recommend ANTLR, in which you can emit such INDENT and DEDENT tokens quite easily. But if you're keen on Eclipse integration, then I don't see how you'd be able to do this using Xtext, sorry.

萌梦深 2024-12-08 02:23:19

Xtext 2.8 版支持空白感知语言。此版本附带“家庭自动化示例”,您可以将其用作模板。

Version 2.8 of Xtext comes with support for Whitespace-Aware Languages. This version ships with the "Home Automation Example" that you can use as a template.

a√萤火虫的光℡ 2024-12-08 02:23:19

对于对 CoffeeScript 感兴趣的人,Adam Schmideg 有一个 使用 XText 的 Eclipse 插件

对于有兴趣在 XText 中解析类似 Python 的 DSL 的人,上述 Ralf Ebert 的 Todotext 代码不再可以从 Github 上获得,但您可以在 Eclipse 测试存储库。请参阅有关此工作的原始线程以及Eclipse 问题 被提出。

我今天一直在研究这段代码,我的结论是它在当前版本的 XText 中不再有效。当XText在Eclipse中使用时,我认为它做了“部分解析”。这与处理缩进敏感语言所需的有状态词法分析器不兼容。所以我怀疑即使你修补了词法分析器,Eclipse 编辑器也无法工作。在该问题中,看起来 Ralf 提出了解决这些问题的补丁,但是查看 XText 源代码,这些更改似乎早已消失?如果我错了并且有人可以让它发挥作用,我会很感兴趣吗?

有一个不同的实现 这里,但我也无法让它与当前版本的 XText 一起使用。

相反,我已切换到 parboiled 它确实支持 基于缩进的语法开箱即用。

For people interested in CoffeeScript, Adam Schmideg has an Eclipse plugin that uses XText.

For people interested in parsing Python-like DSL's in XText, Ralf Ebert's code for Todotext mentioned above is no longer available from Github but you can find it in the Eclipse test repository. See the original thread about this work and the Eclipse issue that was raised about it.

I have been playing with this code today and my conclusion is it no longer works in the current version of XText. When XText is used in Eclipse, I think it does "partial parsing". This is not compatible with the stateful lexer you need to process indentation sensative languages. So I suspect even if you patch the lexer, the Eclipse editor does not work. In the issue, it looks like Ralf proposed patches to address these issues, but looking into the XText source, these changes seem long gone? If I am wrong and someone can get it to work, I would be very interested?

There is a different implementation here but I cannot get that to work with the current version of XText either.

Instead I have switched to parboiled which does supports indentation based grammars out the box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文