Exemple #1
0
 private static bool TryGetBinaryExpression(CSS.IfStatementSyntax node, out CSS.BinaryExpressionSyntax binaryExpressionSyntax, CS.SyntaxKind notEqualsExpression, CS.SyntaxKind operand)
 {
     binaryExpressionSyntax = TrimParenthesis(node) as CSS.BinaryExpressionSyntax;
     return(binaryExpressionSyntax != null &&
            binaryExpressionSyntax.IsKind(notEqualsExpression) &&
            (binaryExpressionSyntax.Left.IsKind(operand) ||
             binaryExpressionSyntax.Right.IsKind(operand)));
 }
Exemple #2
0
        private static CSS.ExpressionSyntax TrimParenthesis(CSS.IfStatementSyntax node)
        {
            var condition = node.Condition;

            while (condition is CSS.ParenthesizedExpressionSyntax pExp)
            {
                condition = pExp.Expression;
            }
            return(condition);
        }
Exemple #3
0
        bool TryConvertRaiseEvent(CSS.IfStatementSyntax node, out IdentifierNameSyntax name, List <ArgumentSyntax> arguments)
        {
            name = null;
            var condition = node.Condition;

            while (condition is CSS.ParenthesizedExpressionSyntax)
            {
                condition = ((CSS.ParenthesizedExpressionSyntax)condition).Expression;
            }
            if (!(condition is CSS.BinaryExpressionSyntax))
            {
                return(false);
            }
            var be = (CSS.BinaryExpressionSyntax)condition;

            if (!be.IsKind(CS.SyntaxKind.NotEqualsExpression) || (!be.Left.IsKind(CS.SyntaxKind.NullLiteralExpression) && !be.Right.IsKind(CS.SyntaxKind.NullLiteralExpression)))
            {
                return(false);
            }
            CSS.ExpressionStatementSyntax singleStatement;
            if (node.Statement is CSS.BlockSyntax)
            {
                var block = ((CSS.BlockSyntax)node.Statement);
                if (block.Statements.Count != 1)
                {
                    return(false);
                }
                singleStatement = block.Statements[0] as CSS.ExpressionStatementSyntax;
            }
            else
            {
                singleStatement = node.Statement as CSS.ExpressionStatementSyntax;
            }
            if (singleStatement == null || !(singleStatement.Expression is CSS.InvocationExpressionSyntax))
            {
                return(false);
            }
            var possibleEventName = GetPossibleEventName(be.Left) ?? GetPossibleEventName(be.Right);

            if (possibleEventName == null)
            {
                return(false);
            }
            var invocation     = (CSS.InvocationExpressionSyntax)singleStatement.Expression;
            var invocationName = GetPossibleEventName(invocation.Expression);

            if (possibleEventName != invocationName)
            {
                return(false);
            }
            name = SyntaxFactory.IdentifierName(possibleEventName);
            arguments.AddRange(invocation.ArgumentList.Arguments.Select(a => (ArgumentSyntax)a.Accept(_nodesVisitor)));
            return(true);
        }
