1#![allow(non_snake_case)]
2
3use crate::parser::{ParseResult, Parser};
4
5pub trait Then<P: Parser, T: Parser>: Parser {
6 fn then(parser: P, then: T) -> Self;
7}
8
9#[derive(Copy, Clone)]
10pub enum Unimplemented {}
11impl Parser for Unimplemented {
12 type Output<'i> = Unimplemented;
13 type Then<T: Parser> = Unimplemented;
14
15 fn parse<'i>(&self, _: &'i [u8]) -> ParseResult<'i, Self::Output<'i>> {
16 unimplemented!();
17 }
18}
19impl<P: Parser, T: Parser> Then<P, T> for Unimplemented {
20 fn then(_: P, _: T) -> Self {
21 unimplemented!();
22 }
23}
24
25macro_rules! then_impl {
26 (
27 $name:ident<$_:ident> => [$($t:ident),+],
28 $next_name:ident<$next_t:ident> => $($tail:tt)*
29 ) => {
30 #[derive(Copy, Clone)]
31 pub struct $name<$($t),+>{
32 $($t: $t,)+
33 }
34 impl<$($t: Parser),+> Parser for $name<$($t),+> {
35 type Output<'i> = ($($t::Output<'i>),+);
36 type Then<T: Parser> = $next_name<$($t),+, T>;
37
38 #[inline(always)]
39 fn parse<'i>(&self, input: &'i [u8]) -> ParseResult<'i, Self::Output<'i>> {
40 $(let ($t, input) = self.$t.parse(input)?;)+
41 Ok((($($t),+), input))
42 }
43 }
44 impl<$($t: Parser),+, T: Parser> Then<$name<$($t),+>, T> for $next_name<$($t),+, T> {
45 fn then(parser: $name<$($t),+>, next: T) -> Self {
46 Self{$($t: parser.$t),+, $next_t: next}
47 }
48 }
49 then_impl!{$next_name<$next_t> => $($tail)*}
50 };
51 (
52 $name:ident<$_:ident> => [$($t:ident),+],
53 ) => {
54 #[derive(Copy, Clone)]
55 pub struct $name<$($t),+>{
56 $($t: $t,)+
57 }
58 impl<$($t: Parser),+> Parser for $name<$($t),+> {
59 type Output<'i> = ($($t::Output<'i>),+);
60 type Then<T: Parser> = Unimplemented;
61
62 #[inline(always)]
63 fn parse<'i>(&self, input: &'i [u8]) -> ParseResult<'i, Self::Output<'i>> {
64 $(let ($t, input) = self.$t.parse(input)?;)+
65 Ok((($($t),+), input))
66 }
67 }
68 };
69}
70
71then_impl! {
72 Then2<B> => [A, B],
73 Then3<C> => [A, B, C],
74 Then4<D> => [A, B, C, D],
75 Then5<E> => [A, B, C, D, E],
76 Then6<F> => [A, B, C, D, E, F],
77 Then7<G> => [A, B, C, D, E, F, G],
78 Then8<H> => [A, B, C, D, E, F, G, H],
79}
80
81impl<A: Parser, B: Parser> Then<A, B> for Then2<A, B> {
82 fn then(parser: A, then: B) -> Self {
83 Then2 { A: parser, B: then }
84 }
85}