1 //! Combinators which take one or more parsers and applies them repeatedly.
2
3 use crate::{
4 error::{
5 Commit, ParseError,
6 ParseResult::{self, *},
7 ResultExt, StdParseResult, StreamError, Tracked,
8 },
9 lib::{borrow::BorrowMut, cmp, marker::PhantomData, mem},
10 parser::{
11 choice::{optional, Optional, Or},
12 combinator::{ignore, Ignore},
13 function::{parser, FnParser},
14 sequence::With,
15 token::{value, Value},
16 FirstMode, ParseMode,
17 },
18 stream::{uncons, Stream, StreamOnce},
19 ErrorOffset, Parser,
20 };
21
22 parser! {
23 pub struct Count;
24
25 /// Parses `parser` from zero up to `count` times.
26 ///
27 /// ```
28 /// # extern crate combine;
29 /// # use combine::*;
30 /// # use combine::error::Info;
31 /// # use combine::stream::easy::Error;
32 /// # fn main() {
33 /// let mut parser = count(2, token(b'a'));
34 ///
35 /// let result = parser.parse(&b"aaab"[..]);
36 /// assert_eq!(result, Ok((b"aa"[..].to_owned(), &b"ab"[..])));
37 /// # }
38 /// ```
39 pub fn count[F, Input, P](count: usize, parser: P)(Input) -> F
40 where [
41 Input: Stream,
42 P: Parser<Input>,
43 F: Extend<P::Output> + Default,
44 ]
45 {
46 count_min_max(0, *count, parser)
47 }
48 }
49
50 parser! {
51 pub struct SkipCount;
52 type PartialState = <With<Count<Sink, Input, P>, Value<Input, ()>> as Parser<Input>>::PartialState;
53 /// Parses `parser` from zero up to `count` times skipping the output of `parser`.
54 ///
55 /// ```
56 /// # extern crate combine;
57 /// # use combine::*;
58 /// # use combine::stream::easy::{Error, Info};
59 /// # fn main() {
60 /// let mut parser = skip_count(2, token(b'a'));
61 ///
62 /// let result = parser.parse(&b"aaab"[..]);
63 /// assert_eq!(result, Ok(((), &b"ab"[..])));
64 /// # }
65 /// ```
66 pub fn skip_count[Input, P](count: usize, parser: P)(Input) -> ()
67 where [
68 P: Parser<Input>
69 ]
70 {
71 self::count::<Sink, _, _>(*count, parser.map(|_| ())).with(value(()))
72 }
73 }
74
75 #[derive(Copy, Clone)]
76 pub struct CountMinMax<F, P> {
77 parser: P,
78 min: usize,
79 max: usize,
80 _marker: PhantomData<fn() -> F>,
81 }
82
83 struct SuggestSizeHint<I> {
84 iterator: I,
85 min: usize,
86 max: Option<usize>,
87 }
88
89 impl<I> Iterator for SuggestSizeHint<I>
90 where
91 I: Iterator,
92 {
93 type Item = I::Item;
94
95 #[inline]
next(&mut self) -> Option<Self::Item>96 fn next(&mut self) -> Option<Self::Item> {
97 self.iterator.next()
98 }
99
100 #[inline]
size_hint(&self) -> (usize, Option<usize>)101 fn size_hint(&self) -> (usize, Option<usize>) {
102 (self.min, self.max)
103 }
104 }
105
suggest_size_hint<I>(iterator: I, (min, max): (usize, Option<usize>)) -> SuggestSizeHint<I> where I: Iterator,106 fn suggest_size_hint<I>(iterator: I, (min, max): (usize, Option<usize>)) -> SuggestSizeHint<I>
107 where
108 I: Iterator,
109 {
110 SuggestSizeHint {
111 iterator,
112 // Invalid input may report an extreme size so we guard against that (while still
113 // optimizing by preallocating for the expected case of success)
114 min: cmp::min(min, 4096),
115 max,
116 }
117 }
118
119 impl<Input, P, F> Parser<Input> for CountMinMax<F, P>
120 where
121 Input: Stream,
122 P: Parser<Input>,
123 F: Extend<P::Output> + Default,
124 {
125 type Output = F;
126 type PartialState = (usize, F, P::PartialState);
127
128 parse_mode!(Input);
129 #[inline]
parse_mode_impl<M>( &mut self, mode: M, input: &mut Input, state: &mut Self::PartialState, ) -> ParseResult<Self::Output, Input::Error> where M: ParseMode,130 fn parse_mode_impl<M>(
131 &mut self,
132 mode: M,
133 input: &mut Input,
134 state: &mut Self::PartialState,
135 ) -> ParseResult<Self::Output, Input::Error>
136 where
137 M: ParseMode,
138 {
139 let (count, elements, child_state) = state;
140
141 let mut iter = self.parser.by_ref().partial_iter(mode, input, child_state);
142 let remaining_min = self.min.saturating_sub(*count);
143 let remaining_max = self.max - *count;
144 elements.extend(suggest_size_hint(
145 iter.by_ref().take(remaining_max).inspect(|_| *count += 1),
146 (remaining_min, Some(remaining_max)),
147 ));
148 if *count < self.min {
149 let err = StreamError::message_format(format_args!(
150 "expected {} more elements",
151 self.min - *count
152 ));
153 iter.fail(err)
154 } else {
155 iter.into_result_fast(elements).map(|x| {
156 *count = 0;
157 x
158 })
159 }
160 }
161
add_error(&mut self, error: &mut Tracked<<Input as StreamOnce>::Error>)162 fn add_error(&mut self, error: &mut Tracked<<Input as StreamOnce>::Error>) {
163 self.parser.add_error(error)
164 }
165 }
166
167 /// Parses `parser` from `min` to `max` times (including `min` and `max`).
168 ///
169 /// ```
170 /// # extern crate combine;
171 /// # use combine::*;
172 /// # use combine::stream::easy::{Error, Info};
173 /// # fn main() {
174 /// let mut parser = count_min_max(2, 2, token(b'a'));
175 ///
176 /// let result = parser.parse(&b"aaab"[..]);
177 /// assert_eq!(result, Ok((b"aa"[..].to_owned(), &b"ab"[..])));
178 /// let result = parser.parse(&b"ab"[..]);
179 /// assert!(result.is_err());
180 /// # }
181 /// ```
182 ///
183 /// # Panics
184 ///
185 /// If `min` > `max`.
count_min_max<F, Input, P>(min: usize, max: usize, parser: P) -> CountMinMax<F, P> where Input: Stream, P: Parser<Input>, F: Extend<P::Output> + Default,186 pub fn count_min_max<F, Input, P>(min: usize, max: usize, parser: P) -> CountMinMax<F, P>
187 where
188 Input: Stream,
189 P: Parser<Input>,
190 F: Extend<P::Output> + Default,
191 {
192 assert!(min <= max);
193
194 CountMinMax {
195 parser,
196 min,
197 max,
198 _marker: PhantomData,
199 }
200 }
201
202 parser! {
203 pub struct SkipCountMinMax;
204 type PartialState = <With<CountMinMax<Sink, P>, Value<Input, ()>> as Parser<Input>>::PartialState;
205 /// Parses `parser` from `min` to `max` times (including `min` and `max`)
206 /// skipping the output of `parser`.
207 ///
208 /// ```
209 /// # extern crate combine;
210 /// # use combine::*;
211 /// # fn main() {
212 /// let mut parser = skip_count_min_max(2, 2, token(b'a'));
213 ///
214 /// let result = parser.parse(&b"aaab"[..]);
215 /// assert_eq!(result, Ok(((), &b"ab"[..])));
216 /// let result = parser.parse(&b"ab"[..]);
217 /// assert!(result.is_err());
218 /// # }
219 /// ```
220 ///
221 /// # Panics
222 ///
223 /// If `min` > `max`.
224 pub fn skip_count_min_max[Input, P](min: usize, max: usize, parser: P)(Input) -> ()
225 where [
226 P: Parser<Input>,
227 ]
228 {
229 count_min_max::<Sink, _, _>(*min, *max, parser.map(|_| ())).with(value(()))
230 }
231 }
232
233 pub struct Iter<'a, Input, P, S, M>
234 where
235 Input: Stream,
236 P: Parser<Input>,
237 {
238 parser: P,
239 input: &'a mut Input,
240 committed: bool,
241 state: State<<Input as StreamOnce>::Error>,
242 partial_state: S,
243 mode: M,
244 }
245
246 enum State<E> {
247 Ok,
248 PeekErr(E),
249 CommitErr(E),
250 }
251
252 impl<'a, Input, P, S, M> Iter<'a, Input, P, S, M>
253 where
254 Input: Stream,
255 P: Parser<Input>,
256 S: BorrowMut<P::PartialState>,
257 {
258 pub fn new(parser: P, mode: M, input: &'a mut Input, partial_state: S) -> Self {
259 Iter {
260 parser,
261 input,
262 committed: false,
263 state: State::Ok,
264 partial_state,
265 mode,
266 }
267 }
268 /// Converts the iterator to a `ParseResult`, returning `Ok` if the parsing so far has be done
269 /// without any errors which committed data.
270 pub fn into_result<O>(self, value: O) -> StdParseResult<O, Input> {
271 self.into_result_(value).into()
272 }
273
274 fn into_result_<O>(self, value: O) -> ParseResult<O, Input::Error> {
275 match self.state {
276 State::Ok | State::PeekErr(_) => {
277 if self.committed {
278 CommitOk(value)
279 } else {
280 PeekOk(value)
281 }
282 }
283 State::CommitErr(e) => CommitErr(e),
284 }
285 }
286
287 fn into_result_fast<O>(self, value: &mut O) -> ParseResult<O, Input::Error>
288 where
289 O: Default,
290 {
291 match self.state {
292 State::Ok | State::PeekErr(_) => {
293 let value = mem::take(value);
294 if self.committed {
295 CommitOk(value)
296 } else {
297 PeekOk(value)
298 }
299 }
300 State::CommitErr(e) => CommitErr(e),
301 }
302 }
303
304 fn fail<T>(
305 self,
306 err: <<Input as StreamOnce>::Error as ParseError<
307 <Input as StreamOnce>::Token,
308 <Input as StreamOnce>::Range,
309 <Input as StreamOnce>::Position,
310 >>::StreamError,
311 ) -> ParseResult<T, Input::Error> {
312 match self.state {
313 State::Ok => {
314 let err = <Input as StreamOnce>::Error::from_error(self.input.position(), err);
315 if self.committed {
316 CommitErr(err)
317 } else {
318 PeekErr(err.into())
319 }
320 }
321 State::PeekErr(mut e) => {
322 let err = <Input as StreamOnce>::Error::from_error(self.input.position(), err);
323 e = e.merge(err);
324 if self.committed {
325 CommitErr(e)
326 } else {
327 PeekErr(e.into())
328 }
329 }
330 State::CommitErr(mut e) => {
331 e.add(err);
332 CommitErr(e)
333 }
334 }
335 }
336 }
337
338 impl<'a, Input, P, S, M> Iterator for Iter<'a, Input, P, S, M>
339 where
340 Input: Stream,
341 P: Parser<Input>,
342 S: BorrowMut<P::PartialState>,
343 M: ParseMode,
344 {
345 type Item = P::Output;
346
347 fn next(&mut self) -> Option<P::Output> {
348 let before = self.input.checkpoint();
349 match self
350 .parser
351 .parse_mode(self.mode, self.input, self.partial_state.borrow_mut())
352 {
353 PeekOk(v) => {
354 self.mode.set_first();
355 Some(v)
356 }
357 CommitOk(v) => {
358 self.mode.set_first();
359 self.committed = true;
360 Some(v)
361 }
362 PeekErr(e) => {
363 self.state = match self.input.reset(before) {
364 Err(err) => State::CommitErr(err),
365 Ok(_) => State::PeekErr(e.error),
366 };
367 None
368 }
369 CommitErr(e) => {
370 self.state = State::CommitErr(e);
371 None
372 }
373 }
374 }
375 }
376
377 #[derive(Copy, Clone)]
378 pub struct Many<F, P>(P, PhantomData<F>);
379
380 impl<F, Input, P> Parser<Input> for Many<F, P>
381 where
382 Input: Stream,
383 P: Parser<Input>,
384 F: Extend<P::Output> + Default,
385 {
386 type Output = F;
387 type PartialState = (F, P::PartialState);
388
389 parse_mode!(Input);
390 #[inline]
391 fn parse_mode_impl<M>(
392 &mut self,
393 mode: M,
394 input: &mut Input,
395 state: &mut Self::PartialState,
396 ) -> ParseResult<Self::Output, Input::Error>
397 where
398 M: ParseMode,
399 {
400 // TODO
401 let (ref mut elements, ref mut child_state) = *state;
402
403 let mut iter = (&mut self.0).partial_iter(mode, input, child_state);
404 elements.extend(iter.by_ref());
405 iter.into_result_fast(elements)
406 }
407
408 fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
409 self.0.add_error(errors)
410 }
411
412 fn add_committed_expected_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
413 self.add_error(errors);
414 }
415
416 fn parser_count(&self) -> ErrorOffset {
417 self.0.parser_count()
418 }
419 }
420
421 /// Parses `p` zero or more times returning a collection with the values from `p`.
422 ///
423 /// If the returned collection cannot be inferred type annotations must be supplied, either by
424 /// annotating the resulting type binding `let collection: Vec<_> = ...` or by specializing when
425 /// calling many, `many::<Vec<_>, _, _>(...)`.
426 ///
427 /// NOTE: If `p` can succeed without consuming any input this may hang forever as `many` will
428 /// repeatedly use `p` to parse the same location in the input every time
429 ///
430 /// ```
431 /// # extern crate combine;
432 /// # use combine::*;
433 /// # use combine::parser::char::digit;
434 /// # fn main() {
435 /// let result = many(digit())
436 /// .parse("123A")
437 /// .map(|x| x.0);
438 /// assert_eq!(result, Ok(vec!['1', '2', '3']));
439 /// # }
440 /// ```
441 pub fn many<F, Input, P>(p: P) -> Many<F, P>
442 where
443 Input: Stream,
444 P: Parser<Input>,
445 F: Extend<P::Output> + Default,
446 {
447 Many(p, PhantomData)
448 }
449
450 #[derive(Copy, Clone)]
451 pub struct Many1<F, P>(P, PhantomData<fn() -> F>);
452 impl<F, Input, P> Parser<Input> for Many1<F, P>
453 where
454 Input: Stream,
455 F: Extend<P::Output> + Default,
456 P: Parser<Input>,
457 {
458 type Output = F;
459 type PartialState = (bool, bool, F, P::PartialState);
460
461 parse_mode!(Input);
462 #[inline]
463 fn parse_mode_impl<M>(
464 &mut self,
465 mut mode: M,
466 input: &mut Input,
467 state: &mut Self::PartialState,
468 ) -> ParseResult<F, Input::Error>
469 where
470 M: ParseMode,
471 {
472 let (ref mut parsed_one, ref mut committed_state, ref mut elements, ref mut child_state) =
473 *state;
474
475 if mode.is_first() || !*parsed_one {
476 debug_assert!(!*parsed_one);
477
478 let (first, committed) = ctry!(self.0.parse_mode(mode, input, child_state));
479 elements.extend(Some(first));
480 // TODO Should PeekOk be an error?
481 *committed_state = !committed.is_peek();
482 *parsed_one = true;
483 mode.set_first();
484 }
485
486 let mut iter = Iter {
487 parser: &mut self.0,
488 committed: *committed_state,
489 input,
490 state: State::Ok,
491 partial_state: child_state,
492 mode,
493 };
494 elements.extend(iter.by_ref());
495
496 iter.into_result_fast(elements).map(|x| {
497 *parsed_one = false;
498 x
499 })
500 }
501
502 fn add_committed_expected_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
503 self.add_error(errors);
504 }
505
506 forward_parser!(Input, add_error parser_count, 0);
507 }
508
509 /// Parses `p` one or more times returning a collection with the values from `p`.
510 ///
511 /// If the returned collection cannot be inferred type annotations must be supplied, either by
512 /// annotating the resulting type binding `let collection: Vec<_> = ...` or by specializing when
513 /// calling many1 `many1::<Vec<_>, _>(...)`.
514 ///
515 /// NOTE: If `p` can succeed without consuming any input this may hang forever as `many1` will
516 /// repeatedly use `p` to parse the same location in the input every time
517 ///
518 ///
519 /// ```
520 /// # extern crate combine;
521 /// # use combine::*;
522 /// # use combine::parser::char::digit;
523 /// # fn main() {
524 /// let result = many1::<Vec<_>, _, _>(digit())
525 /// .parse("A123");
526 /// assert!(result.is_err());
527 /// # }
528 /// ```
529 pub fn many1<F, Input, P>(p: P) -> Many1<F, P>
530 where
531 Input: Stream,
532 F: Extend<P::Output> + Default,
533 P: Parser<Input>,
534 {
535 Many1(p, PhantomData)
536 }
537
538 #[derive(Clone)]
539 #[doc(hidden)]
540 // FIXME Should not be public
541 pub struct Sink;
542
543 impl Default for Sink {
544 fn default() -> Self {
545 Sink
546 }
547 }
548
549 impl<A> Extend<A> for Sink {
550 fn extend<T>(&mut self, iter: T)
551 where
552 T: IntoIterator<Item = A>,
553 {
554 for _ in iter {}
555 }
556 }
557
558 parser! {
559 pub struct SkipMany;
560 type PartialState = <Ignore<Many<Sink, Ignore<P>>> as Parser<Input>>::PartialState;
561 /// Parses `p` zero or more times ignoring the result.
562 ///
563 /// NOTE: If `p` can succeed without consuming any input this may hang forever as `skip_many` will
564 /// repeatedly use `p` to parse the same location in the input every time
565 ///
566 /// ```
567 /// # extern crate combine;
568 /// # use combine::*;
569 /// # use combine::parser::char::digit;
570 /// # fn main() {
571 /// let result = skip_many(digit())
572 /// .parse("A");
573 /// assert_eq!(result, Ok(((), "A")));
574 /// # }
575 /// ```
576 pub fn skip_many[Input, P](p: P)(Input) -> ()
577 where [
578 P: Parser<Input>,
579 ]
580 {
581 ignore(many::<Sink, _, _>(ignore(p)))
582 }
583 }
584
585 parser! {
586 pub struct SkipMany1;
587 type PartialState = <Ignore<Many1<Sink, Ignore<P>>> as Parser<Input>>::PartialState;
588 /// Parses `p` one or more times ignoring the result.
589 ///
590 /// NOTE: If `p` can succeed without consuming any input this may hang forever as `skip_many1` will
591 /// repeatedly use `p` to parse the same location in the input every time
592 ///
593 /// ```
594 /// # extern crate combine;
595 /// # use combine::*;
596 /// # use combine::parser::char::digit;
597 /// # fn main() {
598 /// let result = skip_many1(digit())
599 /// .parse("123A");
600 /// assert_eq!(result, Ok(((), "A")));
601 /// # }
602 /// ```
603 pub fn skip_many1[Input, P](p: P)(Input) -> ()
604 where [
605 P: Parser<Input>,
606 ]
607 {
608 ignore(many1::<Sink, _, _>(ignore(p)))
609 }
610 }
611
612 #[derive(Copy, Clone)]
613 pub struct SepBy<F, P, S> {
614 parser: P,
615 separator: S,
616 _marker: PhantomData<fn() -> F>,
617 }
618 impl<F, Input, P, S> Parser<Input> for SepBy<F, P, S>
619 where
620 Input: Stream,
621 F: Extend<P::Output> + Default,
622 P: Parser<Input>,
623 S: Parser<Input>,
624 {
625 type Output = F;
626 type PartialState = <Or<
627 SepBy1<F, P, S>,
628 FnParser<Input, fn(&mut Input) -> StdParseResult<F, Input>>,
629 > as Parser<Input>>::PartialState;
630
631 parse_mode!(Input);
632 #[inline]
633 fn parse_mode_impl<M>(
634 &mut self,
635 mode: M,
636 input: &mut Input,
637 state: &mut Self::PartialState,
638 ) -> ParseResult<F, Input::Error>
639 where
640 M: ParseMode,
641 {
642 sep_by1(&mut self.parser, &mut self.separator)
643 .or(parser(|_| Ok((F::default(), Commit::Peek(())))))
644 .parse_mode(mode, input, state)
645 }
646
647 fn add_committed_expected_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
648 self.separator.add_error(errors)
649 }
650
651 forward_parser!(Input, add_error parser_count, parser);
652 }
653
654 /// Parses `parser` zero or more time separated by `separator`, returning a collection with the
655 /// values from `p`.
656 ///
657 /// If the returned collection cannot be inferred type annotations must be supplied, either by
658 /// annotating the resulting type binding `let collection: Vec<_> = ...` or by specializing when
659 /// calling `sep_by`, `sep_by::<Vec<_>, _, _>(...)`.
660 ///
661 /// ```
662 /// # extern crate combine;
663 /// # use combine::*;
664 /// # use combine::parser::char::digit;
665 /// # fn main() {
666 /// let mut parser = sep_by(digit(), token(','));
667 /// let result_ok = parser.parse("1,2,3");
668 /// assert_eq!(result_ok, Ok((vec!['1', '2', '3'], "")));
669 /// let result_ok2 = parser.parse("");
670 /// assert_eq!(result_ok2, Ok((vec![], "")));
671 /// # }
672 /// ```
673 pub fn sep_by<F, Input, P, S>(parser: P, separator: S) -> SepBy<F, P, S>
674 where
675 Input: Stream,
676 F: Extend<P::Output> + Default,
677 P: Parser<Input>,
678 S: Parser<Input>,
679 {
680 SepBy {
681 parser,
682 separator,
683 _marker: PhantomData,
684 }
685 }
686
687 #[derive(Copy, Clone)]
688 pub struct SepBy1<F, P, S> {
689 parser: P,
690 separator: S,
691 _marker: PhantomData<fn() -> F>,
692 }
693 impl<F, Input, P, S> Parser<Input> for SepBy1<F, P, S>
694 where
695 Input: Stream,
696 F: Extend<P::Output> + Default,
697 P: Parser<Input>,
698 S: Parser<Input>,
699 {
700 type Output = F;
701 type PartialState = (
702 Option<Commit<()>>,
703 F,
704 <With<S, P> as Parser<Input>>::PartialState,
705 );
706
707 parse_mode!(Input);
708 #[inline]
709 fn parse_mode_impl<M>(
710 &mut self,
711 mode: M,
712 input: &mut Input,
713 state: &mut Self::PartialState,
714 ) -> ParseResult<Self::Output, Input::Error>
715 where
716 M: ParseMode,
717 {
718 let (ref mut parsed_one, ref mut elements, ref mut child_state) = *state;
719
720 let rest = match *parsed_one {
721 Some(rest) => rest,
722 None => {
723 let (first, rest) =
724 ctry!(self
725 .parser
726 .parse_mode(mode, input, &mut child_state.B.state));
727 elements.extend(Some(first));
728 rest
729 }
730 };
731
732 rest.combine_commit(move |_| {
733 let rest = (&mut self.separator).with(&mut self.parser);
734 let mut iter = Iter::new(rest, mode, input, child_state);
735
736 elements.extend(iter.by_ref());
737
738 iter.into_result_fast(elements).map(|x| {
739 *parsed_one = None;
740 x
741 })
742 })
743 }
744
745 fn add_committed_expected_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
746 self.separator.add_error(errors)
747 }
748
749 forward_parser!(Input, add_error parser_count, parser);
750 }
751
752 /// Parses `parser` one or more time separated by `separator`, returning a collection with the
753 /// values from `p`.
754 ///
755 /// If the returned collection cannot be inferred type annotations must be supplied, either by
756 /// annotating the resulting type binding `let collection: Vec<_> = ...` or by specializing when
757 /// calling `sep_by`, `sep_by1::<Vec<_>, _, _>(...)`.
758 ///
759 /// ```
760 /// # extern crate combine;
761 /// # use combine::*;
762 /// # use combine::parser::char::digit;
763 /// # use combine::stream::easy;
764 /// # use combine::stream::position::{self, SourcePosition};
765 /// # fn main() {
766 /// let mut parser = sep_by1(digit(), token(','));
767 /// let result_ok = parser.easy_parse(position::Stream::new("1,2,3"))
768 /// .map(|(vec, state)| (vec, state.input));
769 /// assert_eq!(result_ok, Ok((vec!['1', '2', '3'], "")));
770 /// let result_err = parser.easy_parse(position::Stream::new(""));
771 /// assert_eq!(result_err, Err(easy::Errors {
772 /// position: SourcePosition::default(),
773 /// errors: vec![
774 /// easy::Error::end_of_input(),
775 /// easy::Error::Expected("digit".into())
776 /// ]
777 /// }));
778 /// # }
779 /// ```
780 pub fn sep_by1<F, Input, P, S>(parser: P, separator: S) -> SepBy1<F, P, S>
781 where
782 Input: Stream,
783 F: Extend<P::Output> + Default,
784 P: Parser<Input>,
785 S: Parser<Input>,
786 {
787 SepBy1 {
788 parser,
789 separator,
790 _marker: PhantomData,
791 }
792 }
793
794 #[derive(Copy, Clone)]
795 pub struct SepEndBy<F, P, S> {
796 parser: P,
797 separator: S,
798 _marker: PhantomData<fn() -> F>,
799 }
800
801 impl<F, Input, P, S> Parser<Input> for SepEndBy<F, P, S>
802 where
803 Input: Stream,
804 F: Extend<P::Output> + Default,
805 P: Parser<Input>,
806 S: Parser<Input>,
807 {
808 type Output = F;
809 type PartialState = <Or<
810 SepEndBy1<F, P, S>,
811 FnParser<Input, fn(&mut Input) -> StdParseResult<F, Input>>,
812 > as Parser<Input>>::PartialState;
813
814 parse_mode!(Input);
815 #[inline]
816 fn parse_mode_impl<M>(
817 &mut self,
818 mode: M,
819 input: &mut Input,
820 state: &mut Self::PartialState,
821 ) -> ParseResult<Self::Output, Input::Error>
822 where
823 M: ParseMode,
824 {
825 sep_end_by1(&mut self.parser, &mut self.separator)
826 .or(parser(|_| Ok((F::default(), Commit::Peek(())))))
827 .parse_mode(mode, input, state)
828 }
829
830 fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
831 self.parser.add_error(errors)
832 }
833 }
834
835 /// Parses `parser` zero or more times separated and ended by `separator`, returning a collection
836 /// with the values from `p`.
837 ///
838 /// If the returned collection cannot be inferred type annotations must be supplied, either by
839 /// annotating the resulting type binding `let collection: Vec<_> = ...` or by specializing when
840 /// calling `sep_by`, `sep_by::<Vec<_>, _, _>(...)`
841 ///
842 /// ```
843 /// # extern crate combine;
844 /// # use combine::*;
845 /// # use combine::parser::char::digit;
846 /// # fn main() {
847 /// let mut parser = sep_end_by(digit(), token(';'));
848 /// let result_ok = parser.parse("1;2;3;");
849 /// assert_eq!(result_ok, Ok((vec!['1', '2', '3'], "")));
850 /// let result_ok2 = parser.parse("1;2;3");
851 /// assert_eq!(result_ok2, Ok((vec!['1', '2', '3'], "")));
852 /// # }
853 /// ```
854 pub fn sep_end_by<F, Input, P, S>(parser: P, separator: S) -> SepEndBy<F, P, S>
855 where
856 Input: Stream,
857 F: Extend<P::Output> + Default,
858 P: Parser<Input>,
859 S: Parser<Input>,
860 {
861 SepEndBy {
862 parser,
863 separator,
864 _marker: PhantomData,
865 }
866 }
867
868 #[derive(Copy, Clone)]
869 pub struct SepEndBy1<F, P, S> {
870 parser: P,
871 separator: S,
872 _marker: PhantomData<fn() -> F>,
873 }
874
875 impl<F, Input, P, S> Parser<Input> for SepEndBy1<F, P, S>
876 where
877 Input: Stream,
878 F: Extend<P::Output> + Default,
879 P: Parser<Input>,
880 S: Parser<Input>,
881 {
882 type Output = F;
883 type PartialState = (
884 Option<Commit<()>>,
885 F,
886 <With<S, Optional<P>> as Parser<Input>>::PartialState,
887 );
888
889 parse_mode!(Input);
890 #[inline]
891 fn parse_mode_impl<M>(
892 &mut self,
893 mode: M,
894 input: &mut Input,
895 state: &mut Self::PartialState,
896 ) -> ParseResult<Self::Output, Input::Error>
897 where
898 M: ParseMode,
899 {
900 let (ref mut parsed_one, ref mut elements, ref mut child_state) = *state;
901
902 let rest = match *parsed_one {
903 Some(rest) => rest,
904 None => {
905 let (first, rest) =
906 ctry!(self
907 .parser
908 .parse_mode(mode, input, &mut child_state.B.state));
909 *parsed_one = Some(rest);
910 elements.extend(Some(first));
911 rest
912 }
913 };
914
915 rest.combine_commit(|_| {
916 let rest = (&mut self.separator).with(optional(&mut self.parser));
917 let mut iter = Iter::new(rest, mode, input, child_state);
918
919 // Parse elements until `self.parser` returns `None`
920 elements.extend(iter.by_ref().scan((), |_, x| x));
921
922 if iter.committed {
923 *parsed_one = Some(Commit::Commit(()));
924 }
925
926 iter.into_result_fast(elements).map(|x| {
927 *parsed_one = None;
928 x
929 })
930 })
931 }
932
933 fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
934 self.parser.add_error(errors)
935 }
936 }
937
938 /// Parses `parser` one or more times separated and ended by `separator`, returning a collection
939 /// with the values from `p`.
940 ///
941 /// If the returned collection cannot be inferred type annotations must be
942 /// supplied, either by annotating the resulting type binding `let collection: Vec<_> = ...` or by
943 /// specializing when calling `sep_by`, `sep_by1::<Vec<_>, _, _>(...)`.
944 ///
945 /// ```
946 /// # extern crate combine;
947 /// # use combine::*;
948 /// # use combine::parser::char::digit;
949 /// # use combine::stream::easy;
950 /// # use combine::stream::position::{self, SourcePosition};
951 /// # fn main() {
952 /// let mut parser = sep_end_by1(digit(), token(';'));
953 /// let result_ok = parser.easy_parse(position::Stream::new("1;2;3;"))
954 /// .map(|(vec, state)| (vec, state.input));
955 /// assert_eq!(result_ok, Ok((vec!['1', '2', '3'], "")));
956 /// let result_err = parser.easy_parse(position::Stream::new(""));
957 /// assert_eq!(result_err, Err(easy::Errors {
958 /// position: SourcePosition::default(),
959 /// errors: vec![
960 /// easy::Error::end_of_input(),
961 /// easy::Error::Expected("digit".into())
962 /// ]
963 /// }));
964 /// # }
965 /// ```
966 pub fn sep_end_by1<F, Input, P, S>(parser: P, separator: S) -> SepEndBy1<F, P, S>
967 where
968 Input: Stream,
969 F: Extend<P::Output> + Default,
970 P: Parser<Input>,
971 S: Parser<Input>,
972 {
973 SepEndBy1 {
974 parser,
975 separator,
976 _marker: PhantomData,
977 }
978 }
979
980 #[derive(Copy, Clone)]
981 pub struct Chainl1<P, Op>(P, Op);
982 impl<Input, P, Op> Parser<Input> for Chainl1<P, Op>
983 where
984 Input: Stream,
985 P: Parser<Input>,
986 Op: Parser<Input>,
987 Op::Output: FnOnce(P::Output, P::Output) -> P::Output,
988 {
989 type Output = P::Output;
990 type PartialState = (
991 Option<(P::Output, Commit<()>)>,
992 <(Op, P) as Parser<Input>>::PartialState,
993 );
994
995 parse_mode!(Input);
996 #[inline]
997 fn parse_mode_impl<M>(
998 &mut self,
999 mut mode: M,
1000 input: &mut Input,
1001 state: &mut Self::PartialState,
1002 ) -> ParseResult<Self::Output, Input::Error>
1003 where
1004 M: ParseMode,
1005 {
1006 let (ref mut l_state, ref mut child_state) = *state;
1007
1008 let (mut l, mut committed) = match l_state.take() {
1009 Some(x) => x,
1010 None => {
1011 let x = ctry!(self.0.parse_partial(input, &mut child_state.B.state));
1012 mode.set_first();
1013 x
1014 }
1015 };
1016
1017 loop {
1018 let before = input.checkpoint();
1019 match (&mut self.1, &mut self.0)
1020 .parse_mode(mode, input, child_state)
1021 .into()
1022 {
1023 Ok(((op, r), rest)) => {
1024 l = op(l, r);
1025 committed = committed.merge(rest);
1026 mode.set_first();
1027 }
1028 Err(Commit::Commit(err)) => {
1029 *l_state = Some((l, committed));
1030 return CommitErr(err.error);
1031 }
1032 Err(Commit::Peek(_)) => {
1033 ctry!(input.reset(before).committed());
1034 break;
1035 }
1036 }
1037 }
1038 Ok((l, committed)).into()
1039 }
1040
1041 fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
1042 self.0.add_error(errors)
1043 }
1044 }
1045
1046 /// Parses `p` 1 or more times separated by `op`. The value returned is the one produced by the
1047 /// left associative application of the function returned by the parser `op`.
1048 ///
1049 /// ```
1050 /// # extern crate combine;
1051 /// # use combine::*;
1052 /// # use combine::parser::char::digit;
1053 /// # fn main() {
1054 /// let number = digit().map(|c: char| c.to_digit(10).unwrap());
1055 /// let sub = token('-').map(|_| |l: u32, r: u32| l - r);
1056 /// let mut parser = chainl1(number, sub);
1057 /// assert_eq!(parser.parse("9-3-5"), Ok((1, "")));
1058 /// # }
1059 /// ```
1060 pub fn chainl1<Input, P, Op>(parser: P, op: Op) -> Chainl1<P, Op>
1061 where
1062 Input: Stream,
1063 P: Parser<Input>,
1064 Op: Parser<Input>,
1065 Op::Output: FnOnce(P::Output, P::Output) -> P::Output,
1066 {
1067 Chainl1(parser, op)
1068 }
1069
1070 #[derive(Copy, Clone)]
1071 pub struct Chainr1<P, Op>(P, Op);
1072 impl<Input, P, Op> Parser<Input> for Chainr1<P, Op>
1073 where
1074 Input: Stream,
1075 P: Parser<Input>,
1076 Op: Parser<Input>,
1077 Op::Output: FnOnce(P::Output, P::Output) -> P::Output,
1078 {
1079 type Output = P::Output;
1080 type PartialState = ();
1081 #[inline]
1082 fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<P::Output, Input::Error> {
1083 // FIXME FastResult
1084 let (mut l, mut committed) = ctry!(self.0.parse_lazy(input));
1085 loop {
1086 let before = input.checkpoint();
1087 let op = match self.1.parse_lazy(input).into() {
1088 Ok((x, rest)) => {
1089 committed = committed.merge(rest);
1090 x
1091 }
1092 Err(Commit::Commit(err)) => return CommitErr(err.error),
1093 Err(Commit::Peek(_)) => {
1094 ctry!(input.reset(before).committed());
1095 break;
1096 }
1097 };
1098 let before = input.checkpoint();
1099 match self.parse_lazy(input).into() {
1100 Ok((r, rest)) => {
1101 l = op(l, r);
1102 committed = committed.merge(rest);
1103 }
1104 Err(Commit::Commit(err)) => return CommitErr(err.error),
1105 Err(Commit::Peek(_)) => {
1106 ctry!(input.reset(before).committed());
1107 break;
1108 }
1109 }
1110 }
1111 Ok((l, committed)).into()
1112 }
1113 fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
1114 self.0.add_error(errors)
1115 }
1116 }
1117
1118 /// Parses `p` one or more times separated by `op`. The value returned is the one produced by the
1119 /// right associative application of the function returned by `op`.
1120 ///
1121 /// ```
1122 /// # extern crate combine;
1123 /// # use combine::*;
1124 /// # use combine::parser::char::digit;
1125 /// # fn main() {
1126 /// let number = digit().map(|c: char| c.to_digit(10).unwrap());
1127 /// let pow = token('^').map(|_| |l: u32, r: u32| l.pow(r));
1128 /// let mut parser = chainr1(number, pow);
1129 /// assert_eq!(parser.parse("2^3^2"), Ok((512, "")));
1130 /// }
1131 /// ```
1132 pub fn chainr1<Input, P, Op>(parser: P, op: Op) -> Chainr1<P, Op>
1133 where
1134 Input: Stream,
1135 P: Parser<Input>,
1136 Op: Parser<Input>,
1137 Op::Output: FnOnce(P::Output, P::Output) -> P::Output,
1138 {
1139 Chainr1(parser, op)
1140 }
1141
1142 #[derive(Copy, Clone)]
1143 pub struct TakeUntil<F, P> {
1144 end: P,
1145 _marker: PhantomData<fn() -> F>,
1146 }
1147 impl<F, Input, P> Parser<Input> for TakeUntil<F, P>
1148 where
1149 Input: Stream,
1150 F: Extend<<Input as StreamOnce>::Token> + Default,
1151 P: Parser<Input>,
1152 {
1153 type Output = F;
1154 type PartialState = (F, P::PartialState);
1155
1156 parse_mode!(Input);
1157 #[inline]
1158 fn parse_mode_impl<M>(
1159 &mut self,
1160 mode: M,
1161 input: &mut Input,
1162 state: &mut Self::PartialState,
1163 ) -> ParseResult<Self::Output, Input::Error>
1164 where
1165 M: ParseMode,
1166 {
1167 let (ref mut output, ref mut end_state) = *state;
1168
1169 let mut committed = Commit::Peek(());
1170 loop {
1171 let before = input.checkpoint();
1172 match self.end.parse_mode(mode, input, end_state).into() {
1173 Ok((_, rest)) => {
1174 ctry!(input.reset(before).committed());
1175 return match committed.merge(rest) {
1176 Commit::Commit(()) => CommitOk(mem::take(output)),
1177 Commit::Peek(()) => PeekOk(mem::take(output)),
1178 };
1179 }
1180 Err(Commit::Peek(_)) => {
1181 ctry!(input.reset(before).committed());
1182 output.extend(Some(ctry!(uncons(input)).0));
1183 committed = Commit::Commit(());
1184 }
1185 Err(Commit::Commit(e)) => {
1186 ctry!(input.reset(before).committed());
1187 return CommitErr(e.error);
1188 }
1189 };
1190 }
1191 }
1192 }
1193
1194 /// Takes input until `end` is encountered or `end` indicates that it has committed input before
1195 /// failing (`attempt` can be used to make it look like it has not committed any input)
1196 ///
1197 /// ```
1198 /// # extern crate combine;
1199 /// # use combine::*;
1200 /// # use combine::parser::char;
1201 /// # use combine::parser::byte;
1202 /// # use combine::parser::combinator::attempt;
1203 /// # use combine::parser::repeat::take_until;
1204 /// # fn main() {
1205 /// let mut char_parser = take_until(char::digit());
1206 /// assert_eq!(char_parser.parse("abc123"), Ok(("abc".to_string(), "123")));
1207 ///
1208 /// let mut byte_parser = take_until(byte::bytes(&b"TAG"[..]));
1209 /// assert_eq!(byte_parser.parse(&b"123TAG"[..]), Ok((b"123".to_vec(), &b"TAG"[..])));
1210 /// assert!(byte_parser.parse(&b"123TATAG"[..]).is_err());
1211 ///
1212 /// // `attempt` must be used if the `end` should be consume input before failing
1213 /// let mut byte_parser = take_until(attempt(byte::bytes(&b"TAG"[..])));
1214 /// assert_eq!(byte_parser.parse(&b"123TATAG"[..]), Ok((b"123TA".to_vec(), &b"TAG"[..])));
1215 /// # }
1216 /// ```
1217 pub fn take_until<F, Input, P>(end: P) -> TakeUntil<F, P>
1218 where
1219 Input: Stream,
1220 F: Extend<<Input as StreamOnce>::Token> + Default,
1221 P: Parser<Input>,
1222 {
1223 TakeUntil {
1224 end,
1225 _marker: PhantomData,
1226 }
1227 }
1228
1229 parser! {
1230 pub struct SkipUntil;
1231 type PartialState = <With<TakeUntil<Sink, P>, Value<Input, ()>> as Parser<Input>>::PartialState;
1232 /// Skips input until `end` is encountered or `end` indicates that it has committed input before
1233 /// failing (`attempt` can be used to make it look like it has not committed any input)
1234 ///
1235 /// ```
1236 /// # extern crate combine;
1237 /// # use combine::*;
1238 /// # use combine::parser::char;
1239 /// # use combine::parser::byte;
1240 /// # use combine::parser::combinator::attempt;
1241 /// # use combine::parser::repeat::skip_until;
1242 /// # fn main() {
1243 /// let mut char_parser = skip_until(char::digit());
1244 /// assert_eq!(char_parser.parse("abc123"), Ok(((), "123")));
1245 ///
1246 /// let mut byte_parser = skip_until(byte::bytes(&b"TAG"[..]));
1247 /// assert_eq!(byte_parser.parse(&b"123TAG"[..]), Ok(((), &b"TAG"[..])));
1248 /// assert!(byte_parser.parse(&b"123TATAG"[..]).is_err());
1249 ///
1250 /// // `attempt` must be used if the `end` should consume input before failing
1251 /// let mut byte_parser = skip_until(attempt(byte::bytes(&b"TAG"[..])));
1252 /// assert_eq!(byte_parser.parse(&b"123TATAG"[..]), Ok(((), &b"TAG"[..])));
1253 /// # }
1254 /// ```
1255 pub fn skip_until[Input, P](end: P)(Input) -> ()
1256 where [
1257 P: Parser<Input>,
1258 ]
1259 {
1260 take_until::<Sink, _, _>(end).with(value(()))
1261 }
1262 }
1263
1264 #[derive(Copy, Clone)]
1265 pub struct RepeatUntil<F, P, E> {
1266 parser: P,
1267 end: E,
1268 _marker: PhantomData<fn() -> F>,
1269 }
1270 impl<F, Input, P, E> Parser<Input> for RepeatUntil<F, P, E>
1271 where
1272 Input: Stream,
1273 F: Extend<P::Output> + Default,
1274 P: Parser<Input>,
1275 E: Parser<Input>,
1276 {
1277 type Output = F;
1278 type PartialState = (F, bool, P::PartialState, E::PartialState);
1279
1280 parse_mode!(Input);
1281 #[inline]
1282 fn parse_mode_impl<M>(
1283 &mut self,
1284 mut mode: M,
1285 input: &mut Input,
1286 state: &mut Self::PartialState,
1287 ) -> ParseResult<Self::Output, Input::Error>
1288 where
1289 M: ParseMode,
1290 {
1291 let (output, is_parse, parse_state, end_state) = state;
1292
1293 let mut committed = Commit::Peek(());
1294 loop {
1295 if *is_parse {
1296 let (token, c) = ctry!(self.parser.parse_mode(mode, input, parse_state));
1297 output.extend(Some(token));
1298 committed = committed.merge(c);
1299 *is_parse = false;
1300 } else {
1301 let before = input.checkpoint();
1302 match self.end.parse_mode(mode, input, end_state).into() {
1303 Ok((_, rest)) => {
1304 ctry!(input.reset(before).committed());
1305 return match committed.merge(rest) {
1306 Commit::Commit(()) => CommitOk(mem::take(output)),
1307 Commit::Peek(()) => PeekOk(mem::take(output)),
1308 };
1309 }
1310 Err(Commit::Peek(_)) => {
1311 ctry!(input.reset(before).committed());
1312 mode.set_first();
1313 *is_parse = true;
1314 }
1315 Err(Commit::Commit(e)) => {
1316 ctry!(input.reset(before).committed());
1317 return CommitErr(e.error);
1318 }
1319 }
1320 }
1321 }
1322 }
1323 }
1324
1325 pub fn repeat_until<F, Input, P, E>(parser: P, end: E) -> RepeatUntil<F, P, E>
1326 where
1327 Input: Stream,
1328 F: Extend<P::Output> + Default,
1329 P: Parser<Input>,
1330 E: Parser<Input>,
1331 {
1332 RepeatUntil {
1333 parser,
1334 end,
1335 _marker: PhantomData,
1336 }
1337 }
1338
1339 parser! {
1340 pub struct SkipRepeatUntil;
1341 type PartialState = <With<RepeatUntil<Sink, P, E>, Value<Input, ()>> as Parser<Input>>::PartialState;
1342 /// Skips input until `end` is encountered or `end` indicates that it has committed input before
1343 /// failing (`attempt` can be used to continue skipping even if `end` has committed input)
1344 ///
1345 /// ```
1346 /// # extern crate combine;
1347 /// # use combine::*;
1348 /// # use combine::parser::char;
1349 /// # use combine::parser::byte;
1350 /// # use combine::parser::combinator::attempt;
1351 /// # use combine::parser::repeat::skip_until;
1352 /// # fn main() {
1353 /// let mut char_parser = skip_until(char::digit());
1354 /// assert_eq!(char_parser.parse("abc123"), Ok(((), "123")));
1355 ///
1356 /// let mut byte_parser = skip_until(byte::bytes(&b"TAG"[..]));
1357 /// assert_eq!(byte_parser.parse(&b"123TAG"[..]), Ok(((), &b"TAG"[..])));
1358 /// assert!(byte_parser.parse(&b"123TATAG"[..]).is_err());
1359 ///
1360 /// // `attempt` must be used because the `end` will commit to `TA` before failing,
1361 /// // but we want to continue skipping
1362 /// let mut byte_parser = skip_until(attempt(byte::bytes(&b"TAG"[..])));
1363 /// assert_eq!(byte_parser.parse(&b"123TATAG"[..]), Ok(((), &b"TAG"[..])));
1364 /// }
1365 /// ```
1366 pub fn repeat_skip_until[Input, P, E](parser: P, end: E)(Input) -> ()
1367 where [
1368 P: Parser<Input>,
1369 E: Parser<Input>,
1370 ]
1371 {
1372 repeat_until::<Sink, _, _, _>(parser, end).with(value(()))
1373 }
1374 }
1375
1376 #[derive(Default)]
1377 pub struct EscapedState<T, U>(PhantomData<(T, U)>);
1378
1379 pub struct Escaped<P, Q, I> {
1380 parser: P,
1381 escape: I,
1382 escape_parser: Q,
1383 }
1384 impl<Input, P, Q> Parser<Input> for Escaped<P, Q, Input::Token>
1385 where
1386 Input: Stream,
1387 P: Parser<Input>,
1388 <Input as StreamOnce>::Token: PartialEq,
1389 Q: Parser<Input>,
1390 {
1391 type Output = ();
1392 type PartialState = EscapedState<P::PartialState, Q::PartialState>;
1393
1394 fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<Self::Output, Input::Error> {
1395 let mut committed = Commit::Peek(());
1396 loop {
1397 match self.parser.parse_lazy(input) {
1398 PeekOk(_) => {}
1399 CommitOk(_) => {
1400 committed = Commit::Commit(());
1401 }
1402 PeekErr(_) => {
1403 let checkpoint = input.checkpoint();
1404 match uncons(input) {
1405 CommitOk(ref c) | PeekOk(ref c) if *c == self.escape => {
1406 match self.escape_parser.parse_committed_mode(
1407 FirstMode,
1408 input,
1409 &mut Default::default(),
1410 ) {
1411 PeekOk(_) => {}
1412 CommitOk(_) => {
1413 committed = Commit::Commit(());
1414 }
1415 CommitErr(err) => return CommitErr(err),
1416 PeekErr(err) => {
1417 return CommitErr(err.error);
1418 }
1419 }
1420 }
1421 CommitErr(err) => {
1422 return CommitErr(err);
1423 }
1424 _ => {
1425 ctry!(input.reset(checkpoint).committed());
1426 return if committed.is_peek() {
1427 PeekOk(())
1428 } else {
1429 CommitOk(())
1430 };
1431 }
1432 }
1433 }
1434 CommitErr(err) => return CommitErr(err),
1435 }
1436 }
1437 }
1438
1439 fn add_error(&mut self, errors: &mut Tracked<<Input as StreamOnce>::Error>) {
1440 use crate::error;
1441
1442 self.parser.add_error(errors);
1443
1444 errors.error.add_expected(error::Token(self.escape.clone()));
1445 }
1446 }
1447
1448 /// Parses an escaped string by first applying `parser` which accept the normal characters which do
1449 /// not need escaping. Once `parser` can not consume any more input it checks if the next token
1450 /// is `escape`. If it is then `escape_parser` is used to parse the escaped character and then
1451 /// resumes parsing using `parser`. If `escape` was not found then the parser finishes
1452 /// successfully.
1453 ///
1454 /// This returns `()` since there isn't a good way to collect the output of the parsers so it is
1455 /// best paired with one of the `recognize` parsers.
1456 ///
1457 /// ```
1458 /// # extern crate combine;
1459 /// # use combine::*;
1460 /// # use combine::parser::repeat::escaped;
1461 /// # use combine::parser::char;
1462 /// # use combine::parser::range::{recognize, take_while1};
1463 /// # fn main() {
1464 /// let mut parser = recognize(
1465 /// escaped(take_while1(|c| c != '"' && c != '\\'), '\\', one_of(r#"nr"\"#.chars()))
1466 /// );
1467 /// assert_eq!(parser.parse(r#"ab\"12\n\rc""#), Ok((r#"ab\"12\n\rc"#, r#"""#)));
1468 /// assert!(parser.parse(r#"\"#).is_err());
1469 /// assert!(parser.parse(r#"\a"#).is_err());
1470 /// }
1471 /// ```
1472 pub fn escaped<Input, P, Q>(
1473 parser: P,
1474 escape: <Input as StreamOnce>::Token,
1475 escape_parser: Q,
1476 ) -> Escaped<P, Q, Input::Token>
1477 where
1478 Input: Stream,
1479 P: Parser<Input>,
1480 <Input as StreamOnce>::Token: PartialEq,
1481 Q: Parser<Input>,
1482 {
1483 Escaped {
1484 parser,
1485 escape,
1486 escape_parser,
1487 }
1488 }
1489
1490 pub struct Iterate<F, I, P> {
1491 parser: P,
1492 iterable: I,
1493 _marker: PhantomData<fn() -> F>,
1494 }
1495 impl<'s, 'a, P, Q, I, J, F> Parser<I> for Iterate<F, J, P>
1496 where
1497 P: FnMut(&J::Item, &mut I) -> Q,
1498 Q: Parser<I>,
1499 I: Stream,
1500 J: IntoIterator + Clone,
1501 F: Extend<Q::Output> + Default,
1502 {
1503 type Output = F;
1504 type PartialState = (
1505 Option<(J::IntoIter, Option<J::Item>)>,
1506 bool,
1507 F,
1508 Q::PartialState,
1509 );
1510
1511 parse_mode!(I);
1512
1513 fn parse_mode_impl<M>(
1514 &mut self,
1515 mut mode: M,
1516 input: &mut I,
1517 state: &mut Self::PartialState,
1518 ) -> ParseResult<Self::Output, I::Error>
1519 where
1520 M: ParseMode,
1521 {
1522 let (opt_iter, committed, buf, next) = state;
1523 let (iter, next_item) = match opt_iter {
1524 Some(iter) if !mode.is_first() => iter,
1525 _ => {
1526 *opt_iter = Some((self.iterable.clone().into_iter(), None));
1527 opt_iter.as_mut().unwrap()
1528 }
1529 };
1530
1531 let mut consume = |item: J::Item| {
1532 let mut parser = (self.parser)(&item, input);
1533 let before = input.checkpoint();
1534 match parser.parse_mode(mode, input, next) {
1535 PeekOk(v) => {
1536 mode.set_first();
1537 Ok(v)
1538 }
1539 CommitOk(v) => {
1540 mode.set_first();
1541 *committed = true;
1542 Ok(v)
1543 }
1544 PeekErr(err) => {
1545 if let Err(err) = input.reset(before) {
1546 return Err((item, CommitErr(err)));
1547 }
1548 Err((
1549 item,
1550 if *committed {
1551 CommitErr(err.error)
1552 } else {
1553 PeekErr(err)
1554 },
1555 ))
1556 }
1557 CommitErr(err) => Err((item, CommitErr(err))),
1558 }
1559 };
1560
1561 let result = (|| {
1562 if let Some(item) = next_item.take() {
1563 buf.extend(Some(consume(item)?));
1564 }
1565 let mut result = Ok(());
1566 let size_hint = iter.size_hint();
1567 buf.extend(suggest_size_hint(
1568 iter.scan((), |_, item| match consume(item) {
1569 Ok(item) => Some(item),
1570 Err(err) => {
1571 result = Err(err);
1572 None
1573 }
1574 }),
1575 size_hint,
1576 ));
1577 result
1578 })();
1579
1580 if let Err((item, err)) = result {
1581 *next_item = Some(item);
1582 return err;
1583 }
1584
1585 opt_iter.take();
1586
1587 let value = mem::take(buf);
1588 if *committed {
1589 *committed = false;
1590 CommitOk(value)
1591 } else {
1592 PeekOk(value)
1593 }
1594 }
1595 }
1596
1597 ///
1598 /// ```
1599 /// # use combine::parser::repeat::{count_min_max, iterate};
1600 /// # use combine::*;
1601 ///
1602 /// assert_eq!(
1603 /// iterate(0..3, |&i, _| count_min_max(i, i, any())).parse("abbccc"),
1604 /// Ok((vec!["".to_string(), "a".to_string(), "bb".to_string()], "ccc")),
1605 /// );
1606 /// ```
1607 pub fn iterate<F, J, P, I, Q>(iterable: J, parser: P) -> Iterate<F, J, P>
1608 where
1609 P: FnMut(&J::Item, &mut I) -> Q,
1610 Q: Parser<I>,
1611 I: Stream,
1612 J: IntoIterator + Clone,
1613 F: Extend<Q::Output> + Default,
1614 {
1615 Iterate {
1616 parser,
1617 iterable,
1618 _marker: PhantomData,
1619 }
1620 }
1621