| 483 | } |
| 484 | |
| 485 | fn lambda_func<'a, I, E>(expr: E) -> impl Parser<'a, I, Expr, ParserError<'a>> + Clone + 'a |
| 486 | where |
| 487 | I: Input<'a, Token = lr::Token, Span = Span> + BorrowInput<'a>, |
| 488 | E: Parser<'a, I, Expr, ParserError<'a>> + Clone + 'a, |
| 489 | { |
| 490 | let param = ident_part() |
| 491 | .then(type_expr().delimited_by(ctrl('<'), ctrl('>')).or_not()) |
| 492 | .then(ctrl(':').ignore_then(expr.clone().map(Box::new)).or_not()); |
| 493 | |
| 494 | choice(( |
| 495 | // func |
| 496 | keyword("func").ignore_then( |
| 497 | param |
| 498 | .clone() |
| 499 | .separated_by(new_line().repeated()) |
| 500 | .allow_leading() |
| 501 | .allow_trailing() |
| 502 | .collect::<Vec<_>>(), |
| 503 | ), |
| 504 | // plain |
| 505 | param.repeated().collect(), |
| 506 | )) |
| 507 | .then_ignore(select_ref! { lr::Token { kind: TokenKind::ArrowThin, .. } => () }) |
| 508 | // return type |
| 509 | .then(type_expr().delimited_by(ctrl('<'), ctrl('>')).or_not()) |
| 510 | // body |
| 511 | .then(func_call(expr)) |
| 512 | .map(|((params, return_ty), body)| { |
| 513 | let (pos, name) = params |
| 514 | .into_iter() |
| 515 | .map(|((name, ty), default_value)| FuncParam { |
| 516 | name, |
| 517 | ty, |
| 518 | default_value, |
| 519 | }) |
| 520 | .partition(|p| p.default_value.is_none()); |
| 521 | |
| 522 | Box::new(Func { |
| 523 | params: pos, |
| 524 | named_params: name, |
| 525 | |
| 526 | body: Box::new(body), |
| 527 | return_ty, |
| 528 | }) |
| 529 | }) |
| 530 | .map(ExprKind::Func) |
| 531 | .map_with(|kind, extra| ExprKind::into_expr(kind, extra.span())) |
| 532 | .labelled("function definition") |
| 533 | .boxed() |
| 534 | } |
| 535 | |
| 536 | pub(crate) fn ident<'a, I>() -> impl Parser<'a, I, Ident, ParserError<'a>> + Clone |
| 537 | where |