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

bpo-42128: Structural Pattern Matching (PEP 634) #22917

Merged
merged 260 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
260 commits
Select commit Hold shift + click to select a range
1d39be6
Tentative (incomplete) grammar for match statements
gvanrossum May 25, 2020
67354ad
Add tests for or-patterns
brandtbucher May 25, 2020
5d609c7
Add support for or-patterns
brandtbucher May 25, 2020
a222351
Merge branch 'patma' of https://github.com/brandtbucher/cpython into …
brandtbucher May 25, 2020
1f8bcbd
Clean up pattern matching compiler
brandtbucher May 26, 2020
eb704d4
Add attribute/name support
brandtbucher May 26, 2020
5a361bf
Validate name context
brandtbucher May 26, 2020
9c2e61c
Clean up pattern compiler
brandtbucher May 26, 2020
dddcca8
Don't bind _
brandtbucher May 26, 2020
af70c74
Add sequence tests
brandtbucher May 26, 2020
4191858
Add basic sequence support
brandtbucher May 26, 2020
33633c1
Add support for starred sub-patterns in sequences
brandtbucher May 27, 2020
a36542e
Format tests
brandtbucher May 27, 2020
d62a426
Merge branch 'master' of https://github.com/python/cpython into patma
brandtbucher May 27, 2020
76d108c
Simplify control-flow
brandtbucher May 27, 2020
34c54a0
Rename blocks
brandtbucher May 27, 2020
8bc9ae5
Add GET_MATCH_ITER opcode
brandtbucher May 27, 2020
2586ee6
Add string tests
brandtbucher May 27, 2020
adb04fd
Rename and add patma tests
brandtbucher May 28, 2020
6eb6783
Cleanup
brandtbucher May 28, 2020
f3c513d
Use Mapping/Sequence _collections_abc
brandtbucher May 28, 2020
fc472c7
Add missing error handling
brandtbucher May 28, 2020
26dd4fd
Add GET_MATCH_MAP
brandtbucher May 28, 2020
6ce6c34
Simplify new opcodes
brandtbucher May 28, 2020
ea0de16
Bump MAGIC_NUMBER
brandtbucher May 28, 2020
880a7c2
Add mapping support
brandtbucher May 29, 2020
297ceb2
Add a bunch of mapping tests, plus cleanup
brandtbucher May 29, 2020
1902b21
Reorganize pattern compiler
brandtbucher May 29, 2020
09e8fdc
Handle errors when creating new blocks
brandtbucher May 29, 2020
f612a27
Break out _collections_abc stuff in ceval
brandtbucher May 29, 2020
21ac879
Clean up new opcode docs
brandtbucher May 29, 2020
c103ab4
Simplify string checks
brandtbucher May 29, 2020
14409e0
Add basic AST optimization
brandtbucher May 29, 2020
e91c400
Fix asserts
brandtbucher May 29, 2020
7fb4d11
Implement default object.__match__
brandtbucher May 30, 2020
17356fb
Fix sequence matching bug
brandtbucher May 31, 2020
9d7bd07
BinOp -> BoolOp
brandtbucher May 31, 2020
77fc89d
Fix failing tests
brandtbucher May 31, 2020
bad2fe2
Add MATCH_TYPE opcode
brandtbucher May 31, 2020
2924d2f
Fix parsing/unparsing of name contexts
brandtbucher May 31, 2020
66852e9
Clean up grammar
brandtbucher Jun 1, 2020
f1a1eb9
Fix calls and error messages
brandtbucher Jun 1, 2020
de7c3f0
Miscellaneous cleanup
brandtbucher Jun 1, 2020
89974f0
Convert AST tests to native syntax
brandtbucher Jun 1, 2020
ac87fcb
Allow literal negative numbers
brandtbucher Jun 2, 2020
e3d07ab
Add checks for name bindings
brandtbucher Jun 2, 2020
9da2729
Cleanup
brandtbucher Jun 2, 2020
546b6c4
Add support for complex literals
brandtbucher Jun 2, 2020
1eddf27
Fix AST validation
brandtbucher Jun 2, 2020
04bad8e
MATCH_TYPE -> DESTRUCTURE
brandtbucher Jun 2, 2020
e4be187
Fix attribute parsing
brandtbucher Jun 2, 2020
9a06fe3
Clean up and add tests
brandtbucher Jun 3, 2020
bb0ed11
Support destructuring type matches
brandtbucher Jun 4, 2020
03212ea
Simplify final (or only) case
brandtbucher Jun 4, 2020
ccf8374
Tell the peepholer about all of the jumping we do
brandtbucher Jun 5, 2020
c7f121b
Reject f-strings, addition, and subtraction
brandtbucher Jun 5, 2020
8d61da2
__match_args__ must contain strings
brandtbucher Jun 5, 2020
0c15e70
Better error messages for tuples
brandtbucher Jun 5, 2020
28bf07b
Add length-checking opcodes
brandtbucher Jun 7, 2020
009ca7e
Start phasing in new opcodes
brandtbucher Jun 7, 2020
32b396f
Refactor sequence-matching opcodes
brandtbucher Jun 8, 2020
a7e0488
OLD_MATCH_MAP_STAR -> OLD_MATCH_MAP
brandtbucher Jun 9, 2020
eb0fe26
Support None/missing __match_args__
brandtbucher Jun 9, 2020
6da649b
Catch up with master
brandtbucher Jun 9, 2020
90422e4
Clean up map matches
brandtbucher Jun 9, 2020
73d4a2b
Get rid of LIST_POP
brandtbucher Jun 9, 2020
2c5226a
Validate missing attributes with __match_args__
brandtbucher Jun 10, 2020
f4644aa
Incomplete __match_args_required__ support
brandtbucher Jun 10, 2020
ce7b021
Finish __match_args_required__ support
brandtbucher Jun 10, 2020
99d805c
Catch up with master
brandtbucher Jun 10, 2020
6cc4128
Trivial grammar clean-ups
brandtbucher Jun 10, 2020
4973548
Add back type annotations.
brandtbucher Jun 11, 2020
b50e403
Simplify or-pattern parsing
brandtbucher Jun 11, 2020
1e0e35a
Refactor MATCH opcode.
brandtbucher Jun 11, 2020
f5974ed
Catch up with master
brandtbucher Jun 11, 2020
3130114
Tuples!
brandtbucher Jun 11, 2020
de78bc5
Fix line numbers
brandtbucher Jun 11, 2020
1aecf5f
Catch up with master
brandtbucher Jun 11, 2020
07b1474
Improve compilation of wildcards.
brandtbucher Jun 12, 2020
18ef577
Better error message for bad types.
brandtbucher Jun 12, 2020
4106b66
Fix duplicate test
brandtbucher Jun 12, 2020
5ef4df0
Progress on performance improvements
brandtbucher Jun 12, 2020
ff0c902
Fix segfault on missing attribute
brandtbucher Jun 12, 2020
38000fa
Cleanup
brandtbucher Jun 12, 2020
15e8824
Support for __match_args__ in dataclasses.
viridia Jun 13, 2020
59c7a2f
Performance tweaks and __match__ protocol updates
brandtbucher Jun 15, 2020
db054a6
Match support for namedtuple.
viridia Jun 18, 2020
db4a9b0
Lose __match_args_required__, speed up MATCH_SEQ
brandtbucher Jun 22, 2020
48f18eb
Remove a bunch of new opcodes
brandtbucher Jun 22, 2020
0c82655
Don't raise if no __match_args__
brandtbucher Jun 22, 2020
0ddf11c
Catch up with master
brandtbucher Jun 22, 2020
ef35201
Add ImpossibleMatchError
brandtbucher Jun 23, 2020
276c2b0
Clean up opcodes and fix pickle tests
brandtbucher Jun 23, 2020
14be5cf
Clean up ceval
brandtbucher Jun 24, 2020
23dc88b
Add typing.sealed
brandtbucher Jun 25, 2020
6fa3eb3
Allow unparenthesized tuples as targets
brandtbucher Jun 25, 2020
67146a3
Fix handling of copied mappings
brandtbucher Jun 25, 2020
d7bb7ee
Add tests
brandtbucher Jun 25, 2020
574593c
Fix match target parsing
brandtbucher Jun 25, 2020
340d66f
Add more mapping tests and a performance utility
brandtbucher Jun 26, 2020
ca58086
More cleanup
brandtbucher Jun 27, 2020
3ec5802
Rough revision of __match__ protocol
brandtbucher Jun 28, 2020
0d99af4
Remove old AST tests and add "simple" match tests
brandtbucher Jun 28, 2020
018e6ab
Cleanup
brandtbucher Jun 28, 2020
7a6d897
Add pattern_context
brandtbucher Jun 28, 2020
7953dee
Keep bound names in pattern context
brandtbucher Jun 28, 2020
214101e
Address recent __match__ protocol changes
brandtbucher Jun 29, 2020
9e170bc
Ditch __match__!
brandtbucher Jun 30, 2020
d696de7
Fix whitespace diff
brandtbucher Jun 30, 2020
5e621f6
Catch up with master
brandtbucher Jun 30, 2020
bb8fb1b
Clean up tests and fix refleak in mapping patterns
brandtbucher Jul 1, 2020
36398b1
Minor improvements for class matches
brandtbucher Jul 1, 2020
82055e2
Mapping destructuring improvements
brandtbucher Jul 1, 2020
c9b3b4e
Use tuple for dataclass __match_args__
brandtbucher Jul 2, 2020
793add3
Add tests for new match grammar
brandtbucher Jul 2, 2020
4efdb9a
Clean up compile.c and add SyntaxWarning
brandtbucher Jul 2, 2020
46f3350
Clean up compiler
brandtbucher Jul 3, 2020
9b343b7
Progress on reducing the number of blocks
brandtbucher Jul 3, 2020
a4fd8a3
Simplify emitted bytecode
brandtbucher Jul 3, 2020
c9ad4b4
Catch up with master
brandtbucher Jul 4, 2020
b642e08
Implement __match_args__ for AST classes
pablogsal Jul 4, 2020
e39b3e2
No more leading dots
brandtbucher Jul 5, 2020
123e15b
Only use nonnegative integer indices
brandtbucher Jul 5, 2020
2f03ae0
Remove leading dot rules from unparsing logic
brandtbucher Jul 5, 2020
ef881fa
Use MATCH_ITEM for mapping and class patterns
brandtbucher Jul 5, 2020
eb86fa7
Reduce unnecessary EXTENDED_ARG instructions
brandtbucher Jul 6, 2020
f3d3d83
Documentation and cleanup
brandtbucher Jul 11, 2020
b0a02f5
Generalize conditional jump peepholing
brandtbucher Jul 12, 2020
da6ee98
MATCH_ITEM* -> GET_INDEX*
brandtbucher Jul 12, 2020
3eda95e
Revert old unparse hack
brandtbucher Jul 12, 2020
b3840be
Improve performance for wildcards
brandtbucher Jul 12, 2020
14e4eaa
Add/fix tests
brandtbucher Jul 13, 2020
4789bff
Fix refleaks when tracking names
brandtbucher Jul 13, 2020
1260139
Regenerate keyword module
brandtbucher Jul 13, 2020
34f8111
Add a ton of tests
brandtbucher Jul 14, 2020
3eadc53
Add more tests
brandtbucher Jul 14, 2020
b09e822
Catch up with master
brandtbucher Jul 14, 2020
7d23259
Update test
brandtbucher Jul 14, 2020
bed2e68
target -> subject
brandtbucher Jul 14, 2020
1a70d2c
Add tests
brandtbucher Jul 15, 2020
f83302f
Refactor loads
brandtbucher Jul 15, 2020
1ba56a0
Cleanup and better performance for default cases
brandtbucher Jul 17, 2020
3fd91d9
More cleanup
brandtbucher Jul 20, 2020
1de2a1c
Ditch unhelpful warning and add OPTIM note
brandtbucher Aug 16, 2020
7864718
Preserve TOS when compiling patterns
brandtbucher Aug 17, 2020
6fdc00c
Clean up some duplicate tests
brandtbucher Aug 18, 2020
ee2beb2
Improve test coverage for errors and warnings
brandtbucher Aug 19, 2020
b4b8416
Add more tests for runtime errors
brandtbucher Aug 19, 2020
f819084
Clean up comments
brandtbucher Aug 19, 2020
6228a57
Fix bugs in peephole optimizer changes
brandtbucher Aug 19, 2020
ce7aab4
Apparently the compiler's been rewritten for 3.10... :(
brandtbucher Aug 19, 2020
92f6c53
Disable unfinished validation
brandtbucher Aug 20, 2020
466becd
Skip performance benchmarking for some tests
brandtbucher Aug 20, 2020
a8d0a86
Fix typo
brandtbucher Aug 20, 2020
66d614b
Unify shared SyntaxError test logic
brandtbucher Aug 23, 2020
3f76403
Add two more tests for soft keywords
brandtbucher Aug 23, 2020
a527f66
Refactor MATCH_CLASS
brandtbucher Aug 23, 2020
2cc4fee
Catch up with master
brandtbucher Sep 9, 2020
19af7d5
Literal False/True/None compares by identity
brandtbucher Sep 16, 2020
601645b
Remove sealed
brandtbucher Sep 16, 2020
eed6806
Revert whitespace change
brandtbucher Sep 16, 2020
3c4796c
Assorted cleanup
brandtbucher Sep 16, 2020
45b1429
Add three more identity tests
brandtbucher Sep 16, 2020
99c5296
Catch up with master
brandtbucher Sep 25, 2020
5505fc3
Fixes for grammar changes
brandtbucher Sep 25, 2020
587ba87
Use sets to track map keys and attribute names.
brandtbucher Oct 19, 2020
a1e88fe
Add a few notes.
brandtbucher Oct 19, 2020
3a124db
Catch up with master
brandtbucher Oct 19, 2020
bd2bffb
Catch up with master
brandtbucher Oct 19, 2020
91661eb
"n := p" -> "p as n"
brandtbucher Oct 19, 2020
28ba072
Use new MatchAs node for named patterns
brandtbucher Oct 19, 2020
462796e
Add version check
brandtbucher Oct 19, 2020
ebd95c6
Raise for unreachable patterns
brandtbucher Oct 20, 2020
de1b020
Clean up error messages
brandtbucher Oct 20, 2020
f5c2482
Minor cleanup
brandtbucher Oct 20, 2020
4de619a
Reorganize some sequence/mapping logic
brandtbucher Oct 21, 2020
9f1c91d
More ceval cleanup
brandtbucher Oct 21, 2020
88bd791
Don't mark _ as local in patterns
brandtbucher Oct 21, 2020
12b6575
More cleanup
brandtbucher Oct 21, 2020
d394aa4
Add some tests for non-dict Mappings
brandtbucher Oct 21, 2020
2028445
Remove half-finished PyAST_Validate implementation
brandtbucher Oct 21, 2020
20b59b2
Remove some questionable fast paths
brandtbucher Oct 21, 2020
b42a3ac
Add test_patma to PGO
brandtbucher Oct 21, 2020
ccb5d90
Clean up SyntaxErroring
brandtbucher Oct 22, 2020
7a97a1f
Unsafe caching strikes again
brandtbucher Oct 22, 2020
ff011e0
Fix comment
brandtbucher Oct 22, 2020
99ae758
Remove unused get_len member
brandtbucher Oct 22, 2020
ad84403
Rename simple match flag
brandtbucher Oct 22, 2020
1b0602f
BoolOp -> MatchOr and fix constant folding
brandtbucher Oct 22, 2020
e212ceb
Minor cleanup
brandtbucher Oct 22, 2020
94b19ee
Unify grammar with PEP 634 (mostly)
brandtbucher Oct 22, 2020
77515ec
Clean up open_sequence_pattern usage
brandtbucher Oct 22, 2020
c0a06d1
More cleanup
brandtbucher Oct 23, 2020
26d35e5
Raise remaining SyntaxErrors in compiler
brandtbucher Oct 23, 2020
d43bb0b
Finish compiler cleanup
brandtbucher Oct 23, 2020
008f4ec
Catch up with master
brandtbucher Oct 23, 2020
d5f32da
Add NEWS entry
brandtbucher Oct 23, 2020
949822c
Improve precedence logic in ast.unparse
brandtbucher Oct 23, 2020
a31f2ef
Some VM cleanup
brandtbucher Oct 23, 2020
fe35b08
alternate -> alternative
brandtbucher Oct 23, 2020
e688005
Remove dict copy fast-path, add comments
brandtbucher Oct 24, 2020
5589f77
Finish ceval cleanup
brandtbucher Oct 24, 2020
a0f659a
Catch up with master
brandtbucher Oct 27, 2020
243f3d2
Regenerate the keyword module
brandtbucher Oct 27, 2020
a1ba3a7
Add regen-keyword to regen-all
brandtbucher Oct 27, 2020
812abd0
Catch up wih master
brandtbucher Oct 28, 2020
8b093eb
Remove iterator checks from sequence patterns
brandtbucher Oct 28, 2020
b85fde5
Catch up with master
brandtbucher Nov 6, 2020
130ec39
Catch up with master
brandtbucher Nov 11, 2020
e21d33a
Re-bump magic number
brandtbucher Nov 12, 2020
418c69d
Catch up with master
brandtbucher Nov 12, 2020
8bab3ed
Catch up with master
brandtbucher Dec 10, 2020
7bf4ee4
Catch up with master
brandtbucher Dec 10, 2020
16e218a
Fix compiler warning
brandtbucher Dec 10, 2020
fbd299b
Catch up with master
brandtbucher Dec 14, 2020
3279c57
Catch up with master
brandtbucher Dec 16, 2020
ff03e6b
Catch up with master
brandtbucher Dec 17, 2020
6f09968
Catch up with master
brandtbucher Dec 27, 2020
d9def0b
Catch up with master
brandtbucher Jan 5, 2021
ef87100
Catch up with master
brandtbucher Jan 8, 2021
7eee880
Catch up with master
brandtbucher Jan 13, 2021
f97b2eb
Catch up with master
brandtbucher Jan 18, 2021
165d491
Catch up with master
brandtbucher Feb 1, 2021
c732376
Catch up with master
brandtbucher Feb 2, 2021
c5729ec
Catch up with master
brandtbucher Feb 4, 2021
e149ab5
Catch up with master
brandtbucher Feb 5, 2021
d105180
Catch up with master
brandtbucher Feb 8, 2021
47c9218
CHECK -> RETURN_IF_FASLE
brandtbucher Feb 9, 2021
193f037
Cleanup
brandtbucher Feb 10, 2021
0618706
Catch up with master
brandtbucher Feb 10, 2021
b748e41
pattern_helper_load_constant -> ADDOP_LOAD_CONST
brandtbucher Feb 12, 2021
cc3357c
!x -> x == NULL
brandtbucher Feb 12, 2021
72b623c
pattern_helper_load_attr -> compiler_visit_expr
brandtbucher Feb 12, 2021
93cefc1
GET_INDEX/GET_INDEX_END -> BINARY_SUBSCR
brandtbucher Feb 12, 2021
62372c2
MATCH_KEYS -> MATCH_KEYS/COPY_DICT_WITHOUT_KEYS
brandtbucher Feb 12, 2021
2a5d0dd
Clean up MATCH_KEYS
brandtbucher Feb 12, 2021
fa97ee3
Refactor sequence matches to use existing ops
brandtbucher Feb 13, 2021
0de83a2
Clean up compiler_pattern_sequence
brandtbucher Feb 15, 2021
162f9b6
Clean up comment
brandtbucher Feb 15, 2021
49b2823
Factor out get_match_args
brandtbucher Feb 15, 2021
3f8fdaf
Replace TOS instead of pushing onto it
brandtbucher Feb 16, 2021
632a645
Catch up with master
brandtbucher Feb 16, 2021
0d56285
Catch up with master
brandtbucher Feb 19, 2021
69fa421
Fix NULL pointer error
brandtbucher Feb 20, 2021
1711207
Don't keep uninitialized tuples hanging around
brandtbucher Feb 20, 2021
f7c833e
Catch up with master
brandtbucher Feb 22, 2021
7762099
Fix new refleaks ("-R 3:3" is clean again)
brandtbucher Feb 22, 2021
5e269fb
Use PySet_CheckExact (thanks Pablo!)
brandtbucher Feb 22, 2021
ca6e332
Move some comments
brandtbucher Feb 23, 2021
61616fc
Catch up with master
brandtbucher Feb 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,49 @@ iterations of the loop.
.. versionadded:: 3.2


