From 00a9ac7cf735b6dd55696e26088cc8b619bd26e9 Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Sun, 7 Aug 2022 13:03:09 -0500 Subject: [PATCH] Record types --- axc/src/parser.rs | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/axc/src/parser.rs b/axc/src/parser.rs index 0b8dbc9..c007beb 100644 --- a/axc/src/parser.rs +++ b/axc/src/parser.rs @@ -485,18 +485,22 @@ fn parse_literal(_m: &ParserMeta) -> impl Parser impl Parser> { 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>, +) -> impl Parser> { + 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> { // TODO: add all the patterns choice((parse_capture_pattern(m),))