@@ -31,6 +31,7 @@ struct Parser {
3131 Result<ast::Node*, Error> parse_extern ();
3232 Result<ast::Node*, Error> parse_link_with ();
3333 Result<ast::Node*, Error> parse_type_definition ();
34+ Result<Ok, Error> parse_type_definition_body (ast::TypeNode* node);
3435 Result<ast::Type, Error> parse_type ();
3536 Result<ast::Node*, Error> parse_interface ();
3637 Result<ast::Node*, Error> parse_statement ();
@@ -870,13 +871,26 @@ Result<ast::Node*, Error> Parser::parse_type_definition() {
870871 if (identifier.is_error ()) return identifier;
871872 type.identifier = (ast::IdentifierNode*) identifier.get_value ();
872873
874+ // Parse body
875+ auto body = this ->parse_type_definition_body (&type);
876+ if (body.is_error ()) return body.get_error ();
877+
878+ // return
879+ this ->ast .push_back (type);
880+ return this ->ast .last_element ();
881+ }
882+
883+ // type_definition_body → (("\n"+ IDENTIFIER ": " type)|(CASE IDENTIFIER type_defintion_body))*
884+ Result<Ok, Error> Parser::parse_type_definition_body (ast::TypeNode* node) {
873885 // Set new indentation level
886+ size_t backup = this ->position ;
874887 this ->advance_until_next_statement ();
875888 size_t previous = this ->current_indentation ();
876889 this ->indentation_level .push_back (this ->current ().column );
877890 if (previous >= this ->current_indentation ()) {
878- this ->errors .push_back (errors::expecting_new_indentation_level (this ->location ())); // tested in errors/expecting_new_indentation_level.dm
879- return Error {};
891+ this ->indentation_level .pop_back ();
892+ this ->position = backup;
893+ return Ok{};
880894 }
881895
882896 while (!this ->at_end ()) {
@@ -895,28 +909,47 @@ Result<ast::Node*, Error> Parser::parse_type_definition() {
895909 return Error {};
896910 }
897911
898- // Parse field
899- auto field = this ->parse_identifier ();
900- if (field.is_error ()) return Error {};
912+ if (this ->current () == token::Identifier) {
913+ // Parse field
914+ auto field = this ->parse_identifier ();
915+ if (field.is_error ()) return Error {};
901916
902- // Parse colon
903- auto colon = this ->parse_token (token::Colon);
904- if (colon.is_error ()) return Error {};
917+ // Parse colon
918+ auto colon = this ->parse_token (token::Colon);
919+ if (colon.is_error ()) return Error {};
905920
906- auto type_annotation = this ->parse_type ();
907- if (type_annotation.is_error ()) return Error {};
921+ auto type_annotation = this ->parse_type ();
922+ if (type_annotation.is_error ()) return Error {};
908923
909- ast::set_type (field.get_value (), type_annotation.get_value ());
910-
911- this ->ast .push_back (*field.get_value ());
912- type.fields .push_back ((ast::IdentifierNode*) this ->ast .last_element ());
924+ ast::set_type (field.get_value (), type_annotation.get_value ());
925+ node->fields .push_back ((ast::IdentifierNode*) field.get_value ());
926+ }
927+ else if (this ->current () == token::Case) {
928+ // Parse keyword
929+ auto keyword = this ->parse_token (token::Case);
930+ if (keyword.is_error ()) return Error {};
931+
932+ // Parse identifier
933+ auto identifier = this ->parse_identifier ();
934+ if (identifier.is_error ()) return Error {};
935+
936+ // Parse body
937+ ast::TypeNode new_case = ast::TypeNode{this ->location ().line , this ->location ().column };
938+ this ->ast .push_back (new_case);
939+ node->cases .push_back ((ast::TypeNode*) this ->ast .last_element ());
940+ node->cases [node->cases .size () - 1 ]->identifier = (ast::IdentifierNode*) identifier.get_value ();
941+ auto body = this ->parse_type_definition_body (node->cases [node->cases .size () - 1 ]);
942+ if (body.is_error ()) return body.get_error ();
943+ }
944+ else {
945+ assert (false );
946+ }
913947 }
914948
915949 // Pop indentation level
916950 this ->indentation_level .pop_back ();
917951
918- this ->ast .push_back (type);
919- return this ->ast .last_element ();
952+ return Ok{};
920953}
921954
922955// type → IDENTIFIER ("[" type (", " type)* "]")*
0 commit comments