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

Method parse_except_clause

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

Parses an `except` clause of a `try` statement. # Panics If the parser isn't positioned at an `except` token.

(&mut self)

Source from the content-addressed store, hash-verified

1685 ///
1686 /// If the parser isn't positioned at an `except` token.
1687 fn parse_except_clause(&mut self) -> (ExceptHandler, ExceptClauseKind) {
1688 let start = self.node_start();
1689 self.bump(TokenKind::Except);
1690
1691 let star_token_range = self.current_token_range();
1692 let block_kind = if self.eat(TokenKind::Star) {
1693 ExceptClauseKind::Star(star_token_range)
1694 } else {
1695 ExceptClauseKind::Normal
1696 };
1697
1698 let type_ = if self.at_expr() {
1699 // test_err except_stmt_invalid_expression
1700 // try:
1701 // pass
1702 // except yield x:
1703 // pass
1704 // try:
1705 // pass
1706 // except* *x:
1707 // pass
1708 let parsed_expr = self.parse_expression_list(ExpressionContext::default());
1709 if matches!(
1710 parsed_expr.expr,
1711 Expr::Tuple(ast::ExprTuple {
1712 parenthesized: false,
1713 ..
1714 })
1715 ) {
1716 if self.at(TokenKind::As) {
1717 // test_err except_stmt_unparenthesized_tuple_as
1718 // try:
1719 // pass
1720 // except x, y as exc:
1721 // pass
1722 // try:
1723 // pass
1724 // except* x, y as eg:
1725 // pass
1726 self.add_error(
1727 ParseErrorType::OtherError(
1728 "Multiple exception types must be parenthesized when using `as`"
1729 .to_string(),
1730 ),
1731 &parsed_expr,
1732 );
1733 } else {
1734 // test_err except_stmt_unparenthesized_tuple_no_as_py313
1735 // # parse_options: {"target-version": "3.13"}
1736 // try:
1737 // pass
1738 // except x, y:
1739 // pass
1740 // try:
1741 // pass
1742 // except* x, y:
1743 // pass
1744

Callers 1

parse_try_statementMethod · 0.80

Calls 15

ExceptHandlerEnum · 0.85
node_startMethod · 0.80
current_token_rangeMethod · 0.80
at_exprMethod · 0.80
parse_expression_listMethod · 0.80
parse_identifierMethod · 0.80
expectMethod · 0.80
parse_bodyMethod · 0.80
node_rangeMethod · 0.80
defaultFunction · 0.50

Tested by

no test coverage detected