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

Method parse_lambda_expr

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

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

(&mut self)

Source from the content-addressed store, hash-verified

2959 ///
2960 /// See: <https://docs.python.org/3/reference/expressions.html#lambda>
2961 fn parse_lambda_expr(&mut self) -> ast::ExprLambda {
2962 let start = self.node_start();
2963 self.bump(TokenKind::Lambda);
2964
2965 let parameters = if self.at(TokenKind::Colon) {
2966 // test_ok lambda_with_no_parameters
2967 // lambda: 1
2968 None
2969 } else {
2970 Some(Box::new(self.parse_parameters(FunctionKind::Lambda)))
2971 };
2972
2973 self.expect(TokenKind::Colon);
2974
2975 // test_ok lambda_with_valid_body
2976 // lambda x: x
2977 // lambda x: x if True else y
2978 // lambda x: await x
2979 // lambda x: lambda y: x + y
2980 // lambda x: (yield x) # Parenthesized `yield` is fine
2981 // lambda x: x, *y
2982
2983 // test_err lambda_body_with_starred_expr
2984 // lambda x: *y
2985 // lambda x: *y,
2986 // lambda x: *y, z
2987 // lambda x: *y and z
2988
2989 // test_err lambda_body_with_yield_expr
2990 // lambda x: yield y
2991 // lambda x: yield from y
2992
2993 // `lambda: lambda: lambda: ...` recurses through the lambda body at
2994 // the conditional layer, bypassing the `parse_lhs_expression` guard.
2995 let body =
2996 if let Some(body) = self.with_recursion(Self::parse_conditional_expression_or_higher) {
2997 body
2998 } else {
2999 self.report_recursion_limit_exceeded(self.current_token_range());
3000 self.recursion_recovery_expr()
3001 };
3002
3003 ast::ExprLambda {
3004 body: Box::new(body.expr),
3005 parameters,
3006 range: self.node_range(start),
3007 node_index: AtomicNodeIndex::NONE,
3008 }
3009 }
3010
3011 /// Parses an `if` expression.
3012 ///

Calls 10

node_startMethod · 0.80
parse_parametersMethod · 0.80
expectMethod · 0.80
with_recursionMethod · 0.80
current_token_rangeMethod · 0.80
node_rangeMethod · 0.80
bumpMethod · 0.45
atMethod · 0.45

Tested by

no test coverage detected