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:3030–3056  ·  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

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

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