transduce 0.4.4

Zero-copy isomorphic parsing: your code should look like what it parses.
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
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

//! Common functions to drop in instead of reinventing the wheel.

#![allow(clippy::tests_outside_test_module, clippy::wildcard_imports)]

use crate::{print_error::PrintError, *};
use core::marker::PhantomData;

#[cfg(any(feature = "alloc", test))]
#[cfg_attr(test, allow(unused_imports))]
use alloc::{format, string::String, vec /* the macro */, vec::Vec};

/// Representation of a set. O(lg n)-search BST when allocation is available, otherwise an O(n) slice.
#[cfg(feature = "alloc")]
#[allow(clippy::single_char_lifetime_names)]
type Set<'a, T> = alloc::collections::BTreeSet<T>;

/// Representation of a set. O(lg n)-search BST when allocation is available, otherwise an O(n) slice.
#[cfg(not(feature = "alloc"))]
#[allow(clippy::single_char_lifetime_names)]
type Set<'a, T> = &'a [T];

/// Property test active only when testing (not in debug or release builds).
macro_rules! ptest {
    ($($tt:tt)*) => {
        #[cfg(test)]
        ::proptest::proptest! { $($tt)* }
    };
}

#[cfg(feature = "alloc")]
/// End-of-input error: expected an item but none remaining.
macro_rules! end_of_input {
    () => {
        return Err(ParseError {
            message: String::from("Reached end of input but expected an item"),
            not_yet_parsed: None,
        })
    };
}

#[cfg(not(feature = "alloc"))]
/// End-of-input error: expected an item but none remaining.
macro_rules! end_of_input {
    () => {
        return Err(ParseError {
            message: "Reached end of input but expected an item",
            not_yet_parsed: None,
        })
    };
}

// TODO: const
/// Return anything that does _not_ satisfy this predicate.
#[inline(always)]
#[must_use]
pub fn not<
    'input,
    Input: 'input + PrintError,
    Predicate: Fn(&'input Input) -> bool,
    WriteMessage: Fn(&'input Input) -> Message,
>(
    predicate: Predicate,
    msg: WriteMessage,
) -> Parser<'input, impl Parse<'input, Input = Input, Output = &'input Input>> {
    satisfy(move |x| !predicate(x), msg)
}
ptest! {
    #[test]
    fn prop_not(input: u8, threshold: u8) {
        let pred = move |x: &u8| x < &threshold;
        let slice = &[input];
        assert_eq!(pred(&input), not(pred, |_| string_if_alloc("")).parse(slice).is_err());
    }
}

/// Match any single item and return a reference to it.
#[derive(Debug)]
pub struct Anything<Input: PrintError>(PhantomData<Input>);
impl<'input, Input: 'input + PrintError> Parse<'input> for Anything<Input> {
    type Input = Input;
    type Output = &'input Input;
    #[inline(always)]
    fn partial(
        &self,
        input: &'input [Self::Input],
    ) -> Result<(Self::Output, &'input [Self::Input]), ParseError> {
        if let Some((head, tail)) = input.split_first() {
            Ok((head, tail))
        } else {
            end_of_input!()
        }
    }
}
/// Match any single item and return a reference to it.
#[inline(always)]
#[must_use]
pub const fn anything<'input, Input: 'input + PrintError>() -> Parser<'input, Anything<Input>> {
    Parser::new(Anything(PhantomData))
}
ptest! {
    #[test]
    fn prop_anything(input: u8) {
        assert_eq!(anything().parse(&[input]), Ok(&input));
    }
}