.. opcode:: COPY_DICT_WITHOUT_KEYS

TOS is a tuple of mapping keys, and TOS1 is the match subject. Replace TOS
with a :class:`dict` formed from the items of TOS1, but without any of the
keys in TOS.

.. versionadded:: 3.10


.. opcode:: GET_LEN

Push ``len(TOS)`` onto the stack.

.. versionadded:: 3.10


.. opcode:: MATCH_MAPPING

If TOS is an instance of :class:`collections.abc.Mapping`, push ``True`` onto
the stack. Otherwise, push ``False``.

.. versionadded:: 3.10


.. opcode:: MATCH_SEQUENCE

If TOS is an instance of :class:`collections.abc.Sequence` and is *not* an
instance of :class:`str`/:class:`bytes`/:class:`bytearray`, push ``True``
onto the stack. Otherwise, push ``False``.

.. versionadded:: 3.10


.. opcode:: MATCH_KEYS

TOS is a tuple of mapping keys, and TOS1 is the match subject. If TOS1
contains all of the keys in TOS, push a :class:`tuple` containing the
corresponding values, followed by ``True``. Otherwise, push ``None``,
followed by ``False``.

.. versionadded:: 3.10


All of the following opcodes use their arguments.

.. opcode:: STORE_NAME (namei)
Expand Down Expand Up @@ -1192,6 +1235,19 @@ All of the following opcodes use their arguments.
.. versionadded:: 3.6


