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:2960–3008  ·  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

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

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