示例#1
0
        public override void EnterMethod_declaration(CSharpParser.Method_declarationContext context)
        {
            Console.WriteLine("Entering method_declaration context.");

            // Getting the current scope node and current modifiers:
            Symbol.ModifierFlag modFlags = TreatModTokens();
            modifiersTokens.Clear();

            Node        currentScopeNode = ast.GetNode(symbolTable.CurrentScopeNode);
            ClassSymbol ownerSymbol      = (ClassSymbol)(currentScopeNode.Data);

            MethodSymbol methodSymbol = new MethodSymbol(modFlags, ownerSymbol);
            IToken       idToken      = context.method_member_name().identifier(0).Start;

            symbolTable.AddSymbol(idToken, methodSymbol);

            // Creating the method AST node:
            Node methodNode = new Node(idToken, Node.Kind.MethodDeclaration, currentType, methodSymbol);

            ast.AddNode(methodNode);
            int methodIndex = ast.NodeIndex(methodNode);

            currentScopeNode.AddChildIndex(methodIndex);

            // Entering the method scope:
            symbolTable.EnterScope(methodIndex);

            // Creating a node for each parameter:
            CSharpParser.Formal_parameter_listContext parameters = context.formal_parameter_list();
            if (parameters != null)
            {
                CSharpParser.Fixed_parameterContext[] fixedParams = parameters.fixed_parameters().fixed_parameter();
                foreach (CSharpParser.Fixed_parameterContext parameter in fixedParams)
                {
                    CSharpParser.Arg_declarationContext arg = parameter.arg_declaration();
                    if (arg != null)
                    {
                        CSharpParser.Type_Context typeContext = arg.type_();
                        IToken varToken = arg.identifier().Start;
                        Type   t        = TreatTypeContext(typeContext);

                        VariableSymbol varSymbol = new VariableSymbol(Symbol.ModifierFlag.None, methodSymbol);
                        symbolTable.AddSymbol(varToken, varSymbol);

                        Node varNode = new Node(varToken, Node.Kind.MethodParameter, t, null);
                        ast.AddNode(varNode);
                        int varIndex = ast.NodeIndex(varNode);
                        methodNode.AddChildIndex(varIndex);
                    }
                }
            }

            // Creating the subtree for the method implementation:
            CSharpParser.BlockContext blockContext = context.method_body().block();
            if (blockContext != null)
            {
                TreatBlock(blockContext, methodNode);
            }
        }
        public override object VisitMethod_declaration([NotNull] CSharpParser.Method_declarationContext context)
        {
            if (_isControllerClass.Peek())
            {
                bool isNonAction = false;
                bool isPublic    = false;

                var attributes = context.method_header()?.attributes()?.attribute_section();
                if (attributes != null)
                {
                    foreach (var attributeSection in attributes)
                    {
                        foreach (var attribute in attributeSection.attribute_list().attribute())
                        {
                            if (attribute.GetText() == "NonAction")
                            {
                                isNonAction = true;
                            }
                        }
                    }
                }

                var modifiers = context.method_header()?.method_modifier();
                if (modifiers != null)
                {
                    foreach (var modifier in modifiers)
                    {
                        if (modifier.GetText() == Keywords.Public)
                        {
                            isPublic = true;
                        }
                    }
                }

                if (!isNonAction && isPublic)
                {
                    // Add endpoint info to results
                    var  currentNamespace   = GetCurrentNamespace();
                    var  currentClass       = GetCurrentClass();
                    var  controllerRootName = GetControllerRootName(currentClass);
                    var  actionName         = context.method_header().member_name().identifier().GetText();
                    bool?hasId = false;

                    var fixedParams = context.method_header()?.formal_parameter_list()
                                      ?.fixed_parameters()?.fixed_parameter();
                    if (fixedParams != null)
                    {
                        foreach (var fixedParam in fixedParams)
                        {
                            if (Regex.Match(fixedParam?.type_()?.GetText(), @"^int\??$").Success &&
                                fixedParam?.identifier()?.GetText() == "id")
                            {
                                hasId = true;
                            }
                        }
                    }

                    if (ControllerDict.NamespaceDict.ContainsKey(currentNamespace) &&
                        ControllerDict.NamespaceDict[currentNamespace]
                        .ClassDict.ContainsKey(currentClass) &&
                        ControllerDict.NamespaceDict[currentNamespace]
                        .ClassDict[currentClass].ActionDict.ContainsKey(actionName))
                    {
                        if (!Regex.Match(
                                context.method_body().GetText(), @"ViewData\[""BreadcrumbNode""\]\s*=").Success)
                        {
                            var preMethodWhitespace = Tokens.GetHiddenTokensToLeft(
                                context.Start.TokenIndex, Lexer.Hidden);

                            int tabLevels = 1 + ((preMethodWhitespace?.Count ?? 0) > 0
                                ? _stringUtilService.CalculateTabLevels(
                                                     preMethodWhitespace[0]?.Text ?? string.Empty, _tabString)
                                : 0);

                            int openBraceIndex = context.method_body()?.block()?.OPEN_BRACE().Symbol.TokenIndex ?? -1;

                            if (openBraceIndex > -1)
                            {
                                var assignmentString = _breadcrumbCommandParserService.GenerateBreadcrumbAssignment(
                                    controllerRootName, actionName, hasId, tabLevels, _tabString);
                                SetIsClassModifiedToTrue();
                                IsModified = true;
                                Rewriter.InsertAfter(openBraceIndex, assignmentString);
                            }
                        }
                    }
                }
            }

            VisitChildren(context);

            return(null);
        }