private static SwitchExpressionArmSyntax AsSwitchExpressionArmSyntax(
            AnalyzedSwitchSection section,
            Feature feature
            )
        {
            if (section.Labels.IsDefault)
            {
                return(SwitchExpressionArm(DiscardPattern(), AsExpressionSyntax(section.Body)));
            }

            var pattern    = AsPatternSyntax(section.Labels[0].Pattern, feature);
            var whenClause = AsWhenClause(section.Labels[0]);

            Debug.Assert(
                whenClause == null || section.Labels.Length == 1,
                "We shouldn't have guards when we're combining multiple cases into a single arm"
                );

            for (var i = 1; i < section.Labels.Length; i++)
            {
                var label = section.Labels[i];
                Debug.Assert(
                    label.Guards.Length == 0,
                    "We shouldn't have guards when we're combining multiple cases into a single arm"
                    );
                var nextPattern = AsPatternSyntax(label.Pattern, feature);
                pattern = BinaryPattern(
                    SyntaxKind.OrPattern,
                    pattern.Parenthesize(),
                    nextPattern.Parenthesize()
                    );
            }

            return(SwitchExpressionArm(pattern, whenClause, AsExpressionSyntax(section.Body)));
        }
Exemplo n.º 2
0
        private SyntaxNode AsSwitchSectionSyntax(AnalyzedSwitchSection section, SyntaxGenerator generator)
        {
            var statements = AsSwitchSectionStatements(section.Body);

            return(section.Labels.IsDefault
                ? generator.DefaultSwitchSection(statements)
                : generator.SwitchSectionFromLabels(section.Labels.Select(AsSwitchLabelSyntax), statements));
        }
Exemplo n.º 3
0
 private static SwitchExpressionArmSyntax AsSwitchExpressionArmSyntax(AnalyzedSwitchSection section)
 {
     // In a switch expression, we expect only a single label
     Debug.Assert(section.Labels.IsDefault || section.Labels.Length == 1);
     var(pattern, whenClause) = section.Labels.IsDefault
         ? (DiscardPattern(), null)
         : (AsPatternSyntax(section.Labels[0].Pattern), AsWhenClause(section.Labels[0]));
     return(SwitchExpressionArm(pattern, whenClause, AsExpressionSyntax(section.Body)));
 }