public static QueryExpressionSyntax WithAllClauses(
            this QueryExpressionSyntax query,
            IList <SyntaxNode> allClauses)
        {
            var fromClause = (FromClauseSyntax)allClauses.First();

            return(query.WithFromClause(fromClause).WithBody(query.Body.WithAllClauses(allClauses.Skip(1))));
        }
Ejemplo n.º 2
0
        public override SyntaxNode VisitQueryExpression(QueryExpressionSyntax node)
        {
            var stripExpressionParentParenthesis = StripExpressionParentParenthesis(node);

            if (stripExpressionParentParenthesis != null &&
                stripExpressionParentParenthesis is QueryExpressionSyntax == false)
            {
                return(node);
            }

            ForEachStatementSyntax parent = null;
            var queryExpressionSyntax     = StripExpressionParenthesis(node.FromClause.Expression) as QueryExpressionSyntax;

            if (queryExpressionSyntax != null &&
                StripExpressionParentParenthesis(queryExpressionSyntax) is QueryExpressionSyntax)
            {
                using (RecursiveCall())
                {
                    parent = VisitQueryExpression(queryExpressionSyntax) as ForEachStatementSyntax;
                }

                if (parent != null)
                {
                    node =
                        node.WithFromClause(
                            node.FromClause.WithExpression(IdentifierName(queryExpressionSyntax.FromClause.Identifier)));
                }
            }

            var dummyYield = YieldStatement(SyntaxKind.YieldReturnStatement, LiteralExpression(SyntaxKind.NullLiteralExpression));

            var body = Block().AddStatements(dummyYield);

            foreach (var clause in node.Body.Clauses)
            {
                if (TryRewriteBodyClause(clause, dummyYield, ref body))
                {
                    continue;
                }

                return(base.VisitQueryExpression(node));
            }

            var selectClauseSyntax = node.Body.SelectOrGroup as SelectClauseSyntax;

            if (selectClauseSyntax == null)
            {
                return(base.VisitQueryExpression(node));
            }

            var continuation = node.Body.Continuation;

            while (continuation != null)
            {
                // select new {  } into

                var selectIntoVar = LocalDeclarationStatement(
                    VariableDeclaration(IdentifierName("var"), SingletonSeparatedList(VariableDeclarator(
                                                                                          continuation.Identifier)
                                                                                      .WithInitializer(
                                                                                          EqualsValueClause(selectClauseSyntax.Expression)
                                                                                          )
                                                                                      )
                                        )
                    );

                selectClauseSyntax = continuation.Body.SelectOrGroup as SelectClauseSyntax;

                if (selectClauseSyntax == null)
                {
                    return(base.VisitQueryExpression(node));
                }

                body = body.InsertNodesBefore(FindDummyYieldIn(body), new[] { selectIntoVar });

                foreach (var clause in continuation.Body.Clauses)
                {
                    if (TryRewriteBodyClause(clause, dummyYield, ref body))
                    {
                        continue;
                    }

                    return(base.VisitQueryExpression(node));
                }

                continuation = continuation.Body.Continuation;
            }

            var stmt = ForEachStatement(
                IdentifierName("var"),
                node.FromClause.Identifier,
                node.FromClause.Expression,
                body
                );

            stmt = stmt.ReplaceNode(FindDummyYieldIn(stmt),
                                    YieldStatement(SyntaxKind.YieldReturnStatement, selectClauseSyntax.Expression)
                                    );

            if (parent == null)
            {
                if (_recursiveCallCounter == 0 && _validator.Validate(stmt.ToFullString(), selectClauseSyntax.Expression, throwOnError: false) == false)
                {
                    ThrowIndexRewritingException(node, stmt);
                }

                return(stmt);
            }

            var parentBody = (BlockSyntax)parent.Statement;

            var yieldStatementSyntax = (YieldStatementSyntax)parentBody.Statements.Last();
            var parentVar            = LocalDeclarationStatement(
                VariableDeclaration(IdentifierName("var"), SingletonSeparatedList(VariableDeclarator(
                                                                                      node.FromClause.Identifier)
                                                                                  .WithInitializer(
                                                                                      EqualsValueClause(yieldStatementSyntax.Expression)
                                                                                      )
                                                                                  )
                                    )
                );

            var statementSyntax = stmt.Statement;

            if (statementSyntax is BlockSyntax bs)
            {
                statementSyntax = bs.Statements.Single();
            }

            return(parent.WithStatement(
                       parentBody.ReplaceNode(yieldStatementSyntax, parentVar).AddStatements(statementSyntax)
                       ));
        }