shrimple-parser 0.1.0

Zero-dependency next-gen parsing combinator library with flexible error reporting
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
//! Abstractions for working with patterns.

mod char_predicates;
mod into_pattern;

pub use {
    char_predicates::*,
    into_pattern::*,
};

use {
    crate::{tuple::Tuple, Input},
    core::ops::Not,
};

#[cfg(test)]
use {
    crate::{
        parser::{one, until_ex, Parser},
        error::ParsingError,
    },
    core::convert::Infallible,
};

/// This trait represents an object that can be matched onto a string.
/// This includes functions, characters, [arrays of] characters, strings, but also custom patterns
/// like [`NotEscaped`]
///
/// See built-in patterns and parser adapters for patterns in the [`pattern`](self) module
///
/// Hint: on the success path, the 1st element of the return tuple is the rest of the input (with
/// or without the matched pattern at the start)
pub trait Pattern {
    /// The return values are (rest of the input, matched fragment at the beginning).
    ///
    /// # Errors
    /// In the case of no match, the original `input` is returned as the [`Err`] variant.
    ///
    /// Used by [`crate::parser::one`].
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I>;

    /// The return values are (rest of the input, contiguous matched fragments from the beginning).
    ///
    /// 0 is also a valid number of matches.
    ///
    /// Used by [`crate::parser::many`]
    #[expect(
        clippy::unwrap_used,
        reason = "this will only panic if the pattern does"
    )]
    fn immediate_matches<I: Input>(&self, input: I) -> (I, I) {
        let mut rest = Some(input.clone());
        let rest_ptr = loop {
            match self.immediate_match(rest.take().unwrap()) {
                Ok((x, _)) => rest = Some(x),
                Err(x) => break x.as_ptr(),
            }
        };
        let input_ptr = input.as_ptr();
        input.split_at(rest_ptr as usize - input_ptr as usize).rev()
    }

    /// Like [`Pattern::immediate_matches`], but also counts the number of matches.
    ///
    /// Used by the [`Pattern`] impl of [`NotEscaped`]
    #[expect(
        clippy::unwrap_used,
        reason = "this will only panic if the pattern does"
    )]
    fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize)) {
        let mut rest = Some(input.clone());
        let mut n = 0;
        let rest_ptr = loop {
            match self.immediate_match(rest.take().unwrap()) {
                Ok((x, _)) => {
                    rest = Some(x);
                    n += 1;
                }
                Err(x) => break x.as_ptr(),
            }
        };
        let input_ptr = input.as_ptr();
        input
            .split_at(rest_ptr as usize - input_ptr as usize)
            .rev()
            .map_second(|s| (s, n))
    }

    /// Like [`Pattern::immediate_match`], but matches at the end of `input`.
    /// The return values are (the input before the match, the match)
    ///
    /// # Errors
    /// In the case of no match, the original `input` is returned as the [`Err`] variant.
    ///
    /// Used by the [`Pattern`] impl of [`NotEscaped`]
    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I>;

    /// Like [`Pattern::immediate_matches_counted`], but matches at the end of `input`,
    /// and doesn't return the matched fragment of the input.
    ///
    /// Used by the [`Pattern`] impl of [`NotEscaped`]
    #[expect(
        clippy::unwrap_used,
        reason = "this will only panic if the pattern does"
    )]
    fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize) {
        let mut rest = Some(input);
        let mut n = 0;
        loop {
            match self.trailing_match(rest.take().unwrap()) {
                Ok((before, _)) => {
                    rest = Some(before);
                    n += 1;
                }
                Err(rest) => break (rest, n),
            }
        }
    }

    /// The return values are (the match + rest of the input, (string before the match, the match)).
    ///
    /// # Errors
    /// Returns the provided `input` unchanged in the [`Err`] variant if there's no match.
    ///
    /// Used by [`crate::parser::until`].
    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>;

    /// Like [`Pattern::first_match`], but the match is excluded from the rest of the input.
    ///
    /// # Errors
    /// Returns the provided `input` unchanged in the [`Err`] variant if there's no match.
    ///
    /// Used by [`crate::parser::until_ex`].
    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>;

    /// Get the pattern by reference to avoid moving it, which will happen in generic code
    ///
    /// Do not override this method.
    fn by_ref(&self) -> Ref<'_, Self> {
        Ref(self)
    }

    /// Combine `self` and another pattern into a pattern that matches both of them in a sequence,
    /// with `self` before `other`
    ///
    /// Do not override this method.
    fn and<Other: Pattern>(self, other: Other) -> Chain<Self, Other>
    where
        Self: Sized,
    {
        Chain(self, other)
    }

    /// Create a pattern that'll match `self` only if it's not escaped (immediately preceded)
    /// by the provided pattern.
    ///
    /// Do not override this method.
    fn not_escaped_by<Prefix: Pattern>(self, prefix: Prefix) -> NotEscaped<Prefix, Self>
    where
        Self: Sized,
    {
        NotEscaped(prefix, self)
    }

    /// Create a pattern that'll match `self` only if it's not enclosed (preceded & superceded) by
    /// the provided pattern.
    ///
    /// Do not override this method.
    fn not_enclosed_by<Enclosure: Pattern>(self, enc: Enclosure) -> NotEnclosed<Enclosure, Self>
    where
        Self: Sized,
    {
        NotEnclosed(enc, self)
    }

    /// Creates a greedy pattern that matches `self` as many times as possible. See [`Many`]
    fn many(self) -> Many<Self> 
    where
        Self: Sized,
    {
        Many(self)
    }

    /// Creates a pattern that matches `self` 0 or 1 times. See [`Maybe`]
    fn maybe(self) -> Maybe<Self>
    where
        Self: Sized,
    {
        Maybe(self)
    }
}

