Skip to content

Add support for the match statement - #246

Closed
SahilRajputdotexe wants to merge 1 commit into
davidhalter:masterfrom
SahilRajputdotexe:feat/match-statement
Closed

SahilRajputdotexe wants to merge 1 commit into
davidhalter:masterfrom
SahilRajputdotexe:feat/match-statement

Conversation

@SahilRajputdotexe

Copy link
Copy Markdown

This adds parsing of the match statement (PEP 634) to the Python 3.10 and later grammars. Refs #138, which discusses how to handle the soft keywords.

match and case are soft keywords, so match = 1, match(x) and match[0]: int have to keep working. A match that starts a logical line and whose line ends in a colon is parsed as the statement, anything else is a name. The parser also falls back to a name when case is not accepted as a keyword. This does not follow the demo branch linked in the issue, which makes the parser semi-LL(1). The lookahead here is a small wrapper around the token stream. No new tree classes are added. The new nodes are plain match_stmt, case_block and pattern nodes, and match_stmt and case_block are registered as flow containers.

The error finder reports what CPython reports for the new syntax, for example an irrefutable pattern that is not last, a repeated capture name, * patterns outside a sequence and bad mapping or class patterns. The diff parser and the pep8 normalizer also understand the statement.

import parso
module = parso.load_grammar(version='3.10').parse('match p:\n    case Point(x=0) | [0, *_]:\n        pass\n')

I ran the full test suite on Python 3.8 to 3.14 locally. New tests are in test_parser.py, test_diff_parser.py, test_pep8.py and failing_examples.py, which is compared against CPython's own messages on 3.10 and later.

Parse PEP 634 match statements with the grammars for Python 3.10 and
later. match and case stay usable as names; match is only a statement
keyword when the logical line starts with it and ends with a colon.
The error finder reports the same syntax errors as CPython for the new
statement, and the diff parser and the pep8 normalizer know about it.
@davidhalter

Copy link
Copy Markdown
Owner

LLM slop, closing.

@davidhalter davidhalter closed this Oct 2, 2026
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