aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_parser/src/grammar/expressions/atom.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_parser/src/grammar/expressions/atom.rs')
-rw-r--r--crates/ra_parser/src/grammar/expressions/atom.rs144
1 files changed, 72 insertions, 72 deletions
diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs
index 8dc7e44a9..8b1a1de49 100644
--- a/crates/ra_parser/src/grammar/expressions/atom.rs
+++ b/crates/ra_parser/src/grammar/expressions/atom.rs
@@ -60,29 +60,29 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
60 if let Some(m) = literal(p) { 60 if let Some(m) = literal(p) {
61 return Some((m, BlockLike::NotBlock)); 61 return Some((m, BlockLike::NotBlock));
62 } 62 }
63 if paths::is_path_start(p) || p.at(L_ANGLE) { 63 if paths::is_path_start(p) || p.at(T![<]) {
64 return Some(path_expr(p, r)); 64 return Some(path_expr(p, r));
65 } 65 }
66 let la = p.nth(1); 66 let la = p.nth(1);
67 let done = match p.current() { 67 let done = match p.current() {
68 L_PAREN => tuple_expr(p), 68 T!['('] => tuple_expr(p),
69 L_BRACK => array_expr(p), 69 T!['['] => array_expr(p),
70 PIPE => lambda_expr(p), 70 T![|] => lambda_expr(p),
71 MOVE_KW if la == PIPE => lambda_expr(p), 71 T![move] if la == T![|] => lambda_expr(p),
72 ASYNC_KW if la == PIPE || (la == MOVE_KW && p.nth(2) == PIPE) => lambda_expr(p), 72 T![async] if la == T![|] || (la == T![move] && p.nth(2) == T![|]) => lambda_expr(p),
73 IF_KW => if_expr(p), 73 T![if] => if_expr(p),
74 74
75 LOOP_KW => loop_expr(p, None), 75 T![loop] => loop_expr(p, None),
76 FOR_KW => for_expr(p, None), 76 T![for] => for_expr(p, None),
77 WHILE_KW => while_expr(p, None), 77 T![while] => while_expr(p, None),
78 LIFETIME if la == COLON => { 78 LIFETIME if la == T![:] => {
79 let m = p.start(); 79 let m = p.start();
80 label(p); 80 label(p);
81 match p.current() { 81 match p.current() {
82 LOOP_KW => loop_expr(p, Some(m)), 82 T![loop] => loop_expr(p, Some(m)),
83 FOR_KW => for_expr(p, Some(m)), 83 T![for] => for_expr(p, Some(m)),
84 WHILE_KW => while_expr(p, Some(m)), 84 T![while] => while_expr(p, Some(m)),
85 L_CURLY => block_expr(p, Some(m)), 85 T!['{'] => block_expr(p, Some(m)),
86 _ => { 86 _ => {
87 // test_err misplaced_label_err 87 // test_err misplaced_label_err
88 // fn main() { 88 // fn main() {
@@ -94,22 +94,22 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
94 } 94 }
95 } 95 }
96 } 96 }
97 ASYNC_KW if la == L_CURLY || (la == MOVE_KW && p.nth(2) == L_CURLY) => { 97 T![async] if la == T!['{'] || (la == T![move] && p.nth(2) == T!['{']) => {
98 let m = p.start(); 98 let m = p.start();
99 p.bump(); 99 p.bump();
100 p.eat(MOVE_KW); 100 p.eat(T![move]);
101 block_expr(p, Some(m)) 101 block_expr(p, Some(m))
102 } 102 }
103 MATCH_KW => match_expr(p), 103 T![match] => match_expr(p),
104 UNSAFE_KW if la == L_CURLY => { 104 T![unsafe] if la == T!['{'] => {
105 let m = p.start(); 105 let m = p.start();
106 p.bump(); 106 p.bump();
107 block_expr(p, Some(m)) 107 block_expr(p, Some(m))
108 } 108 }
109 L_CURLY => block_expr(p, None), 109 T!['{'] => block_expr(p, None),
110 RETURN_KW => return_expr(p), 110 T![return] => return_expr(p),
111 CONTINUE_KW => continue_expr(p), 111 T![continue] => continue_expr(p),
112 BREAK_KW => break_expr(p, r), 112 T![break] => break_expr(p, r),
113 _ => { 113 _ => {
114 p.err_recover("expected expression", EXPR_RECOVERY_SET); 114 p.err_recover("expected expression", EXPR_RECOVERY_SET);
115 return None; 115 return None;
@@ -129,25 +129,25 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
129// (1,); 129// (1,);
130// } 130// }
131fn tuple_expr(p: &mut Parser) -> CompletedMarker { 131fn tuple_expr(p: &mut Parser) -> CompletedMarker {
132 assert!(p.at(L_PAREN)); 132 assert!(p.at(T!['(']));
133 let m = p.start(); 133 let m = p.start();
134 p.expect(L_PAREN); 134 p.expect(T!['(']);
135 135
136 let mut saw_comma = false; 136 let mut saw_comma = false;
137 let mut saw_expr = false; 137 let mut saw_expr = false;
138 while !p.at(EOF) && !p.at(R_PAREN) { 138 while !p.at(EOF) && !p.at(T![')']) {
139 saw_expr = true; 139 saw_expr = true;
140 if !p.at_ts(EXPR_FIRST) { 140 if !p.at_ts(EXPR_FIRST) {
141 p.error("expected expression"); 141 p.error("expected expression");
142 break; 142 break;
143 } 143 }
144 expr(p); 144 expr(p);
145 if !p.at(R_PAREN) { 145 if !p.at(T![')']) {
146 saw_comma = true; 146 saw_comma = true;
147 p.expect(COMMA); 147 p.expect(T![,]);
148 } 148 }
149 } 149 }
150 p.expect(R_PAREN); 150 p.expect(T![')']);
151 m.complete(p, if saw_expr && !saw_comma { PAREN_EXPR } else { TUPLE_EXPR }) 151 m.complete(p, if saw_expr && !saw_comma { PAREN_EXPR } else { TUPLE_EXPR })
152} 152}
153 153
@@ -159,21 +159,21 @@ fn tuple_expr(p: &mut Parser) -> CompletedMarker {
159// [1; 2]; 159// [1; 2];
160// } 160// }
161fn array_expr(p: &mut Parser) -> CompletedMarker { 161fn array_expr(p: &mut Parser) -> CompletedMarker {
162 assert!(p.at(L_BRACK)); 162 assert!(p.at(T!['[']));
163 let m = p.start(); 163 let m = p.start();
164 p.bump(); 164 p.bump();
165 if p.eat(R_BRACK) { 165 if p.eat(T![']']) {
166 return m.complete(p, ARRAY_EXPR); 166 return m.complete(p, ARRAY_EXPR);
167 } 167 }
168 expr(p); 168 expr(p);
169 if p.eat(SEMI) { 169 if p.eat(T![;]) {
170 expr(p); 170 expr(p);
171 p.expect(R_BRACK); 171 p.expect(T![']']);
172 return m.complete(p, ARRAY_EXPR); 172 return m.complete(p, ARRAY_EXPR);
173 } 173 }
174 while !p.at(EOF) && !p.at(R_BRACK) { 174 while !p.at(EOF) && !p.at(T![']']) {
175 p.expect(COMMA); 175 p.expect(T![,]);
176 if p.at(R_BRACK) { 176 if p.at(T![']']) {
177 break; 177 break;
178 } 178 }
179 if !p.at_ts(EXPR_FIRST) { 179 if !p.at_ts(EXPR_FIRST) {
@@ -182,7 +182,7 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
182 } 182 }
183 expr(p); 183 expr(p);
184 } 184 }
185 p.expect(R_BRACK); 185 p.expect(T![']']);
186 m.complete(p, ARRAY_EXPR) 186 m.complete(p, ARRAY_EXPR)
187} 187}
188 188
@@ -198,17 +198,17 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
198// } 198// }
199fn lambda_expr(p: &mut Parser) -> CompletedMarker { 199fn lambda_expr(p: &mut Parser) -> CompletedMarker {
200 assert!( 200 assert!(
201 p.at(PIPE) 201 p.at(T![|])
202 || (p.at(MOVE_KW) && p.nth(1) == PIPE) 202 || (p.at(T![move]) && p.nth(1) == T![|])
203 || (p.at(ASYNC_KW) && p.nth(1) == PIPE) 203 || (p.at(T![async]) && p.nth(1) == T![|])
204 || (p.at(ASYNC_KW) && p.nth(1) == MOVE_KW && p.nth(2) == PIPE) 204 || (p.at(T![async]) && p.nth(1) == T![move] && p.nth(2) == T![|])
205 ); 205 );
206 let m = p.start(); 206 let m = p.start();
207 p.eat(ASYNC_KW); 207 p.eat(T![async]);
208 p.eat(MOVE_KW); 208 p.eat(T![move]);
209 params::param_list_opt_types(p); 209 params::param_list_opt_types(p);
210 if opt_fn_ret_type(p) { 210 if opt_fn_ret_type(p) {
211 if !p.at(L_CURLY) { 211 if !p.at(T!['{']) {
212 p.error("expected `{`"); 212 p.error("expected `{`");
213 } 213 }
214 } 214 }
@@ -224,14 +224,14 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker {
224// if S {}; 224// if S {};
225// } 225// }
226fn if_expr(p: &mut Parser) -> CompletedMarker { 226fn if_expr(p: &mut Parser) -> CompletedMarker {
227 assert!(p.at(IF_KW)); 227 assert!(p.at(T![if]));
228 let m = p.start(); 228 let m = p.start();
229 p.bump(); 229 p.bump();
230 cond(p); 230 cond(p);
231 block(p); 231 block(p);
232 if p.at(ELSE_KW) { 232 if p.at(T![else]) {
233 p.bump(); 233 p.bump();
234 if p.at(IF_KW) { 234 if p.at(T![if]) {
235 if_expr(p); 235 if_expr(p);
236 } else { 236 } else {
237 block(p); 237 block(p);
@@ -247,7 +247,7 @@ fn if_expr(p: &mut Parser) -> CompletedMarker {
247// 'c: for x in () {} 247// 'c: for x in () {}
248// } 248// }
249fn label(p: &mut Parser) { 249fn label(p: &mut Parser) {
250 assert!(p.at(LIFETIME) && p.nth(1) == COLON); 250 assert!(p.at(LIFETIME) && p.nth(1) == T![:]);
251 let m = p.start(); 251 let m = p.start();
252 p.bump(); 252 p.bump();
253 p.bump(); 253 p.bump();
@@ -259,7 +259,7 @@ fn label(p: &mut Parser) {
259// loop {}; 259// loop {};
260// } 260// }
261fn loop_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { 261fn loop_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
262 assert!(p.at(LOOP_KW)); 262 assert!(p.at(T![loop]));
263 let m = m.unwrap_or_else(|| p.start()); 263 let m = m.unwrap_or_else(|| p.start());
264 p.bump(); 264 p.bump();
265 block(p); 265 block(p);
@@ -272,7 +272,7 @@ fn loop_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
272// while let Some(x) = it.next() {}; 272// while let Some(x) = it.next() {};
273// } 273// }
274fn while_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { 274fn while_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
275 assert!(p.at(WHILE_KW)); 275 assert!(p.at(T![while]));
276 let m = m.unwrap_or_else(|| p.start()); 276 let m = m.unwrap_or_else(|| p.start());
277 p.bump(); 277 p.bump();
278 cond(p); 278 cond(p);
@@ -285,11 +285,11 @@ fn while_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
285// for x in [] {}; 285// for x in [] {};
286// } 286// }
287fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { 287fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
288 assert!(p.at(FOR_KW)); 288 assert!(p.at(T![for]));
289 let m = m.unwrap_or_else(|| p.start()); 289 let m = m.unwrap_or_else(|| p.start());
290 p.bump(); 290 p.bump();
291 patterns::pattern(p); 291 patterns::pattern(p);
292 p.expect(IN_KW); 292 p.expect(T![in]);
293 expr_no_struct(p); 293 expr_no_struct(p);
294 block(p); 294 block(p);
295 m.complete(p, FOR_EXPR) 295 m.complete(p, FOR_EXPR)
@@ -305,9 +305,9 @@ fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
305// } 305// }
306fn cond(p: &mut Parser) { 306fn cond(p: &mut Parser) {
307 let m = p.start(); 307 let m = p.start();
308 if p.eat(LET_KW) { 308 if p.eat(T![let]) {
309 patterns::pattern_list(p); 309 patterns::pattern_list(p);
310 p.expect(EQ); 310 p.expect(T![=]);
311 } 311 }
312 expr_no_struct(p); 312 expr_no_struct(p);
313 m.complete(p, CONDITION); 313 m.complete(p, CONDITION);
@@ -319,11 +319,11 @@ fn cond(p: &mut Parser) {
319// match S {}; 319// match S {};
320// } 320// }
321fn match_expr(p: &mut Parser) -> CompletedMarker { 321fn match_expr(p: &mut Parser) -> CompletedMarker {
322 assert!(p.at(MATCH_KW)); 322 assert!(p.at(T![match]));
323 let m = p.start(); 323 let m = p.start();
324 p.bump(); 324 p.bump();
325 expr_no_struct(p); 325 expr_no_struct(p);
326 if p.at(L_CURLY) { 326 if p.at(T!['{']) {
327 match_arm_list(p); 327 match_arm_list(p);
328 } else { 328 } else {
329 p.error("expected `{`") 329 p.error("expected `{`")
@@ -332,9 +332,9 @@ fn match_expr(p: &mut Parser) -> CompletedMarker {
332} 332}
333 333
334pub(crate) fn match_arm_list(p: &mut Parser) { 334pub(crate) fn match_arm_list(p: &mut Parser) {
335 assert!(p.at(L_CURLY)); 335 assert!(p.at(T!['{']));
336 let m = p.start(); 336 let m = p.start();
337 p.eat(L_CURLY); 337 p.eat(T!['{']);
338 338
339 // test match_arms_inner_attribute 339 // test match_arms_inner_attribute
340 // fn foo() { 340 // fn foo() {
@@ -347,8 +347,8 @@ pub(crate) fn match_arm_list(p: &mut Parser) {
347 // } 347 // }
348 attributes::inner_attributes(p); 348 attributes::inner_attributes(p);
349 349
350 while !p.at(EOF) && !p.at(R_CURLY) { 350 while !p.at(EOF) && !p.at(T!['}']) {
351 if p.at(L_CURLY) { 351 if p.at(T!['{']) {
352 error_block(p, "expected match arm"); 352 error_block(p, "expected match arm");
353 continue; 353 continue;
354 } 354 }
@@ -362,12 +362,12 @@ pub(crate) fn match_arm_list(p: &mut Parser) {
362 // } 362 // }
363 // } 363 // }
364 if match_arm(p).is_block() { 364 if match_arm(p).is_block() {
365 p.eat(COMMA); 365 p.eat(T![,]);
366 } else if !p.at(R_CURLY) { 366 } else if !p.at(T!['}']) {
367 p.expect(COMMA); 367 p.expect(T![,]);
368 } 368 }
369 } 369 }
370 p.expect(R_CURLY); 370 p.expect(T!['}']);
371 m.complete(p, MATCH_ARM_LIST); 371 m.complete(p, MATCH_ARM_LIST);
372} 372}
373 373
@@ -399,10 +399,10 @@ fn match_arm(p: &mut Parser) -> BlockLike {
399 attributes::outer_attributes(p); 399 attributes::outer_attributes(p);
400 400
401 patterns::pattern_list_r(p, TokenSet::empty()); 401 patterns::pattern_list_r(p, TokenSet::empty());
402 if p.at(IF_KW) { 402 if p.at(T![if]) {
403 match_guard(p); 403 match_guard(p);
404 } 404 }
405 p.expect(FAT_ARROW); 405 p.expect(T![=>]);
406 let blocklike = expr_stmt(p).1; 406 let blocklike = expr_stmt(p).1;
407 m.complete(p, MATCH_ARM); 407 m.complete(p, MATCH_ARM);
408 blocklike 408 blocklike
@@ -415,7 +415,7 @@ fn match_arm(p: &mut Parser) -> BlockLike {
415// } 415// }
416// } 416// }
417fn match_guard(p: &mut Parser) -> CompletedMarker { 417fn match_guard(p: &mut Parser) -> CompletedMarker {
418 assert!(p.at(IF_KW)); 418 assert!(p.at(T![if]));
419 let m = p.start(); 419 let m = p.start();
420 p.bump(); 420 p.bump();
421 expr(p); 421 expr(p);
@@ -429,7 +429,7 @@ fn match_guard(p: &mut Parser) -> CompletedMarker {
429// 'label: {}; 429// 'label: {};
430// } 430// }
431fn block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { 431fn block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
432 assert!(p.at(L_CURLY)); 432 assert!(p.at(T!['{']));
433 let m = m.unwrap_or_else(|| p.start()); 433 let m = m.unwrap_or_else(|| p.start());
434 block(p); 434 block(p);
435 m.complete(p, BLOCK_EXPR) 435 m.complete(p, BLOCK_EXPR)
@@ -441,7 +441,7 @@ fn block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
441// return 92; 441// return 92;
442// } 442// }
443fn return_expr(p: &mut Parser) -> CompletedMarker { 443fn return_expr(p: &mut Parser) -> CompletedMarker {
444 assert!(p.at(RETURN_KW)); 444 assert!(p.at(T![return]));
445 let m = p.start(); 445 let m = p.start();
446 p.bump(); 446 p.bump();
447 if p.at_ts(EXPR_FIRST) { 447 if p.at_ts(EXPR_FIRST) {
@@ -458,7 +458,7 @@ fn return_expr(p: &mut Parser) -> CompletedMarker {
458// } 458// }
459// } 459// }
460fn continue_expr(p: &mut Parser) -> CompletedMarker { 460fn continue_expr(p: &mut Parser) -> CompletedMarker {
461 assert!(p.at(CONTINUE_KW)); 461 assert!(p.at(T![continue]));
462 let m = p.start(); 462 let m = p.start();
463 p.bump(); 463 p.bump();
464 p.eat(LIFETIME); 464 p.eat(LIFETIME);
@@ -475,7 +475,7 @@ fn continue_expr(p: &mut Parser) -> CompletedMarker {
475// } 475// }
476// } 476// }
477fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { 477fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker {
478 assert!(p.at(BREAK_KW)); 478 assert!(p.at(T![break]));
479 let m = p.start(); 479 let m = p.start();
480 p.bump(); 480 p.bump();
481 p.eat(LIFETIME); 481 p.eat(LIFETIME);
@@ -486,7 +486,7 @@ fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker {
486 // for i in break {} 486 // for i in break {}
487 // match break {} 487 // match break {}
488 // } 488 // }
489 if p.at_ts(EXPR_FIRST) && !(r.forbid_structs && p.at(L_CURLY)) { 489 if p.at_ts(EXPR_FIRST) && !(r.forbid_structs && p.at(T!['{'])) {
490 expr(p); 490 expr(p);
491 } 491 }
492 m.complete(p, BREAK_EXPR) 492 m.complete(p, BREAK_EXPR)