Program Language Syntax Highlighting in VB5 and VB6

Program Language Syntax Highlighting in VB5 and VB6

In order to get the code running under VB you will need the following controls (from VBAccelerator)

SSubTimer.DLL – great subclassing control/activex dll.

odcbolst.dll a great owner-drawn combo control.


Overview


This code highlights (a.k.a. colorizes) multiple languages depending on a syntax file specified by the programmer by parsing RTF instead of setting colors using .SelStart, etc so it looks good instead of ugly. It’s also a lot faster.

A list of keywords/functions/subs/operators/etc. needs to be supplied in the syntax file (*.stx). Syntax files can be used w/ a simple text editor like Notepad. I (David Hoyt) am currently working on a Syntax File Editor that will be available in the future at http://vhtml.hypermart.net/ if you need it.

All information regarding functions/subs in the modDynHighlight.bas has been included in the module in the comments. The code is commented thoroughly. To implement the highlighting, call LoadSyntaxFile() and then Highlight().

It works by modifying the RTF code for a richtextbox, so appropriate code would be something like:


Call LoadSyntaxFile("C:\SomeFolder\vbsyntax.stx")
RichTextBox1.TextRTF = Highlight(RichTextBox1.TextRTF)


Syntax File Info.

Editing a syntax file requires a little knowledge for its format…it’s divided into several sections:

Block – Sets how we know we’re at the end of a block…in VB this would be nothing, but in c/c++ or java, it’d be a semi-colon (;)

Comments – This is a unique section…you can add different comment styles by using SingleLine= and MultiLineStart= and MutliLineEnd.

For C/C++ use this syntax:


  SingleLine=//
  MultiLineStart=/*
  MultiLineStart=*/

Func Subs – This is a list of functions/subs for each programming language. This is not yet implemented in full so don’t concern yourself too much w/ this.

Keywords – This holds a list of keywords for the programming language.

Operators – Sets operators such as =, !=, <>, >, <, +, -, *, etc.

String Delim – Sets how the language specifies a string. In VB it’s a double quote (")

These categories must be enclosed by []s. Here’s a small example syntax file:


[Comments]
SingleLine='
SingleLine=REM

[Operators]
=
==
<
>
<>
+

[string Delim]
"

[Block]
 ;

[Func Subs]
mid
InStr

[Keywords]
Integer
Long
as
string
public
private

There ya go!! Just FYI — you can include comments by specifying the first character of a line as a semi-colon. For example:



;The keywords section...
[Keywords]
Hello
;This is a comment!!
NotAComment
ActualKeyword ;uh-oh this will give you an error!! _
              Can't have a comment on the side!

Blank spaces are also ignored. So maybe you’re wondering how you would include a semi-colon if it was needed for a block statement perhaps? That’s simple!! Just put a space in front of it and it will be read like normal in the code. Except in certain cases, each item added is stripped of trailing and beginning spaces (uses Trim() function). Once again, I am working on a syntax file editor that I’ll try to finish up ASAP. Look for it on the URL I specified above. Once that is done, you won’t have to worry about following all that funky syntax for my syntax files!! (c; Well, hope you like the code!!

Download zipped project file (126k)

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read