aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-01-16 17:35:03 +0000
committerAleksey Kladov <[email protected]>2020-01-16 17:35:03 +0000
commitab0a11b1de3237a476639fee39fcf6ef506bdf34 (patch)
treed02418aa8a96b7df2703ed379f96310f7f4441b9
parentd3c4fbbbc45afc7d480185493b5ce77b5daa1747 (diff)
Simplify array parsing
-rw-r--r--crates/ra_parser/src/grammar/attributes.rs22
-rw-r--r--crates/ra_parser/src/grammar/expressions/atom.rs57
-rw-r--r--crates/ra_syntax/test_data/parser/err/0022_bad_exprs.txt33
3 files changed, 44 insertions, 68 deletions
diff --git a/crates/ra_parser/src/grammar/attributes.rs b/crates/ra_parser/src/grammar/attributes.rs
index eeae37aef..f3158ade3 100644
--- a/crates/ra_parser/src/grammar/attributes.rs
+++ b/crates/ra_parser/src/grammar/attributes.rs
@@ -8,28 +8,6 @@ pub(super) fn inner_attributes(p: &mut Parser) {
8 } 8 }
9} 9}
10 10
11pub(super) fn with_outer_attributes(
12 p: &mut Parser,
13 f: impl Fn(&mut Parser) -> Option<CompletedMarker>,
14) -> bool {
15 let am = p.start();
16 let has_attrs = p.at(T![#]);
17 attributes::outer_attributes(p);
18 let cm = f(p);
19 let success = cm.is_some();
20
21 match (has_attrs, cm) {
22 (true, Some(cm)) => {
23 let kind = cm.kind();
24 cm.undo_completion(p).abandon(p);
25 am.complete(p, kind);
26 }
27 _ => am.abandon(p),
28 }
29
30 success
31}
32
33pub(super) fn outer_attributes(p: &mut Parser) { 11pub(super) fn outer_attributes(p: &mut Parser) {
34 while p.at(T![#]) { 12 while p.at(T![#]) {
35 attribute(p, false) 13 attribute(p, false)
diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs
index 700994e80..5c413317e 100644
--- a/crates/ra_parser/src/grammar/expressions/atom.rs
+++ b/crates/ra_parser/src/grammar/expressions/atom.rs
@@ -181,29 +181,19 @@ fn tuple_expr(p: &mut Parser) -> CompletedMarker {
181fn array_expr(p: &mut Parser) -> CompletedMarker { 181fn array_expr(p: &mut Parser) -> CompletedMarker {
182 assert!(p.at(T!['['])); 182 assert!(p.at(T!['[']));
183 let m = p.start(); 183 let m = p.start();
184 p.bump(T!['[']);
185 if p.eat(T![']']) {
186 return m.complete(p, ARRAY_EXPR);
187 }
188 184
189 // test first_array_member_attributes 185 let mut n_exprs = 0u32;
190 // pub const A: &[i64] = &[ 186 let mut has_semi = false;
191 // #[cfg(test)]
192 // 1,
193 // 2,
194 // ];
195 attributes::with_outer_attributes(p, |p| expr(p).0);
196 187
197 if p.eat(T![;]) { 188 p.bump(T!['[']);
198 expr(p);
199 p.expect(T![']']);
200 return m.complete(p, ARRAY_EXPR);
201 }
202 while !p.at(EOF) && !p.at(T![']']) { 189 while !p.at(EOF) && !p.at(T![']']) {
203 p.expect(T![,]); 190 n_exprs += 1;
204 if p.at(T![']']) { 191 // test first_array_member_attributes
205 break; 192 // pub const A: &[i64] = &[
206 } 193 // #[cfg(test)]
194 // 1,
195 // 2,
196 // ];
207 197
208 // test subsequent_array_member_attributes 198 // test subsequent_array_member_attributes
209 // pub const A: &[i64] = &[ 199 // pub const A: &[i64] = &[
@@ -211,17 +201,32 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
211 // #[cfg(test)] 201 // #[cfg(test)]
212 // 2, 202 // 2,
213 // ]; 203 // ];
214 if !attributes::with_outer_attributes(p, |p| { 204 let m = p.start();
215 if !p.at_ts(EXPR_FIRST) { 205 let has_attrs = p.at(T![#]);
216 p.error("expected expression"); 206 attributes::outer_attributes(p);
217 return None; 207
208 let cm = expr(p).0;
209
210 match (has_attrs, cm) {
211 (true, Some(cm)) => {
212 let kind = cm.kind();
213 cm.undo_completion(p).abandon(p);
214 m.complete(p, kind);
218 } 215 }
219 expr(p).0 216 _ => m.abandon(p),
220 }) { 217 }
218
219 if n_exprs == 1 && p.eat(T![;]) {
220 has_semi = true;
221 continue;
222 }
223
224 if has_semi || !p.at(T![']']) && !p.expect(T![,]) {
221 break; 225 break;
222 } 226 }
223 } 227 }
224 p.expect(T![']']); 228 p.expect(T![']']);
229
225 m.complete(p, ARRAY_EXPR) 230 m.complete(p, ARRAY_EXPR)
226} 231}
227 232
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 ee0ac52c3..310a82464 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; 15) 15 EXPR_STMT@[9; 26)
16 ARRAY_EXPR@[9; 15) 16 ARRAY_EXPR@[9; 26)
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"
@@ -22,17 +22,13 @@ SOURCE_FILE@[0; 112)
22 LITERAL@[13; 14) 22 LITERAL@[13; 14)
23 INT_NUMBER@[13; 14) "2" 23 INT_NUMBER@[13; 14) "2"
24 COMMA@[14; 15) "," 24 COMMA@[14; 15) ","
25 WHITESPACE@[15; 16) " " 25 WHITESPACE@[15; 16) " "
26 EXPR_STMT@[16; 17) 26 ERROR@[16; 17)
27 ERROR@[16; 17) 27 AT@[16; 17) "@"
28 AT@[16; 17) "@"
29 EXPR_STMT@[17; 18)
30 ERROR@[17; 18)
31 COMMA@[17; 18) "," 28 COMMA@[17; 18) ","
32 WHITESPACE@[18; 19) " " 29 WHITESPACE@[18; 19) " "
33 STRUCT_DEF@[19; 26) 30 ERROR@[19; 25)
34 STRUCT_KW@[19; 25) "struct" 31 STRUCT_KW@[19; 25) "struct"
35 ERROR@[25; 26)
36 COMMA@[25; 26) "," 32 COMMA@[25; 26) ","
37 WHITESPACE@[26; 27) " " 33 WHITESPACE@[26; 27) " "
38 LET_STMT@[27; 31) 34 LET_STMT@[27; 31)
@@ -151,15 +147,12 @@ SOURCE_FILE@[0; 112)
151 WHITESPACE@[109; 110) " " 147 WHITESPACE@[109; 110) " "
152 R_CURLY@[110; 111) "}" 148 R_CURLY@[110; 111) "}"
153 WHITESPACE@[111; 112) "\n" 149 WHITESPACE@[111; 112) "\n"
154error 15: expected expression
155error 15: expected R_BRACK
156error 15: expected SEMI
157error 16: expected expression 150error 16: expected expression
158error 17: expected SEMI 151error 19: expected expression
159error 17: expected expression 152error 26: expected expression
160error 18: expected SEMI 153error 26: expected COMMA
161error 25: expected a name 154error 26: expected R_BRACK
162error 26: expected `;`, `{`, or `(` 155error 26: expected SEMI
163error 30: expected pattern 156error 30: expected pattern
164error 31: expected SEMI 157error 31: expected SEMI
165error 52: expected expression 158error 52: expected expression