Click here to Skip to main content
12,633,985 members (24,364 online)
Click here to Skip to main content
Add your own
alternative version


68 bookmarked

Multiple Language Syntax Highlighting, Part 2: C# Control

, 12 Mar 2003
Rate this:
Please Sign up or sign in to vote.
Fast and furious colorizing library for source code (C, C++, VBScript, JScript, xml, etc.)
<!-- Add the rest of your HTML here -->


This article is an upgrade of the code submitted in Multiple Language Syntax Highlighting, Part 1: JScript, where a syntax highlighting scheme was proposed.

The technique and ideas for parsing have not change and, therefore, I will not explain the parsing/rendering process in this article. The user who would need more detailled can refer the article cited above. I must also point out that this article is intended to replace entirely the Javascript code in a ( near ?) future. 

As the previous article was an exercice to learn JScript, XSL and regular expression, I used this one to get a first contact with C#.

In the rest of the article, I will refer to the Javascript version as v1.0 and the C# as v2.0.

Moving to C#

As a C++ developper, I can tell you I was glad to quit JavaScript and get started with C# who had a much better (C++) flavour.

Wrapping of the JScript methods in a single C# was quite straightforward and doesn't not deserve much comments.  

CodeColorizer Class

This class is the kernel of the parser. You can colorize code using CodeColorizer.ProcessAndHighlightCode( string ).

Having that job done and the ported code running after fairly small time, it was time to use the power of C# and get things better.

New features

Avoiding Regular Expression Object Construction

In the v1.0, regular expression objects were created each time the parser would change context, although the regular expression string was remaining the same. This was leading to a great number of allocation-compilation of Regex objects (although I have question about object pooling, see Open question below).

A first improvement of the library was to store the Regex objects into a HashTable when parsing the syntax. The class implementing this dictionary is Collections.RegexDictionary.

Hence, when parsing, regular expression object do not need to be built and can be retreived in constant time from the table.

Open Question: does .NET cache regular expression strings in a pool ?

Handling the Case

The case sensitivity of a language can be specified using the argument not-case-sensitive={"yes" or "no" (default)} with the node language.


The parser contains a timer/counter ( see [1] for details ) to bench the transformation. At the end of the article, some benchmarking results are presented. 

Bencharkming quantities are:

  • CodeColorizer.BenchmarkPerChar who returns the number of second to parse a character.
  • CodeColorizer.BenchmarkAvgSec, the parsing time average,
  • CodeColorizer.BenchmarkSec, the last job parsing time

Easier Integration

The library comes with a custom web control that colorizes text.

The Project:

The projects shows the usage of the custom colorizer control. For further details, NDOC documentation has been generated.

You must modify web.config to specify where the xml, xsl files are. See ColorizerLibrary section.