.. opcode:: MATCH_CLASS (count)

TOS is a tuple of keyword attribute names, TOS1 is the class being matched
against, and TOS2 is the match subject. *count* is the number of positional
sub-patterns.

Pop TOS. If TOS2 is an instance of TOS1 and has the positional and keyword
attributes required by *count* and TOS, set TOS to ``True`` and TOS1 to a
tuple of extracted attributes. Otherwise, set TOS to ``False``.

.. versionadded:: 3.10


.. opcode:: HAVE_ARGUMENT

This is not really an opcode. It identifies the dividing line between
Expand Down
1 change: 1 addition & 0 deletions Doc/tools/susp-ignored.csv
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,4 @@ whatsnew/changelog,,::,default::DeprecationWarning
library/importlib.metadata,,:main,"EntryPoint(name='wheel', value='wheel.cli:main', group='console_scripts')"
library/importlib.metadata,,`,loading the metadata for packages for the indicated ``context``.
library/re,,`,"`"
library/dis,,:TOS1,TOS[x:TOS1 - 1 - y]
111 changes: 110 additions & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ compound_stmt[stmt_ty]:
| &('for' | ASYNC) for_stmt
| &'try' try_stmt
| &'while' while_stmt
| match_stmt

# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
assignment[stmt_ty]:
Expand Down Expand Up @@ -207,6 +208,114 @@ except_block[excepthandler_ty]:
| invalid_except_block
finally_block[asdl_stmt_seq*]: 'finally' ':' a=block { a }

