Syntax Highligter with JabberWiki Lexer

This sample code shows how you can easily extend the JabberWiki lexer outside of the core wiki using a config file. With a few regular expressions and some UI work you can easily create a primitive syntax highlighter.

In this example I extended Uri Guy's syntax highlighting RichTextBox by replacing his line processing with the JabberWiki lexer. See his work here. I was able to create an ILexer instance on to lex each line as it is entered. Based on the symbol type of each token I then colored the text appropriately. A possible further extension of this example would be to define the colorization in a config file as well, so as not to require a recompile when adding a new type or changing the desired color.

Download example code

please note that this code is very rough and is intended for example only. there are bugs.
also, this example is built on a pre-release version of JabberWiki and interfaces and features may change in later versions.

Here is a copy of the config file used to create the syntax highlighter:
{code}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
<section name="jabberWiki"
type="JabberWiki.Configuration.WikiConfigurationSection,JabberWiki"/>
</configSections>

<jabberWiki>
<lex lexer="JabberWiki.Lex.Lexer,JabberWiki">
<tokens>
<add name="accessModifier"
tokenizer="JabberWiki.Lex.RegexTokenizer,JabberWiki"
matchText="(public|private|protected|internal|readonly|static|const)"
/>
<add name="nativeTypes"
tokenizer="JabberWiki.Lex.RegexTokenizer,JabberWiki"
matchText="(int|double|float|decimal|bool|string)"
/>
<add name="keywords"
tokenizer="JabberWiki.Lex.RegexTokenizer,JabberWiki"
matchText="(class|void|using|return|new|null|this|namespace|if|else|get|set)"
/>
<add name="strings"
tokenizer="JabberWiki.Lex.RegexTokenizer,JabberWiki"
matchText="&quot;.*&quot;"
/>
<add name="operators"
tokenizer="JabberWiki.Lex.RegexTokenizer,JabberWiki"
matchText="(\|\\+\-|==|=|&amp;&amp;|\.)"
/>
<add name="directive"
tokenizer="JabberWiki.Lex.RegexTokenizer,JabberWiki"
matchText="#.*"
/>
<add name="comment"
tokenizer="JabberWiki.Lex.RegexTokenizer,JabberWiki"
matchText="//.*"
/>
</tokens>
</lex>
</jabberWiki>
</configuration>
{code}

And here is a screenshot of the application running:
screenshot.png

Last edited Sep 18, 2007 at 5:45 PM by troyschneringer, version 6

Comments

No comments yet.