Exemplo n.º 1
0
 public ReadOnlyInterfaceMethod(InterfaceMethod interfaceMethod)
 {
     this.interfaceMethod = interfaceMethod;
     genericParameters    = ReadOnlyGenericParameterDeclaration.Create(interfaceMethod.GenericParameters);
     parameters           = ReadOnlyMethodParameter.Create(interfaceMethod.Parameters);
     returnType           = new ReadOnlyTypeReference(interfaceMethod.ReturnType);
 }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            //AsyncContext.Run(GenerateVisitorInterface);
            string baseDirectory = Path.GetFullPath(
                Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"..\..\..\.."));
            AssemblyWithReflection assembly       = new AssemblyWithReflection(typeof(ClassWithCodeAnalysis).Assembly);
            Interface            visitorInterface = new Interface("ICodeAnalysisVisitor");
            IEnumerable <string> classNames       =
                from @class in assembly.AllClasses
                let baseClassName = @class.BaseClass?.Name
                                    where baseClassName != null &&
                                    baseClassName.StartsWith("Editable") &&
                                    !baseClassName.Contains("Expression") &&
                                    !baseClassName.Contains("Statement")
                                    let className = @class.Name
                                                    orderby className
                                                    select className;

            foreach (string className in classNames)
            {
                string parameterName = Regex.Replace(className, "WithCodeAnalysis$", "");
                parameterName = parameterName.Substring(0, 1).ToLower() + parameterName.Substring(1);
                InterfaceMethod method = new InterfaceMethod($"Visit{className}")
                {
                    ReturnType = new TypeReference(typeof(void)),
                    Parameters = new Collection <MethodParameter>()
                    {
                        new MethodParameter(parameterName, new TypeReference(className))
                    }
                };
                visitorInterface.Body.Methods.Add(method);
            }

            Console.WriteLine(visitorInterface.ToString());
        }
Exemplo n.º 3
0
        public void Interface_Methods()
        {
            const string code = @"    
            public interface Interface1 
            {
                string Method1(int param, Interface1 i);
            }
            ";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Interface clazz    = (Interface)codeRoot.WalkChildren()[0];

            InterfaceMethod inter = (InterfaceMethod)clazz.WalkChildren()[0];

            Assert.That(inter.Name, Is.EqualTo("Method1"));
            Assert.That(inter.ReturnType.ToString(), Is.EqualTo("string"));
            Assert.That(inter.Parameters, Has.Count(2));
            Assert.That(inter.Parameters[0].Name, Is.EqualTo("param"));
            Assert.That(inter.Parameters[0].DataType, Is.EqualTo("int"));
            Assert.That(inter.Parameters[1].Name, Is.EqualTo("i"));
            Assert.That(inter.Parameters[1].DataType, Is.EqualTo("Interface1"));
        }
Exemplo n.º 4
0
        public void InterfaceMethodBasicFeature()
        {
            var md = new InterfaceMethod("GetNumber");

            Assert.AreEqual("void GetNumber();" + SourceCodeGenerator.NewLine
                            , SourceCodeGenerator.Write(SourceCodeLanguage.CSharp, md));
        }
Exemplo n.º 5
0
        public void GenericInterfaceMethod_WithConstraints()
        {
            const string code = @"    
            public interface Interface1 
            {
                string Method1<T>(int param, T i) where T : class;
            }
            ";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            if (parser.ErrorOccurred)
            {
                Assert.Fail(parser.GetFormattedErrors());
            }

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Interface clazz    = (Interface)codeRoot.WalkChildren()[0];

            InterfaceMethod inter = (InterfaceMethod)clazz.WalkChildren()[0];

            Assert.That(inter.Name, Is.EqualTo("Method1"));
            Assert.That(inter.GenericParameters.Count, Is.EqualTo(1));
            Assert.That(inter.GenericParameters[0], Is.EqualTo("T"));
            Assert.That(inter.GenericConstraintClause, Is.EqualTo("where T : class"));
            Assert.That(inter.ReturnType.ToString(), Is.EqualTo("string"));
            Assert.That(inter.Parameters, Has.Count(2));
            Assert.That(inter.Parameters[0].Name, Is.EqualTo("param"));
            Assert.That(inter.Parameters[0].DataType, Is.EqualTo("int"));
            Assert.That(inter.Parameters[1].Name, Is.EqualTo("i"));
            Assert.That(inter.Parameters[1].DataType, Is.EqualTo("T"));
        }
