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!!



Click here for a larger image.

Download zipped project file (126k)



Comments

  • Project Guide - 2000 with new Master Wizard technology

    Posted by Legacy on 10/19/2000 12:00am

    Originally posted by: Boris Vigodny

    About color highlighting there is another way to do this.
    You can see my URL: http://pguide2000.narod.ru/index.htm
    I use logical blocks for color Analyzes and get some usefull effects respectively.

    Vigodny Boris

    Reply
  • Using RichTextBox 32

    Posted by Legacy on 03/12/2000 12:00am

    Originally posted by: Nick Palmius

    I need to use this code with a RichTextBox 32 but this dosn't seem to work, also note that if you change the "UseVersion" Property on the vbalRichEdit Control that came with the sample to "0-eRICHED32" it dosn't work either,
    anyway any help would be helpful

    Reply
  • HTML Article is at ....

    Posted by Legacy on 05/02/1999 12:00am

    Originally posted by: Chris Eastwood

    /vb/articles/1739.shtml


    Sorry it took so long to add it.


    Chris Eastwood

    CodeGuru - the website for developers
    /vb

    Reply
  • I'll be adding the HTML Highlighting Article Soon !

    Posted by Legacy on 04/06/1999 12:00am

    Originally posted by: Chris Eastwood

    For those who want it, David's send me the code for the HTML highlighting and I'll be adding a new article here in the next few days.

    Reply
  • HTML Syntax Highlighting

    Posted by Legacy on 04/04/1999 12:00am

    Originally posted by: David Hoyt

    This is in response to the previous post about HTML and ASP syntax highlighting. I have not created an ASP syntax file simply b/c I don't know much about it at all. I would need someone else to develop this. It would be MUCH appreciated if anyone would do this! If you so desire, you can provide me w/ the syntax rules, keyword list, and functions/subs for it and I'd be happy to create a syntax file for it. The code for the program language syntax highlighting does not color on the fly simply b/c I haven't had time to develop that portion yet. But there's already support for syntax highlighting like VB does it (colors a line after you leave it...just look at VB if you don't know what I'm talking about). If someone develops this first and sends it to me, I'd gladly give them credit. (c; As for HTML...I have already created HTML syntax highlighting. I created this separate b/c it follows certain rules that I could utilize to make the code highlight faster. So it has its own special routine. Perhaps I will post an article on it if enough people ask for it. Otherwise ask me and I'll send you my syntax highlighting control (comes w/ the source so you can modify it...it requires no external syntax files like this code). It colors tags, attributes, comments, and text. With what I've seen in other VB HTML syntax highlighting routines, you've always had to use quotes ("") for it to color properly. Not w/ mine...it works fine w/o quotes. If you would like this control, please contact me at the e-mail address listed. Thank you! (c;

    Sincerely,
    -David Hoyt

    Reply
  • Program Language Syntax Highlighting in VB5 and VB6

    Posted by Legacy on 04/03/1999 12:00am

    Originally posted by: David Miles

    Great Article would be nice to see some syntax files for ASP and HTML

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds