microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ae523d4835a21c7ebd89c1f47afece8cd9264bbf

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

compiler/qsc_parse/src/expr/tests.rs

2183lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use super::expr;
5use crate::tests::check;
6use expect_test::expect;
7
8#[test]
9fn lit_int() {
10 check(expr, "123", &expect!["Expr _id_ [0-3]: Lit: Int(123)"]);
11}
12
13#[test]
14fn lit_int_underscore() {
15 check(
16 expr,
17 "123_456",
18 &expect!["Expr _id_ [0-7]: Lit: Int(123456)"],
19 );
20}
21
22#[test]
23fn lit_int_leading_zero() {
24 check(expr, "0123", &expect!["Expr _id_ [0-4]: Lit: Int(123)"]);
25}
26
27#[test]
28fn lit_int_max() {
29 check(
30 expr,
31 "9_223_372_036_854_775_807",
32 &expect!["Expr _id_ [0-25]: Lit: Int(9223372036854775807)"],
33 );
34}
35
36// NOTE: Since we need to support literals of value i64::MIN while also parsing the negative sign
37// as a unary operator, we need to allow one special case of overflow that is the absolute value
38// of i64::MIN. This will wrap to a negative value. See the `lit_int_min` test below.
39#[test]
40fn lit_int_overflow_min() {
41 check(
42 expr,
43 "9_223_372_036_854_775_808",
44 &expect!["Expr _id_ [0-25]: Lit: Int(-9223372036854775808)"],
45 );
46}
47
48#[test]
49fn lit_int_too_big() {
50 check(
51 expr,
52 "9_223_372_036_854_775_809",
53 &expect![[r#"
54 Error(
55 Lit(
56 "integer",
57 Span {
58 lo: 0,
59 hi: 25,
60 },
61 ),
62 )
63 "#]],
64 );
65}
66
67// NOTE: Since we need to support literals of value i64::MIN while also parsing the negative sign
68// as a unary operator, we need to allow one special case of overflow that is the absolute value
69// of i64::MIN. This will wrap to a negative value, and then negate of i64::MIN is i64::MIN, so
70// the correct value is achieved at runtime.
71#[test]
72fn lit_int_min() {
73 check(
74 expr,
75 "-9_223_372_036_854_775_808",
76 &expect![[r#"
77 Expr _id_ [0-26]: UnOp (Neg):
78 Expr _id_ [1-26]: Lit: Int(-9223372036854775808)"#]],
79 );
80}
81
82#[test]
83fn lit_int_hexadecimal() {
84 check(
85 expr,
86 "0x1a2b3c",
87 &expect!["Expr _id_ [0-8]: Lit: Int(1715004)"],
88 );
89}
90
91#[test]
92fn lit_int_octal() {
93 check(
94 expr,
95 "0o1234567",
96 &expect!["Expr _id_ [0-9]: Lit: Int(342391)"],
97 );
98}
99
100#[test]
101fn lit_int_binary() {
102 check(expr, "0b10110", &expect!["Expr _id_ [0-7]: Lit: Int(22)"]);
103}
104
105#[test]
106fn lit_bigint() {
107 check(expr, "123L", &expect!["Expr _id_ [0-4]: Lit: BigInt(123)"]);
108}
109
110#[test]
111fn lit_bigint_underscore() {
112 check(
113 expr,
114 "123_456L",
115 &expect!["Expr _id_ [0-8]: Lit: BigInt(123456)"],
116 );
117}
118
119#[test]
120fn lit_bigint_hexadecimal() {
121 check(
122 expr,
123 "0x1a2b3cL",
124 &expect!["Expr _id_ [0-9]: Lit: BigInt(1715004)"],
125 );
126}
127
128#[test]
129fn lit_bigint_octal() {
130 check(
131 expr,
132 "0o1234567L",
133 &expect!["Expr _id_ [0-10]: Lit: BigInt(342391)"],
134 );
135}
136
137#[test]
138fn lit_bigint_binary() {
139 check(
140 expr,
141 "0b10110L",
142 &expect!["Expr _id_ [0-8]: Lit: BigInt(22)"],
143 );
144}
145
146#[test]
147fn lit_double() {
148 check(expr, "1.23", &expect!["Expr _id_ [0-4]: Lit: Double(1.23)"]);
149}
150
151#[test]
152fn lit_double_leading_dot() {
153 check(
154 expr,
155 ".23",
156 &expect![[r#"
157 Error(
158 Rule(
159 "expression",
160 Dot,
161 Span {
162 lo: 0,
163 hi: 1,
164 },
165 ),
166 )
167 "#]],
168 );
169}
170
171#[test]
172fn lit_double_trailing_dot() {
173 check(expr, "1.", &expect!["Expr _id_ [0-2]: Lit: Double(1)"]);
174}
175
176#[test]
177fn lit_double_underscore() {
178 check(
179 expr,
180 "123_456.78",
181 &expect!["Expr _id_ [0-10]: Lit: Double(123456.78)"],
182 );
183}
184
185#[test]
186fn lit_double_leading_zero() {
187 check(expr, "0.23", &expect!["Expr _id_ [0-4]: Lit: Double(0.23)"]);
188}
189
190#[test]
191fn lit_double_trailing_exp_0() {
192 check(
193 expr,
194 "0e",
195 &expect![[r#"
196 Error(
197 Lit(
198 "floating-point",
199 Span {
200 lo: 0,
201 hi: 2,
202 },
203 ),
204 )
205 "#]],
206 );
207}
208
209#[test]
210fn lit_double_trailing_exp_1() {
211 check(
212 expr,
213 "1e",
214 &expect![[r#"
215 Error(
216 Lit(
217 "floating-point",
218 Span {
219 lo: 0,
220 hi: 2,
221 },
222 ),
223 )
224 "#]],
225 );
226}
227
228#[test]
229fn lit_double_trailing_dot_trailing_exp() {
230 check(
231 expr,
232 "1.e",
233 &expect![[r#"
234 Error(
235 Lit(
236 "floating-point",
237 Span {
238 lo: 0,
239 hi: 3,
240 },
241 ),
242 )
243 "#]],
244 );
245}
246
247#[test]
248fn lit_double_dot_trailing_exp() {
249 check(
250 expr,
251 "1.2e",
252 &expect![[r#"
253 Error(
254 Lit(
255 "floating-point",
256 Span {
257 lo: 0,
258 hi: 4,
259 },
260 ),
261 )
262 "#]],
263 );
264}
265
266#[test]
267fn lit_double_trailing_exp_dot() {
268 check(
269 expr,
270 "1e.",
271 &expect![[r#"
272 Error(
273 Lit(
274 "floating-point",
275 Span {
276 lo: 0,
277 hi: 2,
278 },
279 ),
280 )
281 "#]],
282 );
283}
284
285#[test]
286fn lit_int_hexadecimal_dot() {
287 check(expr, "0x123.45", &expect!["Expr _id_ [0-5]: Lit: Int(291)"]);
288}
289
290#[test]
291fn lit_string() {
292 check(
293 expr,
294 r#""foo""#,
295 &expect![[r#"Expr _id_ [0-5]: Lit: String("foo")"#]],
296 );
297}
298
299#[test]
300fn lit_string_escape_quote() {
301 check(
302 expr,
303 r#""foo\"bar""#,
304 &expect![[r#"Expr _id_ [0-10]: Lit: String("foo\"bar")"#]],
305 );
306}
307
308#[test]
309fn lit_string_escape_backslash() {
310 check(
311 expr,
312 r#""\\""#,
313 &expect![[r#"Expr _id_ [0-4]: Lit: String("\\")"#]],
314 );
315}
316
317#[test]
318fn lit_string_escape_newline() {
319 check(
320 expr,
321 r#""\n""#,
322 &expect![[r#"Expr _id_ [0-4]: Lit: String("\n")"#]],
323 );
324}
325
326#[test]
327fn lit_string_escape_carriage_return() {
328 check(
329 expr,
330 r#""\r""#,
331 &expect![[r#"Expr _id_ [0-4]: Lit: String("\r")"#]],
332 );
333}
334
335#[test]
336fn lit_string_escape_tab() {
337 check(
338 expr,
339 r#""\t""#,
340 &expect![[r#"Expr _id_ [0-4]: Lit: String("\t")"#]],
341 );
342}
343
344#[test]
345fn lit_string_unknown_escape() {
346 check(
347 expr,
348 r#""\x""#,
349 &expect![[r#"
350 Error(
351 Escape(
352 'x',
353 Span {
354 lo: 2,
355 hi: 3,
356 },
357 ),
358 )
359 "#]],
360 );
361}
362
363#[test]
364fn lit_string_unmatched_quote() {
365 check(
366 expr,
367 r#""Uh oh.."#,
368 &expect![[r#"
369 Error(
370 Rule(
371 "expression",
372 Eof,
373 Span {
374 lo: 8,
375 hi: 8,
376 },
377 ),
378 )
379
380 [
381 Error(
382 Lex(
383 UnterminatedString(
384 Span {
385 lo: 0,
386 hi: 0,
387 },
388 ),
389 ),
390 ),
391 ]"#]],
392 );
393}
394
395#[test]
396fn lit_string_empty() {
397 check(
398 expr,
399 r#""""#,
400 &expect![[r#"Expr _id_ [0-2]: Lit: String("")"#]],
401 );
402}
403
404#[test]
405fn lit_false() {
406 check(expr, "false", &expect!["Expr _id_ [0-5]: Lit: Bool(false)"]);
407}
408
409#[test]
410fn lit_true() {
411 check(expr, "true", &expect!["Expr _id_ [0-4]: Lit: Bool(true)"]);
412}
413
414#[test]
415fn lit_zero() {
416 check(expr, "Zero", &expect!["Expr _id_ [0-4]: Lit: Result(Zero)"]);
417}
418
419#[test]
420fn lit_one() {
421 check(expr, "One", &expect!["Expr _id_ [0-3]: Lit: Result(One)"]);
422}
423
424#[test]
425fn lit_pauli_i() {
426 check(expr, "PauliI", &expect!["Expr _id_ [0-6]: Lit: Pauli(I)"]);
427}
428
429#[test]
430fn lit_pauli_x() {
431 check(expr, "PauliX", &expect!["Expr _id_ [0-6]: Lit: Pauli(X)"]);
432}
433
434#[test]
435fn lit_pauli_y() {
436 check(expr, "PauliY", &expect!["Expr _id_ [0-6]: Lit: Pauli(Y)"]);
437}
438
439#[test]
440fn lit_pauli_z() {
441 check(expr, "PauliZ", &expect!["Expr _id_ [0-6]: Lit: Pauli(Z)"]);
442}
443
444#[test]
445fn hole() {
446 check(expr, "_", &expect!["Expr _id_ [0-1]: Hole"]);
447}
448
449#[test]
450fn single_path() {
451 check(
452 expr,
453 "foo",
454 &expect![[r#"Expr _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "foo")"#]],
455 );
456}
457
458#[test]
459fn double_path() {
460 check(
461 expr,
462 "foo.bar",
463 &expect![[
464 r#"Expr _id_ [0-7]: Path: Path _id_ [0-7] (Ident _id_ [0-3] "foo") (Ident _id_ [4-7] "bar")"#
465 ]],
466 );
467}
468
469#[test]
470fn fail() {
471 check(
472 expr,
473 r#"fail "message""#,
474 &expect![[r#"Expr _id_ [0-14]: Fail: Expr _id_ [5-14]: Lit: String("message")"#]],
475 );
476}
477
478#[test]
479fn for_in() {
480 check(
481 expr,
482 "for x in xs { x }",
483 &expect![[r#"
484 Expr _id_ [0-17]: For:
485 Pat _id_ [4-5]: Bind:
486 Ident _id_ [4-5] "x"
487 Expr _id_ [9-11]: Path: Path _id_ [9-11] (Ident _id_ [9-11] "xs")
488 Block _id_ [12-17]:
489 Stmt _id_ [14-15]: Expr: Expr _id_ [14-15]: Path: Path _id_ [14-15] (Ident _id_ [14-15] "x")"#]],
490 );
491}
492
493#[test]
494fn if_then() {
495 check(
496 expr,
497 "if c { e }",
498 &expect![[r#"
499 Expr _id_ [0-10]: If:
500 Expr _id_ [3-4]: Path: Path _id_ [3-4] (Ident _id_ [3-4] "c")
501 Block _id_ [5-10]:
502 Stmt _id_ [7-8]: Expr: Expr _id_ [7-8]: Path: Path _id_ [7-8] (Ident _id_ [7-8] "e")"#]],
503 );
504}
505
506#[test]
507fn if_else() {
508 check(
509 expr,
510 "if c { x } else { y }",
511 &expect![[r#"
512 Expr _id_ [0-21]: If:
513 Expr _id_ [3-4]: Path: Path _id_ [3-4] (Ident _id_ [3-4] "c")
514 Block _id_ [5-10]:
515 Stmt _id_ [7-8]: Expr: Expr _id_ [7-8]: Path: Path _id_ [7-8] (Ident _id_ [7-8] "x")
516 Expr _id_ [11-21]: Expr Block: Block _id_ [16-21]:
517 Stmt _id_ [18-19]: Expr: Expr _id_ [18-19]: Path: Path _id_ [18-19] (Ident _id_ [18-19] "y")"#]],
518 );
519}
520
521#[test]
522fn if_elif() {
523 check(
524 expr,
525 "if c1 { x } elif c2 { y }",
526 &expect![[r#"
527 Expr _id_ [0-25]: If:
528 Expr _id_ [3-5]: Path: Path _id_ [3-5] (Ident _id_ [3-5] "c1")
529 Block _id_ [6-11]:
530 Stmt _id_ [8-9]: Expr: Expr _id_ [8-9]: Path: Path _id_ [8-9] (Ident _id_ [8-9] "x")
531 Expr _id_ [12-25]: If:
532 Expr _id_ [17-19]: Path: Path _id_ [17-19] (Ident _id_ [17-19] "c2")
533 Block _id_ [20-25]:
534 Stmt _id_ [22-23]: Expr: Expr _id_ [22-23]: Path: Path _id_ [22-23] (Ident _id_ [22-23] "y")"#]],
535 );
536}
537
538#[test]
539fn if_elif_else() {
540 check(
541 expr,
542 "if c1 { x } elif c2 { y } else { z }",
543 &expect![[r#"
544 Expr _id_ [0-36]: If:
545 Expr _id_ [3-5]: Path: Path _id_ [3-5] (Ident _id_ [3-5] "c1")
546 Block _id_ [6-11]:
547 Stmt _id_ [8-9]: Expr: Expr _id_ [8-9]: Path: Path _id_ [8-9] (Ident _id_ [8-9] "x")
548 Expr _id_ [12-36]: If:
549 Expr _id_ [17-19]: Path: Path _id_ [17-19] (Ident _id_ [17-19] "c2")
550 Block _id_ [20-25]:
551 Stmt _id_ [22-23]: Expr: Expr _id_ [22-23]: Path: Path _id_ [22-23] (Ident _id_ [22-23] "y")
552 Expr _id_ [26-36]: Expr Block: Block _id_ [31-36]:
553 Stmt _id_ [33-34]: Expr: Expr _id_ [33-34]: Path: Path _id_ [33-34] (Ident _id_ [33-34] "z")"#]],
554 );
555}
556
557#[test]
558fn repeat_until() {
559 check(
560 expr,
561 "repeat { x } until c",
562 &expect![[r#"
563 Expr _id_ [0-20]: Repeat:
564 Block _id_ [7-12]:
565 Stmt _id_ [9-10]: Expr: Expr _id_ [9-10]: Path: Path _id_ [9-10] (Ident _id_ [9-10] "x")
566 Expr _id_ [19-20]: Path: Path _id_ [19-20] (Ident _id_ [19-20] "c")
567 <no fixup>"#]],
568 );
569}
570
571#[test]
572fn repeat_until_fixup() {
573 check(
574 expr,
575 "repeat { x } until c fixup { y }",
576 &expect![[r#"
577 Expr _id_ [0-32]: Repeat:
578 Block _id_ [7-12]:
579 Stmt _id_ [9-10]: Expr: Expr _id_ [9-10]: Path: Path _id_ [9-10] (Ident _id_ [9-10] "x")
580 Expr _id_ [19-20]: Path: Path _id_ [19-20] (Ident _id_ [19-20] "c")
581 Block _id_ [27-32]:
582 Stmt _id_ [29-30]: Expr: Expr _id_ [29-30]: Path: Path _id_ [29-30] (Ident _id_ [29-30] "y")"#]],
583 );
584}
585
586#[test]
587fn return_expr() {
588 check(
589 expr,
590 "return x",
591 &expect![[
592 r#"Expr _id_ [0-8]: Return: Expr _id_ [7-8]: Path: Path _id_ [7-8] (Ident _id_ [7-8] "x")"#
593 ]],
594 );
595}
596
597#[test]
598fn set() {
599 check(
600 expr,
601 "set x = y",
602 &expect![[r#"
603 Expr _id_ [0-9]: Assign:
604 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
605 Expr _id_ [8-9]: Path: Path _id_ [8-9] (Ident _id_ [8-9] "y")"#]],
606 );
607}
608
609#[test]
610fn set_hole() {
611 check(
612 expr,
613 "set _ = 1",
614 &expect![[r#"
615 Expr _id_ [0-9]: Assign:
616 Expr _id_ [4-5]: Hole
617 Expr _id_ [8-9]: Lit: Int(1)"#]],
618 );
619}
620
621#[test]
622fn set_hole_tuple() {
623 check(
624 expr,
625 "set (x, _) = (1, 2)",
626 &expect![[r#"
627 Expr _id_ [0-19]: Assign:
628 Expr _id_ [4-10]: Tuple:
629 Expr _id_ [5-6]: Path: Path _id_ [5-6] (Ident _id_ [5-6] "x")
630 Expr _id_ [8-9]: Hole
631 Expr _id_ [13-19]: Tuple:
632 Expr _id_ [14-15]: Lit: Int(1)
633 Expr _id_ [17-18]: Lit: Int(2)"#]],
634 );
635}
636
637#[test]
638fn set_hole_tuple_nested() {
639 check(
640 expr,
641 "set (_, (x, _)) = (1, (2, 3))",
642 &expect![[r#"
643 Expr _id_ [0-29]: Assign:
644 Expr _id_ [4-15]: Tuple:
645 Expr _id_ [5-6]: Hole
646 Expr _id_ [8-14]: Tuple:
647 Expr _id_ [9-10]: Path: Path _id_ [9-10] (Ident _id_ [9-10] "x")
648 Expr _id_ [12-13]: Hole
649 Expr _id_ [18-29]: Tuple:
650 Expr _id_ [19-20]: Lit: Int(1)
651 Expr _id_ [22-28]: Tuple:
652 Expr _id_ [23-24]: Lit: Int(2)
653 Expr _id_ [26-27]: Lit: Int(3)"#]],
654 );
655}
656
657#[test]
658fn set_bitwise_and() {
659 check(
660 expr,
661 "set x &&&= y",
662 &expect![[r#"
663 Expr _id_ [0-12]: AssignOp (AndB):
664 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
665 Expr _id_ [11-12]: Path: Path _id_ [11-12] (Ident _id_ [11-12] "y")"#]],
666 );
667}
668
669#[test]
670fn set_logical_and() {
671 check(
672 expr,
673 "set x and= y",
674 &expect![[r#"
675 Expr _id_ [0-12]: AssignOp (AndL):
676 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
677 Expr _id_ [11-12]: Path: Path _id_ [11-12] (Ident _id_ [11-12] "y")"#]],
678 );
679}
680
681#[test]
682fn set_bitwise_or() {
683 check(
684 expr,
685 "set x |||= y",
686 &expect![[r#"
687 Expr _id_ [0-12]: AssignOp (OrB):
688 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
689 Expr _id_ [11-12]: Path: Path _id_ [11-12] (Ident _id_ [11-12] "y")"#]],
690 );
691}
692
693#[test]
694fn set_exp() {
695 check(
696 expr,
697 "set x ^= y",
698 &expect![[r#"
699 Expr _id_ [0-10]: AssignOp (Exp):
700 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
701 Expr _id_ [9-10]: Path: Path _id_ [9-10] (Ident _id_ [9-10] "y")"#]],
702 );
703}
704
705#[test]
706fn set_bitwise_xor() {
707 check(
708 expr,
709 "set x ^^^= y",
710 &expect![[r#"
711 Expr _id_ [0-12]: AssignOp (XorB):
712 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
713 Expr _id_ [11-12]: Path: Path _id_ [11-12] (Ident _id_ [11-12] "y")"#]],
714 );
715}
716
717#[test]
718fn set_shr() {
719 check(
720 expr,
721 "set x >>>= y",
722 &expect![[r#"
723 Expr _id_ [0-12]: AssignOp (Shr):
724 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
725 Expr _id_ [11-12]: Path: Path _id_ [11-12] (Ident _id_ [11-12] "y")"#]],
726 );
727}
728
729#[test]
730fn set_shl() {
731 check(
732 expr,
733 "set x <<<= y",
734 &expect![[r#"
735 Expr _id_ [0-12]: AssignOp (Shl):
736 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
737 Expr _id_ [11-12]: Path: Path _id_ [11-12] (Ident _id_ [11-12] "y")"#]],
738 );
739}
740
741#[test]
742fn set_sub() {
743 check(
744 expr,
745 "set x -= y",
746 &expect![[r#"
747 Expr _id_ [0-10]: AssignOp (Sub):
748 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
749 Expr _id_ [9-10]: Path: Path _id_ [9-10] (Ident _id_ [9-10] "y")"#]],
750 );
751}
752
753#[test]
754fn set_logical_or() {
755 check(
756 expr,
757 "set x or= y",
758 &expect![[r#"
759 Expr _id_ [0-11]: AssignOp (OrL):
760 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
761 Expr _id_ [10-11]: Path: Path _id_ [10-11] (Ident _id_ [10-11] "y")"#]],
762 );
763}
764
765#[test]
766fn set_mod() {
767 check(
768 expr,
769 "set x %= y",
770 &expect![[r#"
771 Expr _id_ [0-10]: AssignOp (Mod):
772 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
773 Expr _id_ [9-10]: Path: Path _id_ [9-10] (Ident _id_ [9-10] "y")"#]],
774 );
775}
776
777#[test]
778fn set_add() {
779 check(
780 expr,
781 "set x += y",
782 &expect![[r#"
783 Expr _id_ [0-10]: AssignOp (Add):
784 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
785 Expr _id_ [9-10]: Path: Path _id_ [9-10] (Ident _id_ [9-10] "y")"#]],
786 );
787}
788
789#[test]
790fn set_div() {
791 check(
792 expr,
793 "set x /= y",
794 &expect![[r#"
795 Expr _id_ [0-10]: AssignOp (Div):
796 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
797 Expr _id_ [9-10]: Path: Path _id_ [9-10] (Ident _id_ [9-10] "y")"#]],
798 );
799}
800
801#[test]
802fn set_mul() {
803 check(
804 expr,
805 "set x *= y",
806 &expect![[r#"
807 Expr _id_ [0-10]: AssignOp (Mul):
808 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
809 Expr _id_ [9-10]: Path: Path _id_ [9-10] (Ident _id_ [9-10] "y")"#]],
810 );
811}
812
813#[test]
814fn set_with_update() {
815 check(
816 expr,
817 "set x w/= i <- y",
818 &expect![[r#"
819 Expr _id_ [0-16]: AssignUpdate:
820 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
821 Expr _id_ [10-11]: Path: Path _id_ [10-11] (Ident _id_ [10-11] "i")
822 Expr _id_ [15-16]: Path: Path _id_ [15-16] (Ident _id_ [15-16] "y")"#]],
823 );
824}
825
826#[test]
827fn while_expr() {
828 check(
829 expr,
830 "while c { x }",
831 &expect![[r#"
832 Expr _id_ [0-13]: While:
833 Expr _id_ [6-7]: Path: Path _id_ [6-7] (Ident _id_ [6-7] "c")
834 Block _id_ [8-13]:
835 Stmt _id_ [10-11]: Expr: Expr _id_ [10-11]: Path: Path _id_ [10-11] (Ident _id_ [10-11] "x")"#]],
836 );
837}
838
839#[test]
840fn within_apply() {
841 check(
842 expr,
843 "within { x } apply { y }",
844 &expect![[r#"
845 Expr _id_ [0-24]: Conjugate:
846 Block _id_ [7-12]:
847 Stmt _id_ [9-10]: Expr: Expr _id_ [9-10]: Path: Path _id_ [9-10] (Ident _id_ [9-10] "x")
848 Block _id_ [19-24]:
849 Stmt _id_ [21-22]: Expr: Expr _id_ [21-22]: Path: Path _id_ [21-22] (Ident _id_ [21-22] "y")"#]],
850 );
851}
852
853#[test]
854fn unit() {
855 check(expr, "()", &expect!["Expr _id_ [0-2]: Unit"]);
856}
857
858#[test]
859fn paren() {
860 check(
861 expr,
862 "(x)",
863 &expect![[
864 r#"Expr _id_ [0-3]: Paren: Expr _id_ [1-2]: Path: Path _id_ [1-2] (Ident _id_ [1-2] "x")"#
865 ]],
866 );
867}
868
869#[test]
870fn singleton_tuple() {
871 check(
872 expr,
873 "(x,)",
874 &expect![[r#"
875 Expr _id_ [0-4]: Tuple:
876 Expr _id_ [1-2]: Path: Path _id_ [1-2] (Ident _id_ [1-2] "x")"#]],
877 );
878}
879
880#[test]
881fn pair() {
882 check(
883 expr,
884 "(x, y)",
885 &expect![[r#"
886 Expr _id_ [0-6]: Tuple:
887 Expr _id_ [1-2]: Path: Path _id_ [1-2] (Ident _id_ [1-2] "x")
888 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "y")"#]],
889 );
890}
891
892#[test]
893fn array_empty() {
894 check(expr, "[]", &expect!["Expr _id_ [0-2]: Array:"]);
895}
896
897#[test]
898fn array_single() {
899 check(
900 expr,
901 "[x]",
902 &expect![[r#"
903 Expr _id_ [0-3]: Array:
904 Expr _id_ [1-2]: Path: Path _id_ [1-2] (Ident _id_ [1-2] "x")"#]],
905 );
906}
907
908#[test]
909fn array_pair() {
910 check(
911 expr,
912 "[x, y]",
913 &expect![[r#"
914 Expr _id_ [0-6]: Array:
915 Expr _id_ [1-2]: Path: Path _id_ [1-2] (Ident _id_ [1-2] "x")
916 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "y")"#]],
917 );
918}
919
920#[test]
921fn array_repeat() {
922 check(
923 expr,
924 "[0, size = 3]",
925 &expect![[r#"
926 Expr _id_ [0-13]: ArrayRepeat:
927 Expr _id_ [1-2]: Lit: Int(0)
928 Expr _id_ [11-12]: Lit: Int(3)"#]],
929 );
930}
931
932#[test]
933fn array_repeat_complex() {
934 check(
935 expr,
936 "[Foo(), size = Count() + 1]",
937 &expect![[r#"
938 Expr _id_ [0-27]: ArrayRepeat:
939 Expr _id_ [1-6]: Call:
940 Expr _id_ [1-4]: Path: Path _id_ [1-4] (Ident _id_ [1-4] "Foo")
941 Expr _id_ [4-6]: Unit
942 Expr _id_ [15-26]: BinOp (Add):
943 Expr _id_ [15-22]: Call:
944 Expr _id_ [15-20]: Path: Path _id_ [15-20] (Ident _id_ [15-20] "Count")
945 Expr _id_ [20-22]: Unit
946 Expr _id_ [25-26]: Lit: Int(1)"#]],
947 );
948}
949
950#[test]
951fn array_size_last_item() {
952 check(
953 expr,
954 "[foo, size]",
955 &expect![[r#"
956 Expr _id_ [0-11]: Array:
957 Expr _id_ [1-4]: Path: Path _id_ [1-4] (Ident _id_ [1-4] "foo")
958 Expr _id_ [6-10]: Path: Path _id_ [6-10] (Ident _id_ [6-10] "size")"#]],
959 );
960}
961
962#[test]
963fn array_size_middle_item() {
964 check(
965 expr,
966 "[foo, size, bar]",
967 &expect![[r#"
968 Expr _id_ [0-16]: Array:
969 Expr _id_ [1-4]: Path: Path _id_ [1-4] (Ident _id_ [1-4] "foo")
970 Expr _id_ [6-10]: Path: Path _id_ [6-10] (Ident _id_ [6-10] "size")
971 Expr _id_ [12-15]: Path: Path _id_ [12-15] (Ident _id_ [12-15] "bar")"#]],
972 );
973}
974
975#[test]
976fn array_repeat_no_items() {
977 check(
978 expr,
979 "[size = 3]",
980 &expect![[r#"
981 Error(
982 Token(
983 Close(
984 Bracket,
985 ),
986 Eq,
987 Span {
988 lo: 6,
989 hi: 7,
990 },
991 ),
992 )
993 "#]],
994 );
995}
996
997#[test]
998fn array_repeat_two_items() {
999 check(
1000 expr,
1001 "[1, 2, size = 3]",
1002 &expect![[r#"
1003 Error(
1004 Token(
1005 Close(
1006 Bracket,
1007 ),
1008 Eq,
1009 Span {
1010 lo: 12,
1011 hi: 13,
1012 },
1013 ),
1014 )
1015 "#]],
1016 );
1017}
1018
1019#[test]
1020fn array_concat() {
1021 check(
1022 expr,
1023 "[1, 2] + [3, 4]",
1024 &expect![[r#"
1025 Expr _id_ [0-15]: BinOp (Add):
1026 Expr _id_ [0-6]: Array:
1027 Expr _id_ [1-2]: Lit: Int(1)
1028 Expr _id_ [4-5]: Lit: Int(2)
1029 Expr _id_ [9-15]: Array:
1030 Expr _id_ [10-11]: Lit: Int(3)
1031 Expr _id_ [13-14]: Lit: Int(4)"#]],
1032 );
1033}
1034
1035#[test]
1036fn and_op() {
1037 check(
1038 expr,
1039 "x and y",
1040 &expect![[r#"
1041 Expr _id_ [0-7]: BinOp (AndL):
1042 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1043 Expr _id_ [6-7]: Path: Path _id_ [6-7] (Ident _id_ [6-7] "y")"#]],
1044 );
1045}
1046
1047#[test]
1048fn or_op() {
1049 check(
1050 expr,
1051 "x or y",
1052 &expect![[r#"
1053 Expr _id_ [0-6]: BinOp (OrL):
1054 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1055 Expr _id_ [5-6]: Path: Path _id_ [5-6] (Ident _id_ [5-6] "y")"#]],
1056 );
1057}
1058
1059#[test]
1060fn and_or_ops() {
1061 check(
1062 expr,
1063 "x or y and z",
1064 &expect![[r#"
1065 Expr _id_ [0-12]: BinOp (OrL):
1066 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1067 Expr _id_ [5-12]: BinOp (AndL):
1068 Expr _id_ [5-6]: Path: Path _id_ [5-6] (Ident _id_ [5-6] "y")
1069 Expr _id_ [11-12]: Path: Path _id_ [11-12] (Ident _id_ [11-12] "z")"#]],
1070 );
1071}
1072
1073#[test]
1074fn eq_op() {
1075 check(
1076 expr,
1077 "x == y",
1078 &expect![[r#"
1079 Expr _id_ [0-6]: BinOp (Eq):
1080 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1081 Expr _id_ [5-6]: Path: Path _id_ [5-6] (Ident _id_ [5-6] "y")"#]],
1082 );
1083}
1084
1085#[test]
1086fn ne_op() {
1087 check(
1088 expr,
1089 "x != y",
1090 &expect![[r#"
1091 Expr _id_ [0-6]: BinOp (Neq):
1092 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1093 Expr _id_ [5-6]: Path: Path _id_ [5-6] (Ident _id_ [5-6] "y")"#]],
1094 );
1095}
1096
1097#[test]
1098fn gt_op() {
1099 check(
1100 expr,
1101 "x > y",
1102 &expect![[r#"
1103 Expr _id_ [0-5]: BinOp (Gt):
1104 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1105 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "y")"#]],
1106 );
1107}
1108
1109#[test]
1110fn gte_op() {
1111 check(
1112 expr,
1113 "x >= y",
1114 &expect![[r#"
1115 Expr _id_ [0-6]: BinOp (Gte):
1116 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1117 Expr _id_ [5-6]: Path: Path _id_ [5-6] (Ident _id_ [5-6] "y")"#]],
1118 );
1119}
1120
1121#[test]
1122fn lt_op() {
1123 check(
1124 expr,
1125 "x < y",
1126 &expect![[r#"
1127 Expr _id_ [0-5]: BinOp (Lt):
1128 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1129 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "y")"#]],
1130 );
1131}
1132
1133#[test]
1134fn lte_op() {
1135 check(
1136 expr,
1137 "x <= y",
1138 &expect![[r#"
1139 Expr _id_ [0-6]: BinOp (Lte):
1140 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1141 Expr _id_ [5-6]: Path: Path _id_ [5-6] (Ident _id_ [5-6] "y")"#]],
1142 );
1143}
1144
1145#[test]
1146fn bitwise_and_op() {
1147 check(
1148 expr,
1149 "x &&& y",
1150 &expect![[r#"
1151 Expr _id_ [0-7]: BinOp (AndB):
1152 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1153 Expr _id_ [6-7]: Path: Path _id_ [6-7] (Ident _id_ [6-7] "y")"#]],
1154 );
1155}
1156
1157#[test]
1158fn bitwise_or_op() {
1159 check(
1160 expr,
1161 "x ||| y",
1162 &expect![[r#"
1163 Expr _id_ [0-7]: BinOp (OrB):
1164 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1165 Expr _id_ [6-7]: Path: Path _id_ [6-7] (Ident _id_ [6-7] "y")"#]],
1166 );
1167}
1168
1169#[test]
1170fn bitwise_and_or_op() {
1171 check(
1172 expr,
1173 "x ||| y &&& z",
1174 &expect![[r#"
1175 Expr _id_ [0-13]: BinOp (OrB):
1176 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1177 Expr _id_ [6-13]: BinOp (AndB):
1178 Expr _id_ [6-7]: Path: Path _id_ [6-7] (Ident _id_ [6-7] "y")
1179 Expr _id_ [12-13]: Path: Path _id_ [12-13] (Ident _id_ [12-13] "z")"#]],
1180 );
1181}
1182
1183#[test]
1184fn bitwise_xor_op() {
1185 check(
1186 expr,
1187 "x ^^^ y",
1188 &expect![[r#"
1189 Expr _id_ [0-7]: BinOp (XorB):
1190 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1191 Expr _id_ [6-7]: Path: Path _id_ [6-7] (Ident _id_ [6-7] "y")"#]],
1192 );
1193}
1194
1195#[test]
1196fn bitwise_or_xor_ops() {
1197 check(
1198 expr,
1199 "x ||| y ^^^ z",
1200 &expect![[r#"
1201 Expr _id_ [0-13]: BinOp (OrB):
1202 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1203 Expr _id_ [6-13]: BinOp (XorB):
1204 Expr _id_ [6-7]: Path: Path _id_ [6-7] (Ident _id_ [6-7] "y")
1205 Expr _id_ [12-13]: Path: Path _id_ [12-13] (Ident _id_ [12-13] "z")"#]],
1206 );
1207}
1208
1209#[test]
1210fn shl_op() {
1211 check(
1212 expr,
1213 "x <<< y",
1214 &expect![[r#"
1215 Expr _id_ [0-7]: BinOp (Shl):
1216 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1217 Expr _id_ [6-7]: Path: Path _id_ [6-7] (Ident _id_ [6-7] "y")"#]],
1218 );
1219}
1220
1221#[test]
1222fn shr_op() {
1223 check(
1224 expr,
1225 "x >>> y",
1226 &expect![[r#"
1227 Expr _id_ [0-7]: BinOp (Shr):
1228 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1229 Expr _id_ [6-7]: Path: Path _id_ [6-7] (Ident _id_ [6-7] "y")"#]],
1230 );
1231}
1232
1233#[test]
1234fn add_op() {
1235 check(
1236 expr,
1237 "x + y",
1238 &expect![[r#"
1239 Expr _id_ [0-5]: BinOp (Add):
1240 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1241 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "y")"#]],
1242 );
1243}
1244
1245#[test]
1246fn add_left_assoc() {
1247 check(
1248 expr,
1249 "x + y + z",
1250 &expect![[r#"
1251 Expr _id_ [0-9]: BinOp (Add):
1252 Expr _id_ [0-5]: BinOp (Add):
1253 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1254 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "y")
1255 Expr _id_ [8-9]: Path: Path _id_ [8-9] (Ident _id_ [8-9] "z")"#]],
1256 );
1257}
1258
1259#[test]
1260fn sub_op() {
1261 check(
1262 expr,
1263 "x - y",
1264 &expect![[r#"
1265 Expr _id_ [0-5]: BinOp (Sub):
1266 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1267 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "y")"#]],
1268 );
1269}
1270
1271#[test]
1272fn mul_op() {
1273 check(
1274 expr,
1275 "x * y",
1276 &expect![[r#"
1277 Expr _id_ [0-5]: BinOp (Mul):
1278 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1279 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "y")"#]],
1280 );
1281}
1282
1283#[test]
1284fn add_mul_ops() {
1285 check(
1286 expr,
1287 "x + y * z",
1288 &expect![[r#"
1289 Expr _id_ [0-9]: BinOp (Add):
1290 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1291 Expr _id_ [4-9]: BinOp (Mul):
1292 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "y")
1293 Expr _id_ [8-9]: Path: Path _id_ [8-9] (Ident _id_ [8-9] "z")"#]],
1294 );
1295}
1296
1297#[test]
1298fn div_op() {
1299 check(
1300 expr,
1301 "x / y",
1302 &expect![[r#"
1303 Expr _id_ [0-5]: BinOp (Div):
1304 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1305 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "y")"#]],
1306 );
1307}
1308
1309#[test]
1310fn mod_op() {
1311 check(
1312 expr,
1313 "x % y",
1314 &expect![[r#"
1315 Expr _id_ [0-5]: BinOp (Mod):
1316 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1317 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "y")"#]],
1318 );
1319}
1320
1321#[test]
1322fn two_plus_two_is_four() {
1323 check(
1324 expr,
1325 "2 + 2 == 4",
1326 &expect![[r#"
1327 Expr _id_ [0-10]: BinOp (Eq):
1328 Expr _id_ [0-5]: BinOp (Add):
1329 Expr _id_ [0-1]: Lit: Int(2)
1330 Expr _id_ [4-5]: Lit: Int(2)
1331 Expr _id_ [9-10]: Lit: Int(4)"#]],
1332 );
1333}
1334
1335#[test]
1336fn exp_op() {
1337 check(
1338 expr,
1339 "x ^ y",
1340 &expect![[r#"
1341 Expr _id_ [0-5]: BinOp (Exp):
1342 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1343 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "y")"#]],
1344 );
1345}
1346
1347#[test]
1348fn exp_right_assoc() {
1349 check(
1350 expr,
1351 "2 ^ 3 ^ 4",
1352 &expect![[r#"
1353 Expr _id_ [0-9]: BinOp (Exp):
1354 Expr _id_ [0-1]: Lit: Int(2)
1355 Expr _id_ [4-9]: BinOp (Exp):
1356 Expr _id_ [4-5]: Lit: Int(3)
1357 Expr _id_ [8-9]: Lit: Int(4)"#]],
1358 );
1359}
1360
1361#[test]
1362fn negate_exp() {
1363 check(
1364 expr,
1365 "-2^3",
1366 &expect![[r#"
1367 Expr _id_ [0-4]: UnOp (Neg):
1368 Expr _id_ [1-4]: BinOp (Exp):
1369 Expr _id_ [1-2]: Lit: Int(2)
1370 Expr _id_ [3-4]: Lit: Int(3)"#]],
1371 );
1372}
1373
1374#[test]
1375fn unwrap_op() {
1376 check(
1377 expr,
1378 "x!",
1379 &expect![[r#"
1380 Expr _id_ [0-2]: UnOp (Unwrap):
1381 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")"#]],
1382 );
1383}
1384
1385#[test]
1386fn logical_not_op() {
1387 check(
1388 expr,
1389 "not x",
1390 &expect![[r#"
1391 Expr _id_ [0-5]: UnOp (NotL):
1392 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")"#]],
1393 );
1394}
1395
1396#[test]
1397fn bitwise_not_op() {
1398 check(
1399 expr,
1400 "~~~x",
1401 &expect![[r#"
1402 Expr _id_ [0-4]: UnOp (NotB):
1403 Expr _id_ [3-4]: Path: Path _id_ [3-4] (Ident _id_ [3-4] "x")"#]],
1404 );
1405}
1406
1407#[test]
1408fn pos_op() {
1409 check(
1410 expr,
1411 "+x",
1412 &expect![[r#"
1413 Expr _id_ [0-2]: UnOp (Pos):
1414 Expr _id_ [1-2]: Path: Path _id_ [1-2] (Ident _id_ [1-2] "x")"#]],
1415 );
1416}
1417
1418#[test]
1419fn neg_op() {
1420 check(
1421 expr,
1422 "-x",
1423 &expect![[r#"
1424 Expr _id_ [0-2]: UnOp (Neg):
1425 Expr _id_ [1-2]: Path: Path _id_ [1-2] (Ident _id_ [1-2] "x")"#]],
1426 );
1427}
1428
1429#[test]
1430fn neg_minus_ops() {
1431 check(
1432 expr,
1433 "-x - y",
1434 &expect![[r#"
1435 Expr _id_ [0-6]: BinOp (Sub):
1436 Expr _id_ [0-2]: UnOp (Neg):
1437 Expr _id_ [1-2]: Path: Path _id_ [1-2] (Ident _id_ [1-2] "x")
1438 Expr _id_ [5-6]: Path: Path _id_ [5-6] (Ident _id_ [5-6] "y")"#]],
1439 );
1440}
1441
1442#[test]
1443fn adjoint_op() {
1444 check(
1445 expr,
1446 "Adjoint x",
1447 &expect![[r#"
1448 Expr _id_ [0-9]: UnOp (Functor Adj):
1449 Expr _id_ [8-9]: Path: Path _id_ [8-9] (Ident _id_ [8-9] "x")"#]],
1450 );
1451}
1452
1453#[test]
1454fn adjoint_call_ops() {
1455 check(
1456 expr,
1457 "Adjoint X(q)",
1458 &expect![[r#"
1459 Expr _id_ [0-12]: Call:
1460 Expr _id_ [0-9]: UnOp (Functor Adj):
1461 Expr _id_ [8-9]: Path: Path _id_ [8-9] (Ident _id_ [8-9] "X")
1462 Expr _id_ [9-12]: Paren: Expr _id_ [10-11]: Path: Path _id_ [10-11] (Ident _id_ [10-11] "q")"#]],
1463 );
1464}
1465
1466#[test]
1467fn adjoint_index_call_ops() {
1468 check(
1469 expr,
1470 "Adjoint ops[i](q)",
1471 &expect![[r#"
1472 Expr _id_ [0-17]: Call:
1473 Expr _id_ [0-14]: UnOp (Functor Adj):
1474 Expr _id_ [8-14]: Index:
1475 Expr _id_ [8-11]: Path: Path _id_ [8-11] (Ident _id_ [8-11] "ops")
1476 Expr _id_ [12-13]: Path: Path _id_ [12-13] (Ident _id_ [12-13] "i")
1477 Expr _id_ [14-17]: Paren: Expr _id_ [15-16]: Path: Path _id_ [15-16] (Ident _id_ [15-16] "q")"#]],
1478 );
1479}
1480
1481#[test]
1482fn controlled_op() {
1483 check(
1484 expr,
1485 "Controlled x",
1486 &expect![[r#"
1487 Expr _id_ [0-12]: UnOp (Functor Ctl):
1488 Expr _id_ [11-12]: Path: Path _id_ [11-12] (Ident _id_ [11-12] "x")"#]],
1489 );
1490}
1491
1492#[test]
1493fn controlled_call_ops() {
1494 check(
1495 expr,
1496 "Controlled X([q1], q2)",
1497 &expect![[r#"
1498 Expr _id_ [0-22]: Call:
1499 Expr _id_ [0-12]: UnOp (Functor Ctl):
1500 Expr _id_ [11-12]: Path: Path _id_ [11-12] (Ident _id_ [11-12] "X")
1501 Expr _id_ [12-22]: Tuple:
1502 Expr _id_ [13-17]: Array:
1503 Expr _id_ [14-16]: Path: Path _id_ [14-16] (Ident _id_ [14-16] "q1")
1504 Expr _id_ [19-21]: Path: Path _id_ [19-21] (Ident _id_ [19-21] "q2")"#]],
1505 );
1506}
1507
1508#[test]
1509fn controlled_index_call_ops() {
1510 check(
1511 expr,
1512 "Controlled ops[i]([q1], q2)",
1513 &expect![[r#"
1514 Expr _id_ [0-27]: Call:
1515 Expr _id_ [0-17]: UnOp (Functor Ctl):
1516 Expr _id_ [11-17]: Index:
1517 Expr _id_ [11-14]: Path: Path _id_ [11-14] (Ident _id_ [11-14] "ops")
1518 Expr _id_ [15-16]: Path: Path _id_ [15-16] (Ident _id_ [15-16] "i")
1519 Expr _id_ [17-27]: Tuple:
1520 Expr _id_ [18-22]: Array:
1521 Expr _id_ [19-21]: Path: Path _id_ [19-21] (Ident _id_ [19-21] "q1")
1522 Expr _id_ [24-26]: Path: Path _id_ [24-26] (Ident _id_ [24-26] "q2")"#]],
1523 );
1524}
1525
1526#[test]
1527fn update_op() {
1528 check(
1529 expr,
1530 "x w/ i <- v",
1531 &expect![[r#"
1532 Expr _id_ [0-11]: TernOp (Update):
1533 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1534 Expr _id_ [5-6]: Path: Path _id_ [5-6] (Ident _id_ [5-6] "i")
1535 Expr _id_ [10-11]: Path: Path _id_ [10-11] (Ident _id_ [10-11] "v")"#]],
1536 );
1537}
1538
1539#[test]
1540fn update_op_left_assoc() {
1541 check(
1542 expr,
1543 "x w/ i1 <- v1 w/ i2 <- v2",
1544 &expect![[r#"
1545 Expr _id_ [0-25]: TernOp (Update):
1546 Expr _id_ [0-13]: TernOp (Update):
1547 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1548 Expr _id_ [5-7]: Path: Path _id_ [5-7] (Ident _id_ [5-7] "i1")
1549 Expr _id_ [11-13]: Path: Path _id_ [11-13] (Ident _id_ [11-13] "v1")
1550 Expr _id_ [17-19]: Path: Path _id_ [17-19] (Ident _id_ [17-19] "i2")
1551 Expr _id_ [23-25]: Path: Path _id_ [23-25] (Ident _id_ [23-25] "v2")"#]],
1552 );
1553}
1554
1555#[test]
1556fn cond_op() {
1557 check(
1558 expr,
1559 "c ? a | b",
1560 &expect![[r#"
1561 Expr _id_ [0-9]: TernOp (Cond):
1562 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "c")
1563 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "a")
1564 Expr _id_ [8-9]: Path: Path _id_ [8-9] (Ident _id_ [8-9] "b")"#]],
1565 );
1566}
1567
1568#[test]
1569fn cond_op_right_assoc() {
1570 check(
1571 expr,
1572 "c1 ? a | c2 ? b | c",
1573 &expect![[r#"
1574 Expr _id_ [0-19]: TernOp (Cond):
1575 Expr _id_ [0-2]: Path: Path _id_ [0-2] (Ident _id_ [0-2] "c1")
1576 Expr _id_ [5-6]: Path: Path _id_ [5-6] (Ident _id_ [5-6] "a")
1577 Expr _id_ [9-19]: TernOp (Cond):
1578 Expr _id_ [9-11]: Path: Path _id_ [9-11] (Ident _id_ [9-11] "c2")
1579 Expr _id_ [14-15]: Path: Path _id_ [14-15] (Ident _id_ [14-15] "b")
1580 Expr _id_ [18-19]: Path: Path _id_ [18-19] (Ident _id_ [18-19] "c")"#]],
1581 );
1582}
1583
1584#[test]
1585fn field_op() {
1586 check(
1587 expr,
1588 "x::foo",
1589 &expect![[r#"
1590 Expr _id_ [0-6]: Field:
1591 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1592 Ident _id_ [3-6] "foo""#]],
1593 );
1594}
1595
1596#[test]
1597fn index_op() {
1598 check(
1599 expr,
1600 "x[i]",
1601 &expect![[r#"
1602 Expr _id_ [0-4]: Index:
1603 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1604 Expr _id_ [2-3]: Path: Path _id_ [2-3] (Ident _id_ [2-3] "i")"#]],
1605 );
1606}
1607
1608#[test]
1609fn call_op_unit() {
1610 check(
1611 expr,
1612 "Foo()",
1613 &expect![[r#"
1614 Expr _id_ [0-5]: Call:
1615 Expr _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Foo")
1616 Expr _id_ [3-5]: Unit"#]],
1617 );
1618}
1619
1620#[test]
1621fn call_op_one() {
1622 check(
1623 expr,
1624 "Foo(x)",
1625 &expect![[r#"
1626 Expr _id_ [0-6]: Call:
1627 Expr _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Foo")
1628 Expr _id_ [3-6]: Paren: Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")"#]],
1629 );
1630}
1631
1632#[test]
1633fn call_op_singleton_tuple() {
1634 check(
1635 expr,
1636 "Foo(x,)",
1637 &expect![[r#"
1638 Expr _id_ [0-7]: Call:
1639 Expr _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Foo")
1640 Expr _id_ [3-7]: Tuple:
1641 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")"#]],
1642 );
1643}
1644
1645#[test]
1646fn call_op_pair() {
1647 check(
1648 expr,
1649 "Foo(x, y)",
1650 &expect![[r#"
1651 Expr _id_ [0-9]: Call:
1652 Expr _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Foo")
1653 Expr _id_ [3-9]: Tuple:
1654 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "x")
1655 Expr _id_ [7-8]: Path: Path _id_ [7-8] (Ident _id_ [7-8] "y")"#]],
1656 );
1657}
1658
1659#[test]
1660fn call_with_array() {
1661 check(
1662 expr,
1663 "f([1, 2])",
1664 &expect![[r#"
1665 Expr _id_ [0-9]: Call:
1666 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "f")
1667 Expr _id_ [1-9]: Paren: Expr _id_ [2-8]: Array:
1668 Expr _id_ [3-4]: Lit: Int(1)
1669 Expr _id_ [6-7]: Lit: Int(2)"#]],
1670 );
1671}
1672
1673#[test]
1674fn call_partial_app() {
1675 check(
1676 expr,
1677 "Foo(1, _, 3)",
1678 &expect![[r#"
1679 Expr _id_ [0-12]: Call:
1680 Expr _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Foo")
1681 Expr _id_ [3-12]: Tuple:
1682 Expr _id_ [4-5]: Lit: Int(1)
1683 Expr _id_ [7-8]: Hole
1684 Expr _id_ [10-11]: Lit: Int(3)"#]],
1685 );
1686}
1687
1688#[test]
1689fn call_partial_app_nested() {
1690 check(
1691 expr,
1692 "Foo(1, _, (_, 4))",
1693 &expect![[r#"
1694 Expr _id_ [0-17]: Call:
1695 Expr _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Foo")
1696 Expr _id_ [3-17]: Tuple:
1697 Expr _id_ [4-5]: Lit: Int(1)
1698 Expr _id_ [7-8]: Hole
1699 Expr _id_ [10-16]: Tuple:
1700 Expr _id_ [11-12]: Hole
1701 Expr _id_ [14-15]: Lit: Int(4)"#]],
1702 );
1703}
1704
1705#[test]
1706fn call_index_ops() {
1707 check(
1708 expr,
1709 "f()[i]",
1710 &expect![[r#"
1711 Expr _id_ [0-6]: Index:
1712 Expr _id_ [0-3]: Call:
1713 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "f")
1714 Expr _id_ [1-3]: Unit
1715 Expr _id_ [4-5]: Path: Path _id_ [4-5] (Ident _id_ [4-5] "i")"#]],
1716 );
1717}
1718
1719#[test]
1720fn index_call_ops() {
1721 check(
1722 expr,
1723 "fs[i]()",
1724 &expect![[r#"
1725 Expr _id_ [0-7]: Call:
1726 Expr _id_ [0-5]: Index:
1727 Expr _id_ [0-2]: Path: Path _id_ [0-2] (Ident _id_ [0-2] "fs")
1728 Expr _id_ [3-4]: Path: Path _id_ [3-4] (Ident _id_ [3-4] "i")
1729 Expr _id_ [5-7]: Unit"#]],
1730 );
1731}
1732
1733#[test]
1734fn range_op() {
1735 check(
1736 expr,
1737 "x..y",
1738 &expect![[r#"
1739 Expr _id_ [0-4]: Range:
1740 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1741 <no step>
1742 Expr _id_ [3-4]: Path: Path _id_ [3-4] (Ident _id_ [3-4] "y")"#]],
1743 );
1744}
1745
1746#[test]
1747fn range_op_with_step() {
1748 check(
1749 expr,
1750 "x..y..z",
1751 &expect![[r#"
1752 Expr _id_ [0-7]: Range:
1753 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "x")
1754 Expr _id_ [3-4]: Path: Path _id_ [3-4] (Ident _id_ [3-4] "y")
1755 Expr _id_ [6-7]: Path: Path _id_ [6-7] (Ident _id_ [6-7] "z")"#]],
1756 );
1757}
1758
1759#[test]
1760fn range_complex_stop() {
1761 check(
1762 expr,
1763 "0..Length(xs) - 1",
1764 &expect![[r#"
1765 Expr _id_ [0-17]: Range:
1766 Expr _id_ [0-1]: Lit: Int(0)
1767 <no step>
1768 Expr _id_ [3-17]: BinOp (Sub):
1769 Expr _id_ [3-13]: Call:
1770 Expr _id_ [3-9]: Path: Path _id_ [3-9] (Ident _id_ [3-9] "Length")
1771 Expr _id_ [9-13]: Paren: Expr _id_ [10-12]: Path: Path _id_ [10-12] (Ident _id_ [10-12] "xs")
1772 Expr _id_ [16-17]: Lit: Int(1)"#]],
1773 );
1774}
1775
1776#[test]
1777fn range_complex_start() {
1778 check(
1779 expr,
1780 "i + 1..n",
1781 &expect![[r#"
1782 Expr _id_ [0-8]: Range:
1783 Expr _id_ [0-5]: BinOp (Add):
1784 Expr _id_ [0-1]: Path: Path _id_ [0-1] (Ident _id_ [0-1] "i")
1785 Expr _id_ [4-5]: Lit: Int(1)
1786 <no step>
1787 Expr _id_ [7-8]: Path: Path _id_ [7-8] (Ident _id_ [7-8] "n")"#]],
1788 );
1789}
1790
1791#[test]
1792fn range_complex_step() {
1793 check(
1794 expr,
1795 "0..s + 1..n",
1796 &expect![[r#"
1797 Expr _id_ [0-11]: Range:
1798 Expr _id_ [0-1]: Lit: Int(0)
1799 Expr _id_ [3-8]: BinOp (Add):
1800 Expr _id_ [3-4]: Path: Path _id_ [3-4] (Ident _id_ [3-4] "s")
1801 Expr _id_ [7-8]: Lit: Int(1)
1802 Expr _id_ [10-11]: Path: Path _id_ [10-11] (Ident _id_ [10-11] "n")"#]],
1803 );
1804}
1805
1806#[test]
1807fn range_start_open() {
1808 check(
1809 expr,
1810 "2...",
1811 &expect![[r#"
1812 Expr _id_ [0-4]: Range:
1813 Expr _id_ [0-1]: Lit: Int(2)
1814 <no step>
1815 <no end>"#]],
1816 );
1817}
1818
1819#[test]
1820fn range_start_step_open() {
1821 check(
1822 expr,
1823 "3..2...",
1824 &expect![[r#"
1825 Expr _id_ [0-7]: Range:
1826 Expr _id_ [0-1]: Lit: Int(3)
1827 Expr _id_ [3-4]: Lit: Int(2)
1828 <no end>"#]],
1829 );
1830}
1831
1832#[test]
1833fn range_open_stop() {
1834 check(
1835 expr,
1836 "...2",
1837 &expect![[r#"
1838 Expr _id_ [0-4]: Range:
1839 <no start>
1840 <no step>
1841 Expr _id_ [3-4]: Lit: Int(2)"#]],
1842 );
1843}
1844
1845#[test]
1846fn range_open_step_stop() {
1847 check(
1848 expr,
1849 "...2..3",
1850 &expect![[r#"
1851 Expr _id_ [0-7]: Range:
1852 <no start>
1853 Expr _id_ [3-4]: Lit: Int(2)
1854 Expr _id_ [6-7]: Lit: Int(3)"#]],
1855 );
1856}
1857
1858#[test]
1859fn range_open_step_open() {
1860 check(
1861 expr,
1862 "...2...",
1863 &expect![[r#"
1864 Expr _id_ [0-7]: Range:
1865 <no start>
1866 Expr _id_ [3-4]: Lit: Int(2)
1867 <no end>"#]],
1868 );
1869}
1870
1871#[test]
1872fn function_lambda() {
1873 check(
1874 expr,
1875 "x -> x + 1",
1876 &expect![[r#"
1877 Expr _id_ [0-10]: Lambda (Function):
1878 Pat _id_ [0-1]: Bind:
1879 Ident _id_ [0-1] "x"
1880 Expr _id_ [5-10]: BinOp (Add):
1881 Expr _id_ [5-6]: Path: Path _id_ [5-6] (Ident _id_ [5-6] "x")
1882 Expr _id_ [9-10]: Lit: Int(1)"#]],
1883 );
1884}
1885
1886#[test]
1887fn operation_lambda() {
1888 check(
1889 expr,
1890 "q => X(q)",
1891 &expect![[r#"
1892 Expr _id_ [0-9]: Lambda (Operation):
1893 Pat _id_ [0-1]: Bind:
1894 Ident _id_ [0-1] "q"
1895 Expr _id_ [5-9]: Call:
1896 Expr _id_ [5-6]: Path: Path _id_ [5-6] (Ident _id_ [5-6] "X")
1897 Expr _id_ [6-9]: Paren: Expr _id_ [7-8]: Path: Path _id_ [7-8] (Ident _id_ [7-8] "q")"#]],
1898 );
1899}
1900
1901#[test]
1902fn lambda_tuple_input() {
1903 check(
1904 expr,
1905 "(x, y) -> x + y",
1906 &expect![[r#"
1907 Expr _id_ [0-15]: Lambda (Function):
1908 Pat _id_ [0-6]: Tuple:
1909 Pat _id_ [1-2]: Bind:
1910 Ident _id_ [1-2] "x"
1911 Pat _id_ [4-5]: Bind:
1912 Ident _id_ [4-5] "y"
1913 Expr _id_ [10-15]: BinOp (Add):
1914 Expr _id_ [10-11]: Path: Path _id_ [10-11] (Ident _id_ [10-11] "x")
1915 Expr _id_ [14-15]: Path: Path _id_ [14-15] (Ident _id_ [14-15] "y")"#]],
1916 );
1917}
1918
1919#[test]
1920fn lambda_invalid_input() {
1921 check(
1922 expr,
1923 "x + 1 -> x",
1924 &expect![[r#"
1925 Error(
1926 Convert(
1927 "pattern",
1928 "expression",
1929 Span {
1930 lo: 0,
1931 hi: 5,
1932 },
1933 ),
1934 )
1935 "#]],
1936 );
1937}
1938
1939#[test]
1940fn lambda_invalid_tuple_input() {
1941 check(
1942 expr,
1943 "(x, y + 1) -> x + y",
1944 &expect![[r#"
1945 Error(
1946 Convert(
1947 "pattern",
1948 "expression",
1949 Span {
1950 lo: 4,
1951 hi: 9,
1952 },
1953 ),
1954 )
1955 "#]],
1956 );
1957}
1958
1959#[test]
1960fn interpolated_string_missing_ending() {
1961 check(
1962 expr,
1963 r#"$"string"#,
1964 &expect![[r#"
1965 Error(
1966 Rule(
1967 "expression",
1968 Eof,
1969 Span {
1970 lo: 8,
1971 hi: 8,
1972 },
1973 ),
1974 )
1975
1976 [
1977 Error(
1978 Lex(
1979 UnterminatedString(
1980 Span {
1981 lo: 0,
1982 hi: 0,
1983 },
1984 ),
1985 ),
1986 ),
1987 ]"#]],
1988 );
1989}
1990
1991#[test]
1992fn interpolated_string() {
1993 check(
1994 expr,
1995 r#"$"string""#,
1996 &expect![[r#"
1997 Expr _id_ [0-9]: Interpolate:
1998 Lit: "string""#]],
1999 );
2000}
2001
2002#[test]
2003fn interpolated_string_braced() {
2004 check(
2005 expr,
2006 r#"$"{x}""#,
2007 &expect![[r#"
2008 Expr _id_ [0-6]: Interpolate:
2009 Expr: Expr _id_ [3-4]: Path: Path _id_ [3-4] (Ident _id_ [3-4] "x")"#]],
2010 );
2011}
2012
2013#[test]
2014fn interpolated_string_escape_brace() {
2015 check(
2016 expr,
2017 r#"$"\{""#,
2018 &expect![[r#"
2019 Expr _id_ [0-5]: Interpolate:
2020 Lit: "\\{""#]],
2021 );
2022}
2023
2024#[test]
2025fn interpolated_string_unclosed_brace() {
2026 check(
2027 expr,
2028 r#"$"{"#,
2029 &expect![[r#"
2030 Error(
2031 Rule(
2032 "expression",
2033 Eof,
2034 Span {
2035 lo: 3,
2036 hi: 3,
2037 },
2038 ),
2039 )
2040 "#]],
2041 );
2042}
2043
2044#[test]
2045fn interpolated_string_unclosed_brace_quote() {
2046 check(
2047 expr,
2048 r#"$"{""#,
2049 &expect![[r#"
2050 Error(
2051 Rule(
2052 "expression",
2053 Eof,
2054 Span {
2055 lo: 4,
2056 hi: 4,
2057 },
2058 ),
2059 )
2060
2061 [
2062 Error(
2063 Lex(
2064 UnterminatedString(
2065 Span {
2066 lo: 3,
2067 hi: 3,
2068 },
2069 ),
2070 ),
2071 ),
2072 ]"#]],
2073 );
2074}
2075
2076#[test]
2077fn interpolated_string_unopened_brace() {
2078 check(
2079 expr,
2080 r#"$"}"#,
2081 &expect![[r#"
2082 Error(
2083 Rule(
2084 "expression",
2085 Eof,
2086 Span {
2087 lo: 3,
2088 hi: 3,
2089 },
2090 ),
2091 )
2092
2093 [
2094 Error(
2095 Lex(
2096 UnterminatedString(
2097 Span {
2098 lo: 0,
2099 hi: 0,
2100 },
2101 ),
2102 ),
2103 ),
2104 ]"#]],
2105 );
2106}
2107
2108#[test]
2109fn interpolated_string_unopened_brace_quote() {
2110 check(
2111 expr,
2112 r#"$"}""#,
2113 &expect![[r#"
2114 Expr _id_ [0-4]: Interpolate:
2115 Lit: "}""#]],
2116 );
2117}
2118
2119#[test]
2120fn interpolated_string_braced_index() {
2121 check(
2122 expr,
2123 r#"$"{xs[0]}""#,
2124 &expect![[r#"
2125 Expr _id_ [0-10]: Interpolate:
2126 Expr: Expr _id_ [3-8]: Index:
2127 Expr _id_ [3-5]: Path: Path _id_ [3-5] (Ident _id_ [3-5] "xs")
2128 Expr _id_ [6-7]: Lit: Int(0)"#]],
2129 );
2130}
2131
2132#[test]
2133fn interpolated_string_two_braced() {
2134 check(
2135 expr,
2136 r#"$"{x} {y}""#,
2137 &expect![[r#"
2138 Expr _id_ [0-10]: Interpolate:
2139 Expr: Expr _id_ [3-4]: Path: Path _id_ [3-4] (Ident _id_ [3-4] "x")
2140 Lit: " "
2141 Expr: Expr _id_ [7-8]: Path: Path _id_ [7-8] (Ident _id_ [7-8] "y")"#]],
2142 );
2143}
2144
2145#[test]
2146fn interpolated_string_braced_normal_string() {
2147 check(
2148 expr,
2149 r#"$"{"{}"}""#,
2150 &expect![[r#"
2151 Expr _id_ [0-9]: Interpolate:
2152 Expr: Expr _id_ [3-7]: Lit: String("{}")"#]],
2153 );
2154}
2155
2156#[test]
2157fn nested_interpolated_string() {
2158 check(
2159 expr,
2160 r#"$"{$"{x}"}""#,
2161 &expect![[r#"
2162 Expr _id_ [0-11]: Interpolate:
2163 Expr: Expr _id_ [3-9]: Interpolate:
2164 Expr: Expr _id_ [6-7]: Path: Path _id_ [6-7] (Ident _id_ [6-7] "x")"#]],
2165 );
2166}
2167
2168#[test]
2169fn nested_interpolated_string_with_exprs() {
2170 check(
2171 expr,
2172 r#"$"foo {x + $"bar {y}"} baz""#,
2173 &expect![[r#"
2174 Expr _id_ [0-27]: Interpolate:
2175 Lit: "foo "
2176 Expr: Expr _id_ [7-21]: BinOp (Add):
2177 Expr _id_ [7-8]: Path: Path _id_ [7-8] (Ident _id_ [7-8] "x")
2178 Expr _id_ [11-21]: Interpolate:
2179 Lit: "bar "
2180 Expr: Expr _id_ [18-19]: Path: Path _id_ [18-19] (Ident _id_ [18-19] "y")
2181 Lit: " baz""#]],
2182 );
2183}