Esempio n. 1
0
        /// <summary>
        /// Extracts interface from a concrete C# class post method inspection
        /// </summary>
        /// <param name="context"></param>
        /// <param name="parameters"></param>
        private void ExtractInterfaceMethodSignature(Method_declarationContext context, string parameters)
        {
            if (_classAlreadyImplementsInterface)
            {
                return;
            }

            var compiledMethod = string.Empty;
            var returnType     = string.Empty;
            var accessMod      = string.Empty;

            if (context.parent is Typed_member_declarationContext)
            {
                var parent       = context.parent as Typed_member_declarationContext;
                var accessModPar = parent.Parent.Parent as Class_member_declarationContext;
                accessMod      = accessModPar.Start.Text;
                returnType     = _tokenStream.GetText(parent.Start, parent.Start);
                compiledMethod = GetCompiledMethodNameIfPublic(parameters, returnType, accessMod);
            }
            else if (context.parent is Common_member_declarationContext)
            {
                var parent       = context.parent as Common_member_declarationContext;
                var accessModPar = parent.Parent.Parent as Class_member_declarationsContext;
                accessMod      = accessModPar.Start.Text;
                returnType     = _tokenStream.GetText(parent.Start, parent.Start);
                compiledMethod = GetCompiledMethodNameIfPublic(parameters, returnType, accessMod);
            }

            if (!string.IsNullOrEmpty(compiledMethod))
            {
                //generate interface method only if public method
                _interfaceDefinitionBuilder.AppendLine(compiledMethod);
            }
        }
        public override object VisitMethod_declaration([NotNull] Method_declarationContext context)
        {
            var identifiers = context.method_member_name().identifier();

            base.VisitMethod_declaration(context);
            return(new List <IdentifierContext>(new IdentifierContext[] { identifiers[identifiers.Length - 1] }));
        }
Esempio n. 3
0
        public void Enter_MethodDeclaration(Method_declarationContext context)
        {
            _currentMethodName = context.method_member_name().GetText();

            var ruleAttr = ValidationRuleProvider <MethodNameValidationAttribute>(nameof(Enter_MethodDeclaration)) as MethodNameValidationAttribute;

            if (ruleAttr != null)
            {
                var nameRuleError = ruleAttr.Validate(_currentNamespace, _currentClassName, _currentMethodName, string.Empty, string.Empty);
                if (nameRuleError != null)
                {
                    _nameRuleErrorsList.Add(nameRuleError);
                }
            }


            var maxLenAttr = ValidationRuleProvider <MaxLengthValidationAttribute>(nameof(Enter_MethodDeclaration)) as MaxLengthValidationAttribute;

            if (maxLenAttr != null)
            {
                var nameRuleError = maxLenAttr.Validate(_currentNamespace, _currentClassName, _currentMethodName, string.Empty, string.Empty);
                if (nameRuleError != null)
                {
                    _nameRuleErrorsList.Add(nameRuleError);
                }
            }

            var    paramses   = context.formal_parameter_list();
            string parameters = HandleMethodParameters(paramses);

            ExtractInterfaceMethodSignature(context, parameters);
        }
        private List <IdentifierContext> SetTypeForFunction(Method_declarationContext context, IType type)
        {
            var identifierContext = (IdentifierContext)Visit(context.method_member_name());
            var symbol            = identifierContext.Symbol as FunctionSymbol;

            symbol.SetType(type);
            return(new List <IdentifierContext>(new IdentifierContext[] { identifierContext }));
        }
