1 // pest. The Elegant Parser
2 // Copyright (c) 2018 Dragoș Tiselice
3 //
4 // Licensed under the Apache License, Version 2.0
5 // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
6 // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. All files in the project carrying such notice may not be copied,
8 // modified, or distributed except according to those terms.
9 #![cfg_attr(not(feature = "std"), no_std)]
10 extern crate alloc;
11 use alloc::{format, vec::Vec};
12
13 #[macro_use]
14 extern crate pest;
15 #[macro_use]
16 extern crate pest_derive;
17
18 #[derive(Parser)]
19 #[grammar = "tests/grammar.pest"]
20 struct GrammarParser;
21
22 #[test]
string()23 fn string() {
24 parses_to! {
25 parser: GrammarParser,
26 input: "abc",
27 rule: Rule::string,
28 tokens: [
29 string(0, 3)
30 ]
31 };
32 }
33
34 #[test]
insensitive()35 fn insensitive() {
36 parses_to! {
37 parser: GrammarParser,
38 input: "aBC",
39 rule: Rule::insensitive,
40 tokens: [
41 insensitive(0, 3)
42 ]
43 };
44 }
45
46 #[test]
range()47 fn range() {
48 parses_to! {
49 parser: GrammarParser,
50 input: "6",
51 rule: Rule::range,
52 tokens: [
53 range(0, 1)
54 ]
55 };
56 }
57
58 #[test]
ident()59 fn ident() {
60 parses_to! {
61 parser: GrammarParser,
62 input: "abc",
63 rule: Rule::ident,
64 tokens: [
65 ident(0, 3, [
66 string(0, 3)
67 ])
68 ]
69 };
70 }
71
72 #[test]
pos_pred()73 fn pos_pred() {
74 parses_to! {
75 parser: GrammarParser,
76 input: "abc",
77 rule: Rule::pos_pred,
78 tokens: [
79 pos_pred(0, 0)
80 ]
81 };
82 }
83
84 #[test]
neg_pred()85 fn neg_pred() {
86 parses_to! {
87 parser: GrammarParser,
88 input: "",
89 rule: Rule::neg_pred,
90 tokens: [
91 neg_pred(0, 0)
92 ]
93 };
94 }
95
96 #[test]
double_neg_pred()97 fn double_neg_pred() {
98 parses_to! {
99 parser: GrammarParser,
100 input: "abc",
101 rule: Rule::double_neg_pred,
102 tokens: [
103 double_neg_pred(0, 0)
104 ]
105 };
106 }
107
108 #[test]
sequence()109 fn sequence() {
110 parses_to! {
111 parser: GrammarParser,
112 input: "abc abc",
113 rule: Rule::sequence,
114 tokens: [
115 sequence(0, 9, [
116 string(0, 3),
117 string(6, 9)
118 ])
119 ]
120 };
121 }
122
123 #[test]
sequence_compound()124 fn sequence_compound() {
125 parses_to! {
126 parser: GrammarParser,
127 input: "abcabc",
128 rule: Rule::sequence_compound,
129 tokens: [
130 sequence_compound(0, 6, [
131 string(0, 3),
132 string(3, 6)
133 ])
134 ]
135 };
136 }
137
138 #[test]
sequence_atomic()139 fn sequence_atomic() {
140 parses_to! {
141 parser: GrammarParser,
142 input: "abcabc",
143 rule: Rule::sequence_atomic,
144 tokens: [
145 sequence_atomic(0, 6)
146 ]
147 };
148 }
149
150 #[test]
sequence_non_atomic()151 fn sequence_non_atomic() {
152 parses_to! {
153 parser: GrammarParser,
154 input: "abc abc",
155 rule: Rule::sequence_non_atomic,
156 tokens: [
157 sequence_non_atomic(0, 9, [
158 sequence(0, 9, [
159 string(0, 3),
160 string(6, 9)
161 ])
162 ])
163 ]
164 };
165 }
166
167 #[test]
168 #[should_panic]
sequence_atomic_space()169 fn sequence_atomic_space() {
170 parses_to! {
171 parser: GrammarParser,
172 input: "abc abc",
173 rule: Rule::sequence_atomic,
174 tokens: []
175 };
176 }
177
178 #[test]
sequence_atomic_compound()179 fn sequence_atomic_compound() {
180 parses_to! {
181 parser: GrammarParser,
182 input: "abcabc",
183 rule: Rule::sequence_atomic_compound,
184 tokens: [
185 sequence_atomic_compound(0, 6, [
186 sequence_compound(0, 6, [
187 string(0, 3),
188 string(3, 6)
189 ])
190 ])
191 ]
192 };
193 }
194
195 #[test]
sequence_compound_nested()196 fn sequence_compound_nested() {
197 parses_to! {
198 parser: GrammarParser,
199 input: "abcabc",
200 rule: Rule::sequence_compound_nested,
201 tokens: [
202 sequence_compound_nested(0, 6, [
203 sequence_nested(0, 6, [
204 string(0, 3),
205 string(3, 6)
206 ])
207 ])
208 ]
209 };
210 }
211
212 #[test]
213 #[should_panic]
sequence_compound_nested_space()214 fn sequence_compound_nested_space() {
215 parses_to! {
216 parser: GrammarParser,
217 input: "abc abc",
218 rule: Rule::sequence_compound_nested,
219 tokens: []
220 };
221 }
222
223 #[test]
choice_string()224 fn choice_string() {
225 parses_to! {
226 parser: GrammarParser,
227 input: "abc",
228 rule: Rule::choice,
229 tokens: [
230 choice(0, 3, [
231 string(0, 3)
232 ])
233 ]
234 };
235 }
236
237 #[test]
choice_range()238 fn choice_range() {
239 parses_to! {
240 parser: GrammarParser,
241 input: "0",
242 rule: Rule::choice,
243 tokens: [
244 choice(0, 1, [
245 range(0, 1)
246 ])
247 ]
248 };
249 }
250
251 #[test]
choice_prefix()252 fn choice_prefix() {
253 parses_to! {
254 parser: GrammarParser,
255 input: "abc",
256 rule: Rule::choice_prefix,
257 tokens: [
258 choice_prefix(0, 3, [
259 string(0, 3)
260 ])
261 ]
262 };
263 }
264
265 #[test]
optional_string()266 fn optional_string() {
267 parses_to! {
268 parser: GrammarParser,
269 input: "abc",
270 rule: Rule::optional,
271 tokens: [
272 optional(0, 3, [
273 string(0, 3)
274 ])
275 ]
276 };
277 }
278
279 #[test]
optional_empty()280 fn optional_empty() {
281 parses_to! {
282 parser: GrammarParser,
283 input: "",
284 rule: Rule::optional,
285 tokens: [
286 optional(0, 0)
287 ]
288 };
289 }
290
291 #[test]
repeat_empty()292 fn repeat_empty() {
293 parses_to! {
294 parser: GrammarParser,
295 input: "",
296 rule: Rule::repeat,
297 tokens: [
298 repeat(0, 0)
299 ]
300 };
301 }
302
303 #[test]
repeat_strings()304 fn repeat_strings() {
305 parses_to! {
306 parser: GrammarParser,
307 input: "abc abc",
308 rule: Rule::repeat,
309 tokens: [
310 repeat(0, 9, [
311 string(0, 3),
312 string(6, 9)
313 ])
314 ]
315 };
316 }
317
318 #[test]
repeat_atomic_empty()319 fn repeat_atomic_empty() {
320 parses_to! {
321 parser: GrammarParser,
322 input: "",
323 rule: Rule::repeat_atomic,
324 tokens: [
325 repeat_atomic(0, 0)
326 ]
327 };
328 }
329
330 #[test]
repeat_atomic_strings()331 fn repeat_atomic_strings() {
332 parses_to! {
333 parser: GrammarParser,
334 input: "abcabc",
335 rule: Rule::repeat_atomic,
336 tokens: [
337 repeat_atomic(0, 6)
338 ]
339 };
340 }
341
342 #[test]
343 #[should_panic]
repeat_atomic_space()344 fn repeat_atomic_space() {
345 parses_to! {
346 parser: GrammarParser,
347 input: "abc abc",
348 rule: Rule::repeat_atomic,
349 tokens: []
350 };
351 }
352
353 #[test]
354 #[should_panic]
repeat_once_empty()355 fn repeat_once_empty() {
356 parses_to! {
357 parser: GrammarParser,
358 input: "",
359 rule: Rule::repeat_once,
360 tokens: []
361 };
362 }
363
364 #[test]
repeat_once_strings()365 fn repeat_once_strings() {
366 parses_to! {
367 parser: GrammarParser,
368 input: "abc abc",
369 rule: Rule::repeat_once,
370 tokens: [
371 repeat_once(0, 9, [
372 string(0, 3),
373 string(6, 9)
374 ])
375 ]
376 };
377 }
378
379 #[test]
380 #[should_panic]
repeat_once_atomic_empty()381 fn repeat_once_atomic_empty() {
382 parses_to! {
383 parser: GrammarParser,
384 input: "",
385 rule: Rule::repeat_once_atomic,
386 tokens: []
387 };
388 }
389
390 #[test]
repeat_once_atomic_strings()391 fn repeat_once_atomic_strings() {
392 parses_to! {
393 parser: GrammarParser,
394 input: "abcabc",
395 rule: Rule::repeat_once_atomic,
396 tokens: [
397 repeat_once_atomic(0, 6)
398 ]
399 };
400 }
401
402 #[test]
403 #[should_panic]
repeat_once_atomic_space()404 fn repeat_once_atomic_space() {
405 parses_to! {
406 parser: GrammarParser,
407 input: "abc abc",
408 rule: Rule::repeat_once_atomic,
409 tokens: []
410 };
411 }
412
413 #[test]
repeat_min_max_twice()414 fn repeat_min_max_twice() {
415 parses_to! {
416 parser: GrammarParser,
417 input: "abc abc",
418 rule: Rule::repeat_min_max,
419 tokens: [
420 repeat_min_max(0, 7, [
421 string(0, 3),
422 string(4, 7)
423 ])
424 ]
425 };
426 }
427
428 #[test]
repeat_min_max_thrice()429 fn repeat_min_max_thrice() {
430 parses_to! {
431 parser: GrammarParser,
432 input: "abc abc abc",
433 rule: Rule::repeat_min_max,
434 tokens: [
435 repeat_min_max(0, 11, [
436 string(0, 3),
437 string(4, 7),
438 string(8, 11)
439 ])
440 ]
441 };
442 }
443
444 #[test]
repeat_min_max_atomic_twice()445 fn repeat_min_max_atomic_twice() {
446 parses_to! {
447 parser: GrammarParser,
448 input: "abcabc",
449 rule: Rule::repeat_min_max_atomic,
450 tokens: [
451 repeat_min_max_atomic(0, 6)
452 ]
453 };
454 }
455
456 #[test]
repeat_min_max_atomic_thrice()457 fn repeat_min_max_atomic_thrice() {
458 parses_to! {
459 parser: GrammarParser,
460 input: "abcabcabc",
461 rule: Rule::repeat_min_max_atomic,
462 tokens: [
463 repeat_min_max_atomic(0, 9)
464 ]
465 };
466 }
467
468 #[test]
469 #[should_panic]
repeat_min_max_atomic_space()470 fn repeat_min_max_atomic_space() {
471 parses_to! {
472 parser: GrammarParser,
473 input: "abc abc",
474 rule: Rule::repeat_min_max_atomic,
475 tokens: []
476 };
477 }
478
479 #[test]
repeat_exact()480 fn repeat_exact() {
481 parses_to! {
482 parser: GrammarParser,
483 input: "abc abc",
484 rule: Rule::repeat_exact,
485 tokens: [
486 repeat_exact(0, 7, [
487 string(0, 3),
488 string(4, 7)
489 ])
490 ]
491 };
492 }
493
494 #[test]
495 #[should_panic]
repeat_min_once()496 fn repeat_min_once() {
497 parses_to! {
498 parser: GrammarParser,
499 input: "abc",
500 rule: Rule::repeat_min,
501 tokens: []
502 };
503 }
504
505 #[test]
repeat_min_twice()506 fn repeat_min_twice() {
507 parses_to! {
508 parser: GrammarParser,
509 input: "abc abc",
510 rule: Rule::repeat_min,
511 tokens: [
512 repeat_min(0, 7, [
513 string(0, 3),
514 string(4, 7)
515 ])
516 ]
517 };
518 }
519
520 #[test]
repeat_min_thrice()521 fn repeat_min_thrice() {
522 parses_to! {
523 parser: GrammarParser,
524 input: "abc abc abc",
525 rule: Rule::repeat_min,
526 tokens: [
527 repeat_min(0, 12, [
528 string(0, 3),
529 string(4, 7),
530 string(9, 12)
531 ])
532 ]
533 };
534 }
535
536 #[test]
537 #[should_panic]
repeat_min_atomic_once()538 fn repeat_min_atomic_once() {
539 parses_to! {
540 parser: GrammarParser,
541 input: "abc",
542 rule: Rule::repeat_min_atomic,
543 tokens: []
544 };
545 }
546
547 #[test]
repeat_min_atomic_twice()548 fn repeat_min_atomic_twice() {
549 parses_to! {
550 parser: GrammarParser,
551 input: "abcabc",
552 rule: Rule::repeat_min_atomic,
553 tokens: [
554 repeat_min_atomic(0, 6)
555 ]
556 };
557 }
558
559 #[test]
repeat_min_atomic_thrice()560 fn repeat_min_atomic_thrice() {
561 parses_to! {
562 parser: GrammarParser,
563 input: "abcabcabc",
564 rule: Rule::repeat_min_atomic,
565 tokens: [
566 repeat_min_atomic(0, 9)
567 ]
568 };
569 }
570
571 #[test]
572 #[should_panic]
repeat_min_atomic_space()573 fn repeat_min_atomic_space() {
574 parses_to! {
575 parser: GrammarParser,
576 input: "abc abc",
577 rule: Rule::repeat_min_atomic,
578 tokens: []
579 };
580 }
581
582 #[test]
repeat_max_once()583 fn repeat_max_once() {
584 parses_to! {
585 parser: GrammarParser,
586 input: "abc",
587 rule: Rule::repeat_max,
588 tokens: [
589 repeat_max(0, 3, [
590 string(0, 3)
591 ])
592 ]
593 };
594 }
595
596 #[test]
repeat_max_twice()597 fn repeat_max_twice() {
598 parses_to! {
599 parser: GrammarParser,
600 input: "abc abc",
601 rule: Rule::repeat_max,
602 tokens: [
603 repeat_max(0, 7, [
604 string(0, 3),
605 string(4, 7)
606 ])
607 ]
608 };
609 }
610
611 #[test]
612 #[should_panic]
repeat_max_thrice()613 fn repeat_max_thrice() {
614 parses_to! {
615 parser: GrammarParser,
616 input: "abc abc",
617 rule: Rule::repeat_max,
618 tokens: []
619 };
620 }
621
622 #[test]
repeat_max_atomic_once()623 fn repeat_max_atomic_once() {
624 parses_to! {
625 parser: GrammarParser,
626 input: "abc",
627 rule: Rule::repeat_max_atomic,
628 tokens: [
629 repeat_max_atomic(0, 3)
630 ]
631 };
632 }
633
634 #[test]
repeat_max_atomic_twice()635 fn repeat_max_atomic_twice() {
636 parses_to! {
637 parser: GrammarParser,
638 input: "abcabc",
639 rule: Rule::repeat_max_atomic,
640 tokens: [
641 repeat_max_atomic(0, 6)
642 ]
643 };
644 }
645
646 #[test]
647 #[should_panic]
repeat_max_atomic_thrice()648 fn repeat_max_atomic_thrice() {
649 parses_to! {
650 parser: GrammarParser,
651 input: "abcabcabc",
652 rule: Rule::repeat_max_atomic,
653 tokens: []
654 };
655 }
656
657 #[test]
658 #[should_panic]
repeat_max_atomic_space()659 fn repeat_max_atomic_space() {
660 parses_to! {
661 parser: GrammarParser,
662 input: "abc abc",
663 rule: Rule::repeat_max_atomic,
664 tokens: []
665 };
666 }
667
668 #[test]
repeat_comment()669 fn repeat_comment() {
670 parses_to! {
671 parser: GrammarParser,
672 input: "abc$$$ $$$abc",
673 rule: Rule::repeat_once,
674 tokens: [
675 repeat_once(0, 13, [
676 string(0, 3),
677 string(10, 13)
678 ])
679 ]
680 };
681 }
682
683 #[test]
soi_at_start()684 fn soi_at_start() {
685 parses_to! {
686 parser: GrammarParser,
687 input: "abc",
688 rule: Rule::soi_at_start,
689 tokens: [
690 soi_at_start(0, 3, [
691 string(0, 3)
692 ])
693 ]
694 };
695 }
696
697 #[test]
peek()698 fn peek() {
699 parses_to! {
700 parser: GrammarParser,
701 input: "0111",
702 rule: Rule::peek_,
703 tokens: [
704 peek_(0, 4, [
705 range(0, 1),
706 range(1, 2)
707 ])
708 ]
709 };
710 }
711
712 #[test]
peek_all()713 fn peek_all() {
714 parses_to! {
715 parser: GrammarParser,
716 input: "0110",
717 rule: Rule::peek_all,
718 tokens: [
719 peek_all(0, 4, [
720 range(0, 1),
721 range(1, 2)
722 ])
723 ]
724 };
725 }
726
727 #[test]
peek_slice_23()728 fn peek_slice_23() {
729 parses_to! {
730 parser: GrammarParser,
731 input: "0123412",
732 rule: Rule::peek_slice_23,
733 tokens: [
734 peek_slice_23(0, 7, [
735 range(0, 1),
736 range(1, 2),
737 range(2, 3),
738 range(3, 4),
739 range(4, 5),
740 ])
741 ]
742 };
743 }
744
745 #[test]
pop()746 fn pop() {
747 parses_to! {
748 parser: GrammarParser,
749 input: "0110",
750 rule: Rule::pop_,
751 tokens: [
752 pop_(0, 4, [
753 range(0, 1),
754 range(1, 2)
755 ])
756 ]
757 };
758 }
759
760 #[test]
pop_all()761 fn pop_all() {
762 parses_to! {
763 parser: GrammarParser,
764 input: "0110",
765 rule: Rule::pop_all,
766 tokens: [
767 pop_all(0, 4, [
768 range(0, 1),
769 range(1, 2)
770 ])
771 ]
772 };
773 }
774
775 #[test]
pop_fail()776 fn pop_fail() {
777 parses_to! {
778 parser: GrammarParser,
779 input: "010",
780 rule: Rule::pop_fail,
781 tokens: [
782 pop_fail(0, 3, [
783 range(0, 1),
784 range(1, 2)
785 ])
786 ]
787 };
788 }
789
790 #[test]
repeat_mutate_stack()791 fn repeat_mutate_stack() {
792 parses_to! {
793 parser: GrammarParser,
794 input: "a,b,c,cba",
795 rule: Rule::repeat_mutate_stack,
796 tokens: [
797 repeat_mutate_stack(0, 9)
798 ]
799 };
800 }
801
802 #[test]
stack_resume_after_fail()803 fn stack_resume_after_fail() {
804 parses_to! {
805 parser: GrammarParser,
806 input: "a,b,c,cba",
807 rule: Rule::stack_resume_after_fail,
808 tokens: [
809 stack_resume_after_fail(0, 9, [
810 repeat_mutate_stack_pop_all(0, 9)
811 ])
812 ]
813 };
814 }
815
816 #[test]
checkpoint_restore()817 fn checkpoint_restore() {
818 parses_to! {
819 parser: GrammarParser,
820 input: "a",
821 rule: Rule::checkpoint_restore,
822 tokens: [
823 checkpoint_restore(0, 1, [EOI(1, 1)])
824 ]
825 };
826 }
827
828 #[test]
ascii_digits()829 fn ascii_digits() {
830 parses_to! {
831 parser: GrammarParser,
832 input: "6",
833 rule: Rule::ascii_digits,
834 tokens: [
835 ascii_digits(0, 1)
836 ]
837 };
838 }
839
840 #[test]
ascii_nonzero_digits()841 fn ascii_nonzero_digits() {
842 parses_to! {
843 parser: GrammarParser,
844 input: "5",
845 rule: Rule::ascii_nonzero_digits,
846 tokens: [
847 ascii_nonzero_digits(0, 1)
848 ]
849 };
850 }
851
852 #[test]
ascii_bin_digits()853 fn ascii_bin_digits() {
854 parses_to! {
855 parser: GrammarParser,
856 input: "1",
857 rule: Rule::ascii_bin_digits,
858 tokens: [
859 ascii_bin_digits(0, 1)
860 ]
861 };
862 }
863
864 #[test]
ascii_oct_digits()865 fn ascii_oct_digits() {
866 parses_to! {
867 parser: GrammarParser,
868 input: "3",
869 rule: Rule::ascii_oct_digits,
870 tokens: [
871 ascii_oct_digits(0, 1)
872 ]
873 };
874 }
875
876 #[test]
ascii_hex_digits()877 fn ascii_hex_digits() {
878 parses_to! {
879 parser: GrammarParser,
880 input: "6bC",
881 rule: Rule::ascii_hex_digits,
882 tokens: [
883 ascii_hex_digits(0, 3)
884 ]
885 };
886 }
887
888 #[test]
ascii_alpha_lowers()889 fn ascii_alpha_lowers() {
890 parses_to! {
891 parser: GrammarParser,
892 input: "a",
893 rule: Rule::ascii_alpha_lowers,
894 tokens: [
895 ascii_alpha_lowers(0, 1)
896 ]
897 };
898 }
899
900 #[test]
ascii_alpha_uppers()901 fn ascii_alpha_uppers() {
902 parses_to! {
903 parser: GrammarParser,
904 input: "K",
905 rule: Rule::ascii_alpha_uppers,
906 tokens: [
907 ascii_alpha_uppers(0, 1)
908 ]
909 };
910 }
911
912 #[test]
ascii_alphas()913 fn ascii_alphas() {
914 parses_to! {
915 parser: GrammarParser,
916 input: "wF",
917 rule: Rule::ascii_alphas,
918 tokens: [
919 ascii_alphas(0, 2)
920 ]
921 };
922 }
923
924 #[test]
ascii_alphanumerics()925 fn ascii_alphanumerics() {
926 parses_to! {
927 parser: GrammarParser,
928 input: "4jU",
929 rule: Rule::ascii_alphanumerics,
930 tokens: [
931 ascii_alphanumerics(0, 3)
932 ]
933 };
934 }
935
936 #[test]
asciis()937 fn asciis() {
938 parses_to! {
939 parser: GrammarParser,
940 input: "x02",
941 rule: Rule::asciis,
942 tokens: [
943 asciis(0, 3)
944 ]
945 };
946 }
947
948 #[test]
newline()949 fn newline() {
950 parses_to! {
951 parser: GrammarParser,
952 input: "\n\r\n\r",
953 rule: Rule::newline,
954 tokens: [
955 newline(0, 4)
956 ]
957 };
958 }
959
960 #[test]
unicode()961 fn unicode() {
962 parses_to! {
963 parser: GrammarParser,
964 input: "نامهای",
965 rule: Rule::unicode,
966 tokens: [
967 unicode(0, 12)
968 ]
969 }
970 }
971
972 #[test]
shadowing()973 fn shadowing() {
974 parses_to! {
975 parser: GrammarParser,
976 input: "shadows builtin",
977 rule: Rule::SYMBOL,
978 tokens: [
979 SYMBOL(0, 15)
980 ]
981 }
982 }
983