aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2018-12-25 13:48:54 +0000
committerFlorian Diebold <[email protected]>2018-12-25 14:16:42 +0000
commit0d724ea572a5dd26acbbf2eb4538eabe454fb894 (patch)
tree077ffa2bda1405af9a91ea9c7ad61f9cb1fc0098 /crates
parent55c941cd9fb90c9340f01981e113aabd058b185b (diff)
Improve parsing of incomplete field accesses in preparation for field completion
We need to be able to get the receiver even if there is no field name yet, and currently "a." wouldn't get parsed as a field name at all. This seems to help.
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_syntax/src/grammar/expressions.rs16
-rw-r--r--crates/ra_syntax/tests/data/parser/err/0029_field_completion.rs3
-rw-r--r--crates/ra_syntax/tests/data/parser/err/0029_field_completion.txt35
3 files changed, 45 insertions, 9 deletions
diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs
index da78d85a2..2d1f17491 100644
--- a/crates/ra_syntax/src/grammar/expressions.rs
+++ b/crates/ra_syntax/src/grammar/expressions.rs
@@ -283,14 +283,10 @@ fn postfix_expr(
283 // } 283 // }
284 L_PAREN if allow_calls => call_expr(p, lhs), 284 L_PAREN if allow_calls => call_expr(p, lhs),
285 L_BRACK if allow_calls => index_expr(p, lhs), 285 L_BRACK if allow_calls => index_expr(p, lhs),
286 DOT if p.nth(1) == IDENT => { 286 DOT if p.nth(1) == IDENT && (p.nth(2) == L_PAREN || p.nth(2) == COLONCOLON) => {
287 if p.nth(2) == L_PAREN || p.nth(2) == COLONCOLON { 287 method_call_expr(p, lhs)
288 method_call_expr(p, lhs)
289 } else {
290 field_expr(p, lhs)
291 }
292 } 288 }
293 DOT if p.nth(1) == INT_NUMBER => field_expr(p, lhs), 289 DOT => field_expr(p, lhs),
294 // test postfix_range 290 // test postfix_range
295 // fn foo() { let x = 1..; } 291 // fn foo() { let x = 1..; }
296 DOTDOT | DOTDOTEQ if !EXPR_FIRST.contains(p.nth(1)) => { 292 DOTDOT | DOTDOTEQ if !EXPR_FIRST.contains(p.nth(1)) => {
@@ -355,13 +351,15 @@ fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
355// x.0.bar; 351// x.0.bar;
356// } 352// }
357fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker { 353fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
358 assert!(p.at(DOT) && (p.nth(1) == IDENT || p.nth(1) == INT_NUMBER)); 354 assert!(p.at(DOT));
359 let m = lhs.precede(p); 355 let m = lhs.precede(p);
360 p.bump(); 356 p.bump();
361 if p.at(IDENT) { 357 if p.at(IDENT) {
362 name_ref(p) 358 name_ref(p)
363 } else { 359 } else if p.at(INT_NUMBER) {
364 p.bump() 360 p.bump()
361 } else {
362 p.error("expected field name or number")
365 } 363 }
366 m.complete(p, FIELD_EXPR) 364 m.complete(p, FIELD_EXPR)
367} 365}
diff --git a/crates/ra_syntax/tests/data/parser/err/0029_field_completion.rs b/crates/ra_syntax/tests/data/parser/err/0029_field_completion.rs
new file mode 100644
index 000000000..a7cdc17bb
--- /dev/null
+++ b/crates/ra_syntax/tests/data/parser/err/0029_field_completion.rs
@@ -0,0 +1,3 @@
1fn foo(a: A) {
2 a.
3}
diff --git a/crates/ra_syntax/tests/data/parser/err/0029_field_completion.txt b/crates/ra_syntax/tests/data/parser/err/0029_field_completion.txt
new file mode 100644
index 000000000..fd2a3f37b
--- /dev/null
+++ b/crates/ra_syntax/tests/data/parser/err/0029_field_completion.txt
@@ -0,0 +1,35 @@
1SOURCE_FILE@[0; 24)
2 FN_DEF@[0; 23)
3 FN_KW@[0; 2)
4 WHITESPACE@[2; 3)
5 NAME@[3; 6)
6 IDENT@[3; 6) "foo"
7 PARAM_LIST@[6; 12)
8 L_PAREN@[6; 7)
9 PARAM@[7; 11)
10 BIND_PAT@[7; 8)
11 NAME@[7; 8)
12 IDENT@[7; 8) "a"
13 COLON@[8; 9)
14 WHITESPACE@[9; 10)
15 PATH_TYPE@[10; 11)
16 PATH@[10; 11)
17 PATH_SEGMENT@[10; 11)
18 NAME_REF@[10; 11)
19 IDENT@[10; 11) "A"
20 R_PAREN@[11; 12)
21 WHITESPACE@[12; 13)
22 BLOCK@[13; 23)
23 L_CURLY@[13; 14)
24 WHITESPACE@[14; 19)
25 FIELD_EXPR@[19; 21)
26 PATH_EXPR@[19; 20)
27 PATH@[19; 20)
28 PATH_SEGMENT@[19; 20)
29 NAME_REF@[19; 20)
30 IDENT@[19; 20) "a"
31 DOT@[20; 21)
32 err: `expected field name or number`
33 WHITESPACE@[21; 22)
34 R_CURLY@[22; 23)
35 WHITESPACE@[23; 24)