使用ANTLR的代码编辑器 [英] Code Editor using ANTLR

查看:100
本文介绍了使用ANTLR的代码编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为自定义语言编写代码编辑器.我们将ANTLR用于词法分析器和解析器,并将CodeMirror用于编辑器框架(在浏览器中运行).

我可以做一些基本的事情,例如关键字的语法着色,以及提供基本的代码补全.

我发现的是用户经常在编辑某些内容,因此ANTLR解析器不是很有用,因为当前的输入流无法完全解析(并且由于操作不当,常常导致ANTLR走错路径)输入流不完整).

因此,我正在使用令牌流来了解正在发生的事情,并尝试提供上下文相关的帮助.

我想知道是否有人可以提供有关使用ANTLR作为代码编辑器一部分的指导.我是否在使用令牌流而不是解析树在正确的轨道上?

是否可以利用ANTLR API来完成诸如预找令牌以弄清用户当前正在编辑内容的整体上下文之类的事情?

抱歉,这有点含糊.刚开始这个项目. :-)

感谢您的帮助.

解决方案

我发现ANTRL非常适合语法检查和简单的详细信息检索(对于有效输入).但是对于代码完成,您有不同的情况.正如您已经发现的那样,解析器通常不能为您提供良好的答案,因为在用户键入时输入无效.巴特(Bart)链接到一个答案,萨姆(Sam)说他使用ANTLR 4实现了一个很好的解决方案,但不幸的是没有说明如何实现.

但是,即使您可以使解析器为您提供一组预期的标记,您将如何使用它呢?如果要使用标识符,您想显示什么?可以是任何东西,例如类成员,变量名等.我不认为这是答案,因此我开发了自己的解决方案,在此进行描述: antlr4-c3 .这是一种Typescript解决方案,但随附了 Java C ++ .

I'm in the process of writing a code editor for a custom language. We're using ANTLR for the lexer and parser, and CodeMirror for the editor framework (running in a browser).

I can do some basic things like syntax coloring of keywords, as well as providing rudimentary code completion.

What I'm finding is that the user is frequently in the middle of editing something, so the ANTLR parser is not very useful since the current input stream is not fully parseable (and often leads ANTLR down the wrong path due to an incomplete input stream).

Therefore, I'm using the token stream to figure out what's going on and attempt to provide context-sensitive help.

I'm wondering if anyone can provide some guidance around using ANTLR as part of a code editor. Am I on the right track using the token stream instead of the parse tree?

Can the ANTLR API be leveraged to do things like looking ahead for tokens to figure out the overall context of what the user is currently editing?

Sorry if this is kind of vague. Just getting started on this project. :-)

Thanks for any help.

解决方案

I find ANTRL great for syntax checking and easy details retrieval for valid input. For code completion however you have a different scenario. As you found out already the parser can often not give you good answers, because the input is not valid while the user is typing. Bart has linked to an answer where Sam described that he implemented a great solution using ANTLR 4, but unfortunately doesn't describe how.

But even if you can get the parser to give you a set of expected tokens, what are you going to make out of it? What do you wanna show if, say, an identifier is expected? That can be anything, like a class member, a var name etc. I don't believe this is the answer, hence I developed an own solution which I describe here: Universal Code Completion using ANTLR. This is for ANTLR 3, but can certainly be made working with 4 as well.

This article also contains several links to (C++) source code that shows how code completion is implemented in my application. It's amazing how simple the implementation is, after all, but still delivers very precise results.

Update

Meanwhile I developed a solution for ANTLR4 too, called antlr4-c3. This is a Typescript solution but comes with translations to Java and C++.

这篇关于使用ANTLR的代码编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