Parses an `if` expression. # Panics If the parser isn't positioned at an `if` token. See:
(&mut self, body: Expr, start: TextSize)
| 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 | /// |
no test coverage detected