/// <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;
        }
Example #2
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;
 }