wiwi/parser/
util.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
use crate::prelude_std::*;
use super::{ stateful, stateless, Input, ParserPhantom, Result, Success };

/// Wraps an implementor of [`Parser`](stateless::Parser) and provides an implementation
/// of [`ParserStateful`](stateful::ParserStateful)
#[inline]
pub fn adapt_stateful<P, I, O, E>(parser: P) -> AdaptStateful<P, I, O, E>
where
	I: Input,
	P: stateless::Parser<I, O, E>
{
	AdaptStateful { parser, __marker: PhantomData }
}

#[inline]
pub(super) fn map<P, F, O>(parser: P, map: F) -> Map<P, F, O> {
	Map { parser, map, __marker: PhantomData }
}

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

impl<P, I, O, E> stateful::ParserStateful<I, O, E> for AdaptStateful<P, I, O, E>
where
	I: Input,
	P: stateless::Parser<I, O, E>
{
	#[inline]
	fn parse(&mut self, input: I) -> Result<I, O, E> {
		self.parser.parse(input)
	}
}

pub struct Map<P, F, O> {
	parser: P,
	map: F,
	__marker: PhantomData<fn(O) -> O>
}

impl<P, I, O, E, F, O2> stateless::Parser<I, O2, E> for Map<P, F, O>
where
	I: Input,
	P: stateless::Parser<I, O, E>,
	F: Fn(O) -> O2
{
	#[inline]
	fn parse(&self, input: I) -> Result<I, O2, E> {
		self.parser.parse(input).map(|Success { output, remaining_input }| {
			Success { output: (self.map)(output), remaining_input }
		})
	}
}

impl<P, I, O, E, F, O2> stateful::ParserStateful<I, O2, E> for Map<P, F, O>
where
	I: Input,
	P: stateful::ParserStateful<I, O, E>,
	F: FnMut(O) -> O2
{
	#[inline]
	fn parse(&mut self, input: I) -> Result<I, O2, E> {
		self.parser.parse(input).map(|Success { output, remaining_input }| {
			Success { output: (self.map)(output), remaining_input }
		})
	}
}