Parses a single match case block. # Panics If the parser isn't positioned at a `case` token. See:
(&mut self)
| 2757 | /// |
| 2758 | /// See: <https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-case_block> |
| 2759 | fn parse_match_case(&mut self) -> ast::MatchCase { |
| 2760 | let start = self.node_start(); |
| 2761 | self.bump(TokenKind::Case); |
| 2762 | |
| 2763 | // test_err match_stmt_missing_pattern |
| 2764 | // match x: |
| 2765 | // case : ... |
| 2766 | let pattern = self.parse_match_patterns(); |
| 2767 | |
| 2768 | let guard = if self.eat(TokenKind::If) { |
| 2769 | if self.at_expr() { |
| 2770 | // test_ok match_stmt_valid_guard_expr |
| 2771 | // match x: |
| 2772 | // case y if a := 1: ... |
| 2773 | // match x: |
| 2774 | // case y if a if True else b: ... |
| 2775 | // match x: |
| 2776 | // case y if lambda a: b: ... |
| 2777 | // match x: |
| 2778 | // case y if (yield x): ... |
| 2779 | |
| 2780 | // test_err match_stmt_invalid_guard_expr |
| 2781 | // match x: |
| 2782 | // case y if *a: ... |
| 2783 | // match x: |
| 2784 | // case y if (*a): ... |
| 2785 | // match x: |
| 2786 | // case y if yield x: ... |
| 2787 | Some(Box::new( |
| 2788 | self.parse_named_expression_or_higher(ExpressionContext::default()) |
| 2789 | .expr, |
| 2790 | )) |
| 2791 | } else { |
| 2792 | // test_err match_stmt_missing_guard_expr |
| 2793 | // match x: |
| 2794 | // case y if: ... |
| 2795 | self.add_error( |
| 2796 | ParseErrorType::ExpectedExpression, |
| 2797 | self.current_token_range(), |
| 2798 | ); |
| 2799 | None |
| 2800 | } |
| 2801 | } else { |
| 2802 | None |
| 2803 | }; |
| 2804 | |
| 2805 | self.expect(TokenKind::Colon); |
| 2806 | |
| 2807 | // test_err case_expect_indented_block |
| 2808 | // match subject: |
| 2809 | // case 1: |
| 2810 | // case 2: ... |
| 2811 | let body = self.parse_body(Clause::Case); |
| 2812 | |
| 2813 | ast::MatchCase { |
| 2814 | pattern, |
| 2815 | guard, |
| 2816 | body, |
no test coverage detected