/// Make sure we've reached exactly the end of the stream of input; don't advance to it.
#[derive(Debug)]
pub struct End<Input: PrintError>(PhantomData<Input>);
impl<'input, Input: 'input + PrintError> Parse<'input> for End<Input> {
    type Input = Input;
    type Output = ();
    #[inline(always)]
    fn partial(
        &self,
        input: &'input [Self::Input],
    ) -> Result<(Self::Output, &'input [Self::Input]), ParseError> {
        if !input.is_empty() {
            bail!(
                "Expected the end of the input stream but found input remaining",
                "Expected the end of the input stream but found input remaining",
                input,
            )
        }
        Ok(((), &[]))
    }
}
/// Make sure we've reached exactly the end of the stream of input; don't advance to it.
#[inline(always)]
#[must_use]
pub const fn end<'input, Input: 'input + PrintError>() -> Parser<'input, End<Input>> {
    Parser::new(End(PhantomData))
}
ptest! {
    #[test]
    fn prop_end(slice: Vec<u8>) {
        assert_eq!(end().parse(&slice).is_ok(), slice.is_empty());
    }
}

// TODO: const (nightly?)
/// Skip zero or more items while this predicate holds on them.
#[inline(always)]
#[must_use]
pub fn satisfy<
    'input,
    Input: 'input + PrintError,
    Predicate: Fn(&'input Input) -> bool,
    WriteMessage: Fn(&'input Input) -> Message,
>(
    predicate: Predicate,
    msg: WriteMessage,
) -> Parser<'input, impl Parse<'input, Input = Input, Output = &'input Input>> {
    anything() ^ move |x| if predicate(x) { Ok(x) } else { Err(msg(x)) }
}
ptest! {
    #[test]
    fn prop_satisfy(input: u8, threshold: u8) {
        let pred = move |x: &u8| x < &threshold;
        let slice = &[input];
        assert_eq!(pred(&input), satisfy(pred, |_| string_if_alloc("")).parse(slice).is_ok());
    }
}

// TODO: const (nightly?)
/// Skip zero or more items while this predicate holds on them.
#[inline(always)]
#[must_use]
pub fn satisfy_result<
    'input,
    Input: 'input + PrintError,
    Predicate: Fn(&'input Input) -> Result<(), Message>,
>(
    predicate: Predicate,
) -> Parser<'input, impl Parse<'input, Input = Input, Output = &'input Input>> {
    anything() ^ move |x| predicate(x).map(|()| x)
}
ptest! {
    #[test]
    fn prop_satisfy_result(input: u8, threshold: u8) {
        let pred = move |x: &u8| x < &threshold;
        let slice = &[input];
        assert_eq!(pred(&input), satisfy_result(move |x| if pred(x) { Ok(()) } else { Err(string_if_alloc("")) }).parse(slice).is_ok());
    }
}

// TODO: const
/// Match exactly this item and return a reference to the original (not the parsed one).
#[inline(always)]
#[must_use]
pub fn exact<Input: PartialEq<Input> + PrintError>(
    reference: &'_ Input,
) -> Parser<'_, impl Parse<'_, Input = Input, Output = &'_ Input>> {
    #![cfg_attr(not(feature = "alloc"), allow(unused_variables))]
    satisfy(
        move |x| reference.eq(x),
        move |head| {
            format_if_alloc!(
                "Expected one exact item but found another",
                "Expected {reference:#?} but found {head:#?}"
            )
        },
    )
}
ptest! {
    #[test]
    fn prop_exact(a: u8, b: u8) {
        let slice_a = &[a];
        let slice_b = &[b];
        assert_eq!(exact(&a).parse(slice_a), Ok(&a));
        assert_eq!(exact(&a).parse(slice_b).is_ok(), a == b);
    }
}

