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/expressions.rs | 6 +++++ src/parser/event_parser/grammar/items/mod.rs | 6 ++++- src/parser/event_parser/grammar/items/structs.rs | 34 ++++++++++++++++++++++++ src/syntax_kinds.rs | 4 +++ 4 files changed, 49 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/parser/event_parser/grammar/expressions.rs b/src/parser/event_parser/grammar/expressions.rs index c81dc6c35..8caaf3553 100644 --- a/src/parser/event_parser/grammar/expressions.rs +++ b/src/parser/event_parser/grammar/expressions.rs @@ -12,3 +12,9 @@ pub(super) fn literal(p: &mut Parser) -> bool { _ => false, } } + +pub(super) fn expr(p: &mut Parser) { + if !literal(p) { + p.error().message("expected expression").emit(); + } +} 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(); diff --git a/src/syntax_kinds.rs b/src/syntax_kinds.rs index b6c281cd5..519326f48 100644 --- a/src/syntax_kinds.rs +++ b/src/syntax_kinds.rs @@ -76,6 +76,8 @@ pub enum SyntaxKind { SHEBANG, FILE, STRUCT_ITEM, + ENUM_ITEM, + ENUM_VARIANT, NAMED_FIELD, POS_FIELD, FN_ITEM, @@ -176,6 +178,8 @@ impl SyntaxKind { SHEBANG => &SyntaxInfo { name: "SHEBANG" }, FILE => &SyntaxInfo { name: "FILE" }, STRUCT_ITEM => &SyntaxInfo { name: "STRUCT_ITEM" }, + ENUM_ITEM => &SyntaxInfo { name: "ENUM_ITEM" }, + ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" }, NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" }, POS_FIELD => &SyntaxInfo { name: "POS_FIELD" }, FN_ITEM => &SyntaxInfo { name: "FN_ITEM" }, -- cgit v1.2.3