1use std::marker::PhantomData;
2use std::iter::FromIterator;
3use {Consume, ParseError, ParseResult, Parser, ParserBase, ParserMut, ParserOnce, Stream};
4
5pub trait ParserIteratorBase {
6 type Input;
7 type Element;
8 fn emptiable() -> bool
9 where
10 Self: Sized,
11 {
12 false
13 }
14
15 fn collect<V: FromIterator<Self::Element>>(self) -> Collect<Self, V>
16 where
17 Self: Sized,
18 {
19 collect(self)
20 }
21}
22pub trait ParserIteratorMut<S: Stream<Item = Self::Input> + ?Sized>
23 : ParserIteratorBase {
24 type State;
25 fn begin(&self) -> Self::State;
26 fn next_mut(
27 &mut self,
28 stream: &mut S,
29 state: &mut Self::State,
30 ) -> ParseResult<Option<(Option<Self::Element>, Consume)>>;
31 fn emit_expectations(&self, stream: &mut S);
32}
33pub trait ParserIterator<S: Stream<Item = Self::Input> + ?Sized>
34 : ParserIteratorMut<S> {
35 fn next(
36 &self,
37 stream: &mut S,
38 state: &mut Self::State,
39 ) -> ParseResult<Option<(Option<Self::Element>, Consume)>>;
40}
41
42pub(crate) fn many1<P: ParserBase>(p: P) -> Many1<P> {
43 Many1(p)
44}
45pub struct Many1<P: ParserBase>(P);
46impl<P> ParserIteratorBase for Many1<P>
47where
48 P: ParserBase,
49{
50 type Input = P::Input;
51 type Element = P::Output;
52 fn emptiable() -> bool {
53 P::emptiable()
54 }
55}
56impl<S, P> ParserIteratorMut<S> for Many1<P>
57where
58 S: Stream<Item = P::Input> + ?Sized,
59 P: ParserMut<S>,
60{
61 type State = usize;
62 fn begin(&self) -> usize {
63 0
64 }
65 fn next_mut(
66 &mut self,
67 stream: &mut S,
68 state: &mut Self::State,
69 ) -> ParseResult<Option<(Option<Self::Element>, Consume)>> {
70 if let Some((x, c)) = self.0.parse_lookahead_mut(stream)? {
71 *state += 1;
72 Ok(Some((Some(x), c)))
73 } else if *state == 0 {
74 Ok(None)
75 } else {
76 Ok(Some((None, Consume::Empty)))
77 }
78 }
79 fn emit_expectations(&self, stream: &mut S) {
80 self.0.emit_expectations(stream);
81 }
82}
83impl<S, P> ParserIterator<S> for Many1<P>
84where
85 S: Stream<Item = P::Input> + ?Sized,
86 P: Parser<S>,
87{
88 fn next(
89 &self,
90 stream: &mut S,
91 state: &mut Self::State,
92 ) -> ParseResult<Option<(Option<Self::Element>, Consume)>> {
93 if let Some((x, c)) = self.0.parse_lookahead(stream)? {
94 *state += 1;
95 Ok(Some((Some(x), c)))
96 } else if *state == 0 {
97 Ok(None)
98 } else {
99 Ok(Some((None, Consume::Empty)))
100 }
101 }
102}
103
104pub(crate) fn collect<P, V>(p: P) -> Collect<P, V>
105where
106 P: ParserIteratorBase,
107 V: FromIterator<P::Element>,
108{
109 Collect(p, PhantomData)
110}
111pub struct Collect<P, V>(P, PhantomData<fn() -> V>)
112where
113 P: ParserIteratorBase,
114 V: FromIterator<P::Element>;
115impl<P, V> ParserBase for Collect<P, V>
116where
117 P: ParserIteratorBase,
118 V: FromIterator<P::Element>,
119{
120 type Input = P::Input;
121 type Output = V;
122 fn emptiable() -> bool {
123 P::emptiable()
124 }
125}
126impl<S, P, V> ParserOnce<S> for Collect<P, V>
127where
128 S: Stream<Item = P::Input> + ?Sized,
129 P: ParserIteratorMut<S>,
130 V: FromIterator<P::Element>,
131{
132 fn parse_lookahead_once(
133 mut self,
134 stream: &mut S,
135 ) -> ParseResult<Option<(Self::Output, Consume)>> {
136 ParserMut::parse_lookahead_mut(&mut self, stream)
137 }
138 fn emit_expectations(&self, stream: &mut S) {
139 self.0.emit_expectations(stream);
140 }
141}
142impl<S, P, V> ParserMut<S> for Collect<P, V>
143where
144 S: Stream<Item = P::Input> + ?Sized,
145 P: ParserIteratorMut<S>,
146 V: FromIterator<P::Element>,
147{
148 fn parse_lookahead_mut(
149 &mut self,
150 stream: &mut S,
151 ) -> ParseResult<Option<(Self::Output, Consume)>> {
152 let state = self.0.begin();
153 let mut iter = CollectorMut {
154 parser: &mut self.0,
155 stream,
156 state,
157 consume: Consume::Empty,
158 };
159 let x: ParseResult<Option<Self::Output>> = iter.by_ref().collect();
160 x.map(|x| x.map(|x| (x, iter.consume)))
161 }
162}
163impl<S, P, V> Parser<S> for Collect<P, V>
164where
165 S: Stream<Item = P::Input> + ?Sized,
166 P: ParserIterator<S>,
167 V: FromIterator<P::Element>,
168{
169 fn parse_lookahead(&self, stream: &mut S) -> ParseResult<Option<(Self::Output, Consume)>> {
170 let state = self.0.begin();
171 let mut iter = Collector {
172 parser: &self.0,
173 stream,
174 state,
175 consume: Consume::Empty,
176 };
177 let x: ParseResult<Option<Self::Output>> = iter.by_ref().collect();
178 x.map(|x| x.map(|x| (x, iter.consume)))
179 }
180}
181
182struct CollectorMut<'a, S, P>
183where
184 S: Stream<Item = P::Input> + ?Sized + 'a,
185 P: ParserIteratorMut<S> + 'a,
186{
187 stream: &'a mut S,
188 parser: &'a mut P,
189 state: P::State,
190 consume: Consume,
191}
192impl<'a, S, P> Iterator for CollectorMut<'a, S, P>
193where
194 S: Stream<Item = P::Input> + ?Sized + 'a,
195 P: ParserIteratorMut<S> + 'a,
196{
197 type Item = ParseResult<Option<P::Element>>;
198
199 #[inline]
200 fn next(&mut self) -> Option<Self::Item> {
201 match self.parser.next_mut(self.stream, &mut self.state) {
202 Ok(Some((Some(x), c))) => {
203 self.consume |= c;
204 Some(Ok(Some(x)))
205 }
206 Ok(Some((None, _))) => None,
207 Ok(None) => if self.consume == Consume::Empty {
208 Some(Ok(None))
209 } else {
210 Some(Err(ParseError::SyntaxError))
211 },
212 Err(e) => Some(Err(e)),
213 }
214 }
215}
216
217struct Collector<'a, S, P>
218where
219 S: Stream<Item = P::Input> + ?Sized + 'a,
220 P: ParserIterator<S> + 'a,
221{
222 stream: &'a mut S,
223 parser: &'a P,
224 state: P::State,
225 consume: Consume,
226}
227impl<'a, S, P> Iterator for Collector<'a, S, P>
228where
229 S: Stream<Item = P::Input> + ?Sized + 'a,
230 P: ParserIterator<S> + 'a,
231{
232 type Item = ParseResult<Option<P::Element>>;
233
234 #[inline]
235 fn next(&mut self) -> Option<Self::Item> {
236 match self.parser.next(self.stream, &mut self.state) {
237 Ok(Some((Some(x), c))) => {
238 self.consume |= c;
239 Some(Ok(Some(x)))
240 }
241 Ok(Some((None, _))) => None,
242 Ok(None) => if self.consume == Consume::Empty {
243 Some(Ok(None))
244 } else {
245 Some(Err(ParseError::SyntaxError))
246 },
247 Err(e) => Some(Err(e)),
248 }
249 }
250}