/// Match exactly this sequence of items.
#[derive(Debug)]
pub struct ExactSeq<'platonic, Input: PartialEq + PrintError>(&'platonic [Input]);
impl<'input, Input: 'input + PartialEq + PrintError> Parse<'input> for ExactSeq<'_, Input> {
    type Input = Input;
    type Output = &'input [Input];
    #[inline(always)]
    fn partial(
        &self,
        input: &'input [Self::Input],
    ) -> Result<(Self::Output, &'input [Self::Input]), ParseError> {
        #![cfg_attr(not(feature = "alloc"), allow(unused_variables))]
        let mut etc = input;
        for i in self.0 {
            match etc.split_first() {
                None => end_of_input!(),
                Some((head, tail)) => {
                    if head != i {
                        let e = self.0;
                        bail!(
                            "Expected {e:#?} but found {head:#?} instead of {i:#?}",
                            "Expected one character but found another",
                            tail
                        )
                    }
                    etc = tail;
                }
            }
        }
        Ok((input.split_at(self.0.len()).0, etc))
    }
}
/// Match exactly this sequence of items.
#[inline(always)]
#[must_use]
pub const fn exact_seq<'platonic, 'input, Input: 'input + PartialEq + PrintError>(
    exactly: &'platonic [Input],
) -> Parser<'input, ExactSeq<'platonic, Input>> {
    Parser::new(ExactSeq(exactly))
}
ptest! {
    #[test]
    fn prop_exact_seq(a: Vec<u8>, b: Vec<u8>) {
        assert_eq!(exact_seq(&a).parse(&a), Ok(&*a));
        assert_eq!(exact_seq(&a).parse(&b).is_ok(), a == b);
    }
}

/// Match any of a set of options. Set represented as a binary tree for efficient lookup.
#[inline(always)]
#[must_use]
pub fn any<'input, Input: 'input + Ord + PrintError>(
    set: Set<'input, Input>,
) -> Parser<'input, impl Parse<'input, Input = Input, Output = &'input Input>> {
    satisfy_result(move |x| {
        if set.contains(x) {
            Ok(())
        } else {
            Err(format_if_alloc!(
                "Expected one of a set of single items but the actual item was not one of them",
                "Expected an element of {set:#?} but found {x:#?}"
            ))
        }
    })
}
ptest! {
    #[test]
    fn prop_any(input: u8, set: alloc::collections::BTreeSet<u8>) {
        let slice = &[input];
        #[cfg(feature="alloc")]
        assert_eq!(set.contains(&input), any(set).parse(slice).is_ok());
        #[cfg(not(feature="alloc"))]
        {
            let flat: Vec<_> = set.iter().copied().collect();
            assert_eq!(set.contains(&input), any(&flat).parse(slice).is_ok());
        }
    }
}

/// Match any of a set of sequences of options. Set represented as a binary tree for efficient lookup.
#[derive(Debug)]
pub struct AnySeq<'platonic, Input: Ord + PrintError>(Set<'platonic, &'platonic [Input]>); // TODO: reference the btree
impl<'input, Input: 'input + Ord + PrintError> Parse<'input> for AnySeq<'_, Input> {
    type Input = Input;
    type Output = &'input [Input];
    #[inline(always)]
    fn partial(
        &self,
        input: &'input [Self::Input],
    ) -> Result<(Self::Output, &'input [Self::Input]), ParseError> {
        #![cfg_attr(not(feature = "alloc"), allow(unused_variables))]
        let Some((head, _)) = input.split_first() else {
            if self.0.contains(&input/* [] */) {
                return Ok((input, input));
            }
            end_of_input!()
        };
        // With `alloc`: O(lg n)
        #[cfg(feature = "alloc")]
        for &option in self.0.range(core::slice::from_ref(head)..=input).rev() {
            if let Ok((parsed, etc)) = exact_seq(option).partial(input) {
                return Ok((parsed, etc));
            }
        }
        // Without: O(n)
        #[cfg(not(feature = "alloc"))]
        {
            let mut update_me: Option<(Self::Output, &'input [Self::Input])> = None;
            for &option in self.0 {
                if let Ok((best_yet, etc)) = exact_seq(option).partial(input) {
                    if let Some((prev_best_yet, prev_etc)) = update_me {
                        if etc.len() >= prev_etc.len() {
                            continue;
                        }
                    }
                    update_me = Some((best_yet, etc));
                }
            }
            if let Some(winner_winner_chicken_dinner) = update_me {
                return Ok(winner_winner_chicken_dinner);
            }
        }
        match input.split_first() {
            Some((_, tail)) => {
                let e = &self.0;
                bail!(
                    "Expected one of {e:#?}",
                    "Expected a sequence of characters but found another",
                    tail
                )
            }
            None => end_of_input!(),
        }
    }
}
/// Match any of a set of sequences of options, preferring the longest applicable. Set represented as a binary tree for efficient lookup.
#[inline(always)]
#[must_use]
pub const fn any_seq<'platonic, 'input, Input: 'input + Ord + PrintError>(
    set: Set<'platonic, &'platonic [Input]>,
) -> Parser<'input, AnySeq<'platonic, Input>> {
    Parser::new(AnySeq(set))
}
ptest! {
    #[test]
    fn prop_any_seq(mut input: Vec<u8>, mut aux: Vec<u8>) {
        #[cfg(feature = "alloc")]
        assert_eq!(input == aux, any_seq(Set::from_iter([&*aux])).parse(&input).is_ok());
        #[cfg(not(feature = "alloc"))]
        assert_eq!(input == aux, any_seq(&[&aux]).parse(&input).is_ok());
        input.append(&mut aux);
        #[cfg(feature = "alloc")]
        assert_eq!(any_seq(Set::from_iter([&*input])).partial(&input).map(|(a, _)| a), Ok(&*input));
        #[cfg(not(feature = "alloc"))]
        assert_eq!(any_seq(&[&input]).partial(&input).map(|(a, _)| a), Ok(&*input));
    }
    #[test]
    #[allow(clippy::indexing_slicing)]
    fn prop_any_seq_prefix(input: Vec<u8>) {
        // Construct a set of all prefixes of `input`, including `input` itself (last),
        // and we should still end up with the whole enchilada
        #[cfg(feature="alloc")]
        assert_eq!(any_seq((0..=input.len()).map(|i| &input[..i]).collect()).parse(&input), Ok(&*input));
        #[cfg(not(feature="alloc"))] {
            let v: Vec<_> = (0..=input.len()).map(|i| &input[..i]).collect();
            assert_eq!(any_seq(&v).parse(&input), Ok(&*input));
        }
    }
}

