aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-01-28 19:59:18 +0000
committerAleksey Kladov <[email protected]>2018-01-28 19:59:18 +0000
commitb7c141e0b5c66c51db7171b1de30d4c60d1048c0 (patch)
treef73695cfca44f775f913391084c9079add4a041e
parentab81e4c7b4383a153145752059be59e17571fad3 (diff)
Parse enums
-rw-r--r--grammar.ron2
-rw-r--r--src/parser/event_parser/grammar/expressions.rs6
-rw-r--r--src/parser/event_parser/grammar/items/mod.rs6
-rw-r--r--src/parser/event_parser/grammar/items/structs.rs34
-rw-r--r--src/syntax_kinds.rs4
-rw-r--r--tests/data/parser/ok/0019_enums.rs25
-rw-r--r--tests/data/parser/ok/0019_enums.txt118
7 files changed, 194 insertions, 1 deletions
diff --git a/grammar.ron b/grammar.ron
index 7c76d595d..d7fea44fb 100644
--- a/grammar.ron
+++ b/grammar.ron
@@ -74,6 +74,8 @@ Grammar(
74 nodes: [ 74 nodes: [
75 "FILE", 75 "FILE",
76 "STRUCT_ITEM", 76 "STRUCT_ITEM",
77 "ENUM_ITEM",
78 "ENUM_VARIANT",
77 "NAMED_FIELD", 79 "NAMED_FIELD",
78 "POS_FIELD", 80 "POS_FIELD",
79 "FN_ITEM", 81 "FN_ITEM",
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 {
12 _ => false, 12 _ => false,
13 } 13 }
14} 14}
15
16pub(super) fn expr(p: &mut Parser) {
17 if !literal(p) {
18 p.error().message("expected expression").emit();
19 }
20}
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) {
10} 10}
11 11
12pub(super) const ITEM_FIRST: TokenSet = 12pub(super) const ITEM_FIRST: TokenSet =
13 token_set![EXTERN_KW, MOD_KW, USE_KW, STRUCT_KW, FN_KW, PUB_KW, POUND,]; 13 token_set![EXTERN_KW, MOD_KW, USE_KW, STRUCT_KW, ENUM_KW, FN_KW, PUB_KW, POUND];
14 14
15fn item(p: &mut Parser) { 15fn item(p: &mut Parser) {
16 let item = p.start(); 16 let item = p.start();
@@ -34,6 +34,10 @@ fn item(p: &mut Parser) {
34 structs::struct_item(p); 34 structs::struct_item(p);
35 STRUCT_ITEM 35 STRUCT_ITEM
36 } 36 }
37 ENUM_KW => {
38 structs::enum_item(p);
39 ENUM_ITEM
40 }
37 FN_KW => { 41 FN_KW => {
38 fn_item(p); 42 fn_item(p);
39 FN_ITEM 43 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) {
40 } 40 }
41} 41}
42 42
43pub(super) fn enum_item(p: &mut Parser) {
44 assert!(p.at(ENUM_KW));
45 p.bump();
46 p.expect(IDENT);
47 type_param_list(p);
48 where_clause(p);
49 if p.expect(L_CURLY) {
50 while !p.at(EOF) && !p.at(R_CURLY) {
51 let var = p.start();
52 attributes::outer_attributes(p);
53 if p.at(IDENT) {
54 p.bump();
55 match p.current() {
56 L_CURLY => named_fields(p),
57 L_PAREN => pos_fields(p),
58 EQ => {
59 p.bump();
60 expressions::expr(p);
61 }
62 _ => ()
63 }
64 var.complete(p, ENUM_VARIANT);
65 } else {
66 var.abandon(p);
67 p.err_and_bump("expected enum variant");
68 }
69 if !p.at(R_CURLY) {
70 p.expect(COMMA);
71 }
72 }
73 p.expect(R_CURLY);
74 }
75}
76
43fn named_fields(p: &mut Parser) { 77fn named_fields(p: &mut Parser) {
44 assert!(p.at(L_CURLY)); 78 assert!(p.at(L_CURLY));
45 p.bump(); 79 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 {
76 SHEBANG, 76 SHEBANG,
77 FILE, 77 FILE,
78 STRUCT_ITEM, 78 STRUCT_ITEM,
79 ENUM_ITEM,
80 ENUM_VARIANT,
79 NAMED_FIELD, 81 NAMED_FIELD,
80 POS_FIELD, 82 POS_FIELD,
81 FN_ITEM, 83 FN_ITEM,
@@ -176,6 +178,8 @@ impl SyntaxKind {
176 SHEBANG => &SyntaxInfo { name: "SHEBANG" }, 178 SHEBANG => &SyntaxInfo { name: "SHEBANG" },
177 FILE => &SyntaxInfo { name: "FILE" }, 179 FILE => &SyntaxInfo { name: "FILE" },
178 STRUCT_ITEM => &SyntaxInfo { name: "STRUCT_ITEM" }, 180 STRUCT_ITEM => &SyntaxInfo { name: "STRUCT_ITEM" },
181 ENUM_ITEM => &SyntaxInfo { name: "ENUM_ITEM" },
182 ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" },
179 NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" }, 183 NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" },
180 POS_FIELD => &SyntaxInfo { name: "POS_FIELD" }, 184 POS_FIELD => &SyntaxInfo { name: "POS_FIELD" },
181 FN_ITEM => &SyntaxInfo { name: "FN_ITEM" }, 185 FN_ITEM => &SyntaxInfo { name: "FN_ITEM" },
diff --git a/tests/data/parser/ok/0019_enums.rs b/tests/data/parser/ok/0019_enums.rs
new file mode 100644
index 000000000..7a1afa0e6
--- /dev/null
+++ b/tests/data/parser/ok/0019_enums.rs
@@ -0,0 +1,25 @@
1enum E1 {
2}
3
4enum E2<T> {
5}
6
7enum E3 {
8 X
9}
10
11enum E4 {
12 X,
13}
14
15enum E5 {
16 A,
17 B = 92,
18 C {
19 a: u32,
20 pub b: f64,
21 },
22 F {},
23 D(u32,),
24 E(),
25}
diff --git a/tests/data/parser/ok/0019_enums.txt b/tests/data/parser/ok/0019_enums.txt
new file mode 100644
index 000000000..a864f72c7
--- /dev/null
+++ b/tests/data/parser/ok/0019_enums.txt
@@ -0,0 +1,118 @@
1FILE@[0; 182)
2 ENUM_ITEM@[0; 13)
3 ENUM_KW@[0; 4)
4 WHITESPACE@[4; 5)
5 IDENT@[5; 7)
6 WHITESPACE@[7; 8)
7 L_CURLY@[8; 9)
8 WHITESPACE@[9; 10)
9 R_CURLY@[10; 11)
10 WHITESPACE@[11; 13)
11 ENUM_ITEM@[13; 29)
12 ENUM_KW@[13; 17)
13 WHITESPACE@[17; 18)
14 IDENT@[18; 20)
15 TYPE_PARAM_LIST@[20; 24)
16 L_ANGLE@[20; 21)
17 TYPE_PARAM@[21; 22)
18 IDENT@[21; 22)
19 R_ANGLE@[22; 23)
20 WHITESPACE@[23; 24)
21 L_CURLY@[24; 25)
22 WHITESPACE@[25; 26)
23 R_CURLY@[26; 27)
24 WHITESPACE@[27; 29)
25 ENUM_ITEM@[29; 48)
26 ENUM_KW@[29; 33)
27 WHITESPACE@[33; 34)
28 IDENT@[34; 36)
29 WHITESPACE@[36; 37)
30 L_CURLY@[37; 38)
31 ENUM_VARIANT@[38; 45)
32 WHITESPACE@[38; 43)
33 IDENT@[43; 44)
34 WHITESPACE@[44; 45)
35 R_CURLY@[45; 46)
36 WHITESPACE@[46; 48)
37 ENUM_ITEM@[48; 68)
38 ENUM_KW@[48; 52)
39 WHITESPACE@[52; 53)
40 IDENT@[53; 55)
41 WHITESPACE@[55; 56)
42 L_CURLY@[56; 57)
43 ENUM_VARIANT@[57; 63)
44 WHITESPACE@[57; 62)
45 IDENT@[62; 63)
46 COMMA@[63; 64)
47 WHITESPACE@[64; 65)
48 R_CURLY@[65; 66)
49 WHITESPACE@[66; 68)
50 ENUM_ITEM@[68; 182)
51 ENUM_KW@[68; 72)
52 WHITESPACE@[72; 73)
53 IDENT@[73; 75)
54 WHITESPACE@[75; 76)
55 L_CURLY@[76; 77)
56 ENUM_VARIANT@[77; 83)
57 WHITESPACE@[77; 82)
58 IDENT@[82; 83)
59 COMMA@[83; 84)
60 ENUM_VARIANT@[84; 95)
61 WHITESPACE@[84; 89)
62 IDENT@[89; 90)
63 WHITESPACE@[90; 91)
64 EQ@[91; 92)
65 LITERAL@[92; 95)
66 WHITESPACE@[92; 93)
67 INT_NUMBER@[93; 95)
68 COMMA@[95; 96)
69 ENUM_VARIANT@[96; 146)
70 WHITESPACE@[96; 101)
71 IDENT@[101; 102)
72 WHITESPACE@[102; 103)
73 L_CURLY@[103; 104)
74 NAMED_FIELD@[104; 119)
75 WHITESPACE@[104; 113)
76 IDENT@[113; 114)
77 COLON@[114; 115)
78 WHITESPACE@[115; 116)
79 IDENT@[116; 119)
80 COMMA@[119; 120)
81 NAMED_FIELD@[120; 139)
82 VISIBILITY@[120; 133)
83 WHITESPACE@[120; 129)
84 PUB_KW@[129; 132)
85 WHITESPACE@[132; 133)
86 IDENT@[133; 134)
87 COLON@[134; 135)
88 WHITESPACE@[135; 136)
89 IDENT@[136; 139)
90 COMMA@[139; 140)
91 WHITESPACE@[140; 145)
92 R_CURLY@[145; 146)
93 COMMA@[146; 147)
94 ENUM_VARIANT@[147; 156)
95 WHITESPACE@[147; 152)
96 IDENT@[152; 153)
97 WHITESPACE@[153; 154)
98 L_CURLY@[154; 155)
99 R_CURLY@[155; 156)
100 COMMA@[156; 157)
101 ENUM_VARIANT@[157; 169)
102 WHITESPACE@[157; 162)
103 IDENT@[162; 163)
104 L_PAREN@[163; 164)
105 POS_FIELD@[164; 167)
106 IDENT@[164; 167)
107 COMMA@[167; 168)
108 R_PAREN@[168; 169)
109 COMMA@[169; 170)
110 ENUM_VARIANT@[170; 178)
111 WHITESPACE@[170; 175)
112 IDENT@[175; 176)
113 L_PAREN@[176; 177)
114 R_PAREN@[177; 178)
115 COMMA@[178; 179)
116 WHITESPACE@[179; 180)
117 R_CURLY@[180; 181)
118 WHITESPACE@[181; 182)