aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_parser/src/grammar/expressions
diff options
context:
space:
mode:
authorBenjamin Coenen <[email protected]>2020-05-02 19:27:02 +0100
committerBenjamin Coenen <[email protected]>2020-05-02 19:27:02 +0100
commit4613497a7714c6cd87166e6525d764d75f8acefd (patch)
tree2527ae2c0ef2ef100efee3fcb8899f8e34d0d573 /crates/ra_parser/src/grammar/expressions
parent19e28888aa41b2845b47adb7314aed99d3c48679 (diff)
parent89e1f97515c36ab97bd378d972cabec0feb6d77e (diff)
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into fix_4202
Diffstat (limited to 'crates/ra_parser/src/grammar/expressions')
-rw-r--r--crates/ra_parser/src/grammar/expressions/atom.rs86
1 files changed, 58 insertions, 28 deletions
diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs
index 0d277a586..706a2f796 100644
--- a/crates/ra_parser/src/grammar/expressions/atom.rs
+++ b/crates/ra_parser/src/grammar/expressions/atom.rs
@@ -92,7 +92,12 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
92 T![loop] => loop_expr(p, Some(m)), 92 T![loop] => loop_expr(p, Some(m)),
93 T![for] => for_expr(p, Some(m)), 93 T![for] => for_expr(p, Some(m)),
94 T![while] => while_expr(p, Some(m)), 94 T![while] => while_expr(p, Some(m)),
95 T!['{'] => block_expr(p, Some(m)), 95 // test labeled_block
96 // fn f() { 'label: {}; }
97 T!['{'] => {
98 block_expr(p);
99 m.complete(p, EFFECT_EXPR)
100 }
96 _ => { 101 _ => {
97 // test_err misplaced_label_err 102 // test_err misplaced_label_err
98 // fn main() { 103 // fn main() {
@@ -108,13 +113,17 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
108 let m = p.start(); 113 let m = p.start();
109 p.bump(T![async]); 114 p.bump(T![async]);
110 p.eat(T![move]); 115 p.eat(T![move]);
111 block_expr(p, Some(m)) 116 block_expr(p);
117 m.complete(p, EFFECT_EXPR)
112 } 118 }
113 T![match] => match_expr(p), 119 T![match] => match_expr(p),
120 // test unsafe_block
121 // fn f() { unsafe { } }
114 T![unsafe] if la == T!['{'] => { 122 T![unsafe] if la == T!['{'] => {
115 let m = p.start(); 123 let m = p.start();
116 p.bump(T![unsafe]); 124 p.bump(T![unsafe]);
117 block_expr(p, Some(m)) 125 block_expr(p);
126 m.complete(p, EFFECT_EXPR)
118 } 127 }
119 T!['{'] => { 128 T!['{'] => {
120 // test for_range_from 129 // test for_range_from
@@ -123,7 +132,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
123 // break; 132 // break;
124 // } 133 // }
125 // } 134 // }
126 block_expr(p, None) 135 block_expr_unchecked(p)
127 } 136 }
128 T![return] => return_expr(p), 137 T![return] => return_expr(p),
129 T![continue] => continue_expr(p), 138 T![continue] => continue_expr(p),
@@ -134,7 +143,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
134 } 143 }
135 }; 144 };
136 let blocklike = match done.kind() { 145 let blocklike = match done.kind() {
137 IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR | TRY_BLOCK_EXPR => { 146 IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR | EFFECT_EXPR => {
138 BlockLike::Block 147 BlockLike::Block
139 } 148 }
140 _ => BlockLike::NotBlock, 149 _ => BlockLike::NotBlock,
@@ -231,13 +240,9 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker {
231 p.eat(T![move]); 240 p.eat(T![move]);
232 params::param_list_closure(p); 241 params::param_list_closure(p);
233 if opt_fn_ret_type(p) { 242 if opt_fn_ret_type(p) {
234 if p.at(T!['{']) { 243 // test lambda_ret_block
235 // test lambda_ret_block 244 // fn main() { || -> i32 { 92 }(); }
236 // fn main() { || -> i32 { 92 }(); } 245 block_expr(p);
237 block_expr(p, None);
238 } else {
239 p.error("expected `{`");
240 }
241 } else { 246 } else {
242 if p.at_ts(EXPR_FIRST) { 247 if p.at_ts(EXPR_FIRST) {
243 expr(p); 248 expr(p);
@@ -261,13 +266,13 @@ fn if_expr(p: &mut Parser) -> CompletedMarker {
261 let m = p.start(); 266 let m = p.start();
262 p.bump(T![if]); 267 p.bump(T![if]);
263 cond(p); 268 cond(p);
264 block(p); 269 block_expr(p);
265 if p.at(T![else]) { 270 if p.at(T![else]) {
266 p.bump(T![else]); 271 p.bump(T![else]);
267 if p.at(T![if]) { 272 if p.at(T![if]) {
268 if_expr(p); 273 if_expr(p);
269 } else { 274 } else {
270 block(p); 275 block_expr(p);
271 } 276 }
272 } 277 }
273 m.complete(p, IF_EXPR) 278 m.complete(p, IF_EXPR)
@@ -295,7 +300,7 @@ fn loop_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
295 assert!(p.at(T![loop])); 300 assert!(p.at(T![loop]));
296 let m = m.unwrap_or_else(|| p.start()); 301 let m = m.unwrap_or_else(|| p.start());
297 p.bump(T![loop]); 302 p.bump(T![loop]);
298 block(p); 303 block_expr(p);
299 m.complete(p, LOOP_EXPR) 304 m.complete(p, LOOP_EXPR)
300} 305}
301 306
@@ -310,7 +315,7 @@ fn while_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
310 let m = m.unwrap_or_else(|| p.start()); 315 let m = m.unwrap_or_else(|| p.start());
311 p.bump(T![while]); 316 p.bump(T![while]);
312 cond(p); 317 cond(p);
313 block(p); 318 block_expr(p);
314 m.complete(p, WHILE_EXPR) 319 m.complete(p, WHILE_EXPR)
315} 320}
316 321
@@ -325,7 +330,7 @@ fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
325 patterns::pattern(p); 330 patterns::pattern(p);
326 p.expect(T![in]); 331 p.expect(T![in]);
327 expr_no_struct(p); 332 expr_no_struct(p);
328 block(p); 333 block_expr(p);
329 m.complete(p, FOR_EXPR) 334 m.complete(p, FOR_EXPR)
330} 335}
331 336
@@ -458,16 +463,25 @@ fn match_guard(p: &mut Parser) -> CompletedMarker {
458 m.complete(p, MATCH_GUARD) 463 m.complete(p, MATCH_GUARD)
459} 464}
460 465
461// test block_expr 466// test block
462// fn foo() { 467// fn a() {}
463// {}; 468// fn b() { let _ = 1; }
464// unsafe {}; 469// fn c() { 1; 2; }
465// 'label: {}; 470// fn d() { 1; 2 }
466// } 471pub(crate) fn block_expr(p: &mut Parser) {
467pub(super) fn block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { 472 if !p.at(T!['{']) {
473 p.error("expected a block");
474 return;
475 }
476 block_expr_unchecked(p);
477}
478
479fn block_expr_unchecked(p: &mut Parser) -> CompletedMarker {
468 assert!(p.at(T!['{'])); 480 assert!(p.at(T!['{']));
469 let m = m.unwrap_or_else(|| p.start()); 481 let m = p.start();
470 naked_block(p); 482 p.bump(T!['{']);
483 expr_block_contents(p);
484 p.expect(T!['}']);
471 m.complete(p, BLOCK_EXPR) 485 m.complete(p, BLOCK_EXPR)
472} 486}
473 487
@@ -535,9 +549,25 @@ fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker {
535fn try_block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { 549fn try_block_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
536 assert!(p.at(T![try])); 550 assert!(p.at(T![try]));
537 let m = m.unwrap_or_else(|| p.start()); 551 let m = m.unwrap_or_else(|| p.start());
552 // Special-case `try!` as macro.
553 // This is a hack until we do proper edition support
554 if p.nth_at(1, T![!]) {
555 // test try_macro_fallback
556 // fn foo() { try!(Ok(())); }
557 let path = p.start();
558 let path_segment = p.start();
559 let name_ref = p.start();
560 p.bump_remap(IDENT);
561 name_ref.complete(p, NAME_REF);
562 path_segment.complete(p, PATH_SEGMENT);
563 path.complete(p, PATH);
564 let _block_like = items::macro_call_after_excl(p);
565 return m.complete(p, MACRO_CALL);
566 }
567
538 p.bump(T![try]); 568 p.bump(T![try]);
539 block(p); 569 block_expr(p);
540 m.complete(p, TRY_EXPR) 570 m.complete(p, EFFECT_EXPR)
541} 571}
542 572
543// test box_expr 573// test box_expr