Exemplo n.º 6
0
        public void InterfaceMethodParameters()
        {
            var md = new InterfaceMethod("GetDisplayName");

            md.Parameters.Add(new MethodParameter("String", "name"));
            md.Parameters.Add(new MethodParameter("Int32", "age"));

            Assert.AreEqual("void GetDisplayName(String name, Int32 age);" + SourceCodeGenerator.NewLine
                            , SourceCodeGenerator.Write(SourceCodeLanguage.CSharp, md));
        }
Exemplo n.º 7
0
        public void InterfaceMethodPartialFeature()
        {
            var md = new InterfaceMethod("GetNumber");

            //These will be ignored
            md.Parameters.Add(new MethodParameter("Int32", "number"));
            md.GenericParameters.Add("T");
            md.ReturnTypeName = new TypeName("Int32");

            Assert.AreEqual("Int32 GetNumber<T>(Int32 number);" + SourceCodeGenerator.NewLine
                            , SourceCodeGenerator.Write(SourceCodeLanguage.CSharp, md));
        }
Exemplo n.º 8
0
        protected CodeRoot CreateInterfaceAndMethod(string parameterName)
        {
            InterfaceMethod inter = new InterfaceMethod(controller, "Method1");

            inter.ReturnType = new DataType(controller, "int");
            Parameter param = new Parameter(controller);

            param.Name     = parameterName;
            param.DataType = "int";
            inter.Parameters.Add(param);

            return(CreateNamespaceAndInterface(inter));
        }
        public void An_InterfaceMethod_Is_Created()
        {
            const string code = "void Method1();";

            CSharpParser   parser = new CSharpParser();
            IBaseConstruct bc     = parser.ParseSingleConstruct(code, BaseConstructType.InterfaceMethodDeclaration);

            Assert.That(bc, Is.Not.Null);
            Assert.That(bc, Is.InstanceOfType(typeof(InterfaceMethod)));

            InterfaceMethod con = (InterfaceMethod)bc;

            Assert.That(con.Name, Is.EqualTo("Method1"));
            Assert.That(con.ReturnType.Name, Is.EqualTo("void"));
        }
Exemplo n.º 10
0
            public (IParseFunction, IReadOnlyList <InterfaceMethod>) Visit(Sequence target, INodeType input)
            {
                var interfaceMethods = new List <InterfaceMethod>();

                InterfaceMethod interfaceMethod = null;
                var             types           = target.Steps.Where(i => i.IsReturned).Select(i => i.Function.ReturnType).ToList();

                if (types.Count > 0)
                {
                    var name = $"{interfaceMethodName}{++count}";
                    interfaceMethod = new InterfaceMethod(input, name, types);
                    interfaceMethods.Add(interfaceMethod);
                }

                return(new Sequence(target.Steps, interfaceMethod), interfaceMethods);
            }
Exemplo n.º 11
0
        public void InterfaceMethod()
        {
            Interface       it   = new Interface(controller, "Interface1");
            InterfaceMethod item = new InterfaceMethod(controller, "Function1", new DataType(controller, "int"));

            item.Parameters.Add(new Parameter(controller, "float", "f"));
            item.Parameters.Add(new Parameter(controller, "InputObject", "j"));
            Assert.That(item.FullyQualifiedDisplayName, Is.EqualTo("Function1 (float, InputObject)"));

            it.AddChild(item);
            Namespace ns = new Namespace(controller);

            ns.Name = "ns1";
            ns.AddChild(it);

            Assert.That(item.FullyQualifiedDisplayName, Is.EqualTo("ns1.Interface1.Function1 (float, InputObject)"));
        }
Exemplo n.º 12
0
        private void BasicTargetWithInterfaceMethod(IParseFunction target, InterfaceMethod interfaceMethod, Signature signature)
        {
            var resultName = "r";
            var nodeName   = $"{resultName}.Node";
            var invoker    = this.invokers[target];

            writer.VarAssign(resultName, invoker("input", "inputPosition", "states", "factory"));             // Invocation
            writer.IfNullReturnNull(resultName);

            var decl = new Decl(target.ReturnType, nodeName);

            var returnExpression = GetReturnExpression(target.ReturnType, new[] { decl }, $"{resultName}.Advanced", "factory", interfaceMethod);

            if (signature.IsMemoized)
            {
                var memField = NameGen.MemoizedFieldName(signature.Name);
                returnExpression = $"states[inputPosition].{memField} = {returnExpression}";
            }
            writer.Return(returnExpression);
        }
