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