impl<F: Fn(char) -> bool> Pattern for F {
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        match input.chars().next().filter(|c| self(*c)) {
            Some(c) => Ok(input.split_at(c.len_utf8()).rev()),
            None => Err(input),
        }
    }

    fn immediate_matches<I: Input>(&self, input: I) -> (I, I) {
        let mid = input.find(|c| !self(c)).unwrap_or(input.len());
        input.split_at(mid).rev()
    }

    fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize)) {
        let mut char_index = 0;
        let byte_index = input
            .char_indices()
            .inspect(|_| char_index += 1)
            .find_map(|(bi, c)| self(c).not().then_some(bi))
            .inspect(|_| char_index -= 1)
            .unwrap_or(input.len());
        input
            .split_at(byte_index)
            .rev()
            .map_second(|s| (s, char_index))
    }

    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        match input.strip_suffix(self).map(str::len) {
            Some(len) => Ok(input.split_at(len)),
            None => Err(input),
        }
    }

    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        match input.char_indices().find(|(_, c)| self(*c)) {
            Some((at, ch)) => {
                let (before, after) = input.split_at(at);
                let r#match = after.clone().before(ch.len_utf8());
                Ok((after, (before, r#match)))
            }
            None => Err(input),
        }
    }

    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        match input.char_indices().find(|(_, c)| self(*c)) {
            Some((at, ch)) => {
                let (before, after) = input.split_at(at);
                let (r#match, after) = after.split_at(ch.len_utf8());
                Ok((after, (before, r#match)))
            }
            None => Err(input),
        }
    }
}

/// This is a specialised, optimised impl for matching any `char` in the array. For a more general
/// pattern combinator, use a tuple.
impl<const N: usize> Pattern for [char; N] {
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        match input.strip_prefix(self) {
            Some(rest) => {
                let matched_pat_len = input.len() - rest.len();
                Ok(input.split_at(matched_pat_len).rev())
            }
            None => Err(input),
        }
    }

    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        match input.strip_suffix(self) {
            Some(rest) => {
                let rest_len = rest.len();
                Ok(input.split_at(rest_len))
            }
            None => Err(input),
        }
    }

    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        match input.find(self) {
            Some(at) => {
                let (prev, match_and_rest) = input.split_at(at);
                let matched_pat_len = match_and_rest.chars().next().map_or(0, char::len_utf8);
                let r#match = match_and_rest.clone().before(matched_pat_len);
                Ok((match_and_rest, (prev, r#match)))
            }
            None => Err(input),
        }
    }

    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        match input.find(self) {
            Some(at) => {
                let (prev, match_and_rest) = input.split_at(at);
                let matched_pat_len = match_and_rest.chars().next().map_or(0, char::len_utf8);
                let (r#match, rest) = match_and_rest.split_at(matched_pat_len);
                Ok((rest, (prev, r#match)))
            }
            None => Err(input),
        }
    }
}

impl Pattern for &str {
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        if input.starts_with(*self) {
            Ok(input.split_at(self.len()).rev())
        } else {
            Err(input)
        }
    }

    fn immediate_matches<I: Input>(&self, input: I) -> (I, I) {
        let rest_len = input.trim_start_matches(self).len();
        let input_len = input.len();
        input.split_at(input_len - rest_len).rev()
    }

    fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize)) {
        self.immediate_matches(input)
            .map_second(|s| (s.len().checked_div(self.len()).unwrap_or(0), s).rev())
    }

    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        if input.ends_with(self) {
            let mid = input.len() - self.len();
            Ok(input.split_at(mid))
        } else {
            Err(input)
        }
    }

    fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize) {
        let trimmed_len = input.trim_end_matches(self).len();
        let input_len = input.len();
        (
            input.before(trimmed_len),
            (input_len - trimmed_len) / self.len(),
        )
    }

    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        match input.find(*self) {
            Some(at) => {
                let (before, after) = input.split_at(at);
                let r#match = after.clone().before(self.len());
                Ok((after, (before, r#match)))
            }
            None => Err(input),
        }
    }

    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        match input.find(*self) {
            Some(at) => {
                let (before, after) = input.split_at(at);
                let (r#match, after) = after.split_at(self.len());
                Ok((after, (before, r#match)))
            }
            None => Err(input),
        }
    }
}

impl Pattern for char {
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        if input.starts_with(*self) {
            Ok(input.split_at(self.len_utf8()).rev())
        } else {
            Err(input)
        }
    }

    fn immediate_matches<I: Input>(&self, input: I) -> (I, I) {
        let rest_len = input.trim_start_matches(*self).len();
        let input_len = input.len();
        input.split_at(input_len - rest_len).rev()
    }

    fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize)) {
        self.immediate_matches(input)
            .map_second(|s| (s.len() / self.len_utf8(), s).rev())
    }

    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        if input.ends_with(*self) {
            let mid = input.len() - self.len_utf8();
            Ok(input.split_at(mid))
        } else {
            Err(input)
        }
    }

    fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize) {
        let trimmed_len = input.trim_end_matches(*self).len();
        let input_len = input.len();
        (
            input.before(trimmed_len),
            (input_len - trimmed_len) / self.len_utf8(),
        )
    }

    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        match input.find(*self) {
            Some(at) => {
                let (before, after) = input.split_at(at);
                let r#match = after.clone().before(self.len_utf8());
                Ok((after, (before, r#match)))
            }
            None => Err(input),
        }
    }

    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        match input.find(*self) {
            Some(at) => {
                let (before, after) = input.split_at(at);
                let (r#match, after) = after.split_at(self.len_utf8());
                Ok((after, (before, r#match)))
            }
            None => Err(input),
        }
    }
}

#[cfg(feature = "either")]
macro_rules! fwd_method_impl {
    ($(fn $name:ident -> $ret:ty;)+) => {
        $(
            fn $name<I: Input>(&self, input: I) -> $ret {
                match self {
                    either::Either::Left(l) => l.$name(input),
                    either::Either::Right(r) => r.$name(input),
                }
            }
        )+
    };
}

#[cfg(feature = "either")]
impl<L: Pattern, R: Pattern> Pattern for either::Either<L, R> {
    fwd_method_impl! {
        fn immediate_match -> Result<(I, I), I>;
        fn immediate_matches -> (I, I);
        fn immediate_matches_counted -> (I, (I, usize));
        fn trailing_match -> Result<(I, I), I>;
        fn trailing_matches_counted -> (I, usize);
        fn first_match -> Result<(I, (I, I)), I>;
        fn first_match_ex -> Result<(I, (I, I)), I>;
    }
}

/// Pattern that's the reference to another pattern, used in generic code to reuse the pattern.
#[repr(transparent)]
pub struct Ref<'this, T: ?Sized + Pattern>(&'this T);

impl<T: ?Sized + Pattern> Clone for Ref<'_, T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T: ?Sized + Pattern> Copy for Ref<'_, T> {}

impl<T: ?Sized + Pattern> Pattern for Ref<'_, T> {
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        T::immediate_match(self.0, input)
    }

    fn immediate_matches<I: Input>(&self, input: I) -> (I, I) {
        T::immediate_matches(self.0, input)
    }

    fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize)) {
        T::immediate_matches_counted(self.0, input)
    }

    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        T::trailing_match(self.0, input)
    }

    fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize) {
        T::trailing_matches_counted(self.0, input)
    }

    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        T::first_match(self.0, input)
    }

    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        T::first_match_ex(self.0, input)
    }
}

