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:1535–1682  ·  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

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