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:2963–3011  ·  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

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

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