-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: split lambda functions into a separate file
- Loading branch information
1 parent
7774b50
commit def29f3
Showing
2 changed files
with
47 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use chumsky::{primitive::just, Parser}; | ||
|
||
use crate::{ | ||
parser::{labels::ParsingRuleLabel, parameter_name_recovery, parameter_recovery, NoirParser}, | ||
token::Token, | ||
Expression, ExpressionKind, Lambda, Pattern, UnresolvedType, | ||
}; | ||
|
||
use super::{parse_type, pattern}; | ||
|
||
pub(super) fn lambda<'a>( | ||
expr_parser: impl NoirParser<Expression> + 'a, | ||
) -> impl NoirParser<ExpressionKind> + 'a { | ||
lambda_parameters() | ||
.delimited_by(just(Token::Pipe), just(Token::Pipe)) | ||
.then(lambda_return_type()) | ||
.then(expr_parser) | ||
.map(|((parameters, return_type), body)| { | ||
ExpressionKind::Lambda(Box::new(Lambda { parameters, return_type, body })) | ||
}) | ||
} | ||
|
||
fn lambda_parameters() -> impl NoirParser<Vec<(Pattern, UnresolvedType)>> { | ||
let typ = parse_type().recover_via(parameter_recovery()); | ||
let typ = just(Token::Colon).ignore_then(typ); | ||
|
||
let parameter = pattern() | ||
.recover_via(parameter_name_recovery()) | ||
.then(typ.or_not().map(|typ| typ.unwrap_or_else(UnresolvedType::unspecified))); | ||
|
||
parameter | ||
.separated_by(just(Token::Comma)) | ||
.allow_trailing() | ||
.labelled(ParsingRuleLabel::Parameter) | ||
} | ||
|
||
fn lambda_return_type() -> impl NoirParser<UnresolvedType> { | ||
just(Token::Arrow) | ||
.ignore_then(parse_type()) | ||
.or_not() | ||
.map(|ret| ret.unwrap_or_else(UnresolvedType::unspecified)) | ||
} |