Syntax highlighting

Started by
3 comments, last by TheBluMage 19 years, 4 months ago
Hi, I'm writing a syntax highlighter for C++/CLI and so far, all of the one-line highlighting (single keywords, strings, etc.) is working fine. I would also like to be able to highlight multi-line comments, preprocessor macros and other things that can extend beyond one line. Right now I just check to see how many characters have been entered or deleted (when the text is changed) and use the cursor position to determine which lines need to have their syntax re-highlighted. I can't apply the same technique to multi-line targets, as I'm not sure how to tell where highlighting should begin and/or end. I've Googled around, but I haven't been able to find anything that really goes into depth on this subject. If anyone knows of any articles, links, or info that they can post, it is much appreciated!
Advertisement
The most common method of syntax highlighting is using regular expressions to match the various syntactic elements, and highlighting the entirety of the match appropriately. Alternatively, a full context free parser can be used to identify the syntactic elements in exactly the same way the compiler recognizes them.

Scintilla, on the other hand, uses custom lexers that perform essentially the same function as the regexps, but in a cruder yet clearer fashion.
---New infokeeps brain running;must gas up!
Using a parser class may help you to make a syntax highligh editor not only for C/C++ but for html, VB, etc...
You can download the source for loads of text editors for free and read how they do it. I would paste how Emacs does it, but it's based on compiled emacs lisp for a 'mode'. IIRC, it uses something similar to the regexps that Flarelocke mentioned.
I didn't realize how powerful regular expressions could be until I started looking into them today. I think I'll be able to work something out with them. Thanks for your replies!

This topic is closed to new replies.

Advertisement