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

Print | posted on Wednesday, August 20, 2008 3:17 PM

Feedback


Gravatar

# re: Custom Syntax Highlighting 8/20/2008 5:40 PM Tobin Harris

Good work! Looks great!


Gravatar

# re: Custom Syntax Highlighting 8/20/2008 6:01 PM josh

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


Gravatar

# re: Custom Syntax Highlighting 8/20/2008 10:46 PM 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.


Gravatar

# re: Custom Syntax Highlighting 8/20/2008 11:08 PM 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


Gravatar

# re: Custom Syntax Highlighting 8/21/2008 2:54 PM 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).


Gravatar

# re: Custom Syntax Highlighting 8/21/2008 2:57 PM 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!


Gravatar

# re: Custom Syntax Highlighting 8/22/2008 10:57 AM 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.