Esempio n. 5
0
        private void ParseClassDeclaration(Class_definitionContext node)
        {
            var members = node.class_body().class_member_declarations().class_member_declaration();

            foreach (var member in members)
            {
                Common_member_declarationContext child = member.GetRuleContext <Common_member_declarationContext>(0);

                if (child is Common_member_declarationContext)
                {
                    Common_member_declarationContext commonMemberContext = child as Common_member_declarationContext;

                    Event_declarationContext        eventContext      = commonMemberContext.event_declaration();
                    Typed_member_declarationContext typeMemberContext = commonMemberContext.typed_member_declaration();
                    Method_declarationContext       methodContext     = commonMemberContext.method_declaration();

                    string type = "";
                    string name = "";

                    if (eventContext != null)
                    {
                        type = "Event";
                        name = eventContext.variable_declarators().GetText();
                    }

                    if (typeMemberContext != null)
                    {
                        Field_declarationContext    field    = typeMemberContext.field_declaration();
                        Property_declarationContext property = typeMemberContext.property_declaration();

                        if (field != null)
                        {
                            type = "Field";
                            name = field.variable_declarators().GetText();
                        }
                        else if (property != null)
                        {
                            type = "Property";
                            name = property.member_name().GetText();
                        }
                    }
                    else if (methodContext != null)
                    {
                        type = "Method";
                        name = methodContext.method_member_name().GetText();
                    }

                    if (!string.IsNullOrEmpty(name))
                    {
                        this.WriteKeyValue(type, name);
                    }
                }
            }
        }
        public bool HasCheckMethod(Method_declarationContext method_DeclarationContext)
        {
            bool hasIfStatement      = method_DeclarationContext.GetDeepChildContext <IfStatementContext>().Count > 0;
            bool hasForeachStatement = method_DeclarationContext.GetDeepChildContext <ForeachStatementContext>().Count > 0;
            bool hasWhileStatement   = method_DeclarationContext.GetDeepChildContext <WhileStatementContext>().Count > 0;
            bool hasForStatement     = method_DeclarationContext.GetDeepChildContext <ForStatementContext>().Count > 0;

            if (!hasIfStatement && !hasForeachStatement && !hasWhileStatement && !hasForeachStatement)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
        /// <summary>
        /// Define function symbol and scope
        /// </summary>
        /// <param name="context"></param>
        public override void EnterMethod_declaration([NotNull] Method_declarationContext context)
        {
            var identityContextList    = context.method_member_name().identifier();
            var lastestIdentityContext = identityContextList[identityContextList.Length - 1]; //is name of method

            MethodSymbol methodSymbol = new MethodSymbol(lastestIdentityContext.GetText())
            {
                DefNode = lastestIdentityContext
            };

            methodSymbol.SetEnclosingScope(currentScope);

            lastestIdentityContext.Symbol = methodSymbol;
            lastestIdentityContext.Scope  = methodSymbol;

            Define(methodSymbol);
            currentScope = methodSymbol;
        }
        public void CheckLoop(ParserRuleContext context, out ErrorInformation error)
        {
            error = null;
            List <Unary_expressionContext> unaryExpressionContext = ((Method_declarationContext)context).GetDeepChildContext <Unary_expressionContext>();
            Method_declarationContext      methodContext          = ((Method_declarationContext)context);
            string methodName = methodContext.method_member_name().identifier()[0].GetText();

            foreach (Unary_expressionContext contxt in unaryExpressionContext)
            {
                if (IsMethod(contxt) && !HasCheckMethod(methodContext))
                {
                    if (IsSameName(contxt, methodName))
                    {
                        error = new ErrorInformation()
                        {
                            ErrorCode    = "WA0005",
                            ErrorMessage = "UIT: Recursive function should have conditional statement to stop recursive",
                            StartIndex   = contxt.Start.StartIndex,
                            Length       = contxt.Stop.StopIndex - contxt.Start.StartIndex + 1
                        };
                    }
                }
            }
        }
 public override void ExitMethod_declaration([NotNull] Method_declarationContext context)
 {
     currentScope = currentScope.GetEnclosingScope();
 }
Esempio n. 10
0
	public Method_declarationContext method_declaration() {
		Method_declarationContext _localctx = new Method_declarationContext(_ctx, State);
		EnterRule(_localctx, 52, RULE_method_declaration);
		int _la;
		try {
			EnterOuterAlt(_localctx, 1);
			{
			State = 473;
			_la = _input.La(1);
			if (_la==LPAREN) {
				{
				State = 472; method_type();
				}
			}

			State = 475; method_selector();
			State = 476; Match(SEMI);
			}
		}
		catch (RecognitionException re) {
			_localctx.exception = re;
			_errHandler.ReportError(this, re);
			_errHandler.Recover(this, re);
		}
		finally {
			ExitRule();
		}
		return _localctx;
	}