コード例 #1
0
        public override object VisitClass_declaration([NotNull] CSharpParser.Class_declarationContext context)
        {
            bool matchNames        = context.identifier().GetText() == ServiceClassName;
            bool isTypeParamsMatch = true;
            var  typeParams        = context?.type_parameter_list()?.type_parameter();

            if (!(typeParams is null && (TypeParameters is null || TypeParameters.Count == 0)))
            {
                if ((typeParams?.Length ?? 0) != (TypeParameters?.Count ?? 0))
                {
                    isTypeParamsMatch = false;
                }
                else
                {
                    for (int i = 0; i < typeParams.Length; ++i)
                    {
                        if (typeParams[i].identifier().GetText() != TypeParameters[i].TypeParam)
                        {
                            isTypeParamsMatch = false;
                            break;
                        }
                    }
                }
            }

            if (matchNames && isTypeParamsMatch)
            {
                HasServiceClass          = true;
                Results.ServiceNamespace = string.Join(".", _currentNamespace.ToArray().Reverse());
                var classDeclaration = new ClassInterfaceDeclaration()
                {
                    IsInterface = false,
                    Attributes  = _cSharpParserService.GetTextWithWhitespace(Tokens, context?.attributes()),
                    Identifier  = ServiceClassName
                };

                //Results.ClassInterfaceDeclarations.Add(className, classDeclaration);
                Results.ServiceDeclaration = classDeclaration;

                if (context?.PARTIAL()?.GetText() != null)
                {
                    classDeclaration.Modifiers.Add(context.PARTIAL().GetText());
                }

                if (context?.class_modifier() != null)
                {
                    foreach (var modifier in context.class_modifier())
                    {
                        classDeclaration.Modifiers.Add(modifier.GetText());
                    }
                }

                var typeParameters     = context?.type_parameter_list()?.type_parameter();
                var constraintsClauses =
                    context?.type_parameter_constraints_clauses()?.type_parameter_constraints_clause();

                classDeclaration.TypeParameters =
                    _cSharpParserService.ParseTypeParameterList(Tokens, typeParameters, constraintsClauses);

                var baseClassType = _cSharpParserService.GetTextWithWhitespaceMinifiedLite(
                    Tokens, context?.class_base()?.class_type());
                var interfaceTypes = context?.class_base()?.interface_type_list()?.interface_type();
                if (interfaceTypes != null || baseClassType != null)
                {
                    classDeclaration.Base = new ClassInterfaceBase();
                    if (baseClassType != null)
                    {
                        classDeclaration.Base.ClassType = baseClassType;
                    }

                    if (interfaceTypes != null)
                    {
                        foreach (var interfaceType in interfaceTypes)
                        {
                            classDeclaration.Base.InterfaceTypeList.Add(
                                _cSharpParserService.GetTextWithWhitespaceMinifiedLite(Tokens, interfaceType));
                        }
                    }
                }

                var memberDeclarations = context?.class_body()?.class_member_declarations()?.class_member_declaration();
                if (memberDeclarations != null)
                {
                    foreach (var memberDeclaration in memberDeclarations)
                    {
                        bool isPublic = false;

                        var classMethodDeclaration   = memberDeclaration?.method_declaration();
                        var classPropertyDeclaration = memberDeclaration?.property_declaration();
                        if (classMethodDeclaration != null)
                        {
                            var methodDeclaration = new MethodDeclaration()
                            {
                                Attributes = _cSharpParserService.GetTextWithWhitespace(
                                    Tokens, classMethodDeclaration?.method_header()?.attributes()),
                                ReturnType = _cSharpParserService.GetTextWithWhitespaceMinifiedLite(
                                    Tokens, classMethodDeclaration.method_header().return_type()),
                                Identifier = classMethodDeclaration.method_header().member_name().GetText(),
                                Body       = _cSharpParserService.GetTextWithWhitespaceUntab(
                                    Tokens, classMethodDeclaration.method_body())
                            };

                            var methodModifiers = classMethodDeclaration?.method_header()?.method_modifier();
                            if (methodModifiers != null)
                            {
                                foreach (var methodMod in methodModifiers)
                                {
                                    if (methodMod.GetText() == Keywords.Public)
                                    {
                                        isPublic = true;
                                    }
                                    methodDeclaration.Modifiers.Add(methodMod.GetText());
                                }
                            }

                            if (!isPublic)
                            {
                                continue;
                            }

                            var formalParameterList = classMethodDeclaration?.method_header()?.formal_parameter_list();
                            methodDeclaration.FormalParameterList =
                                _cSharpParserService.ParseFormalParameterList(Tokens, formalParameterList);

                            var methodTypeParameters =
                                classMethodDeclaration?.method_header()?.type_parameter_list()?.type_parameter();
                            var methodConstraintsClauses = classMethodDeclaration
                                                           ?.method_header()
                                                           ?.type_parameter_constraints_clauses()
                                                           ?.type_parameter_constraints_clause();

                            methodDeclaration.TypeParameters = _cSharpParserService
                                                               .ParseTypeParameterList(Tokens, methodTypeParameters, methodConstraintsClauses);

                            classDeclaration.Body.MethodDeclarations.Add(methodDeclaration);
                        }
                        else if (classPropertyDeclaration != null)
                        {
                            var propertyDeclaration = new PropertyDeclaration()
                            {
                                Attributes = _cSharpParserService.GetTextWithWhitespace(
                                    Tokens, classPropertyDeclaration?.attributes()),
                                Type = _cSharpParserService.GetTextWithWhitespaceMinifiedLite(
                                    Tokens, classPropertyDeclaration.type_()),
                                Identifier = classPropertyDeclaration.member_name().GetText(),
                                Body       = new PropertyBody()
                                {
                                    Text = _cSharpParserService.GetTextWithWhitespaceUntab(
                                        Tokens, classPropertyDeclaration.property_body()),
                                    HasGetAccessor = classPropertyDeclaration.property_body().expression() != null
                                                     | classPropertyDeclaration.property_body()
                                                     ?.accessor_declarations()
                                                     ?.get_accessor_declaration() != null,
                                    HasSetAccessor = classPropertyDeclaration.property_body()
                                                     ?.accessor_declarations()
                                                     ?.set_accessor_declaration() != null
                                }
                            };

                            var propertyModifiers = classPropertyDeclaration?.property_modifier();
                            if (propertyModifiers != null)
                            {
                                foreach (var propModifier in propertyModifiers)
                                {
                                    if (propModifier.GetText() == Keywords.Public)
                                    {
                                        isPublic = true;
                                    }
                                    propertyDeclaration.Modifiers.Add(propModifier.GetText());
                                }
                            }

                            if (!isPublic)
                            {
                                continue;
                            }

                            classDeclaration.Body.PropertyDeclarations.Add(propertyDeclaration);
                        }
                    }
                }
            }
            VisitChildren(context);
            return(null);
        }
