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

Method parse_try_statement

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

Parses a `try` statement. # Panics If the parser isn't positioned at a `try` token. See:

(&mut self)

Source from the content-addressed store, hash-verified

1531 ///
1532 /// See: <https://docs.python.org/3/reference/compound_stmts.html#the-try-statement>
1533 fn parse_try_statement(&mut self) -> ast::StmtTry {
1534 let try_start = self.node_start();
1535 self.bump(TokenKind::Try);
1536 self.expect(TokenKind::Colon);
1537
1538 let mut is_star: Option<bool> = None;
1539
1540 let try_body = self.parse_body(Clause::Try);
1541
1542 let has_except = self.at(TokenKind::Except);
1543
1544 // test_err try_stmt_mixed_except_kind
1545 // try:
1546 // pass
1547 // except:
1548 // pass
1549 // except* ExceptionGroup:
1550 // pass
1551 // try:
1552 // pass
1553 // except* ExceptionGroup:
1554 // pass
1555 // except:
1556 // pass
1557 // try:
1558 // pass
1559 // except:
1560 // pass
1561 // except:
1562 // pass
1563 // except* ExceptionGroup:
1564 // pass
1565 // except* ExceptionGroup:
1566 // pass
1567 let mut mixed_except_ranges = Vec::new();
1568 let mut handlers = Vec::new();
1569 self.parse_clauses(Clause::Except, |p| {
1570 let (handler, kind) = p.parse_except_clause();
1571 if let ExceptClauseKind::Star(range) = kind {
1572 p.add_unsupported_syntax_error(UnsupportedSyntaxErrorKind::ExceptStar, range);
1573 }
1574 if is_star.is_none() {
1575 is_star = Some(kind.is_star());
1576 } else if is_star != Some(kind.is_star()) {
1577 mixed_except_ranges.push(handler.range());
1578 }
1579 if handlers.is_empty() {
1580 handlers.reserve_exact(1);
1581 }
1582 handlers.push(handler);
1583 });
1584 handlers.shrink_to_fit();
1585
1586 // Empty handler has `is_star` false.
1587 let is_star = is_star.unwrap_or_default();
1588 for handler_err_range in mixed_except_ranges {
1589 self.add_error(
1590 ParseErrorType::OtherError(

Callers 1

parse_statementMethod · 0.80

Calls 15

node_startMethod · 0.80
expectMethod · 0.80
parse_bodyMethod · 0.80
parse_clausesMethod · 0.80
parse_except_clauseMethod · 0.80
reserve_exactMethod · 0.80
current_token_rangeMethod · 0.80
node_rangeMethod · 0.80
bumpMethod · 0.45
atMethod · 0.45
is_noneMethod · 0.45

Tested by

no test coverage detected