use self::Needed::*;
use crate::combinator::*;
#[cfg(feature = "std")]
use crate::error::DbgErr;
use crate::error::{self, Context, ContextError, ErrorKind, ParseError};
use crate::input::InputIsStreaming;
use crate::input::*;
use crate::lib::std::fmt;
use crate::lib::std::ops::RangeFrom;
use core::num::NonZeroUsize;
pub type IResult<I, O, E = error::Error<I>> = Result<(I, O), Err<E>>;
pub trait FinishIResult<I, O, E> {
fn finish(self) -> Result<O, E>;
fn finish_err(self) -> Result<(I, O), E>;
}
impl<I, O, E> FinishIResult<I, O, E> for IResult<I, O, E>
where
I: crate::input::InputLength,
I: crate::input::IntoOutput,
I: InputIsStreaming<false>,
I: Clone,
E: crate::error::ParseError<I>,
{
fn finish(self) -> Result<O, E> {
let (i, o) = self.finish_err()?;
crate::combinator::eof(i).finish_err()?;
Ok(o)
}
fn finish_err(self) -> Result<(I, O), E> {
match self {
Ok(res) => Ok(res),
Err(Err::Error(e)) | Err(Err::Failure(e)) => Err(e),
Err(Err::Incomplete(_)) => {
panic!("`InputIsStreaming<false>` conflicts with `Err(Err::Incomplete(_))`")
}
}
}
}
#[doc(hidden)]
#[deprecated(
since = "8.0.0",
note = "Replaced with `FinishIResult` which is available via `nom8::prelude`"
)]
pub trait Finish<I, O, E> {
#[deprecated(
since = "8.0.0",
note = "Replaced with `FinishIResult::finish_err` which is available via `nom8::prelude`"
)]
fn finish(self) -> Result<(I, O), E>;
}
#[allow(deprecated)]
impl<I, O, E> Finish<I, O, E> for IResult<I, O, E> {
fn finish(self) -> Result<(I, O), E> {
match self {
Ok(res) => Ok(res),
Err(Err::Error(e)) | Err(Err::Failure(e)) => Err(e),
Err(Err::Incomplete(_)) => {
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")
}
}
}
}
pub trait IntoOutputIResult<I, O, E> {
fn into_output(self) -> IResult<I, O, E>;
}
impl<I, E> IntoOutputIResult<I, <I as crate::input::IntoOutput>::Output, E> for IResult<I, I, E>
where
I: crate::input::IntoOutput,
{
fn into_output(self) -> IResult<I, <I as crate::input::IntoOutput>::Output, E> {
self.map(|(i, o)| (i, o.into_output()))
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
pub enum Needed {
Unknown,
Size(NonZeroUsize),
}
impl Needed {
pub fn new(s: usize) -> Self {
match NonZeroUsize::new(s) {
Some(sz) => Needed::Size(sz),
None => Needed::Unknown,
}
}
pub fn is_known(&self) -> bool {
*self != Unknown
}
#[inline]
pub fn map<F: Fn(NonZeroUsize) -> usize>(self, f: F) -> Needed {
match self {
Unknown => Unknown,
Size(n) => Needed::new(f(n)),
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
pub enum Err<E> {
Incomplete(Needed),
Error(E),
Failure(E),
}
impl<E> Err<E> {
pub fn is_incomplete(&self) -> bool {
if let Err::Incomplete(_) = self {
true
} else {
false
}
}
pub fn map<E2, F>(self, f: F) -> Err<E2>
where
F: FnOnce(E) -> E2,
{
match self {
Err::Incomplete(n) => Err::Incomplete(n),
Err::Failure(t) => Err::Failure(f(t)),
Err::Error(t) => Err::Error(f(t)),
}
}
pub fn convert<F>(e: Err<F>) -> Self
where
E: From<F>,
{
e.map(crate::lib::std::convert::Into::into)
}
}
impl<T> Err<(T, ErrorKind)> {
pub fn map_input<U, F>(self, f: F) -> Err<(U, ErrorKind)>
where
F: FnOnce(T) -> U,
{
match self {
Err::Incomplete(n) => Err::Incomplete(n),
Err::Failure((input, k)) => Err::Failure((f(input), k)),
Err::Error((input, k)) => Err::Error((f(input), k)),
}
}
}
impl<T> Err<error::Error<T>> {
pub fn map_input<U, F>(self, f: F) -> Err<error::Error<U>>
where
F: FnOnce(T) -> U,
{
match self {
Err::Incomplete(n) => Err::Incomplete(n),
Err::Failure(error::Error { input, code }) => Err::Failure(error::Error {
input: f(input),
code,
}),
Err::Error(error::Error { input, code }) => Err::Error(error::Error {
input: f(input),
code,
}),
}
}
}
#[cfg(feature = "alloc")]
use crate::lib::std::{borrow::ToOwned, string::String, vec::Vec};
impl Err<(&[u8], ErrorKind)> {
#[cfg(feature = "alloc")]
pub fn to_owned(self) -> Err<(Vec<u8>, ErrorKind)> {
self.map_input(ToOwned::to_owned)
}
}
impl Err<(&str, ErrorKind)> {
#[cfg(feature = "alloc")]
pub fn to_owned(self) -> Err<(String, ErrorKind)> {
self.map_input(ToOwned::to_owned)
}
}
impl Err<error::Error<&[u8]>> {
#[cfg(feature = "alloc")]
pub fn to_owned(self) -> Err<error::Error<Vec<u8>>> {
self.map_input(ToOwned::to_owned)
}
}
impl Err<error::Error<&str>> {
#[cfg(feature = "alloc")]
pub fn to_owned(self) -> Err<error::Error<String>> {
self.map_input(ToOwned::to_owned)
}
}
impl<E: Eq> Eq for Err<E> {}
impl<E> fmt::Display for Err<E>
where
E: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Err::Incomplete(Needed::Size(u)) => write!(f, "Parsing requires {} bytes/chars", u),
Err::Incomplete(Needed::Unknown) => write!(f, "Parsing requires more data"),
Err::Failure(c) => write!(f, "Parsing Failure: {:?}", c),
Err::Error(c) => write!(f, "Parsing Error: {:?}", c),
}
}
}
#[cfg(feature = "std")]
use std::error::Error;
#[cfg(feature = "std")]
impl<E> Error for Err<E>
where
E: fmt::Debug,
{
fn source(&self) -> Option<&(dyn Error + 'static)> {
None }
}
pub trait Parser<I, O, E> {
fn parse(&mut self, input: I) -> IResult<I, O, E>;
fn by_ref(&mut self) -> ByRef<Self>
where
Self: core::marker::Sized,
{
ByRef::new(self)
}
fn value<O2>(self, val: O2) -> Value<Self, O, O2>
where
Self: core::marker::Sized,
O2: Clone,
{
Value::new(self, val)
}
fn output_into<O2: From<O>>(self) -> OutputInto<Self, O, O2>
where
Self: core::marker::Sized,
{
OutputInto::new(self)
}
fn recognize(self) -> Recognize<Self, O>
where
Self: core::marker::Sized,
{
Recognize::new(self)
}
fn with_recognized(self) -> WithRecognized<Self, O>
where
Self: core::marker::Sized,
{
WithRecognized::new(self)
}
fn span(self) -> Span<Self, O>
where
Self: core::marker::Sized,
I: Location + Clone,
{
Span::new(self)
}
fn with_span(self) -> WithSpan<Self, O>
where
Self: core::marker::Sized,
I: Location + Clone,
{
WithSpan::new(self)
}
fn map<G, O2>(self, g: G) -> Map<Self, G, O>
where
G: Fn(O) -> O2,
Self: core::marker::Sized,
{
Map::new(self, g)
}
fn map_res<G, O2, E2>(self, g: G) -> MapRes<Self, G, O>
where
Self: core::marker::Sized,
G: FnMut(O) -> Result<O2, E2>,
{
MapRes::new(self, g)
}
fn map_opt<G, O2>(self, g: G) -> MapOpt<Self, G, O>
where
Self: core::marker::Sized,
G: FnMut(O) -> Option<O2>,
{
MapOpt::new(self, g)
}
fn flat_map<G, H, O2>(self, g: G) -> FlatMap<Self, G, O>
where
G: FnMut(O) -> H,
H: Parser<I, O2, E>,
Self: core::marker::Sized,
{
FlatMap::new(self, g)
}
fn and_then<G, O2>(self, g: G) -> AndThen<Self, G, O>
where
G: Parser<O, O2, E>,
Self: core::marker::Sized,
{
AndThen::new(self, g)
}
fn verify<G, O2: ?Sized>(self, second: G) -> Verify<Self, G, O2>
where
Self: core::marker::Sized,
G: Fn(&O2) -> bool,
{
Verify::new(self, second)
}
fn context<C>(self, context: C) -> Context<Self, O, C>
where
Self: core::marker::Sized,
C: Clone,
E: ContextError<I, C>,
{
Context::new(self, context)
}
fn complete(self) -> Complete<Self>
where
Self: core::marker::Sized,
{
Complete::new(self)
}
fn err_into<E2: From<E>>(self) -> ErrInto<Self, E, E2>
where
Self: core::marker::Sized,
{
ErrInto::new(self)
}
#[cfg(feature = "std")]
fn dbg_err<C>(self, context: C) -> DbgErr<Self, O, C>
where
C: std::fmt::Display,
Self: core::marker::Sized,
{
DbgErr::new(self, context)
}
#[deprecated(since = "8.0.0", note = "Replaced with `nom8::sequence::tuple")]
fn and<G, O2>(self, g: G) -> And<Self, G>
where
G: Parser<I, O2, E>,
Self: core::marker::Sized,
{
And::new(self, g)
}
#[deprecated(since = "8.0.0", note = "Replaced with `nom8::branch::alt")]
fn or<G>(self, g: G) -> Or<Self, G>
where
G: Parser<I, O, E>,
Self: core::marker::Sized,
{
Or::new(self, g)
}
}
impl<'a, I, O, E, F> Parser<I, O, E> for F
where
F: FnMut(I) -> IResult<I, O, E> + 'a,
{
fn parse(&mut self, i: I) -> IResult<I, O, E> {
self(i)
}
}
impl<I, E> Parser<I, u8, E> for u8
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength + InputIsStreaming<false>,
E: ParseError<I>,
{
fn parse(&mut self, i: I) -> IResult<I, u8, E> {
crate::bytes::one_of(*self).parse(i)
}
}
impl<I, E> Parser<I, <I as InputIter>::Item, E> for char
where
I: Slice<RangeFrom<usize>> + InputIter + InputLength + InputIsStreaming<false>,
<I as InputIter>::Item: AsChar + Copy,
E: ParseError<I>,
{
fn parse(&mut self, i: I) -> IResult<I, <I as InputIter>::Item, E> {
crate::bytes::one_of(*self).parse(i)
}
}
impl<'s, I, E: ParseError<I>> Parser<I, <I as IntoOutput>::Output, E> for &'s [u8]
where
I: InputTake + InputLength + Compare<&'s [u8]> + InputIsStreaming<false>,
I: IntoOutput,
{
fn parse(&mut self, i: I) -> IResult<I, <I as IntoOutput>::Output, E> {
crate::bytes::tag(*self).parse(i)
}
}
impl<'s, I, E: ParseError<I>, const N: usize> Parser<I, <I as IntoOutput>::Output, E>
for &'s [u8; N]
where
I: InputTake + InputLength + Compare<&'s [u8; N]> + InputIsStreaming<false>,
I: IntoOutput,
{
fn parse(&mut self, i: I) -> IResult<I, <I as IntoOutput>::Output, E> {
crate::bytes::tag(*self).parse(i)
}
}
impl<'s, I, E: ParseError<I>> Parser<I, <I as IntoOutput>::Output, E> for &'s str
where
I: InputTake + InputLength + Compare<&'s str> + InputIsStreaming<false>,
I: IntoOutput,
{
fn parse(&mut self, i: I) -> IResult<I, <I as IntoOutput>::Output, E> {
crate::bytes::tag(*self).parse(i)
}
}
impl<I, E: ParseError<I>> Parser<I, (), E> for () {
fn parse(&mut self, i: I) -> IResult<I, (), E> {
Ok((i, ()))
}
}
macro_rules! impl_parser_for_tuple {
($($parser:ident $output:ident),+) => (
#[allow(non_snake_case)]
impl<I, $($output),+, E: ParseError<I>, $($parser),+> Parser<I, ($($output),+,), E> for ($($parser),+,)
where
$($parser: Parser<I, $output, E>),+
{
fn parse(&mut self, i: I) -> IResult<I, ($($output),+,), E> {
let ($(ref mut $parser),+,) = *self;
$(let(i, $output) = $parser.parse(i)?;)+
Ok((i, ($($output),+,)))
}
}
)
}
macro_rules! impl_parser_for_tuples {
($parser1:ident $output1:ident, $($parser:ident $output:ident),+) => {
impl_parser_for_tuples!(__impl $parser1 $output1; $($parser $output),+);
};
(__impl $($parser:ident $output:ident),+; $parser1:ident $output1:ident $(,$parser2:ident $output2:ident)*) => {
impl_parser_for_tuple!($($parser $output),+);
impl_parser_for_tuples!(__impl $($parser $output),+, $parser1 $output1; $($parser2 $output2),*);
};
(__impl $($parser:ident $output:ident),+;) => {
impl_parser_for_tuple!($($parser $output),+);
}
}
impl_parser_for_tuples!(
P1 O1,
P2 O2,
P3 O3,
P4 O4,
P5 O5,
P6 O6,
P7 O7,
P8 O8,
P9 O9,
P10 O10,
P11 O11,
P12 O12,
P13 O13,
P14 O14,
P15 O15,
P16 O16,
P17 O17,
P18 O18,
P19 O19,
P20 O20,
P21 O21
);
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "alloc")]
impl<'a, I, O, E> Parser<I, O, E> for Box<dyn Parser<I, O, E> + 'a> {
fn parse(&mut self, input: I) -> IResult<I, O, E> {
(**self).parse(input)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::bytes::{tag, take};
use crate::error::ErrorKind;
use crate::input::Streaming;
use crate::number::be_u16;
#[doc(hidden)]
#[macro_export]
macro_rules! assert_size (
($t:ty, $sz:expr) => (
assert!(crate::lib::std::mem::size_of::<$t>() <= $sz, "{} <= {} failed", crate::lib::std::mem::size_of::<$t>(), $sz);
);
);
#[test]
#[cfg(target_pointer_width = "64")]
fn size_test() {
assert_size!(IResult<&[u8], &[u8], (&[u8], u32)>, 40);
assert_size!(IResult<&str, &str, u32>, 40);
assert_size!(Needed, 8);
assert_size!(Err<u32>, 16);
assert_size!(ErrorKind, 1);
}
#[test]
fn err_map_test() {
let e = Err::Error(1);
assert_eq!(e.map(|v| v + 1), Err::Error(2));
}
#[test]
fn single_element_tuples() {
use crate::character::alpha1;
use crate::{error::ErrorKind, Err};
let mut parser = (alpha1,);
assert_eq!(parser.parse("abc123def"), Ok(("123def", ("abc",))));
assert_eq!(
parser.parse("123def"),
Err(Err::Error(("123def", ErrorKind::Alpha)))
);
}
#[test]
fn tuple_test() {
fn tuple_3(i: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, (u16, &[u8], &[u8])> {
(be_u16, take(3u8), tag("fg")).parse(i)
}
assert_eq!(
tuple_3(Streaming(&b"abcdefgh"[..])),
Ok((Streaming(&b"h"[..]), (0x6162u16, &b"cde"[..], &b"fg"[..])))
);
assert_eq!(
tuple_3(Streaming(&b"abcd"[..])),
Err(Err::Incomplete(Needed::new(1)))
);
assert_eq!(
tuple_3(Streaming(&b"abcde"[..])),
Err(Err::Incomplete(Needed::new(2)))
);
assert_eq!(
tuple_3(Streaming(&b"abcdejk"[..])),
Err(Err::Error(error_position!(
Streaming(&b"jk"[..]),
ErrorKind::Tag
)))
);
}
#[test]
fn unit_type() {
fn parser(i: &str) -> IResult<&str, ()> {
().parse(i)
}
assert_eq!(parser.parse("abxsbsh"), Ok(("abxsbsh", ())));
assert_eq!(parser.parse("sdfjakdsas"), Ok(("sdfjakdsas", ())));
assert_eq!(parser.parse(""), Ok(("", ())));
}
}