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:1540–1687  ·  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

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