/// Parse if possible; otherwise, don't move.
#[derive(Debug)]
pub struct Optional<'input, Fallible: Parse<'input>>(Parser<'input, Fallible>);
impl<'input, Fallible: Parse<'input>> Parse<'input> for Optional<'input, Fallible> {
    type Input = Fallible::Input;
    type Output = Option<Fallible::Output>;
    #[inline(always)]
    fn partial(
        &self,
        input: &'input [Self::Input],
    ) -> Result<(Self::Output, &'input [Self::Input]), ParseError> {
        Ok(self
            .0
            .partial(input)
            .map_or_else(|_| (None, input), |(parsed, etc)| (Some(parsed), etc)))
    }
}
/// Parse if possible; otherwise, don't move.
#[inline(always)]
#[must_use]
pub const fn optional<'input, Fallible: Parse<'input>>(
    fallible: Parser<'input, Fallible>,
) -> Parser<'input, Optional<'input, Fallible>> {
    Parser::new(Optional(fallible))
}
ptest! {
    #[test]
    #[allow(clippy::unwrap_used)]
    fn prop_optional(input: [u8; 1]) {
        let parser = optional(lowercase());
        let c = input.first().unwrap();
        if c.is_ascii_lowercase() {
            assert_eq!(parser.partial(&input), Ok((Some(c), &[][..])));
        } else {
            assert_eq!(parser.partial(&input), Ok((None, &input[..])));
        }
    }
}

/// Continue parsing while a predicate holds. Return a contiguous slice referencing the section of input that held.
#[derive(Debug)]
pub struct ParseWhile<Input: PrintError, Predicate: Fn(&Input) -> bool>(
    Predicate,
    PhantomData<Input>,
);
impl<'input, Input: 'input + PrintError, Predicate: Fn(&Input) -> bool> Parse<'input>
    for ParseWhile<Input, Predicate>
{
    type Input = Input;
    type Output = &'input [Input];
    #[inline(always)]
    fn partial(
        &self,
        input: &'input [Self::Input],
    ) -> Result<(Self::Output, &'input [Self::Input]), ParseError> {
        for (i, x) in input.iter().enumerate() {
            if !self.0(x) {
                return Ok(input.split_at(i));
            }
        }
        Ok((input, &[]))
    }
}
/// Match any of a set of sequences of options. Set represented as a binary tree for efficient lookup.
#[inline(always)]
#[must_use]
pub const fn parse_while<'input, Input: 'input + PrintError, Predicate: Fn(&Input) -> bool>(
    predicate: Predicate,
) -> Parser<'input, ParseWhile<Input, Predicate>> {
    Parser::new(ParseWhile(predicate, PhantomData))
}
ptest! {
    #[test]
    #[allow(clippy::unwrap_used)]
    fn prop_parse_while(input: Vec<u8>, threshold: u8) {
        let prop = move |x: &u8| x < &threshold;
        let (pass, etc) = parse_while(prop).partial(&input).unwrap();
        for x in pass {
            assert!(prop(x));
        }
        if let Some(x) = etc.first() {
            assert!(!prop(x));
        }
    }
}

