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

Method parse_if_expression

crates/ruff_python_parser/src/parser/expression.rs:3020–3046  ·  view source on GitHub ↗

Parses an `if` expression. # Panics If the parser isn't positioned at an `if` token. See:

(&mut self, body: Expr, start: TextSize)

Source from the content-addressed store, hash-verified

3018 ///
3019 /// See: <https://docs.python.org/3/reference/expressions.html#conditional-expressions>
3020 pub(super) fn parse_if_expression(&mut self, body: Expr, start: TextSize) -> ast::ExprIf {
3021 self.bump(TokenKind::If);
3022
3023 let test = self.parse_simple_expression(ExpressionContext::default());
3024
3025 self.expect(TokenKind::Else);
3026
3027 // `a if b else a if b else ...` recurses through `orelse` at the
3028 // conditional layer, which is not covered by the `parse_lhs_expression`
3029 // guard (that scope is released once each atom is parsed). Guard here.
3030 let orelse = if let Some(orelse) =
3031 self.with_recursion(Self::parse_conditional_expression_or_higher)
3032 {
3033 orelse
3034 } else {
3035 self.report_recursion_limit_exceeded(self.current_token_range());
3036 self.recursion_recovery_expr()
3037 };
3038
3039 ast::ExprIf {
3040 body: Box::new(body),
3041 test: Box::new(test.expr),
3042 orelse: Box::new(orelse.expr),
3043 range: self.node_range(start),
3044 node_index: AtomicNodeIndex::NONE,
3045 }
3046 }
3047
3048 /// Parses an IPython escape command at the expression level.
3049 ///

Calls 9

expectMethod · 0.80
with_recursionMethod · 0.80
current_token_rangeMethod · 0.80
node_rangeMethod · 0.80
defaultFunction · 0.50
bumpMethod · 0.45

Tested by

no test coverage detected