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

Method parse_elif_or_else_clause

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

Parses an `elif` or `else` clause. # Panics If the parser isn't positioned at an `elif` or `else` token.

(&mut self, kind: ElifOrElse)

Source from the content-addressed store, hash-verified

1485 ///
1486 /// If the parser isn't positioned at an `elif` or `else` token.
1487 fn parse_elif_or_else_clause(&mut self, kind: ElifOrElse) -> ast::ElifElseClause {
1488 let start = self.node_start();
1489 self.bump(kind.as_token_kind());
1490
1491 let test = if kind.is_elif() {
1492 // test_err if_stmt_invalid_elif_test_expr
1493 // if x:
1494 // pass
1495 // elif *x:
1496 // pass
1497 // elif yield x:
1498 // pass
1499 Some(
1500 self.parse_named_expression_or_higher(ExpressionContext::default())
1501 .expr,
1502 )
1503 } else {
1504 None
1505 };
1506
1507 // test_err if_stmt_elif_missing_colon
1508 // if x:
1509 // pass
1510 // elif y
1511 // pass
1512 // else:
1513 // pass
1514 self.expect(TokenKind::Colon);
1515
1516 let body = self.parse_body(kind.as_clause());
1517
1518 ast::ElifElseClause {
1519 test,
1520 body,
1521 range: self.node_range(start),
1522 node_index: AtomicNodeIndex::NONE,
1523 }
1524 }
1525
1526 /// Parses a `try` statement.
1527 ///

Callers 1

parse_if_statementMethod · 0.80

Calls 10

node_startMethod · 0.80
as_token_kindMethod · 0.80
expectMethod · 0.80
parse_bodyMethod · 0.80
as_clauseMethod · 0.80
node_rangeMethod · 0.80
defaultFunction · 0.50
bumpMethod · 0.45
is_elifMethod · 0.45

Tested by

no test coverage detected