Exemple #1
0
        public void Test_Large_Switch_Single_Construct_Parse()
        {
            const string codeToParse = "public void Method1(){ string nullableType = isNullable ? \"?\" : \"\";\tswitch(sqlDataType.Trim().ToLower())\t{\t\tcase \"bigint\":\t\t\treturn \"long\" + nullableType;\t\tcase \"binary\":\t\t\treturn \"byte[]\";\t\tcase \"bit\":\t\t\treturn \"bool\" + nullableType;\t\tcase \"char\":\t\t\treturn \"string\";\t\tcase \"datetime\":\t\t\treturn \"DateTime\" + nullableType;\t\tcase \"decimal\":\t\t\treturn \"decimal\" + nullableType;\t\tcase \"float\":\t\t\treturn \"double\" + nullableType;\t\tcase \"image\":\t\t\treturn \"byte[]\";\t\tcase \"int\":\t\t\treturn \"int\" + nullableType;\t\tcase \"money\":\t\t\treturn \"decimal\" + nullableType;\t\tcase \"nchar\":\t\t\treturn \"string\";\t\tcase \"ntext\":\t\t\treturn \"string\";\t\tcase \"numeric\":\t\t\treturn \"decimal\" + nullableType;\t\tcase \"nvarchar\":\t\t\treturn \"string\";\t\tcase \"real\":\t\t\treturn \"float\" + nullableType;\t\tcase \"smalldatetime\":\t\t\treturn \"DateTime\" + nullableType;\t\tcase \"smallint\":\t\t\treturn \"short\" + nullableType;\t\tcase \"smallmoney\":\t\t\treturn \"decimal\" + nullableType;\t\tcase \"sql_variant\":\t\t\treturn \"object\";\t\tcase \"text\":\t\t\treturn \"string\";\t\tcase \"timestamp\":\t\t\treturn \"byte[]\";\t\tcase \"tinyint\":\t\t\treturn \"byte\" + nullableType;\t\tcase \"uniqueidentifier\":\t\t\treturn \"Guid\" + nullableType;\t\tcase \"varbinary\":\t\t\treturn \"byte[]\";\t\tcase \"varchar\":\t\t\treturn \"string\";\t\tcase \"xml\":\t\t\treturn \"string\";\t\tdefault:\t\t\tthrow new Exception(sqlDataType + \" data type not supported by the ArchAngel.Providers.Database.Model API yet. Please contact Slyce support: [email protected]\"); } }";

            CSharpParser csf = new CSharpParser();
            Function     f   = (Function)csf.ParseSingleConstruct(codeToParse, BaseConstructType.MethodDeclaration);

            Assert.That(csf.ErrorOccurred, Is.False);
            Assert.That(f.Name, Is.EqualTo("Method1"));
        }
        public void A_Structure_Is_Created()
        {
            const string code = "public struct Structure1 { }";

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

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

            Struct con = (Struct)bc;

            Assert.That(con.Name, Is.EqualTo("Structure1"));
        }
        public void A_UsingStatement_Is_Created()
        {
            const string code = "using System;";

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

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

            UsingStatement con = (UsingStatement)bc;

            Assert.That(con.Value, Is.EqualTo("System"));
        }
        public void A_Namespace_Is_Created()
        {
            const string code = "namespace Namespace1 { }";

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

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

            Namespace con = (Namespace)bc;

            Assert.That(con.Name, Is.EqualTo("Namespace1"));
        }
        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"));
        }
        public void An_InterfaceEvent_Is_Created()
        {
            const string code = "event Delegate1 Event1;";

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

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

            InterfaceEvent con = (InterfaceEvent)bc;

            Assert.That(con.Name, Is.EqualTo("Event1"));
            Assert.That(con.DataType.Name, Is.EqualTo("Delegate1"));
        }
        public void An_Interface_Is_Created()
        {
            const string code = "public interface Interface1 { }";

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

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

            Interface con = (Interface)bc;

            Assert.That(con.Name, Is.EqualTo("Interface1"));
            Assert.That(con.Modifiers.Count, Is.EqualTo(1));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
        }
        public void A_Class_Is_Created()
        {
            const string code = "public class Class1 { }";

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

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

            Class con = (Class)bc;

            Assert.That(con.Name, Is.EqualTo("Class1"));
            Assert.That(con.Modifiers.Count, Is.EqualTo(1));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
        }
        public void A_Destructor_Is_Created()
        {
            // This should not be the name of the class
            const string code = "public ~MadeUpClassName() { }";

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

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

            Destructor con = (Destructor)bc;

            Assert.That(con.Name, Is.EqualTo("MadeUpClassName"));
            Assert.That(con.BodyText, Is.EqualTo("{\n}\n".Replace("\n", Environment.NewLine)));
        }
        public void An_InterfaceProperty_Is_Created()
        {
            const string code = "int Property1 { get; set; }";

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

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

            InterfaceProperty con = (InterfaceProperty)bc;

            Assert.That(con.Name, Is.EqualTo("Property1"));
            Assert.That(con.DataType.Name, Is.EqualTo("int"));
            Assert.That(con.GetAccessor, Is.Not.Null);
            Assert.That(con.SetAccessor, Is.Not.Null);
        }
        public void An_Event_Is_Created()
        {
            const string code = "public event Delegate1 Event1;";

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

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

            Event con = (Event)bc;

            Assert.That(con.Name, Is.EqualTo("Event1"));
            Assert.That(con.Modifiers.Count, Is.EqualTo(1));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
            Assert.That(con.DataType.Name, Is.EqualTo("Delegate1"));
        }
        public void A_Delegate_Is_Created()
        {
            const string code = "public delegate int Delegate1();";

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

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

            Delegate con = (Delegate)bc;

            Assert.That(con.Name, Is.EqualTo("Delegate1"));
            Assert.That(con.Parameters.Count, Is.EqualTo(0));
            Assert.That(con.ReturnType.Name, Is.EqualTo("int"));
            Assert.That(con.Modifiers.Count, Is.EqualTo(1));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
        }
        public void A_Field_Is_Created()
        {
            const string code = "public string Key = \"12345\";";

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

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

            Field con = (Field)bc;

            Assert.That(con.Name, Is.EqualTo("Key"));
            Assert.That(con.Modifiers.Count, Is.EqualTo(1));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
            Assert.That(con.DataType.Name, Is.EqualTo("string"));
            Assert.That(con.InitialValue, Is.EqualTo("\"12345\""));
        }
        public void Parser_Exception_Thrown()
        {
            CSharpParser parser = new CSharpParser();

            try
            {
                const string code = "Nonsense, and more nonsense.";

                parser.ParseSingleConstruct(code, BaseConstructType.ConstructorDeclaration);

                Assert.Fail("Parser exception not thrown");
            }
            catch (ParserException)
            {
                // Everything went according to plan
            }

            Assert.That(parser.ErrorOccurred);
            Assert.That(parser.SyntaxErrors.Count, Is.GreaterThan(0));
        }
        public void A_Property_Is_Created()
        {
            const string code = "public int Property1 { get {return 1; } }";

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

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

            Property con = (Property)bc;

            Assert.That(con.Name, Is.EqualTo("Property1"));
            Assert.That(con.Modifiers.Count, Is.EqualTo(1));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
            Assert.That(con.DataType.Name, Is.EqualTo("int"));
            Assert.That(con.GetAccessor, Is.Not.Null);
            Assert.That(con.SetAccessor, Is.Null);
            Assert.That(con.GetAccessor.BodyText, Is.EqualTo("{ return 1; }"));
        }
        public void A_Method_Is_Created()
        {
            const string code = "public static SomeDataType Method1(int i) { }";

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

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

            Function con = (Function)bc;

            Assert.That(con.Name, Is.EqualTo("Method1"));
            Assert.That(con.Modifiers.Count, Is.EqualTo(2));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
            Assert.That(con.Modifiers[1], Is.EqualTo("static"));
            Assert.That(con.ReturnType.Name, Is.EqualTo("SomeDataType"));
            Assert.That(con.Parameters.Count, Is.EqualTo(1));
            Assert.That(con.Parameters[0].DataType, Is.EqualTo("int"));
            Assert.That(con.Parameters[0].Name, Is.EqualTo("i"));
        }
        public void An_Operator_Is_Created()
        {
            const string code = "public static int operator+(int i) { }";

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

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

            Operator con = (Operator)bc;

            Assert.That(con.Name, Is.EqualTo("+"));
            Assert.That(con.Modifiers.Count, Is.EqualTo(2));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
            Assert.That(con.Modifiers[1], Is.EqualTo("static"));
            Assert.That(con.DataType.Name, Is.EqualTo("int"));
            Assert.That(con.Parameters.Count, Is.EqualTo(1));
            Assert.That(con.Parameters[0].DataType, Is.EqualTo("int"));
            Assert.That(con.Parameters[0].Name, Is.EqualTo("i"));
        }
