Beispiel #1
0
        CatchBlockSyntax ConvertCatchClause(int index, CSS.CatchClauseSyntax catchClause)
        {
            var statements = ConvertBlock(catchClause.Block);

            if (catchClause.Declaration == null)
            {
                return(SyntaxFactory.CatchBlock(SyntaxFactory.CatchStatement(), statements));
            }
            var    type = (TypeSyntax)catchClause.Declaration.Type.Accept(_nodesVisitor);
            string simpleTypeName;

            if (type is QualifiedNameSyntax)
            {
                simpleTypeName = ((QualifiedNameSyntax)type).Right.ToString();
            }
            else
            {
                simpleTypeName = type.ToString();
            }
            return(SyntaxFactory.CatchBlock(
                       SyntaxFactory.CatchStatement(
                           SyntaxFactory.IdentifierName(SyntaxTokenExtensions.IsKind(catchClause.Declaration.Identifier, CS.SyntaxKind.None) ? SyntaxFactory.Identifier($"__unused{simpleTypeName}{index + 1}__") : CommonConversions.ConvertIdentifier(catchClause.Declaration.Identifier)),
                           SyntaxFactory.SimpleAsClause(type),
                           catchClause.Filter == null ? null : SyntaxFactory.CatchFilterClause((ExpressionSyntax)catchClause.Filter.FilterExpression.Accept(_nodesVisitor))
                           ), statements
                       ));
        }
Beispiel #2
0
 public override SyntaxList <StatementSyntax> VisitLabeledStatement(CSS.LabeledStatementSyntax node)
 {
     return(SyntaxFactory.SingletonList <StatementSyntax>(SyntaxFactory.LabelStatement(CommonConversions.ConvertIdentifier(node.Identifier)))
            .AddRange(ConvertBlock(node.Statement)));
 }
