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

Method parse_match_body

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

Parses the body of a `match` statement. This method expects that the parser is positioned at a `Newline` token. If not, it adds a syntax error and continues parsing.

(&mut self)

Source from the content-addressed store, hash-verified

2696 /// This method expects that the parser is positioned at a `Newline` token. If not, it adds a
2697 /// syntax error and continues parsing.
2698 fn parse_match_body(&mut self) -> Vec<ast::MatchCase> {
2699 // test_err match_stmt_no_newline_before_case
2700 // match foo: case _: ...
2701 self.expect(TokenKind::Newline);
2702
2703 // Use `eat` instead of `expect` for better error message.
2704 if !self.eat(TokenKind::Indent) {
2705 // test_err match_stmt_expect_indented_block
2706 // match foo:
2707 // case _: ...
2708 self.add_error(
2709 ParseErrorType::OtherError(
2710 "Expected an indented block after `match` statement".to_string(),
2711 ),
2712 self.current_token_range(),
2713 );
2714 }
2715
2716 let cases = self.parse_match_case_blocks();
2717
2718 // TODO(dhruvmanila): Should we expect `Dedent` only if there was an `Indent` present?
2719 self.expect(TokenKind::Dedent);
2720
2721 cases
2722 }
2723
2724 /// Parses a list of match case blocks.
2725 fn parse_match_case_blocks(&mut self) -> Vec<ast::MatchCase> {

Callers 2

parse_match_statementMethod · 0.80

Calls 6

expectMethod · 0.80
current_token_rangeMethod · 0.80
eatMethod · 0.45
add_errorMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected