Parsing VB6 code in .NET.

I have a WPF project written in C #, and in order to get some information about the external dependency, I need to parse a VB6 script. The location of the script changes, and its contents change some, but the main code that interests me will have 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 the 'blah' field is set to 'some value', the list for the 'other blah' field changes to the list 'list name'. I tried Googling for a VB6 parser written as a .NET library, but haven’t found anything yet. At the risk of getting an answer like this , should I just use regular expressions to search for code like this in a VB6 script and extract the data I need? The code is found in the subroutine, so I can not pass "blah", "some value" and return "other blah", "list name". I do not control the contents of this VB6 script.

+5
source share
1 answer

. , , .

Fields("Target").List = Lists("Value"):

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

:

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+)*
";

( , , ):

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);
}

. :

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

: https://gist.github.com/880148

+4

All Articles