aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_parser/src/grammar
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
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')
-rw-r--r--crates/ra_parser/src/grammar/expressions.rs26
-rw-r--r--crates/ra_parser/src/grammar/expressions/atom.rs86
-rw-r--r--crates/ra_parser/src/grammar/items.rs13
-rw-r--r--crates/ra_parser/src/grammar/items/use_item.rs2
-rw-r--r--crates/ra_parser/src/grammar/type_args.rs2
5 files changed, 74 insertions, 55 deletions
diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs
index cb30b25a8..34f039768 100644
--- a/crates/ra_parser/src/grammar/expressions.rs
+++ b/crates/ra_parser/src/grammar/expressions.rs
@@ -2,7 +2,7 @@
2 2
3mod atom; 3mod atom;
4 4
5pub(crate) use self::atom::match_arm_list; 5pub(crate) use self::atom::{block_expr, match_arm_list};
6pub(super) use self::atom::{literal, LITERAL_FIRST}; 6pub(super) use self::atom::{literal, LITERAL_FIRST};
7use super::*; 7use super::*;
8 8
@@ -49,28 +49,6 @@ fn expr_no_struct(p: &mut Parser) {
49 expr_bp(p, r, 1); 49 expr_bp(p, r, 1);
50} 50}
51 51
52// test block
53// fn a() {}
54// fn b() { let _ = 1; }
55// fn c() { 1; 2; }
56// fn d() { 1; 2 }
57pub(crate) fn block(p: &mut Parser) {
58 if !p.at(T!['{']) {
59 p.error("expected a block");
60 return;
61 }
62 atom::block_expr(p, None);
63}
64
65pub(crate) fn naked_block(p: &mut Parser) {
66 assert!(p.at(T!['{']));
67 let m = p.start();
68 p.bump(T!['{']);
69 expr_block_contents(p);
70 p.expect(T!['}']);
71 m.complete(p, BLOCK);
72}
73
74fn is_expr_stmt_attr_allowed(kind: SyntaxKind) -> bool { 52fn is_expr_stmt_attr_allowed(kind: SyntaxKind) -> bool {
75 match kind { 53 match kind {
76 BIN_EXPR | RANGE_EXPR | IF_EXPR => false, 54 BIN_EXPR | RANGE_EXPR | IF_EXPR => false,
@@ -197,7 +175,7 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) {
197 } 175 }
198} 176}
199 177
200pub(crate) fn expr_block_contents(p: &mut Parser) { 178pub(super) fn expr_block_contents(p: &mut Parser) {
201 // This is checked by a validator 179 // This is checked by a validator
202 attributes::inner_attributes(p); 180 attributes::inner_attributes(p);
203 181
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
diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs
index 433ed6812..67a924de5 100644
--- a/crates/ra_parser/src/grammar/items.rs
+++ b/crates/ra_parser/src/grammar/items.rs
@@ -329,7 +329,7 @@ fn fn_def(p: &mut Parser) {
329 if p.at(T![;]) { 329 if p.at(T![;]) {
330 p.bump(T![;]); 330 p.bump(T![;]);
331 } else { 331 } else {
332 expressions::block(p) 332 expressions::block_expr(p)
333 } 333 }
334} 334}
335 335
@@ -415,6 +415,17 @@ pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
415 if p.at(IDENT) { 415 if p.at(IDENT) {
416 name(p); 416 name(p);
417 } 417 }
418 // Special-case `macro_rules! try`.
419 // This is a hack until we do proper edition support
420
421 // test try_macro_rules
422 // macro_rules! try { () => {} }
423 if p.at(T![try]) {
424 let m = p.start();
425 p.bump_remap(IDENT);
426 m.complete(p, NAME);
427 }
428
418 match p.current() { 429 match p.current() {
419 T!['{'] => { 430 T!['{'] => {
420 token_tree(p); 431 token_tree(p);
diff --git a/crates/ra_parser/src/grammar/items/use_item.rs b/crates/ra_parser/src/grammar/items/use_item.rs
index e3b991c8c..3a0c7a31a 100644
--- a/crates/ra_parser/src/grammar/items/use_item.rs
+++ b/crates/ra_parser/src/grammar/items/use_item.rs
@@ -47,7 +47,7 @@ fn use_tree(p: &mut Parser, top_level: bool) {
47 // use {crate::path::from::root, or::path::from::crate_name}; // Rust 2018 (with a crate named `or`) 47 // use {crate::path::from::root, or::path::from::crate_name}; // Rust 2018 (with a crate named `or`)
48 // use {path::from::root}; // Rust 2015 48 // use {path::from::root}; // Rust 2015
49 // use ::{some::arbritrary::path}; // Rust 2015 49 // use ::{some::arbritrary::path}; // Rust 2015
50 // use ::{{{crate::export}}}; // Nonsensical but perfectly legal nestnig 50 // use ::{{{root::export}}}; // Nonsensical but perfectly legal nesting
51 T!['{'] => { 51 T!['{'] => {
52 use_tree_list(p); 52 use_tree_list(p);
53 } 53 }
diff --git a/crates/ra_parser/src/grammar/type_args.rs b/crates/ra_parser/src/grammar/type_args.rs
index 33d9973e9..2d61f9d80 100644
--- a/crates/ra_parser/src/grammar/type_args.rs
+++ b/crates/ra_parser/src/grammar/type_args.rs
@@ -48,7 +48,7 @@ fn type_arg(p: &mut Parser) {
48 m.complete(p, ASSOC_TYPE_ARG); 48 m.complete(p, ASSOC_TYPE_ARG);
49 } 49 }
50 T!['{'] => { 50 T!['{'] => {
51 expressions::block(p); 51 expressions::block_expr(p);
52 m.complete(p, CONST_ARG); 52 m.complete(p, CONST_ARG);
53 } 53 }
54 k if k.is_literal() => { 54 k if k.is_literal() => {