docs icon indicating copy to clipboard operation
docs copied to clipboard

Problem with switch_label grammar rule, and possibly ambiguity

Open RexJaeschke opened this issue 3 years ago • 4 comments

I'm working on the spec for adding patterns as at V7 to the Ecma standard.

We have the following grammar:

switch_label
    : 'case' complex_pattern case_guard? ':'
    | 'case' constant_expression case_guard? ':'
    | 'default' ':'
    ;

This refers to the rule complex_pattern, which is NEVER defined or referred to in any other proposals from V7-V10! So that name has to be wrong. I'm thinking it should simply be pattern instead.

If we assume that is the correct fix,

    : 'case' pattern case_guard? ':'

would be satisfied by

    : 'case' constant_pattern case_guard? ':'

which is equivalent to

    : 'case' constant_expression case_guard? ':'

making both paths equivalent, and so ambiguous.

If the second case really is a subset of the first, we don't need, and can't have, the second one.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

RexJaeschke avatar Aug 02 '22 20:08 RexJaeschke

ping @333fred @jcouv

Who should I route this one to?

(If Rex's analysis is correct, I can submit the PR)

BillWagner avatar Aug 03 '22 13:08 BillWagner

I would ask @gafter what he meant to write there.

333fred avatar Aug 03 '22 14:08 333fred

FWIW, we have three cases in the implementation (see antlr snippet below) and the parser deals with some expression-or-pattern ambiguity (see ParseExpressionOrPatternForSwitchStatementCore):

switch_label
  : case_pattern_switch_label
  | case_switch_label
  | default_switch_label
  ;
  
case_pattern_switch_label
  : 'case' pattern when_clause? ':'
  ;

case_switch_label
  : 'case' expression ':'
  ;

Might be some backwards compatibility concern? But the main compatibility concern mentioned in the speclet and a quick scan of LDM notes regards binding of identifier X as type vs. constant (which interpretation do we prefer), and making that work across is, case and patterns in general. But I think that's a different issue than one above.

I would suggest digging through LDM notes more and starting a broader email with LDM and compiler teams.

jcouv avatar Aug 03 '22 16:08 jcouv

I think complex_pattern is supposed to be any pattern that isn't a constant pattern.

complex_pattern
    : declaration_pattern
    | var_pattern
    ;

That's because, as a pattern, a constant pattern only accepts a shift_expression, but as a case label any expression will do.

gafter avatar Aug 05 '22 19:08 gafter