示例#1
0
        private void btnTranslateWithLogging_Click(object sender, EventArgs e)
        {
            var outFolder       = Folder + "-out";
            var extraModuleName = "VB6CompilerExtra.bas";

            var extraModule = @"
Public Sub Log(ByVal Msg As String)
    Static FileNum As Single
    If FileNum = 0 Then
        FileNum = FreeFile
        Open App.Path & ""\ProgramLog.txt"" For Output As FileNum
    End If
    Print #FileNum, Msg
End Sub

Public Sub LogEnter(ParamArray Text() As Variant)
    Dim intLoopIndex As Integer, Serialized As String
    Serialized = """"
    For intLoopIndex = 0 To UBound(Text)
      Serialized = Serialized & "", "" & Text(intLoopIndex)
    Next intLoopIndex
    Log ""ENTER "" & Serialized
End Sub

Public Sub LogLeave(ParamArray Text() As Variant)
    Dim intLoopIndex As Integer, Serialized As String
    Serialized = """"
    For intLoopIndex = 0 To UBound(Text)
      Serialized = Serialized & "", "" & Text(intLoopIndex)
    Next intLoopIndex
    Log ""LEAVE "" & Serialized
End Sub

Public Function SerializeInteger(arg as Integer) as String
    SerializeInteger = CStr(arg)
End Function

Public Function SerializeDouble(arg as Double) as String
    SerializeDouble = CStr(arg)
End Function

Public Function SerializeSingle(arg as Single) as String
    SerializeSingle = CStr(arg)
End Function

Public Function SerializeBoolean(arg as Boolean) as String
    If arg Then
        SerializeBoolean = ""True""
    Else
        SerializeBoolean = ""False""
    End If
End Function

Public Function SerializeForm(arg as Form) as String
    SerializeForm = arg.Name
End Function

Public Function SerializeString(arg As String) As String
    SerializeString = arg
End Function

Public Function SerializeDate(arg As Date) As String
    SerializeDate = CStr(arg)
End Function
";

            foreach (var fileName in VB6Compiler.GetFiles(Folder, true, true))
            {
                var    bname       = Path.GetFileName(fileName);
                string outFileName = Path.Combine(outFolder, bname);
                if (
                    (fileName.EndsWith(".frx", System.StringComparison.InvariantCulture)) ||
                    (fileName.EndsWith(".vbw", System.StringComparison.InvariantCulture)))
                {
                    System.IO.File.Copy(fileName, outFileName, true);
                    continue;
                }
                if (fileName.EndsWith(".vbp", System.StringComparison.InvariantCulture))
                {
                    var text = System.IO.File.ReadAllText(fileName, Encoding.GetEncoding(1252));
                    text += "Module=mod_VB6CompilerExtra; " + extraModuleName + "\r\n";
                    System.IO.File.WriteAllText(outFileName, text, Encoding.GetEncoding(1252));
                    continue;
                }

                var compileResult = VB6Compiler.Compile(fileName, null, false);
                var tree          = new VB6NodeTree(compileResult);
                tree.AddExtraModule = (a, b) =>
                {
                    extraModule += b;
                };
                var se = VB6NodeTranslatorLoader.Translate(tree);
                var sl = se.ToList();
                sl.AddRange(GetComments(compileResult));
                sl.Sort((a, b) => a.index.CompareTo(b.index));
                //var s = String.Join("", sl.Select(x => x.index.ToString() + ":" + x.token));
                var s = String.Join("", sl.Select(x => x.token));
                System.IO.Directory.CreateDirectory(outFolder);
                s = Regex.Replace(s, "([^\r])\n", "$1\r\n");
                System.IO.File.WriteAllText(outFileName, s, Encoding.GetEncoding(1252));
            }

            System.IO.File.WriteAllText(Path.Combine(outFolder, extraModuleName), extraModule, Encoding.GetEncoding(1252));

            string            message = "Wrote new files to: " + outFolder;
            string            caption = "Compilation Successful!";
            MessageBoxButtons buttons = MessageBoxButtons.OK;
            var result = MessageBox.Show(message, caption, buttons);
        }