match_stmt[stmt_ty]:
| "match" subject=subject_expr ':' NEWLINE INDENT cases[asdl_match_case_seq*]=case_block+ DEDENT {
CHECK_VERSION(stmt_ty, 10, "Pattern matching is", _Py_Match(subject, cases, EXTRA)) }
subject_expr[expr_ty]:
| value=star_named_expression ',' values=star_named_expressions? {
_Py_Tuple(CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, value, values)), Load, EXTRA) }
| named_expression
case_block[match_case_ty]:
| "case" pattern=patterns guard=guard? ':' body=block {
_Py_match_case(pattern, guard, body, p->arena) }
guard[expr_ty]: 'if' guard=named_expression { guard }

patterns[expr_ty]:
| values[asdl_expr_seq*]=open_sequence_pattern {
_Py_Tuple(values, Load, EXTRA) }
| pattern
pattern[expr_ty]:
| as_pattern
| or_pattern
as_pattern[expr_ty]:
| pattern=or_pattern 'as' target=capture_pattern {
_Py_MatchAs(pattern, target->v.Name.id, EXTRA) }
or_pattern[expr_ty]:
| patterns[asdl_expr_seq*]='|'.closed_pattern+ {
asdl_seq_LEN(patterns) == 1 ? asdl_seq_GET(patterns, 0) : _Py_MatchOr(patterns, EXTRA) }
closed_pattern[expr_ty]:
| literal_pattern
| capture_pattern
| wildcard_pattern
| value_pattern
| group_pattern
| sequence_pattern
| mapping_pattern
| class_pattern

