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 From c23a2519e18ab1cd8c09c6b25d1c1c8ab9a1f3c1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 28 Jan 2018 23:13:32 +0300 Subject: Print identifiers in test data --- src/tree/mod.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/tree/mod.rs b/src/tree/mod.rs index 795f23f42..4131d855d 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -106,7 +106,19 @@ impl<'f> Node<'f> { impl<'f> fmt::Debug for Node<'f> { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{:?}@{:?}", self.kind(), self.range()) + write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; + if has_short_text(self.kind()) { + write!(fmt, " \"{}\"", self.text())?; + } + Ok(()) + } +} + +fn has_short_text(kind: SyntaxKind) -> bool { + use syntax_kinds::*; + match kind { + IDENT | LIFETIME => true, + _ => false } } -- cgit v1.2.3 From 55e2a478be04bdaba164ea92160e52a6fea5abe5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 28 Jan 2018 23:14:00 +0300 Subject: Reformat --- src/parser/event_parser/grammar/items/mod.rs | 5 +++-- src/parser/event_parser/grammar/items/structs.rs | 2 +- src/tree/mod.rs | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/parser/event_parser/grammar/items/mod.rs b/src/parser/event_parser/grammar/items/mod.rs index 886c02d37..0a50fffc1 100644 --- a/src/parser/event_parser/grammar/items/mod.rs +++ b/src/parser/event_parser/grammar/items/mod.rs @@ -9,8 +9,9 @@ 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, ENUM_KW, FN_KW, PUB_KW, POUND]; +pub(super) const ITEM_FIRST: TokenSet = 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(); diff --git a/src/parser/event_parser/grammar/items/structs.rs b/src/parser/event_parser/grammar/items/structs.rs index 670406071..6e438413b 100644 --- a/src/parser/event_parser/grammar/items/structs.rs +++ b/src/parser/event_parser/grammar/items/structs.rs @@ -59,7 +59,7 @@ pub(super) fn enum_item(p: &mut Parser) { p.bump(); expressions::expr(p); } - _ => () + _ => (), } var.complete(p, ENUM_VARIANT); } else { diff --git a/src/tree/mod.rs b/src/tree/mod.rs index 4131d855d..9ed0504c7 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -118,7 +118,7 @@ fn has_short_text(kind: SyntaxKind) -> bool { use syntax_kinds::*; match kind { IDENT | LIFETIME => true, - _ => false + _ => false, } } -- cgit v1.2.3