Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing VB6 syntax

Tags:

parsing

vb6

I need to inject some code into an existing VB6 application.

What I would like to do is add logging code to the top of every method across a few hundred vb6 files, logging the method name and parameters with values.

The writing of the code is easy, but where I am struggling a bit is the matching of the method or property header in VB6 syntax, as there appears to be a great number of variations and optional keywords.

Has anyone got any suggestions about how to achieve this? I have tried and failed with RegEx and have resorted to tokenising the code and looking for token patterns.

like image 270
benPearce Avatar asked Dec 02 '22 01:12

benPearce


2 Answers

It may be easier to write it as a VB6 addin that allows you to enumerate all modules/procedures and insert code to suit.
Alternatively, use MZTools which is free and can add headers to individual procedures or new ones automatically.

like image 122
Deanna Avatar answered Dec 05 '22 05:12

Deanna


You probably want something more robust then regular expressions for a project like this. I don't know of any OSS VB6 parser implementations off hand but I would recommend using a proper tool for this. This activity is sometimes called Aspect Oriented Programming or Mixins if you were to generalize the approach of injecting code at compile time.

I will take a moment to plug my own tool meta# which allows you to build a pattern matching grammar for exactly these types of scenarios but you could also use one of many others such as Lexx/Yacc, Flexx/Bison or ANTLR.

But even if you don't use mine specifically here is the general strategy I would take to solve the problem:

  1. Create a code transformation (pre-compile) build step
  2. Parse the files into an object model
  3. Insert new objects into this model representing the logging calls
  4. Generate new code files based on that object model
  5. Compile the generated code only.
  6. Generated code is a build artifact and is never edited or added to source control.

Run this transform step whenever you build.

like image 31
justin.m.chase Avatar answered Dec 05 '22 05:12

justin.m.chase