literal_pattern[expr_ty]:
| signed_number !('+' | '-')
| real=signed_number '+' imag=NUMBER { _Py_BinOp(real, Add, imag, EXTRA) }
| real=signed_number '-' imag=NUMBER { _Py_BinOp(real, Sub, imag, EXTRA) }
| strings
| 'None' { _Py_Constant(Py_None, NULL, EXTRA) }
| 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
| 'False' { _Py_Constant(Py_False, NULL, EXTRA) }
signed_number[expr_ty]:
| NUMBER
| '-' number=NUMBER { _Py_UnaryOp(USub, number, EXTRA) }

capture_pattern[expr_ty]:
| !"_" name=NAME !('.' | '(' | '=') {
_PyPegen_set_expr_context(p, name, Store) }

wildcard_pattern[expr_ty]:
| "_" { _Py_Name(CHECK(PyObject*, _PyPegen_new_identifier(p, "_")), Store, EXTRA) }

value_pattern[expr_ty]:
| attr=attr !('.' | '(' | '=') { attr }
attr[expr_ty]:
| value=name_or_attr '.' attr=NAME {
_Py_Attribute(value, attr->v.Name.id, Load, EXTRA) }
name_or_attr[expr_ty]:
| attr
| NAME

group_pattern[expr_ty]:
| '(' pattern=pattern ')' { pattern }

