nom8/
parser.rs

1//! Basic types to build the parsers
2
3use self::Needed::*;
4use crate::combinator::*;
5#[cfg(feature = "std")]
6use crate::error::DbgErr;
7use crate::error::{self, Context, ContextError, ErrorKind, ParseError};
8use crate::input::InputIsStreaming;
9use crate::input::*;
10use crate::lib::std::fmt;
11use crate::lib::std::ops::RangeFrom;
12use core::num::NonZeroUsize;
13
14/// Holds the result of parsing functions
15///
16/// It depends on the input type `I`, the output type `O`, and the error type `E`
17/// (by default `(I, nom8::ErrorKind)`)
18///
19/// The `Ok` side is a pair containing the remainder of the input (the part of the data that
20/// was not parsed) and the produced value. The `Err` side contains an instance of `nom8::Err`.
21///
22/// Outside of the parsing code, you can use the [`FinishIResult::finish`] method to convert
23/// it to a more common result type
24pub type IResult<I, O, E = error::Error<I>> = Result<(I, O), Err<E>>;
25
26/// Extension trait to convert a parser's [`IResult`] to a more manageable type
27pub trait FinishIResult<I, O, E> {
28  /// Converts the parser's [`IResult`] to a type that is more consumable by callers.
29  ///
30  /// Errors if the parser is not at the [end of input][crate::combinator::eof].  See
31  /// [`FinishIResult::finish_err`] if the remaining input is needed.
32  ///
33  /// # Panic
34  ///
35  /// If the result is `Err(Err::Incomplete(_))`, this method will panic.
36  /// - "complete" parsers: It will not be an issue, `Incomplete` is never used
37  /// - "streaming" parsers: `Incomplete` will be returned if there's not enough data
38  /// for the parser to decide, and you should gather more data before parsing again.
39  /// Once the parser returns either `Ok(_)`, `Err(Err::Error(_))` or `Err(Err::Failure(_))`,
40  /// you can get out of the parsing loop and call `finish_err()` on the parser's result
41  fn finish(self) -> Result<O, E>;
42
43  /// Converts the parser's [`IResult`] to a type that is more consumable by errors.
44  ///
45  ///  It keeps the same `Ok` branch, and merges `Err::Error` and `Err::Failure` into the `Err`
46  ///  side.
47  ///
48  /// # Panic
49  ///
50  /// If the result is `Err(Err::Incomplete(_))`, this method will panic as [`Err::Incomplete`]
51  /// should only be set when the input is [`InputIsStreaming<false>`] which this isn't implemented
52  /// for.
53  fn finish_err(self) -> Result<(I, O), E>;
54}
55
56impl<I, O, E> FinishIResult<I, O, E> for IResult<I, O, E>
57where
58  I: crate::input::InputLength,
59  I: crate::input::IntoOutput,
60  // Force users to deal with `Incomplete` when `InputIsStreaming<true>`
61  I: InputIsStreaming<false>,
62  I: Clone,
63  E: crate::error::ParseError<I>,
64{
65  fn finish(self) -> Result<O, E> {
66    let (i, o) = self.finish_err()?;
67    crate::combinator::eof(i).finish_err()?;
68    Ok(o)
69  }
70
71  fn finish_err(self) -> Result<(I, O), E> {
72    match self {
73      Ok(res) => Ok(res),
74      Err(Err::Error(e)) | Err(Err::Failure(e)) => Err(e),
75      Err(Err::Incomplete(_)) => {
76        panic!("`InputIsStreaming<false>` conflicts with `Err(Err::Incomplete(_))`")
77      }
78    }
79  }
80}
81
82#[doc(hidden)]
83#[deprecated(
84  since = "8.0.0",
85  note = "Replaced with `FinishIResult` which is available via `nom8::prelude`"
86)]
87pub trait Finish<I, O, E> {
88  #[deprecated(
89    since = "8.0.0",
90    note = "Replaced with `FinishIResult::finish_err` which is available via `nom8::prelude`"
91  )]
92  fn finish(self) -> Result<(I, O), E>;
93}
94
95#[allow(deprecated)]
96impl<I, O, E> Finish<I, O, E> for IResult<I, O, E> {
97  fn finish(self) -> Result<(I, O), E> {
98    match self {
99      Ok(res) => Ok(res),
100      Err(Err::Error(e)) | Err(Err::Failure(e)) => Err(e),
101      Err(Err::Incomplete(_)) => {
102        panic!("Cannot call `finish()` on `Err(Err::Incomplete(_))`: this result means that the parser does not have enough data to decide, you should gather more data and try to reapply  the parser instead")
103      }
104    }
105  }
106}
107
108/// Convert an `Input` into an appropriate `Output` type
109pub trait IntoOutputIResult<I, O, E> {
110  /// Convert an `Input` into an appropriate `Output` type
111  fn into_output(self) -> IResult<I, O, E>;
112}
113
114impl<I, E> IntoOutputIResult<I, <I as crate::input::IntoOutput>::Output, E> for IResult<I, I, E>
115where
116  I: crate::input::IntoOutput,
117{
118  fn into_output(self) -> IResult<I, <I as crate::input::IntoOutput>::Output, E> {
119    self.map(|(i, o)| (i, o.into_output()))
120  }
121}
122
123/// Contains information on needed data if a parser returned `Incomplete`
124///
125/// **Note:** This is only possible for `Input` types that implement [`InputIsStreaming<true>`],
126/// like [`Streaming`][crate::input::Streaming].
127#[derive(Debug, PartialEq, Eq, Clone, Copy)]
128#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
129pub enum Needed {
130  /// Needs more data, but we do not know how much
131  Unknown,
132  /// Contains the required data size in bytes
133  Size(NonZeroUsize),
134}
135
136impl Needed {
137  /// Creates `Needed` instance, returns `Needed::Unknown` if the argument is zero
138  pub fn new(s: usize) -> Self {
139    match NonZeroUsize::new(s) {
140      Some(sz) => Needed::Size(sz),
141      None => Needed::Unknown,
142    }
143  }
144
145  /// Indicates if we know how many bytes we need
146  pub fn is_known(&self) -> bool {
147    *self != Unknown
148  }
149
150  /// Maps a `Needed` to `Needed` by applying a function to a contained `Size` value.
151  #[inline]
152  pub fn map<F: Fn(NonZeroUsize) -> usize>(self, f: F) -> Needed {
153    match self {
154      Unknown => Unknown,
155      Size(n) => Needed::new(f(n)),
156    }
157  }
158}
159
160/// The `Err` enum indicates the parser was not successful
161///
162/// It has three cases:
163///
164/// * `Incomplete` indicates that more data is needed to decide. The [`Needed`] enum
165/// can contain how many additional bytes are necessary. If you are sure your parser
166/// is working on full data, you can wrap your parser with the `complete` combinator
167/// to transform that case in `Error`
168/// * `Error` means some parser did not succeed, but another one might (as an example,
169/// when testing different branches of an `alt` combinator)
170/// * `Failure` indicates an unrecoverable error. As an example, if you recognize a prefix
171/// to decide on the next parser to apply, and that parser fails, you know there's no need
172/// to try other parsers, you were already in the right branch, so the data is invalid
173///
174#[derive(Debug, Clone, PartialEq)]
175#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
176pub enum Err<E> {
177  /// There was not enough data
178  ///
179  /// This must only be set when the `Input` is [`InputIsStreaming<true>`], like with
180  /// [`Streaming`][crate::input::Streaming]
181  ///
182  /// Convert this into an `Error` with [`Parser::complete`][Parser::complete]
183  Incomplete(Needed),
184  /// The parser had an error (recoverable)
185  Error(E),
186  /// The parser had an unrecoverable error: we got to the right
187  /// branch and we know other branches won't work, so backtrack
188  /// as fast as possible
189  Failure(E),
190}
191
192impl<E> Err<E> {
193  /// Tests if the result is Incomplete
194  pub fn is_incomplete(&self) -> bool {
195    if let Err::Incomplete(_) = self {
196      true
197    } else {
198      false
199    }
200  }
201
202  /// Applies the given function to the inner error
203  pub fn map<E2, F>(self, f: F) -> Err<E2>
204  where
205    F: FnOnce(E) -> E2,
206  {
207    match self {
208      Err::Incomplete(n) => Err::Incomplete(n),
209      Err::Failure(t) => Err::Failure(f(t)),
210      Err::Error(t) => Err::Error(f(t)),
211    }
212  }
213
214  /// Automatically converts between errors if the underlying type supports it
215  pub fn convert<F>(e: Err<F>) -> Self
216  where
217    E: From<F>,
218  {
219    e.map(crate::lib::std::convert::Into::into)
220  }
221}
222
223impl<T> Err<(T, ErrorKind)> {
224  /// Maps `Err<(T, ErrorKind)>` to `Err<(U, ErrorKind)>` with the given `F: T -> U`
225  pub fn map_input<U, F>(self, f: F) -> Err<(U, ErrorKind)>
226  where
227    F: FnOnce(T) -> U,
228  {
229    match self {
230      Err::Incomplete(n) => Err::Incomplete(n),
231      Err::Failure((input, k)) => Err::Failure((f(input), k)),
232      Err::Error((input, k)) => Err::Error((f(input), k)),
233    }
234  }
235}
236
237impl<T> Err<error::Error<T>> {
238  /// Maps `Err<error::Error<T>>` to `Err<error::Error<U>>` with the given `F: T -> U`
239  pub fn map_input<U, F>(self, f: F) -> Err<error::Error<U>>
240  where
241    F: FnOnce(T) -> U,
242  {
243    match self {
244      Err::Incomplete(n) => Err::Incomplete(n),
245      Err::Failure(error::Error { input, code }) => Err::Failure(error::Error {
246        input: f(input),
247        code,
248      }),
249      Err::Error(error::Error { input, code }) => Err::Error(error::Error {
250        input: f(input),
251        code,
252      }),
253    }
254  }
255}
256
257#[cfg(feature = "alloc")]
258use crate::lib::std::{borrow::ToOwned, string::String, vec::Vec};
259impl Err<(&[u8], ErrorKind)> {
260  /// Obtaining ownership
261  #[cfg(feature = "alloc")]
262  pub fn to_owned(self) -> Err<(Vec<u8>, ErrorKind)> {
263    self.map_input(ToOwned::to_owned)
264  }
265}
266
267impl Err<(&str, ErrorKind)> {
268  /// Obtaining ownership
269  #[cfg(feature = "alloc")]
270  pub fn to_owned(self) -> Err<(String, ErrorKind)> {
271    self.map_input(ToOwned::to_owned)
272  }
273}
274
275impl Err<error::Error<&[u8]>> {
276  /// Obtaining ownership
277  #[cfg(feature = "alloc")]
278  pub fn to_owned(self) -> Err<error::Error<Vec<u8>>> {
279    self.map_input(ToOwned::to_owned)
280  }
281}
282
283impl Err<error::Error<&str>> {
284  /// Obtaining ownership
285  #[cfg(feature = "alloc")]
286  pub fn to_owned(self) -> Err<error::Error<String>> {
287    self.map_input(ToOwned::to_owned)
288  }
289}
290
291impl<E: Eq> Eq for Err<E> {}
292
293impl<E> fmt::Display for Err<E>
294where
295  E: fmt::Debug,
296{
297  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
298    match self {
299      Err::Incomplete(Needed::Size(u)) => write!(f, "Parsing requires {} bytes/chars", u),
300      Err::Incomplete(Needed::Unknown) => write!(f, "Parsing requires more data"),
301      Err::Failure(c) => write!(f, "Parsing Failure: {:?}", c),
302      Err::Error(c) => write!(f, "Parsing Error: {:?}", c),
303    }
304  }
305}
306
307#[cfg(feature = "std")]
308use std::error::Error;
309
310#[cfg(feature = "std")]
311impl<E> Error for Err<E>
312where
313  E: fmt::Debug,
314{
315  fn source(&self) -> Option<&(dyn Error + 'static)> {
316    None // no underlying error
317  }
318}
319
320/// All nom parsers implement this trait
321///
322/// The simplest way to implement a `Parser` is with a function
323/// ```rust
324/// use nom8::prelude::*;
325///
326/// fn success(input: &str) -> IResult<&str, ()> {
327///     let output = ();
328///     Ok((input, output))
329/// }
330///
331/// let (input, output) = success.parse("Hello").unwrap();
332/// assert_eq!(input, "Hello");  // We didn't consume any input
333/// ```
334///
335/// which can be made stateful by returning a function
336/// ```rust
337/// use nom8::prelude::*;
338///
339/// fn success<O: Clone>(output: O) -> impl FnMut(&str) -> IResult<&str, O> {
340///     move |input: &str| {
341///         let output = output.clone();
342///         Ok((input, output))
343///     }
344/// }
345///
346/// let (input, output) = success("World").parse("Hello").unwrap();
347/// assert_eq!(input, "Hello");  // We didn't consume any input
348/// assert_eq!(output, "World");
349/// ```
350///
351/// Additionally, some basic types implement `Parser` as well, including
352/// - `u8` and `char`, see [`nom8::character::char`][crate::bytes::one_of]
353/// - `&[u8]` and `&str`, see [`nom8::character::char`][crate::bytes::tag]
354pub trait Parser<I, O, E> {
355  /// A parser takes in input type, and returns a `Result` containing
356  /// either the remaining input and the output value, or an error
357  fn parse(&mut self, input: I) -> IResult<I, O, E>;
358
359  /// Treat `&mut Self` as a parser
360  ///
361  /// This helps when needing to move a `Parser` when all you have is a `&mut Parser`.
362  ///
363  /// # Example
364  ///
365  /// Because parsers are `FnMut`, they can be called multiple times.  This prevents moving `f`
366  /// into [`length_data`][crate::multi::length_data] and `g` into
367  /// [`complete`][Parser::complete]:
368  /// ```rust,compile_fail
369  /// # use nom8::prelude::*;
370  /// # use nom8::IResult;
371  /// # use nom8::Parser;
372  /// # use nom8::error::ParseError;
373  /// # use nom8::multi::length_data;
374  /// pub fn length_value<'i, O, E: ParseError<&'i [u8]>>(
375  ///     mut f: impl Parser<&'i [u8], usize, E>,
376  ///     mut g: impl Parser<&'i [u8], O, E>
377  /// ) -> impl FnMut(&'i [u8]) -> IResult<&'i [u8], O, E> {
378  ///   move |i: &'i [u8]| {
379  ///     let (i, data) = length_data(f).parse(i)?;
380  ///     let (_, o) = g.complete().parse(data)?;
381  ///     Ok((i, o))
382  ///   }
383  /// }
384  /// ```
385  ///
386  /// By adding `by_ref`, we can make this work:
387  /// ```rust
388  /// # use nom8::prelude::*;
389  /// # use nom8::IResult;
390  /// # use nom8::Parser;
391  /// # use nom8::error::ParseError;
392  /// # use nom8::multi::length_data;
393  /// pub fn length_value<'i, O, E: ParseError<&'i [u8]>>(
394  ///     mut f: impl Parser<&'i [u8], usize, E>,
395  ///     mut g: impl Parser<&'i [u8], O, E>
396  /// ) -> impl FnMut(&'i [u8]) -> IResult<&'i [u8], O, E> {
397  ///   move |i: &'i [u8]| {
398  ///     let (i, data) = length_data(f.by_ref()).parse(i)?;
399  ///     let (_, o) = g.by_ref().complete().parse(data)?;
400  ///     Ok((i, o))
401  ///   }
402  /// }
403  /// ```
404  fn by_ref(&mut self) -> ByRef<Self>
405  where
406    Self: core::marker::Sized,
407  {
408    ByRef::new(self)
409  }
410  /// Returns the provided value if the child parser succeeds.
411  ///
412  /// # Example
413  ///
414  /// ```rust
415  /// # use nom8::{Err,error::ErrorKind, IResult, Parser};
416  /// use nom8::character::alpha1;
417  /// # fn main() {
418  ///
419  /// let mut parser = alpha1.value(1234);
420  ///
421  /// assert_eq!(parser.parse("abcd"), Ok(("", 1234)));
422  /// assert_eq!(parser.parse("123abcd;"), Err(Err::Error(("123abcd;", ErrorKind::Alpha))));
423  /// # }
424  /// ```
425  fn value<O2>(self, val: O2) -> Value<Self, O, O2>
426  where
427    Self: core::marker::Sized,
428    O2: Clone,
429  {
430    Value::new(self, val)
431  }
432
433  /// Convert the parser's output to another type using [`std::convert::From`]
434  ///
435  /// # Example
436  ///
437  /// ```rust
438  /// # use nom8::IResult;
439  /// # use nom8::Parser;
440  /// use nom8::character::alpha1;
441  /// # fn main() {
442  ///
443  ///  fn parser1(i: &str) -> IResult<&str, &str> {
444  ///    alpha1(i)
445  ///  }
446  ///
447  ///  let mut parser2 = parser1.output_into();
448  ///
449  /// // the parser converts the &str output of the child parser into a Vec<u8>
450  /// let bytes: IResult<&str, Vec<u8>> = parser2.parse("abcd");
451  /// assert_eq!(bytes, Ok(("", vec![97, 98, 99, 100])));
452  /// # }
453  /// ```
454  fn output_into<O2: From<O>>(self) -> OutputInto<Self, O, O2>
455  where
456    Self: core::marker::Sized,
457  {
458    OutputInto::new(self)
459  }
460
461  /// If the child parser was successful, return the consumed input as produced value.
462  ///
463  /// # Example
464  ///
465  /// ```rust
466  /// # use nom8::{Err,error::ErrorKind, IResult, Parser};
467  /// use nom8::character::{alpha1};
468  /// use nom8::sequence::separated_pair;
469  /// # fn main() {
470  ///
471  /// let mut parser = separated_pair(alpha1, ',', alpha1).recognize();
472  ///
473  /// assert_eq!(parser.parse("abcd,efgh"), Ok(("", "abcd,efgh")));
474  /// assert_eq!(parser.parse("abcd;"),Err(Err::Error((";", ErrorKind::OneOf))));
475  /// # }
476  /// ```
477  fn recognize(self) -> Recognize<Self, O>
478  where
479    Self: core::marker::Sized,
480  {
481    Recognize::new(self)
482  }
483
484  /// if the child parser was successful, return the consumed input with the output
485  /// as a tuple. Functions similarly to [recognize](fn.recognize.html) except it
486  /// returns the parser output as well.
487  ///
488  /// This can be useful especially in cases where the output is not the same type
489  /// as the input, or the input is a user defined type.
490  ///
491  /// Returned tuple is of the format `(produced output, consumed input)`.
492  ///
493  /// # Example
494  ///
495  /// ```rust
496  /// # use nom8::prelude::*;
497  /// # use nom8::{Err,error::ErrorKind, IResult};
498  /// use nom8::character::{alpha1};
499  /// use nom8::bytes::tag;
500  /// use nom8::sequence::separated_pair;
501  ///
502  /// fn inner_parser(input: &str) -> IResult<&str, bool> {
503  ///     tag("1234").value(true).parse(input)
504  /// }
505  ///
506  /// # fn main() {
507  ///
508  /// let mut consumed_parser = separated_pair(alpha1, ',', alpha1).value(true).with_recognized();
509  ///
510  /// assert_eq!(consumed_parser.parse("abcd,efgh1"), Ok(("1", (true, "abcd,efgh"))));
511  /// assert_eq!(consumed_parser.parse("abcd;"),Err(Err::Error((";", ErrorKind::OneOf))));
512  ///
513  /// // the second output (representing the consumed input)
514  /// // should be the same as that of the `recognize` parser.
515  /// let mut recognize_parser = inner_parser.recognize();
516  /// let mut consumed_parser = inner_parser.with_recognized().map(|(output, consumed)| consumed);
517  ///
518  /// assert_eq!(recognize_parser.parse("1234"), consumed_parser.parse("1234"));
519  /// assert_eq!(recognize_parser.parse("abcd"), consumed_parser.parse("abcd"));
520  /// # }
521  /// ```
522  fn with_recognized(self) -> WithRecognized<Self, O>
523  where
524    Self: core::marker::Sized,
525  {
526    WithRecognized::new(self)
527  }
528
529  /// If the child parser was successful, return the location of the consumed input as produced value.
530  ///
531  /// # Example
532  ///
533  /// ```rust
534  /// # use nom8::prelude::*;
535  /// # use nom8::{Err,error::ErrorKind, IResult, Parser, input::Slice};
536  /// use nom8::input::Located;
537  /// use nom8::character::alpha1;
538  /// use nom8::sequence::separated_pair;
539  ///
540  /// let mut parser = separated_pair(alpha1.span(), ',', alpha1.span());
541  ///
542  /// assert_eq!(parser.parse(Located::new("abcd,efgh")).finish(), Ok((0..4, 5..9)));
543  /// assert_eq!(parser.parse(Located::new("abcd;")),Err(Err::Error((Located::new("abcd;").slice(4..), ErrorKind::OneOf))));
544  /// ```
545  fn span(self) -> Span<Self, O>
546  where
547    Self: core::marker::Sized,
548    I: Location + Clone,
549  {
550    Span::new(self)
551  }
552
553  /// if the child parser was successful, return the location of consumed input with the output
554  /// as a tuple. Functions similarly to [Parser::span] except it
555  /// returns the parser output as well.
556  ///
557  /// This can be useful especially in cases where the output is not the same type
558  /// as the input, or the input is a user defined type.
559  ///
560  /// Returned tuple is of the format `(produced output, consumed input)`.
561  ///
562  /// # Example
563  ///
564  /// ```rust
565  /// # use nom8::prelude::*;
566  /// # use nom8::{Err,error::ErrorKind, IResult, input::Slice};
567  /// use nom8::input::Located;
568  /// use nom8::character::alpha1;
569  /// use nom8::bytes::tag;
570  /// use nom8::sequence::separated_pair;
571  ///
572  /// fn inner_parser(input: Located<&str>) -> IResult<Located<&str>, bool> {
573  ///     tag("1234").value(true).parse(input)
574  /// }
575  ///
576  /// # fn main() {
577  ///
578  /// let mut consumed_parser = separated_pair(alpha1.value(1).with_span(), ',', alpha1.value(2).with_span());
579  ///
580  /// assert_eq!(consumed_parser.parse(Located::new("abcd,efgh")).finish(), Ok(((1, 0..4), (2, 5..9))));
581  /// assert_eq!(consumed_parser.parse(Located::new("abcd;")),Err(Err::Error((Located::new("abcd;").slice(4..), ErrorKind::OneOf))));
582  ///
583  /// // the second output (representing the consumed input)
584  /// // should be the same as that of the `span` parser.
585  /// let mut recognize_parser = inner_parser.span();
586  /// let mut consumed_parser = inner_parser.with_span().map(|(output, consumed)| consumed);
587  ///
588  /// assert_eq!(recognize_parser.parse(Located::new("1234")), consumed_parser.parse(Located::new("1234")));
589  /// assert_eq!(recognize_parser.parse(Located::new("abcd")), consumed_parser.parse(Located::new("abcd")));
590  /// # }
591  /// ```
592  fn with_span(self) -> WithSpan<Self, O>
593  where
594    Self: core::marker::Sized,
595    I: Location + Clone,
596  {
597    WithSpan::new(self)
598  }
599
600  /// Maps a function over the result of a parser
601  ///
602  /// # Example
603  ///
604  /// ```rust
605  /// use nom8::{Err,error::ErrorKind, IResult,Parser};
606  /// use nom8::character::digit1;
607  /// # fn main() {
608  ///
609  /// let mut parser = digit1.map(|s: &str| s.len());
610  ///
611  /// // the parser will count how many characters were returned by digit1
612  /// assert_eq!(parser.parse("123456"), Ok(("", 6)));
613  ///
614  /// // this will fail if digit1 fails
615  /// assert_eq!(parser.parse("abc"), Err(Err::Error(("abc", ErrorKind::Digit))));
616  /// # }
617  /// ```
618  fn map<G, O2>(self, g: G) -> Map<Self, G, O>
619  where
620    G: Fn(O) -> O2,
621    Self: core::marker::Sized,
622  {
623    Map::new(self, g)
624  }
625
626  /// Applies a function returning a `Result` over the result of a parser.
627  ///
628  /// # Example
629  ///
630  /// ```rust
631  /// # use nom8::{Err,error::ErrorKind, IResult, Parser};
632  /// use nom8::character::digit1;
633  /// # fn main() {
634  ///
635  /// let mut parse = digit1.map_res(|s: &str| s.parse::<u8>());
636  ///
637  /// // the parser will convert the result of digit1 to a number
638  /// assert_eq!(parse.parse("123"), Ok(("", 123)));
639  ///
640  /// // this will fail if digit1 fails
641  /// assert_eq!(parse.parse("abc"), Err(Err::Error(("abc", ErrorKind::Digit))));
642  ///
643  /// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`)
644  /// assert_eq!(parse.parse("123456"), Err(Err::Error(("123456", ErrorKind::MapRes))));
645  /// # }
646  /// ```
647  fn map_res<G, O2, E2>(self, g: G) -> MapRes<Self, G, O>
648  where
649    Self: core::marker::Sized,
650    G: FnMut(O) -> Result<O2, E2>,
651  {
652    MapRes::new(self, g)
653  }
654
655  /// Applies a function returning an `Option` over the result of a parser.
656  ///
657  /// # Example
658  ///
659  /// ```rust
660  /// # use nom8::{Err,error::ErrorKind, IResult, Parser};
661  /// use nom8::character::digit1;
662  /// # fn main() {
663  ///
664  /// let mut parse = digit1.map_opt(|s: &str| s.parse::<u8>().ok());
665  ///
666  /// // the parser will convert the result of digit1 to a number
667  /// assert_eq!(parse.parse("123"), Ok(("", 123)));
668  ///
669  /// // this will fail if digit1 fails
670  /// assert_eq!(parse.parse("abc"), Err(Err::Error(("abc", ErrorKind::Digit))));
671  ///
672  /// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`)
673  /// assert_eq!(parse.parse("123456"), Err(Err::Error(("123456", ErrorKind::MapOpt))));
674  /// # }
675  /// ```
676  fn map_opt<G, O2>(self, g: G) -> MapOpt<Self, G, O>
677  where
678    Self: core::marker::Sized,
679    G: FnMut(O) -> Option<O2>,
680  {
681    MapOpt::new(self, g)
682  }
683
684  /// Creates a second parser from the output of the first one, then apply over the rest of the input
685  ///
686  /// # Example
687  ///
688  /// ```rust
689  /// # use nom8::{Err,error::ErrorKind, IResult, Parser};
690  /// use nom8::bytes::take;
691  /// use nom8::number::u8;
692  /// # fn main() {
693  ///
694  /// let mut length_data = u8.flat_map(take);
695  ///
696  /// assert_eq!(length_data.parse(&[2, 0, 1, 2][..]), Ok((&[2][..], &[0, 1][..])));
697  /// assert_eq!(length_data.parse(&[4, 0, 1, 2][..]), Err(Err::Error((&[0, 1, 2][..], ErrorKind::Eof))));
698  /// # }
699  /// ```
700  fn flat_map<G, H, O2>(self, g: G) -> FlatMap<Self, G, O>
701  where
702    G: FnMut(O) -> H,
703    H: Parser<I, O2, E>,
704    Self: core::marker::Sized,
705  {
706    FlatMap::new(self, g)
707  }
708
709  /// Applies a second parser over the output of the first one
710  ///
711  /// # Example
712  ///
713  /// ```rust
714  /// # use nom8::{Err,error::ErrorKind, IResult, Parser};
715  /// use nom8::character::digit1;
716  /// use nom8::bytes::take;
717  /// # fn main() {
718  ///
719  /// let mut digits = take(5u8).and_then(digit1);
720  ///
721  /// assert_eq!(digits.parse("12345"), Ok(("", "12345")));
722  /// assert_eq!(digits.parse("123ab"), Ok(("", "123")));
723  /// assert_eq!(digits.parse("123"), Err(Err::Error(("123", ErrorKind::Eof))));
724  /// # }
725  /// ```
726  fn and_then<G, O2>(self, g: G) -> AndThen<Self, G, O>
727  where
728    G: Parser<O, O2, E>,
729    Self: core::marker::Sized,
730  {
731    AndThen::new(self, g)
732  }
733
734  /// Returns the result of the child parser if it satisfies a verification function.
735  ///
736  /// The verification function takes as argument a reference to the output of the
737  /// parser.
738  ///
739  /// # Example
740  ///
741  /// ```rust
742  /// # use nom8::{Err,error::ErrorKind, IResult, Parser};
743  /// # use nom8::character::alpha1;
744  /// # fn main() {
745  ///
746  /// let mut parser = alpha1.verify(|s: &str| s.len() == 4);
747  ///
748  /// assert_eq!(parser.parse("abcd"), Ok(("", "abcd")));
749  /// assert_eq!(parser.parse("abcde"), Err(Err::Error(("abcde", ErrorKind::Verify))));
750  /// assert_eq!(parser.parse("123abcd;"),Err(Err::Error(("123abcd;", ErrorKind::Alpha))));
751  /// # }
752  /// ```
753  fn verify<G, O2: ?Sized>(self, second: G) -> Verify<Self, G, O2>
754  where
755    Self: core::marker::Sized,
756    G: Fn(&O2) -> bool,
757  {
758    Verify::new(self, second)
759  }
760
761  /// If parsing fails, add context to the error
762  ///
763  /// This is used mainly to add user friendly information
764  /// to errors when backtracking through a parse tree.
765  fn context<C>(self, context: C) -> Context<Self, O, C>
766  where
767    Self: core::marker::Sized,
768    C: Clone,
769    E: ContextError<I, C>,
770  {
771    Context::new(self, context)
772  }
773
774  /// Transforms [`Incomplete`][crate::Err::Incomplete] into [`Error`][crate::Err::Error]
775  ///
776  /// # Example
777  ///
778  /// ```rust
779  /// # use nom8::{Err,error::ErrorKind, IResult, input::Streaming, Parser};
780  /// # use nom8::bytes::take;
781  /// # fn main() {
782  ///
783  /// let mut parser = take(5u8).complete();
784  ///
785  /// assert_eq!(parser.parse(Streaming("abcdefg")), Ok((Streaming("fg"), "abcde")));
786  /// assert_eq!(parser.parse(Streaming("abcd")), Err(Err::Error((Streaming("abcd"), ErrorKind::Complete))));
787  /// # }
788  /// ```
789  fn complete(self) -> Complete<Self>
790  where
791    Self: core::marker::Sized,
792  {
793    Complete::new(self)
794  }
795
796  /// Convert the parser's error to another type using [`std::convert::From`]
797  fn err_into<E2: From<E>>(self) -> ErrInto<Self, E, E2>
798  where
799    Self: core::marker::Sized,
800  {
801    ErrInto::new(self)
802  }
803
804  /// Prints a message and the input if the parser fails.
805  ///
806  /// The message prints the `Error` or `Incomplete`
807  /// and the parser's calling code.
808  ///
809  /// It also displays the input in hexdump format
810  ///
811  /// ```rust
812  /// use nom8::prelude::*;
813  /// use nom8::{IResult, bytes::tag};
814  ///
815  /// fn f(i: &[u8]) -> IResult<&[u8], &[u8]> {
816  ///   tag("abcd").dbg_err("alpha tag").parse(i)
817  /// }
818  ///
819  /// let a = &b"efghijkl"[..];
820  /// f(a);
821  /// ```
822  ///
823  /// Will print the following message:
824  /// ```console
825  /// alpha tag: Error(Position(0, [101, 102, 103, 104, 105, 106, 107, 108])) at:
826  /// 00000000        65 66 67 68 69 6a 6b 6c         efghijkl
827  /// ```
828  #[cfg(feature = "std")]
829  fn dbg_err<C>(self, context: C) -> DbgErr<Self, O, C>
830  where
831    C: std::fmt::Display,
832    Self: core::marker::Sized,
833  {
834    DbgErr::new(self, context)
835  }
836
837  /// Applies a second parser after the first one, return their results as a tuple
838  ///
839  /// **WARNING:** Deprecated, replaced with [`nom8::sequence::tuple`][crate::sequence::tuple]
840  #[deprecated(since = "8.0.0", note = "Replaced with `nom8::sequence::tuple")]
841  fn and<G, O2>(self, g: G) -> And<Self, G>
842  where
843    G: Parser<I, O2, E>,
844    Self: core::marker::Sized,
845  {
846    And::new(self, g)
847  }
848
849  /// Applies a second parser over the input if the first one failed
850  ///
851  /// **WARNING:** Deprecated, replaced with [`nom8::branch::alt`][crate::branch::alt]
852  #[deprecated(since = "8.0.0", note = "Replaced with `nom8::branch::alt")]
853  fn or<G>(self, g: G) -> Or<Self, G>
854  where
855    G: Parser<I, O, E>,
856    Self: core::marker::Sized,
857  {
858    Or::new(self, g)
859  }
860}
861
862impl<'a, I, O, E, F> Parser<I, O, E> for F
863where
864  F: FnMut(I) -> IResult<I, O, E> + 'a,
865{
866  fn parse(&mut self, i: I) -> IResult<I, O, E> {
867    self(i)
868  }
869}
870
871/// This is a shortcut for [`one_of`][crate::bytes::one_of].
872///
873/// # Example
874///
875/// ```
876/// # use nom8::prelude::*;
877/// # use nom8::{Err, error::{ErrorKind, Error}};
878/// fn parser(i: &[u8]) -> IResult<&[u8], u8> {
879///     b'a'.parse(i)
880/// }
881/// assert_eq!(parser(&b"abc"[..]), Ok((&b"bc"[..], b'a')));
882/// assert_eq!(parser(&b" abc"[..]), Err(Err::Error(Error::new(&b" abc"[..], ErrorKind::OneOf))));
883/// assert_eq!(parser(&b"bc"[..]), Err(Err::Error(Error::new(&b"bc"[..], ErrorKind::OneOf))));
884/// assert_eq!(parser(&b""[..]), Err(Err::Error(Error::new(&b""[..], ErrorKind::OneOf))));
885/// ```
886impl<I, E> Parser<I, u8, E> for u8
887where
888  I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength + InputIsStreaming<false>,
889  E: ParseError<I>,
890{
891  fn parse(&mut self, i: I) -> IResult<I, u8, E> {
892    crate::bytes::one_of(*self).parse(i)
893  }
894}
895
896/// This is a shortcut for [`one_of`][crate::bytes::one_of].
897///
898/// # Example
899///
900/// ```
901/// # use nom8::prelude::*;
902/// # use nom8::{Err, error::{ErrorKind, Error}};
903/// fn parser(i: &str) -> IResult<&str, char> {
904///     'a'.parse(i)
905/// }
906/// assert_eq!(parser("abc"), Ok(("bc", 'a')));
907/// assert_eq!(parser(" abc"), Err(Err::Error(Error::new(" abc", ErrorKind::OneOf))));
908/// assert_eq!(parser("bc"), Err(Err::Error(Error::new("bc", ErrorKind::OneOf))));
909/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::OneOf))));
910/// ```
911impl<I, E> Parser<I, <I as InputIter>::Item, E> for char
912where
913  I: Slice<RangeFrom<usize>> + InputIter + InputLength + InputIsStreaming<false>,
914  <I as InputIter>::Item: AsChar + Copy,
915  E: ParseError<I>,
916{
917  fn parse(&mut self, i: I) -> IResult<I, <I as InputIter>::Item, E> {
918    crate::bytes::one_of(*self).parse(i)
919  }
920}
921
922/// This is a shortcut for [`tag`][crate::bytes::tag].
923///
924/// # Example
925/// ```rust
926/// # use nom8::prelude::*;
927/// # use nom8::{Err, error::{Error, ErrorKind}, Needed};
928/// # use nom8::branch::alt;
929/// # use nom8::bytes::take;
930///
931/// fn parser(s: &[u8]) -> IResult<&[u8], &[u8]> {
932///   alt((&"Hello"[..], take(5usize))).parse(s)
933/// }
934///
935/// assert_eq!(parser(&b"Hello, World!"[..]), Ok((&b", World!"[..], &b"Hello"[..])));
936/// assert_eq!(parser(&b"Something"[..]), Ok((&b"hing"[..], &b"Somet"[..])));
937/// assert_eq!(parser(&b"Some"[..]), Err(Err::Error(Error::new(&b"Some"[..], ErrorKind::Eof))));
938/// assert_eq!(parser(&b""[..]), Err(Err::Error(Error::new(&b""[..], ErrorKind::Eof))));
939/// ```
940impl<'s, I, E: ParseError<I>> Parser<I, <I as IntoOutput>::Output, E> for &'s [u8]
941where
942  I: InputTake + InputLength + Compare<&'s [u8]> + InputIsStreaming<false>,
943  I: IntoOutput,
944{
945  fn parse(&mut self, i: I) -> IResult<I, <I as IntoOutput>::Output, E> {
946    crate::bytes::tag(*self).parse(i)
947  }
948}
949
950/// This is a shortcut for [`tag`][crate::bytes::tag].
951///
952/// # Example
953/// ```rust
954/// # use nom8::prelude::*;
955/// # use nom8::{Err, error::{Error, ErrorKind}, Needed};
956/// # use nom8::branch::alt;
957/// # use nom8::bytes::take;
958///
959/// fn parser(s: &[u8]) -> IResult<&[u8], &[u8]> {
960///   alt((b"Hello", take(5usize))).parse(s)
961/// }
962///
963/// assert_eq!(parser(&b"Hello, World!"[..]), Ok((&b", World!"[..], &b"Hello"[..])));
964/// assert_eq!(parser(&b"Something"[..]), Ok((&b"hing"[..], &b"Somet"[..])));
965/// assert_eq!(parser(&b"Some"[..]), Err(Err::Error(Error::new(&b"Some"[..], ErrorKind::Eof))));
966/// assert_eq!(parser(&b""[..]), Err(Err::Error(Error::new(&b""[..], ErrorKind::Eof))));
967/// ```
968impl<'s, I, E: ParseError<I>, const N: usize> Parser<I, <I as IntoOutput>::Output, E>
969  for &'s [u8; N]
970where
971  I: InputTake + InputLength + Compare<&'s [u8; N]> + InputIsStreaming<false>,
972  I: IntoOutput,
973{
974  fn parse(&mut self, i: I) -> IResult<I, <I as IntoOutput>::Output, E> {
975    crate::bytes::tag(*self).parse(i)
976  }
977}
978
979/// This is a shortcut for [`tag`][crate::bytes::tag].
980///
981/// # Example
982/// ```rust
983/// # use nom8::prelude::*;
984/// # use nom8::{Err, error::{Error, ErrorKind}, Needed};
985/// # use nom8::branch::alt;
986/// # use nom8::bytes::take;
987///
988/// fn parser(s: &str) -> IResult<&str, &str> {
989///   alt(("Hello", take(5usize))).parse(s)
990/// }
991///
992/// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello")));
993/// assert_eq!(parser("Something"), Ok(("hing", "Somet")));
994/// assert_eq!(parser("Some"), Err(Err::Error(Error::new("Some", ErrorKind::Eof))));
995/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Eof))));
996/// ```
997impl<'s, I, E: ParseError<I>> Parser<I, <I as IntoOutput>::Output, E> for &'s str
998where
999  I: InputTake + InputLength + Compare<&'s str> + InputIsStreaming<false>,
1000  I: IntoOutput,
1001{
1002  fn parse(&mut self, i: I) -> IResult<I, <I as IntoOutput>::Output, E> {
1003    crate::bytes::tag(*self).parse(i)
1004  }
1005}
1006
1007impl<I, E: ParseError<I>> Parser<I, (), E> for () {
1008  fn parse(&mut self, i: I) -> IResult<I, (), E> {
1009    Ok((i, ()))
1010  }
1011}
1012
1013macro_rules! impl_parser_for_tuple {
1014  ($($parser:ident $output:ident),+) => (
1015    #[allow(non_snake_case)]
1016    impl<I, $($output),+, E: ParseError<I>, $($parser),+> Parser<I, ($($output),+,), E> for ($($parser),+,)
1017    where
1018      $($parser: Parser<I, $output, E>),+
1019    {
1020      fn parse(&mut self, i: I) -> IResult<I, ($($output),+,), E> {
1021        let ($(ref mut $parser),+,) = *self;
1022
1023        $(let(i, $output) = $parser.parse(i)?;)+
1024
1025        Ok((i, ($($output),+,)))
1026      }
1027    }
1028  )
1029}
1030
1031macro_rules! impl_parser_for_tuples {
1032    ($parser1:ident $output1:ident, $($parser:ident $output:ident),+) => {
1033        impl_parser_for_tuples!(__impl $parser1 $output1; $($parser $output),+);
1034    };
1035    (__impl $($parser:ident $output:ident),+; $parser1:ident $output1:ident $(,$parser2:ident $output2:ident)*) => {
1036        impl_parser_for_tuple!($($parser $output),+);
1037        impl_parser_for_tuples!(__impl $($parser $output),+, $parser1 $output1; $($parser2 $output2),*);
1038    };
1039    (__impl $($parser:ident $output:ident),+;) => {
1040        impl_parser_for_tuple!($($parser $output),+);
1041    }
1042}
1043
1044impl_parser_for_tuples!(
1045  P1 O1,
1046  P2 O2,
1047  P3 O3,
1048  P4 O4,
1049  P5 O5,
1050  P6 O6,
1051  P7 O7,
1052  P8 O8,
1053  P9 O9,
1054  P10 O10,
1055  P11 O11,
1056  P12 O12,
1057  P13 O13,
1058  P14 O14,
1059  P15 O15,
1060  P16 O16,
1061  P17 O17,
1062  P18 O18,
1063  P19 O19,
1064  P20 O20,
1065  P21 O21
1066);
1067
1068#[cfg(feature = "alloc")]
1069use alloc::boxed::Box;
1070
1071#[cfg(feature = "alloc")]
1072impl<'a, I, O, E> Parser<I, O, E> for Box<dyn Parser<I, O, E> + 'a> {
1073  fn parse(&mut self, input: I) -> IResult<I, O, E> {
1074    (**self).parse(input)
1075  }
1076}
1077
1078#[cfg(test)]
1079mod tests {
1080  use super::*;
1081  use crate::bytes::{tag, take};
1082  use crate::error::ErrorKind;
1083  use crate::input::Streaming;
1084  use crate::number::be_u16;
1085
1086  #[doc(hidden)]
1087  #[macro_export]
1088  macro_rules! assert_size (
1089    ($t:ty, $sz:expr) => (
1090      assert!(crate::lib::std::mem::size_of::<$t>() <= $sz, "{} <= {} failed", crate::lib::std::mem::size_of::<$t>(), $sz);
1091    );
1092  );
1093
1094  #[test]
1095  #[cfg(target_pointer_width = "64")]
1096  fn size_test() {
1097    assert_size!(IResult<&[u8], &[u8], (&[u8], u32)>, 40);
1098    assert_size!(IResult<&str, &str, u32>, 40);
1099    assert_size!(Needed, 8);
1100    assert_size!(Err<u32>, 16);
1101    assert_size!(ErrorKind, 1);
1102  }
1103
1104  #[test]
1105  fn err_map_test() {
1106    let e = Err::Error(1);
1107    assert_eq!(e.map(|v| v + 1), Err::Error(2));
1108  }
1109
1110  #[test]
1111  fn single_element_tuples() {
1112    use crate::character::alpha1;
1113    use crate::{error::ErrorKind, Err};
1114
1115    let mut parser = (alpha1,);
1116    assert_eq!(parser.parse("abc123def"), Ok(("123def", ("abc",))));
1117    assert_eq!(
1118      parser.parse("123def"),
1119      Err(Err::Error(("123def", ErrorKind::Alpha)))
1120    );
1121  }
1122
1123  #[test]
1124  fn tuple_test() {
1125    fn tuple_3(i: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, (u16, &[u8], &[u8])> {
1126      (be_u16, take(3u8), tag("fg")).parse(i)
1127    }
1128
1129    assert_eq!(
1130      tuple_3(Streaming(&b"abcdefgh"[..])),
1131      Ok((Streaming(&b"h"[..]), (0x6162u16, &b"cde"[..], &b"fg"[..])))
1132    );
1133    assert_eq!(
1134      tuple_3(Streaming(&b"abcd"[..])),
1135      Err(Err::Incomplete(Needed::new(1)))
1136    );
1137    assert_eq!(
1138      tuple_3(Streaming(&b"abcde"[..])),
1139      Err(Err::Incomplete(Needed::new(2)))
1140    );
1141    assert_eq!(
1142      tuple_3(Streaming(&b"abcdejk"[..])),
1143      Err(Err::Error(error_position!(
1144        Streaming(&b"jk"[..]),
1145        ErrorKind::Tag
1146      )))
1147    );
1148  }
1149
1150  #[test]
1151  fn unit_type() {
1152    fn parser(i: &str) -> IResult<&str, ()> {
1153      ().parse(i)
1154    }
1155    assert_eq!(parser.parse("abxsbsh"), Ok(("abxsbsh", ())));
1156    assert_eq!(parser.parse("sdfjakdsas"), Ok(("sdfjakdsas", ())));
1157    assert_eq!(parser.parse(""), Ok(("", ())));
1158  }
1159}