Exemplo n.º 13
0
        public void InterfaceBasicFeature()
        {
            var c = new Interface("IPerson");

            var p = new InterfaceProperty("Int32", "Age", true, true);

            c.Properties.Add(p);

            var md = new InterfaceMethod("GetNumber");

            c.Methods.Add(md);

            Assert.AreEqual("public interface IPerson" + SourceCodeGenerator.NewLine
                            + "{" + SourceCodeGenerator.NewLine
                            + "    Int32 Age { get; set; }" + SourceCodeGenerator.NewLine
                            + SourceCodeGenerator.NewLine
                            + "    void GetNumber();" + SourceCodeGenerator.NewLine
                            + "}" + SourceCodeGenerator.NewLine
                            , SourceCodeGenerator.Write(SourceCodeLanguage.CSharp, c));
        }
Exemplo n.º 14
0
        private static async Task GenerateVisitorInterface()
        {
            string baseDirectory = Path.GetFullPath(
                Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"..\..\..\.."));
            ProjectWithCodeAnalysis project = await ProjectWithCodeAnalysis.OpenAsync(
                Path.Combine(baseDirectory, @"CSharpDom.CodeAnalysis\CSharpDom.CodeAnalysis.csproj"));

            LoadedProjectWithCodeAnalysis loadedProject = await project.LoadAsync();

            Interface            visitorInterface = new Interface("ICodeAnalysisVisitor");
            IEnumerable <string> classNames       =
                from @class in loadedProject.AllClasses
                let baseClassName = @class.BaseClass?.Name
                                    where baseClassName != null &&
                                    baseClassName.StartsWith("Editable") &&
                                    !baseClassName.Contains("Expression") &&
                                    !baseClassName.Contains("Statement")
                                    let className = @class.Name
                                                    orderby className
                                                    select className;

            foreach (string className in classNames)
            {
                string parameterName = Regex.Replace(className, "WithCodeAnalysis$", "");
                parameterName = parameterName.Substring(0, 1).ToLower() + parameterName.Substring(1);
                InterfaceMethod method = new InterfaceMethod($"Visit{className}")
                {
                    ReturnType = new TypeReference(typeof(void)),
                    Parameters = new Collection <MethodParameter>()
                    {
                        new MethodParameter(parameterName, new TypeReference(className))
                    }
                };
                visitorInterface.Body.Methods.Add(method);
            }

            Console.WriteLine(visitorInterface.ToString());
        }
Exemplo n.º 15
0
            public (IParseFunction, IReadOnlyList <InterfaceMethod>) Visit(Selection target, INodeType input)
            {
                var interfaceMethods = new List <InterfaceMethod>();
                var newSteps         = new List <SelectionStep>();

                foreach (var step in target.Steps)
                {
                    IParseFunction func           = step.Function;
                    INodeType      funcReturnType = func.ReturnType;

                    InterfaceMethod interfaceMethod = null;
                    if (input != EmptyNodeType.Instance)
                    {
                        var name = $"{interfaceMethodName}{++count}";
                        interfaceMethod = new InterfaceMethod(input, name, GetParameterTypesFromReturnType(funcReturnType));
                        interfaceMethods.Add(interfaceMethod);
                    }

                    var newStep = new SelectionStep(func, interfaceMethod);
                    newSteps.Add(newStep);
                }

                return(new Selection(newSteps), interfaceMethods);
            }
Exemplo n.º 16
0
 public InterfaceMethodPrinter(InterfaceMethod obj)
     : base(obj)
 {
     this.obj = obj;
 }