sequence_pattern[expr_ty]:
| '[' values=maybe_sequence_pattern? ']' { _Py_List(values, Load, EXTRA) }
| '(' values=open_sequence_pattern? ')' { _Py_Tuple(values, Load, EXTRA) }
open_sequence_pattern[asdl_seq*]:
| value=maybe_star_pattern ',' values=maybe_sequence_pattern? {
_PyPegen_seq_insert_in_front(p, value, values) }
maybe_sequence_pattern[asdl_seq*]:
| values=','.maybe_star_pattern+ ','? { values }
maybe_star_pattern[expr_ty]:
| star_pattern
| pattern
star_pattern[expr_ty]:
| '*' value=(capture_pattern | wildcard_pattern) {
_Py_Starred(value, Store, EXTRA) }

mapping_pattern[expr_ty]:
| '{' items=items_pattern? '}' {
_Py_Dict(CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, items)), CHECK(asdl_expr_seq*, _PyPegen_get_values(p, items)), EXTRA) }
items_pattern[asdl_seq*]:
| items=','.key_value_pattern+ ','? { items }
key_value_pattern[KeyValuePair*]:
| key=(literal_pattern | value_pattern) ':' value=pattern {
_PyPegen_key_value_pair(p, key, value) }
| double_star_pattern
double_star_pattern[KeyValuePair*]:
| '**' value=capture_pattern { _PyPegen_key_value_pair(p, NULL, value) }

