Parses a parameter list for the given function kind. See:
(&mut self, function_kind: FunctionKind)
| 3324 | /// |
| 3325 | /// See: <https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-parameter_list> |
| 3326 | pub(super) fn parse_parameters(&mut self, function_kind: FunctionKind) -> ast::Parameters { |
| 3327 | let start = self.node_start(); |
| 3328 | |
| 3329 | if matches!(function_kind, FunctionKind::FunctionDef) { |
| 3330 | self.expect(TokenKind::Lpar); |
| 3331 | } |
| 3332 | |
| 3333 | // TODO(dhruvmanila): This has the same problem as `parse_match_pattern_mapping` |
| 3334 | // has where if there are multiple kwarg or vararg, the last one will win and |
| 3335 | // the parser will drop the previous ones. Another thing is the vararg and kwarg |
| 3336 | // uses `Parameter` (not `ParameterWithDefault`) which means that the parser cannot |
| 3337 | // recover well from `*args=(1, 2)`. |
| 3338 | let mut parameters = ast::Parameters::default(); |
| 3339 | let parameters_snapshot = self.parameter_scratch.snapshot(); |
| 3340 | let mut args_snapshot = None; |
| 3341 | let mut kwonlyargs_snapshot = None; |
| 3342 | |
| 3343 | let mut seen_default_param = false; // `a=10` |
| 3344 | let mut seen_positional_only_separator = false; // `/` |
| 3345 | let mut seen_keyword_only_separator = false; // `*` |
| 3346 | let mut seen_keyword_only_param_after_separator = false; |
| 3347 | |
| 3348 | // Range of the keyword only separator if it's the last parameter in the list. |
| 3349 | let mut last_keyword_only_separator_range = None; |
| 3350 | |
| 3351 | self.parse_comma_separated_list(RecoveryContextKind::Parameters(function_kind), |parser| { |
| 3352 | let param_start = parser.node_start(); |
| 3353 | |
| 3354 | if parameters.kwarg.is_some() { |
| 3355 | // test_err params_follows_var_keyword_param |
| 3356 | // def foo(**kwargs, a, /, b=10, *, *args): ... |
| 3357 | parser.add_error( |
| 3358 | ParseErrorType::ParamAfterVarKeywordParam, |
| 3359 | parser.current_token_range(), |
| 3360 | ); |
| 3361 | } |
| 3362 | |
| 3363 | match parser.current_token_kind() { |
| 3364 | TokenKind::Star => { |
| 3365 | let star_range = parser.current_token_range(); |
| 3366 | parser.bump(TokenKind::Star); |
| 3367 | |
| 3368 | kwonlyargs_snapshot.get_or_insert_with(|| parser.parameter_scratch.snapshot()); |
| 3369 | |
| 3370 | if parser.at_identifier_or_soft_keyword() { |
| 3371 | let param = parser.parse_parameter( |
| 3372 | param_start, |
| 3373 | function_kind, |
| 3374 | AllowStarAnnotation::Yes, |
| 3375 | ); |
| 3376 | let param_star_range = parser.node_range(star_range.start()); |
| 3377 | |
| 3378 | if parser.at(TokenKind::Equal) { |
| 3379 | // test_err params_var_positional_with_default |
| 3380 | // def foo(a, *args=(1, 2)): ... |
| 3381 | parser.add_error( |
| 3382 | ParseErrorType::VarParameterWithDefault, |
| 3383 | parser.current_token_range(), |
no test coverage detected