问题描述
如何使用Xtext来解析语义空白的语言?我试图为CoffeeScript编写语法,但我找不到任何有关这方面的文档。
AFAIK不能。
在解析Python类语言的情况下,您需要lexer发出 INDENT
和 DEDENT
令牌。为了实现这一点,你需要在lexer规则(Xtext的 terminal
规则)内支持语义谓词,它首先检查当前位置在线下一个字符int输入等于0(行的开头)和是''
或'\t '
。
但浏览,我不认为这是Xtext支持的时刻。由于Xtext 2.0已经为生产规则中的语义谓词添加了支持 (参见: 6.2.8。句法谓词 b
$ b
使用Xtext的唯一方法是让lexer产生终端空间和换行符,但是这会使你的生产规则完全混乱。
如果你想使用Java(和面向Java的解析器生成器)解析这样的语言,我会推荐ANTLR,你可以发出这样的 INDENT
和 DEDENT
令牌。但是如果你热衷于Eclipse集成,那么我不知道你将如何使用Xtext,这样做。对不起。
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, 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.
这篇关于Xtext:具有重要/语义空白的语言的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!