[英]I have a grammar in my vscode extension. I want to programmatically identify comments in a TextLine or TextDocument object
I'm working on a VSCode extension for the ChucK music programming language. 我正在为ChucK音乐编程语言开发VSCode扩展。 I have released a version which just does syntax highlighting using a
.tmLanguage.json
file. 我发布了一个仅使用
.tmLanguage.json
文件进行语法高亮显示的版本。 Now I'm working on a syntax checker. 现在,我正在研究语法检查器。
The syntax error location identified by ChucK is a bit confusing, and it would clearer if I extended the range to just past the end of the previous statement or block, which would mean looking backwards for the previous ;
ChucK标识的语法错误位置有些混乱,如果我将范围扩展到前一个语句或块的末尾,这将更清楚,这意味着向后看前一个
;
or }
character. 或
}
字符。
This would be simple, except for the fact I need to skip comments. 这很简单,除了我需要跳过评论的事实。 ChucK has C++ style comments, with both
//
line comments and /* */
block comments. ChucK具有C ++样式的注释,并带有
//
行注释和/* */
块注释。 These are correctly highlighted by my grammar file. 这些由我的语法文件正确突出显示。
Is there any way I can get at the syntax information the grammar has already produced to identify these comments? 我有什么办法可以获取语法所产生的语法信息来识别这些注释? Scanning the docs, it appears that I may be able to grab visual attributes like text color, but that seems theme-dependent and error-prone.
扫描文档后,看来我也许能够抓住诸如文字颜色之类的视觉属性,但这似乎与主题相关且容易出错。 How can my code get directly at how the grammar has identified a particular character or region?
我的代码如何直接了解语法如何识别特定字符或区域?
If you want to do error checking for a custom language you will have to write a full parser for that. 如果要对自定义语言进行错误检查,则必须为此编写一个完整的解析器。 Many extension authors have done that (or use an external parser like clang for C++).
许多扩展作者已经做到了这一点(或者使用外部解析器,例如C ++的clang)。 Once you got the syntax structure it is easy to find certain ranges of syntactic elements and the parser will give you the error location (if there's an error).
一旦获得了语法结构,就很容易找到一定范围的语法元素,并且解析器将为您提供错误位置(如果有错误)。
I successfully used ANTLR4 (via the antlr4ts typescript runtime) to parse and process a custom language (in my case ANTLR4 grammars). 我成功地使用了ANTLR4(通过antlr4ts打字稿运行时)来解析和处理自定义语言(以我的情况为ANTLR4语法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.