Tuple types

This commit is contained in:
Alex Bethel 2022-08-07 12:58:44 -05:00
parent e56a56693c
commit 02134f542c

View file

@ -4,6 +4,7 @@ use std::{error::Error, fmt::Display};
use chumsky::{ use chumsky::{
prelude::{choice, end, just, none_of, one_of, Simple}, prelude::{choice, end, just, none_of, one_of, Simple},
recursive::recursive,
text::{ident, int, keyword, whitespace}, text::{ident, int, keyword, whitespace},
Parser, Parser,
}; };
@ -483,14 +484,19 @@ 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>> {
parse_named_type(m).repeated().at_least(1).map(|types| { recursive(|rec| {
types choice((parse_named_type(m), parse_tuple_type(m, rec)))
.into_iter() .repeated()
.reduce(|l, r| Type::Application { .at_least(1)
function: Box::new(l), .map(|types| {
expression: Box::new(r), types
.into_iter()
.reduce(|l, r| Type::Application {
function: Box::new(l),
expression: Box::new(r),
})
.unwrap()
}) })
.unwrap()
}) })
} }
@ -498,6 +504,22 @@ fn parse_named_type(_m: &ParserMeta) -> impl Parser<char, Type, Error = Simple<c
ident().then_ignore(whitespace()).map(Type::Named) ident().then_ignore(whitespace()).map(Type::Named)
} }
fn parse_tuple_type(
_m: &ParserMeta,
rec: impl Parser<char, Type, Error = Simple<char>>,
) -> impl Parser<char, Type, Error = Simple<char>> {
rec.separated_by(just(',').then(whitespace()))
.allow_trailing()
.delimited_by(just('(').then(whitespace()), just(')').then(whitespace()))
.map(|types| {
if types.len() == 1 {
types.into_iter().next().unwrap()
} else {
Type::Tuple(types)
}
})
}
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),))