Monday, February 28, 2011

How did I implement Syntax Highlighting?

If an IDE does not have the feature like Syntax Highlighting in its code editor, then in my opinion it is worthless using it. Syntax highlighting is a cool feature. It makes the code look cleaner, understandable, and easier to debug. Thats why even text books have now started printing highlighted code instead to the old black and white tradition. Virtual 8085 supports very basic syntax highlighting of 8085 assembly language code.

In this first version of V8085, only the mnemonics and comments are highlighted, not the operands. The most important ingredient in syntax highlighting is a TextBox which offers colouring of text (A RichTextBox). What I did is to monitor the current line the user is editing. Then find a match for a mnemonic in that line. If a mnemonic is found, then select that mnemonic and set its colour. But before selecting that part of line (mnemonic) we have to first save the previous selection made by the user (otherwise the previously selected text will be lost), then do our task, and restore the previous selection. Seems easy, right. It is!

For matching a particular string inside a string and get its position, we made the use of Regular Expression. The RegEx class in .NET provides functions for regular expressions. We have to match a Mnemonic inside the line which the user is currently editing. I saved all the mnemonics in a static string and used it as a match pattern for RegEx. For comments, I used the match string [^; ! ;] which means that any string that starts with a semicolon (;). When you match the pattern string with a particular string, then it gives you the index and length our your match. Thus using these two parameters you can easily select the text in Rich text box and colour it.

(Click to view code snippet)
The source code editor of V8085 showing syntax highlighting of code and intelligent help

Thus that was about syntax highlighting in Virtual 8085. Have a nice day!

1 comment:

  1. This comment has been removed by the author.

    ReplyDelete