示例#1
0
        public static void Main(string[] args)
        {
            string directory   = "../../../Project/Src/Ast/";
            string visitorsDir = "../../../Project/Src/Visitors/";

            Debug.WriteLine("AST Generator running...");
            if (!File.Exists(directory + "INode.cs"))
            {
                Debug.WriteLine("did not find output directory");
                return;
            }
            if (!File.Exists(visitorsDir + "AbstractAstTransformer.cs"))
            {
                Debug.WriteLine("did not find visitor output directory");
                return;
            }

            List <Type> nodeTypes = new List <Type>();

            foreach (Type type in typeof(MainClass).Assembly.GetTypes())
            {
                if (type.IsClass && typeof(INode).IsAssignableFrom(type))
                {
                    nodeTypes.Add(type);
                }
            }
            nodeTypes.Sort(delegate(Type a, Type b) { return(a.Name.CompareTo(b.Name)); });

            CodeCompileUnit ccu = new CodeCompileUnit();
            CodeNamespace   cns = ccu.AddNamespace("ICSharpCode.NRefactory.Ast");

            cns.AddImport("System");
            cns.AddImport("System.Collections.Generic");
            foreach (Type type in nodeTypes)
            {
                if (type.GetCustomAttributes(typeof(CustomImplementationAttribute), false).Length == 0)
                {
                    CodeTypeDeclaration ctd = cns.AddType(type.Name);
                    if (type.IsAbstract)
                    {
                        ctd.TypeAttributes |= TypeAttributes.Abstract;
                    }
                    ctd.BaseTypes.Add(new CodeTypeReference(type.BaseType.Name));

                    ProcessType(type, ctd);

                    foreach (object o in type.GetCustomAttributes(false))
                    {
                        if (o is TypeImplementationModifierAttribute)
                        {
                            (o as TypeImplementationModifierAttribute).ModifyImplementation(cns, ctd, type);
                        }
                    }

                    if (!type.IsAbstract)
                    {
                        CodeMemberMethod method = new CodeMemberMethod();
                        method.Name       = "AcceptVisitor";
                        method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
                        method.Parameters.Add(new CodeParameterDeclarationExpression("IAstVisitor", "visitor"));
                        method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "data"));
                        method.ReturnType = new CodeTypeReference(typeof(object));
                        CodeExpression ex = new CodeVariableReferenceExpression("visitor");
                        ex = new CodeMethodInvokeExpression(ex, VisitPrefix + ctd.Name,
                                                            new CodeThisReferenceExpression(),
                                                            new CodeVariableReferenceExpression("data"));
                        method.Statements.Add(new CodeMethodReturnStatement(ex));
                        ctd.Members.Add(method);

                        method            = new CodeMemberMethod();
                        method.Name       = "ToString";
                        method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
                        method.ReturnType = new CodeTypeReference(typeof(string));
                        method.Statements.Add(new CodeMethodReturnStatement(CreateToString(type)));
                        ctd.Members.Add(method);
                    }
                }
            }

            System.CodeDom.Compiler.CodeGeneratorOptions settings = new System.CodeDom.Compiler.CodeGeneratorOptions();
            settings.IndentString  = "\t";
            settings.VerbatimOrder = true;

            using (StringWriter writer = new StringWriter()) {
                new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
                File.WriteAllText(directory + "Generated.cs", writer.ToString());
            }

            ccu = new CodeCompileUnit();
            cns = ccu.AddNamespace("ICSharpCode.NRefactory");
            cns.AddImport("System");
            cns.AddImport("ICSharpCode.NRefactory.Ast");
            cns.Types.Add(CreateAstVisitorInterface(nodeTypes));

            using (StringWriter writer = new StringWriter()) {
                new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
                File.WriteAllText(visitorsDir + "../IAstVisitor.cs", writer.ToString());
            }

            ccu = new CodeCompileUnit();
            cns = ccu.AddNamespace("ICSharpCode.NRefactory.Visitors");
            cns.AddImport("System");
            cns.AddImport("System.Collections.Generic");
            cns.AddImport("System.Diagnostics");
            cns.AddImport("ICSharpCode.NRefactory.Ast");
            cns.Types.Add(CreateAstVisitorClass(nodeTypes, false));

            using (StringWriter writer = new StringWriter()) {
                new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
                File.WriteAllText(visitorsDir + "AbstractAstVisitor.cs", writer.ToString());
            }

            ccu = new CodeCompileUnit();
            cns = ccu.AddNamespace("ICSharpCode.NRefactory.Visitors");
            cns.AddImport("System");
            cns.AddImport("System.Collections.Generic");
            cns.AddImport("System.Diagnostics");
            cns.AddImport("ICSharpCode.NRefactory.Ast");
            cns.Types.Add(CreateAstVisitorClass(nodeTypes, true));

            using (StringWriter writer = new StringWriter()) {
                new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
                File.WriteAllText(visitorsDir + "AbstractAstTransformer.cs", writer.ToString());
            }

            ccu = new CodeCompileUnit();
            cns = ccu.AddNamespace("ICSharpCode.NRefactory.Visitors");
            cns.AddImport("System");
            cns.AddImport("ICSharpCode.NRefactory.Ast");
            cns.Types.Add(CreateNodeTrackingAstVisitorClass(nodeTypes));

            using (StringWriter writer = new StringWriter()) {
                new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
                // CodeDom cannot output "sealed", so we need to use this hack:
                File.WriteAllText(visitorsDir + "NodeTrackingAstVisitor.cs",
                                  writer.ToString().Replace("public override object", "public sealed override object"));
            }

            //NotImplementedAstVisitor
            ccu = new CodeCompileUnit();
            cns = ccu.AddNamespace("ICSharpCode.NRefactory.Visitors");
            cns.AddImport("System");
            cns.AddImport("ICSharpCode.NRefactory.Ast");
            cns.Types.Add(CreateNotImplementedAstVisitorClass(nodeTypes));

            using (StringWriter writer = new StringWriter()) {
                new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
                File.WriteAllText(visitorsDir + "NotImplementedAstVisitor.cs", writer.ToString());
            }
            Debug.WriteLine("AST Generator done!");
        }
