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