MCPcopy Create free account
hub / github.com/astral-sh/ruff / parse_match_case

Method parse_match_case

crates/ruff_python_parser/src/parser/statement.rs:2768–2829  ·  view source on GitHub ↗

Parses a single match case block. # Panics If the parser isn't positioned at a `case` token. See:

(&mut self)

Source from the content-addressed store, hash-verified

2766 ///
2767 /// See: <https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-case_block>
2768 fn parse_match_case(&mut self) -> ast::MatchCase {
2769 let start = self.node_start();
2770 self.bump(TokenKind::Case);
2771
2772 // test_err match_stmt_missing_pattern
2773 // match x:
2774 // case : ...
2775 let pattern = self.parse_match_patterns();
2776
2777 let guard = if self.eat(TokenKind::If) {
2778 if self.at_expr() {
2779 // test_ok match_stmt_valid_guard_expr
2780 // match x:
2781 // case y if a := 1: ...
2782 // match x:
2783 // case y if a if True else b: ...
2784 // match x:
2785 // case y if lambda a: b: ...
2786 // match x:
2787 // case y if (yield x): ...
2788
2789 // test_err match_stmt_invalid_guard_expr
2790 // match x:
2791 // case y if *a: ...
2792 // match x:
2793 // case y if (*a): ...
2794 // match x:
2795 // case y if yield x: ...
2796 Some(Box::new(
2797 self.parse_named_expression_or_higher(ExpressionContext::default())
2798 .expr,
2799 ))
2800 } else {
2801 // test_err match_stmt_missing_guard_expr
2802 // match x:
2803 // case y if: ...
2804 self.add_error(
2805 ParseErrorType::ExpectedExpression,
2806 self.current_token_range(),
2807 );
2808 None
2809 }
2810 } else {
2811 None
2812 };
2813
2814 self.expect(TokenKind::Colon);
2815
2816 // test_err case_expect_indented_block
2817 // match subject:
2818 // case 1:
2819 // case 2: ...
2820 let body = self.parse_body(Clause::Case);
2821
2822 ast::MatchCase {
2823 pattern,
2824 guard,
2825 body,

Callers 1

Calls 12

node_startMethod · 0.80
parse_match_patternsMethod · 0.80
at_exprMethod · 0.80
current_token_rangeMethod · 0.80
expectMethod · 0.80
parse_bodyMethod · 0.80
node_rangeMethod · 0.80
defaultFunction · 0.50
bumpMethod · 0.45
eatMethod · 0.45
add_errorMethod · 0.45

Tested by

no test coverage detected