class_pattern[expr_ty]:
| func=name_or_attr '(' ')' { _Py_Call(func, NULL, NULL, EXTRA) }
| func=name_or_attr '(' args=positional_patterns ','? ')' {
_Py_Call(func, args, NULL, EXTRA) }
| func=name_or_attr '(' keywords=keyword_patterns ','? ')' {
_Py_Call(func, NULL, keywords, EXTRA) }
| func=name_or_attr '(' args=positional_patterns ',' keywords=keyword_patterns ','? ')' {
_Py_Call(func, args, keywords, EXTRA) }
positional_patterns[asdl_expr_seq*]:
| args[asdl_expr_seq*]=','.pattern+ { args }
keyword_patterns[asdl_keyword_seq*]:
| keywords[asdl_keyword_seq*]=','.keyword_pattern+ { keywords }
keyword_pattern[keyword_ty]:
| arg=NAME '=' value=pattern { _Py_keyword(arg->v.Name.id, value, EXTRA) }

return_stmt[stmt_ty]:
| 'return' a=[star_expressions] { _Py_Return(a, EXTRA) }

Expand Down Expand Up @@ -676,7 +785,7 @@ invalid_assignment:
RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
| (star_targets '=')* a=yield_expr '=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "assignment to yield expression not possible") }
| a=star_expressions augassign (yield_expr | star_expressions) {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a,
"'%s' is an illegal expression for augmented assignment",
_PyPegen_get_expr_name(a)
Expand Down
55 changes: 50 additions & 5 deletions Include/Python-ast.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Include/internal/pycore_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ struct ast_state {
PyObject *Lt_type;
PyObject *MatMult_singleton;
PyObject *MatMult_type;
PyObject *MatchAs_type;
PyObject *MatchOr_type;
PyObject *Match_type;
PyObject *Mod_singleton;
PyObject *Mod_type;
PyObject *Module_type;
Expand Down Expand Up @@ -137,6 +140,7 @@ struct ast_state {
PyObject *Yield_type;
PyObject *__dict__;
PyObject *__doc__;
PyObject *__match_args__;
PyObject *__module__;
PyObject *_attributes;
PyObject *_fields;
Expand All @@ -153,6 +157,7 @@ struct ast_state {
PyObject *bases;
PyObject *body;
PyObject *boolop_type;
PyObject *cases;
PyObject *cause;
PyObject *cmpop_type;
PyObject *col_offset;
Expand All @@ -175,6 +180,7 @@ struct ast_state {
PyObject *format_spec;
PyObject *func;
PyObject *generators;
PyObject *guard;
PyObject *handlers;
PyObject *id;
PyObject *ifs;
Expand All @@ -193,6 +199,7 @@ struct ast_state {
PyObject *level;
PyObject *lineno;
PyObject *lower;
PyObject *match_case_type;
PyObject *mod_type;
PyObject *module;
PyObject *msg;
Expand All @@ -204,13 +211,16 @@ struct ast_state {
PyObject *ops;
PyObject *optional_vars;
PyObject *orelse;
PyObject *pattern;
PyObject *patterns;
PyObject *posonlyargs;
PyObject *returns;
PyObject *right;
PyObject *simple;
PyObject *slice;
PyObject *step;
PyObject *stmt_type;
PyObject *subject;
PyObject *tag;
PyObject *target;
PyObject *targets;
Expand Down
Loading