Exemple #4
0
        public override SyntaxList <StatementSyntax> VisitIfStatement(CSS.IfStatementSyntax node)
        {
            IdentifierNameSyntax  name;
            List <ArgumentSyntax> arguments = new List <ArgumentSyntax>();
            StatementSyntax       stmt;

            if (node.Else == null && TryConvertRaiseEvent(node, out name, arguments))
            {
                stmt = SyntaxFactory.RaiseEventStatement(name, SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(arguments)));
                return(SyntaxFactory.SingletonList(stmt));
            }
            var             elseIfBlocks = new List <ElseIfBlockSyntax>();
            ElseBlockSyntax elseBlock    = null;

            CollectElseBlocks(node, elseIfBlocks, ref elseBlock);
            if (node.Statement is CSS.BlockSyntax)
            {
                stmt = SyntaxFactory.MultiLineIfBlock(
                    SyntaxFactory.IfStatement((ExpressionSyntax)node.Condition.Accept(_nodesVisitor)).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword)),
                    ConvertBlock(node.Statement),
                    SyntaxFactory.List(elseIfBlocks),
                    elseBlock
                    );
            }
            else
            {
                if (elseIfBlocks.Any() || elseBlock != null || !IsSimpleStatement(node.Statement))
                {
                    stmt = SyntaxFactory.MultiLineIfBlock(
                        SyntaxFactory.IfStatement((ExpressionSyntax)node.Condition.Accept(_nodesVisitor)).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword)),
                        ConvertBlock(node.Statement),
                        SyntaxFactory.List(elseIfBlocks),
                        elseBlock
                        );
                }
                else
                {
                    stmt = SyntaxFactory.SingleLineIfStatement(
                        (ExpressionSyntax)node.Condition.Accept(_nodesVisitor),
                        ConvertBlock(node.Statement),
                        null
                        ).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword));
                }
            }
            return(SyntaxFactory.SingletonList(stmt));
        }
Exemple #5
0
 void CollectElseBlocks(CSS.IfStatementSyntax node, List <ElseIfBlockSyntax> elseIfBlocks, ref ElseBlockSyntax elseBlock)
 {
     if (node.Else == null)
     {
         return;
     }
     if (node.Else.Statement is CSS.IfStatementSyntax)
     {
         var elseIf = (CSS.IfStatementSyntax)node.Else.Statement;
         if (elseIf.Statement is CSS.BlockSyntax)
         {
             var block = (CSS.BlockSyntax)elseIf.Statement;
             elseIfBlocks.Add(
                 SyntaxFactory.ElseIfBlock(
                     SyntaxFactory.ElseIfStatement((ExpressionSyntax)elseIf.Condition.Accept(nodesVisitor)).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword)),
                     SyntaxFactory.List(block.Statements.SelectMany(s => s.Accept(this)))
                     )
                 );
         }
         else
         {
             elseIfBlocks.Add(
                 SyntaxFactory.ElseIfBlock(
                     SyntaxFactory.ElseIfStatement((ExpressionSyntax)elseIf.Condition.Accept(nodesVisitor)).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword)),
                     elseIf.Statement.Accept(this)
                     )
                 );
         }
         CollectElseBlocks(elseIf, elseIfBlocks, ref elseBlock);
     }
     else if (node.Else.Statement is CSS.BlockSyntax)
     {
         var block = (CSS.BlockSyntax)node.Else.Statement;
         elseBlock = SyntaxFactory.ElseBlock(SyntaxFactory.List(block.Statements.SelectMany(s => s.Accept(this))));
     }
     else
     {
         elseBlock = SyntaxFactory.ElseBlock(SyntaxFactory.List(node.Else.Statement.Accept(this)));
     }
 }
Exemple #6
0
 void CollectElseBlocks(CSS.IfStatementSyntax node, List <ElseIfBlockSyntax> elseIfBlocks, ref ElseBlockSyntax elseBlock)
 {
     if (node.Else == null)
     {
         return;
     }
     if (node.Else.Statement is CSS.IfStatementSyntax)
     {
         var elseIf = (CSS.IfStatementSyntax)node.Else.Statement;
         elseIfBlocks.Add(
             SyntaxFactory.ElseIfBlock(
                 SyntaxFactory.ElseIfStatement((ExpressionSyntax)elseIf.Condition.Accept(_nodesVisitor)).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword)),
                 ConvertBlock(elseIf.Statement)
                 )
             );
         CollectElseBlocks(elseIf, elseIfBlocks, ref elseBlock);
     }
     else
     {
         elseBlock = SyntaxFactory.ElseBlock(ConvertBlock(node.Else.Statement));
     }
 }
