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:1524–1671  ·  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

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