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
use crate::{CreateParserState, SeparatedParser};
use crate::{
    IntegerParser, LiteralParser, ParseResult, Parser, RepeatParser, SequenceParser,
    SequenceParserState, StringParser,
};

/// Data that can be parsed incrementally.
pub trait HasParser {
    /// The parser for the data.
    type Parser: Parser<Output = Self>;

    /// Create a new parser.
    fn new_parser() -> Self::Parser;

    /// Create a new parser state.
    fn create_parser_state() -> <Self::Parser as Parser>::PartialState;
}

macro_rules! int_parser {
    ($ty:ident, $num:ty, $test:ident) => {
        #[doc = "A parser for `"]
        #[doc = stringify!($num)]
        #[doc = "`."]
        #[derive(Clone, Debug)]
        pub struct $ty {
            parser: IntegerParser,
        }

        impl Default for $ty {
            fn default() -> Self {
                Self {
                    parser: IntegerParser::new((<$num>::MIN as i128)..=(<$num>::MAX as i128)),
                }
            }
        }

        impl CreateParserState for $ty {
            fn create_parser_state(&self) -> <Self as Parser>::PartialState {
                self.parser.create_parser_state()
            }
        }

        impl Parser for $ty {
            type Error = <IntegerParser as Parser>::Error;
            type Output = $num;
            type PartialState = <IntegerParser as Parser>::PartialState;

            fn parse<'a>(
                &self,
                state: &Self::PartialState,
                input: &'a [u8],
            ) -> Result<ParseResult<'a, Self::PartialState, Self::Output>, Self::Error> {
                self.parser
                    .parse(state, input)
                    .map(|result| result.map(|output| output as $num))
            }
        }

        impl HasParser for $num {
            type Parser = $ty;

            fn new_parser() -> Self::Parser {
                $ty::default()
            }

            fn create_parser_state() -> <Self::Parser as Parser>::PartialState {
                Default::default()
            }
        }

        #[test]
        fn $test() {
            let parser = <$num as HasParser>::new_parser();
            let state = <$num as HasParser>::create_parser_state();
            for _ in 0..100 {
                let input = rand::random::<$num>();
                let input_str = input.to_string() + "\n";
                println!("input: {:?}", input_str);
                let result = parser.parse(&state, input_str.as_bytes());
                assert_eq!(
                    result,
                    Ok(ParseResult::Finished {
                        result: input,
                        remaining: b"\n",
                    })
                );
            }
        }
    };
}

int_parser!(U8Parser, u8, test_u8);
int_parser!(U16Parser, u16, test_u16);
int_parser!(U32Parser, u32, test_u32);
int_parser!(U64Parser, u64, test_u64);
int_parser!(I8Parser, i8, test_i8);
int_parser!(I16Parser, i16, test_i16);
int_parser!(I32Parser, i32, test_i32);
int_parser!(I64Parser, i64, test_i64);

impl HasParser for String {
    type Parser = StringParser<fn(char) -> bool>;

    fn new_parser() -> Self::Parser {
        StringParser::new(0..=usize::MAX)
    }

    fn create_parser_state() -> <Self::Parser as Parser>::PartialState {
        Default::default()
    }
}

/// A parser for a vector of a type.
#[derive(Clone, Debug)]
pub struct VecParser<T: HasParser> {
    parser: SequenceParser<
        LiteralParser<&'static str>,
        SequenceParser<
            SeparatedParser<T::Parser, LiteralParser<&'static str>>,
            LiteralParser<&'static str>,
        >,
    >,
}

impl<T: HasParser> Default for VecParser<T>
where
    <T::Parser as Parser>::PartialState: Clone,
    <T::Parser as Parser>::Output: Clone,
    <T as HasParser>::Parser: CreateParserState,
{
    fn default() -> Self {
        Self {
            parser: SequenceParser::new(
                LiteralParser::new("["),
                SequenceParser::new(
                    SeparatedParser::new(T::new_parser(), LiteralParser::new(", "), 0..=usize::MAX),
                    LiteralParser::new("]"),
                ),
            ),
        }
    }
}

impl<T: HasParser> CreateParserState for VecParser<T>
where
    <T::Parser as Parser>::PartialState: Clone,
    <T::Parser as Parser>::Output: Clone,
    <T as HasParser>::Parser: CreateParserState,
{
    fn create_parser_state(&self) -> <Self as Parser>::PartialState {
        self.parser.create_parser_state()
    }
}

