Record types

This commit is contained in:
Alex Bethel 2022-08-07 13:03:09 -05:00
parent 02134f542c
commit 00a9ac7cf7

View file

@ -485,7 +485,11 @@ fn parse_literal(_m: &ParserMeta) -> impl Parser<char, Expr, Error = Simple<char
fn parse_type(m: &ParserMeta) -> impl Parser<char, Type, Error = Simple<char>> { fn parse_type(m: &ParserMeta) -> impl Parser<char, Type, Error = Simple<char>> {
recursive(|rec| { recursive(|rec| {
choice((parse_named_type(m), parse_tuple_type(m, rec))) choice((
parse_named_type(m),
parse_tuple_type(m, rec.clone()),
parse_record_type(m, rec.clone()),
))
.repeated() .repeated()
.at_least(1) .at_least(1)
.map(|types| { .map(|types| {
@ -513,6 +517,7 @@ fn parse_tuple_type(
.delimited_by(just('(').then(whitespace()), just(')').then(whitespace())) .delimited_by(just('(').then(whitespace()), just(')').then(whitespace()))
.map(|types| { .map(|types| {
if types.len() == 1 { if types.len() == 1 {
// `(Int)` is the same as `Int`
types.into_iter().next().unwrap() types.into_iter().next().unwrap()
} else { } else {
Type::Tuple(types) 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>> { fn parse_pattern(m: &ParserMeta) -> impl Parser<char, Pattern, Error = Simple<char>> {
// TODO: add all the patterns // TODO: add all the patterns
choice((parse_capture_pattern(m),)) choice((parse_capture_pattern(m),))