1use 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
22parser! {
23pub struct Count;
24
25pub fn count[F, Input, P](count: usize, parser: P)(Input) -> F
40where [
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
50parser! {
51 pub struct SkipCount;
52 type PartialState = <With<Count<Sink, Input, P>, Value<Input, ()>> as Parser<Input>>::PartialState;
53 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)]
76pub struct CountMinMax<F, P> {
77 parser: P,
78 min: usize,
79 max: usize,
80 _marker: PhantomData<fn() -> F>,
81}
82
83struct SuggestSizeHint<I> {
84 iterator: I,
85 min: usize,
86 max: Option<usize>,
87}
88
89impl<I> Iterator for SuggestSizeHint<I>
90where
91 I: Iterator,
92{
93 type Item = I::Item;
94
95 #[inline]
96 fn next(&mut self) -> Option<Self::Item> {
97 self.iterator.next()
98 }
99
100 #[inline]
101 fn size_hint(&self) -> (usize, Option<usize>) {
102 (self.min, self.max)
103 }
104}
105
106fn suggest_size_hint<I>(iterator: I, (min, max): (usize, Option<usize>)) -> SuggestSizeHint<I>
107where
108 I: Iterator,
109{
110 SuggestSizeHint {
111 iterator,
112 min: cmp::min(min, 4096),
115 max,
116 }
117}
118
119impl<Input, P, F> Parser<Input> for CountMinMax<F, P>
120where
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]
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
162 fn add_error(&mut self, error: &mut Tracked<<Input as StreamOnce>::Error>) {
163 self.parser.add_error(error)
164 }
165}
166
167pub fn count_min_max<F, Input, P>(min: usize, max: usize, parser: P) -> CountMinMax<F, P>
187where
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
202parser! {
203 pub struct SkipCountMinMax;
204 type PartialState = <With<CountMinMax<Sink, P>, Value<Input, ()>> as Parser<Input>>::PartialState;
205 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
233pub struct Iter<'a, Input, P, S, M>
234where
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
246enum State<E> {
247 Ok,
248 PeekErr(E),
249 CommitErr(E),
250}
251
252impl<'a, Input, P, S, M> Iter<'a, Input, P, S, M>
253where
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 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
338impl<'a, Input, P, S, M> Iterator for Iter<'a, Input, P, S, M>
339where
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)]
378pub struct Many<F, P>(P, PhantomData<F>);
379
380impl<F, Input, P> Parser<Input> for Many<F, P>
381where
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 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
421pub fn many<F, Input, P>(p: P) -> Many<F, P>
442where
443 Input: Stream,
444 P: Parser<Input>,
445 F: Extend<P::Output> + Default,
446{
447 Many(p, PhantomData)
448}
449
450#[derive(Copy, Clone)]
451pub struct Many1<F, P>(P, PhantomData<fn() -> F>);
452impl<F, Input, P> Parser<Input> for Many1<F, P>
453where
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 *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
509pub fn many1<F, Input, P>(p: P) -> Many1<F, P>
530where
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)]
540pub struct Sink;
542
543impl Default for Sink {
544 fn default() -> Self {
545 Sink
546 }
547}
548
549impl<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
558parser! {
559 pub struct SkipMany;
560 type PartialState = <Ignore<Many<Sink, Ignore<P>>> as Parser<Input>>::PartialState;
561pub fn skip_many[Input, P](p: P)(Input) -> ()
577where [
578 P: Parser<Input>,
579]
580{
581 ignore(many::<Sink, _, _>(ignore(p)))
582}
583}
584
585parser! {
586 pub struct SkipMany1;
587 type PartialState = <Ignore<Many1<Sink, Ignore<P>>> as Parser<Input>>::PartialState;
588pub fn skip_many1[Input, P](p: P)(Input) -> ()
604where [
605 P: Parser<Input>,
606]
607{
608 ignore(many1::<Sink, _, _>(ignore(p)))
609}
610}
611
612#[derive(Copy, Clone)]
613pub struct SepBy<F, P, S> {
614 parser: P,
615 separator: S,
616 _marker: PhantomData<fn() -> F>,
617}
618impl<F, Input, P, S> Parser<Input> for SepBy<F, P, S>
619where
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
654pub fn sep_by<F, Input, P, S>(parser: P, separator: S) -> SepBy<F, P, S>
674where
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)]
688pub struct SepBy1<F, P, S> {
689 parser: P,
690 separator: S,
691 _marker: PhantomData<fn() -> F>,
692}
693impl<F, Input, P, S> Parser<Input> for SepBy1<F, P, S>
694where
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
752pub fn sep_by1<F, Input, P, S>(parser: P, separator: S) -> SepBy1<F, P, S>
781where
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)]
795pub struct SepEndBy<F, P, S> {
796 parser: P,
797 separator: S,
798 _marker: PhantomData<fn() -> F>,
799}
800
801impl<F, Input, P, S> Parser<Input> for SepEndBy<F, P, S>
802where
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
835pub fn sep_end_by<F, Input, P, S>(parser: P, separator: S) -> SepEndBy<F, P, S>
855where
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)]
869pub struct SepEndBy1<F, P, S> {
870 parser: P,
871 separator: S,
872 _marker: PhantomData<fn() -> F>,
873}
874
875impl<F, Input, P, S> Parser<Input> for SepEndBy1<F, P, S>
876where
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 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
938pub fn sep_end_by1<F, Input, P, S>(parser: P, separator: S) -> SepEndBy1<F, P, S>
967where
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)]
981pub struct Chainl1<P, Op>(P, Op);
982impl<Input, P, Op> Parser<Input> for Chainl1<P, Op>
983where
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
1046pub fn chainl1<Input, P, Op>(parser: P, op: Op) -> Chainl1<P, Op>
1061where
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)]
1071pub struct Chainr1<P, Op>(P, Op);
1072impl<Input, P, Op> Parser<Input> for Chainr1<P, Op>
1073where
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 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
1118pub fn chainr1<Input, P, Op>(parser: P, op: Op) -> Chainr1<P, Op>
1133where
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)]
1143pub struct TakeUntil<F, P> {
1144 end: P,
1145 _marker: PhantomData<fn() -> F>,
1146}
1147impl<F, Input, P> Parser<Input> for TakeUntil<F, P>
1148where
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
1194pub fn take_until<F, Input, P>(end: P) -> TakeUntil<F, P>
1218where
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
1229parser! {
1230 pub struct SkipUntil;
1231 type PartialState = <With<TakeUntil<Sink, P>, Value<Input, ()>> as Parser<Input>>::PartialState;
1232 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)]
1265pub struct RepeatUntil<F, P, E> {
1266 parser: P,
1267 end: E,
1268 _marker: PhantomData<fn() -> F>,
1269}
1270impl<F, Input, P, E> Parser<Input> for RepeatUntil<F, P, E>
1271where
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
1325pub fn repeat_until<F, Input, P, E>(parser: P, end: E) -> RepeatUntil<F, P, E>
1326where
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
1339parser! {
1340 pub struct SkipRepeatUntil;
1341 type PartialState = <With<RepeatUntil<Sink, P, E>, Value<Input, ()>> as Parser<Input>>::PartialState;
1342 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)]
1377pub struct EscapedState<T, U>(PhantomData<(T, U)>);
1378
1379pub struct Escaped<P, Q, I> {
1380 parser: P,
1381 escape: I,
1382 escape_parser: Q,
1383}
1384impl<Input, P, Q> Parser<Input> for Escaped<P, Q, Input::Token>
1385where
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
1448pub fn escaped<Input, P, Q>(
1473 parser: P,
1474 escape: <Input as StreamOnce>::Token,
1475 escape_parser: Q,
1476) -> Escaped<P, Q, Input::Token>
1477where
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
1490pub struct Iterate<F, I, P> {
1491 parser: P,
1492 iterable: I,
1493 _marker: PhantomData<fn() -> F>,
1494}
1495impl<'s, 'a, P, Q, I, J, F> Parser<I> for Iterate<F, J, P>
1496where
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
1597pub fn iterate<F, J, P, I, Q>(iterable: J, parser: P) -> Iterate<F, J, P>
1608where
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}