Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: parse pattern matching #13572

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

fedeci
Copy link
Member

@fedeci fedeci commented Jul 19, 2021

Q                       A
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link
License MIT

WIP: adding it here just to keep track of things to do and of what have been done.

  • Raise for & and | at the same level (e.g. when ({ x: 200 | 201 & 300 }) {})
  • Parse CallExpression + BlockStatement for newline after match
    • Disallow rest parameter for MatchExpression
  • Accept simple UnaryExpressions as MatchPattern
  • Support with and as
  • Support expression mode

@babel-bot
Copy link
Collaborator

babel-bot commented Jul 19, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/48511/

@fedeci fedeci changed the title feat: parse pattern matching WIP: parse pattern matching Jul 19, 2021
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 19, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 79e5c87:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

this.hasPlugin("patternMatching") &&
!this.hasFollowingLineBreak()
) {
return this.parseMaybeMatchExpression();
Copy link
Contributor

@JLHwung JLHwung Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb Can I assume match {} is a primary expression?

If so we should handle it in parseExprAtom, like #13573.

We probably need something like parseCoverMatchCallAndMatchExpression, similar to how we are disambiguating async () and async () => {}.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is a "primary" expression?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, i'm not super sure about this part. i would expect match () {} to be allowed anywhere any other expression is currently allowed (nested inside the pin operator in another match construct, of course, it would only be allowed with parens)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I have open tc39/proposal-pattern-matching#214 and let's continue there.

@fedeci
Copy link
Member Author

fedeci commented Jul 21, 2021

@ljharb @JLHwung In cases like the ones below should we parse it as CallExpression + missing semicolon or throw unexpected?

match (x,) {
  ...
}

match (x, y) {
  ...
}

@JLHwung
Copy link
Contributor

JLHwung commented Jul 21, 2021

@fedeci Good question! Yes, a match call expression match() is an ambiguous match expression head iff it satisfies all the following conditions:

  • Its callee match is an identifier which does not contain escape
  • It has exactly one argument
  • The argument is an expression
  • The argument has no trailing comma

If a match call is not ambiguous, this.semicolon() will throw after the match call.

@ljharb
Copy link
Member

ljharb commented Jul 21, 2021

Both examples in #13572 (comment) should indeed be parsed as a match expression, since x, y is a comma operator-separated expression.

@fedeci
Copy link
Member Author

fedeci commented Jul 21, 2021

@ljharb Will it be matched like this?

match (x, y) {
  when ({ j }, k) { }
}

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Jul 21, 2021

match (x, y) { ... } is probably the same as x, match (y) { ... }: x is evaluated and then ignored, as in any sequence expression.

@fedeci fedeci force-pushed the feature/parse-pattern-matching branch from b4a7cc7 to bc04533 Compare September 3, 2021 14:57
@ljharb
Copy link
Member

ljharb commented Sep 3, 2021

Note that in addition to combinatory likely using and/or, it’s seeming like the pin operator will change from ^(expr)/^chainOrIdentifier to ${expr}

@fedeci
Copy link
Member Author

fedeci commented Sep 3, 2021

Great, that will simplify the parsing.

@fedeci fedeci force-pushed the feature/parse-pattern-matching branch from bc04533 to 79e5c87 Compare September 3, 2021 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants