diff options
Diffstat (limited to 'src/parser/event_parser/grammar/items')
-rw-r--r-- | src/parser/event_parser/grammar/items/mod.rs | 178 | ||||
-rw-r--r-- | src/parser/event_parser/grammar/items/structs.rs | 83 |
2 files changed, 261 insertions, 0 deletions
diff --git a/src/parser/event_parser/grammar/items/mod.rs b/src/parser/event_parser/grammar/items/mod.rs new file mode 100644 index 000000000..867776415 --- /dev/null +++ b/src/parser/event_parser/grammar/items/mod.rs | |||
@@ -0,0 +1,178 @@ | |||
1 | use super::*; | ||
2 | |||
3 | mod structs; | ||
4 | |||
5 | pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { | ||
6 | attributes::inner_attributes(p); | ||
7 | while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) { | ||
8 | item(p); | ||
9 | } | ||
10 | } | ||
11 | |||
12 | pub(super) const ITEM_FIRST: TokenSet = | ||
13 | token_set![EXTERN_KW, MOD_KW, USE_KW, STRUCT_KW, FN_KW, PUB_KW, POUND,]; | ||
14 | |||
15 | fn item(p: &mut Parser) { | ||
16 | let item = p.start(); | ||
17 | attributes::outer_attributes(p); | ||
18 | visibility(p); | ||
19 | let la = p.nth(1); | ||
20 | let item_kind = match p.current() { | ||
21 | EXTERN_KW if la == CRATE_KW => { | ||
22 | extern_crate_item(p); | ||
23 | EXTERN_CRATE_ITEM | ||
24 | } | ||
25 | MOD_KW => { | ||
26 | mod_item(p); | ||
27 | MOD_ITEM | ||
28 | } | ||
29 | USE_KW => { | ||
30 | use_item(p); | ||
31 | USE_ITEM | ||
32 | } | ||
33 | STRUCT_KW => { | ||
34 | structs::struct_item(p); | ||
35 | STRUCT_ITEM | ||
36 | } | ||
37 | FN_KW => { | ||
38 | fn_item(p); | ||
39 | FN_ITEM | ||
40 | } | ||
41 | L_CURLY => { | ||
42 | item.abandon(p); | ||
43 | error_block(p, "expected item"); | ||
44 | return; | ||
45 | } | ||
46 | err_token => { | ||
47 | item.abandon(p); | ||
48 | let message = if err_token == SEMI { | ||
49 | //TODO: if the item is incomplete, this message is misleading | ||
50 | "expected item, found `;`\n\ | ||
51 | consider removing this semicolon" | ||
52 | } else { | ||
53 | "expected item" | ||
54 | }; | ||
55 | p.err_and_bump(message); | ||
56 | return; | ||
57 | } | ||
58 | }; | ||
59 | item.complete(p, item_kind); | ||
60 | } | ||
61 | |||
62 | fn generic_parameters(_: &mut Parser) {} | ||
63 | |||
64 | fn where_clause(_: &mut Parser) {} | ||
65 | |||
66 | fn extern_crate_item(p: &mut Parser) { | ||
67 | assert!(p.at(EXTERN_KW)); | ||
68 | p.bump(); | ||
69 | assert!(p.at(CRATE_KW)); | ||
70 | p.bump(); | ||
71 | |||
72 | p.expect(IDENT) && alias(p) && p.expect(SEMI); | ||
73 | } | ||
74 | |||
75 | fn mod_item(p: &mut Parser) { | ||
76 | assert!(p.at(MOD_KW)); | ||
77 | p.bump(); | ||
78 | |||
79 | if p.expect(IDENT) && !p.eat(SEMI) { | ||
80 | if p.expect(L_CURLY) { | ||
81 | mod_contents(p, true); | ||
82 | p.expect(R_CURLY); | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | |||
87 | pub(super) fn is_use_tree_start(kind: SyntaxKind) -> bool { | ||
88 | kind == STAR || kind == L_CURLY | ||
89 | } | ||
90 | |||
91 | fn use_item(p: &mut Parser) { | ||
92 | assert!(p.at(USE_KW)); | ||
93 | p.bump(); | ||
94 | |||
95 | use_tree(p); | ||
96 | p.expect(SEMI); | ||
97 | |||
98 | fn use_tree(p: &mut Parser) { | ||
99 | let la = p.nth(1); | ||
100 | let m = p.start(); | ||
101 | match (p.current(), la) { | ||
102 | (STAR, _) => p.bump(), | ||
103 | (COLONCOLON, STAR) => { | ||
104 | p.bump(); | ||
105 | p.bump(); | ||
106 | } | ||
107 | (L_CURLY, _) | (COLONCOLON, L_CURLY) => { | ||
108 | if p.at(COLONCOLON) { | ||
109 | p.bump(); | ||
110 | } | ||
111 | nested_trees(p); | ||
112 | } | ||
113 | _ if paths::is_path_start(p) => { | ||
114 | paths::use_path(p); | ||
115 | match p.current() { | ||
116 | AS_KW => { | ||
117 | alias(p); | ||
118 | } | ||
119 | COLONCOLON => { | ||
120 | p.bump(); | ||
121 | match p.current() { | ||
122 | STAR => { | ||
123 | p.bump(); | ||
124 | } | ||
125 | L_CURLY => nested_trees(p), | ||
126 | _ => { | ||
127 | // is this unreachable? | ||
128 | p.error().message("expected `{` or `*`").emit(); | ||
129 | } | ||
130 | } | ||
131 | } | ||
132 | _ => (), | ||
133 | } | ||
134 | } | ||
135 | _ => { | ||
136 | m.abandon(p); | ||
137 | p.err_and_bump("expected one of `*`, `::`, `{`, `self`, `super`, `indent`"); | ||
138 | return; | ||
139 | } | ||
140 | } | ||
141 | m.complete(p, USE_TREE); | ||
142 | } | ||
143 | |||
144 | fn nested_trees(p: &mut Parser) { | ||
145 | assert!(p.at(L_CURLY)); | ||
146 | p.bump(); | ||
147 | while !p.at(EOF) && !p.at(R_CURLY) { | ||
148 | use_tree(p); | ||
149 | if !p.at(R_CURLY) { | ||
150 | p.expect(COMMA); | ||
151 | } | ||
152 | } | ||
153 | p.expect(R_CURLY); | ||
154 | } | ||
155 | } | ||
156 | |||
157 | fn fn_item(p: &mut Parser) { | ||
158 | assert!(p.at(FN_KW)); | ||
159 | p.bump(); | ||
160 | |||
161 | p.expect(IDENT); | ||
162 | if p.at(L_PAREN) { | ||
163 | fn_value_parameters(p); | ||
164 | } else { | ||
165 | p.error().message("expected function arguments").emit(); | ||
166 | } | ||
167 | |||
168 | if p.at(L_CURLY) { | ||
169 | p.expect(L_CURLY); | ||
170 | p.expect(R_CURLY); | ||
171 | } | ||
172 | |||
173 | fn fn_value_parameters(p: &mut Parser) { | ||
174 | assert!(p.at(L_PAREN)); | ||
175 | p.bump(); | ||
176 | p.expect(R_PAREN); | ||
177 | } | ||
178 | } | ||
diff --git a/src/parser/event_parser/grammar/items/structs.rs b/src/parser/event_parser/grammar/items/structs.rs new file mode 100644 index 000000000..b31cf18df --- /dev/null +++ b/src/parser/event_parser/grammar/items/structs.rs | |||
@@ -0,0 +1,83 @@ | |||
1 | use super::*; | ||
2 | |||
3 | pub(super) fn struct_item(p: &mut Parser) { | ||
4 | assert!(p.at(STRUCT_KW)); | ||
5 | p.bump(); | ||
6 | |||
7 | if !p.expect(IDENT) { | ||
8 | return; | ||
9 | } | ||
10 | generic_parameters(p); | ||
11 | match p.current() { | ||
12 | WHERE_KW => { | ||
13 | where_clause(p); | ||
14 | match p.current() { | ||
15 | SEMI => { | ||
16 | p.bump(); | ||
17 | return; | ||
18 | } | ||
19 | L_CURLY => named_fields(p), | ||
20 | _ => { | ||
21 | //TODO: special case `(` error message | ||
22 | p.error().message("expected `;` or `{`").emit(); | ||
23 | return; | ||
24 | } | ||
25 | } | ||
26 | } | ||
27 | SEMI => { | ||
28 | p.bump(); | ||
29 | return; | ||
30 | } | ||
31 | L_CURLY => named_fields(p), | ||
32 | L_PAREN => { | ||
33 | pos_fields(p); | ||
34 | p.expect(SEMI); | ||
35 | } | ||
36 | _ => { | ||
37 | p.error().message("expected `;`, `{`, or `(`").emit(); | ||
38 | return; | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | |||
43 | fn named_fields(p: &mut Parser) { | ||
44 | assert!(p.at(L_CURLY)); | ||
45 | p.bump(); | ||
46 | while !p.at(R_CURLY) && !p.at(EOF) { | ||
47 | named_field(p); | ||
48 | if !p.at(R_CURLY) { | ||
49 | p.expect(COMMA); | ||
50 | } | ||
51 | } | ||
52 | p.expect(R_CURLY); | ||
53 | |||
54 | fn named_field(p: &mut Parser) { | ||
55 | let field = p.start(); | ||
56 | visibility(p); | ||
57 | if p.expect(IDENT) { | ||
58 | p.expect(COLON); | ||
59 | types::type_ref(p); | ||
60 | field.complete(p, NAMED_FIELD); | ||
61 | } else { | ||
62 | field.abandon(p); | ||
63 | p.err_and_bump("expected field declaration"); | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | |||
68 | fn pos_fields(p: &mut Parser) { | ||
69 | if !p.expect(L_PAREN) { | ||
70 | return; | ||
71 | } | ||
72 | while !p.at(R_PAREN) && !p.at(EOF) { | ||
73 | let pos_field = p.start(); | ||
74 | visibility(p); | ||
75 | types::type_ref(p); | ||
76 | pos_field.complete(p, POS_FIELD); | ||
77 | |||
78 | if !p.at(R_PAREN) { | ||
79 | p.expect(COMMA); | ||
80 | } | ||
81 | } | ||
82 | p.expect(R_PAREN); | ||
83 | } | ||