Sign in to follow this  
TheBluMage

Syntax highlighting

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this