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:2772–2833  ·  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

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