wiwi/parser/
stateful.rs

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
use crate::prelude_std::*;
use crate::num::*;
use super::{ stateless, util, Error, Input, Needle, Parser as _, ParserPhantom, Result, Success };

pub trait ParserStateful<I, O, E = ()>
where
	I: Input
{
	fn parse(&mut self, input: I) -> Result<I, O, E>;

	#[inline]
	fn map<F, O2>(self, f: F) -> util::Map<Self, F, O>
	where
		Self: Sized,
		F: FnMut(O) -> O2
	{
		util::map(self, f)
	}
}

impl<F, I, O, E> ParserStateful<I, O, E> for F
where
	I: Input,
	F: FnMut(I) -> Result<I, O, E>
{
	#[inline]
	fn parse(&mut self, input: I) -> Result<I, O, E> {
		self(input)
	}
}

#[inline]
pub fn delimited<P, I, O, E, EReal, PBefore, OBefore, EBefore, PAfter, OAfter, EAfter>(
	parser_before: PBefore,
	parser: P,
	parser_after: PAfter
) -> Delimited<P, I, O, E, EReal, PBefore, OBefore, EBefore, PAfter, OAfter, EAfter>
where
	I: Input,
	PBefore: ParserStateful<I, OBefore, EBefore>,
	P: ParserStateful<I, O, E>,
	PAfter: ParserStateful<I, OAfter, EAfter>,
	EBefore: Into<EReal>,
	E: Into<EReal>,
	EAfter: Into<EReal>
{
	Delimited {
		parser_before,
		parser,
		parser_after,
		__marker: PhantomData
	}
}