示例#2
0
        public static void Main(string[] args)
        {
            string directory   = "../../NRefactory/Project/Src/Ast/";
            string visitorsDir = "../../NRefactory/Project/Src/Visitors/";

            Console.WriteLine("AST Generator running...");
            if (!File.Exists(directory + "INode.cs"))
            {
                Console.WriteLine("did not find output directory " + Path.GetFullPath(Path.GetDirectoryName(directory)));
                return;
            }
            if (!File.Exists(visitorsDir + "AbstractAstTransformer.cs"))
            {
                Console.WriteLine("did not find visitor output directory");
                return;
            }

            List <Type> nodeTypes = new List <Type>();

            foreach (Type type in typeof(MainClass).Assembly.GetTypes())
            {
                if (type.IsClass && typeof(INode).IsAssignableFrom(type))
                {
                    nodeTypes.Add(type);
                }
            }
            nodeTypes.Sort(delegate(Type a, Type b) { return(a.Name.CompareTo(b.Name)); });

            CodeCompileUnit ccu = new CodeCompileUnit();
            CodeNamespace   cns = ccu.AddNamespace("ICSharpCode.NRefactory.Ast");

            cns.AddImport("System");
            cns.AddImport("System.Collections.Generic");
            cns.AddImport("AgentRalph.Visitors");
            foreach (Type type in nodeTypes)
            {
                if (type.GetCustomAttributes(typeof(CustomImplementationAttribute), false).Length == 0)
                {
                    CodeTypeDeclaration ctd = cns.AddType(type.Name);
                    if (type.IsAbstract)
                    {
                        ctd.TypeAttributes |= TypeAttributes.Abstract;
                    }
                    ctd.IsPartial = true;
                    ctd.BaseTypes.Add(new CodeTypeReference(type.BaseType.Name));

                    ProcessType(type, ctd);

                    foreach (object o in type.GetCustomAttributes(false))
                    {
                        if (o is TypeImplementationModifierAttribute)
                        {
                            (o as TypeImplementationModifierAttribute).ModifyImplementation(cns, ctd, type);
                        }
                    }

                    if (!type.IsAbstract)
                    {
                        AddAnAcceptVisitorMethod(type, ctd, "IAstVisitor", typeof(object));

                        // Create an overload
                        AddAnAcceptVisitorMethod(type, ctd, "AstComparisonVisitor", typeof(bool));

                        AddAChillunsGetter(type, ctd);

                        var method = new CodeMemberMethod();
                        method.Name       = "ToString";
                        method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
                        method.ReturnType = new CodeTypeReference(typeof(string));
                        method.Statements.Add(new CodeMethodReturnStatement(CreateToString(type)));
                        ctd.Members.Add(method);
                    }
                }
            }

            System.CodeDom.Compiler.CodeGeneratorOptions settings = new System.CodeDom.Compiler.CodeGeneratorOptions();
            settings.IndentString  = "\t";
            settings.VerbatimOrder = true;

            using (StringWriter writer = new StringWriter()) {
                new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
                File.WriteAllText(directory + "Generated.cs", writer.ToString());
            }

            ccu = new CodeCompileUnit();
            cns = ccu.AddNamespace("ICSharpCode.NRefactory");
            cns.AddImport("System");
            cns.AddImport("ICSharpCode.NRefactory.Ast");
            cns.Types.Add(CreateAstVisitorInterface(nodeTypes));

            using (StringWriter writer = new StringWriter()) {
                new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
                File.WriteAllText(visitorsDir + "../IAstVisitor.cs", writer.ToString());
            }

            // My modifications to the generation process are not preserving the creation of these particular files.
            // So I just don't regenerate them for the time being.
//			ccu = new CodeCompileUnit();
//			cns = ccu.AddNamespace("ICSharpCode.NRefactory.Visitors");
//			cns.AddImport("System");
//			cns.AddImport("System.Collections.Generic");
//			cns.AddImport("System.Diagnostics");
//			cns.AddImport("ICSharpCode.NRefactory.Ast");
//			cns.Types.Add(CreateAstVisitorClass(nodeTypes, false));
//
//			using (StringWriter writer = new StringWriter()) {
//				new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
//				File.WriteAllText(visitorsDir + "AbstractAstVisitor.cs", writer.ToString());
//			}
//
//			ccu = new CodeCompileUnit();
//			cns = ccu.AddNamespace("ICSharpCode.NRefactory.Visitors");
//			cns.AddImport("System");
//			cns.AddImport("System.Collections.Generic");
//			cns.AddImport("System.Diagnostics");
//			cns.AddImport("ICSharpCode.NRefactory.Ast");
//			cns.Types.Add(CreateAstVisitorClass(nodeTypes, true));
//
//			using (StringWriter writer = new StringWriter()) {
//				new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
//				File.WriteAllText(visitorsDir + "AbstractAstTransformer.cs", writer.ToString());
//			}

            ccu = new CodeCompileUnit();
            cns = ccu.AddNamespace("ICSharpCode.NRefactory.Visitors");
            cns.AddImport("System");
            cns.AddImport("ICSharpCode.NRefactory.Ast");
            cns.Types.Add(CreateNodeTrackingAstVisitorClass(nodeTypes));

            using (StringWriter writer = new StringWriter()) {
                new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
                // CodeDom cannot output "sealed", so we need to use this hack:
                File.WriteAllText(visitorsDir + "NodeTrackingAstVisitor.cs",
                                  writer.ToString().Replace("public override object", "public sealed override object"));
            }

            //NotImplementedAstVisitor
            ccu = new CodeCompileUnit();
            cns = ccu.AddNamespace("ICSharpCode.NRefactory.Visitors");
            cns.AddImport("System");
            cns.AddImport("ICSharpCode.NRefactory.Ast");
            cns.Types.Add(CreateNotImplementedAstVisitorClass(nodeTypes));

            using (StringWriter writer = new StringWriter()) {
                new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
                File.WriteAllText(visitorsDir + "NotImplementedAstVisitor.cs", writer.ToString());
            }


            // TwoTreeAbstractAstVisitor
            ccu = new CodeCompileUnit();
            cns = ccu.AddNamespace("AgentRalph.Visitors");
            cns.AddImport("System");
            cns.AddImport("System.Collections.Generic");
            cns.AddImport("System.Diagnostics");
            cns.AddImport("ICSharpCode.NRefactory.Ast");
            cns.AddImport("ICSharpCode.NRefactory");
            cns.Types.Add(CreateAstComparisonVisitorClass(nodeTypes));

            using (StringWriter writer = new StringWriter())
            {
                new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
                // Put it in the NRefactor project
                var path = visitorsDir + "AstComparisonVisitor.Generated.cs";
                File.WriteAllText(path, writer.ToString());
                Console.WriteLine("Created file " + path);
            }



            Console.WriteLine("AST Generator done!");
        }
示例#3
0
 public static CodeNamespaceImport AddImport(this CodeNamespace ns, string namespaceToImport)
 => ns.AddImport(new CodeNamespaceImport(namespaceToImport));