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:1689–1834  ·  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

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

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