aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-01-17 10:44:40 +0000
committerAleksey Kladov <[email protected]>2020-01-17 10:44:40 +0000
commitb7c45fba57224a013fbc926abd2e8e9f8f3c77d4 (patch)
treed60e5d620be3a99cfd7eb1dceaa232e19c99e7e6 /crates
parent90b8a31b83b1aafc3fb555ba4307527f9258f46d (diff)
Extract expr_with_attrs
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_parser/src/grammar/expressions.rs20
-rw-r--r--crates/ra_parser/src/grammar/expressions/atom.rs15
-rw-r--r--crates/ra_syntax/test_data/parser/err/0022_bad_exprs.txt24
3 files changed, 36 insertions, 23 deletions
diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs
index a31a7a08d..3cf619e38 100644
--- a/crates/ra_parser/src/grammar/expressions.rs
+++ b/crates/ra_parser/src/grammar/expressions.rs
@@ -19,6 +19,26 @@ pub(super) fn expr(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {
19 expr_bp(p, r, 1) 19 expr_bp(p, r, 1)
20} 20}
21 21
22pub(super) fn expr_with_attrs(p: &mut Parser) -> bool {
23 let m = p.start();
24 let has_attrs = p.at(T![#]);
25 attributes::outer_attributes(p);
26
27 let (cm, _block_like) = expr(p);
28 let success = cm.is_some();
29
30 match (has_attrs, cm) {
31 (true, Some(cm)) => {
32 let kind = cm.kind();
33 cm.undo_completion(p).abandon(p);
34 m.complete(p, kind);
35 }
36 _ => m.abandon(p),
37 }
38
39 success
40}
41
22pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) { 42pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {
23 let r = Restrictions { forbid_structs: false, prefer_stmt: true }; 43 let r = Restrictions { forbid_structs: false, prefer_stmt: true };
24 expr_bp(p, r, 1) 44 expr_bp(p, r, 1)
diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs
index a98a2a3ef..2cc321473 100644
--- a/crates/ra_parser/src/grammar/expressions/atom.rs
+++ b/crates/ra_parser/src/grammar/expressions/atom.rs
@@ -191,19 +191,8 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
191 191
192 // test array_attrs 192 // test array_attrs
193 // const A: &[i64] = &[1, #[cfg(test)] 2]; 193 // const A: &[i64] = &[1, #[cfg(test)] 2];
194 let m = p.start(); 194 if !expr_with_attrs(p) {
195 let has_attrs = p.at(T![#]); 195 break;
196 attributes::outer_attributes(p);
197
198 let cm = expr(p).0;
199
200 match (has_attrs, cm) {
201 (true, Some(cm)) => {
202 let kind = cm.kind();
203 cm.undo_completion(p).abandon(p);
204 m.complete(p, kind);
205 }
206 _ => m.abandon(p),
207 } 196 }
208 197
209 if n_exprs == 1 && p.eat(T![;]) { 198 if n_exprs == 1 && p.eat(T![;]) {
diff --git a/crates/ra_syntax/test_data/parser/err/0022_bad_exprs.txt b/crates/ra_syntax/test_data/parser/err/0022_bad_exprs.txt
index 310a82464..2237eb413 100644
--- a/crates/ra_syntax/test_data/parser/err/0022_bad_exprs.txt
+++ b/crates/ra_syntax/test_data/parser/err/0022_bad_exprs.txt
@@ -12,8 +12,8 @@ SOURCE_FILE@[0; 112)
12 BLOCK@[7; 33) 12 BLOCK@[7; 33)
13 L_CURLY@[7; 8) "{" 13 L_CURLY@[7; 8) "{"
14 WHITESPACE@[8; 9) " " 14 WHITESPACE@[8; 9) " "
15 EXPR_STMT@[9; 26) 15 EXPR_STMT@[9; 17)
16 ARRAY_EXPR@[9; 26) 16 ARRAY_EXPR@[9; 17)
17 L_BRACK@[9; 10) "[" 17 L_BRACK@[9; 10) "["
18 LITERAL@[10; 11) 18 LITERAL@[10; 11)
19 INT_NUMBER@[10; 11) "1" 19 INT_NUMBER@[10; 11) "1"
@@ -25,10 +25,13 @@ SOURCE_FILE@[0; 112)
25 WHITESPACE@[15; 16) " " 25 WHITESPACE@[15; 16) " "
26 ERROR@[16; 17) 26 ERROR@[16; 17)
27 AT@[16; 17) "@" 27 AT@[16; 17) "@"
28 EXPR_STMT@[17; 18)
29 ERROR@[17; 18)
28 COMMA@[17; 18) "," 30 COMMA@[17; 18) ","
29 WHITESPACE@[18; 19) " " 31 WHITESPACE@[18; 19) " "
30 ERROR@[19; 25) 32 STRUCT_DEF@[19; 26)
31 STRUCT_KW@[19; 25) "struct" 33 STRUCT_KW@[19; 25) "struct"
34 ERROR@[25; 26)
32 COMMA@[25; 26) "," 35 COMMA@[25; 26) ","
33 WHITESPACE@[26; 27) " " 36 WHITESPACE@[26; 27) " "
34 LET_STMT@[27; 31) 37 LET_STMT@[27; 31)
@@ -148,11 +151,12 @@ SOURCE_FILE@[0; 112)
148 R_CURLY@[110; 111) "}" 151 R_CURLY@[110; 111) "}"
149 WHITESPACE@[111; 112) "\n" 152 WHITESPACE@[111; 112) "\n"
150error 16: expected expression 153error 16: expected expression
151error 19: expected expression 154error 17: expected R_BRACK
152error 26: expected expression 155error 17: expected SEMI
153error 26: expected COMMA 156error 17: expected expression
154error 26: expected R_BRACK 157error 18: expected SEMI
155error 26: expected SEMI 158error 25: expected a name
159error 26: expected `;`, `{`, or `(`
156error 30: expected pattern 160error 30: expected pattern
157error 31: expected SEMI 161error 31: expected SEMI
158error 52: expected expression 162error 52: expected expression