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

Add detailed pattern matching support #234

Merged
merged 2 commits into from
Aug 16, 2023
Merged

Conversation

jasontatton
Copy link
Contributor

Greetings, at Meta we are making use of tree-sitter-python in the ERRPY project which is leveraged by the Pyre type checker in order to parse Python code at Meta.

We would like to upstream this enhancement to the grammar which adds detailed support for pattern match cases (over just expression).

Within the ERRPY project this has enabled us to be able to correctly produce an AST for the Python 3.10 syntax of pattern match cases and also reject invalid case patterns (which would otherwise be accepted when the rule mapped to expression).

We have updated and enhanced the test cases to be more comprehensive of the various pattern match cases. We have also run tree-sitter generate and included the results as part of this PR.

One thing which we were unable to determine was how to run the code formatter for grammar.js.

@amaanq
Copy link
Member

amaanq commented Jul 27, 2023

Interesting changes, I'll start taking a look now

You can run the format check with npm run lint and then format it with eslint grammar.js --fix, though I guess maybe it's better to have a script to run eslint with fixes

@jasontatton
Copy link
Contributor Author

I have run npm run lint now

@amaanq
Copy link
Member

amaanq commented Jul 29, 2023

I do have some thoughts on this - I can write more details in a couple days since I'm a bit busy atm

There are a lot of unnecessary fields, the way fields should be used imo is, if you write a capture for a specific node, can it match multiple spots in the parent node? If so, then a field can be useful. Say we have the following:

version: $ => seq($.number, $.number, $.number)

Each number is the semver major minor and patch, so a field here makes sense like so:

version: $ => seq(
  field('major', $.number), 
  field('minor', $.number), 
  field('patch', $.number),
)

Anyways, I'd definitely remove some of them to potentially reduce state count, +500 isn't good imo

grammar.js Outdated Show resolved Hide resolved
grammar.js Outdated Show resolved Hide resolved
@amaanq
Copy link
Member

amaanq commented Jul 29, 2023

I have some more thoughts on the naming/visibility of some of these rules, but I'll discuss that more maybe Sunday

@jasontatton
Copy link
Contributor Author

I have removed the unneeded fields as requested

@jasontatton jasontatton requested a review from amaanq August 1, 2023 21:49
@amaanq
Copy link
Member

amaanq commented Aug 14, 2023

I don't like the recursive structure of rules that's typically seen in other notations - tree-sitter doesn't need all of that because of its precedence system. I'll rework it then merge

@amaanq
Copy link
Member

amaanq commented Aug 15, 2023

I'll note that the style I'd want to see here is similar to the one I created for types here

#239

In that case, types are used sparsely, and should always be a higher precedence than a potential expression that can conflict w/ it.

@amaanq
Copy link
Member

amaanq commented Aug 16, 2023

@jasontatton I've updated it to the style I like, but it might not be perfect. Just double check it and let me know if it seems fine to you, and I'll merge this. Thanks for being patient 🙂

Another note for the future, please don't open PRs from your master branch, it makes it annoying to check out locally (naming conflicts), and for pushing to (git requires me to manually specify HEAD for master), just create a new branch when making pull requests to avoid this issue in the future :)

@amaanq amaanq force-pushed the master branch 2 times, most recently from 6fbdc07 to a2dd880 Compare August 16, 2023 03:54
@jasontatton
Copy link
Contributor Author

Ok will make a new branch for a PR next time.

The changes look good to me.

@amaanq
Copy link
Member

amaanq commented Aug 16, 2023

Awesome, thanks for the contribution!

@amaanq amaanq merged commit 71b2a12 into tree-sitter:master Aug 16, 2023
4 checks passed
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.

2 participants