/// Pattern that matches pattern `Inner` not escaped by `Prefix`.
/// "escaped" here means that the pattern `Inner` is preceded by a `Prefix` that's not preceded by
/// itself.
///
/// For example, for a pattern `NotEscaped('\', '0')`, the strings "0", "\\0" & "\\\\\\0" will have
/// a match, but the strings "\0", "\\ \0" & "\\\\\\\0" won't.
#[derive(Clone, Copy)]
pub struct NotEscaped<Prefix: Pattern, Inner: Pattern>(pub Prefix, pub Inner);

impl<Prefix: Pattern, Inner: Pattern> Pattern for NotEscaped<Prefix, Inner> {
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        self.1.immediate_match(input)
    }

    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        let (rest, r#match) = self.1.trailing_match(input.clone())?;
        let (rest, n_prefixes) = self.0.trailing_matches_counted(rest);
        (n_prefixes % 2 == 0)
            .then_some((rest, r#match))
            .ok_or(input)
    }

    fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize) {
        let (rest, n) = self.1.trailing_matches_counted(input);
        if n == 0 {
            return (rest, 0);
        }
        let no_1st_prefix = match self.0.trailing_match(rest.clone()) {
            Ok((x, _)) => x,
            Err(rest) => return (rest, n),
        };
        let (_, n_prefixes_minus_one) = self.0.trailing_matches_counted(no_1st_prefix.clone());
        if n_prefixes_minus_one % 2 != 0 {
            (rest, n)
        } else {
            (no_1st_prefix, n - 1)
        }
    }

    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let mut rest = input.clone();
        while !rest.is_empty() {
            let (before, r#match);
            (rest, (before, r#match)) = self.1.first_match(rest)?;
            let before = match self.0.trailing_match(before) {
                Ok((x, _)) => x,
                Err(before) => return Ok((rest, (before, r#match))),
            };
            let (before, n_prefixes_minus_one) = self.0.trailing_matches_counted(before);
            if n_prefixes_minus_one % 2 != 0 {
                return Ok((rest, (before, r#match)));
            }
        }
        Err(input)
    }

    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let mut rest = input.clone();
        loop {
            let (before, r#match);
            (rest, (before, r#match)) = self.1.first_match_ex(rest)?;
            let Ok((before, _)) = self.0.trailing_match(before) else {
                let index = r#match.as_ptr() as usize - input.as_ptr() as usize;
                let before = input.before(index);
                return Ok((rest, (before, r#match)));
            };
            let (_, n_prefixes_minus_one) = self.0.trailing_matches_counted(before);
            if n_prefixes_minus_one % 2 != 0 {
                let index = r#match.as_ptr() as usize - input.as_ptr() as usize;
                let before = input.before(index);
                return Ok((rest, (before, r#match)));
            }
        }
    }
}

/// Pattern that matches pattern `Inner` not surrounded by `Enclosure`.
pub struct NotEnclosed<Enclosure: Pattern, Inner: Pattern>(pub Enclosure, pub Inner);

impl<Enclosure: Pattern, Inner: Pattern> Pattern for NotEnclosed<Enclosure, Inner> {
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        self.1.immediate_match(input)
    }

    fn immediate_matches<I: Input>(&self, input: I) -> (I, I) {
        self.1.immediate_matches(input)
    }

    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        self.1.trailing_match(input)
    }

    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let mut enclosed = false;
        let mut rest = &*input;
        loop {
            let (after_enc, (before_enc, enc)) =
                self.0.first_match_ex(rest).unwrap_or(("", (rest, "")));
            let (after_inner, (before_inner, inner)) =
                self.1.first_match_ex(rest).unwrap_or(("", (rest, "")));

            if [enc, inner] == ["", ""] {
                break Err(input);
            }

            if before_enc.len() < before_inner.len() {
                rest = after_enc;
                enclosed = !enclosed;
            } else if enclosed {
                rest = after_inner;
            } else {
                let match_len = inner.len();
                let before_len = input.len() - after_inner.len() - match_len;
                let (before, rest_and_match) = input.split_at(before_len);
                let r#match = rest_and_match.clone().before(match_len);
                break Ok((rest_and_match, (before, r#match)));
            }
        }
    }

    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let mut enclosed = false;
        let mut rest = &*input;
        loop {
            let (after_enc, (before_enc, enc)) =
                self.0.first_match_ex(rest).unwrap_or(("", (rest, "")));
            let (after_inner, (before_inner, inner)) =
                self.1.first_match_ex(rest).unwrap_or(("", (rest, "")));

            if [enc, inner] == ["", ""] {
                break Err(input);
            }

            if before_enc.len() < before_inner.len() {
                rest = after_enc;
                enclosed = !enclosed;
            } else if enclosed {
                rest = after_inner;
            } else {
                let match_len = inner.len();
                let before_len = input.len() - after_inner.len() - match_len;
                let (before, rest_and_match) = input.split_at(before_len);
                let (r#match, rest) = rest_and_match.split_at(match_len);
                break Ok((rest, (before, r#match)));
            }
        }
    }
}

/// A pattern that matches any 1 character.
#[derive(Clone, Copy)]
pub struct Any;

impl Pattern for Any {
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        match input.chars().next() {
            Some(ch) => Ok(input.split_at(ch.len_utf8()).rev()),
            None => Err(input),
        }
    }

    fn immediate_matches<I: Input>(&self, input: I) -> (I, I) {
        let input_len = input.len();
        input.split_at(input_len).rev()
    }

    fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize)) {
        let input_len = input.len();
        let input_n_chars = input.chars().count();
        input.split_at(input_len).rev().map_second(|matched| (matched, input_n_chars))
    }

    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        match input.chars().next_back() {
            Some(ch) => Ok(input.split_at(ch.len_utf8())),
            None => Err(input),
        }
    }

    fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize) {
        let input_n_chars = input.chars().count();
        (input.start(), input_n_chars)
    }

    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let first_char_len = input.chars().next().map_or(0, char::len_utf8);
        let before = input.clone().start();
        let first_char_span = input.clone().before(first_char_len);
        Ok((input, (before, first_char_span)))
    }

    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let first_char_len = input.chars().next().map_or(0, char::len_utf8);
        let before = input.clone().start();
        let (first_char_span, after) = input.split_at(first_char_len);
        Ok((after, (before, first_char_span)))
    }
}

/// A tuple pattern matches either of the 2 patterns in a short-circuiting & commutative manner,
/// with `self` tried first. Tuples of more than 2 elements can be turned into a pattern via
/// the [`IntoPattern`] trait.
///
/// # Performance
/// If you want to match either of N chars, use an array of them as a pattern instead, as this
/// struct has a general impl that may miss optimisations applicable to the case of `[char; N]`
/// being the pattern.
impl<P1: Pattern, P2: Pattern> Pattern for (P1, P2) {
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        self.0
            .immediate_match(input)
            .or_else(|input| self.1.immediate_match(input))
    }

    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        self.0
            .trailing_match(input)
            .or_else(|input| self.1.trailing_match(input))
    }

    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let (before1, match1) = self.0.first_match(&*input).map_or((&*input, ""), |x| x.1);
        let (before2, match2) = self.1.first_match(&*input).map_or((&*input, ""), |x| x.1);

        if [match1, match2] == ["", ""] {
            return Err(input);
        }

        let [before_len, match_len] = if before1.len() < before2.len() {
            [before1.len(), match1.len()]
        } else {
            [before2.len(), match2.len()]
        };

        let (before, match_rest) = input.split_at(before_len);
        let r#match = match_rest.clone().before(match_len);
        Ok((match_rest, (before, r#match)))
    }

    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let (before1, match1) = self.0.first_match(&*input).map_or((&*input, ""), |x| x.1);
        let (before2, match2) = self.1.first_match(&*input).map_or((&*input, ""), |x| x.1);

        if [match1, match2] == ["", ""] {
            return Err(input);
        }

        let [before_len, match_len] = if before1.len() < before2.len() {
            [before1.len(), match1.len()]
        } else {
            [before2.len(), match2.len()]
        };

        let (before, match_rest) = input.split_at(before_len);
        let (r#match, rest) = match_rest.split_at(match_len);
        Ok((rest, (before, r#match)))
    }
}

/// A pattern that matches `P1` immediately followed by `P2`.
///
/// A match is only produced when **both** patterns match consecutively at
/// the same position: `P1` at the current position and `P2` right after it.
/// The combined match spans the entirety of both sub-matches.
///
/// # Note
/// For `first_match` / `first_match_ex`, every occurrence of `P1` in the
/// input is tried in left-to-right order; the first one where `P2`
/// immediately follows is returned.  Occurrences of `P1` that are *not*
/// followed by `P2` are skipped.
///
/// More conveniently created via [`Pattern::and`].
///
/// # Example
/// ```rust
/// # fn main() {
/// use {
///     shrimple_parser::{
///         Pattern,
///         parser::{one, until_ex},
///         pattern::{ascii_digit, alphabetic, Chain},
///     },
///     core::convert::Infallible,
/// };
///
/// // Matches a digit immediately followed by an alphabetic character.
/// assert_eq!(
///     one::<_, Infallible>(ascii_digit.and(alphabetic))("3x rest"),
///     Ok((" rest", "3x")),
/// );
///
/// // Returns an error when the pattern is not at the start.
/// assert!(
///     one::<_, Infallible>(ascii_digit.and(alphabetic))("x3 rest")
///         .is_err()
/// );
///
/// // Finds the first '$' that is immediately followed by '{'.
/// assert_eq!(
///     until_ex::<_, Infallible>('$'.and('{'))("foo${bar}"),
///     Ok(("bar}", "foo")),
/// );
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Chain<P1: Pattern, P2: Pattern>(pub P1, pub P2);

impl<P1: Pattern, P2: Pattern> Pattern for Chain<P1, P2> {
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        // Try P1 at the start; keep the original `input` for the error path.
        let rest_after_p1 = match self.0.immediate_match(input.clone()) {
            Ok((rest, _)) => rest,
            Err(_) => return Err(input),
        };
        // Try P2 immediately after P1.
        match self.1.immediate_match(rest_after_p1) {
            Ok((rest_after_p2, _)) => {
                // The combined match spans from the start of `input` to the
                // start of `rest_after_p2`.
                let match_len = input.len() - rest_after_p2.len();
                let (matched, rest) = input.split_at(match_len);
                Ok((rest, matched))
            }
            Err(_) => Err(input),
        }
    }

    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        // First strip P2 from the end.
        let before_p2 = match self.1.trailing_match(input.clone()) {
            Ok((before, _)) => before,
            Err(_) => return Err(input),
        };
        // Then strip P1 from the end of what remains.
        match self.0.trailing_match(before_p2) {
            Ok((before_p1, _)) => {
                // `before_p1.len()` is the byte offset where the chain match
                // starts inside `input`.
                Ok(input.split_at(before_p1.len()))
            }
            Err(_) => Err(input),
        }
    }

    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let mut rest = input.clone();
        loop {
            // Find the next P1, advancing `rest` past each failed candidate.
            let (after_p1, (_, p1_match)) = match self.0.first_match_ex(rest) {
                Ok(result) => result,
                Err(_) => return Err(input),
            };
            // Check whether P2 immediately follows this P1.
            match self.1.immediate_match(after_p1.clone()) {
                Ok((after_p2, _)) => {
                    // Reconstruct `before` and the chain match relative to the
                    // original `input` using length arithmetic so that the
                    // returned slices stay within the same allocation.
                    let p1_start = input.len() - after_p1.len() - p1_match.len();
                    let chain_len = p1_match.len() + after_p1.len() - after_p2.len();
                    let (before, chain_and_rest) = input.split_at(p1_start);
                    let chain = chain_and_rest.clone().before(chain_len);
                    return Ok((chain_and_rest, (before, chain)));
                }
                // P2 did not follow this P1; advance past P1 and keep looking.
                Err(_) => rest = after_p1,
            }
        }
    }

    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let mut rest = input.clone();
        loop {
            // Find the next P1, advancing `rest` past each failed candidate.
            let (after_p1, (_, p1_match)) = match self.0.first_match_ex(rest) {
                Ok(result) => result,
                Err(_) => return Err(input),
            };
            // Check whether P2 immediately follows this P1.
            match self.1.immediate_match(after_p1.clone()) {
                Ok((after_p2, _)) => {
                    let p1_start = input.len() - after_p1.len() - p1_match.len();
                    let chain_len = p1_match.len() + after_p1.len() - after_p2.len();
                    let (before, chain_start) = input.split_at(p1_start);
                    let chain = chain_start.before(chain_len);
                    return Ok((after_p2, (before, chain)));
                }
                // P2 did not follow this P1; advance past P1 and keep looking.
                Err(_) => rest = after_p1,
            }
        }
    }
}

/// A pattern that matches any number of contiguous occurrences of the inner pattern `T`,
/// including 0.
///
/// Because 0 occurrences is always a valid match, [`Many`] never fails: [`Pattern::immediate_match`]
/// and [`Pattern::trailing_match`] always return [`Ok`], and [`Pattern::first_match`] /
/// [`Pattern::first_match_ex`] always match at the very start of the input (with a possibly
/// empty match).
///
/// More conveniently created via [`Pattern::many`]
///
/// # Example
/// ```rust
/// # fn main() {
/// use {
///     shrimple_parser::{
///         Pattern,
///         parser::one,
///         pattern::{ascii_digit, Many},
///     },
///     core::convert::Infallible,
/// };
///
/// // Matches 0 or more ASCII digits from the start of the input.
/// assert_eq!(
///     one::<_, Infallible>(ascii_digit.many())("123abc"),
///     Ok(("abc", "123")),
/// );
///
/// // 0 matches is still a match, with an empty matched fragment.
/// assert_eq!(
///     one::<_, Infallible>(ascii_digit.many())("abc"),
///     Ok(("abc", "")),
/// );
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Many<T: Pattern>(pub T);

impl<T: Pattern> Pattern for Many<T> {
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        Ok(self.0.immediate_matches(input))
    }

    /// Repeating "0 or more `T`" is the same as "0 or more `T`", so this delegates directly to
    /// `T`'s implementation rather than looping on [`Pattern::immediate_match`], which would
    /// never terminate since [`Many::immediate_match`] never fails.
    fn immediate_matches<I: Input>(&self, input: I) -> (I, I) {
        self.0.immediate_matches(input)
    }

    fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize)) {
        self.0.immediate_matches_counted(input)
    }

    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        let (rest, _) = self.0.trailing_matches_counted(input.clone());
        let rest_len = rest.len();
        Ok(input.split_at(rest_len))
    }

    /// See [`Many::immediate_matches`] for why this delegates straight to `T` instead of
    /// looping on [`Pattern::trailing_match`].
    fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize) {
        self.0.trailing_matches_counted(input)
    }

    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let (after_first, (before, first)) = self.0.first_match_ex(input.clone())?;
        let (_, more) = self.0.immediate_matches(after_first);

        let before_len = before.len();
        let match_len = first.len() + more.len();

        let (before, match_and_rest) = input.split_at(before_len);
        let matched = match_and_rest.clone().before(match_len);
        Ok((match_and_rest, (before, matched)))
    }

    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let (after_first, (before, first)) = self.0.first_match_ex(input.clone())?;
        let (_, more) = self.0.immediate_matches(after_first);

        let before_len = before.len();
        let match_len = first.len() + more.len();

        let (before, match_and_rest) = input.split_at(before_len);
        let (matched, rest) = match_and_rest.split_at(match_len);
        Ok((rest, (before, matched)))
    }
}

/// A pattern that matches 0 or 1 occurrences of the inner pattern `T`, making it optional.
///
/// Because both 0 and 1 occurrences are valid matches, [`Maybe`] never fails:
/// [`Pattern::immediate_match`] and [`Pattern::trailing_match`] always return [`Ok`], falling
/// back to an empty match at the relevant end of the input if `T` doesn't match there. Likewise,
/// [`Pattern::first_match`] / [`Pattern::first_match_ex`] always match at the very start of the
/// input.
///
/// More conveniently created via [`Pattern::maybe`]
///
/// # Example
/// ```rust
/// # fn main() {
/// use {
///     shrimple_parser::{
///         Pattern,
///         parser::one,
///         pattern::Maybe,
///     },
///     core::convert::Infallible,
/// };
///
/// // Matches an optional '-' sign at the start of the input.
/// assert_eq!(
///     one::<_, Infallible>('-'.maybe())("-123"),
///     Ok(("123", "-")),
/// );
///
/// // No '-' present - still matches, with an empty matched fragment.
/// assert_eq!(
///     one::<_, Infallible>('-'.maybe())("123"),
///     Ok(("123", "")),
/// );
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Maybe<T: Pattern>(pub T);

impl<T: Pattern> Pattern for Maybe<T> {
    fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        self.0
            .immediate_match(input)
            .or_else(|input| Ok(input.split_at(0).rev()))
    }

    /// Repeating "0 or 1 `T`" is the same as "0 or more `T`", so this delegates directly to
    /// `T`'s implementation rather than looping on [`Pattern::immediate_match`], which would
    /// never terminate since [`Maybe::immediate_match`] never fails.
    fn immediate_matches<I: Input>(&self, input: I) -> (I, I) {
        self.0.immediate_matches(input)
    }

    fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize)) {
        self.0.immediate_matches_counted(input)
    }

    fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I> {
        self.0.trailing_match(input).or_else(|input| {
            let len = input.len();
            Ok(input.split_at(len))
        })
    }

    /// See [`Maybe::immediate_matches`] for why this delegates straight to `T` instead of
    /// looping on [`Pattern::trailing_match`].
    fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize) {
        self.0.trailing_matches_counted(input)
    }

    fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let (_, matched) = self.immediate_match(input.clone())?;
        let before = input.clone().before(0);
        Ok((input, (before, matched)))
    }

    fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I> {
        let (rest, matched) = self.immediate_match(input.clone())?;
        let before = input.before(0);
        Ok((rest, (before, matched)))
    }
}

