/// <summary> /// Reads an anonymous method from the code. /// </summary> /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param> /// <returns>Returns the expression.</returns> private AnonymousMethodExpression GetAnonymousMethodExpression(bool unsafeCode) { Param.Ignore(unsafeCode); // Get the delegate keyword. Node<CsToken> firstTokenNode = this.tokens.InsertLast(this.GetToken(CsTokenType.Delegate, SymbolType.Delegate)); // Check whether the next symbol is an opening parenthesis. Symbol symbol = this.GetNextSymbol(); ICollection<Parameter> parameters = null; if (symbol.SymbolType == SymbolType.OpenParenthesis) { parameters = this.ParseAnonymousMethodParameterList(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); Node<CsToken> openingBracketNode = this.tokens.InsertLast(openingBracket); // Read the child statements. Node<CsToken> closingBracketNode = this.ParseStatementScope(anonymousMethod, 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.StartPoint)); } } // Return the expression. return anonymousMethod; }