Add support for pattern additions
This PR might be incomplete; read below.
Regarding the MS proposal:
- Type patterns: Grammar rule primary_pattern is defined as something different from pattern, but as best as I can tell, they are exactly the same thing, so I replaced all occurrences of primary_pattern with pattern. Is that OK? (Also, the use of primary suggests here are secondary/other forms, but I don't see what they are.)
- Relational patterns: the proposed grammar shows relational_expression as the operand of each relational pattern, but the accompanying narrative goes on to constrain such relational expressions to constants only. So what not use *constant_expression" instead? And that's what I did. Am I missing something?
- Relational patterns: The proposal did not include support for enums, but as they are supported, I included them in the spec.
- Logical patterns: The proposal calls these pattern combinators; however, that term is not used elsewhere on MS's tutorial pages, so I used the more obvious (and elsewhere used) logical patterns instead.
- Logical patterns: The fact that the rule logical_pattern always expands to disjunctive_pattern may seem unnecessary, but it allows the placement of logical_pattern in the rule pattern rather than pushing the (odd-looking) disjunctive_pattern up there instead. (We use this approach elsewhere in the grammar.)
- The (substantial) second half of the proposal, "Open Issues with Proposed Changes," raises questions/issues. However, unlike all (or at least most) other MS proposals I've worked with, there is no follow-on discussion to say how these topics were actually addressed in the final implementation. Certainly, if any of the suggested approaches were taken, more words will need to be added to this PR to reflect that. I'll need help from someone at MS to resolve this.
This PR was created based on the assumption that the edits proposed by PR #873 for V8-related pattern-matching features will be adopted, and no other non-trivial edits will be made to that part of V8. When that PR is finally merged, this PR might need adjustment to reflect any changes in the V8 proposal review/adoption.
Regarding the OP, I have the following comments:
(1) Re "Type patterns: Grammar rule primary_pattern is defined as something different from pattern, but as best as I can tell, they are exactly the same thing, so I replaced all occurrences of primary_pattern with pattern. Is that OK? (Also, the use of primary suggests here are secondary/other forms, but I don't see what they are.)"
Several kinds of patterns are not primary_patterns. Specifically: disjunctive_pattern, conjunctive_pattern, and negated_pattern.
(2) Re: "Relational patterns: the proposed grammar shows relational_expression as the operand of each relational pattern, but the accompanying narrative goes on to constrain such relational expressions to constants only. So what not use *constant_expression" instead? And that's what I did. Am I missing something?"
Yes, there is a reason. The grammar rule for constant_expression makes it equivalent to expression (see section 12.23). So it includes, for example, operators at a looser precedence level, such as equality_expression which in this context it ought not do. Your change makes the following code ambiguous:
x = a is < b == c;
Because the left operand of the == c could be a is < b (correct) or it could be b (incorrect) due to your change.
(3) Re: "Relational patterns: The proposal did not include support for enums, but as they are supported, I included them in the spec."
Yes, good.
(4) Re: "Logical patterns: The proposal calls these pattern combinators; however, that term is not used elsewhere on MS's tutorial pages, so I used the more obvious (and elsewhere used) logical patterns instead."
Yes, good.
(5) Re: "Logical patterns: The fact that the rule logical_pattern always expands to disjunctive_pattern may seem unnecessary, but it allows the placement of logical_pattern in the rule pattern rather than pushing the (odd-looking) disjunctive_pattern up there instead. (We use this approach elsewhere in the grammar.)"
Okay, but I don't see a problem with using disjunctive_pattern as a right-hand-side in the rule for pattern.
(6) Re: "The (substantial) second half of the proposal, "Open Issues with Proposed Changes," raises questions/issues. However, unlike all (or at least most) other MS proposals I've worked with, there is no follow-on discussion to say how these topics were actually addressed in the final implementation. Certainly, if any of the suggested approaches were taken, more words will need to be added to this PR to reflect that. I'll need help from someone at MS to resolve this."
We should open issues for each of the open issues (or one issue for all of them) to make sure we address them.
Closing in favor of #1460