Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing VB6 code in .NET

I have a WPF project written in C#, and in order to get some information about an external dependency, I need to parse a VB6 script. The script's location changes and its content changes some, but the main code I'm interested in will be of the format:

Select Case Fields("blah").Value
    Case "Some value"
        Fields("other blah").List = Lists("a list name")
    ...
End Select

I need to extract from this that when field 'blah' is set to 'some value', the list for field 'other blah' changes to list 'a list name'. I tried Googling around for a VB6 parser written as a .NET library but haven't found anything yet. At the risk of getting an answer like this one, should I just use regular expressions to find the code like this in the VB6 script, and extract the data I need? The code is found in a subroutine such that I can't pass in 'blah', 'some value' and get back 'other blah', 'a list name'. I have no control over the contents of this VB6 script.

like image 674
Sarah Vessels Avatar asked Oct 12 '22 11:10

Sarah Vessels


1 Answers

You can parse it in a few steps. Please note the regex misses strings and comments, so use with care.

First, we'll use a helper class for the Fields("Target").List = Lists("Value") lines:

class ListData
{
    public string Target { get; set; }
    public string Value { get; set; }
}

Out patterns:

string patternSelectCase = @"
Select\s+Case\s+Fields\(""(?<CaseField>[\w\s]+)""\)\.Value
(?<Cases>.*?)
End\s+Select
";

string patternCase = @"
Case\s+""(?<Case>[\w\s]+)""\s+
(?:Fields\(""(?<Target>[\w\s]+)""\)\.List\s*=\s*Lists\(""(?<Value>[\w\s]+)""\)\s+)*
";

Next, we can try to parse the text in two passes (the code is a little ugly, by the way, but fairly basic):

MatchCollection matches = Regex.Matches(vb, patternSelectCase,
        RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | 
        RegexOptions.Singleline);

Console.WriteLine(matches.Count);

var data = new Dictionary<String, Dictionary<String, List<ListData>>>();
foreach (Match match in matches)
{
    var caseData = new Dictionary<String, List<ListData>>();
    string caseField = match.Groups["CaseField"].Value;
    string cases = match.Groups["Cases"].Value;

    MatchCollection casesMatches = Regex.Matches(cases, patternCase,
             RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | 
             RegexOptions.Singleline);
    foreach (Match caseMatch in casesMatches)
    {
        string caseTitle = caseMatch.Groups["Case"].Value;
        var targetCaptures = caseMatch.Groups["Target"].Captures.Cast<Capture>();
        var valueCaptures = caseMatch.Groups["Value"].Captures.Cast<Capture>();
        caseData.Add(caseTitle, targetCaptures.Zip(valueCaptures, (t, v) =>
            new ListData
            {
                Target = t.Value,
                Value = v.Value
            }).ToList());
    }

    data.Add(caseField, caseData);
}

Now you have a dictionary with all data. For example:

string s = data["foo"]["Some value2"].First().Value;

Here's a working example: https://gist.github.com/880148

like image 123
Kobi Avatar answered Oct 14 '22 10:10

Kobi