/// <summary>
        /// Reads an anonymous method from the code.
        /// </summary>
        /// <param name="parentReference">The parent code unit.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
        /// <returns>Returns the expression.</returns>
        private AnonymousMethodExpression GetAnonymousMethodExpression(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            var expressionReference = new Reference<ICodePart>();

            // Get the delegate keyword.
            Node<CsToken> firstTokenNode = this.tokens.InsertLast(
                this.GetToken(CsTokenType.Delegate, SymbolType.Delegate, parentReference, expressionReference));

            // Check whether the next symbol is an opening parenthesis.
            Symbol symbol = this.GetNextSymbol(expressionReference);

            ICollection<Parameter> parameters = null;
            if (symbol.SymbolType == SymbolType.OpenParenthesis)
            {
                parameters = this.ParseAnonymousMethodParameterList(expressionReference, unsafeCode);
            }

            // Create the anonymous method object now.
            AnonymousMethodExpression anonymousMethod = new AnonymousMethodExpression();

            // The next symbol must be an opening curly bracket.
            Bracket openingBracket = this.GetBracketToken(CsTokenType.OpenCurlyBracket, SymbolType.OpenCurlyBracket, expressionReference);
            Node<CsToken> openingBracketNode = this.tokens.InsertLast(openingBracket);

            // Read the child statements.
            Node<CsToken> closingBracketNode = this.ParseStatementScope(anonymousMethod, expressionReference, unsafeCode);

            if (closingBracketNode == null)
            {
                // If we failed to get a closing bracket back, then there is a syntax
                // error in the document since there is an opening bracket with no matching
                // closing bracket.
                throw this.CreateSyntaxException();
            }

            openingBracket.MatchingBracketNode = closingBracketNode;
            ((Bracket)closingBracketNode.Value).MatchingBracketNode = openingBracketNode;

            // Create the token list for the anonymous method expression.
            anonymousMethod.Tokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last);

            // Get the item's argument list if necessary.
            if (parameters != null && parameters.Count > 0)
            {
                anonymousMethod.AddParameters(parameters);
            }

            // Add a variable for each of the parameters.
            if (anonymousMethod.Parameters != null && anonymousMethod.Parameters.Count > 0)
            {
                // Add a variable for each of the parameters.
                foreach (Parameter parameter in anonymousMethod.Parameters)
                {
                    anonymousMethod.Variables.Add(new Variable(
                        parameter.Type, parameter.Name, VariableModifiers.None, parameter.Location, expressionReference, parameter.Generated));
                }
            }

            // Return the expression.
            expressionReference.Target = anonymousMethod;
            return anonymousMethod;
        }
 private void CheckAnonymousMethodParenthesis(CsElement element, AnonymousMethodExpression expression)
 {
     if ((expression.Parameters == null) || (expression.Parameters.Count == 0))
     {
         for (Microsoft.StyleCop.Node<CsToken> node = expression.Tokens.First; node != expression.Tokens.Last; node = node.Next)
         {
             if (node.Value.CsTokenType == CsTokenType.OpenCurlyBracket)
             {
                 return;
             }
             if (node.Value.CsTokenType == CsTokenType.OpenParenthesis)
             {
                 base.AddViolation(element, node.Value.LineNumber, Microsoft.StyleCop.CSharp.Rules.RemoveDelegateParenthesisWhenPossible, new object[0]);
                 return;
             }
         }
     }
 }
예제 #3
0
 private AnonymousMethodExpression GetAnonymousMethodExpression(bool unsafeCode)
 {
     Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Delegate, SymbolType.Delegate));
     Symbol nextSymbol = this.GetNextSymbol();
     ICollection<Parameter> items = null;
     if (nextSymbol.SymbolType == SymbolType.OpenParenthesis)
     {
         items = this.ParseAnonymousMethodParameterList(unsafeCode);
     }
     AnonymousMethodExpression parent = new AnonymousMethodExpression();
     Bracket bracketToken = this.GetBracketToken(CsTokenType.OpenCurlyBracket, SymbolType.OpenCurlyBracket);
     Microsoft.StyleCop.Node<CsToken> node2 = this.tokens.InsertLast(bracketToken);
     Microsoft.StyleCop.Node<CsToken> node3 = this.ParseStatementScope(parent, unsafeCode);
     if (node3 == null)
     {
         throw this.CreateSyntaxException();
     }
     bracketToken.MatchingBracketNode = node3;
     ((Bracket) node3.Value).MatchingBracketNode = node2;
     parent.Tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last);
     if ((items != null) && (items.Count > 0))
     {
         parent.AddParameters(items);
     }
     if ((parent.Parameters != null) && (parent.Parameters.Count > 0))
     {
         foreach (Parameter parameter in parent.Parameters)
         {
             parent.Variables.Add(new Variable(parameter.Type, parameter.Name, VariableModifiers.None, parameter.Location.StartPoint, parameter.Generated));
         }
     }
     return parent;
 }
        /// <summary>
        /// Checks that parenthesis are used correctly within an anonymous method.
        /// </summary>
        /// <param name="element">The parent element.</param>
        /// <param name="expression">The expression to check.</param>
        private void CheckAnonymousMethodParenthesis(CsElement element, AnonymousMethodExpression expression)
        {
            Param.AssertNotNull(element, "element");
            Param.AssertNotNull(expression, "expression");

            if (expression.Parameters == null || expression.Parameters.Count == 0)
            {
                // Check for parenthesis.
                for (Node<CsToken> tokenNode = expression.Tokens.First; tokenNode != expression.Tokens.Last; tokenNode = tokenNode.Next)
                {
                    if (tokenNode.Value.CsTokenType == CsTokenType.OpenCurlyBracket)
                    {
                        break;
                    }
                    else if (tokenNode.Value.CsTokenType == CsTokenType.OpenParenthesis)
                    {
                        this.AddViolation(element, tokenNode.Value.LineNumber, Rules.RemoveDelegateParenthesisWhenPossible);
                        break;
                    }
                }
            }
        }