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:2885–2926  ·  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

2883 ///
2884 /// See: <https://docs.python.org/3/reference/expressions.html#lambda>
2885 fn parse_lambda_expr(&mut self) -> ast::ExprLambda {
2886 let start = self.node_start();
2887 self.bump(TokenKind::Lambda);
2888
2889 let parameters = if self.at(TokenKind::Colon) {
2890 // test_ok lambda_with_no_parameters
2891 // lambda: 1
2892 None
2893 } else {
2894 Some(Box::new(self.parse_parameters(FunctionKind::Lambda)))
2895 };
2896
2897 self.expect(TokenKind::Colon);
2898
2899 // test_ok lambda_with_valid_body
2900 // lambda x: x
2901 // lambda x: x if True else y
2902 // lambda x: await x
2903 // lambda x: lambda y: x + y
2904 // lambda x: (yield x) # Parenthesized `yield` is fine
2905 // lambda x: x, *y
2906
2907 // test_err lambda_body_with_starred_expr
2908 // lambda x: *y
2909 // lambda x: *y,
2910 // lambda x: *y, z
2911 // lambda x: *y and z
2912
2913 // test_err lambda_body_with_yield_expr
2914 // lambda x: yield y
2915 // lambda x: yield from y
2916
2917 // Lambda bodies recurse through the conditional layer without entering the binary parser.
2918 let body = self.with_recursion(Self::parse_conditional_expression_or_higher);
2919
2920 ast::ExprLambda {
2921 body: Box::new(body.expr),
2922 parameters,
2923 range: self.node_range(start),
2924 node_index: AtomicNodeIndex::NONE,
2925 }
2926 }
2927
2928 /// Parses an `if` expression.
2929 ///

Calls 7

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

Tested by

no test coverage detected