예제 #1
0
        /// <summary>
        /// Reads a lambda expression.
        /// </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 LambdaExpression GetLambdaExpression(Reference<ICodePart> parentReference, bool unsafeCode)
        {
            Param.AssertNotNull(parentReference, "parentReference");
            Param.Ignore(unsafeCode);

            // Create an empty lambda expression.
            LambdaExpression lambdaExpression = new LambdaExpression();
            Reference<ICodePart> expressionReference = new Reference<ICodePart>();

            Node<CsToken> previousTokenNode = this.tokens.Last;

            // First symbol could be 'async' for asynchronous anonymous functions.
            Symbol nextSymbol = this.symbols.Peek(1);
            if (nextSymbol.SymbolType == SymbolType.Other && nextSymbol.Text == "async")
            {
                this.tokens.Add(this.GetToken(CsTokenType.Async, SymbolType.Other, expressionReference));
                lambdaExpression.Async = true;
            }

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

            ICollection<Parameter> parameters = null;

            if (symbol.SymbolType == SymbolType.OpenParenthesis)
            {
                parameters = this.ParseAnonymousMethodParameterList(expressionReference, 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, expressionReference);
                this.tokens.Add(token);

                // Add the single parameter.
                lambdaExpression.AddParameter(
                    new Parameter(
                        null, 
                        token.Text, 
                        expressionReference, 
                        ParameterModifiers.None, 
                        null, 
                        token.Location, 
                        new CsTokenList(this.tokens, this.tokens.Last, this.tokens.Last), 
                        token.Generated));
            }

            // Get the lambda operator.
            this.tokens.Add(this.GetOperatorToken(OperatorType.Lambda, expressionReference));

            // 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(expressionReference);

            if (symbol.SymbolType == SymbolType.OpenCurlyBracket)
            {
                lambdaExpression.AnonymousFunctionBody = this.GetNextStatement(expressionReference, unsafeCode);
            }
            else
            {
                lambdaExpression.AnonymousFunctionBody = this.GetNextExpression(ExpressionPrecedence.None, expressionReference, 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, expressionReference, parameter.Generated));
                }
            }

            // Return the expression.
            expressionReference.Target = lambdaExpression;
            return lambdaExpression;
        }
        /// <summary>
        /// The save.
        /// </summary>
        /// <param name="lambdaExpression">
        /// The lambda expression.
        /// </param>
        private void Save(LambdaExpression lambdaExpression)
        {
            this.cppWriter.Write("[=]");
            this.Save(lambdaExpression.Parameters, this.cppWriter, SavingOptions.None /*SavingOptions.ApplyReference*/);

            var methodInvocationExpression = lambdaExpression.AnonymousFunctionBody as MethodInvocationExpression;
            if (methodInvocationExpression != null)
            {
                this.SetMarkBeginOfBlock();
                this.cppWriter.WriteLine();
                this.cppWriter.WriteLine("{");
                this.cppWriter.Indent++;
                this.Save(methodInvocationExpression);
                this.cppWriter.WriteLine(";");
                this.cppWriter.Indent--;
                this.cppWriter.Write("}");

                return;
            }

            this.Save(lambdaExpression.AnonymousFunctionBody, SaveICodeUnit.NoNewLine);
            this.SetMarkBeginOfBlock();
        }