#[test]
fn char_pat() {
    assert_eq!(
        until_ex::<_, Infallible>('"')
            .parse(r#"this is what they call a \"test\", right?" - he said"#),
        Ok((
            r#"test\", right?" - he said"#,
            r"this is what they call a \"
        )),
    );
}

#[test]
fn not_escaped_pat() {
    assert_eq!(
        until_ex::<_, Infallible>(NotEscaped('\\', '"'))
            .parse(r#"this is what they call a \"test\", right?" - he said"#),
        Ok((" - he said", r#"this is what they call a \"test\", right?"#)),
    );
}

#[test]
fn str_pat() {
    assert_eq!(one::<_, Infallible>("abc")("abcdef"), Ok(("def", "abc")));
}

#[test]
fn array_pat() {
    assert_eq!(
        until_ex::<_, Infallible>([';', '\''])("abc;def'xyz"),
        Ok(("def'xyz", "abc"))
    );
}

#[test]
fn union_pat() {
    let src = "abc\\def'xyz;";
    assert_eq!(
        until_ex::<_, Infallible>((';', '\''))(src),
        until_ex([';', '\''])(src)
    );
}

#[test]
fn chain_pattern_immediate_match_success() {
    assert_eq!(
        one::<_, Infallible>('a'.and('b'))("abcde"),
        Ok(("cde", "ab")),
    );
}

#[test]
fn chain_pattern_immediate_match_p1_fails() {
    assert_eq!(
        one::<_, Infallible>('a'.and('b'))("xbc"),
        Err(ParsingError::new_recoverable("xbc")),
    );
}

#[test]
fn chain_pattern_immediate_match_p2_fails() {
    assert_eq!(
        one::<_, Infallible>('a'.and('b'))("axc"),
        Err(ParsingError::new_recoverable("axc")),
    );
}

#[test]
fn chain_pattern_immediate_match_predicate_patterns() {
    assert_eq!(
        one::<_, Infallible>(ascii_digit.and(alphabetic))("3x rest"),
        Ok((" rest", "3x")),
    );
}

#[test]
fn chain_pattern_first_match_ex_found_immediately() {
    assert_eq!(
        one::<_, Infallible>('$'.and('{'))("${bar}"),
        Ok(("bar}", "${")),
    );
}

#[test]
fn chain_pattern_first_match_ex_skips_p1_without_p2() {
    assert_eq!(
        until_ex::<_, Infallible>('a'.and('b'))("xaabyz"),
        Ok(("yz", "xa")),
    );
}

#[test]
fn chain_pattern_first_match_ex_string_patterns() {
    assert_eq!(
        until_ex::<_, Infallible>('$'.and('{'))("foo${bar}"),
        Ok(("bar}", "foo")),
    );
}

#[test]
fn chain_pattern_first_match_ex_no_match() {
    // No 'a' is ever immediately followed by 'b'.
    assert!(until_ex::<_, Infallible>('a'.and('b'))("xaxcyz").is_err());
}

#[test]
fn chain_pattern_first_match_not_first_p1_match() {
    assert_eq!(
        until_ex::<_, Infallible>('a'.and("--"))("aaaa--aaa"),
        Ok(("aaa", "aaa")),
    )
}

#[test]
fn many_matches_zero_or_more() {
    assert_eq!(
        one::<_, Infallible>(ascii_digit.many())("123abc"),
        Ok(("abc", "123")),
    );
    assert_eq!(
        one::<_, Infallible>(ascii_digit.many())("abc"),
        Ok(("abc", "")),
    );
}

#[test]
fn many_chains_with_other_patterns() {
    // "1" or more digits followed by an alphabetic character.
    assert_eq!(
        one::<_, Infallible>(ascii_digit.and(ascii_digit.many()))("123abc"),
        Ok(("abc", "123")),
    );
}

#[test]
fn many_used_in_until_ex() {
    assert_eq!(
        until_ex::<_, Infallible>(ascii_digit.many().and(';'))("abc123;rest"),
        Ok(("rest", "abc")),
    );
}

#[test]
fn maybe_matches_zero_or_one() {
    assert_eq!(
        one::<_, Infallible>('-'.maybe())("-123"),
        Ok(("123", "-")),
    );
    assert_eq!(
        one::<_, Infallible>('-'.maybe())("123"),
        Ok(("123", "")),
    );
}

#[test]
fn maybe_chains_with_other_patterns() {
    assert_eq!(
        one::<_, Infallible>('-'.maybe().and(ascii_digit).and(ascii_digit.many()))("-123abc"),
        Ok(("abc", "-123")),
    );
    assert_eq!(
        one::<_, Infallible>('-'.many().and(ascii_digit).and(ascii_digit.many()))("123abc"),
        Ok(("abc", "123")),
    );
}

#[test]
fn maybe_pattern_immediate_match_p1_fails() {
    assert_eq!(
        one::<_, Infallible>('-'.maybe().and('1'))("1xyz"),
        Ok(("xyz", "1")),
    );
    assert_eq!(
        one::<_, Infallible>('-'.maybe().and('1'))("2xyz"),
        Err(ParsingError::new_recoverable("2xyz")),
    );
}