Exemplo n.º 17
0
        private static String GetReturnExpression(INodeType returnType, IReadOnlyList <Decl> nodes, String inputPositionReference, String factoryName, InterfaceMethod interfaceMethod)
        {
            String nodeString;

            if (returnType == EmptyNodeType.Instance)
            {
                nodeString = "EmptyNode.Instance";
            }
            else if (interfaceMethod != null)
            {
                String param;
                if (nodes.Count == 1 && nodes[0].Type == EmptyNodeType.Instance)
                {
                    param = String.Empty;
                }
                else
                {
                    param = String.Join(", ", nodes.Select(i => i.Name));
                }

                nodeString = $"{factoryName}.{interfaceMethod.Name}({param})";
            }
            else if (nodes.Count == 1)
            {
                nodeString = nodes[0].Name;
            }
            else
            {
                // Make a tuple
                nodeString = $"({String.Join(", ", nodes.Select(i => i.Name))})";
            }

            var returnTypeString = returnType.GetParseResultTypeString();

            return($"new {returnTypeString}({nodeString}, {inputPositionReference})");
        }
        protected CodeRoot CreateInterfaceAndMethod(string parameterName)
        {
            InterfaceMethod inter = new InterfaceMethod(controller, "Method1");
            inter.ReturnType = new DataType(controller, "int");
            Parameter param = new Parameter(controller);
            param.Name = parameterName;
            param.DataType = "int";
            inter.Parameters.Add(param);

            return CreateNamespaceAndInterface(inter);
        }
        public void InterfaceMethod()
        {
            Interface it = new Interface(controller, "Interface1");
            InterfaceMethod item = new InterfaceMethod(controller, "Function1", new DataType(controller, "int"));
            item.Parameters.Add(new Parameter(controller, "float", "f"));
            item.Parameters.Add(new Parameter(controller, "InputObject", "j"));
            Assert.That(item.FullyQualifiedDisplayName, Is.EqualTo("Function1 (float, InputObject)"));

            it.AddChild(item);
            Namespace ns = new Namespace(controller);
            ns.Name = "ns1";
            ns.AddChild(it);

            Assert.That(item.FullyQualifiedDisplayName, Is.EqualTo("ns1.Interface1.Function1 (float, InputObject)"));
        }
Exemplo n.º 20
0
 public void ImplementsAdd(InterfaceMethod interfaceString)
 {
     mImplementList.Add(interfaceString);
 }
 public VBInterfaceMethodPrinter(InterfaceMethod obj)
 {
     this.obj = obj;
 }
        public void InterfaceMethod_Change_DataType()
        {
            const string name = "MyInterfaceMethod1";
            DataType type1 = new DataType(controller, DataType1);
            DataType type2 = new DataType(controller, DataType2);
            const string expectedResult = DataType2 + " " + name;

            InterfaceMethod merged1 = new InterfaceMethod(controller, name);
            InterfaceMethod merged2 = new InterfaceMethod(controller, name);
            InterfaceMethod merged3 = new InterfaceMethod(controller, name);

            InterfaceMethod unchanging = new InterfaceMethod(controller, name, type1);
            InterfaceMethod changing = new InterfaceMethod(controller, name, type2);

            Merge_And_Assert(merged1, merged2, merged3, changing, unchanging, expectedResult);
        }
        public void InterfaceMethod_Add_HasNewKeyword()
        {
            const string name = "MyInterfaceMethod1";
            DataType type1 = new DataType(controller, DataType1);
            const string expectedResult = "new " + DataType1 + " " + name;

            InterfaceMethod merged1 = new InterfaceMethod(controller, name);
            InterfaceMethod merged2 = new InterfaceMethod(controller, name);
            InterfaceMethod merged3 = new InterfaceMethod(controller, name);

            InterfaceMethod unchanging = new InterfaceMethod(controller, name, type1);
            InterfaceMethod changing = new InterfaceMethod(controller, name, type1);
            changing.HasNewKeyword = true;

            Merge_And_Assert(merged1, merged2, merged3, changing, unchanging, expectedResult);
        }
Exemplo n.º 24
0
        private void Process_Interace_Method_Declaration(InterfaceMethodDeclaration node)
        {
            if (node == null) throw new ArgumentNullException("node");

            InterfaceMethod inter = new InterfaceMethod(controller, node.Name.Text);
            inter.ReturnType = FormatterUtility.GetDataTypeFromTypeReference(node.ReturnType, document, controller);
            foreach (ParameterDeclaration paramNode in node.Parameters)
            {
                inter.Parameters.Add(GetParameterFromParameterDeclaration(document, controller, paramNode));
            }

            if (node.IsGenericMethodDefinition)
            {
                List<string> genericTypeReferences = new List<string>();
                List<string> genericParameterContraints = new List<string>();

                foreach (TypeReference gtp in node.GenericTypeArguments)
                {
                    Process_Generic_Type_Argument(gtp, genericTypeReferences, genericParameterContraints);
                }

                inter.GenericParameters.AddRange(genericTypeReferences);

                if (genericParameterContraints.Count > 0)
                {
                    inter.GenericConstraintClause = string.Format("where {0}", string.Join(", ", genericParameterContraints.ToArray()));
                }
            }

            SetupBaseConstruct(node, inter);
        }
Exemplo n.º 25
0
 public VBInterfaceMethodPrinter(InterfaceMethod obj)
 {
     this.obj = obj;
 }
 public InterfaceMethodPrinter(InterfaceMethod obj)
     : base(obj)
 {
     this.obj = obj;
 }