Record types

main
Alex Bethel 2022-08-07 13:03:09 -05:00
parent 02134f542c
commit 00a9ac7cf7
1 changed files with 32 additions and 12 deletions

View File

@ -485,18 +485,22 @@ fn parse_literal(_m: &ParserMeta) -> impl Parser<char, Expr, Error = Simple<char
fn parse_type(m: &ParserMeta) -> impl Parser<char, Type, Error = Simple<char>> {
recursive(|rec| {
choice((parse_named_type(m), parse_tuple_type(m, rec)))
.repeated()
.at_least(1)
.map(|types| {
types
.into_iter()
.reduce(|l, r| Type::Application {
function: Box::new(l),
expression: Box::new(r),
})
.unwrap()
})
choice((
parse_named_type(m),
parse_tuple_type(m, rec.clone()),
parse_record_type(m, rec.clone()),
))
.repeated()
.at_least(1)
.map(|types| {
types
.into_iter()
.reduce(|l, r| Type::Application {
function: Box::new(l),
expression: Box::new(r),
})
.unwrap()
})
})
}
@ -513,6 +517,7 @@ fn parse_tuple_type(
.delimited_by(just('(').then(whitespace()), just(')').then(whitespace()))
.map(|types| {
if types.len() == 1 {
// `(Int)` is the same as `Int`
types.into_iter().next().unwrap()
} else {
Type::Tuple(types)
@ -520,6 +525,21 @@ fn parse_tuple_type(
})
}
fn parse_record_type(
_m: &ParserMeta,
rec: impl Parser<char, Type, Error = Simple<char>>,
) -> impl Parser<char, Type, Error = Simple<char>> {
ident()
.then_ignore(whitespace())
.then_ignore(just(':'))
.then_ignore(whitespace())
.then(rec)
.separated_by(just(',').then(whitespace()))
.allow_trailing()
.delimited_by(just('{').then(whitespace()), just('}').then(whitespace()))
.map(Type::Record)
}
fn parse_pattern(m: &ParserMeta) -> impl Parser<char, Pattern, Error = Simple<char>> {
// TODO: add all the patterns
choice((parse_capture_pattern(m),))