VB6 parsing syntax

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

What I would like to do is add a registration code to the top of each method in several hundred vb6 files, registering the method name and parameters with values.

Writing code is very simple, but I'm struggling a bit to map a method or property header in VB6 syntax, as there are a lot of variations and optional keywords.

Does anyone have any suggestions on how to achieve this? I tried and failed with RegEx and resorted to code tokenization and search for token templates.

+4
source share
3 answers

It may be easier to write it as a VB6 add-on, which allows you to list all modules / procedures and insert code as required.
In addition, use MZTools , which is free and can automatically add headers to individual procedures or new ones.

+17
source

You probably need something more robust than regular expressions for such a project. I do not know any implementation of the OSS VB6 parser, but I would recommend using a suitable tool for this. This activity is sometimes called Aspect Oriented Programming or Mixins, if you want to generalize the approach of entering code at compile time.

I'll take a moment to plug in my own meta # tool, which allows you to build a pattern matching grammar for just these types of scripts, but you can also use one of many others such as Lexx / Yacc, Flexx / Bison or ANTLR.

But even if you are not using mine specifically, here is a general strategy that I would take to solve the problem:

  • Creating a code building phase (precompilation)
  • Parse files in the object model
  • Insert new objects into this model representing the log calls
  • Creating new code files based on this object model
  • Compile only the generated code.
  • The generated code is an assembly artifact and is never edited or added to the original control.

Run this transformation step whenever you build.

+1
source

Our DMS Software Reengineering Toolkit with its Visual Basic Front End can be used for this.

DMS analyzes the source code using the front end for abstract syntax trees, and then allows you to use arbitrary analysis / transformation for these trees. Many transformation changes can be made by converting the source files to their original state , in which the code is rewritten using "if you see this syntax, replace it with this syntax", using grammar as a way to define abstract placeholders. This makes it easy to write transformations to code using familiar syntax. This generalizes the OP method, which attempts to match token sequences.

The OP problem can be posed as an aspect like rewriting a form:

default domain VisualBasic~VB6; rule function_insert_log_call(a: attributes, t: type, i: IDENTIFIER, p: parameters, s:statements) = function -> function = " \a FUNCTION \i ( \p ) AS \t \s END FUNCTION" -> "\a FUNCTION \i ( \p ) AS \t my_log(\tostring\(\i\)) \s END FUNCTION"; rule subroutine_insert_log_call(a: attributes, i: IDENTIFIER, p: parameters, s:statements) = subroutine -> subroutine = " \a SUB \i ( \p ) \s END SUB" -> " \a SUB \i ( \p ) my_log(\tostring\(\i\)) \s END SUB"; 

These rewrites are of the form

  rule *rulename* ( *patternvars* ) *nonterminal* -> *nonterminal* = " *syntaxpattern* " -> " *syntaxpattern* "; 

The given specific rules will recognize headers and function objects regardless of the contents / spaces / comments, since they actually correspond to AST. "..." is the meta-lead, and the external is the syntax of the DMS rule and internally is the syntax of VB6. \ N inside the "..." is the (AST) parameter that must correspond to the grammatical nonterminal N declared in the header as rule. .. n: N .... tostring is a regular meta function (called using meta parens ()) that converts an argument from a node tree to a node tree for a string string.

OP may require more rules than to handle other cases; perhaps he wants to keep a GOSUB call log and / or to capture functional parameters in a log call.

Another answer involves getting a parser generator and, well, defining VB6 to enable parsing. It is important to understand that getting the VB6 syntax right is really difficult; langauge is poorly documented and has some really weirder rules regarding spaces, statements-inside lines and statements across line boundaries. If you do not, you simply cannot parse hundreds of files. We had to define our own grammar (like DMS has for many other languages ).

You can learn more about software / code logging with software transformations here.

+1
source

Source: https://habr.com/ru/post/1383773/


All Articles