Beispiel #3
0
        bool ConvertForToSimpleForNext(CSS.ForStatementSyntax node, out StatementSyntax block)
        {
            //   ForStatement -> ForNextStatement when for-loop is simple

            // only the following forms of the for-statement are allowed:
            // for (TypeReference name = start; name < oneAfterEnd; name += step)
            // for (name = start; name < oneAfterEnd; name += step)
            // for (TypeReference name = start; name <= end; name += step)
            // for (name = start; name <= end; name += step)
            // for (TypeReference name = start; name > oneAfterEnd; name -= step)
            // for (name = start; name > oneAfterEnd; name -= step)
            // for (TypeReference name = start; name >= end; name -= step)
            // for (name = start; name >= end; name -= step)

            block = null;

            // check if the form is valid and collect TypeReference, name, start, end and step
            bool hasVariable = node.Declaration != null && node.Declaration.Variables.Count == 1;

            if (!hasVariable && node.Initializers.Count != 1)
            {
                return(false);
            }
            if (node.Incrementors.Count != 1)
            {
                return(false);
            }
            var iterator = node.Incrementors.FirstOrDefault()?.Accept(_nodesVisitor) as AssignmentStatementSyntax;

            if (iterator == null || !iterator.IsKind(SyntaxKind.AddAssignmentStatement, SyntaxKind.SubtractAssignmentStatement))
            {
                return(false);
            }
            var iteratorIdentifier = iterator.Left as IdentifierNameSyntax;

            if (iteratorIdentifier == null)
            {
                return(false);
            }
            var stepExpression = iterator.Right as LiteralExpressionSyntax;

            if (stepExpression == null || !(stepExpression.Token.Value is int))
            {
                return(false);
            }
            int step = (int)stepExpression.Token.Value;

            if (SyntaxTokenExtensions.IsKind(iterator.OperatorToken, SyntaxKind.MinusToken))
            {
                step = -step;
            }

            var condition = node.Condition as CSS.BinaryExpressionSyntax;

            if (condition == null || !(condition.Left is CSS.IdentifierNameSyntax))
            {
                return(false);
            }
            if (((CSS.IdentifierNameSyntax)condition.Left).Identifier.IsEquivalentTo(iteratorIdentifier.Identifier))
            {
                return(false);
            }

            ExpressionSyntax end;

            if (iterator.IsKind(SyntaxKind.SubtractAssignmentStatement))
            {
                if (condition.IsKind(CS.SyntaxKind.GreaterThanOrEqualExpression))
                {
                    end = (ExpressionSyntax)condition.Right.Accept(_nodesVisitor);
                }
                else if (condition.IsKind(CS.SyntaxKind.GreaterThanExpression))
                {
                    end = SyntaxFactory.BinaryExpression(SyntaxKind.AddExpression, (ExpressionSyntax)condition.Right.Accept(_nodesVisitor), SyntaxFactory.Token(SyntaxKind.PlusToken), CommonConversions.Literal(1));
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                if (condition.IsKind(CS.SyntaxKind.LessThanOrEqualExpression))
                {
                    end = (ExpressionSyntax)condition.Right.Accept(_nodesVisitor);
                }
                else if (condition.IsKind(CS.SyntaxKind.LessThanExpression))
                {
                    end = SyntaxFactory.BinaryExpression(SyntaxKind.SubtractExpression, (ExpressionSyntax)condition.Right.Accept(_nodesVisitor), SyntaxFactory.Token(SyntaxKind.MinusToken), CommonConversions.Literal(1));
                }
                else
                {
                    return(false);
                }
            }

            VisualBasicSyntaxNode variable;
            ExpressionSyntax      start;

            if (hasVariable)
            {
                var v = node.Declaration.Variables[0];
                start = (ExpressionSyntax)v.Initializer?.Value.Accept(_nodesVisitor);
                if (start == null)
                {
                    return(false);
                }
                variable = SyntaxFactory.VariableDeclarator(
                    SyntaxFactory.SingletonSeparatedList(SyntaxFactory.ModifiedIdentifier(CommonConversions.ConvertIdentifier(v.Identifier))),
                    node.Declaration.Type.IsVar ? null : SyntaxFactory.SimpleAsClause((TypeSyntax)node.Declaration.Type.Accept(_nodesVisitor)),
                    null
                    );
            }
            else
            {
                var initializer = node.Initializers.FirstOrDefault() as CSS.AssignmentExpressionSyntax;
                if (initializer == null || !initializer.IsKind(CS.SyntaxKind.SimpleAssignmentExpression))
                {
                    return(false);
                }
                if (!(initializer.Left is CSS.IdentifierNameSyntax))
                {
                    return(false);
                }
                if (((CSS.IdentifierNameSyntax)initializer.Left).Identifier.IsEquivalentTo(iteratorIdentifier.Identifier))
                {
                    return(false);
                }
                variable = initializer.Left.Accept(_nodesVisitor);
                start    = (ExpressionSyntax)initializer.Right.Accept(_nodesVisitor);
            }

            block = SyntaxFactory.ForBlock(
                SyntaxFactory.ForStatement(variable, start, end, step == 1 ? null : SyntaxFactory.ForStepClause(CommonConversions.Literal(step))),
                ConvertBlock(node.Statement),
                SyntaxFactory.NextStatement()
                );
            return(true);
        }
Beispiel #4
0
        public override SyntaxList <StatementSyntax> VisitForEachStatement(CSS.ForEachStatementSyntax node)
        {
            VisualBasicSyntaxNode variable;

            if (node.Type.IsVar)
            {
                variable = SyntaxFactory.IdentifierName(CommonConversions.ConvertIdentifier(node.Identifier));
            }
            else
            {
                variable = SyntaxFactory.VariableDeclarator(
                    SyntaxFactory.SingletonSeparatedList(SyntaxFactory.ModifiedIdentifier(CommonConversions.ConvertIdentifier(node.Identifier))),
                    SyntaxFactory.SimpleAsClause((TypeSyntax)node.Type.Accept(_nodesVisitor)),
                    null
                    );
            }
            var expression = (ExpressionSyntax)node.Expression.Accept(_nodesVisitor);
            var stmt       = ConvertBlock(node.Statement);
            var block      = SyntaxFactory.ForEachBlock(
                SyntaxFactory.ForEachStatement(variable, expression),
                stmt,
                SyntaxFactory.NextStatement()
                );

            return(SyntaxFactory.SingletonList <StatementSyntax>(block));
        }