[Journal - Syntax Highlighting for Embedded Code]

Syntax Highlighting for Embedded Code

Wednesday, November 1, 2006

How's that for syntax highlighting? Just configure a language in WebEdit.NET so that it uses Gregor.Editing.Coloring.CEmbeddedLanguageParser as its syntax parser, specify the tokens that mark embedded code, and optionally say which language information to use for the embedded and embedding code, respectively.

For testing purposes, I've configured a new language called "Embedded Language", and assigned the CEmbeddedLanguageParser for syntax highlighting. The sample document "Test.embed" uses an ASP-like syntax for marking up embedded script (C#, in this example).

A little .NET Console user function lets me pick the parser for the outer code, the embedded code, or both. This way, whenever I want to focus on a given part of the code, I can dim out the other part:

configureEmbeddedLanguages(sOuter, sInner)
{
    lang = App.Context.LanguagePool.GetLanguageByExtension(".embed");
    lang.SyntaxParser.OuterLanguageName = sOuter;
    lang.SyntaxParser.InnerLanguageName = sInner;
    WebEditApp.EditManager.SetSyntaxColoring(true);
}

This mechanism works for any configured language that supports a syntax parser. Currently, it's limited to only one embedded language, and the embedded code has to be marked up by special tokens (which are compared literally). A scenario where several embedded languages are needed is editing HTML document - the may contain scripts as well as style sheets, for example. However, for this to work properly, sufficient knowlege of the embedding (outer) language is required, and therefore, specific syntax parsers should be used. CEmbeddedLanguageParser is for very simple cases like the above.

Implementing nested parsing - even recursively - is simple enough, given the support of the range classes in Gregor.Editing.Coloring. For examples, have a look at the CEmbeddedLanguageParser.Reparse method.