aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_parser/src/grammar/items.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_parser/src/grammar/items.rs')
-rw-r--r--crates/ra_parser/src/grammar/items.rs114
1 files changed, 57 insertions, 57 deletions
diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs
index 97f8122a9..6728e395f 100644
--- a/crates/ra_parser/src/grammar/items.rs
+++ b/crates/ra_parser/src/grammar/items.rs
@@ -19,7 +19,7 @@ use super::*;
19// struct S; 19// struct S;
20pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { 20pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
21 attributes::inner_attributes(p); 21 attributes::inner_attributes(p);
22 while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) { 22 while !p.at(EOF) && !(stop_on_r_curly && p.at(T!['}'])) {
23 item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod) 23 item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod)
24 } 24 }
25} 25}
@@ -45,20 +45,20 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemF
45 match macro_call(p) { 45 match macro_call(p) {
46 BlockLike::Block => (), 46 BlockLike::Block => (),
47 BlockLike::NotBlock => { 47 BlockLike::NotBlock => {
48 p.expect(SEMI); 48 p.expect(T![;]);
49 } 49 }
50 } 50 }
51 m.complete(p, MACRO_CALL); 51 m.complete(p, MACRO_CALL);
52 } else { 52 } else {
53 m.abandon(p); 53 m.abandon(p);
54 if p.at(L_CURLY) { 54 if p.at(T!['{']) {
55 error_block(p, "expected an item"); 55 error_block(p, "expected an item");
56 } else if p.at(R_CURLY) && !stop_on_r_curly { 56 } else if p.at(T!['}']) && !stop_on_r_curly {
57 let e = p.start(); 57 let e = p.start();
58 p.error("unmatched `}`"); 58 p.error("unmatched `}`");
59 p.bump(); 59 p.bump();
60 e.complete(p, ERROR); 60 e.complete(p, ERROR);
61 } else if !p.at(EOF) && !p.at(R_CURLY) { 61 } else if !p.at(EOF) && !p.at(T!['}']) {
62 p.err_and_bump("expected an item"); 62 p.err_and_bump("expected an item");
63 } else { 63 } else {
64 p.error("expected an item"); 64 p.error("expected an item");
@@ -79,32 +79,32 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul
79 let mut has_mods = false; 79 let mut has_mods = false;
80 80
81 // modifiers 81 // modifiers
82 has_mods |= p.eat(CONST_KW); 82 has_mods |= p.eat(T![const]);
83 83
84 // test_err unsafe_block_in_mod 84 // test_err unsafe_block_in_mod
85 // fn foo(){} unsafe { } fn bar(){} 85 // fn foo(){} unsafe { } fn bar(){}
86 if p.at(UNSAFE_KW) && p.nth(1) != L_CURLY { 86 if p.at(T![unsafe]) && p.nth(1) != T!['{'] {
87 p.eat(UNSAFE_KW); 87 p.eat(T![unsafe]);
88 has_mods = true; 88 has_mods = true;
89 } 89 }
90 90
91 // test_err async_without_semicolon 91 // test_err async_without_semicolon
92 // fn foo() { let _ = async {} } 92 // fn foo() { let _ = async {} }
93 if p.at(ASYNC_KW) && p.nth(1) != L_CURLY && p.nth(1) != MOVE_KW && p.nth(1) != PIPE { 93 if p.at(T![async]) && p.nth(1) != T!['{'] && p.nth(1) != T![move] && p.nth(1) != T![|] {
94 p.eat(ASYNC_KW); 94 p.eat(T![async]);
95 has_mods = true; 95 has_mods = true;
96 } 96 }
97 97
98 if p.at(EXTERN_KW) { 98 if p.at(T![extern]) {
99 has_mods = true; 99 has_mods = true;
100 abi(p); 100 abi(p);
101 } 101 }
102 if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == TRAIT_KW { 102 if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == T![trait] {
103 p.bump_remap(AUTO_KW); 103 p.bump_remap(T![auto]);
104 has_mods = true; 104 has_mods = true;
105 } 105 }
106 if p.at(IDENT) && p.at_contextual_kw("default") && p.nth(1) == IMPL_KW { 106 if p.at(IDENT) && p.at_contextual_kw("default") && p.nth(1) == T![impl ] {
107 p.bump_remap(DEFAULT_KW); 107 p.bump_remap(T![default]);
108 has_mods = true; 108 has_mods = true;
109 } 109 }
110 110
@@ -135,7 +135,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul
135 // test_err wrong_order_fns 135 // test_err wrong_order_fns
136 // async unsafe fn foo() {} 136 // async unsafe fn foo() {}
137 // unsafe const fn bar() {} 137 // unsafe const fn bar() {}
138 FN_KW => { 138 T![fn] => {
139 fn_def(p, flavor); 139 fn_def(p, flavor);
140 m.complete(p, FN_DEF); 140 m.complete(p, FN_DEF);
141 } 141 }
@@ -148,7 +148,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul
148 148
149 // test unsafe_auto_trait 149 // test unsafe_auto_trait
150 // unsafe auto trait T {} 150 // unsafe auto trait T {}
151 TRAIT_KW => { 151 T![trait] => {
152 traits::trait_def(p); 152 traits::trait_def(p);
153 m.complete(p, TRAIT_DEF); 153 m.complete(p, TRAIT_DEF);
154 } 154 }
@@ -161,7 +161,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul
161 161
162 // test unsafe_default_impl 162 // test unsafe_default_impl
163 // unsafe default impl Foo {} 163 // unsafe default impl Foo {}
164 IMPL_KW => { 164 T![impl ] => {
165 traits::impl_block(p); 165 traits::impl_block(p);
166 m.complete(p, IMPL_BLOCK); 166 m.complete(p, IMPL_BLOCK);
167 } 167 }
@@ -186,10 +186,10 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
186 match p.current() { 186 match p.current() {
187 // test extern_crate 187 // test extern_crate
188 // extern crate foo; 188 // extern crate foo;
189 EXTERN_KW if la == CRATE_KW => extern_crate_item(p, m), 189 T![extern] if la == T![crate] => extern_crate_item(p, m),
190 TYPE_KW => type_def(p, m), 190 T![type] => type_def(p, m),
191 MOD_KW => mod_item(p, m), 191 T![mod] => mod_item(p, m),
192 STRUCT_KW => { 192 T![struct] => {
193 // test struct_items 193 // test struct_items
194 // struct Foo; 194 // struct Foo;
195 // struct Foo {} 195 // struct Foo {}
@@ -199,7 +199,7 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
199 // a: i32, 199 // a: i32,
200 // b: f32, 200 // b: f32,
201 // } 201 // }
202 nominal::struct_def(p, m, STRUCT_KW); 202 nominal::struct_def(p, m, T![struct]);
203 } 203 }
204 IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => { 204 IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => {
205 // test union_items 205 // test union_items
@@ -208,16 +208,16 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
208 // a: i32, 208 // a: i32,
209 // b: f32, 209 // b: f32,
210 // } 210 // }
211 nominal::struct_def(p, m, UNION_KW); 211 nominal::struct_def(p, m, T![union]);
212 } 212 }
213 ENUM_KW => nominal::enum_def(p, m), 213 T![enum] => nominal::enum_def(p, m),
214 USE_KW => use_item::use_item(p, m), 214 T![use] => use_item::use_item(p, m),
215 CONST_KW if (la == IDENT || la == MUT_KW) => consts::const_def(p, m), 215 T![const] if (la == IDENT || la == T![mut]) => consts::const_def(p, m),
216 STATIC_KW => consts::static_def(p, m), 216 T![static] => consts::static_def(p, m),
217 // test extern_block 217 // test extern_block
218 // extern {} 218 // extern {}
219 EXTERN_KW 219 T![extern]
220 if la == L_CURLY || ((la == STRING || la == RAW_STRING) && p.nth(2) == L_CURLY) => 220 if la == T!['{'] || ((la == STRING || la == RAW_STRING) && p.nth(2) == T!['{']) =>
221 { 221 {
222 abi(p); 222 abi(p);
223 extern_item_list(p); 223 extern_item_list(p);
@@ -225,7 +225,7 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
225 } 225 }
226 _ => return Err(m), 226 _ => return Err(m),
227 }; 227 };
228 if p.at(SEMI) { 228 if p.at(T![;]) {
229 p.err_and_bump( 229 p.err_and_bump(
230 "expected item, found `;`\n\ 230 "expected item, found `;`\n\
231 consider removing this semicolon", 231 consider removing this semicolon",
@@ -235,27 +235,27 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
235} 235}
236 236
237fn extern_crate_item(p: &mut Parser, m: Marker) { 237fn extern_crate_item(p: &mut Parser, m: Marker) {
238 assert!(p.at(EXTERN_KW)); 238 assert!(p.at(T![extern]));
239 p.bump(); 239 p.bump();
240 assert!(p.at(CRATE_KW)); 240 assert!(p.at(T![crate]));
241 p.bump(); 241 p.bump();
242 name_ref(p); 242 name_ref(p);
243 opt_alias(p); 243 opt_alias(p);
244 p.expect(SEMI); 244 p.expect(T![;]);
245 m.complete(p, EXTERN_CRATE_ITEM); 245 m.complete(p, EXTERN_CRATE_ITEM);
246} 246}
247 247
248pub(crate) fn extern_item_list(p: &mut Parser) { 248pub(crate) fn extern_item_list(p: &mut Parser) {
249 assert!(p.at(L_CURLY)); 249 assert!(p.at(T!['{']));
250 let m = p.start(); 250 let m = p.start();
251 p.bump(); 251 p.bump();
252 mod_contents(p, true); 252 mod_contents(p, true);
253 p.expect(R_CURLY); 253 p.expect(T!['}']);
254 m.complete(p, EXTERN_ITEM_LIST); 254 m.complete(p, EXTERN_ITEM_LIST);
255} 255}
256 256
257fn fn_def(p: &mut Parser, flavor: ItemFlavor) { 257fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
258 assert!(p.at(FN_KW)); 258 assert!(p.at(T![fn]));
259 p.bump(); 259 p.bump();
260 260
261 name_r(p, ITEM_RECOVERY_SET); 261 name_r(p, ITEM_RECOVERY_SET);
@@ -263,7 +263,7 @@ fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
263 // fn foo<T: Clone + Copy>(){} 263 // fn foo<T: Clone + Copy>(){}
264 type_params::opt_type_param_list(p); 264 type_params::opt_type_param_list(p);
265 265
266 if p.at(L_PAREN) { 266 if p.at(T!['(']) {
267 match flavor { 267 match flavor {
268 ItemFlavor::Mod => params::param_list(p), 268 ItemFlavor::Mod => params::param_list(p),
269 ItemFlavor::Trait => params::param_list_opt_patterns(p), 269 ItemFlavor::Trait => params::param_list_opt_patterns(p),
@@ -282,7 +282,7 @@ fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
282 282
283 // test fn_decl 283 // test fn_decl
284 // trait T { fn foo(); } 284 // trait T { fn foo(); }
285 if p.at(SEMI) { 285 if p.at(T![;]) {
286 p.bump(); 286 p.bump();
287 } else { 287 } else {
288 expressions::block(p) 288 expressions::block(p)
@@ -292,7 +292,7 @@ fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
292// test type_item 292// test type_item
293// type Foo = Bar; 293// type Foo = Bar;
294fn type_def(p: &mut Parser, m: Marker) { 294fn type_def(p: &mut Parser, m: Marker) {
295 assert!(p.at(TYPE_KW)); 295 assert!(p.at(T![type]));
296 p.bump(); 296 p.bump();
297 297
298 name(p); 298 name(p);
@@ -301,7 +301,7 @@ fn type_def(p: &mut Parser, m: Marker) {
301 // type Result<T> = (); 301 // type Result<T> = ();
302 type_params::opt_type_param_list(p); 302 type_params::opt_type_param_list(p);
303 303
304 if p.at(COLON) { 304 if p.at(T![:]) {
305 type_params::bounds(p); 305 type_params::bounds(p);
306 } 306 }
307 307
@@ -309,32 +309,32 @@ fn type_def(p: &mut Parser, m: Marker) {
309 // type Foo where Foo: Copy = (); 309 // type Foo where Foo: Copy = ();
310 type_params::opt_where_clause(p); 310 type_params::opt_where_clause(p);
311 311
312 if p.eat(EQ) { 312 if p.eat(T![=]) {
313 types::type_(p); 313 types::type_(p);
314 } 314 }
315 p.expect(SEMI); 315 p.expect(T![;]);
316 m.complete(p, TYPE_ALIAS_DEF); 316 m.complete(p, TYPE_ALIAS_DEF);
317} 317}
318 318
319pub(crate) fn mod_item(p: &mut Parser, m: Marker) { 319pub(crate) fn mod_item(p: &mut Parser, m: Marker) {
320 assert!(p.at(MOD_KW)); 320 assert!(p.at(T![mod]));
321 p.bump(); 321 p.bump();
322 322
323 name(p); 323 name(p);
324 if p.at(L_CURLY) { 324 if p.at(T!['{']) {
325 mod_item_list(p); 325 mod_item_list(p);
326 } else if !p.eat(SEMI) { 326 } else if !p.eat(T![;]) {
327 p.error("expected `;` or `{`"); 327 p.error("expected `;` or `{`");
328 } 328 }
329 m.complete(p, MODULE); 329 m.complete(p, MODULE);
330} 330}
331 331
332pub(crate) fn mod_item_list(p: &mut Parser) { 332pub(crate) fn mod_item_list(p: &mut Parser) {
333 assert!(p.at(L_CURLY)); 333 assert!(p.at(T!['{']));
334 let m = p.start(); 334 let m = p.start();
335 p.bump(); 335 p.bump();
336 mod_contents(p, true); 336 mod_contents(p, true);
337 p.expect(R_CURLY); 337 p.expect(T!['}']);
338 m.complete(p, ITEM_LIST); 338 m.complete(p, ITEM_LIST);
339} 339}
340 340
@@ -345,16 +345,16 @@ fn macro_call(p: &mut Parser) -> BlockLike {
345} 345}
346 346
347pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike { 347pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
348 p.expect(EXCL); 348 p.expect(T![!]);
349 if p.at(IDENT) { 349 if p.at(IDENT) {
350 name(p); 350 name(p);
351 } 351 }
352 match p.current() { 352 match p.current() {
353 L_CURLY => { 353 T!['{'] => {
354 token_tree(p); 354 token_tree(p);
355 BlockLike::Block 355 BlockLike::Block
356 } 356 }
357 L_PAREN | L_BRACK => { 357 T!['('] | T!['['] => {
358 token_tree(p); 358 token_tree(p);
359 BlockLike::NotBlock 359 BlockLike::NotBlock
360 } 360 }
@@ -367,22 +367,22 @@ pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
367 367
368pub(crate) fn token_tree(p: &mut Parser) { 368pub(crate) fn token_tree(p: &mut Parser) {
369 let closing_paren_kind = match p.current() { 369 let closing_paren_kind = match p.current() {
370 L_CURLY => R_CURLY, 370 T!['{'] => T!['}'],
371 L_PAREN => R_PAREN, 371 T!['('] => T![')'],
372 L_BRACK => R_BRACK, 372 T!['['] => T![']'],
373 _ => unreachable!(), 373 _ => unreachable!(),
374 }; 374 };
375 let m = p.start(); 375 let m = p.start();
376 p.bump(); 376 p.bump();
377 while !p.at(EOF) && !p.at(closing_paren_kind) { 377 while !p.at(EOF) && !p.at(closing_paren_kind) {
378 match p.current() { 378 match p.current() {
379 L_CURLY | L_PAREN | L_BRACK => token_tree(p), 379 T!['{'] | T!['('] | T!['['] => token_tree(p),
380 R_CURLY => { 380 T!['}'] => {
381 p.error("unmatched `}`"); 381 p.error("unmatched `}`");
382 m.complete(p, TOKEN_TREE); 382 m.complete(p, TOKEN_TREE);
383 return; 383 return;
384 } 384 }
385 R_PAREN | R_BRACK => p.err_and_bump("unmatched brace"), 385 T![')'] | T![']'] => p.err_and_bump("unmatched brace"),
386 _ => p.bump_raw(), 386 _ => p.bump_raw(),
387 } 387 }
388 } 388 }