简体   繁体   中英

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. I have released a version which just does syntax highlighting using a .tmLanguage.json file. 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 ; 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. 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++). 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM