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:1528–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

1526 ///
1527 /// See: <https://docs.python.org/3/reference/compound_stmts.html#the-try-statement>
1528 fn parse_try_statement(&mut self) -> ast::StmtTry {
1529 let try_start = self.node_start();
1530 self.bump(TokenKind::Try);
1531 self.expect(TokenKind::Colon);
1532
1533 let mut is_star: Option<bool> = None;
1534
1535 let try_body = self.parse_body(Clause::Try);
1536
1537 let has_except = self.at(TokenKind::Except);
1538
1539 // test_err try_stmt_mixed_except_kind
1540 // try:
1541 // pass
1542 // except:
1543 // pass
1544 // except* ExceptionGroup:
1545 // pass
1546 // try:
1547 // pass
1548 // except* ExceptionGroup:
1549 // pass
1550 // except:
1551 // pass
1552 // try:
1553 // pass
1554 // except:
1555 // pass
1556 // except:
1557 // pass
1558 // except* ExceptionGroup:
1559 // pass
1560 // except* ExceptionGroup:
1561 // pass
1562 let mut mixed_except_ranges = Vec::new();
1563 let mut handlers = self.parse_clauses(Clause::Except, |p| {
1564 let (handler, kind) = p.parse_except_clause();
1565 if let ExceptClauseKind::Star(range) = kind {
1566 p.add_unsupported_syntax_error(UnsupportedSyntaxErrorKind::ExceptStar, range);
1567 }
1568 if is_star.is_none() {
1569 is_star = Some(kind.is_star());
1570 } else if is_star != Some(kind.is_star()) {
1571 mixed_except_ranges.push(handler.range());
1572 }
1573 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(
1582 "Cannot have both 'except' and 'except*' on the same 'try'".to_string(),
1583 ),
1584 handler_err_range,
1585 );

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
current_token_rangeMethod · 0.80
node_rangeMethod · 0.80
bumpMethod · 0.45
atMethod · 0.45
is_noneMethod · 0.45
is_starMethod · 0.45

Tested by

no test coverage detected