Exemple #18
0
        public static FunctionInfo CreateFunctionInfoFrom(ApiExtensionMethod extMethod)
        {
            MethodInfo method = extMethod.ExtendedMethod;

            string formattedCode = extMethod.OverridingFunctionBody;

            if (extMethod.HasOverride == false && string.IsNullOrEmpty(extMethod.DefaultCode) == false)
            {
                CSharpParser parser = new CSharpParser();
                parser.FormatSettings.MaintainWhitespace = false;

                string methodText = string.Format("public void {0} () {{ {1} }}", method.Name, extMethod.DefaultCode);

                var bc = parser.ParseSingleConstruct(methodText, BaseConstructType.MethodDeclaration);

                // Remove the start and end braces
                formattedCode = Utility.StandardizeLineBreaks(bc.ToString(), "\n");
                // + 1 to get past the {, +1 to remove the first line break.
                formattedCode = formattedCode.Substring(formattedCode.IndexOf('{') + 2);
                formattedCode = formattedCode.Substring(0, formattedCode.LastIndexOf('}') - 1);

                formattedCode = RemoveTabs(formattedCode);
            }

            FunctionInfo fi = new FunctionInfo(
                method.Name,
                method.ReturnType,
                formattedCode,
                true,
                SyntaxEditorHelper.ScriptLanguageTypes.CSharp,
                extMethod.Description,
                "C#",
                "Extension Methods");

            return(fi);
        }