// TODO: enable without `alloc` with a maximum length
#[cfg(feature = "alloc")]
/// Continue parsing as long as we can. Return a contiguous slice referencing the section of input that successfully parsed.
#[derive(Debug)]
pub struct Runaway<'input, Each: Parse<'input>>(Parser<'input, Each>);
#[cfg(feature = "alloc")]
impl<'input, Each: Parse<'input>> Parse<'input> for Runaway<'input, Each> {
    type Input = Each::Input;
    type Output = Vec<Each::Output>;
    #[inline(always)]
    fn partial(
        &self,
        input: &'input [Self::Input],
    ) -> Result<(Self::Output, &'input [Self::Input]), ParseError> {
        let mut results = vec![];
        let mut remaining = input;
        while let Ok((parsed, etc)) = self.0.partial(remaining) {
            results.push(parsed);
            remaining = etc;
        }
        Ok((results, remaining))
    }
}
#[cfg(feature = "alloc")]
/// Match any of a set of sequences of options. Set represented as a binary tree for efficient lookup.
#[inline(always)]
#[must_use]
pub const fn runaway<'input, Each: Parse<'input>>(
    each: Parser<'input, Each>,
) -> Parser<'input, Runaway<'input, Each>> {
    Parser::new(Runaway(each))
}
#[cfg(feature = "alloc")]
ptest! {
    #[test]
    #[allow(clippy::unwrap_used)]
    fn prop_runaway(input: Vec<u8>) {
        let (pass, etc) = runaway(lowercase()).partial(&input).unwrap();
        for x in pass {
            assert!(x.is_ascii_lowercase());
        }
        if let Some(x) = etc.first() {
            assert!(!x.is_ascii_lowercase());
        }
    }
}

/// Match zero or more whitespace characters and return a reference to their contiguous slice.
#[inline(always)]
#[must_use]
pub fn maybe_space<'input>() -> Parser<'input, impl Parse<'input, Input = u8>> {
    parse_while(u8::is_ascii_whitespace)
}
// any test would follow tautologically from the definition of `parse_while`

/// Match one or more whitespace characters and return a reference to their contiguous slice.
#[inline(always)]
#[must_use]
pub fn need_space<'input>() -> Parser<'input, impl Parse<'input, Input = u8>> {
    #![allow(clippy::arithmetic_side_effects)]
    satisfy(u8::is_ascii_whitespace, |_| {
        string_if_alloc("Expected whitespace")
    }) >> parse_while(u8::is_ascii_whitespace)
}
// any test would follow tautologically from the definition of `parse_while`

/// Match any lowercase letter and return a reference to it.
#[inline(always)]
#[must_use]
pub fn lowercase<'input>() -> Parser<'input, impl Parse<'input, Input = u8, Output = &'input u8>> {
    #![cfg_attr(not(feature = "alloc"), allow(unused_variables))]
    satisfy(u8::is_ascii_lowercase, |x| {
        format_if_alloc!(
            "Expected a lowercase letter",
            "Expected a lowercase letter but found '{:}'",
            char::from(*x)
        )
    })
}
ptest! {
    #[test]
    fn prop_lowercase(input: u8) {
        let slice = &[input];
        assert_eq!(lowercase().partial(slice).is_ok(), input.is_ascii_lowercase());
    }
}

