From b7c141e0b5c66c51db7171b1de30d4c60d1048c0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 28 Jan 2018 22:59:18 +0300 Subject: Parse enums --- src/parser/event_parser/grammar/items/mod.rs | 6 ++++- src/parser/event_parser/grammar/items/structs.rs | 34 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) (limited to 'src/parser/event_parser/grammar/items') diff --git a/src/parser/event_parser/grammar/items/mod.rs b/src/parser/event_parser/grammar/items/mod.rs index f10fb230b..886c02d37 100644 --- a/src/parser/event_parser/grammar/items/mod.rs +++ b/src/parser/event_parser/grammar/items/mod.rs @@ -10,7 +10,7 @@ pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { } pub(super) const ITEM_FIRST: TokenSet = - token_set![EXTERN_KW, MOD_KW, USE_KW, STRUCT_KW, FN_KW, PUB_KW, POUND,]; + token_set![EXTERN_KW, MOD_KW, USE_KW, STRUCT_KW, ENUM_KW, FN_KW, PUB_KW, POUND]; fn item(p: &mut Parser) { let item = p.start(); @@ -34,6 +34,10 @@ fn item(p: &mut Parser) { structs::struct_item(p); STRUCT_ITEM } + ENUM_KW => { + structs::enum_item(p); + ENUM_ITEM + } FN_KW => { fn_item(p); FN_ITEM diff --git a/src/parser/event_parser/grammar/items/structs.rs b/src/parser/event_parser/grammar/items/structs.rs index 0934f3d28..670406071 100644 --- a/src/parser/event_parser/grammar/items/structs.rs +++ b/src/parser/event_parser/grammar/items/structs.rs @@ -40,6 +40,40 @@ pub(super) fn struct_item(p: &mut Parser) { } } +pub(super) fn enum_item(p: &mut Parser) { + assert!(p.at(ENUM_KW)); + p.bump(); + p.expect(IDENT); + type_param_list(p); + where_clause(p); + if p.expect(L_CURLY) { + while !p.at(EOF) && !p.at(R_CURLY) { + let var = p.start(); + attributes::outer_attributes(p); + if p.at(IDENT) { + p.bump(); + match p.current() { + L_CURLY => named_fields(p), + L_PAREN => pos_fields(p), + EQ => { + p.bump(); + expressions::expr(p); + } + _ => () + } + var.complete(p, ENUM_VARIANT); + } else { + var.abandon(p); + p.err_and_bump("expected enum variant"); + } + if !p.at(R_CURLY) { + p.expect(COMMA); + } + } + p.expect(R_CURLY); + } +} + fn named_fields(p: &mut Parser) { assert!(p.at(L_CURLY)); p.bump(); -- cgit v1.2.3