예제 #1
0
        internal BoundPattern BindPattern(
            PatternSyntax node,
            BoundExpression operand,
            TypeSymbol operandType,
            bool hasErrors,
            DiagnosticBag diagnostics,
            bool wasSwitchCase = false)
        {
            switch (node.Kind())
            {
                case SyntaxKind.DeclarationPattern:
                    return BindDeclarationPattern(
                        (DeclarationPatternSyntax)node, operand, operandType, hasErrors, diagnostics);

                case SyntaxKind.ConstantPattern:
                    return BindConstantPattern(
                        (ConstantPatternSyntax)node, operand, operandType, hasErrors, diagnostics, wasSwitchCase);

                default:
                    throw ExceptionUtilities.UnexpectedValue(node.Kind());
            }
        }
예제 #2
0
        internal BoundPattern BindPattern(
            PatternSyntax node,
            BoundExpression operand,
            TypeSymbol operandType,
            bool hasErrors,
            DiagnosticBag diagnostics,
            bool wasSwitchCase = false)
        {
            switch (node.Kind())
            {
            case SyntaxKind.DeclarationPattern:
                return(BindDeclarationPattern(
                           (DeclarationPatternSyntax)node, operand, operandType, hasErrors, diagnostics));

            case SyntaxKind.ConstantPattern:
                return(BindConstantPattern(
                           (ConstantPatternSyntax)node, operand, operandType, hasErrors, diagnostics, wasSwitchCase));

            default:
                throw ExceptionUtilities.UnexpectedValue(node.Kind());
            }
        }
예제 #3
0
        private static Task <Document> ConvertSwitchExpressionToSwitchStatement(
            Document document,
            SwitchExpressionSyntax switchExpression,
            CancellationToken cancellationToken)
        {
            SeparatedSyntaxList <SwitchExpressionArmSyntax> arms = switchExpression.Arms;

            SyntaxToken[] separators = arms.GetSeparators().ToArray();

            IEnumerable <SwitchSectionSyntax> sections = arms.Select((arm, i) =>
            {
                PatternSyntax pattern       = arm.Pattern;
                ExpressionSyntax expression = arm.Expression;
                SyntaxToken semicolon       = Token(SyntaxKind.SemicolonToken);
                SyntaxToken separator       = default;

                if (i < separators.Length)
                {
                    separator = separators[i];
                }

                if (separator.IsKind(SyntaxKind.None) ||
                    separator.IsMissing)
                {
                    semicolon  = semicolon.WithTrailingTrivia(arm.GetTrailingTrivia());
                    expression = expression.WithoutTrailingTrivia();
                }
                else
                {
                    semicolon = semicolon.WithTriviaFrom(separator);
                }

                SyntaxKind kind         = pattern.Kind();
                SwitchLabelSyntax label = default;

                if (kind == SyntaxKind.ConstantPattern)
                {
                    label = CaseSwitchLabel(
                        Token(SyntaxKind.CaseKeyword).WithLeadingTrivia(pattern.GetLeadingTrivia()),
                        ((ConstantPatternSyntax)pattern).Expression.WithoutLeadingTrivia(),
                        Token(SyntaxKind.ColonToken).WithTriviaFrom(arm.EqualsGreaterThanToken));
                }
                else if (kind == SyntaxKind.DiscardPattern)
                {
                    label = DefaultSwitchLabel(Token(SyntaxKind.DefaultKeyword), Token(SyntaxKind.ColonToken));
                }
                else
                {
                    throw new InvalidOperationException();
                }

                StatementSyntax statement = CreateStatement(expression, semicolon);

                return(SwitchSection(label, statement));
            });

            var returnStatement = (ReturnStatementSyntax)switchExpression.Parent;

            SwitchStatementSyntax switchStatement = SwitchStatement(
                switchExpression.SwitchKeyword.WithTriviaFrom(returnStatement.ReturnKeyword),
                OpenParenToken(),
                switchExpression.GoverningExpression,
                CloseParenToken().WithTrailingTrivia(switchExpression.SwitchKeyword.TrailingTrivia),
                switchExpression.OpenBraceToken,
                sections.ToSyntaxList(),
                switchExpression.CloseBraceToken);

            switchStatement = switchStatement.WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(returnStatement, switchStatement, cancellationToken));