-4

I would like to try to parse VB code to analyze some functions/subs.

What library or method do you recommend to parse VB code? My idea is to count them and get the number of lines of each of them. (I'm analyzing VB4, VB5 and VB6 files). I usually code in C# and Python.

Thank you

shinjidev
  • 383
  • 1
  • 6
  • 21
  • 1
    Please read [help]. Questions asking for libraries or other resources are off topic. – rory.ap Dec 30 '16 at 13:09
  • My condolences. Before .NET VB had a *lot* of hard-coded exceptional cases, which is why there never was an easy way to migrate from VB6 to VB.NET. Things like default properties meant that *very* often you had no idea what you called - the textbox value or its text property? That's why there were no VB6 refactoring tools too. You could find a parser generator with a VB6 syntax, but you should expect to find multiple problems – Panagiotis Kanavos Dec 30 '16 at 13:10
  • @shinjidev What kind of analysis are you trying to do? Perhaps a simple regex would be enough, or a simplistic parser using a parser generator like ANTLR, if you don't require bullet-proof parsing. Counting input parameters for example can be done with a regex – Panagiotis Kanavos Dec 30 '16 at 13:13
  • @shinjidev check for example [this question](http://stackoverflow.com/questions/5382088/parsing-vb6-code-in-net). A set of regex patterns were enough to parse input that followed a specific layout – Panagiotis Kanavos Dec 30 '16 at 13:18
  • @shinjidev if this question gets closed, post a question asking for that precise thing. Luckily, counting function/method LOCs is relatively easy because the start and end are easily identified, ie `Function whatever`, `End Function` Worst case, you can catch any text between them, with the ? operator to capture the mimimum amount of text, eg `.*?` and count the newlines – Panagiotis Kanavos Dec 30 '16 at 14:08

2 Answers2

1

Plenty of code line counting apps are available for vb6 (https://www.google.com/search?q=vb6+count+lines+of+code). I have used what is currently the first hit on that google search (http://www.freevbcode.com/ShowCode.asp?ID=1975) and it has done a credible job for our needs.

It outputs stats on a per module basis, this may be along the lines of what you're looking for? For instance:

Module Name:  mymodule.bas

           2,662  Total number of lines of code
           2,619  Miscellaneous lines of code
              21  Sub routine headings
               7  Function routine headings
               0  Property Let routine headings
               0  Property Get routine headings
               0  Property Set routine headings
               0  API Declare statements
              15  Constant variables
               0  Type Structures
               1  Auto generated lines (Not Included)
             363  Blank lines (Not Included)
           1,488  Comment lines (Not Included)

Source (vb6) in included with this app, so you could also enhance it for your own needs.

MarkL
  • 1,721
  • 1
  • 11
  • 22
  • Thank you @MarkL, I found that when I tried to search for something similar to my requirements. But, I need to count the lines of code of each function and get the names of the functions. Thank you for the link. – shinjidev Dec 30 '16 at 19:26
  • As I noted, the source is available for this app, it obviously is identifying the sub and functions, so I'd think it wouldn't be major work to have it count and document the lines between the sub/function and end sub/function. But I haven't reviewed this source, so that's a bit of conjecture on my part. Good Luck! – MarkL Dec 30 '16 at 21:02
0

I finally followed the recommendation of @PanagiotisKanavos. I used a regex like this one:

var regex = new Regex(@"(Private\s|Public\s)*(Function|Sub)+\s(\w+)(\s*)(\()", RegexOptions.IgnoreCase);

This regex is really useful because It avoids the import of libraries in VB3-6. Finally, the way to count the lines of code was following the "End Sub" or "End Function". I don't know if VB3-6 uses another way to ends functions and methods.

I hope it will be useful for some.

shinjidev
  • 383
  • 1
  • 6
  • 21