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]
node_tag()266 fn node_tag() {
267 parses_to! {
268 parser: GrammarParser,
269 input: "abc",
270 rule: Rule::node_tag,
271 tokens: [
272 node_tag(0, 3, [
273 string(0, 3)
274 ])
275 ]
276 };
277 }
278
279 #[test]
optional_string()280 fn optional_string() {
281 parses_to! {
282 parser: GrammarParser,
283 input: "abc",
284 rule: Rule::optional,
285 tokens: [
286 optional(0, 3, [
287 string(0, 3)
288 ])
289 ]
290 };
291 }
292
293 #[test]
optional_empty()294 fn optional_empty() {
295 parses_to! {
296 parser: GrammarParser,
297 input: "",
298 rule: Rule::optional,
299 tokens: [
300 optional(0, 0)
301 ]
302 };
303 }
304
305 #[test]
repeat_empty()306 fn repeat_empty() {
307 parses_to! {
308 parser: GrammarParser,
309 input: "",
310 rule: Rule::repeat,
311 tokens: [
312 repeat(0, 0)
313 ]
314 };
315 }
316
317 #[test]
repeat_strings()318 fn repeat_strings() {
319 parses_to! {
320 parser: GrammarParser,
321 input: "abc abc",
322 rule: Rule::repeat,
323 tokens: [
324 repeat(0, 9, [
325 string(0, 3),
326 string(6, 9)
327 ])
328 ]
329 };
330 }
331
332 #[test]
repeat_atomic_empty()333 fn repeat_atomic_empty() {
334 parses_to! {
335 parser: GrammarParser,
336 input: "",
337 rule: Rule::repeat_atomic,
338 tokens: [
339 repeat_atomic(0, 0)
340 ]
341 };
342 }
343
344 #[test]
repeat_atomic_strings()345 fn repeat_atomic_strings() {
346 parses_to! {
347 parser: GrammarParser,
348 input: "abcabc",
349 rule: Rule::repeat_atomic,
350 tokens: [
351 repeat_atomic(0, 6)
352 ]
353 };
354 }
355
356 #[test]
357 #[should_panic]
repeat_atomic_space()358 fn repeat_atomic_space() {
359 parses_to! {
360 parser: GrammarParser,
361 input: "abc abc",
362 rule: Rule::repeat_atomic,
363 tokens: []
364 };
365 }
366
367 #[test]
368 #[should_panic]
repeat_once_empty()369 fn repeat_once_empty() {
370 parses_to! {
371 parser: GrammarParser,
372 input: "",
373 rule: Rule::repeat_once,
374 tokens: []
375 };
376 }
377
378 #[test]
repeat_once_strings()379 fn repeat_once_strings() {
380 parses_to! {
381 parser: GrammarParser,
382 input: "abc abc",
383 rule: Rule::repeat_once,
384 tokens: [
385 repeat_once(0, 9, [
386 string(0, 3),
387 string(6, 9)
388 ])
389 ]
390 };
391 }
392
393 #[test]
394 #[should_panic]
repeat_once_atomic_empty()395 fn repeat_once_atomic_empty() {
396 parses_to! {
397 parser: GrammarParser,
398 input: "",
399 rule: Rule::repeat_once_atomic,
400 tokens: []
401 };
402 }
403
404 #[test]
repeat_once_atomic_strings()405 fn repeat_once_atomic_strings() {
406 parses_to! {
407 parser: GrammarParser,
408 input: "abcabc",
409 rule: Rule::repeat_once_atomic,
410 tokens: [
411 repeat_once_atomic(0, 6)
412 ]
413 };
414 }
415
416 #[test]
417 #[should_panic]
repeat_once_atomic_space()418 fn repeat_once_atomic_space() {
419 parses_to! {
420 parser: GrammarParser,
421 input: "abc abc",
422 rule: Rule::repeat_once_atomic,
423 tokens: []
424 };
425 }
426
427 #[test]
repeat_min_max_twice()428 fn repeat_min_max_twice() {
429 parses_to! {
430 parser: GrammarParser,
431 input: "abc abc",
432 rule: Rule::repeat_min_max,
433 tokens: [
434 repeat_min_max(0, 7, [
435 string(0, 3),
436 string(4, 7)
437 ])
438 ]
439 };
440 }
441
442 #[test]
repeat_min_max_thrice()443 fn repeat_min_max_thrice() {
444 parses_to! {
445 parser: GrammarParser,
446 input: "abc abc abc",
447 rule: Rule::repeat_min_max,
448 tokens: [
449 repeat_min_max(0, 11, [
450 string(0, 3),
451 string(4, 7),
452 string(8, 11)
453 ])
454 ]
455 };
456 }
457
458 #[test]
repeat_min_max_atomic_twice()459 fn repeat_min_max_atomic_twice() {
460 parses_to! {
461 parser: GrammarParser,
462 input: "abcabc",
463 rule: Rule::repeat_min_max_atomic,
464 tokens: [
465 repeat_min_max_atomic(0, 6)
466 ]
467 };
468 }
469
470 #[test]
repeat_min_max_atomic_thrice()471 fn repeat_min_max_atomic_thrice() {
472 parses_to! {
473 parser: GrammarParser,
474 input: "abcabcabc",
475 rule: Rule::repeat_min_max_atomic,
476 tokens: [
477 repeat_min_max_atomic(0, 9)
478 ]
479 };
480 }
481
482 #[test]
483 #[should_panic]
repeat_min_max_atomic_space()484 fn repeat_min_max_atomic_space() {
485 parses_to! {
486 parser: GrammarParser,
487 input: "abc abc",
488 rule: Rule::repeat_min_max_atomic,
489 tokens: []
490 };
491 }
492
493 #[test]
repeat_exact()494 fn repeat_exact() {
495 parses_to! {
496 parser: GrammarParser,
497 input: "abc abc",
498 rule: Rule::repeat_exact,
499 tokens: [
500 repeat_exact(0, 7, [
501 string(0, 3),
502 string(4, 7)
503 ])
504 ]
505 };
506 }
507
508 #[test]
509 #[should_panic]
repeat_min_once()510 fn repeat_min_once() {
511 parses_to! {
512 parser: GrammarParser,
513 input: "abc",
514 rule: Rule::repeat_min,
515 tokens: []
516 };
517 }
518
519 #[test]
repeat_min_twice()520 fn repeat_min_twice() {
521 parses_to! {
522 parser: GrammarParser,
523 input: "abc abc",
524 rule: Rule::repeat_min,
525 tokens: [
526 repeat_min(0, 7, [
527 string(0, 3),
528 string(4, 7)
529 ])
530 ]
531 };
532 }
533
534 #[test]
repeat_min_thrice()535 fn repeat_min_thrice() {
536 parses_to! {
537 parser: GrammarParser,
538 input: "abc abc abc",
539 rule: Rule::repeat_min,
540 tokens: [
541 repeat_min(0, 12, [
542 string(0, 3),
543 string(4, 7),
544 string(9, 12)
545 ])
546 ]
547 };
548 }
549
550 #[test]
551 #[should_panic]
repeat_min_atomic_once()552 fn repeat_min_atomic_once() {
553 parses_to! {
554 parser: GrammarParser,
555 input: "abc",
556 rule: Rule::repeat_min_atomic,
557 tokens: []
558 };
559 }
560
561 #[test]
repeat_min_atomic_twice()562 fn repeat_min_atomic_twice() {
563 parses_to! {
564 parser: GrammarParser,
565 input: "abcabc",
566 rule: Rule::repeat_min_atomic,
567 tokens: [
568 repeat_min_atomic(0, 6)
569 ]
570 };
571 }
572
573 #[test]
repeat_min_atomic_thrice()574 fn repeat_min_atomic_thrice() {
575 parses_to! {
576 parser: GrammarParser,
577 input: "abcabcabc",
578 rule: Rule::repeat_min_atomic,
579 tokens: [
580 repeat_min_atomic(0, 9)
581 ]
582 };
583 }
584
585 #[test]
586 #[should_panic]
repeat_min_atomic_space()587 fn repeat_min_atomic_space() {
588 parses_to! {
589 parser: GrammarParser,
590 input: "abc abc",
591 rule: Rule::repeat_min_atomic,
592 tokens: []
593 };
594 }
595
596 #[test]
repeat_max_once()597 fn repeat_max_once() {
598 parses_to! {
599 parser: GrammarParser,
600 input: "abc",
601 rule: Rule::repeat_max,
602 tokens: [
603 repeat_max(0, 3, [
604 string(0, 3)
605 ])
606 ]
607 };
608 }
609
610 #[test]
repeat_max_twice()611 fn repeat_max_twice() {
612 parses_to! {
613 parser: GrammarParser,
614 input: "abc abc",
615 rule: Rule::repeat_max,
616 tokens: [
617 repeat_max(0, 7, [
618 string(0, 3),
619 string(4, 7)
620 ])
621 ]
622 };
623 }
624
625 #[test]
626 #[should_panic]
repeat_max_thrice()627 fn repeat_max_thrice() {
628 parses_to! {
629 parser: GrammarParser,
630 input: "abc abc",
631 rule: Rule::repeat_max,
632 tokens: []
633 };
634 }
635
636 #[test]
repeat_max_atomic_once()637 fn repeat_max_atomic_once() {
638 parses_to! {
639 parser: GrammarParser,
640 input: "abc",
641 rule: Rule::repeat_max_atomic,
642 tokens: [
643 repeat_max_atomic(0, 3)
644 ]
645 };
646 }
647
648 #[test]
repeat_max_atomic_twice()649 fn repeat_max_atomic_twice() {
650 parses_to! {
651 parser: GrammarParser,
652 input: "abcabc",
653 rule: Rule::repeat_max_atomic,
654 tokens: [
655 repeat_max_atomic(0, 6)
656 ]
657 };
658 }
659
660 #[test]
661 #[should_panic]
repeat_max_atomic_thrice()662 fn repeat_max_atomic_thrice() {
663 parses_to! {
664 parser: GrammarParser,
665 input: "abcabcabc",
666 rule: Rule::repeat_max_atomic,
667 tokens: []
668 };
669 }
670
671 #[test]
672 #[should_panic]
repeat_max_atomic_space()673 fn repeat_max_atomic_space() {
674 parses_to! {
675 parser: GrammarParser,
676 input: "abc abc",
677 rule: Rule::repeat_max_atomic,
678 tokens: []
679 };
680 }
681
682 #[test]
repeat_comment()683 fn repeat_comment() {
684 parses_to! {
685 parser: GrammarParser,
686 input: "abc$$$ $$$abc",
687 rule: Rule::repeat_once,
688 tokens: [
689 repeat_once(0, 13, [
690 string(0, 3),
691 string(10, 13)
692 ])
693 ]
694 };
695 }
696
697 #[test]
soi_at_start()698 fn soi_at_start() {
699 parses_to! {
700 parser: GrammarParser,
701 input: "abc",
702 rule: Rule::soi_at_start,
703 tokens: [
704 soi_at_start(0, 3, [
705 string(0, 3)
706 ])
707 ]
708 };
709 }
710
711 #[test]
peek()712 fn peek() {
713 parses_to! {
714 parser: GrammarParser,
715 input: "0111",
716 rule: Rule::peek_,
717 tokens: [
718 peek_(0, 4, [
719 range(0, 1),
720 range(1, 2)
721 ])
722 ]
723 };
724 }
725
726 #[test]
peek_all()727 fn peek_all() {
728 parses_to! {
729 parser: GrammarParser,
730 input: "0110",
731 rule: Rule::peek_all,
732 tokens: [
733 peek_all(0, 4, [
734 range(0, 1),
735 range(1, 2)
736 ])
737 ]
738 };
739 }
740
741 #[test]
peek_slice_23()742 fn peek_slice_23() {
743 parses_to! {
744 parser: GrammarParser,
745 input: "0123412",
746 rule: Rule::peek_slice_23,
747 tokens: [
748 peek_slice_23(0, 7, [
749 range(0, 1),
750 range(1, 2),
751 range(2, 3),
752 range(3, 4),
753 range(4, 5),
754 ])
755 ]
756 };
757 }
758
759 #[test]
pop()760 fn pop() {
761 parses_to! {
762 parser: GrammarParser,
763 input: "0110",
764 rule: Rule::pop_,
765 tokens: [
766 pop_(0, 4, [
767 range(0, 1),
768 range(1, 2)
769 ])
770 ]
771 };
772 }
773
774 #[test]
pop_all()775 fn pop_all() {
776 parses_to! {
777 parser: GrammarParser,
778 input: "0110",
779 rule: Rule::pop_all,
780 tokens: [
781 pop_all(0, 4, [
782 range(0, 1),
783 range(1, 2)
784 ])
785 ]
786 };
787 }
788
789 #[test]
pop_fail()790 fn pop_fail() {
791 parses_to! {
792 parser: GrammarParser,
793 input: "010",
794 rule: Rule::pop_fail,
795 tokens: [
796 pop_fail(0, 3, [
797 range(0, 1),
798 range(1, 2)
799 ])
800 ]
801 };
802 }
803
804 #[test]
repeat_mutate_stack()805 fn repeat_mutate_stack() {
806 parses_to! {
807 parser: GrammarParser,
808 input: "a,b,c,cba",
809 rule: Rule::repeat_mutate_stack,
810 tokens: [
811 repeat_mutate_stack(0, 9)
812 ]
813 };
814 }
815
816 #[test]
stack_resume_after_fail()817 fn stack_resume_after_fail() {
818 parses_to! {
819 parser: GrammarParser,
820 input: "a,b,c,cba",
821 rule: Rule::stack_resume_after_fail,
822 tokens: [
823 stack_resume_after_fail(0, 9, [
824 repeat_mutate_stack_pop_all(0, 9)
825 ])
826 ]
827 };
828 }
829
830 #[test]
checkpoint_restore()831 fn checkpoint_restore() {
832 parses_to! {
833 parser: GrammarParser,
834 input: "a",
835 rule: Rule::checkpoint_restore,
836 tokens: [
837 checkpoint_restore(0, 1, [EOI(1, 1)])
838 ]
839 };
840 }
841
842 #[test]
ascii_digits()843 fn ascii_digits() {
844 parses_to! {
845 parser: GrammarParser,
846 input: "6",
847 rule: Rule::ascii_digits,
848 tokens: [
849 ascii_digits(0, 1)
850 ]
851 };
852 }
853
854 #[test]
ascii_nonzero_digits()855 fn ascii_nonzero_digits() {
856 parses_to! {
857 parser: GrammarParser,
858 input: "5",
859 rule: Rule::ascii_nonzero_digits,
860 tokens: [
861 ascii_nonzero_digits(0, 1)
862 ]
863 };
864 }
865
866 #[test]
ascii_bin_digits()867 fn ascii_bin_digits() {
868 parses_to! {
869 parser: GrammarParser,
870 input: "1",
871 rule: Rule::ascii_bin_digits,
872 tokens: [
873 ascii_bin_digits(0, 1)
874 ]
875 };
876 }
877
878 #[test]
ascii_oct_digits()879 fn ascii_oct_digits() {
880 parses_to! {
881 parser: GrammarParser,
882 input: "3",
883 rule: Rule::ascii_oct_digits,
884 tokens: [
885 ascii_oct_digits(0, 1)
886 ]
887 };
888 }
889
890 #[test]
ascii_hex_digits()891 fn ascii_hex_digits() {
892 parses_to! {
893 parser: GrammarParser,
894 input: "6bC",
895 rule: Rule::ascii_hex_digits,
896 tokens: [
897 ascii_hex_digits(0, 3)
898 ]
899 };
900 }
901
902 #[test]
ascii_alpha_lowers()903 fn ascii_alpha_lowers() {
904 parses_to! {
905 parser: GrammarParser,
906 input: "a",
907 rule: Rule::ascii_alpha_lowers,
908 tokens: [
909 ascii_alpha_lowers(0, 1)
910 ]
911 };
912 }
913
914 #[test]
ascii_alpha_uppers()915 fn ascii_alpha_uppers() {
916 parses_to! {
917 parser: GrammarParser,
918 input: "K",
919 rule: Rule::ascii_alpha_uppers,
920 tokens: [
921 ascii_alpha_uppers(0, 1)
922 ]
923 };
924 }
925
926 #[test]
ascii_alphas()927 fn ascii_alphas() {
928 parses_to! {
929 parser: GrammarParser,
930 input: "wF",
931 rule: Rule::ascii_alphas,
932 tokens: [
933 ascii_alphas(0, 2)
934 ]
935 };
936 }
937
938 #[test]
ascii_alphanumerics()939 fn ascii_alphanumerics() {
940 parses_to! {
941 parser: GrammarParser,
942 input: "4jU",
943 rule: Rule::ascii_alphanumerics,
944 tokens: [
945 ascii_alphanumerics(0, 3)
946 ]
947 };
948 }
949
950 #[test]
asciis()951 fn asciis() {
952 parses_to! {
953 parser: GrammarParser,
954 input: "x02",
955 rule: Rule::asciis,
956 tokens: [
957 asciis(0, 3)
958 ]
959 };
960 }
961
962 #[test]
newline()963 fn newline() {
964 parses_to! {
965 parser: GrammarParser,
966 input: "\n\r\n\r",
967 rule: Rule::newline,
968 tokens: [
969 newline(0, 4)
970 ]
971 };
972 }
973
974 #[test]
unicode()975 fn unicode() {
976 parses_to! {
977 parser: GrammarParser,
978 input: "نامهای",
979 rule: Rule::unicode,
980 tokens: [
981 unicode(0, 12)
982 ]
983 }
984 }
985
986 #[test]
shadowing()987 fn shadowing() {
988 parses_to! {
989 parser: GrammarParser,
990 input: "shadows builtin",
991 rule: Rule::SYMBOL,
992 tokens: [
993 SYMBOL(0, 15)
994 ]
995 }
996 }
997