示例#2
0
 public VB6SubTree(VB6NodeTree parent, ParseTree root)
 {
     this.parent = parent ?? throw new ArgumentNullException(nameof(parent));
     this.root   = root;
     depth       = parent.GetDepth(root);
 }
        public static IEnumerable <OutToken> Translate(VB6NodeTree nodeTree)
        {
            if (nodeTree == null)
            {
                throw new ArgumentNullException(nameof(nodeTree));
            }

            var dict = new Dictionary <ContextNodeType, VB6NodeTranslator>();

            // dict[ContextNodeType.AmbiguousIdentifierContext] = new VB6NodeTranslatorLogging.AmbiguousIdentifierContext(nodeTree, dict);
            dict[ContextNodeType.AmbiguousIdentifierContext] = new VB6NodeTranslatorLogging.AmbiguousIdentifierContext(nodeTree, dict);
            dict[ContextNodeType.AmbiguousKeywordContext]    = new VB6NodeTranslatorLogging.AmbiguousKeywordContext(nodeTree, dict);
            dict[ContextNodeType.AppActivateStmtContext]     = new VB6NodeTranslatorLogging.AppActivateStmtContext(nodeTree, dict);
            dict[ContextNodeType.ArgCallContext]             = new VB6NodeTranslatorLogging.ArgCallContext(nodeTree, dict);
            dict[ContextNodeType.ArgContext]                           = new VB6NodeTranslatorLogging.ArgContext(nodeTree, dict);
            dict[ContextNodeType.ArgDefaultValueContext]               = new VB6NodeTranslatorLogging.ArgDefaultValueContext(nodeTree, dict);
            dict[ContextNodeType.ArgListContext]                       = new VB6NodeTranslatorLogging.ArgListContext(nodeTree, dict);
            dict[ContextNodeType.ArgsCallContext]                      = new VB6NodeTranslatorLogging.ArgsCallContext(nodeTree, dict);
            dict[ContextNodeType.AsTypeClauseContext]                  = new VB6NodeTranslatorLogging.AsTypeClauseContext(nodeTree, dict);
            dict[ContextNodeType.AttributeStmtContext]                 = new VB6NodeTranslatorLogging.AttributeStmtContext(nodeTree, dict);
            dict[ContextNodeType.BaseTypeContext]                      = new VB6NodeTranslatorLogging.BaseTypeContext(nodeTree, dict);
            dict[ContextNodeType.BeepStmtContext]                      = new VB6NodeTranslatorLogging.BeepStmtContext(nodeTree, dict);
            dict[ContextNodeType.BlockContext]                         = new VB6NodeTranslatorLogging.BlockContext(nodeTree, dict);
            dict[ContextNodeType.BlockIfThenElseContext]               = new VB6NodeTranslatorLogging.BlockIfThenElseContext(nodeTree, dict);
            dict[ContextNodeType.BlockStmtContext]                     = new VB6NodeTranslatorLogging.BlockStmtContext(nodeTree, dict);
            dict[ContextNodeType.CallContext]                          = new VB6NodeTranslatorLogging.CallContext(nodeTree, dict);
            dict[ContextNodeType.CaseCondElseContext]                  = new VB6NodeTranslatorLogging.CaseCondElseContext(nodeTree, dict);
            dict[ContextNodeType.CaseCondExprContext]                  = new VB6NodeTranslatorLogging.CaseCondExprContext(nodeTree, dict);
            dict[ContextNodeType.CaseCondExprIsContext]                = new VB6NodeTranslatorLogging.CaseCondExprIsContext(nodeTree, dict);
            dict[ContextNodeType.CaseCondExprToContext]                = new VB6NodeTranslatorLogging.CaseCondExprToContext(nodeTree, dict);
            dict[ContextNodeType.CaseCondExprValueContext]             = new VB6NodeTranslatorLogging.CaseCondExprValueContext(nodeTree, dict);
            dict[ContextNodeType.CertainIdentifierContext]             = new VB6NodeTranslatorLogging.CertainIdentifierContext(nodeTree, dict);
            dict[ContextNodeType.ChDirStmtContext]                     = new VB6NodeTranslatorLogging.ChDirStmtContext(nodeTree, dict);
            dict[ContextNodeType.ChDriveStmtContext]                   = new VB6NodeTranslatorLogging.ChDriveStmtContext(nodeTree, dict);
            dict[ContextNodeType.CloseStmtContext]                     = new VB6NodeTranslatorLogging.CloseStmtContext(nodeTree, dict);
            dict[ContextNodeType.ComparisonOperatorContext]            = new VB6NodeTranslatorLogging.ComparisonOperatorContext(nodeTree, dict);
            dict[ContextNodeType.ComplexTypeContext]                   = new VB6NodeTranslatorLogging.ComplexTypeContext(nodeTree, dict);
            dict[ContextNodeType.CondExprContext]                      = new VB6NodeTranslatorLogging.CondExprContext(nodeTree, dict);
            dict[ContextNodeType.ConstStmtContext]                     = new VB6NodeTranslatorLogging.ConstStmtContext(nodeTree, dict);
            dict[ContextNodeType.ConstSubStmtContext]                  = new VB6NodeTranslatorLogging.ConstSubStmtContext(nodeTree, dict);
            dict[ContextNodeType.ControlPropertiesContext]             = new VB6NodeTranslatorLogging.ControlPropertiesContext(nodeTree, dict);
            dict[ContextNodeType.Cp_ControlIdentifierContext]          = new VB6NodeTranslatorLogging.Cp_ControlIdentifierContext(nodeTree, dict);
            dict[ContextNodeType.Cp_ControlTypeContext]                = new VB6NodeTranslatorLogging.Cp_ControlTypeContext(nodeTree, dict);
            dict[ContextNodeType.Cp_NestedPropertyContext]             = new VB6NodeTranslatorLogging.Cp_NestedPropertyContext(nodeTree, dict);
            dict[ContextNodeType.Cp_PropertiesContext]                 = new VB6NodeTranslatorLogging.Cp_PropertiesContext(nodeTree, dict);
            dict[ContextNodeType.Cp_PropertyValueContext]              = new VB6NodeTranslatorLogging.Cp_PropertyValueContext(nodeTree, dict);
            dict[ContextNodeType.Cp_SinglePropertyContext]             = new VB6NodeTranslatorLogging.Cp_SinglePropertyContext(nodeTree, dict);
            dict[ContextNodeType.DateStmtContext]                      = new VB6NodeTranslatorLogging.DateStmtContext(nodeTree, dict);
            dict[ContextNodeType.DeclareStmtContext]                   = new VB6NodeTranslatorLogging.DeclareStmtContext(nodeTree, dict);
            dict[ContextNodeType.DeftypeStmtContext]                   = new VB6NodeTranslatorLogging.DeftypeStmtContext(nodeTree, dict);
            dict[ContextNodeType.DeleteSettingStmtContext]             = new VB6NodeTranslatorLogging.DeleteSettingStmtContext(nodeTree, dict);
            dict[ContextNodeType.DictionaryCallStmtContext]            = new VB6NodeTranslatorLogging.DictionaryCallStmtContext(nodeTree, dict);
            dict[ContextNodeType.DoLoopStmtContext]                    = new VB6NodeTranslatorLogging.DoLoopStmtContext(nodeTree, dict);
            dict[ContextNodeType.ECS_MemberProcedureCallContext]       = new VB6NodeTranslatorLogging.ECS_MemberProcedureCallContext(nodeTree, dict);
            dict[ContextNodeType.ECS_ProcedureCallContext]             = new VB6NodeTranslatorLogging.ECS_ProcedureCallContext(nodeTree, dict);
            dict[ContextNodeType.EndStmtContext]                       = new VB6NodeTranslatorLogging.EndStmtContext(nodeTree, dict);
            dict[ContextNodeType.EnumerationStmt_ConstantContext]      = new VB6NodeTranslatorLogging.EnumerationStmt_ConstantContext(nodeTree, dict);
            dict[ContextNodeType.EnumerationStmtContext]               = new VB6NodeTranslatorLogging.EnumerationStmtContext(nodeTree, dict);
            dict[ContextNodeType.EraseStmtContext]                     = new VB6NodeTranslatorLogging.EraseStmtContext(nodeTree, dict);
            dict[ContextNodeType.EventStmtContext]                     = new VB6NodeTranslatorLogging.EventStmtContext(nodeTree, dict);
            dict[ContextNodeType.ExitStmtContext]                      = new VB6NodeTranslatorLogging.ExitStmtContext(nodeTree, dict);
            dict[ContextNodeType.ExplicitCallStmtContext]              = new VB6NodeTranslatorLogging.ExplicitCallStmtContext(nodeTree, dict);
            dict[ContextNodeType.FieldLengthContext]                   = new VB6NodeTranslatorLogging.FieldLengthContext(nodeTree, dict);
            dict[ContextNodeType.FilecopyStmtContext]                  = new VB6NodeTranslatorLogging.FilecopyStmtContext(nodeTree, dict);
            dict[ContextNodeType.ForEachStmtContext]                   = new VB6NodeTranslatorLogging.ForEachStmtContext(nodeTree, dict);
            dict[ContextNodeType.ForNextStmtContext]                   = new VB6NodeTranslatorLogging.ForNextStmtContext(nodeTree, dict);
            dict[ContextNodeType.FunctionStmtContext]                  = new VB6NodeTranslatorLogging.FunctionStmtContext(nodeTree, dict);
            dict[ContextNodeType.GetStmtContext]                       = new VB6NodeTranslatorLogging.GetStmtContext(nodeTree, dict);
            dict[ContextNodeType.GoToStmtContext]                      = new VB6NodeTranslatorLogging.GoToStmtContext(nodeTree, dict);
            dict[ContextNodeType.ICS_B_MemberProcedureCallContext]     = new VB6NodeTranslatorLogging.ICS_B_MemberProcedureCallContext(nodeTree, dict);
            dict[ContextNodeType.ICS_B_ProcedureCallContext]           = new VB6NodeTranslatorLogging.ICS_B_ProcedureCallContext(nodeTree, dict);
            dict[ContextNodeType.ICS_S_DictionaryCallContext]          = new VB6NodeTranslatorLogging.ICS_S_DictionaryCallContext(nodeTree, dict);
            dict[ContextNodeType.ICS_S_MemberCallContext]              = new VB6NodeTranslatorLogging.ICS_S_MemberCallContext(nodeTree, dict);
            dict[ContextNodeType.ICS_S_MembersCallContext]             = new VB6NodeTranslatorLogging.ICS_S_MembersCallContext(nodeTree, dict);
            dict[ContextNodeType.ICS_S_ProcedureOrArrayCallContext]    = new VB6NodeTranslatorLogging.ICS_S_ProcedureOrArrayCallContext(nodeTree, dict);
            dict[ContextNodeType.ICS_S_VariableOrProcedureCallContext] = new VB6NodeTranslatorLogging.ICS_S_VariableOrProcedureCallContext(nodeTree, dict);
            dict[ContextNodeType.IfBlockStmtContext]                   = new VB6NodeTranslatorLogging.IfBlockStmtContext(nodeTree, dict);
            dict[ContextNodeType.IfConditionStmtContext]               = new VB6NodeTranslatorLogging.IfConditionStmtContext(nodeTree, dict);
            dict[ContextNodeType.IfElseBlockStmtContext]               = new VB6NodeTranslatorLogging.IfElseBlockStmtContext(nodeTree, dict);
            dict[ContextNodeType.IfElseIfBlockStmtContext]             = new VB6NodeTranslatorLogging.IfElseIfBlockStmtContext(nodeTree, dict);
            dict[ContextNodeType.ImplicitCallStmt_InBlockContext]      = new VB6NodeTranslatorLogging.ImplicitCallStmt_InBlockContext(nodeTree, dict);
            dict[ContextNodeType.ImplicitCallStmt_InStmtContext]       = new VB6NodeTranslatorLogging.ImplicitCallStmt_InStmtContext(nodeTree, dict);
            dict[ContextNodeType.InlineIfThenElseContext]              = new VB6NodeTranslatorLogging.InlineIfThenElseContext(nodeTree, dict);
            dict[ContextNodeType.KillStmtContext]                      = new VB6NodeTranslatorLogging.KillStmtContext(nodeTree, dict);
            dict[ContextNodeType.LetStmtContext]                       = new VB6NodeTranslatorLogging.LetStmtContext(nodeTree, dict);
            dict[ContextNodeType.LetterrangeContext]                   = new VB6NodeTranslatorLogging.LetterrangeContext(nodeTree, dict);
            dict[ContextNodeType.LineInputStmtContext]                 = new VB6NodeTranslatorLogging.LineInputStmtContext(nodeTree, dict);
            dict[ContextNodeType.LineLabelContext]                     = new VB6NodeTranslatorLogging.LineLabelContext(nodeTree, dict);
            dict[ContextNodeType.LiteralContext]                       = new VB6NodeTranslatorLogging.LiteralContext(nodeTree, dict);
            dict[ContextNodeType.LsetStmtContext]                      = new VB6NodeTranslatorLogging.LsetStmtContext(nodeTree, dict);
            dict[ContextNodeType.MemberCallContext]                    = new VB6NodeTranslatorLogging.MemberCallContext(nodeTree, dict);
            dict[ContextNodeType.MkdirStmtContext]                     = new VB6NodeTranslatorLogging.MkdirStmtContext(nodeTree, dict);
            dict[ContextNodeType.ModuleAttributesContext]              = new VB6NodeTranslatorLogging.ModuleAttributesContext(nodeTree, dict);
            dict[ContextNodeType.ModuleBlockContext]                   = new VB6NodeTranslatorLogging.ModuleBlockContext(nodeTree, dict);
            dict[ContextNodeType.ModuleBodyContext]                    = new VB6NodeTranslatorLogging.ModuleBodyContext(nodeTree, dict);
            dict[ContextNodeType.ModuleBodyElementContext]             = new VB6NodeTranslatorLogging.ModuleBodyElementContext(nodeTree, dict);
            dict[ContextNodeType.ModuleConfigElementContext]           = new VB6NodeTranslatorLogging.ModuleConfigElementContext(nodeTree, dict);
            dict[ContextNodeType.ModuleContext]                        = new VB6NodeTranslatorLogging.ModuleContext(nodeTree, dict);
            dict[ContextNodeType.ModuleHeaderContext]                  = new VB6NodeTranslatorLogging.ModuleHeaderContext(nodeTree, dict);
            dict[ContextNodeType.ModuleOptionsContext]                 = new VB6NodeTranslatorLogging.ModuleOptionsContext(nodeTree, dict);
            dict[ContextNodeType.ModuleReferenceComponentContext]      = new VB6NodeTranslatorLogging.ModuleReferenceComponentContext(nodeTree, dict);
            dict[ContextNodeType.ModuleReferenceContext]               = new VB6NodeTranslatorLogging.ModuleReferenceContext(nodeTree, dict);
            dict[ContextNodeType.ModuleReferencesContext]              = new VB6NodeTranslatorLogging.ModuleReferencesContext(nodeTree, dict);
            dict[ContextNodeType.ModuleReferenceValueContext]          = new VB6NodeTranslatorLogging.ModuleReferenceValueContext(nodeTree, dict);
            dict[ContextNodeType.NameStmtContext]                      = new VB6NodeTranslatorLogging.NameStmtContext(nodeTree, dict);
            dict[ContextNodeType.OnErrorStmtContext]                   = new VB6NodeTranslatorLogging.OnErrorStmtContext(nodeTree, dict);
            dict[ContextNodeType.OpenStmtContext]                      = new VB6NodeTranslatorLogging.OpenStmtContext(nodeTree, dict);
            dict[ContextNodeType.OptionBaseStmtContext]                = new VB6NodeTranslatorLogging.OptionBaseStmtContext(nodeTree, dict);
            dict[ContextNodeType.OptionCompareStmtContext]             = new VB6NodeTranslatorLogging.OptionCompareStmtContext(nodeTree, dict);
            dict[ContextNodeType.OptionExplicitStmtContext]            = new VB6NodeTranslatorLogging.OptionExplicitStmtContext(nodeTree, dict);
            dict[ContextNodeType.OptionPrivateModuleStmtContext]       = new VB6NodeTranslatorLogging.OptionPrivateModuleStmtContext(nodeTree, dict);
            dict[ContextNodeType.OutputList_ExpressionContext]         = new VB6NodeTranslatorLogging.OutputList_ExpressionContext(nodeTree, dict);
            dict[ContextNodeType.OutputListContext]                    = new VB6NodeTranslatorLogging.OutputListContext(nodeTree, dict);
            dict[ContextNodeType.ParserRuleContext]                    = new VB6NodeTranslatorLogging.ParserRuleContext(nodeTree, dict);
            dict[ContextNodeType.PrintStmtContext]                     = new VB6NodeTranslatorLogging.PrintStmtContext(nodeTree, dict);
            dict[ContextNodeType.PropertyGetStmtContext]               = new VB6NodeTranslatorLogging.PropertyGetStmtContext(nodeTree, dict);
            dict[ContextNodeType.PropertyLetStmtContext]               = new VB6NodeTranslatorLogging.PropertyLetStmtContext(nodeTree, dict);
            dict[ContextNodeType.PropertySetStmtContext]               = new VB6NodeTranslatorLogging.PropertySetStmtContext(nodeTree, dict);
            dict[ContextNodeType.PublicPrivateGlobalVisibilityContext] = new VB6NodeTranslatorLogging.PublicPrivateGlobalVisibilityContext(nodeTree, dict);
            dict[ContextNodeType.PublicPrivateVisibilityContext]       = new VB6NodeTranslatorLogging.PublicPrivateVisibilityContext(nodeTree, dict);
            dict[ContextNodeType.PutStmtContext]                       = new VB6NodeTranslatorLogging.PutStmtContext(nodeTree, dict);
            dict[ContextNodeType.RedimSubStmtContext]                  = new VB6NodeTranslatorLogging.RedimSubStmtContext(nodeTree, dict);
            dict[ContextNodeType.ResumeStmtContext]                    = new VB6NodeTranslatorLogging.ResumeStmtContext(nodeTree, dict);
            dict[ContextNodeType.SaveSettingStmtContext]               = new VB6NodeTranslatorLogging.SaveSettingStmtContext(nodeTree, dict);
            dict[ContextNodeType.SC_CaseContext]                       = new VB6NodeTranslatorLogging.SC_CaseContext(nodeTree, dict);
            dict[ContextNodeType.SC_CondContext]                       = new VB6NodeTranslatorLogging.SC_CondContext(nodeTree, dict);
            dict[ContextNodeType.SC_CondExprContext]                   = new VB6NodeTranslatorLogging.SC_CondExprContext(nodeTree, dict);
            dict[ContextNodeType.SelectCaseStmtContext]                = new VB6NodeTranslatorLogging.SelectCaseStmtContext(nodeTree, dict);
            dict[ContextNodeType.SetStmtContext]                       = new VB6NodeTranslatorLogging.SetStmtContext(nodeTree, dict);
            dict[ContextNodeType.StartRuleContext]                     = new VB6NodeTranslatorLogging.StartRuleContext(nodeTree, dict);
            dict[ContextNodeType.SubscriptContext]                     = new VB6NodeTranslatorLogging.SubscriptContext(nodeTree, dict);
            dict[ContextNodeType.SubscriptsContext]                    = new VB6NodeTranslatorLogging.SubscriptsContext(nodeTree, dict);
            dict[ContextNodeType.SubStmtContext]                       = new VB6NodeTranslatorLogging.SubStmtContext(nodeTree, dict);
            dict[ContextNodeType.TypeContext]                          = new VB6NodeTranslatorLogging.TypeContext(nodeTree, dict);
            dict[ContextNodeType.TypeHintContext]                      = new VB6NodeTranslatorLogging.TypeHintContext(nodeTree, dict);
            dict[ContextNodeType.TypeOfStmtContext]                    = new VB6NodeTranslatorLogging.TypeOfStmtContext(nodeTree, dict);
            dict[ContextNodeType.TypeStmt_ElementContext]              = new VB6NodeTranslatorLogging.TypeStmt_ElementContext(nodeTree, dict);
            dict[ContextNodeType.TypeStmtContext]                      = new VB6NodeTranslatorLogging.TypeStmtContext(nodeTree, dict);
            dict[ContextNodeType.UnloadStmtContext]                    = new VB6NodeTranslatorLogging.UnloadStmtContext(nodeTree, dict);
            dict[ContextNodeType.ValueStmtContext]                     = new VB6NodeTranslatorLogging.ValueStmtContext(nodeTree, dict);
            dict[ContextNodeType.VariableListStmtContext]              = new VB6NodeTranslatorLogging.VariableListStmtContext(nodeTree, dict);
            dict[ContextNodeType.VariableStmtContext]                  = new VB6NodeTranslatorLogging.VariableStmtContext(nodeTree, dict);
            dict[ContextNodeType.VariableSubStmtContext]               = new VB6NodeTranslatorLogging.VariableSubStmtContext(nodeTree, dict);
            dict[ContextNodeType.VisibilityContext]                    = new VB6NodeTranslatorLogging.VisibilityContext(nodeTree, dict);
            dict[ContextNodeType.VsAddContext]                         = new VB6NodeTranslatorLogging.VsAddContext(nodeTree, dict);
            dict[ContextNodeType.VsAddressOfContext]                   = new VB6NodeTranslatorLogging.VsAddressOfContext(nodeTree, dict);
            dict[ContextNodeType.VsAmpContext]                         = new VB6NodeTranslatorLogging.VsAmpContext(nodeTree, dict);
            dict[ContextNodeType.VsAndContext]                         = new VB6NodeTranslatorLogging.VsAndContext(nodeTree, dict);
            dict[ContextNodeType.VsAssignContext]                      = new VB6NodeTranslatorLogging.VsAssignContext(nodeTree, dict);
            dict[ContextNodeType.VsDivContext]                         = new VB6NodeTranslatorLogging.VsDivContext(nodeTree, dict);
            dict[ContextNodeType.VsEqContext]                          = new VB6NodeTranslatorLogging.VsEqContext(nodeTree, dict);
            dict[ContextNodeType.VsEqvContext]                         = new VB6NodeTranslatorLogging.VsEqvContext(nodeTree, dict);
            dict[ContextNodeType.VsGeqContext]                         = new VB6NodeTranslatorLogging.VsGeqContext(nodeTree, dict);
            dict[ContextNodeType.VsGtContext]                          = new VB6NodeTranslatorLogging.VsGtContext(nodeTree, dict);
            dict[ContextNodeType.VsICSContext]                         = new VB6NodeTranslatorLogging.VsICSContext(nodeTree, dict);
            dict[ContextNodeType.VsImpContext]                         = new VB6NodeTranslatorLogging.VsImpContext(nodeTree, dict);
            dict[ContextNodeType.VsIsContext]                          = new VB6NodeTranslatorLogging.VsIsContext(nodeTree, dict);
            dict[ContextNodeType.VsLeqContext]                         = new VB6NodeTranslatorLogging.VsLeqContext(nodeTree, dict);
            dict[ContextNodeType.VsLikeContext]                        = new VB6NodeTranslatorLogging.VsLikeContext(nodeTree, dict);
            dict[ContextNodeType.VsLiteralContext]                     = new VB6NodeTranslatorLogging.VsLiteralContext(nodeTree, dict);
            dict[ContextNodeType.VsLtContext]                          = new VB6NodeTranslatorLogging.VsLtContext(nodeTree, dict);
            dict[ContextNodeType.VsMidContext]                         = new VB6NodeTranslatorLogging.VsMidContext(nodeTree, dict);
            dict[ContextNodeType.VsMinusContext]                       = new VB6NodeTranslatorLogging.VsMinusContext(nodeTree, dict);
            dict[ContextNodeType.VsModContext]                         = new VB6NodeTranslatorLogging.VsModContext(nodeTree, dict);
            dict[ContextNodeType.VsMultContext]                        = new VB6NodeTranslatorLogging.VsMultContext(nodeTree, dict);
            dict[ContextNodeType.VsNegationContext]                    = new VB6NodeTranslatorLogging.VsNegationContext(nodeTree, dict);
            dict[ContextNodeType.VsNeqContext]                         = new VB6NodeTranslatorLogging.VsNeqContext(nodeTree, dict);
            dict[ContextNodeType.VsNewContext]                         = new VB6NodeTranslatorLogging.VsNewContext(nodeTree, dict);
            dict[ContextNodeType.VsNotContext]                         = new VB6NodeTranslatorLogging.VsNotContext(nodeTree, dict);
            dict[ContextNodeType.VsOrContext]                          = new VB6NodeTranslatorLogging.VsOrContext(nodeTree, dict);
            dict[ContextNodeType.VsPlusContext]                        = new VB6NodeTranslatorLogging.VsPlusContext(nodeTree, dict);
            dict[ContextNodeType.VsPowContext]                         = new VB6NodeTranslatorLogging.VsPowContext(nodeTree, dict);
            dict[ContextNodeType.VsStructContext]                      = new VB6NodeTranslatorLogging.VsStructContext(nodeTree, dict);
            dict[ContextNodeType.VsTypeOfContext]                      = new VB6NodeTranslatorLogging.VsTypeOfContext(nodeTree, dict);
            dict[ContextNodeType.VsXorContext]                         = new VB6NodeTranslatorLogging.VsXorContext(nodeTree, dict);
            dict[ContextNodeType.WhileWendStmtContext]                 = new VB6NodeTranslatorLogging.WhileWendStmtContext(nodeTree, dict);
            dict[ContextNodeType.WithStmtContext]                      = new VB6NodeTranslatorLogging.WithStmtContext(nodeTree, dict);
            dict[ContextNodeType.WriteStmtContext]                     = new VB6NodeTranslatorLogging.WriteStmtContext(nodeTree, dict);


            if (!Enum.TryParse(nodeTree.GetRoot().GetType().Name, out ContextNodeType contextNodeType))
            {
                throw new ArgumentException("contextNodeType");
            }
            //return dict[contextNodeType].Translate(nodeTree.GetChildren(nodeTree.GetRoot()));
            return(dict[contextNodeType].Translate(new List <ParseTree> {
                nodeTree.GetRoot()
            }));
        }
        public void GetPatterns(VB6NodeTree nodeTree)
        {
            if (nodeTree == null)
            {
                throw new ArgumentNullException(nameof(nodeTree));
            }

            // Iterate over all nodes and add them to node hash based on their concatenated type strings
            foreach (var node in nodeTree.GetAllNodes())
            {
                var subtree = new VB6SubTree(nodeTree, node);

                //var nodeTreeHashString = GetNodeTreeHashString(subtree);
                var nodeHash = GetNodeHash(node);

                if (!nodeHashDict.ContainsKey(nodeHash))
                {
                    nodeHashDict[nodeHash] = new Dictionary <string, List <VB6SubTree> >();
                }
                var tokens = String.Join(" ", GetTokens(node));
                if (!nodeHashDict[nodeHash].ContainsKey(tokens))
                {
                    nodeHashDict[nodeHash][tokens] = new List <VB6SubTree>();
                }
                nodeHashDict[nodeHash][tokens].Add(subtree);
            }

            foreach (var key in nodeHashDict.Keys)
            {
                foreach (var key2 in nodeHashDict[key].Keys)
                {
                    var s = new List <string>();
                    foreach (var subtree in nodeHashDict[key][key2])
                    {
                        var node   = subtree.GetRoot();
                        var tokens = String.Join(" ", GetTokens(node));
                        s.Add(tokens);
                    }
                    var s2 = String.Join(", ", s);
                    DebugClass.LogError("NodeTreeHashString: " + key + " " + key2 + ": " + nodeHashDict[key][key2].Count + ": " + s2);
                }
            }

            // TODO: Is it possible to auto-generate this dictionary?
            // No stress, it's small, but would be nice for other languages.
            var replaceable = new Dictionary <string, bool>();

            replaceable["AmbiguousIdentifierContext"] = true;
            replaceable["CertainIdentifierContext"]   = true;
            replaceable["LiteralContext"]             = true;
            replaceable["FieldLengthContext"]         = true;

            // Iterate over all nodes and replace each token/text in pattern with pattern letter,
            // if there are two or more variations of this token under this node type name
            const string letters = "ABCDEFGHIJKLMNOPQRSTUVXYZ";

            foreach (var node in nodeTree.GetAllNodes())
            {
                var subtree     = new VB6SubTree(nodeTree, node);
                int letterIndex = 0;
                var text        = node.getText();
                var pattern     = text;

                foreach (var child in subtree.GetAllNodes())
                {
                    if (replaceable.ContainsKey(VbToCsharpPattern.LookupNodeType(child)))
                    {
                        //DebugClass.LogError("REPLACING: " + VbToCsharpPattern.LookupNodeType(child));
                        var tokens = GetTokens(child);
                        foreach (var token in tokens)
                        {
                            if (letterIndex < letters.Length)
                            {
                                var oldPattern = pattern;
                                pattern = pattern.Replace(token, letters[letterIndex].ToString(System.Globalization.CultureInfo.InvariantCulture));
                                if (pattern != oldPattern)
                                {
                                    letterIndex++;
                                }
                                //DebugClass.LogError("REPLACING: " + token);
                            }

                            if (letterIndex >= letters.Length)
                            {
                                break;
                            }
                        }
                        if (letterIndex >= letters.Length)
                        {
                            break;
                        }
                    }
                }
                if (pattern.Length >= 100)
                {
                    pattern = "PATTERN TOO LONG";
                }
                generatedPatterns[node] = pattern;
            }
        }
        public ASTPatternGenerator(ParseTree node)
        {
            VB6NodeTree nodeTree = new VB6NodeTree(node);

            GetPatterns(nodeTree);
        }
        public static void GetCode(VB6NodeTree nodeTree)
        {
            const string genFolder = @"F:\emh-dev\VB6ToCSharpCompiler\VB6ToCSharpCompiler\VB6NodeTranslatorLogging";

            Dictionary <string, Dictionary <ImmutableList <string>, List <VB6SubTree> > > nodeTypeDict = new Dictionary <string, Dictionary <ImmutableList <string>, List <VB6SubTree> > >();

            if (nodeTree == null)
            {
                throw new ArgumentNullException(nameof(nodeTree));
            }

            // Iterate over all nodes and add them to node hash based on their concatenated type strings
            var assignments = new List <string>();

            foreach (var node in nodeTree.GetAllNodes())
            {
                var subtree = new VB6SubTree(nodeTree, node);

                //var nodeTreeHashString = GetNodeTreeHashString(subtree);
                var nodeHash = GetNodeHash(node);

                if (!nodeTypeDict.ContainsKey(nodeHash))
                {
                    nodeTypeDict[nodeHash] = new Dictionary <ImmutableList <string>, List <VB6SubTree> >();
                }

                var children = ImmutableList.Create <string>();
                foreach (var child in nodeTree.GetChildren(node))
                {
                    var tokens = String.Join(" ", GetTokens(child));
                    //if (!string.IsNullOrEmpty(tokens))
                    //{
                    //    children = children.Add("\"" + tokens + "\"");
                    //} else
                    //{

                    //}
                    children = children.Add(GetNodeHash(child));
                }

                if (!nodeTypeDict[nodeHash].ContainsKey(children))
                {
                    nodeTypeDict[nodeHash][children] = new List <VB6SubTree>();
                }
                nodeTypeDict[nodeHash][children].Add(subtree);
            }

            var hasContexts = new Dictionary <string, bool>();

            foreach (ContextNodeType contextNodeType in (ContextNodeType[])Enum.GetValues(typeof(ContextNodeType)))
            {
                var typeName  = contextNodeType.ToString("F");
                var fileName  = typeName + ".cs";
                var outString = GetCls(typeName);
                hasContexts[typeName] = true;
                System.IO.File.WriteAllText(Path.Combine(genFolder, fileName), outString);

                typeName =
                    typeName[0]
                    .ToString(System.Globalization.CultureInfo.InvariantCulture)
                    .ToUpper(System.Globalization.CultureInfo.InvariantCulture) + typeName.Substring(1);
                var assignment =
                    "dict[ContextNodeType.$TYPE] = new VB6NodeTranslatorLogging.$TYPE(nodeTree, dict);\r\n"
                    .Replace("$TYPE", typeName).Replace("$TYPE", typeName);
                assignments.Add(assignment);
            }

            var mainClass = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VB6ToCSharpCompiler
{
    public static class VB6NodeTranslatorLoader
    {
        public static IEnumerable<string> Translate(VB6NodeTree nodeTree) {
            if (nodeTree == null)
            {
                throw new ArgumentNullException(nameof(nodeTree));
            }

            var dict = new Dictionary<ContextNodeType, VB6NodeTranslator>();
            
            // dict[ContextNodeType.AmbiguousIdentifierContext] = new VB6NodeTranslatorLogging.AmbiguousIdentifierContext(nodeTree, dict);
            $ASSIGNMENTS
            
            if (!Enum.TryParse(nodeTree.GetRoot().GetType().Name, out ContextNodeType contextNodeType))
            {
                throw new ArgumentException(""contextNodeType"");
            }
            return dict[contextNodeType].Translate(nodeTree.GetChildren(nodeTree.GetRoot()));
        }
    }
}
";

            mainClass = mainClass.Replace("$ASSIGNMENTS", String.Join("", assignments));
            System.IO.File.WriteAllText(Path.Combine(genFolder, "../VB6NodeTranslatorLoader.cs"), mainClass);

            //foreach (var key in nodeTypeDict.Keys)
            //{
            //    var typeName = key;
            //    if (!hasContexts.ContainsKey(typeName))
            //    {
            //        DebugClass.LogError(typeName + ",");
            //        hasContexts[typeName] = true;
            //    }
            //    var fileName = typeName + ".cs";
            //    var outString = GetCls(typeName);
            //    System.IO.File.WriteAllText(Path.Combine(genFolder, fileName), outString);
            //}
        }
示例#7
0
 public VB6NodeTranslator(VB6NodeTree nodeTree, Dictionary <ContextNodeType, VB6NodeTranslator> translatorDict)
 {
     this.nodeTree       = nodeTree;
     this.translatorDict = translatorDict;
 }