/// Match any uppercase letter and return a reference to it.
#[inline(always)]
#[must_use]
pub fn uppercase<'input>() -> Parser<'input, impl Parse<'input, Input = u8, Output = &'input u8>> {
    #![cfg_attr(not(feature = "alloc"), allow(unused_variables))]
    satisfy(u8::is_ascii_uppercase, |x| {
        format_if_alloc!(
            "Expected an uppercase letter",
            "Expected an uppercase letter but found '{:}'",
            char::from(*x)
        )
    })
}
ptest! {
    #[test]
    fn prop_uppercase(input: u8) {
        let slice = &[input];
        assert_eq!(uppercase().partial(slice).is_ok(), input.is_ascii_uppercase());
    }
}

#[cfg(feature = "alloc")]
/// Parse a punctuated (e.g. comma-separated) list of elements determined by the parsers passed in here.
#[derive(Debug)]
pub struct Punctuated<'input, Element: Parse<'input>, Punct: Parse<'input, Input = Element::Input>>
{
    /// Parser for each element.
    element: Parser<'input, Element>,
    /// Parser for punctuation.
    punct: Parser<'input, Punct>,
    /// Whether to allow trailing punctuation, e.g. `a, b, c,` instead of only `a, b, c`.
    allow_trailing: bool,
}
#[cfg(feature = "alloc")]
impl<'input, Element: Parse<'input>, Punct: Parse<'input, Input = Element::Input>> Parse<'input>
    for Punctuated<'input, Element, Punct>
{
    type Input = Punct::Input;
    type Output = Vec<Element::Output>;
    #[inline(always)]
    fn partial(
        &self,
        input: &'input [Self::Input],
    ) -> Result<(Self::Output, &'input [Self::Input]), ParseError> {
        let (first, mut remaining) = self.element.partial(input)?;
        let mut results = vec![first];
        while let Ok(Ok((out, etc))) = {
            self.punct
                .partial(remaining)
                .map(|(_, etc)| self.element.partial(etc))
        } {
            results.push(out);
            remaining = etc;
        }
        if self.allow_trailing {
            if let Ok((_, after_trailing)) = self.punct.partial(remaining) {
                remaining = after_trailing;
            }
        }
        Ok((results, remaining))
    }
}
#[cfg(feature = "alloc")]
/// Match any of a set of sequences of options. Set represented as a binary tree for efficient lookup.
#[inline(always)]
#[must_use]
pub const fn punctuated<
    'input,
    Element: Parse<'input>,
    Punct: Parse<'input, Input = Element::Input>,
>(
    element: Parser<'input, Element>,
    punct: Parser<'input, Punct>,
    allow_trailing: bool,
) -> Parser<'input, Punctuated<'input, Element, Punct>> {
    Parser::new(Punctuated {
        element,
        punct,
        allow_trailing,
    })
}

#[cfg(feature = "alloc")]
/// Match any of a set of sequences of options. Set represented as a binary tree for efficient lookup.
#[inline(always)]
#[must_use]
pub fn comma_separated<'input, Element: Parse<'input, Input = u8>>(
    element: Parser<'input, Element>,
    allow_trailing: bool,
) -> Parser<
    'input,
    DiscardLeft<
        'input,
        impl Parse<'input, Input = u8>,
        Punctuated<
            'input,
            DiscardRight<'input, Element, impl Parse<'input, Input = u8>>,
            impl Parse<'input, Input = u8>,
        >,
    >,
> {
    #![allow(clippy::arithmetic_side_effects)]
    maybe_space()
        >> punctuated(
            element << maybe_space(),
            exact(&b',') << maybe_space(),
            allow_trailing,
        )
}

