/// <summary> /// Reads a lambda expression. /// </summary> /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param> /// <returns>Returns the expression.</returns> private LambdaExpression GetLambdaExpression(bool unsafeCode) { Param.Ignore(unsafeCode); // Create an empty lambda expression. LambdaExpression lambdaExpression = new LambdaExpression(); // Check whether the next symbol is an opening parenthesis. Symbol symbol = this.GetNextSymbol(); Node<CsToken> previousTokenNode = this.tokens.Last; ICollection<Parameter> parameters = null; if (symbol.SymbolType == SymbolType.OpenParenthesis) { parameters = this.ParseAnonymousMethodParameterList(unsafeCode); } else { // Since the statement did not begin with an opening parenthesis, // it must begin with a single unknown symbol. if (symbol.SymbolType != SymbolType.Other) { throw new SyntaxException(this.document.SourceCode, symbol.LineNumber); } CsToken token = this.GetToken(CsTokenType.Other, SymbolType.Other); this.tokens.Add(token); // Add the single parameter. lambdaExpression.AddParameter(new Parameter( null, token.Text, ParameterModifiers.None, token.Location, new CsTokenList(this.tokens, this.tokens.Last, this.tokens.Last))); } // Get the lambda operator. this.tokens.Add(this.GetOperatorToken(OperatorType.Lambda)); // Get the body of the expression. This can either be an expression or a statement. // If it starts with an opening curly bracket, it's a statement, otherwise it's an expression. symbol = this.GetNextSymbol(); if (symbol.SymbolType == SymbolType.OpenCurlyBracket) { lambdaExpression.AnonymousFunctionBody = this.GetNextStatement(unsafeCode); } else { lambdaExpression.AnonymousFunctionBody = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode); } // Create the overall token list for the expression. Node<CsToken> firstNode = previousTokenNode == null ? this.tokens.First : previousTokenNode.Next; lambdaExpression.Tokens = new CsTokenList(this.tokens, firstNode, this.tokens.Last); // Get the item's argument list if necessary. if (parameters != null && parameters.Count > 0) { lambdaExpression.AddParameters(parameters); } // Add a variable for each of the parameters. if (lambdaExpression.Parameters != null && lambdaExpression.Parameters.Count > 0) { // Add a variable for each of the parameters. foreach (Parameter parameter in lambdaExpression.Parameters) { lambdaExpression.Variables.Add(new Variable( parameter.Type, parameter.Name, VariableModifiers.None, parameter.Location.StartPoint)); } } // Return the expression. return lambdaExpression; }