Exemple #19
0
        public IBaseConstruct CreateBaseConstruct(string code, IBaseConstruct originalConstruct)
        {
            CSharpParser csFormatter = new CSharpParser();

            return(csFormatter.ParseSingleConstruct(code, originalConstruct as BaseConstruct));
        }
Exemple #20
0
        public string GetFormattedDefaultCode()
        {
            bool       isMethod = false;
            MethodInfo method   = null;

            bool   hasApiExt   = false;
            string defaultCode = "";

            foreach (MethodInfo meth in ObjectType.GetMethods())
            {
                if (meth.Name == PropertyName)
                {
                    ParameterInfo[] realParams = meth.GetParameters();

                    if (realParams.Length == ParameterTypes.Count)
                    {
                        bool paramsMatch = true;

                        for (int paramCounter = 0; paramCounter < realParams.Length; paramCounter++)
                        {
                            if (realParams[paramCounter].ParameterType.FullName != ParameterTypes[paramCounter].DataType.FullName)
                            {
                                paramsMatch = false;
                                break;
                            }
                        }
                        if (paramsMatch)
                        {
                            object[] allAttributes = meth.GetCustomAttributes(false);

                            foreach (object att in allAttributes)
                            {
                                Type attType = att.GetType();

                                if (Utility.StringsAreEqual(attType.Name, "ApiExtensionAttribute", true))
                                {
                                    hasApiExt   = true;
                                    method      = meth;
                                    defaultCode = (string)attType.InvokeMember("DefaultCode", BindingFlags.GetProperty, null, att, null);
                                }
                            }
                            break;
                        }
                    }
                }
            }
            Type returnType = null;

            if (method != null)
            {
                isMethod   = true;
                returnType = method.ReturnType;
            }
            else
            {
                PropertyInfo property      = ObjectType.GetProperty(PropertyName);
                object[]     allAttributes = property.GetCustomAttributes(false);

                foreach (object att in allAttributes)
                {
                    Type attType = att.GetType();

                    if (Utility.StringsAreEqual(attType.Name, "ApiExtensionAttribute", true))
                    {
                        hasApiExt   = true;
                        defaultCode = (string)attType.InvokeMember("DefaultCode", BindingFlags.GetProperty, null, att, null);
                        break;
                    }
                }
                returnType = property.PropertyType;
            }
            if (!hasApiExt)// attributes != null && attributes.Length == 0)
            {
                throw new NotImplementedException(String.Format("DefaultCodeAttribute not implemented for {0}.{1} yet.", ObjectType.FullName, PropertyName));
            }
            string       code = defaultCode;// attributes[0].DefaultCode;
            CSharpParser csf  = new CSharpParser();

            if (isMethod)
            {
                Function f = (Function)csf.ParseSingleConstruct("public void Method1(){ " + code + " }", BaseConstructType.MethodDeclaration);
                code = f.BodyText;
            }
            else
            {
                Property p = (Property)csf.ParseSingleConstruct("public int Property1 { " + code + " }", BaseConstructType.PropertyDeclaration);
                if (p.GetAccessor != null)
                {
                    code = p.GetAccessor.BodyText;
                }
                else
                {
                    code = p.SetAccessor.BodyText;
                }
            }
            int secondLineBreak = code.IndexOf("\r\n", code.IndexOf("{")) + 2;
            int lastLineBreak   = code.LastIndexOf("\r\n", code.LastIndexOf("}"));

            if (lastLineBreak > secondLineBreak)
            {
                code = code.Substring(secondLineBreak, lastLineBreak - secondLineBreak);
            }
            else
            {
                code = "";
            }
            string[] lines = Utility.StandardizeLineBreaks(code, Utility.LineBreaks.Unix).Split('\n');

            StringBuilder sb = new StringBuilder(lines.Length * 100);

            for (int i = 0; i < lines.Length; i++)
            {
                if (lines[i].Length > 1)
                {
                    sb.AppendLine(lines[i].Substring(1));
                }
                else
                {
                    sb.AppendLine(lines[i]);
                }
            }
            return(sb.ToString());
        }