#[cfg(feature = "alloc")]
/// Parse a punctuated (e.g. by mathematical operators) list of elements determined by the parsers passed in here.
#[derive(Debug)]
pub struct PunctuatedMeaningfully<
    'input,
    Element: Parse<'input>,
    Punct: Parse<'input, Input = Element::Input>,
> {
    /// Parser for each element.
    element: Parser<'input, Element>,
    /// Parser for punctuation.
    punct: Parser<'input, Punct>,
    /// Whether to allow trailing punctuation, e.g. `a, b, c,` instead of only `a, b, c`.
    allow_trailing: bool,
}
#[cfg(feature = "alloc")]
impl<'input, Element: Parse<'input>, Punct: Parse<'input, Input = Element::Input>> Parse<'input>
    for PunctuatedMeaningfully<'input, Element, Punct>
{
    type Input = Punct::Input;
    type Output = (Element::Output, Vec<(Punct::Output, Element::Output)>);
    #[inline(always)]
    fn partial(
        &self,
        input: &'input [Self::Input],
    ) -> Result<(Self::Output, &'input [Self::Input]), ParseError> {
        let (head, mut remaining) = self.element.partial(input)?;
        let mut tail = vec![];
        while let Ok((p, Ok((e, etc)))) = self
            .punct
            .partial(remaining)
            .map(|(parsed, etc)| (parsed, self.element.partial(etc)))
        {
            tail.push((p, e));
            remaining = etc;
        }
        if self.allow_trailing {
            if let Ok((_, after_trailing)) = self.punct.partial(remaining) {
                remaining = after_trailing;
            }
        }
        Ok(((head, tail), remaining))
    }
}
#[cfg(feature = "alloc")]
/// Match any of a set of sequences of options. Set represented as a binary tree for efficient lookup.
#[inline(always)]
#[must_use]
pub const fn punctuated_meaningfully<
    'input,
    Element: Parse<'input>,
    Punct: Parse<'input, Input = Element::Input>,
>(
    element: Parser<'input, Element>,
    punct: Parser<'input, Punct>,
    allow_trailing: bool,
) -> Parser<'input, PunctuatedMeaningfully<'input, Element, Punct>> {
    Parser::new(PunctuatedMeaningfully {
        element,
        punct,
        allow_trailing,
    })
}

/// Match a `snake_case` term.
#[inline(always)]
#[must_use]
pub fn snake_case<'input>() -> Parser<'input, impl Parse<'input, Input = u8, Output = &'input [u8]>>
{
    parse_while(|x| {
        (lowercase() | exact(&b'_'))
            .partial(core::slice::from_ref(x))
            .is_ok()
    })
}
ptest! {
    #[test]
    fn prop_snake_case(input: Vec<u8>) {
        let filtered = input.into_iter().filter(|x| matches!(x, &(b'a'..=b'z') | &b'_')).collect::<Vec<_>>();
        assert_eq!(snake_case().parse(&filtered), Ok(&*filtered));
    }
}

/// Match a `SCREAMING_CASE` term.
#[inline(always)]
#[must_use]
pub fn screaming_case<'input>(
) -> Parser<'input, impl Parse<'input, Input = u8, Output = &'input [u8]>> {
    parse_while(|x| {
        (uppercase() | exact(&b'_'))
            .partial(core::slice::from_ref(x))
            .is_ok()
    })
}
ptest! {
    #[test]
    fn prop_screaming_case(input: Vec<u8>) {
        let filtered = input.into_iter().filter(|x| matches!(x, &(b'A'..=b'Z') | &b'_')).collect::<Vec<_>>();
        assert_eq!(screaming_case().parse(&filtered), Ok(&*filtered));
    }
}

/// Match a single digit.
#[inline(always)]
#[must_use]
pub fn digit<'input>() -> Parser<'input, impl Parse<'input, Input = u8, Output = u8>> {
    #![cfg_attr(not(feature = "alloc"), allow(unused_variables))]
    satisfy(u8::is_ascii_digit, |x| {
        format_if_alloc!(
            "Expected an uppercase letter",
            "Expected an uppercase letter but found '{:}' (ASCII #{x:})",
            char::from(*x)
        )
    })
    .pipe(|x| {
        #[allow(clippy::unnecessary_lazy_evaluations)]
        x.checked_sub(b'0').ok_or_else(|| {
            format_if_alloc!(
                "Found an ASCII digit but couldn't subtract 0 from it",
                "Found an ASCII digit but couldn't subtract '0' from it. Digit was '{:}' (ASCII #{x:}).",
                char::from(*x)
            )
        })
    })
}

