ANTLR: How to avoid re-parsing entire file when user modifies text

Viewed 733

edit: For those interested/who want to see exactly what I'm doing, the source code of my app can be found here.


I'm building a code editor app with C# that offers syntax highlighting. I'm currently using ANTLR for C# to parse the code in order to highlight it. So far, my app can highlight the code really fast when the user initially opens the file. However, I haven't written any code to re-highlight the text when the user starts editing it.

I want the editor to perform well for large files, so I don't want to re-parse the entire file each time the user types a character. I did a bit of research, and it seems like what I'm looking for is an incremental parser. Unfortunately, it seems like ANTLR v4 can't do incremental parsing, so I'm unsure what to do.

My question is: is there another approach I can take, using ANTLR, to not freeze the app whenever the user types? I'm really hesitant to give up on ANTLR since there are a bunch of free grammars available for it, so it's not much work to add support for a new language. I've looked into TextMate grammars, VSCode uses lots of them, but I don't understand them and there are no C# libraries available to manipulate them.

Thanks for helping!

2 Answers
Related