-
Notifications
You must be signed in to change notification settings - Fork 144
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
feat: add support for pattern matching #140
Conversation
4c83b38
to
5d8f09c
Compare
I need to solve this somehow with patterns (including "or" and "as" patterns) |
314a28e
to
2e38411
Compare
@@ -522,6 +534,14 @@ module.exports = grammar({ | |||
choice($.identifier, $.keyword_identifier, $.subscript, $.attribute) | |||
), | |||
|
|||
// Extended patterns (patterns allowed in match statement are far more flexible than simple patterns | |||
|
|||
as_pattern: $ => prec.left(seq( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or call this alias_expression?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The patterns in a match case are a subset of expressions. I simplified using expression here since I didn't want to replicated all the new patterns that basically follow the same rules as expressions.
Test fail for uses of match not as a keyword (django loves match)
Match is a soft keyword, the python parser would backtrack when seeing a |
A possible hacky resolution would be to have a common parent for assignments that assign to _maybe_match_statement: $ => seq(
choice($.match_statement,
alias(seq(alias("match", $.identifier),
choice(
seq('=', field('right', $._right_hand_side)),
seq(':', field('type', $.type)),
seq(':', field('type', $.type), '=', field('right', $._right_hand_side))
)), $.assignment))),
|
d253bb8
to
d49540f
Compare
Maybe there could also be a test case for a variable called match: int = 3 |
@thomkeh sure, but right now it doesn't parse the simple case correctly, also for |
55ab5fa
to
b6e4254
Compare
b9526a8
to
e2f8f54
Compare
64f0694
to
471d02b
Compare
if you're looking for weird match test cases you can go through the ones in CPython: https://github.com/python/cpython/blob/main/Lib/test/test_patma.py There's plenty of weird ones https://github.com/python/cpython/blob/eb483c46d62707bdf705491f76cf1fa9642fb47e/Lib/test/test_patma.py#L1205 |
a802812
to
3c903f0
Compare
Thanks @GBeauregard I hope I fixed a few edge cases in test_patma.py . The file now parses successfully. |
41267b4
to
92f52e9
Compare
I think you figured it out (and I don't think it mattered for this comment anyway), but to clarify the PEP 622 you linked here is not the accepted pattern matching specification (note the status is superseded); the final/accepted specification differs in a few aspects and was put in PEP 634: https://www.python.org/dev/peps/pep-0634/
no problem, thanks for implementing parsing for the fun new feature :) |
ca50b1f
to
3b4246e
Compare
One more comment, this and the See also https://docs.python.org/3.10/reference/compound_stmts.html#wildcard-patterns This can be useful for users as well, since the syntax highlighting difference will hint that |
This PR just regards all of the patterns introduced by pattern matching as expressions. To treat |
Hey guys. Just wondering when this will get merged :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, @theHamsta, thanks for taking this on!
You'll need to rebase onto the latest master since I just merged a couple of other PRs. Also, can you add match
and case
as new keywords in the syntax highlighting file? And ideally some highlighting tests to test/highlight/keywords.py
. Then I think this is good to merge!
3b4246e
to
6d5a897
Compare
@dcreager rebased and added a highlight test |
https://www.python.org/dev/peps/pep-0636/
Fixes #116