diff options
Diffstat (limited to 'src/grammar/items/structs.rs')
-rw-r--r-- | src/grammar/items/structs.rs | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/grammar/items/structs.rs b/src/grammar/items/structs.rs new file mode 100644 index 000000000..7ced542a4 --- /dev/null +++ b/src/grammar/items/structs.rs | |||
@@ -0,0 +1,116 @@ | |||
1 | use super::*; | ||
2 | |||
3 | pub(super) fn struct_item(p: &mut Parser) { | ||
4 | assert!(p.at(STRUCT_KW)); | ||
5 | p.bump(); | ||
6 | |||
7 | name(p); | ||
8 | type_params::type_param_list(p); | ||
9 | match p.current() { | ||
10 | WHERE_KW => { | ||
11 | type_params::where_clause(p); | ||
12 | match p.current() { | ||
13 | SEMI => { | ||
14 | p.bump(); | ||
15 | return; | ||
16 | } | ||
17 | L_CURLY => named_fields(p), | ||
18 | _ => { | ||
19 | //TODO: special case `(` error message | ||
20 | p.error("expected `;` or `{`"); | ||
21 | return; | ||
22 | } | ||
23 | } | ||
24 | } | ||
25 | SEMI => { | ||
26 | p.bump(); | ||
27 | return; | ||
28 | } | ||
29 | L_CURLY => named_fields(p), | ||
30 | L_PAREN => { | ||
31 | pos_fields(p); | ||
32 | p.expect(SEMI); | ||
33 | } | ||
34 | _ => { | ||
35 | p.error("expected `;`, `{`, or `(`"); | ||
36 | return; | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | |||
41 | pub(super) fn enum_item(p: &mut Parser) { | ||
42 | assert!(p.at(ENUM_KW)); | ||
43 | p.bump(); | ||
44 | name(p); | ||
45 | type_params::type_param_list(p); | ||
46 | type_params::where_clause(p); | ||
47 | if p.expect(L_CURLY) { | ||
48 | while !p.at(EOF) && !p.at(R_CURLY) { | ||
49 | let var = p.start(); | ||
50 | attributes::outer_attributes(p); | ||
51 | if p.at(IDENT) { | ||
52 | p.bump(); | ||
53 | match p.current() { | ||
54 | L_CURLY => named_fields(p), | ||
55 | L_PAREN => pos_fields(p), | ||
56 | EQ => { | ||
57 | p.bump(); | ||
58 | expressions::expr(p); | ||
59 | } | ||
60 | _ => (), | ||
61 | } | ||
62 | var.complete(p, ENUM_VARIANT); | ||
63 | } else { | ||
64 | var.abandon(p); | ||
65 | p.err_and_bump("expected enum variant"); | ||
66 | } | ||
67 | if !p.at(R_CURLY) { | ||
68 | p.expect(COMMA); | ||
69 | } | ||
70 | } | ||
71 | p.expect(R_CURLY); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | fn named_fields(p: &mut Parser) { | ||
76 | assert!(p.at(L_CURLY)); | ||
77 | p.bump(); | ||
78 | while !p.at(R_CURLY) && !p.at(EOF) { | ||
79 | named_field(p); | ||
80 | if !p.at(R_CURLY) { | ||
81 | p.expect(COMMA); | ||
82 | } | ||
83 | } | ||
84 | p.expect(R_CURLY); | ||
85 | |||
86 | fn named_field(p: &mut Parser) { | ||
87 | let field = p.start(); | ||
88 | visibility(p); | ||
89 | if p.at(IDENT) { | ||
90 | name(p); | ||
91 | p.expect(COLON); | ||
92 | types::type_(p); | ||
93 | field.complete(p, NAMED_FIELD); | ||
94 | } else { | ||
95 | field.abandon(p); | ||
96 | p.err_and_bump("expected field declaration"); | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | |||
101 | fn pos_fields(p: &mut Parser) { | ||
102 | if !p.expect(L_PAREN) { | ||
103 | return; | ||
104 | } | ||
105 | while !p.at(R_PAREN) && !p.at(EOF) { | ||
106 | let pos_field = p.start(); | ||
107 | visibility(p); | ||
108 | types::type_(p); | ||
109 | pos_field.complete(p, POS_FIELD); | ||
110 | |||
111 | if !p.at(R_PAREN) { | ||
112 | p.expect(COMMA); | ||
113 | } | ||
114 | } | ||
115 | p.expect(R_PAREN); | ||
116 | } | ||