コード例 #2
0
        public override object VisitInterface_declaration([NotNull] CSharpParser.Interface_declarationContext context)
        {
            bool matchNames            = context.identifier().GetText() == ServiceInterfaceName;
            bool isTypeParamsMatch     = true;
            var  variantTypeParameters = context?.variant_type_parameter_list()?.variant_type_parameter();

            if (!(variantTypeParameters is null && (TypeParameters is null || TypeParameters.Count == 0)))
            {
                if ((variantTypeParameters?.Length ?? 0) != (TypeParameters?.Count ?? 0))
                {
                    isTypeParamsMatch = false;
                }
                else
                {
                    for (int i = 0; i < variantTypeParameters.Length; ++i)
                    {
                        if (variantTypeParameters[i].identifier().GetText() != TypeParameters[i].TypeParam)
                        {
                            isTypeParamsMatch = false;
                            break;
                        }
                    }
                }
            }

            if (matchNames && isTypeParamsMatch)
            {
                HasServiceInterface = true;

                Results.ServiceNamespace = string.Join(".", _currentNamespace.ToArray().Reverse());
                var interfaceDeclaration = new ClassInterfaceDeclaration()
                {
                    IsInterface = true,
                    Attributes  = _cSharpParserService.GetTextWithWhitespace(Tokens, context?.attributes()),
                    Identifier  = ServiceInterfaceName
                };

                //Results.ClassInterfaceDeclarations.Add(interfaceName, interfaceDeclaration);
                Results.ServiceDeclaration = interfaceDeclaration;

                if (context?.PARTIAL()?.GetText() != null)
                {
                    interfaceDeclaration.Modifiers.Add(context.PARTIAL().GetText());
                }

                if (context?.interface_modifier() != null)
                {
                    foreach (var modifier in context.interface_modifier())
                    {
                        interfaceDeclaration.Modifiers.Add(modifier.GetText());
                    }
                }

                var constraintsClauses =
                    context?.type_parameter_constraints_clauses()?.type_parameter_constraints_clause();

                interfaceDeclaration.TypeParameters =
                    _cSharpParserService.ParseVariantTypeParameterList(
                        Tokens, variantTypeParameters, constraintsClauses);

                var interfaceTypes = context?.interface_base()?.interface_type_list()?.interface_type();
                if (interfaceTypes != null)
                {
                    interfaceDeclaration.Base = new ClassInterfaceBase();
                    foreach (var interfaceType in interfaceTypes)
                    {
                        interfaceDeclaration.Base.InterfaceTypeList.Add(
                            _cSharpParserService.GetTextWithWhitespaceMinifiedLite(Tokens, interfaceType));
                    }
                }

                var memberDeclarations = context?.interface_body()?.interface_member_declaration();
                if (memberDeclarations != null)
                {
                    foreach (var memberDeclaration in memberDeclarations)
                    {
                        var interfaceMethodDeclaration   = memberDeclaration?.interface_method_declaration();
                        var interfacePropertyDeclaration = memberDeclaration?.interface_property_declaration();
                        if (interfaceMethodDeclaration != null)
                        {
                            var methodDeclaration = new MethodDeclaration()
                            {
                                Attributes = _cSharpParserService.GetTextWithWhitespace(
                                    Tokens, interfaceMethodDeclaration?.attributes()),
                                ReturnType = _cSharpParserService.GetTextWithWhitespaceMinifiedLite(
                                    Tokens, interfaceMethodDeclaration.return_type()),
                                Identifier = interfaceMethodDeclaration.identifier().GetText()
                            };

                            if (interfaceMethodDeclaration?.NEW()?.GetText() != null)
                            {
                                methodDeclaration.Modifiers.Add(interfaceMethodDeclaration.NEW().GetText());
                            }

                            var formalParameterList = interfaceMethodDeclaration?.formal_parameter_list();
                            methodDeclaration.FormalParameterList =
                                _cSharpParserService.ParseFormalParameterList(Tokens, formalParameterList);

                            var methodTypeParameters =
                                interfaceMethodDeclaration?.type_parameter_list()?.type_parameter();
                            var methodConstraintsClauses = interfaceMethodDeclaration
                                                           ?.type_parameter_constraints_clauses()
                                                           ?.type_parameter_constraints_clause();

                            methodDeclaration.TypeParameters = _cSharpParserService
                                                               .ParseTypeParameterList(Tokens, methodTypeParameters, methodConstraintsClauses);

                            interfaceDeclaration.Body.MethodDeclarations.Add(methodDeclaration);
                        }
                        else if (interfacePropertyDeclaration != null)
                        {
                            var propertyDeclaration = new PropertyDeclaration()
                            {
                                Attributes = _cSharpParserService.GetTextWithWhitespace(
                                    Tokens, interfacePropertyDeclaration?.attributes()),
                                Type = _cSharpParserService.GetTextWithWhitespaceMinifiedLite(
                                    Tokens, interfacePropertyDeclaration.type_()),
                                Identifier = interfacePropertyDeclaration.identifier().GetText(),
                                Body       = new PropertyBody()
                                {
                                    Text = _cSharpParserService.GetTextWithWhitespaceUntab(
                                        Tokens, interfacePropertyDeclaration.interface_accessors()),
                                    HasGetAccessor = interfacePropertyDeclaration.interface_accessors()
                                                     .interface_get_accessor() != null,
                                    HasSetAccessor = interfacePropertyDeclaration.interface_accessors()
                                                     .interface_set_accessor() != null
                                }
                            };

                            if (interfacePropertyDeclaration?.NEW() != null)
                            {
                                propertyDeclaration.Modifiers.Add(interfacePropertyDeclaration?.NEW()?.GetText());
                            }

                            interfaceDeclaration.Body.PropertyDeclarations.Add(propertyDeclaration);
                        }
                    }
                }
            }
            VisitChildren(context);
            return(null);
        }