More type name stuff - compiling!

This commit is contained in:
greg 2020-03-12 04:02:26 -07:00
parent 78b86b3531
commit ad0434007b

View File

@ -457,8 +457,10 @@ fn formal_params(text: &str) -> ParseResult<Vec<FormalParam>> {
}
fn formal_param(text: &str) -> ParseResult<FormalParam> {
let p = tuple((identifier, opt(type_anno)));
//TODO handle default arg
let default = opt(preceded(ws(tag("=")), expression));
let p = tuple((ws(identifier), opt(ws(type_anno)), default));
map(p, |(name, anno, default)|
FormalParam { name, anno, default })(text)
}
fn type_declaration(text: &str) -> ParseResult<Declaration> {
@ -466,7 +468,7 @@ fn type_declaration(text: &str) -> ParseResult<Declaration> {
}
fn type_declaration_body(text: &str) -> ParseResult<Declaration> {
let t = tuple((opt(tag("mut"))), ws(type_singleton_name), ws(tag("=")), ws(type_body));
let t = tuple((opt(tag("mut")), ws(type_singleton_name), ws(tag("=")), ws(type_body)));
alt((
preceded(tag("alias"), ws(type_alias)),
map(t, |(mut_kw, name, _, body)| {
@ -475,10 +477,29 @@ fn type_declaration_body(text: &str) -> ParseResult<Declaration> {
))(text)
}
fn type_body(text: &str) -> ParseResult<TypeBody> {
let p = separated_nonempty_list(ws(tag("|")), variant_specifier);
map(p, TypeBody)(text)
}
fn variant_specifier(text: &str) -> ParseResult<Variant> {
use self::Variant::*;
let tuple_struct =
delimited(tag("("), separated_nonempty_list(ws(tag(",")), type_name), ws(tag(")")));
//TODO record
let p = tuple((identifier, opt(tuple_struct)));
map(p, |(name, maybe_tuple_members)| match maybe_tuple_members {
Some(members) => TupleStruct(name, members),
None => UnitStruct(name),
})(text)
}
fn type_singleton_name(text: &str) -> ParseResult<TypeSingletonName> {
tuple((identifier, opt(delimited(tag("<"),
let p = tuple((identifier, opt(delimited(tag("<"),
separated_nonempty_list(tag(","), ws(type_name)),
tag(">")))))(text)
tag(">")))));
map(p, |(name, params)| TypeSingletonName { name, params: params.unwrap_or(vec![]) })(text)
}
fn type_alias(text: &str) -> ParseResult<Declaration> {