diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-05 10:28:24 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-05 10:28:24 +0000 |
commit | e7241274ef052fc3081b6dbc2e80266d6703c80c (patch) | |
tree | 4428c987a10a08a3cfb1eeaae497a9bc37a6af9a | |
parent | fe48f9f4d243aff59abc44e9ef9ccce9d43c01a8 (diff) | |
parent | eb1ac43867921501723457846f7aaaa36cd4ebdd (diff) |
Merge #930
930: Add support for parsing multiple if and while-let patterns r=matklad a=vipentti
Co-authored-by: Ville Penttinen <[email protected]>
-rw-r--r-- | crates/ra_parser/src/grammar/expressions/atom.rs | 14 | ||||
-rw-r--r-- | crates/ra_parser/src/grammar/patterns.rs | 16 | ||||
-rw-r--r-- | crates/ra_syntax/tests/data/parser/inline/ok/0030_cond.rs | 6 | ||||
-rw-r--r-- | crates/ra_syntax/tests/data/parser/inline/ok/0030_cond.txt | 159 |
4 files changed, 188 insertions, 7 deletions
diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index e74305b6a..9f282c74d 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs | |||
@@ -281,10 +281,16 @@ fn for_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker { | |||
281 | 281 | ||
282 | // test cond | 282 | // test cond |
283 | // fn foo() { if let Some(_) = None {} } | 283 | // fn foo() { if let Some(_) = None {} } |
284 | // fn bar() { | ||
285 | // if let Some(_) | Some(_) = None {} | ||
286 | // if let | Some(_) = None {} | ||
287 | // while let Some(_) | Some(_) = None {} | ||
288 | // while let | Some(_) = None {} | ||
289 | // } | ||
284 | fn cond(p: &mut Parser) { | 290 | fn cond(p: &mut Parser) { |
285 | let m = p.start(); | 291 | let m = p.start(); |
286 | if p.eat(LET_KW) { | 292 | if p.eat(LET_KW) { |
287 | patterns::pattern(p); | 293 | patterns::pattern_list(p); |
288 | p.expect(EQ); | 294 | p.expect(EQ); |
289 | } | 295 | } |
290 | expr_no_struct(p); | 296 | expr_no_struct(p); |
@@ -376,11 +382,7 @@ pub(crate) fn match_arm_list(p: &mut Parser) { | |||
376 | // } | 382 | // } |
377 | fn match_arm(p: &mut Parser) -> BlockLike { | 383 | fn match_arm(p: &mut Parser) -> BlockLike { |
378 | let m = p.start(); | 384 | let m = p.start(); |
379 | p.eat(PIPE); | 385 | patterns::pattern_list_r(p, TokenSet::empty()); |
380 | patterns::pattern_r(p, TokenSet::empty()); | ||
381 | while p.eat(PIPE) { | ||
382 | patterns::pattern(p); | ||
383 | } | ||
384 | if p.at(IF_KW) { | 386 | if p.at(IF_KW) { |
385 | match_guard(p); | 387 | match_guard(p); |
386 | } | 388 | } |
diff --git a/crates/ra_parser/src/grammar/patterns.rs b/crates/ra_parser/src/grammar/patterns.rs index 9d7da639d..befe6687d 100644 --- a/crates/ra_parser/src/grammar/patterns.rs +++ b/crates/ra_parser/src/grammar/patterns.rs | |||
@@ -8,6 +8,22 @@ pub(super) fn pattern(p: &mut Parser) { | |||
8 | pattern_r(p, PAT_RECOVERY_SET) | 8 | pattern_r(p, PAT_RECOVERY_SET) |
9 | } | 9 | } |
10 | 10 | ||
11 | /// Parses a pattern list separated by pipes `|` | ||
12 | pub(super) fn pattern_list(p: &mut Parser) { | ||
13 | pattern_list_r(p, PAT_RECOVERY_SET) | ||
14 | } | ||
15 | |||
16 | /// Parses a pattern list separated by pipes `|` | ||
17 | /// using the given `recovery_set` | ||
18 | pub(super) fn pattern_list_r(p: &mut Parser, recovery_set: TokenSet) { | ||
19 | p.eat(PIPE); | ||
20 | pattern_r(p, recovery_set); | ||
21 | |||
22 | while p.eat(PIPE) { | ||
23 | pattern_r(p, recovery_set); | ||
24 | } | ||
25 | } | ||
26 | |||
11 | pub(super) fn pattern_r(p: &mut Parser, recovery_set: TokenSet) { | 27 | pub(super) fn pattern_r(p: &mut Parser, recovery_set: TokenSet) { |
12 | if let Some(lhs) = atom_pat(p, recovery_set) { | 28 | if let Some(lhs) = atom_pat(p, recovery_set) { |
13 | // test range_pat | 29 | // test range_pat |
diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0030_cond.rs b/crates/ra_syntax/tests/data/parser/inline/ok/0030_cond.rs index fdb37ee6f..2552a2621 100644 --- a/crates/ra_syntax/tests/data/parser/inline/ok/0030_cond.rs +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0030_cond.rs | |||
@@ -1 +1,7 @@ | |||
1 | fn foo() { if let Some(_) = None {} } | 1 | fn foo() { if let Some(_) = None {} } |
2 | fn bar() { | ||
3 | if let Some(_) | Some(_) = None {} | ||
4 | if let | Some(_) = None {} | ||
5 | while let Some(_) | Some(_) = None {} | ||
6 | while let | Some(_) = None {} | ||
7 | } | ||
diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0030_cond.txt b/crates/ra_syntax/tests/data/parser/inline/ok/0030_cond.txt index 641a09fff..6dea1848b 100644 --- a/crates/ra_syntax/tests/data/parser/inline/ok/0030_cond.txt +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0030_cond.txt | |||
@@ -1,4 +1,4 @@ | |||
1 | SOURCE_FILE@[0; 38) | 1 | SOURCE_FILE@[0; 197) |
2 | FN_DEF@[0; 37) | 2 | FN_DEF@[0; 37) |
3 | FN_KW@[0; 2) | 3 | FN_KW@[0; 2) |
4 | WHITESPACE@[2; 3) | 4 | WHITESPACE@[2; 3) |
@@ -41,3 +41,160 @@ SOURCE_FILE@[0; 38) | |||
41 | WHITESPACE@[35; 36) | 41 | WHITESPACE@[35; 36) |
42 | R_CURLY@[36; 37) | 42 | R_CURLY@[36; 37) |
43 | WHITESPACE@[37; 38) | 43 | WHITESPACE@[37; 38) |
44 | FN_DEF@[38; 196) | ||
45 | FN_KW@[38; 40) | ||
46 | WHITESPACE@[40; 41) | ||
47 | NAME@[41; 44) | ||
48 | IDENT@[41; 44) "bar" | ||
49 | PARAM_LIST@[44; 46) | ||
50 | L_PAREN@[44; 45) | ||
51 | R_PAREN@[45; 46) | ||
52 | WHITESPACE@[46; 47) | ||
53 | BLOCK@[47; 196) | ||
54 | L_CURLY@[47; 48) | ||
55 | WHITESPACE@[48; 53) | ||
56 | EXPR_STMT@[53; 87) | ||
57 | IF_EXPR@[53; 87) | ||
58 | IF_KW@[53; 55) | ||
59 | WHITESPACE@[55; 56) | ||
60 | CONDITION@[56; 84) | ||
61 | LET_KW@[56; 59) | ||
62 | WHITESPACE@[59; 60) | ||
63 | TUPLE_STRUCT_PAT@[60; 67) | ||
64 | PATH@[60; 64) | ||
65 | PATH_SEGMENT@[60; 64) | ||
66 | NAME_REF@[60; 64) | ||
67 | IDENT@[60; 64) "Some" | ||
68 | L_PAREN@[64; 65) | ||
69 | PLACEHOLDER_PAT@[65; 66) | ||
70 | UNDERSCORE@[65; 66) | ||
71 | R_PAREN@[66; 67) | ||
72 | WHITESPACE@[67; 68) | ||
73 | PIPE@[68; 69) | ||
74 | WHITESPACE@[69; 70) | ||
75 | TUPLE_STRUCT_PAT@[70; 77) | ||
76 | PATH@[70; 74) | ||
77 | PATH_SEGMENT@[70; 74) | ||
78 | NAME_REF@[70; 74) | ||
79 | IDENT@[70; 74) "Some" | ||
80 | L_PAREN@[74; 75) | ||
81 | PLACEHOLDER_PAT@[75; 76) | ||
82 | UNDERSCORE@[75; 76) | ||
83 | R_PAREN@[76; 77) | ||
84 | WHITESPACE@[77; 78) | ||
85 | EQ@[78; 79) | ||
86 | WHITESPACE@[79; 80) | ||
87 | PATH_EXPR@[80; 84) | ||
88 | PATH@[80; 84) | ||
89 | PATH_SEGMENT@[80; 84) | ||
90 | NAME_REF@[80; 84) | ||
91 | IDENT@[80; 84) "None" | ||
92 | WHITESPACE@[84; 85) | ||
93 | BLOCK@[85; 87) | ||
94 | L_CURLY@[85; 86) | ||
95 | R_CURLY@[86; 87) | ||
96 | WHITESPACE@[87; 92) | ||
97 | EXPR_STMT@[92; 118) | ||
98 | IF_EXPR@[92; 118) | ||
99 | IF_KW@[92; 94) | ||
100 | WHITESPACE@[94; 95) | ||
101 | CONDITION@[95; 115) | ||
102 | LET_KW@[95; 98) | ||
103 | WHITESPACE@[98; 99) | ||
104 | PIPE@[99; 100) | ||
105 | WHITESPACE@[100; 101) | ||
106 | TUPLE_STRUCT_PAT@[101; 108) | ||
107 | PATH@[101; 105) | ||
108 | PATH_SEGMENT@[101; 105) | ||
109 | NAME_REF@[101; 105) | ||
110 | IDENT@[101; 105) "Some" | ||
111 | L_PAREN@[105; 106) | ||
112 | PLACEHOLDER_PAT@[106; 107) | ||
113 | UNDERSCORE@[106; 107) | ||
114 | R_PAREN@[107; 108) | ||
115 | WHITESPACE@[108; 109) | ||
116 | EQ@[109; 110) | ||
117 | WHITESPACE@[110; 111) | ||
118 | PATH_EXPR@[111; 115) | ||
119 | PATH@[111; 115) | ||
120 | PATH_SEGMENT@[111; 115) | ||
121 | NAME_REF@[111; 115) | ||
122 | IDENT@[111; 115) "None" | ||
123 | WHITESPACE@[115; 116) | ||
124 | BLOCK@[116; 118) | ||
125 | L_CURLY@[116; 117) | ||
126 | R_CURLY@[117; 118) | ||
127 | WHITESPACE@[118; 123) | ||
128 | EXPR_STMT@[123; 160) | ||
129 | WHILE_EXPR@[123; 160) | ||
130 | WHILE_KW@[123; 128) | ||
131 | WHITESPACE@[128; 129) | ||
132 | CONDITION@[129; 157) | ||
133 | LET_KW@[129; 132) | ||
134 | WHITESPACE@[132; 133) | ||
135 | TUPLE_STRUCT_PAT@[133; 140) | ||
136 | PATH@[133; 137) | ||
137 | PATH_SEGMENT@[133; 137) | ||
138 | NAME_REF@[133; 137) | ||
139 | IDENT@[133; 137) "Some" | ||
140 | L_PAREN@[137; 138) | ||
141 | PLACEHOLDER_PAT@[138; 139) | ||
142 | UNDERSCORE@[138; 139) | ||
143 | R_PAREN@[139; 140) | ||
144 | WHITESPACE@[140; 141) | ||
145 | PIPE@[141; 142) | ||
146 | WHITESPACE@[142; 143) | ||
147 | TUPLE_STRUCT_PAT@[143; 150) | ||
148 | PATH@[143; 147) | ||
149 | PATH_SEGMENT@[143; 147) | ||
150 | NAME_REF@[143; 147) | ||
151 | IDENT@[143; 147) "Some" | ||
152 | L_PAREN@[147; 148) | ||
153 | PLACEHOLDER_PAT@[148; 149) | ||
154 | UNDERSCORE@[148; 149) | ||
155 | R_PAREN@[149; 150) | ||
156 | WHITESPACE@[150; 151) | ||
157 | EQ@[151; 152) | ||
158 | WHITESPACE@[152; 153) | ||
159 | PATH_EXPR@[153; 157) | ||
160 | PATH@[153; 157) | ||
161 | PATH_SEGMENT@[153; 157) | ||
162 | NAME_REF@[153; 157) | ||
163 | IDENT@[153; 157) "None" | ||
164 | WHITESPACE@[157; 158) | ||
165 | BLOCK@[158; 160) | ||
166 | L_CURLY@[158; 159) | ||
167 | R_CURLY@[159; 160) | ||
168 | WHITESPACE@[160; 165) | ||
169 | WHILE_EXPR@[165; 194) | ||
170 | WHILE_KW@[165; 170) | ||
171 | WHITESPACE@[170; 171) | ||
172 | CONDITION@[171; 191) | ||
173 | LET_KW@[171; 174) | ||
174 | WHITESPACE@[174; 175) | ||
175 | PIPE@[175; 176) | ||
176 | WHITESPACE@[176; 177) | ||
177 | TUPLE_STRUCT_PAT@[177; 184) | ||
178 | PATH@[177; 181) | ||
179 | PATH_SEGMENT@[177; 181) | ||
180 | NAME_REF@[177; 181) | ||
181 | IDENT@[177; 181) "Some" | ||
182 | L_PAREN@[181; 182) | ||
183 | PLACEHOLDER_PAT@[182; 183) | ||
184 | UNDERSCORE@[182; 183) | ||
185 | R_PAREN@[183; 184) | ||
186 | WHITESPACE@[184; 185) | ||
187 | EQ@[185; 186) | ||
188 | WHITESPACE@[186; 187) | ||
189 | PATH_EXPR@[187; 191) | ||
190 | PATH@[187; 191) | ||
191 | PATH_SEGMENT@[187; 191) | ||
192 | NAME_REF@[187; 191) | ||
193 | IDENT@[187; 191) "None" | ||
194 | WHITESPACE@[191; 192) | ||
195 | BLOCK@[192; 194) | ||
196 | L_CURLY@[192; 193) | ||
197 | R_CURLY@[193; 194) | ||
198 | WHITESPACE@[194; 195) | ||
199 | R_CURLY@[195; 196) | ||
200 | WHITESPACE@[196; 197) | ||