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:2802–2863  ·  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

2800 ///
2801 /// See: <https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-case_block>
2802 fn parse_match_case(&mut self) -> ast::MatchCase {
2803 let start = self.node_start();
2804 self.bump(TokenKind::Case);
2805
2806 // test_err match_stmt_missing_pattern
2807 // match x:
2808 // case : ...
2809 let pattern = self.parse_match_patterns();
2810
2811 let guard = if self.eat(TokenKind::If) {
2812 if self.at_expr() {
2813 // test_ok match_stmt_valid_guard_expr
2814 // match x:
2815 // case y if a := 1: ...
2816 // match x:
2817 // case y if a if True else b: ...
2818 // match x:
2819 // case y if lambda a: b: ...
2820 // match x:
2821 // case y if (yield x): ...
2822
2823 // test_err match_stmt_invalid_guard_expr
2824 // match x:
2825 // case y if *a: ...
2826 // match x:
2827 // case y if (*a): ...
2828 // match x:
2829 // case y if yield x: ...
2830 Some(Box::new(
2831 self.parse_named_expression_or_higher(ExpressionContext::default())
2832 .expr,
2833 ))
2834 } else {
2835 // test_err match_stmt_missing_guard_expr
2836 // match x:
2837 // case y if: ...
2838 self.add_error(
2839 ParseErrorType::ExpectedExpression,
2840 self.current_token_range(),
2841 );
2842 None
2843 }
2844 } else {
2845 None
2846 };
2847
2848 self.expect(TokenKind::Colon);
2849
2850 // test_err case_expect_indented_block
2851 // match subject:
2852 // case 1:
2853 // case 2: ...
2854 let body = self.parse_body(Clause::Case);
2855
2856 ast::MatchCase {
2857 pattern,
2858 guard,
2859 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