Exemplo n.º 1
0
        public static List <IndexedPath> GetExtendedPathList(VB6NodeTree nodeTree, ParseTree node)
        {
            if (nodeTree == null)
            {
                throw new ArgumentNullException(nameof(nodeTree));
            }

            var iterationNode = node;
            var s             = new List <IndexedPath>();
            int depth         = 0;

            while (iterationNode != null)
            {
                var index = -1;
                if (iterationNode.getParent() != null)
                {
                    var i = 0;
                    foreach (var child in nodeTree.GetChildren(iterationNode.getParent()))
                    {
                        if (child == iterationNode)
                        {
                            index = i;
                            break;
                        }
                        i++;
                    }

                    if (index == -1)
                    {
                        throw new InvalidOperationException("could not find child node in parent");
                    }
                }
                s.Add(new IndexedPath(VbToCsharpPattern.LookupNodeType(iterationNode), index, iterationNode.getText()));
                iterationNode = iterationNode.getParent();
                depth++;
                if (depth > 100)
                {
                    throw new InvalidOperationException("depth too big");
                }
            }
            s.Reverse();
            return(s);
        }
Exemplo n.º 2
0
        public StatementSyntax TranslateProperty(
            string ctrlType,
            string ctrlIdentifier,
            VisualBasic6Parser.Cp_SinglePropertyContext property
            )
        {
            var propertyName =
                vb6NodeTree
                .GetChildren(property)
                .Find(x => x is VisualBasic6Parser.ImplicitCallStmt_InStmtContext)
                .getText()
                .Trim();
            var propertyValueContext =
                vb6NodeTree
                .GetChildren(property)
                .Find(x => x is VisualBasic6Parser.Cp_PropertyValueContext);
            var literalContext =
                vb6NodeTree
                .GetChildren(propertyValueContext)
                .Find(x => x is VisualBasic6Parser.LiteralContext);

            var asg = (LiteralImpl)_compileResult.Program.getASGElementRegistry().getASGElement(literalContext);

            if (asg == null)
            {
                DebugClass.LogError("ASG missing for: " + ctrlIdentifier + " for property: " + propertyName);
                throw new System.NotImplementedException("Don't know how to handle ASG null.");
            }

            var valueNode = TranslatorForExpression.GetExpression(asg);

            var valueString = valueNode.NormalizeWhitespace().ToFullString();

            var translatedPropertyName = TranslatePropertyName(ctrlType, propertyName);

            return(SyntaxFactory.ParseStatement(ctrlIdentifier + "." + translatedPropertyName + " = " + valueString + ";"));
        }
        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);
            //}
        }