Xtext:具有重要/语义空白的语言语法
如何使用 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.
推荐答案
AFAIK,你不能.
在解析类 Python 语言的情况下,您需要词法分析器发出 INDENT
和 DEDENT
标记.为此,您需要在词法分析器规则(Xtext 的 terminal
规则)中支持语义谓词,该规则将首先检查输入中下一个字符的当前位置是否等于0(行首)and 是 ' '
或 ' '
.
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 ' '
.
但是浏览 文档,我没有看到目前 Xtext 支持这一点.自 Xtext 2.0 起,已添加了对生产规则中语义谓词的支持(参见:6.2.8. Syntactic Predicates),但在终端规则中不支持.
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.
使用 Xtext 执行此操作的唯一方法是让词法分析器产生终端空格和换行符,但这会使您的生产规则完全混乱.
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.
如果您想使用 Java(和面向 Java 的解析器生成器)解析这样的语言,我建议您使用 ANTLR,您可以在其中 发出这样的 INDENT
和 DEDENT
标记很容易.但是,如果您热衷于 Eclipse 集成,那么我看不出您如何使用 Xtext 来做到这一点,抱歉.
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.
相关文章