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

Pattern Matching Branch Review #2

Closed
wants to merge 207 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
207 commits
Select commit Hold shift + click to select a range
511c659
Add AST/compiler support for switches and guards
brandtbucher May 24, 2020
1c37cca
Add tests for pattern-matching
brandtbucher May 24, 2020
42cc51b
Add soft keywords
gvanrossum May 25, 2020
0fe638f
Fix bad type hint
brandtbucher May 25, 2020
40a0c79
Add support for named patterns
brandtbucher May 25, 2020
6188176
Add more tests for named patterns
brandtbucher May 25, 2020
45f228e
Merge branch 'patma' of https://github.com/brandtbucher/cpython into …
brandtbucher May 25, 2020
f11c0ae
Merge branch 'master' of https://github.com/python/cpython into patma
brandtbucher May 25, 2020
3323ae0
Add a nested walrus test
brandtbucher May 25, 2020
409de6c
Fix D() macro usage for soft keywords
gvanrossum May 25, 2020
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
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
73 changes: 73 additions & 0 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,30 @@ iterations of the loop.
.. versionadded:: 3.2


.. 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`, is not an
instance of :class:`str`/:class:`bytes`/:class:`bytearray`, and is not an
:term:`iterator`, push ``True`` onto the stack. Otherwise, push ``False``.

.. versionadded:: 3.10


All of the following opcodes use their arguments.

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


.. opcode:: GET_INDEX (i)

TOS is a sequence. Push ``TOS[i]`` onto the stack.

.. versionadded:: 3.10


.. opcode:: GET_INDEX_END (i)

TOS is a sequence, and TOS1 is its length. Push ``TOS[TOS1 - 1 - i]`` onto
the stack.

.. versionadded:: 3.10


.. opcode:: GET_INDEX_SLICE (i)

TOS is a sequence, and TOS1 is its length. Push a list formed from
``TOS[x:TOS1 - 1 - y]`` onto the stack, where *x* is the low byte of *i* and
*y* is the high bytes of *i*.

.. versionadded:: 3.10


.. 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.

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

.. versionadded:: 3.10


.. opcode:: MATCH_KEYS (copy)

TOS is a tuple of mapping keys, and TOS1 is the match subject. If TOS1
contains all of the keys in TOS, replace TOS with a tuple containing the
corresponding values and push ``True``. Otherwise, push ``False``.

If *copy* is non-zero, gather remaining items into a :class:`dict` and put it
on the stack where the subject used to be.

.. 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 @@ -359,3 +359,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 @@ -204,6 +205,114 @@ except_block[excepthandler_ty]:
| 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
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 @@ -668,7 +777,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.

5 changes: 4 additions & 1 deletion Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ struct _is {
PyObject *builtins;
PyObject *importlib;

// Kept handy for pattern matching:
PyObject *map_abc; // _collections_abc.Mapping
PyObject *seq_abc; // _collections_abc.Sequence

/* Used in Modules/_threadmodule.c. */
long num_threads;
/* Support for runtime thread stack size tuning.
Expand Down Expand Up @@ -290,4 +294,3 @@ PyAPI_FUNC(void) _PyInterpreterState_IDDecref(struct _is *);
}
#endif
#endif /* !Py_INTERNAL_INTERP_H */

5 changes: 5 additions & 0 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ given type object has a specified feature.
/* Type is abstract and cannot be instantiated */
#define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)

// This undocumented flag gives certain built-ins their unique pattern-matching
// behavior, which allows a single positional subpattern to match against the
// subject itself (rather than a mapped attribute on it):
#define _Py_TPFLAGS_MATCH_SELF (1UL << 21)

/* These flags are used to determine if a type is a subclass. */
#define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24)
#define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25)
Expand Down
8 changes: 8 additions & 0 deletions Include/opcode.h

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

Loading