nom8/multi/
mod.rs

1//! Combinators applying their child parser multiple times
2
3#[cfg(test)]
4mod tests;
5
6use crate::error::ErrorKind;
7use crate::error::ParseError;
8use crate::input::{InputIsStreaming, InputIter, InputLength, InputTake, IntoOutput, ToUsize};
9#[cfg(feature = "alloc")]
10use crate::lib::std::vec::Vec;
11use crate::{Err, IResult, Parser};
12
13/// Don't pre-allocate more than 64KiB when calling `Vec::with_capacity`.
14///
15/// Pre-allocating memory is a nice optimization but count fields can't
16/// always be trusted. We should clamp initial capacities to some reasonable
17/// amount. This reduces the risk of a bogus count value triggering a panic
18/// due to an OOM error.
19///
20/// This does not affect correctness. Nom will always read the full number
21/// of elements regardless of the capacity cap.
22const MAX_INITIAL_CAPACITY: usize = 65536;
23
24/// Repeats the embedded parser, gathering the results in a `Vec`.
25///
26/// This stops on [`Err::Error`].  To instead chain an error up, see
27/// [`cut`][crate::combinator::cut].
28///
29/// # Arguments
30/// * `f` The parser to apply.
31///
32/// *Note*: if the parser passed in accepts empty inputs (like `alpha0` or `digit0`), `many0` will
33/// return an error, to prevent going into an infinite loop
34///
35/// ```rust
36/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
37/// use nom8::multi::many0;
38/// use nom8::bytes::tag;
39///
40/// fn parser(s: &str) -> IResult<&str, Vec<&str>> {
41///   many0(tag("abc"))(s)
42/// }
43///
44/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"])));
45/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"])));
46/// assert_eq!(parser("123123"), Ok(("123123", vec![])));
47/// assert_eq!(parser(""), Ok(("", vec![])));
48/// ```
49#[cfg(feature = "alloc")]
50pub fn many0<I, O, E, F>(mut f: F) -> impl FnMut(I) -> IResult<I, Vec<O>, E>
51where
52  I: Clone + InputLength,
53  F: Parser<I, O, E>,
54  E: ParseError<I>,
55{
56  move |mut i: I| {
57    let mut acc = crate::lib::std::vec::Vec::with_capacity(4);
58    loop {
59      let len = i.input_len();
60      match f.parse(i.clone()) {
61        Err(Err::Error(_)) => return Ok((i, acc)),
62        Err(e) => return Err(e),
63        Ok((i1, o)) => {
64          // infinite loop check: the parser must always consume
65          if i1.input_len() == len {
66            return Err(Err::Error(E::from_error_kind(i, ErrorKind::Many0)));
67          }
68
69          i = i1;
70          acc.push(o);
71        }
72      }
73    }
74  }
75}
76
77/// Runs the embedded parser, gathering the results in a `Vec`.
78///
79/// This stops on [`Err::Error`] if there is at least one result.  To instead chain an error up,
80/// see [`cut`][crate::combinator::cut].
81///
82/// # Arguments
83/// * `f` The parser to apply.
84///
85/// *Note*: If the parser passed to `many1` accepts empty inputs
86/// (like `alpha0` or `digit0`), `many1` will return an error,
87/// to prevent going into an infinite loop.
88///
89/// ```rust
90/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
91/// use nom8::multi::many1;
92/// use nom8::bytes::tag;
93///
94/// fn parser(s: &str) -> IResult<&str, Vec<&str>> {
95///   many1(tag("abc"))(s)
96/// }
97///
98/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"])));
99/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"])));
100/// assert_eq!(parser("123123"), Err(Err::Error(Error::new("123123", ErrorKind::Tag))));
101/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Tag))));
102/// ```
103#[cfg(feature = "alloc")]
104pub fn many1<I, O, E, F>(mut f: F) -> impl FnMut(I) -> IResult<I, Vec<O>, E>
105where
106  I: Clone + InputLength,
107  F: Parser<I, O, E>,
108  E: ParseError<I>,
109{
110  move |mut i: I| match f.parse(i.clone()) {
111    Err(Err::Error(err)) => Err(Err::Error(E::append(i, ErrorKind::Many1, err))),
112    Err(e) => Err(e),
113    Ok((i1, o)) => {
114      let mut acc = crate::lib::std::vec::Vec::with_capacity(4);
115      acc.push(o);
116      i = i1;
117
118      loop {
119        let len = i.input_len();
120        match f.parse(i.clone()) {
121          Err(Err::Error(_)) => return Ok((i, acc)),
122          Err(e) => return Err(e),
123          Ok((i1, o)) => {
124            // infinite loop check: the parser must always consume
125            if i1.input_len() == len {
126              return Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1)));
127            }
128
129            i = i1;
130            acc.push(o);
131          }
132        }
133      }
134    }
135  }
136}
137
138/// Applies the parser `f` until the parser `g` produces a result.
139///
140/// Returns a tuple of the results of `f` in a `Vec` and the result of `g`.
141///
142/// `f` keeps going so long as `g` produces [`Err::Error`]. To instead chain an error up, see [`cut`][crate::combinator::cut].
143///
144/// ```rust
145/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
146/// use nom8::multi::many_till;
147/// use nom8::bytes::tag;
148///
149/// fn parser(s: &str) -> IResult<&str, (Vec<&str>, &str)> {
150///   many_till(tag("abc"), tag("end"))(s)
151/// };
152///
153/// assert_eq!(parser("abcabcend"), Ok(("", (vec!["abc", "abc"], "end"))));
154/// assert_eq!(parser("abc123end"), Err(Err::Error(Error::new("123end", ErrorKind::Tag))));
155/// assert_eq!(parser("123123end"), Err(Err::Error(Error::new("123123end", ErrorKind::Tag))));
156/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Tag))));
157/// assert_eq!(parser("abcendefg"), Ok(("efg", (vec!["abc"], "end"))));
158/// ```
159#[cfg(feature = "alloc")]
160pub fn many_till<I, O, P, E, F, G>(
161  mut f: F,
162  mut g: G,
163) -> impl FnMut(I) -> IResult<I, (Vec<O>, P), E>
164where
165  I: Clone + InputLength,
166  F: Parser<I, O, E>,
167  G: Parser<I, P, E>,
168  E: ParseError<I>,
169{
170  move |mut i: I| {
171    let mut res = crate::lib::std::vec::Vec::new();
172    loop {
173      let len = i.input_len();
174      match g.parse(i.clone()) {
175        Ok((i1, o)) => return Ok((i1, (res, o))),
176        Err(Err::Error(_)) => {
177          match f.parse(i.clone()) {
178            Err(Err::Error(err)) => return Err(Err::Error(E::append(i, ErrorKind::ManyTill, err))),
179            Err(e) => return Err(e),
180            Ok((i1, o)) => {
181              // infinite loop check: the parser must always consume
182              if i1.input_len() == len {
183                return Err(Err::Error(E::from_error_kind(i1, ErrorKind::ManyTill)));
184              }
185
186              res.push(o);
187              i = i1;
188            }
189          }
190        }
191        Err(e) => return Err(e),
192      }
193    }
194  }
195}
196
197/// Alternates between two parsers to produce a list of elements.
198///
199/// This stops when either parser returns [`Err::Error`].  To instead chain an error up, see
200/// [`cut`][crate::combinator::cut].
201///
202/// # Arguments
203/// * `sep` Parses the separator between list elements.
204/// * `f` Parses the elements of the list.
205///
206/// ```rust
207/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
208/// use nom8::multi::separated_list0;
209/// use nom8::bytes::tag;
210///
211/// fn parser(s: &str) -> IResult<&str, Vec<&str>> {
212///   separated_list0(tag("|"), tag("abc"))(s)
213/// }
214///
215/// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"])));
216/// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"])));
217/// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"])));
218/// assert_eq!(parser(""), Ok(("", vec![])));
219/// assert_eq!(parser("def|abc"), Ok(("def|abc", vec![])));
220/// ```
221#[cfg(feature = "alloc")]
222pub fn separated_list0<I, O, O2, E, F, G>(
223  mut sep: G,
224  mut f: F,
225) -> impl FnMut(I) -> IResult<I, Vec<O>, E>
226where
227  I: Clone + InputLength,
228  F: Parser<I, O, E>,
229  G: Parser<I, O2, E>,
230  E: ParseError<I>,
231{
232  move |mut i: I| {
233    let mut res = Vec::new();
234
235    match f.parse(i.clone()) {
236      Err(Err::Error(_)) => return Ok((i, res)),
237      Err(e) => return Err(e),
238      Ok((i1, o)) => {
239        res.push(o);
240        i = i1;
241      }
242    }
243
244    loop {
245      let len = i.input_len();
246      match sep.parse(i.clone()) {
247        Err(Err::Error(_)) => return Ok((i, res)),
248        Err(e) => return Err(e),
249        Ok((i1, _)) => {
250          // infinite loop check: the parser must always consume
251          if i1.input_len() == len {
252            return Err(Err::Error(E::from_error_kind(i1, ErrorKind::SeparatedList)));
253          }
254
255          match f.parse(i1.clone()) {
256            Err(Err::Error(_)) => return Ok((i, res)),
257            Err(e) => return Err(e),
258            Ok((i2, o)) => {
259              res.push(o);
260              i = i2;
261            }
262          }
263        }
264      }
265    }
266  }
267}
268
269/// Alternates between two parsers to produce a list of elements until [`Err::Error`].
270///
271/// Fails if the element parser does not produce at least one element.$
272///
273/// This stops when either parser returns [`Err::Error`].  To instead chain an error up, see
274/// [`cut`][crate::combinator::cut].
275///
276/// # Arguments
277/// * `sep` Parses the separator between list elements.
278/// * `f` Parses the elements of the list.
279/// ```rust
280/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
281/// use nom8::multi::separated_list1;
282/// use nom8::bytes::tag;
283///
284/// fn parser(s: &str) -> IResult<&str, Vec<&str>> {
285///   separated_list1(tag("|"), tag("abc"))(s)
286/// }
287///
288/// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"])));
289/// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"])));
290/// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"])));
291/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Tag))));
292/// assert_eq!(parser("def|abc"), Err(Err::Error(Error::new("def|abc", ErrorKind::Tag))));
293/// ```
294#[cfg(feature = "alloc")]
295pub fn separated_list1<I, O, O2, E, F, G>(
296  mut sep: G,
297  mut f: F,
298) -> impl FnMut(I) -> IResult<I, Vec<O>, E>
299where
300  I: Clone + InputLength,
301  F: Parser<I, O, E>,
302  G: Parser<I, O2, E>,
303  E: ParseError<I>,
304{
305  move |mut i: I| {
306    let mut res = Vec::new();
307
308    // Parse the first element
309    match f.parse(i.clone()) {
310      Err(e) => return Err(e),
311      Ok((i1, o)) => {
312        res.push(o);
313        i = i1;
314      }
315    }
316
317    loop {
318      let len = i.input_len();
319      match sep.parse(i.clone()) {
320        Err(Err::Error(_)) => return Ok((i, res)),
321        Err(e) => return Err(e),
322        Ok((i1, _)) => {
323          // infinite loop check: the parser must always consume
324          if i1.input_len() == len {
325            return Err(Err::Error(E::from_error_kind(i1, ErrorKind::SeparatedList)));
326          }
327
328          match f.parse(i1.clone()) {
329            Err(Err::Error(_)) => return Ok((i, res)),
330            Err(e) => return Err(e),
331            Ok((i2, o)) => {
332              res.push(o);
333              i = i2;
334            }
335          }
336        }
337      }
338    }
339  }
340}
341
342/// Repeats the embedded parser `m..=n` times
343///
344/// This stops before `n` when the parser returns [`Err::Error`].  To instead chain an error up, see
345/// [`cut`][crate::combinator::cut].
346///
347/// # Arguments
348/// * `m` The minimum number of iterations.
349/// * `n` The maximum number of iterations.
350/// * `f` The parser to apply.
351///
352/// *Note*: If the parser passed to `many1` accepts empty inputs
353/// (like `alpha0` or `digit0`), `many1` will return an error,
354/// to prevent going into an infinite loop.
355///
356/// ```rust
357/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
358/// use nom8::multi::many_m_n;
359/// use nom8::bytes::tag;
360///
361/// fn parser(s: &str) -> IResult<&str, Vec<&str>> {
362///   many_m_n(0, 2, tag("abc"))(s)
363/// }
364///
365/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"])));
366/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"])));
367/// assert_eq!(parser("123123"), Ok(("123123", vec![])));
368/// assert_eq!(parser(""), Ok(("", vec![])));
369/// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"])));
370/// ```
371#[cfg(feature = "alloc")]
372pub fn many_m_n<I, O, E, F>(
373  min: usize,
374  max: usize,
375  mut parse: F,
376) -> impl FnMut(I) -> IResult<I, Vec<O>, E>
377where
378  I: Clone + InputLength,
379  F: Parser<I, O, E>,
380  E: ParseError<I>,
381{
382  move |mut input: I| {
383    if min > max {
384      return Err(Err::Failure(E::from_error_kind(input, ErrorKind::ManyMN)));
385    }
386
387    let mut res = crate::lib::std::vec::Vec::with_capacity(min.clamp(0, MAX_INITIAL_CAPACITY));
388    for count in 0..max {
389      let len = input.input_len();
390      match parse.parse(input.clone()) {
391        Ok((tail, value)) => {
392          // infinite loop check: the parser must always consume
393          if tail.input_len() == len {
394            return Err(Err::Error(E::from_error_kind(input, ErrorKind::ManyMN)));
395          }
396
397          res.push(value);
398          input = tail;
399        }
400        Err(Err::Error(e)) => {
401          if count < min {
402            return Err(Err::Error(E::append(input, ErrorKind::ManyMN, e)));
403          } else {
404            return Ok((input, res));
405          }
406        }
407        Err(e) => {
408          return Err(e);
409        }
410      }
411    }
412
413    Ok((input, res))
414  }
415}
416
417/// Repeats the embedded parser, counting the results
418///
419/// This stops on [`Err::Error`].  To instead chain an error up, see
420/// [`cut`][crate::combinator::cut].
421///
422/// # Arguments
423/// * `f` The parser to apply.
424///
425/// *Note*: if the parser passed in accepts empty inputs (like `alpha0` or `digit0`), `many0` will
426/// return an error, to prevent going into an infinite loop
427///
428/// ```rust
429/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
430/// use nom8::multi::many0_count;
431/// use nom8::bytes::tag;
432///
433/// fn parser(s: &str) -> IResult<&str, usize> {
434///   many0_count(tag("abc"))(s)
435/// }
436///
437/// assert_eq!(parser("abcabc"), Ok(("", 2)));
438/// assert_eq!(parser("abc123"), Ok(("123", 1)));
439/// assert_eq!(parser("123123"), Ok(("123123", 0)));
440/// assert_eq!(parser(""), Ok(("", 0)));
441/// ```
442pub fn many0_count<I, O, E, F>(mut f: F) -> impl FnMut(I) -> IResult<I, usize, E>
443where
444  I: Clone + InputLength,
445  F: Parser<I, O, E>,
446  E: ParseError<I>,
447{
448  move |i: I| {
449    let mut input = i;
450    let mut count = 0;
451
452    loop {
453      let input_ = input.clone();
454      let len = input.input_len();
455      match f.parse(input_) {
456        Ok((i, _)) => {
457          // infinite loop check: the parser must always consume
458          if i.input_len() == len {
459            return Err(Err::Error(E::from_error_kind(input, ErrorKind::Many0Count)));
460          }
461
462          input = i;
463          count += 1;
464        }
465
466        Err(Err::Error(_)) => return Ok((input, count)),
467
468        Err(e) => return Err(e),
469      }
470    }
471  }
472}
473
474/// Runs the embedded parser, counting the results.
475///
476/// This stops on [`Err::Error`] if there is at least one result.  To instead chain an error up,
477/// see [`cut`][crate::combinator::cut].
478///
479/// # Arguments
480/// * `f` The parser to apply.
481///
482/// *Note*: If the parser passed to `many1` accepts empty inputs
483/// (like `alpha0` or `digit0`), `many1` will return an error,
484/// to prevent going into an infinite loop.
485///
486/// ```rust
487/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
488/// use nom8::multi::many1_count;
489/// use nom8::bytes::tag;
490///
491/// fn parser(s: &str) -> IResult<&str, usize> {
492///   many1_count(tag("abc"))(s)
493/// }
494///
495/// assert_eq!(parser("abcabc"), Ok(("", 2)));
496/// assert_eq!(parser("abc123"), Ok(("123", 1)));
497/// assert_eq!(parser("123123"), Err(Err::Error(Error::new("123123", ErrorKind::Many1Count))));
498/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Many1Count))));
499/// ```
500pub fn many1_count<I, O, E, F>(mut f: F) -> impl FnMut(I) -> IResult<I, usize, E>
501where
502  I: Clone + InputLength,
503  F: Parser<I, O, E>,
504  E: ParseError<I>,
505{
506  move |i: I| {
507    let i_ = i.clone();
508    match f.parse(i_) {
509      Err(Err::Error(_)) => Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1Count))),
510      Err(i) => Err(i),
511      Ok((i1, _)) => {
512        let mut count = 1;
513        let mut input = i1;
514
515        loop {
516          let len = input.input_len();
517          let input_ = input.clone();
518          match f.parse(input_) {
519            Err(Err::Error(_)) => return Ok((input, count)),
520            Err(e) => return Err(e),
521            Ok((i, _)) => {
522              // infinite loop check: the parser must always consume
523              if i.input_len() == len {
524                return Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1Count)));
525              }
526
527              count += 1;
528              input = i;
529            }
530          }
531        }
532      }
533    }
534  }
535}
536
537/// Runs the embedded parser `count` times, gathering the results in a `Vec`
538///
539/// # Arguments
540/// * `f` The parser to apply.
541/// * `count` How often to apply the parser.
542/// ```rust
543/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
544/// use nom8::multi::count;
545/// use nom8::bytes::tag;
546///
547/// fn parser(s: &str) -> IResult<&str, Vec<&str>> {
548///   count(tag("abc"), 2)(s)
549/// }
550///
551/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"])));
552/// assert_eq!(parser("abc123"), Err(Err::Error(Error::new("123", ErrorKind::Tag))));
553/// assert_eq!(parser("123123"), Err(Err::Error(Error::new("123123", ErrorKind::Tag))));
554/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Tag))));
555/// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"])));
556/// ```
557#[cfg(feature = "alloc")]
558pub fn count<I, O, E, F>(mut f: F, count: usize) -> impl FnMut(I) -> IResult<I, Vec<O>, E>
559where
560  I: Clone + PartialEq,
561  F: Parser<I, O, E>,
562  E: ParseError<I>,
563{
564  move |i: I| {
565    let mut input = i.clone();
566    let mut res = crate::lib::std::vec::Vec::with_capacity(count.clamp(0, MAX_INITIAL_CAPACITY));
567
568    for _ in 0..count {
569      let input_ = input.clone();
570      match f.parse(input_) {
571        Ok((i, o)) => {
572          res.push(o);
573          input = i;
574        }
575        Err(Err::Error(e)) => {
576          return Err(Err::Error(E::append(i, ErrorKind::Count, e)));
577        }
578        Err(e) => {
579          return Err(e);
580        }
581      }
582    }
583
584    Ok((input, res))
585  }
586}
587
588/// Runs the embedded parser repeatedly, filling the given slice with results.
589///
590/// This parser fails if the input runs out before the given slice is full.
591///
592/// # Arguments
593/// * `f` The parser to apply.
594/// * `buf` The slice to fill
595/// ```rust
596/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
597/// use nom8::multi::fill;
598/// use nom8::bytes::tag;
599///
600/// fn parser(s: &str) -> IResult<&str, [&str; 2]> {
601///   let mut buf = ["", ""];
602///   let (rest, ()) = fill(tag("abc"), &mut buf)(s)?;
603///   Ok((rest, buf))
604/// }
605///
606/// assert_eq!(parser("abcabc"), Ok(("", ["abc", "abc"])));
607/// assert_eq!(parser("abc123"), Err(Err::Error(Error::new("123", ErrorKind::Tag))));
608/// assert_eq!(parser("123123"), Err(Err::Error(Error::new("123123", ErrorKind::Tag))));
609/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Tag))));
610/// assert_eq!(parser("abcabcabc"), Ok(("abc", ["abc", "abc"])));
611/// ```
612pub fn fill<'a, I, O, E, F>(mut f: F, buf: &'a mut [O]) -> impl FnMut(I) -> IResult<I, (), E> + 'a
613where
614  I: Clone + PartialEq,
615  F: Parser<I, O, E> + 'a,
616  E: ParseError<I>,
617{
618  move |i: I| {
619    let mut input = i.clone();
620
621    for elem in buf.iter_mut() {
622      let input_ = input.clone();
623      match f.parse(input_) {
624        Ok((i, o)) => {
625          *elem = o;
626          input = i;
627        }
628        Err(Err::Error(e)) => {
629          return Err(Err::Error(E::append(i, ErrorKind::Count, e)));
630        }
631        Err(e) => {
632          return Err(e);
633        }
634      }
635    }
636
637    Ok((input, ()))
638  }
639}
640
641/// Repeats the embedded parser, calling `g` to gather the results.
642///
643/// This stops on [`Err::Error`].  To instead chain an error up, see
644/// [`cut`][crate::combinator::cut].
645///
646/// # Arguments
647/// * `f` The parser to apply.
648/// * `init` A function returning the initial value.
649/// * `g` The function that combines a result of `f` with
650///       the current accumulator.
651///
652/// *Note*: if the parser passed in accepts empty inputs (like `alpha0` or `digit0`), `many0` will
653/// return an error, to prevent going into an infinite loop
654///
655/// ```rust
656/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
657/// use nom8::multi::fold_many0;
658/// use nom8::bytes::tag;
659///
660/// fn parser(s: &str) -> IResult<&str, Vec<&str>> {
661///   fold_many0(
662///     tag("abc"),
663///     Vec::new,
664///     |mut acc: Vec<_>, item| {
665///       acc.push(item);
666///       acc
667///     }
668///   )(s)
669/// }
670///
671/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"])));
672/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"])));
673/// assert_eq!(parser("123123"), Ok(("123123", vec![])));
674/// assert_eq!(parser(""), Ok(("", vec![])));
675/// ```
676pub fn fold_many0<I, O, E, F, G, H, R>(
677  mut f: F,
678  mut init: H,
679  mut g: G,
680) -> impl FnMut(I) -> IResult<I, R, E>
681where
682  I: Clone + InputLength,
683  F: Parser<I, O, E>,
684  G: FnMut(R, O) -> R,
685  H: FnMut() -> R,
686  E: ParseError<I>,
687{
688  move |i: I| {
689    let mut res = init();
690    let mut input = i;
691
692    loop {
693      let i_ = input.clone();
694      let len = input.input_len();
695      match f.parse(i_) {
696        Ok((i, o)) => {
697          // infinite loop check: the parser must always consume
698          if i.input_len() == len {
699            return Err(Err::Error(E::from_error_kind(input, ErrorKind::Many0)));
700          }
701
702          res = g(res, o);
703          input = i;
704        }
705        Err(Err::Error(_)) => {
706          return Ok((input, res));
707        }
708        Err(e) => {
709          return Err(e);
710        }
711      }
712    }
713  }
714}
715
716/// Repeats the embedded parser, calling `g` to gather the results.
717///
718/// This stops on [`Err::Error`] if there is at least one result.  To instead chain an error up,
719/// see [`cut`][crate::combinator::cut].
720///
721/// # Arguments
722/// * `f` The parser to apply.
723/// * `init` A function returning the initial value.
724/// * `g` The function that combines a result of `f` with
725///       the current accumulator.
726///
727/// *Note*: If the parser passed to `many1` accepts empty inputs
728/// (like `alpha0` or `digit0`), `many1` will return an error,
729/// to prevent going into an infinite loop.
730///
731/// ```rust
732/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
733/// use nom8::multi::fold_many1;
734/// use nom8::bytes::tag;
735///
736/// fn parser(s: &str) -> IResult<&str, Vec<&str>> {
737///   fold_many1(
738///     tag("abc"),
739///     Vec::new,
740///     |mut acc: Vec<_>, item| {
741///       acc.push(item);
742///       acc
743///     }
744///   )(s)
745/// }
746///
747/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"])));
748/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"])));
749/// assert_eq!(parser("123123"), Err(Err::Error(Error::new("123123", ErrorKind::Many1))));
750/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Many1))));
751/// ```
752pub fn fold_many1<I, O, E, F, G, H, R>(
753  mut f: F,
754  mut init: H,
755  mut g: G,
756) -> impl FnMut(I) -> IResult<I, R, E>
757where
758  I: Clone + InputLength,
759  F: Parser<I, O, E>,
760  G: FnMut(R, O) -> R,
761  H: FnMut() -> R,
762  E: ParseError<I>,
763{
764  move |i: I| {
765    let _i = i.clone();
766    let init = init();
767    match f.parse(_i) {
768      Err(Err::Error(_)) => Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1))),
769      Err(e) => Err(e),
770      Ok((i1, o1)) => {
771        let mut acc = g(init, o1);
772        let mut input = i1;
773
774        loop {
775          let _input = input.clone();
776          let len = input.input_len();
777          match f.parse(_input) {
778            Err(Err::Error(_)) => {
779              break;
780            }
781            Err(e) => return Err(e),
782            Ok((i, o)) => {
783              // infinite loop check: the parser must always consume
784              if i.input_len() == len {
785                return Err(Err::Failure(E::from_error_kind(i, ErrorKind::Many1)));
786              }
787
788              acc = g(acc, o);
789              input = i;
790            }
791          }
792        }
793
794        Ok((input, acc))
795      }
796    }
797  }
798}
799
800/// Repeats the embedded parser `m..=n` times, calling `g` to gather the results
801///
802/// This stops before `n` when the parser returns [`Err::Error`].  To instead chain an error up, see
803/// [`cut`][crate::combinator::cut].
804///
805/// # Arguments
806/// * `m` The minimum number of iterations.
807/// * `n` The maximum number of iterations.
808/// * `f` The parser to apply.
809/// * `init` A function returning the initial value.
810/// * `g` The function that combines a result of `f` with
811///       the current accumulator.
812///
813/// *Note*: If the parser passed to `many1` accepts empty inputs
814/// (like `alpha0` or `digit0`), `many1` will return an error,
815/// to prevent going into an infinite loop.
816///
817/// ```rust
818/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
819/// use nom8::multi::fold_many_m_n;
820/// use nom8::bytes::tag;
821///
822/// fn parser(s: &str) -> IResult<&str, Vec<&str>> {
823///   fold_many_m_n(
824///     0,
825///     2,
826///     tag("abc"),
827///     Vec::new,
828///     |mut acc: Vec<_>, item| {
829///       acc.push(item);
830///       acc
831///     }
832///   )(s)
833/// }
834///
835/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"])));
836/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"])));
837/// assert_eq!(parser("123123"), Ok(("123123", vec![])));
838/// assert_eq!(parser(""), Ok(("", vec![])));
839/// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"])));
840/// ```
841pub fn fold_many_m_n<I, O, E, F, G, H, R>(
842  min: usize,
843  max: usize,
844  mut parse: F,
845  mut init: H,
846  mut fold: G,
847) -> impl FnMut(I) -> IResult<I, R, E>
848where
849  I: Clone + InputLength,
850  F: Parser<I, O, E>,
851  G: FnMut(R, O) -> R,
852  H: FnMut() -> R,
853  E: ParseError<I>,
854{
855  move |mut input: I| {
856    if min > max {
857      return Err(Err::Failure(E::from_error_kind(input, ErrorKind::ManyMN)));
858    }
859
860    let mut acc = init();
861    for count in 0..max {
862      let len = input.input_len();
863      match parse.parse(input.clone()) {
864        Ok((tail, value)) => {
865          // infinite loop check: the parser must always consume
866          if tail.input_len() == len {
867            return Err(Err::Error(E::from_error_kind(tail, ErrorKind::ManyMN)));
868          }
869
870          acc = fold(acc, value);
871          input = tail;
872        }
873        //FInputXMError: handle failure properly
874        Err(Err::Error(err)) => {
875          if count < min {
876            return Err(Err::Error(E::append(input, ErrorKind::ManyMN, err)));
877          } else {
878            break;
879          }
880        }
881        Err(e) => return Err(e),
882      }
883    }
884
885    Ok((input, acc))
886  }
887}
888
889/// Gets a number from the parser and returns a
890/// subslice of the input of that size.
891///
892/// *Complete version*: Returns an error if there is not enough input data.
893///
894/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
895///
896/// # Arguments
897/// * `f` The parser to apply.
898/// ```rust
899/// # use nom8::{Err, error::ErrorKind, Needed, IResult, input::Streaming};
900/// use nom8::number::be_u16;
901/// use nom8::multi::length_data;
902/// use nom8::bytes::tag;
903///
904/// fn parser(s: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, &[u8]> {
905///   length_data(be_u16)(s)
906/// }
907///
908/// assert_eq!(parser(Streaming(b"\x00\x03abcefg")), Ok((Streaming(&b"efg"[..]), &b"abc"[..])));
909/// assert_eq!(parser(Streaming(b"\x00\x03a")), Err(Err::Incomplete(Needed::new(2))));
910/// ```
911pub fn length_data<I, N, E, F, const STREAMING: bool>(
912  mut f: F,
913) -> impl FnMut(I) -> IResult<I, <I as IntoOutput>::Output, E>
914where
915  I: InputLength + InputTake + InputIter + IntoOutput + InputIsStreaming<STREAMING>,
916  N: ToUsize,
917  F: Parser<I, N, E>,
918  E: ParseError<I>,
919{
920  move |i: I| {
921    let (i, length) = f.parse(i)?;
922
923    crate::bytes::take(length).parse(i)
924  }
925}
926
927/// Gets a number from the first parser,
928/// takes a subslice of the input of that size,
929/// then applies the second parser on that subslice.
930/// If the second parser returns `Incomplete`,
931/// `length_value` will return an error.
932///
933/// *Complete version*: Returns an error if there is not enough input data.
934///
935/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there is not enough data.
936///
937/// # Arguments
938/// * `f` The parser to apply.
939/// * `g` The parser to apply on the subslice.
940/// ```rust
941/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult, input::Streaming};
942/// use nom8::number::be_u16;
943/// use nom8::multi::length_value;
944/// use nom8::bytes::tag;
945///
946/// fn parser(s: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, &[u8]> {
947///   length_value(be_u16, tag("abc"))(s)
948/// }
949///
950/// assert_eq!(parser(Streaming(b"\x00\x03abcefg")), Ok((Streaming(&b"efg"[..]), &b"abc"[..])));
951/// assert_eq!(parser(Streaming(b"\x00\x03123123")), Err(Err::Error(Error::new(Streaming(&b"123"[..]), ErrorKind::Tag))));
952/// assert_eq!(parser(Streaming(b"\x00\x03a")), Err(Err::Incomplete(Needed::new(2))));
953/// ```
954pub fn length_value<I, O, N, E, F, G, const STREAMING: bool>(
955  mut f: F,
956  mut g: G,
957) -> impl FnMut(I) -> IResult<I, O, E>
958where
959  I: InputLength + InputTake + InputIter + IntoOutput + InputIsStreaming<STREAMING>,
960  I: Clone,
961  N: ToUsize,
962  F: Parser<I, N, E>,
963  G: Parser<I, O, E>,
964  E: ParseError<I>,
965{
966  move |i: I| {
967    let (i, data) = length_data(f.by_ref()).parse(i)?;
968    let data = I::merge_output(i.clone(), data);
969    let (_, o) = g.by_ref().complete().parse(data)?;
970    Ok((i, o))
971  }
972}
973
974/// Gets a number from the first parser,
975/// then applies the second parser that many times.
976/// # Arguments
977/// * `f` The parser to apply to obtain the count.
978/// * `g` The parser to apply repeatedly.
979/// ```rust
980/// # use nom8::prelude::*;
981/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
982/// use nom8::number::u8;
983/// use nom8::multi::length_count;
984/// use nom8::bytes::tag;
985/// use nom8::combinator::map;
986///
987/// fn parser(s: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
988///   length_count(u8.map(|i| {
989///      println!("got number: {}", i);
990///      i
991///   }), tag("abc"))(s)
992/// }
993///
994/// assert_eq!(parser(&b"\x02abcabcabc"[..]), Ok(((&b"abc"[..], vec![&b"abc"[..], &b"abc"[..]]))));
995/// assert_eq!(parser(b"\x03123123123"), Err(Err::Error(Error::new(&b"123123123"[..], ErrorKind::Tag))));
996/// ```
997#[cfg(feature = "alloc")]
998pub fn length_count<I, O, N, E, F, G>(mut f: F, mut g: G) -> impl FnMut(I) -> IResult<I, Vec<O>, E>
999where
1000  I: Clone,
1001  N: ToUsize,
1002  F: Parser<I, N, E>,
1003  G: Parser<I, O, E>,
1004  E: ParseError<I>,
1005{
1006  move |i: I| {
1007    let (i, count) = f.parse(i)?;
1008    let mut input = i.clone();
1009    let mut res = Vec::new();
1010
1011    for _ in 0..count.to_usize() {
1012      let input_ = input.clone();
1013      match g.parse(input_) {
1014        Ok((i, o)) => {
1015          res.push(o);
1016          input = i;
1017        }
1018        Err(Err::Error(e)) => {
1019          return Err(Err::Error(E::append(i, ErrorKind::Count, e)));
1020        }
1021        Err(e) => {
1022          return Err(e);
1023        }
1024      }
1025    }
1026
1027    Ok((input, res))
1028  }
1029}