impl<T: HasParser> Parser for VecParser<T>
where
    <T::Parser as Parser>::PartialState: Clone,
    <T::Parser as Parser>::Output: Clone,
    <T as HasParser>::Parser: CreateParserState,
{
    type Error = <SequenceParser<
        LiteralParser<&'static str>,
        SequenceParser<
            RepeatParser<SequenceParser<T::Parser, LiteralParser<&'static str>>>,
            LiteralParser<&'static str>,
        >,
    > as Parser>::Error;
    type Output = Vec<<T::Parser as Parser>::Output>;
    type PartialState = <SequenceParser<
        LiteralParser<&'static str>,
        SequenceParser<
            SeparatedParser<T::Parser, LiteralParser<&'static str>>,
            LiteralParser<&'static str>,
        >,
    > as Parser>::PartialState;

    fn parse<'a>(
        &self,
        state: &Self::PartialState,
        input: &'a [u8],
    ) -> Result<ParseResult<'a, Self::PartialState, Self::Output>, Self::Error> {
        self.parser
            .parse(state, input)
            .map(|result| result.map(|((), (outputs, ()))| outputs))
    }
}

impl<T: HasParser> HasParser for Vec<T>
where
    <T::Parser as Parser>::PartialState: Clone,
    <T::Parser as Parser>::Output: Clone,
    <T as HasParser>::Parser: CreateParserState,
{
    type Parser = VecParser<T>;

    fn new_parser() -> Self::Parser {
        VecParser::default()
    }

    fn create_parser_state() -> <Self::Parser as Parser>::PartialState {
        SequenceParserState::default()
    }
}

/// A parser for a fixed size array of a type.
pub struct ArrayParser<const N: usize, T: HasParser> {
    parser: SequenceParser<
        LiteralParser<&'static str>,
        SequenceParser<
            SeparatedParser<T::Parser, LiteralParser<&'static str>>,
            LiteralParser<&'static str>,
        >,
    >,
}

impl<const N: usize, T: HasParser> Default for ArrayParser<N, T>
where
    <T::Parser as Parser>::PartialState: Clone,
    <T::Parser as Parser>::Output: Clone,
    <T as HasParser>::Parser: CreateParserState,
{
    fn default() -> Self {
        Self {
            parser: SequenceParser::new(
                LiteralParser::new("["),
                SequenceParser::new(
                    SeparatedParser::new(T::new_parser(), LiteralParser::new(", "), N..=N),
                    LiteralParser::new("]"),
                ),
            ),
        }
    }
}

impl<const N: usize, T: HasParser> CreateParserState for ArrayParser<N, T>
where
    <T::Parser as Parser>::PartialState: Clone,
    <T::Parser as Parser>::Output: Clone,
    <T as HasParser>::Parser: CreateParserState,
{
    fn create_parser_state(&self) -> <Self as Parser>::PartialState {
        self.parser.create_parser_state()
    }
}

impl<const N: usize, T: HasParser> Parser for ArrayParser<N, T>
where
    <T::Parser as Parser>::PartialState: Clone,
    <T::Parser as Parser>::Output: Clone,
    <T as HasParser>::Parser: CreateParserState,
{
    type Error = <SequenceParser<
        LiteralParser<&'static str>,
        SequenceParser<
            RepeatParser<SequenceParser<T::Parser, LiteralParser<&'static str>>>,
            LiteralParser<&'static str>,
        >,
    > as Parser>::Error;
    type Output = [<T::Parser as Parser>::Output; N];
    type PartialState = <SequenceParser<
        LiteralParser<&'static str>,
        SequenceParser<
            SeparatedParser<T::Parser, LiteralParser<&'static str>>,
            LiteralParser<&'static str>,
        >,
    > as Parser>::PartialState;

    fn parse<'a>(
        &self,
        state: &Self::PartialState,
        input: &'a [u8],
    ) -> Result<ParseResult<'a, Self::PartialState, Self::Output>, Self::Error> {
        self.parser.parse(state, input).map(|result| {
            result.map(|((), (outputs, ()))| {
                outputs
                    .try_into()
                    .unwrap_or_else(|_| panic!("ArrayParser: wrong number of elements"))
            })
        })
    }
}

impl<const N: usize, T: HasParser> HasParser for [T; N]
where
    <T::Parser as Parser>::PartialState: Clone,
    <T::Parser as Parser>::Output: Clone,
    <T as HasParser>::Parser: CreateParserState,
{
    type Parser = ArrayParser<N, T>;

    fn new_parser() -> Self::Parser {
        ArrayParser::default()
    }

    fn create_parser_state() -> <Self::Parser as Parser>::PartialState {
        SequenceParserState::default()
    }
}