编写没有语法文件的自定义 Xtext/ANTLR 词法分析器
我正在为 CoffeeScript 编写一个 Eclipse/Xtext 插件,我意识到我可能需要手动为其编写一个词法分析器。 CoffeeScript 解析器还使用 手写词法分析器处理语法中的缩进和其他技巧。
Xtext 生成一个扩展 org.eclipse.xtext.parser.antlr.Lexer 的类,该类又扩展 org.antlr.runtime.Lexer 。所以我想我会延长它。我可以看到两种方法来
- 覆盖
mTokens()
。这是通过生成的代码来完成的,改变内部状态。 - 覆盖
nextToken()
这似乎是一种自然的方法,但随后我必须跟踪内部状态。
我找不到任何示例如何在没有语法文件的情况下为 ANTLR 编写一个简单的词法分析器。所以最简单的答案是指向一个的指针。
Xtext:具有重要/语义空白的语言的语法 指的是 todotext通过更改底层输入流中的标记来解决缩进问题。我不想走那条路,因为处理咖啡脚本语法的其他技巧会很困难。
更新:
我同时意识到我的问题部分是 Xtext 特定的。
I'm writing an Eclipse/Xtext plugin for CoffeeScript, and I realized I'll probably need to write a lexer for it by hand. CoffeeScript parser also uses a hand-written lexer to handle indentation and other tricks in the grammar.
Xtext generates a class that extends org.eclipse.xtext.parser.antlr.Lexer
which in turn extends org.antlr.runtime.Lexer
. So I suppose I'll have extend it. I can see two ways to do that
- Override
mTokens()
. This is done by the generated code, changing the internal state. - Override
nextToken()
which seems a natural approach, but then I'll have to keep track of the internal state.
I couldn't find any example how to write even a simple lexer for ANTLR without a grammar file. So the easiest answer would be a pointer to one.
An answer to Xtext: grammar for language with significant/semantic whitespace refers to todotext which handles the problem of indentation by changing the tokens in the underlying input stream. I don't want to go that way, because it would be difficult to handle other tricks of the coffeescript grammar.
UPDATE:
I realized in the meantime that my question was partly Xtext specific.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我所做的——而且它有效。
然后我有一个稍微定制的解析器,其中包含:
我还必须更改我的
MylangRuntimeModule.java
以包含此方法就是这样。
Here is what I did -- and it works.
Then I have a slightly customized parser containing:
I also had to change my
MylangRuntimeModule.java
to contain this methodAnd that's it.
另一种方法(无需创建自定义解析器)是通过扩展 Xtext 的词法分析器 (org.eclipse.xtext.parser.antlr.Lexer) 来创建自定义词法分析器,如下所示:
然后将其绑定到模块中:
如果您想看一下完整的示例,我为 StringTemplate 的基于 Xtext 的编辑器实现了一个自定义词法分析器,名为 hastee 。
Another way (without the need to create a custom parser) is to create a custom lexer by extending Xtext's lexer (org.eclipse.xtext.parser.antlr.Lexer) as follows:
Then you bind it in your module:
If you want to have a look at a complete example, I have implemented a custom lexer for an Xtext-based editor for StringTemplate called hastee.