Exemple #7
0
            public override SyntaxList <StatementSyntax> VisitIfStatement(CSS.IfStatementSyntax node)
            {
                StatementSyntax stmt;
                var             elseIfBlocks = new List <ElseIfBlockSyntax>();
                ElseBlockSyntax elseBlock    = null;

                CollectElseBlocks(node, elseIfBlocks, ref elseBlock);
                if (node.Statement is CSS.BlockSyntax)
                {
                    var b = (CSS.BlockSyntax)node.Statement;
                    stmt = SyntaxFactory.MultiLineIfBlock(
                        SyntaxFactory.IfStatement((ExpressionSyntax)node.Condition.Accept(nodesVisitor)).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword)),
                        SyntaxFactory.List(b.Statements.SelectMany(s => s.Accept(this))),
                        SyntaxFactory.List(elseIfBlocks),
                        elseBlock
                        );
                }
                else
                {
                    if (elseIfBlocks.Any())
                    {
                        stmt = SyntaxFactory.MultiLineIfBlock(
                            SyntaxFactory.IfStatement((ExpressionSyntax)node.Condition.Accept(nodesVisitor)).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword)),
                            node.Statement.Accept(this),
                            SyntaxFactory.List(elseIfBlocks),
                            elseBlock
                            );
                    }
                    else
                    {
                        stmt = SyntaxFactory.SingleLineIfStatement(
                            (ExpressionSyntax)node.Condition.Accept(nodesVisitor),
                            node.Statement.Accept(this),
                            SyntaxFactory.SingleLineElseClause(elseBlock.Statements)
                            ).WithThenKeyword(SyntaxFactory.Token(SyntaxKind.ThenKeyword));
                    }
                }
                return(SyntaxFactory.SingletonList <StatementSyntax>(stmt));
            }
            public override SyntaxList <VB.Syntax.StatementSyntax> VisitIfStatement(CS.Syntax.IfStatementSyntax node)
            {
                VB.Syntax.IfStatementSyntax ifStatement = VB.SyntaxFactory.IfStatement(
                    VB.SyntaxFactory.Token(VB.SyntaxKind.IfKeyword),
                    nodeVisitor.VisitExpression(node.Condition),
                    VB.SyntaxFactory.Token(VB.SyntaxKind.ThenKeyword));

                List <VB.Syntax.ElseIfBlockSyntax> elseIfBlocks = new List <VB.Syntax.ElseIfBlockSyntax>();
                ElseClauseSyntax currentElseClause = node.Else;

                while (currentElseClause != null)
                {
                    if (!currentElseClause.Statement.IsKind(CS.SyntaxKind.IfStatement))
                    {
                        break;
                    }

                    IfStatementSyntax nestedIf = (CS.Syntax.IfStatementSyntax)currentElseClause.Statement;
                    currentElseClause = nestedIf.Else;

                    VB.Syntax.ElseIfStatementSyntax elseIfStatement = VB.SyntaxFactory.ElseIfStatement(
                        VB.SyntaxFactory.Token(VB.SyntaxKind.ElseIfKeyword),
                        nodeVisitor.VisitExpression(nestedIf.Condition),
                        VB.SyntaxFactory.Token(VB.SyntaxKind.ThenKeyword));
                    VB.Syntax.ElseIfBlockSyntax elseIfBlock = VB.SyntaxFactory.ElseIfBlock(
                        elseIfStatement,
                        Visit(nestedIf.Statement));
                    elseIfBlocks.Add(elseIfBlock);
                }

                return(List <VB.Syntax.StatementSyntax>(
                           VB.SyntaxFactory.MultiLineIfBlock(
                               ifStatement,
                               Visit(node.Statement),
                               List <VB.Syntax.ElseIfBlockSyntax>(elseIfBlocks),
                               currentElseClause == null ? null : nodeVisitor.Visit <VB.Syntax.ElseBlockSyntax>(currentElseClause))));
            }
Exemple #9
0
 bool TryConvertIfNotNullRaiseEvent(CSS.IfStatementSyntax node, out StatementSyntax raiseEventStatement)
 {
     raiseEventStatement = null;
     return(TryGetBinaryExpression(node, out var comparisonExpression, CS.SyntaxKind.NotEqualsExpression, CS.SyntaxKind.NullLiteralExpression) &&
            TryConvertRaiseEvent(node.Statement, comparisonExpression, ref raiseEventStatement));
 }