macro_rules! decl_num_fn {
	{ $($(#[$meta:meta])* $struct:ident $fn:ident)* } => {
		$(
			$(#[$meta])*
			#[inline]
			pub fn $fn() -> $struct {
				$struct { inner: stateless::$fn() }
			}
		)*
	}
}

decl_num_fn! {
	NumU8LE num_u8_le
	NumU8BE num_u8_be
	NumU8NE num_u8_ne

	NumU16LE num_u16_le
	NumU16BE num_u16_be
	NumU16NE num_u16_ne

	NumU32LE num_u32_le
	NumU32BE num_u32_be
	NumU32NE num_u32_ne

	NumU64LE num_u64_le
	NumU64BE num_u64_be
	NumU64NE num_u64_ne

	NumU128LE num_u128_le
	NumU128BE num_u128_be
	NumU128NE num_u128_ne

	NumI8LE num_i8_le
	NumI8BE num_i8_be
	NumI8NE num_i8_ne

	NumI16LE num_i16_le
	NumI16BE num_i16_be
	NumI16NE num_i16_ne

	NumI32LE num_i32_le
	NumI32BE num_i32_be
	NumI32NE num_i32_ne

	NumI64LE num_i64_le
	NumI64BE num_i64_be
	NumI64NE num_i64_ne

	NumI128LE num_i128_le
	NumI128BE num_i128_be
	NumI128NE num_i128_ne

	NumF32LE num_f32_le
	NumF32BE num_f32_be
	NumF32NE num_f32_ne

	NumF64LE num_f64_le
	NumF64BE num_f64_be
	NumF64NE num_f64_ne

	NumUsizeLE num_usize_le
	NumUsizeBE num_usize_be
	NumUsizeNE num_usize_ne
	NumIsizeLE num_isize_le
	NumIsizeBE num_isize_be
	NumIsizeNE num_isize_ne
}

#[inline]
pub fn tag<T>(tag: T) -> Tag<T> {
	Tag { inner: stateless::tag(tag) }
}

#[inline]
pub fn take<N>(amount: N) -> Take
where
	N: IntoUsize
{
	Take { inner: stateless::take(amount) }
}

#[inline]
pub fn take_array<const N: usize>() -> TakeArray<N> {
	TakeArray { inner: stateless::take_array() }
}

#[inline]
pub fn take_const<const N: usize>() -> TakeConst<N> {
	TakeConst { inner: stateless::take_const() }
}

#[inline]
pub fn void<P, I, O, E>(parser: P) -> Void<P, I, O, E>
where
	I: Input,
	P: ParserStateful<I, O, E>
{
	Void { parser, __marker: PhantomData }
}

#[expect(
	clippy::type_complexity,
	reason = "good naming makes it look alright I guess lol"
)]
pub struct Delimited<P, I, O, E, EReal, PBefore, OBefore, EBefore, PAfter, OAfter, EAfter>
where
	I: Input
{
	parser_before: PBefore,
	parser: P,
	parser_after: PAfter,
	__marker: PhantomData<(
		ParserPhantom<I, OBefore, EBefore>,
		ParserPhantom<I, O, E>,
		ParserPhantom<I, OAfter, EAfter>,
		fn() -> EReal
	)>
}

impl<P, I, O, E, EReal, PBefore, OBefore, EBefore, PAfter, OAfter, EAfter> ParserStateful<I, O, EReal>
for Delimited<P, I, O, E, EReal, PBefore, OBefore, EBefore, PAfter, OAfter, EAfter>
where
	I: Input,
	PBefore: ParserStateful<I, OBefore, EBefore>,
	P: ParserStateful<I, O, E>,
	PAfter: ParserStateful<I, OAfter, EAfter>,
	EBefore: Into<EReal>,
	E: Into<EReal>,
	EAfter: Into<EReal>
{
	#[inline]
	fn parse(&mut self, input: I) -> Result<I, O, EReal> {
		let Success {
			output: _output_before,
			remaining_input: input
		} = self.parser_before.parse(input).map_err(Error::into)?;

		let Success {
			output,
			remaining_input: input
		} = self.parser.parse(input).map_err(Error::into)?;

		let Success {
			output: _output_after,
			remaining_input
		} = self.parser_after.parse(input).map_err(Error::into)?;

		Ok(Success { output, remaining_input })
	}
}

macro_rules! decl_num_struct {
	{ $($(#[$meta:meta])* $num:ident $struct_le:ident $struct_be:ident $struct_ne:ident)* } => {
		$(
			decl_num_struct! { $(#[$meta])* $num $struct_le }
			decl_num_struct! { $(#[$meta])* $num $struct_be }
			decl_num_struct! { $(#[$meta])* $num $struct_ne }
		)*
	};

	{ $(#[$meta:meta])* $num:ident $struct:ident } => {
		$(#[$meta])*
		#[repr(transparent)]
		pub struct $struct {
			inner: stateless::$struct
		}

		$(#[$meta])*
		impl<'h> ParserStateful<&'h [u8], $num> for $struct {
			#[inline]
			fn parse(&mut self, input: &'h [u8]) -> Result<&'h [u8], $num> {
				self.inner.parse(input)
			}
		}
	};
}

decl_num_struct! {
	u8 NumU8LE NumU8BE NumU8NE
	u16 NumU16LE NumU16BE NumU16NE
	u32 NumU32LE NumU32BE NumU32NE
	u64 NumU64LE NumU64BE NumU64NE
	u128 NumU128LE NumU128BE NumU128NE

	i8 NumI8LE NumI8BE NumI8NE
	i16 NumI16LE NumI16BE NumI16NE
	i32 NumI32LE NumI32BE NumI32NE
	i64 NumI64LE NumI64BE NumI64NE
	i128 NumI128LE NumI128BE NumI128NE

	f32 NumF32LE NumF32BE NumF32NE
	f64 NumF64LE NumF64BE NumF64NE

	usize NumUsizeLE NumUsizeBE NumUsizeNE
	isize NumIsizeLE NumIsizeBE NumIsizeNE
}

#[repr(transparent)]
pub struct Tag<T> {
	inner: stateless::Tag<T>
}

impl<I, T> ParserStateful<I, ()> for Tag<T>
where
	I: Input,
	T: Needle<I>
{
	#[inline]
	fn parse(&mut self, input: I) -> Result<I, ()> {
		self.inner.parse(input)
	}
}

#[repr(transparent)]
pub struct Take {
	inner: stateless::Take
}

impl<I> ParserStateful<I, I> for Take
where
	I: Input
{
	#[inline]
	fn parse(&mut self, input: I) -> Result<I, I> {
		self.inner.parse(input)
	}
}

pub struct TakeArray<const N: usize> {
	inner: stateless::TakeArray<N>
}

impl<I, const N: usize> ParserStateful<I, I::ConstSizeOwned<N>> for TakeArray<N>
where
	I: Input
{
	#[inline]
	fn parse(&mut self, input: I) -> Result<I, I::ConstSizeOwned<N>> {
		self.inner.parse(input)
	}
}

#[repr(transparent)]
pub struct TakeConst<const N: usize> {
	inner: stateless::TakeConst<N>
}

impl<I, const N: usize> ParserStateful<I, I::ConstSize<N>> for TakeConst<N>
where
	I: Input
{
	#[inline]
	fn parse(&mut self, input: I) -> Result<I, I::ConstSize<N>> {
		self.inner.parse(input)
	}
}

#[repr(transparent)]
pub struct Void<P, I, O, E>
where
	I: Input
{
	parser: P,
	__marker: ParserPhantom<I, O, E>
}

impl<P, I, O, E> ParserStateful<I, (), E> for Void<P, I, O, E>
where
	I: Input,
	P: ParserStateful<I, O, E>
{
	#[inline]
	fn parse(&mut self, input: I) -> Result<I, (), E> {
		let Success { output: _void, remaining_input } = self.parser.parse(input)?;
		Ok(Success { output: (), remaining_input })
	}
}