Ayende @ Rahien

Unnatural acts on source code

Custom Syntax Highlighting

Just using the Boo syntax isn't really enough in many cases, you want to handle your own custom keywords, behaviors, etc.

#Develop make this a piece of cake, since it defines the syntax highlighting using an XML file, and handles the actual parsing and coloring on its on. Here is the overall structure of such a file:

<?xml version="1.0"?>
<SyntaxDefinition name="Boo" 
                  extensions=".boo">
  <Environment>
    <Default bold="false"
             italic="false"
             color="SystemColors.WindowText"
             bgcolor="SystemColors.Window" />
    <Selection bold="false"
               italic="false"
               color="SystemColors.HighlightText"
               bgcolor="SystemColors.Highlight" />
  </Environment>

  <Digits name="Digits"
          bold="false"
          italic="false"
          color="DarkBlue" />

  <RuleSets>
    <RuleSet ignorecase="false" >
      <Delimiters>&amp;&lt;&gt;~!@$%^*()-+=|\#/{}[]:;"' ,	.?</Delimiters>

      <Span name="LineComment"
            stopateol="true"
            bold="false"
            italic="false"
            color="Gray" >
        <Begin >#</Begin>
      </Span>

      <KeyWords name="JumpStatements"
                bold="false"
                italic="false"
                color="Navy" >
        <Key word="break"/>
        <Key word="continue"/>
        <Key word="return"/>
        <Key word="yield"/>
        <Key word="goto" />
      </KeyWords>

    </RuleSet>
  </RuleSets>
</SyntaxDefinition>

As you can see, this is pretty easy to work with. Now let us add our own keywords:

<KeyWords name="DslKeywords"
          bold="false"
          italic="false"
          color="DarkOrange" >
  <Key word="specification"/>
  <Key word="users_per_machine"/>
  <Key word="requires"/>
  <Key word="same_machine_as"/>
</KeyWords>

Now we need to load the new language definition (don't forget to change the name, I changed it to "dsl") to the editor an select it:

HighlightingManager.Manager.AddSyntaxModeFileProvider(
    new FileSyntaxModeProvider(@"C:\Path\to\language\definition"));
//.. setup text editor
editorControl.SetHighlighting("dsl");

The result?

image

Comments

Tobin Harris
08/20/2008 02:40 PM by
Tobin Harris

Good work! Looks great!

josh
08/20/2008 03:01 PM by
josh

huh. interesting & pretty cool. Do you use SharpDevelop often?

Andrey Shchekin
08/20/2008 07:46 PM by
Andrey Shchekin

Does not the Boo-provided AST have enough information to do the highlighting? I've never lloked at the Boo compiler, but it is probably possible to get AST annotated with line/column numbers?

One of my dream projects was building ANTLR/Coco grammar-based highlighting support into Visual Studio -- basically a VS/proj configuration option ".x is handled by ....\x.grammar". Unfortunately actually implementing such thing can take an awful lot of time.

Ayende Rahien
08/20/2008 08:08 PM by
Ayende Rahien

Andrey,

It does, but you don't want to do that. It is too complex.

You suggestion is what #Develop does, but with XML instead of .g files

Andrey Shchekin
08/21/2008 11:54 AM by
Andrey Shchekin

As far as I see from this XML, it is not a grammar -- it is a keyword list.

So it is not possible to create a valid C# highlighting using it.

The nice thing about working with grammar/AST is that you always get highlighting based on how this compiles (and doing compilation/parsing in background may actually make sense for small DSL files).

Ayende Rahien
08/21/2008 11:57 AM by
Ayende Rahien

Andrey,

I have only shown a very small part of it. It is most certainly possible to create a C# highlighting with it. In fact, there is such a language definition!

Sagar
08/22/2008 07:57 AM by
Sagar

Does anyone know how Eclipse handles syntax highlighting? There are some tips for using JFace Text and similar, but its not certainly as simple as in #develop!!

Comments have been closed on this topic.