[1]High Performance timer in C#
[2]Multiple Language Syntax Highlighting, Part 1: JScript


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jonathan de Halleux
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (

You may also be interested in...


Comments and Discussions

GeneralDistributing modified code Pin
Eric Woodruff15-Nov-06 21:12
memberEric Woodruff15-Nov-06 21:12 
Hi Jonathan,

I've been trying out your C# code colorizer and it works really well. I'd like to use it in the Sandcastle help file builder that I've developed to colorize the example code blocks. In case you don't know, Sandcastle is the new tool from Microsoft used to document .NET assemblies. The help file builder that I developed acts as a front end for it to make it easier to use. It's open source and is hosted at[^]. I've made several additions and changes to the code colorizer and was wondering if you'd mind my distributing the modified source code along with the source for the help file builder. I'll add a credit noting you as the original author in the About box and the help file and reference the original article on Code Project. If there's anything else you'd like me to do, just let me know. Below is a list of the changes I've made so far. Once I'm done, I can send you the modified version if you'd like it.

- Fixed bug in FindRule that caused it to return null if there was only one keyword set in a context.

- Modified XmlHelper to move trailing CR/LF's into their own code tag. This keeps all tags within a single line and we can more easily add line numbering and region folding to the end result.

Changes to highlight.css/.xml/.xsl:

- Reworked the keyword lists to share common keywords amongst similar languages.

- Added support for C# and VB.NET.

- Removed the preprocessor keyword list and added a generic handler for preprocessor directives in each language that needed it.

- Added include file handler for C and C++.

- Added number handler to each languages and the XSL transformation.

- Reworked the VB comment expression and moved the REM keyword into it as well.

- Modified the style names to be generic.


Generalrunning the demo Pin
Ori-2-Sep-06 4:38
memberOri-2-Sep-06 4:38 
Generalsource code Pin
zikha14-Aug-06 1:33
memberzikha14-Aug-06 1:33 
QuestionHow to integrate into the IDE Pin
Anonymous17-Dec-04 9:05
sussAnonymous17-Dec-04 9:05 
GeneralC# Syntax Pin
Bassam Abdul-Baki27-Apr-04 4:24
memberBassam Abdul-Baki27-Apr-04 4:24 
GeneralRe: C# Syntax Pin
Jonathan de Halleux27-Apr-04 4:40
memberJonathan de Halleux27-Apr-04 4:40 
GeneralRe: C# Syntax Pin
Bassam Abdul-Baki27-Apr-04 8:20
memberBassam Abdul-Baki27-Apr-04 8:20 
GeneralRe: C# Syntax Pin
Jonathan de Halleux28-Apr-04 23:19
memberJonathan de Halleux28-Apr-04 23:19 
GeneralRe: C# Syntax Pin
Bassam Abdul-Baki29-Apr-04 3:09
memberBassam Abdul-Baki29-Apr-04 3:09 
GeneralRe: C# Syntax Pin
Jonathan de Halleux29-Apr-04 3:14
memberJonathan de Halleux29-Apr-04 3:14 
GeneralHay alguna api para controlar llmadas telefonicas con net2phone Pin
MarcelCH1-Apr-04 7:10
memberMarcelCH1-Apr-04 7:10 
GeneralRe: Calling C# functions from MFC/C++ Pin
Alex Evans16-Feb-04 10:27
memberAlex Evans16-Feb-04 10:27 
GeneralRe: Calling C# functions from MFC/C++ Pin
Dave Bacher3-Mar-06 10:01
memberDave Bacher3-Mar-06 10:01 
GeneralCalling C# functions from MFC/C++ Pin
Alex Evans15-Feb-04 20:10
memberAlex Evans15-Feb-04 20:10 
GeneralRe: Calling C# functions from MFC/C++ Pin
Jonathan de Halleux15-Feb-04 22:54
memberJonathan de Halleux15-Feb-04 22:54 
QuestionRTF? Pin
Beater23-Jun-03 19:58
memberBeater23-Jun-03 19:58 
AnswerRe: RTF? Pin
Jonathan de Halleux23-Jun-03 22:07
memberJonathan de Halleux23-Jun-03 22:07 
GeneralRe: RTF? Pin
Ricardo Mendes6-Oct-03 9:01
memberRicardo Mendes6-Oct-03 9:01 
GeneralRe: RTF? Pin
Jonathan de Halleux6-Oct-03 9:32
memberJonathan de Halleux6-Oct-03 9:32 
GeneralOther langages supported Pin
DD le postier31-Mar-03 5:04
sussDD le postier31-Mar-03 5:04 
GeneralRe: Other langages supported Pin
Jonathan de Halleux28-Jul-03 14:30
memberJonathan de Halleux28-Jul-03 14:30 
GeneralGood stuff Pin
Rudi Larno12-Mar-03 22:07
sussRudi Larno12-Mar-03 22:07 
GeneralWhen??? Pin
Jonathan de Halleux13-Mar-03 1:37
memberJonathan de Halleux13-Mar-03 1:37 
GeneralIn answer to your open question... Pin
Ceiled12-Mar-03 6:56
memberCeiled12-Mar-03 6:56 
GeneralThough! Pin
Jonathan de Halleux12-Mar-03 8:24
memberJonathan de Halleux12-Mar-03 8:24 
GeneralRe: Though! Pin
Ceiled12-Mar-03 9:24
memberCeiled12-Mar-03 9:24 
GeneralRe: Though! Pin
Eric Woodruff14-Nov-06 7:25
memberEric Woodruff14-Nov-06 7:25 
GeneralWow ... Thanks ... Pin
Daniel S.11-Mar-03 21:31
memberDaniel S.11-Mar-03 21:31 
GeneralAdvise Pin
Jonathan de Halleux12-Mar-03 1:27
memberJonathan de Halleux12-Mar-03 1:27 
GeneralRe: Advise Pin
Daniel S.12-Mar-03 4:15
memberDaniel S.12-Mar-03 4:15 
GeneralRe: Advise Pin
Jonathan de Halleux13-Mar-03 1:41
memberJonathan de Halleux13-Mar-03 1:41 
GeneralRe: Advise Pin
Daniel S.13-Mar-03 1:55
memberDaniel S.13-Mar-03 1:55 
GeneralCode Pin
David Gallagher11-Mar-03 12:22
memberDavid Gallagher11-Mar-03 12:22 
GeneralFixed, sorry Pin
Jonathan de Halleux12-Mar-03 0:28
memberJonathan de Halleux12-Mar-03 0:28 
GeneralDownload link does not work. Pin
Patrick Lujan11-Mar-03 12:18
memberPatrick Lujan11-Mar-03 12:18 
GeneralRe: Download link does not work. Pin
Sajith M11-Mar-03 15:48
memberSajith M11-Mar-03 15:48 
GeneralFixed Pin
Jonathan de Halleux12-Mar-03 0:29
memberJonathan de Halleux12-Mar-03 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161208.2 | Last Updated 13 Mar 2003
Article Copyright 2003 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid