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:2959–3007  ·  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

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

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