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:2893–2934  ·  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

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

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