Exemple #10
0
        internal static SyntaxToken GetFirstExcludedToken(StatementSyntax statement)
        {
            Debug.Assert(statement != null);
            switch (statement.Kind())
            {
            case SyntaxKind.Block:
                return(((BlockSyntax)statement).CloseBraceToken);

            case SyntaxKind.BreakStatement:
                return(((BreakStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.CheckedStatement:
            case SyntaxKind.UncheckedStatement:
                return(((CheckedStatementSyntax)statement).Block.CloseBraceToken);

            case SyntaxKind.ContinueStatement:
                return(((ContinueStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.LocalDeclarationStatement:
                return(((LocalDeclarationStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.DoStatement:
                return(((DoStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.EmptyStatement:
                return(((EmptyStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.ExpressionStatement:
                return(((ExpressionStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.FixedStatement:
                return(GetFirstExcludedToken(((FixedStatementSyntax)statement).Statement));

            case SyntaxKind.ForEachStatement:
            case SyntaxKind.ForEachVariableStatement:
                return(GetFirstExcludedToken(((CommonForEachStatementSyntax)statement).Statement));

            case SyntaxKind.ForStatement:
                return(GetFirstExcludedToken(((ForStatementSyntax)statement).Statement));

            case SyntaxKind.GotoDefaultStatement:
            case SyntaxKind.GotoCaseStatement:
            case SyntaxKind.GotoStatement:
                return(((GotoStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.IfStatement:
                IfStatementSyntax ifStmt  = (IfStatementSyntax)statement;
                ElseClauseSyntax  elseOpt = ifStmt.Else;
                return(GetFirstExcludedToken(elseOpt == null ? ifStmt.Statement : elseOpt.Statement));

            case SyntaxKind.LabeledStatement:
                return(GetFirstExcludedToken(((LabeledStatementSyntax)statement).Statement));

            case SyntaxKind.LockStatement:
                return(GetFirstExcludedToken(((LockStatementSyntax)statement).Statement));

            case SyntaxKind.ReturnStatement:
                return(((ReturnStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.SwitchStatement:
                return(((SwitchStatementSyntax)statement).CloseBraceToken);

            case SyntaxKind.ThrowStatement:
                return(((ThrowStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.TryStatement:
                TryStatementSyntax tryStmt = (TryStatementSyntax)statement;

                FinallyClauseSyntax finallyClause = tryStmt.Finally;
                if (finallyClause != null)
                {
                    return(finallyClause.Block.CloseBraceToken);
                }

                CatchClauseSyntax lastCatch = tryStmt.Catches.LastOrDefault();
                if (lastCatch != null)
                {
                    return(lastCatch.Block.CloseBraceToken);
                }
                return(tryStmt.Block.CloseBraceToken);

            case SyntaxKind.UnsafeStatement:
                return(((UnsafeStatementSyntax)statement).Block.CloseBraceToken);

            case SyntaxKind.UsingStatement:
                return(GetFirstExcludedToken(((UsingStatementSyntax)statement).Statement));

            case SyntaxKind.WhileStatement:
                return(GetFirstExcludedToken(((WhileStatementSyntax)statement).Statement));

            case SyntaxKind.YieldReturnStatement:
            case SyntaxKind.YieldBreakStatement:
                return(((YieldStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.LocalFunctionStatement:
                LocalFunctionStatementSyntax localFunctionStmt = (LocalFunctionStatementSyntax)statement;
                if (localFunctionStmt.Body != null)
                {
                    return(GetFirstExcludedToken(localFunctionStmt.Body));
                }
                if (localFunctionStmt.SemicolonToken != default(SyntaxToken))
                {
                    return(localFunctionStmt.SemicolonToken);
                }
                return(localFunctionStmt.ParameterList.GetLastToken());

            default:
                throw ExceptionUtilities.UnexpectedValue(statement.Kind());
            }
        }