Parses a lambda expression. # Panics If the parser isn't positioned at a `lambda` token. See:
(&mut self)
| 2971 | /// |
| 2972 | /// See: <https://docs.python.org/3/reference/expressions.html#lambda> |
| 2973 | fn parse_lambda_expr(&mut self) -> ast::ExprLambda { |
| 2974 | let start = self.node_start(); |
| 2975 | self.bump(TokenKind::Lambda); |
| 2976 | |
| 2977 | let parameters = if self.at(TokenKind::Colon) { |
| 2978 | // test_ok lambda_with_no_parameters |
| 2979 | // lambda: 1 |
| 2980 | None |
| 2981 | } else { |
| 2982 | Some(Box::new(self.parse_parameters(FunctionKind::Lambda))) |
| 2983 | }; |
| 2984 | |
| 2985 | self.expect(TokenKind::Colon); |
| 2986 | |
| 2987 | // test_ok lambda_with_valid_body |
| 2988 | // lambda x: x |
| 2989 | // lambda x: x if True else y |
| 2990 | // lambda x: await x |
| 2991 | // lambda x: lambda y: x + y |
| 2992 | // lambda x: (yield x) # Parenthesized `yield` is fine |
| 2993 | // lambda x: x, *y |
| 2994 | |
| 2995 | // test_err lambda_body_with_starred_expr |
| 2996 | // lambda x: *y |
| 2997 | // lambda x: *y, |
| 2998 | // lambda x: *y, z |
| 2999 | // lambda x: *y and z |
| 3000 | |
| 3001 | // test_err lambda_body_with_yield_expr |
| 3002 | // lambda x: yield y |
| 3003 | // lambda x: yield from y |
| 3004 | |
| 3005 | // `lambda: lambda: lambda: ...` recurses through the lambda body at |
| 3006 | // the conditional layer, bypassing the `parse_lhs_expression` guard. |
| 3007 | let body = |
| 3008 | if let Some(body) = self.with_recursion(Self::parse_conditional_expression_or_higher) { |
| 3009 | body |
| 3010 | } else { |
| 3011 | self.report_recursion_limit_exceeded(self.current_token_range()); |
| 3012 | self.recursion_recovery_expr() |
| 3013 | }; |
| 3014 | |
| 3015 | ast::ExprLambda { |
| 3016 | body: Box::new(body.expr), |
| 3017 | parameters, |
| 3018 | range: self.node_range(start), |
| 3019 | node_index: AtomicNodeIndex::NONE, |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | /// Parses an `if` expression. |
| 3024 | /// |
no test coverage detected