#[cfg(feature = "alloc")]
/// Match a base-ten unsigned integer.
#[inline(always)]
#[must_use]
pub fn unsigned_integer<'input>() -> Parser<'input, impl Parse<'input, Input = u8, Output = usize>>
{
    runaway(digit()).pipe(move |s| {
        s.into_iter().fold(Ok(0), |acc, digit| {
            acc.map_or_else(Err, |x: usize| {
                match x.checked_mul(10).map(|x0| x0.checked_add(digit.into())) {
                    Some(Some(y)) => Ok(y),
                    _ => Err(string_if_alloc(
                        "Parsing error: parsed integer that would overflow a Rust `usize`",
                    )),
                }
            })
        })
    })
}

#[cfg(feature = "alloc")]
/// Match a base-ten signed integer.
#[inline(always)]
#[must_use]
pub fn signed_integer<'input>() -> Parser<'input, impl Parse<'input, Input = u8, Output = isize>> {
    (optional(exact(&b'-'))
        & runaway(digit()).pipe(move |s| {
            s.into_iter().fold(Ok(0), |acc, digit| {
                acc.map_or_else(Err, |x: isize| {
                    match x.checked_mul(10).map(|x0| x0.checked_add(digit.into())) {
                        Some(Some(y)) => Ok(y),
                        _ => Err(String::from(
                            "Parsing error: parsed integer that would overflow a Rust `isize`",
                        )),
                    }
                })
            })
        }))
    .pipe(|(maybe_neg, abs)| {
        if maybe_neg.is_some() {
            abs.checked_neg().ok_or_else(|| {
                String::from(
                    "Parsing error: parsed a negative integer that would overflow a Rust `isize`",
                )
            })
        } else {
            Ok(abs)
        }
    })
}

#[cfg(feature = "alloc")]
/// Operator-precedence parsing, e.g. for infix math.
pub mod precedence {
    #[allow(clippy::wildcard_imports)]
    use super::*;

    /// Parse binary operations without grouping them by precedence into a tree: just return a list of operators and things to the right of them.
    #[inline(always)]
    #[must_use]
    pub fn raw_binary_ops<'platonic, 'input, Primary: Parse<'input, Input = u8>>(
        primary: Parser<'input, Primary>,
        ops: Set<'platonic, &'platonic [u8]>,
    ) -> Parser<
        'input,
        PunctuatedMeaningfully<
            'input,
            DiscardRight<'input, Primary, impl Parse<'input, Input = u8>>,
            DiscardRight<'input, AnySeq<'platonic, u8>, impl Parse<'input, Input = u8>>,
        >,
    > {
        #![allow(clippy::arithmetic_side_effects)]
        punctuated_meaningfully(
            primary << maybe_space(),
            any_seq(ops) << maybe_space(),
            false,
        )
    }
}

/// Common patterns in programming languages.
pub mod lang {
    #[allow(clippy::wildcard_imports)]
    use super::*;

    /// Parse a `let some_name = expression...;`-style binding.
    #[inline(always)]
    #[must_use]
    pub fn let_expr<'input, P: Parse<'input, Input = u8>>(
        expression: Parser<'input, P>,
    ) -> Parser<
        'input,
        Both<
            'input,
            impl Parse<'input, Input = u8, Output = &'input [u8]>,
            DiscardLeft<'input, impl Parse<'input, Input = u8>, P>,
        >,
    > {
        #![allow(clippy::arithmetic_side_effects)]
        exact_seq(b"let") >> need_space() >> snake_case()
            & maybe_space() >> exact(&b'=') >> maybe_space() >> expression
    }
}