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.
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.
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:
Run this transform step whenever you build.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With