コード例 #1
0
        public override SyntaxNode VisitForEachStatement(ForEachStatementSyntax node)
        {
            var blockResumeLocation = GetBlockResumeLocation(node.Statement);

            node = (ForEachStatementSyntax)base.VisitForEachStatement(node);

            node = node.WithStatement(InjectedBlock(node.Statement, blockResumeLocation));
            return(node);
        }
コード例 #2
0
        public override SyntaxNode VisitForEachStatement(ForEachStatementSyntax node)
        {
            var body      = node.Statement;
            var bodyBlock = ToBlockSyntax(body);

            if (!bodyBlock.Equals(body))
            {
                node = node.WithStatement(bodyBlock);
            }
            return(node);
        }
コード例 #3
0
        public override SyntaxNode VisitForEachStatement(ForEachStatementSyntax node)
        {
            this.loopLevel++;

            var statement = base.VisitForEachStatement(node
                                                       .WithStatement(GetLoopBlock(node.Statement)))
                            .WithAdditionalAnnotations(this.isLoop);

            this.loopLevel--;

            return(statement);
        }
コード例 #4
0
        // TODO: what other one-line statements might need their curly braces added? for? while?
        public override SyntaxNode VisitForEachStatement(ForEachStatementSyntax node)
        {
            ForEachStatementSyntax newNode = (ForEachStatementSyntax)base.VisitForEachStatement(node);

            if (!(newNode.Statement is BlockSyntax))
            {
                BlockSyntax block = SyntaxFactory.Block(newNode.Statement);
                newNode = node.WithStatement(block);
            }

            return(newNode);
        }
コード例 #5
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)
                       ));
        }
コード例 #6
0
ファイル: IdentifyingVisitor.cs プロジェクト: ermau/Instant
        public override SyntaxNode VisitForEachStatement(ForEachStatementSyntax node)
        {
            this.loopLevel++;

            var statement = base.VisitForEachStatement (node
                                .WithStatement (GetLoopBlock (node.Statement)))
                            .WithAdditionalAnnotations (this.isLoop);

            this.loopLevel--;

            return statement;
        }