From c605f76059005ae78e58514300e520b7665dab24 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 8 Oct 2017 19:02:52 -0700 Subject: [PATCH] More type work II --- src/schala_lang/parsing.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index 4d34761..2ea2f3d 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -283,8 +283,9 @@ type_declaration_body := 'alias' type_alias | IDENTIFIER '=' type_body type_alias := IDENTIFIER '=' type_name type_body := variant_specifier ('|' variant_specifier)* -variant_specifier := IDENTIFIER | IDENTIFIER '{' member_list '}' | IDENTIFIER '(' type_name* ')' -member_list := (IDENTIFIER type_anno)* +variant_specifier := IDENTIFIER | IDENTIFIER '{' typed_identifier_list '}' | IDENTIFIER '(' type_name* ')' +typed_identifier_list := typed_identifier* +typed_identifier := IDENTIFIER type_anno func_declaration := 'fn' IDENTIFIER '(' param_list ')' param_list := (IDENTIFIER type_anno+ ',')* @@ -432,7 +433,7 @@ pub struct TypeBody(pub Vec); #[derive(Debug, PartialEq, Clone)] pub enum Variant { Singleton(Rc), - ArgumentConstructor(Rc, Vec), + TupleStruct(Rc, Vec), Record(Rc, Vec<(Rc, TypeAnno)>), } @@ -619,7 +620,27 @@ impl Parser { }); parse_method!(variant_specifier(&mut self) -> ParseResult { - Ok(Variant::Singleton(self.identifier()?)) + use self::Variant::*; + + let name = self.identifier()?; + match self.peek() { + LParen => { + let tuple_members = delimited!(self, LParen, '(', type_name, Comma, RParen, ')'); + Ok(TupleStruct(name, tuple_members)) + }, + LCurlyBrace => { + let typed_identifier_list = delimited!(self, LCurlyBrace, '{', typed_identifier, Comma, RCurlyBrace, '}'); + Ok(Record(name, typed_identifier_list)) + }, + _ => Ok(Singleton(name)) + } + }); + + parse_method!(typed_identifier(&mut self) -> ParseResult<(Rc, TypeAnno)> { + let identifier = self.identifier()?; + expect!(self, Colon, "':'"); + let type_name = self.type_name()?; + Ok((identifier, type_name)) }); parse_method!(func_declaration(&mut self) -> ParseResult {