Skip to content

Commit 40620bf

Browse files
authored
Implement or-pattern (#84)
* Implement or-pattern * Remove spacial or-pattern inside match_pattern * Fix errors in tests due last commit
1 parent 8023833 commit 40620bf

File tree

6 files changed

+60567
-60980
lines changed

6 files changed

+60567
-60980
lines changed

corpus/expressions.txt

+71-19
Original file line numberDiff line numberDiff line change
@@ -414,25 +414,77 @@ let msg = match x {
414414
---
415415

416416
(source_file
417-
(match_expression (identifier) (match_block
418-
(match_arm (match_pattern (integer_literal)) (block (string_literal)))
419-
(match_arm (match_pattern (integer_literal)) (string_literal))
420-
(match_arm (match_pattern (negative_literal (integer_literal))) (integer_literal))
421-
(match_arm (match_pattern (negative_literal (float_literal))) (integer_literal))
422-
(match_arm
423-
(attribute_item (meta_item (identifier)))
424-
(match_pattern (integer_literal)) (string_literal))
425-
(match_arm
426-
(macro_invocation (identifier) (token_tree (integer_literal)))
427-
(string_literal))
428-
(match_arm (match_pattern) (string_literal))))
429-
(let_declaration (identifier) (match_expression (identifier) (match_block
430-
(match_arm (match_pattern (integer_literal) (integer_literal) (integer_literal)) (string_literal))
431-
(match_arm (match_pattern (identifier) (binary_expression (identifier) (integer_literal))) (string_literal))
432-
(match_arm
433-
(match_pattern (identifier) (binary_expression (identifier) (integer_literal)))
434-
(if_expression (identifier) (block (string_literal))))
435-
(match_arm (match_pattern) (string_literal))))))
417+
(match_expression
418+
value: (identifier)
419+
body: (match_block
420+
(match_arm
421+
pattern: (match_pattern
422+
(integer_literal))
423+
value: (block
424+
(string_literal)))
425+
(match_arm
426+
pattern: (match_pattern
427+
(integer_literal))
428+
value: (string_literal))
429+
(match_arm
430+
pattern: (match_pattern
431+
(negative_literal
432+
(integer_literal)))
433+
value: (integer_literal))
434+
(match_arm
435+
pattern: (match_pattern
436+
(negative_literal
437+
(float_literal)))
438+
value: (integer_literal))
439+
(match_arm
440+
(attribute_item
441+
(meta_item
442+
(identifier)))
443+
pattern: (match_pattern
444+
(integer_literal))
445+
value: (string_literal))
446+
(match_arm
447+
pattern: (macro_invocation
448+
macro: (identifier)
449+
(token_tree
450+
(integer_literal)))
451+
value: (string_literal))
452+
(match_arm
453+
pattern: (match_pattern)
454+
value: (string_literal))))
455+
(let_declaration
456+
pattern: (identifier)
457+
value: (match_expression
458+
value: (identifier)
459+
body: (match_block
460+
(match_arm
461+
pattern: (match_pattern
462+
(or_pattern
463+
(or_pattern
464+
(integer_literal)
465+
(integer_literal))
466+
(integer_literal)))
467+
value: (string_literal))
468+
(match_arm
469+
pattern: (match_pattern
470+
(identifier)
471+
condition: (binary_expression
472+
left: (identifier)
473+
right: (integer_literal)))
474+
value: (string_literal))
475+
(match_arm
476+
pattern: (match_pattern
477+
(identifier)
478+
condition: (binary_expression
479+
left: (identifier)
480+
right: (integer_literal)))
481+
value: (if_expression
482+
condition: (identifier)
483+
consequence: (block
484+
(string_literal))))
485+
(match_arm
486+
pattern: (match_pattern)
487+
value: (string_literal))))))
436488

437489
============================================
438490
While expressions

corpus/patterns.txt

+193-18
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,198 @@ match x {
115115
---
116116

117117
(source_file
118-
(match_expression (identifier) (match_block
119-
(match_arm
120-
(match_pattern
121-
(captured_pattern (identifier) (tuple_struct_pattern (identifier)))
122-
(captured_pattern (identifier) (tuple_struct_pattern (identifier) (remaining_field_pattern))))
123-
(identifier))
124-
(match_arm
125-
(match_pattern
126-
(captured_pattern (identifier) (range_pattern (integer_literal) (integer_literal))))
127-
(identifier))
128-
(match_arm
129-
(match_pattern
130-
(tuple_struct_pattern (identifier) (range_pattern (integer_literal) (integer_literal))))
118+
(match_expression
119+
value: (identifier)
120+
body: (match_block
121+
(match_arm
122+
pattern: (match_pattern
123+
(or_pattern
124+
(captured_pattern
125+
(identifier)
126+
(tuple_struct_pattern
127+
type: (identifier)))
128+
(captured_pattern
129+
(identifier)
130+
(tuple_struct_pattern
131+
type: (identifier)
132+
(remaining_field_pattern)))))
133+
value: (identifier))
134+
(match_arm
135+
pattern: (match_pattern
136+
(captured_pattern
137+
(identifier)
138+
(range_pattern
139+
(integer_literal)
140+
(integer_literal))))
141+
value: (identifier))
142+
(match_arm
143+
pattern: (match_pattern
144+
(tuple_struct_pattern
145+
type: (identifier)
146+
(range_pattern
147+
(integer_literal)
148+
(integer_literal))))
149+
value: (identifier))
150+
(match_arm
151+
pattern: (match_pattern
152+
(captured_pattern
153+
(identifier)
154+
(range_pattern
155+
(identifier)
156+
(identifier))))
157+
value: (identifier))
158+
(match_arm
159+
pattern: (match_pattern
160+
(captured_pattern
161+
(identifier)
162+
(range_pattern
163+
(identifier)
164+
(identifier))))
165+
value: (identifier)))))
166+
167+
=================================
168+
Or patterns
169+
=================================
170+
171+
if let A(x) | B(x) = expr {
172+
do_stuff_with(x);
173+
}
174+
175+
while let A(x) | B(x) = expr {
176+
do_stuff_with(x);
177+
}
178+
179+
let Ok(index) | Err(index) = slice.binary_search(&x);
180+
181+
for ref a | b in c {}
182+
183+
let Ok(x) | Err(x) = binary_search(x);
184+
185+
for A | B | C in c {}
186+
187+
|(Ok(x) | Err(x))| expr();
188+
189+
let ref mut x @ (A | B | C);
190+
191+
fn foo((1 | 2 | 3): u8) {}
192+
193+
// Discomment after box pattern land on master
194+
// let box (A | B | C);
195+
196+
// Not handled cause devs didn't got into agreement if should be acceptd or not
197+
// |Ok(x) | Err(x)| expr();
198+
199+
---
200+
201+
(source_file
202+
(if_let_expression
203+
pattern: (or_pattern
204+
(tuple_struct_pattern
205+
type: (identifier)
206+
(identifier))
207+
(tuple_struct_pattern
208+
type: (identifier)
209+
(identifier)))
210+
value: (identifier)
211+
consequence: (block
212+
(call_expression
213+
function: (identifier)
214+
arguments: (arguments
215+
(identifier)))))
216+
(while_let_expression
217+
pattern: (or_pattern
218+
(tuple_struct_pattern
219+
type: (identifier)
220+
(identifier))
221+
(tuple_struct_pattern
222+
type: (identifier)
223+
(identifier)))
224+
value: (identifier)
225+
body: (block
226+
(call_expression
227+
function: (identifier)
228+
arguments: (arguments
229+
(identifier)))))
230+
(let_declaration
231+
pattern: (or_pattern
232+
(tuple_struct_pattern
233+
type: (identifier)
234+
(identifier))
235+
(tuple_struct_pattern
236+
type: (identifier)
237+
(identifier)))
238+
value: (call_expression
239+
function: (field_expression
240+
value: (identifier)
241+
field: (field_identifier))
242+
arguments: (arguments
243+
(reference_expression
244+
value: (identifier)))))
245+
(for_expression
246+
pattern: (or_pattern
247+
(ref_pattern
248+
(identifier))
131249
(identifier))
132-
(match_arm
133-
(match_pattern (captured_pattern (identifier) (range_pattern (identifier) (identifier))))
250+
value: (identifier)
251+
body: (block))
252+
(let_declaration
253+
pattern: (or_pattern
254+
(tuple_struct_pattern
255+
type: (identifier)
256+
(identifier))
257+
(tuple_struct_pattern
258+
type: (identifier)
259+
(identifier)))
260+
value: (call_expression
261+
function: (identifier)
262+
arguments: (arguments
263+
(identifier))))
264+
(for_expression
265+
pattern: (or_pattern
266+
(or_pattern
267+
(identifier)
268+
(identifier))
134269
(identifier))
135-
(match_arm
136-
(match_pattern (captured_pattern (identifier) (range_pattern (identifier) (identifier))))
137-
(identifier)))))
270+
value: (identifier)
271+
body: (block))
272+
(closure_expression
273+
parameters: (closure_parameters
274+
(tuple_pattern
275+
(or_pattern
276+
(tuple_struct_pattern
277+
type: (identifier)
278+
(identifier))
279+
(tuple_struct_pattern
280+
type: (identifier)
281+
(identifier)))))
282+
body: (call_expression
283+
function: (identifier)
284+
arguments: (arguments)))
285+
(let_declaration
286+
pattern: (ref_pattern
287+
(mut_pattern
288+
(mutable_specifier)
289+
(captured_pattern
290+
(identifier)
291+
(tuple_pattern
292+
(or_pattern
293+
(or_pattern
294+
(identifier)
295+
(identifier))
296+
(identifier)))))))
297+
(function_item
298+
name: (identifier)
299+
parameters: (parameters
300+
(parameter
301+
pattern: (tuple_pattern
302+
(or_pattern
303+
(or_pattern
304+
(integer_literal)
305+
(integer_literal))
306+
(integer_literal)))
307+
type: (primitive_type)))
308+
body: (block))
309+
(line_comment)
310+
(line_comment)
311+
(line_comment)
312+
(line_comment))

grammar.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,6 @@ module.exports = grammar({
11251125

11261126
match_pattern: $ => seq(
11271127
$._pattern,
1128-
repeat(seq('|', $._pattern)),
11291128
optional(seq('if', field('condition', $._expression)))
11301129
),
11311130

@@ -1240,6 +1239,7 @@ module.exports = grammar({
12401239
$.remaining_field_pattern,
12411240
$.mut_pattern,
12421241
$.range_pattern,
1242+
$.or_pattern,
12431243
'_'
12441244
),
12451245

@@ -1328,6 +1328,12 @@ module.exports = grammar({
13281328
$._pattern
13291329
),
13301330

1331+
or_pattern: $ => prec.left(-2, seq(
1332+
$._pattern,
1333+
'|',
1334+
$._pattern,
1335+
)),
1336+
13311337
// Section - Literals
13321338

13331339
_literal: $ => choice(

0 commit comments

Comments
 (0)