From 2aa4a82499d4becd2284cdb482213d541b8804dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 16:29:10 +0200 Subject: Adding upstream version 86.0.1. Signed-off-by: Daniel Baumann --- third_party/rust/nom/src/bits/complete.rs | 75 ++ third_party/rust/nom/src/bits/macros.rs | 263 +++++ third_party/rust/nom/src/bits/mod.rs | 119 +++ third_party/rust/nom/src/bits/streaming.rs | 75 ++ third_party/rust/nom/src/branch/macros.rs | 940 ++++++++++++++++++ third_party/rust/nom/src/branch/mod.rs | 263 +++++ third_party/rust/nom/src/bytes/complete.rs | 658 ++++++++++++ third_party/rust/nom/src/bytes/macros.rs | 938 ++++++++++++++++++ third_party/rust/nom/src/bytes/mod.rs | 7 + third_party/rust/nom/src/bytes/streaming.rs | 645 ++++++++++++ third_party/rust/nom/src/character/complete.rs | 1082 ++++++++++++++++++++ third_party/rust/nom/src/character/macros.rs | 112 +++ third_party/rust/nom/src/character/mod.rs | 101 ++ third_party/rust/nom/src/character/streaming.rs | 1010 +++++++++++++++++++ third_party/rust/nom/src/combinator/macros.rs | 1201 ++++++++++++++++++++++ third_party/rust/nom/src/combinator/mod.rs | 859 ++++++++++++++++ third_party/rust/nom/src/error.rs | 776 +++++++++++++++ third_party/rust/nom/src/internal.rs | 185 ++++ third_party/rust/nom/src/lib.rs | 513 ++++++++++ third_party/rust/nom/src/methods.rs | 20 + third_party/rust/nom/src/multi/macros.rs | 988 +++++++++++++++++++ third_party/rust/nom/src/multi/mod.rs | 997 +++++++++++++++++++ third_party/rust/nom/src/number/complete.rs | 1198 ++++++++++++++++++++++ third_party/rust/nom/src/number/macros.rs | 265 +++++ third_party/rust/nom/src/number/mod.rs | 17 + third_party/rust/nom/src/number/streaming.rs | 1207 +++++++++++++++++++++++ third_party/rust/nom/src/regexp.rs | 1090 ++++++++++++++++++++ third_party/rust/nom/src/sequence/macros.rs | 860 ++++++++++++++++ third_party/rust/nom/src/sequence/mod.rs | 313 ++++++ third_party/rust/nom/src/str.rs | 494 ++++++++++ third_party/rust/nom/src/traits.rs | 1186 ++++++++++++++++++++++ third_party/rust/nom/src/util.rs | 214 ++++ third_party/rust/nom/src/whitespace.rs | 1077 ++++++++++++++++++++ 33 files changed, 19748 insertions(+) create mode 100644 third_party/rust/nom/src/bits/complete.rs create mode 100644 third_party/rust/nom/src/bits/macros.rs create mode 100644 third_party/rust/nom/src/bits/mod.rs create mode 100644 third_party/rust/nom/src/bits/streaming.rs create mode 100644 third_party/rust/nom/src/branch/macros.rs create mode 100644 third_party/rust/nom/src/branch/mod.rs create mode 100644 third_party/rust/nom/src/bytes/complete.rs create mode 100644 third_party/rust/nom/src/bytes/macros.rs create mode 100644 third_party/rust/nom/src/bytes/mod.rs create mode 100644 third_party/rust/nom/src/bytes/streaming.rs create mode 100644 third_party/rust/nom/src/character/complete.rs create mode 100644 third_party/rust/nom/src/character/macros.rs create mode 100644 third_party/rust/nom/src/character/mod.rs create mode 100644 third_party/rust/nom/src/character/streaming.rs create mode 100644 third_party/rust/nom/src/combinator/macros.rs create mode 100644 third_party/rust/nom/src/combinator/mod.rs create mode 100644 third_party/rust/nom/src/error.rs create mode 100644 third_party/rust/nom/src/internal.rs create mode 100644 third_party/rust/nom/src/lib.rs create mode 100644 third_party/rust/nom/src/methods.rs create mode 100644 third_party/rust/nom/src/multi/macros.rs create mode 100644 third_party/rust/nom/src/multi/mod.rs create mode 100644 third_party/rust/nom/src/number/complete.rs create mode 100644 third_party/rust/nom/src/number/macros.rs create mode 100644 third_party/rust/nom/src/number/mod.rs create mode 100644 third_party/rust/nom/src/number/streaming.rs create mode 100644 third_party/rust/nom/src/regexp.rs create mode 100644 third_party/rust/nom/src/sequence/macros.rs create mode 100644 third_party/rust/nom/src/sequence/mod.rs create mode 100644 third_party/rust/nom/src/str.rs create mode 100644 third_party/rust/nom/src/traits.rs create mode 100644 third_party/rust/nom/src/util.rs create mode 100644 third_party/rust/nom/src/whitespace.rs (limited to 'third_party/rust/nom/src') diff --git a/third_party/rust/nom/src/bits/complete.rs b/third_party/rust/nom/src/bits/complete.rs new file mode 100644 index 0000000000..f868ec2643 --- /dev/null +++ b/third_party/rust/nom/src/bits/complete.rs @@ -0,0 +1,75 @@ +//! bit level parsers +//! + +use crate::error::{ErrorKind, ParseError}; +use crate::internal::{Err, IResult}; +use crate::lib::std::ops::{AddAssign, RangeFrom, Shl, Shr, Div}; +use crate::traits::{InputIter, InputLength, Slice, ToUsize}; + +/// generates a parser taking `count` bits +pub fn take>(count: C) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E> +where + I: Slice> + InputIter + InputLength, + C: ToUsize, + O: From + AddAssign + Shl + Shr, +{ + let count = count.to_usize(); + move |(input, bit_offset): (I, usize)| { + if count == 0 { + Ok(((input, bit_offset), 0u8.into())) + } else { + let cnt = (count + bit_offset).div(8); + if input.input_len() * 8 < count + bit_offset { + Err(Err::Error(E::from_error_kind((input, bit_offset), ErrorKind::Eof))) + } else { + let mut acc:O = (0 as u8).into(); + let mut offset: usize = bit_offset; + let mut remaining: usize = count; + let mut end_offset: usize = 0; + + for byte in input.iter_elements().take(cnt + 1) { + if remaining == 0 { + break; + } + let val: O = if offset == 0 { + byte.into() + } else { + ((byte << offset) as u8 >> offset).into() + }; + + if remaining < 8 - offset { + acc += val >> (8 - offset - remaining); + end_offset = remaining + offset; + break; + } else { + acc += val << (remaining - (8 - offset)); + remaining -= 8 - offset; + offset = 0; + } + } + Ok(( (input.slice(cnt..), end_offset) , acc)) + } + } + } +} + +/// generates a parser taking `count` bits and comparing them to `pattern` +pub fn tag>(pattern: O, count: C) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E> +where + I: Slice> + InputIter + InputLength + Clone, + C: ToUsize, + O: From + AddAssign + Shl + Shr + PartialEq, +{ + let count = count.to_usize(); + move |input: (I, usize)| { + let inp = input.clone(); + + take(count)(input).and_then(|(i, o)| { + if pattern == o { + Ok((i, o)) + } else { + Err(Err::Error(error_position!(inp, ErrorKind::TagBits))) + } + }) + } +} diff --git a/third_party/rust/nom/src/bits/macros.rs b/third_party/rust/nom/src/bits/macros.rs new file mode 100644 index 0000000000..b056503fc6 --- /dev/null +++ b/third_party/rust/nom/src/bits/macros.rs @@ -0,0 +1,263 @@ +//! Bit level parsers and combinators +//! +//! Bit parsing is handled by tweaking the input in most macros. +//! In byte level parsing, the input is generally a `&[u8]` passed from combinator +//! to combinator as the slices are manipulated. +//! +//! Bit parsers take a `(&[u8], usize)` as input. The first part of the tuple is a byte slice, +//! the second part is a bit offset in the first byte of the slice. +//! +//! By passing a pair like this, we can leverage most of the existing combinators, and avoid +//! transforming the whole slice to a vector of booleans. This should make it easy +//! to see a byte slice as a bit stream, and parse code points of arbitrary bit length. +//! + +/// Transforms its byte slice input into a bit stream for the underlying parser. This allows the +/// given bit stream parser to work on a byte slice input. +/// +/// Signature: +/// `bits!( parser ) => ( &[u8], (&[u8], usize) -> IResult<(&[u8], usize), T> ) -> IResult<&[u8], T>` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// # fn main() { +/// named!( take_4_bits, bits!( take_bits!( 4u8 ) ) ); +/// +/// let input = vec![0xAB, 0xCD, 0xEF, 0x12]; +/// let sl = &input[..]; +/// +/// assert_eq!(take_4_bits( sl ), Ok( (&sl[1..], 0xA) )); +/// assert_eq!(take_4_bits( &b""[..] ), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +#[macro_export(local_inner_macros)] +macro_rules! bits ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ({ + $crate::bits::bitsc($i, move |i| { $submac!(i, $($args)*) }) + }); + ($i:expr, $f:expr) => ( + bits!($i, call!($f)) + ); +); + +/// Counterpart to bits, bytes! transforms its bit stream input into a byte slice for the underlying +/// parser, allowing byte-slice parsers to work on bit streams. +/// +/// Signature: +/// `bytes!( parser ) => ( (&[u8], usize), &[u8] -> IResult<&[u8], T> ) -> IResult<(&[u8], usize), T>`, +/// +/// A partial byte remaining in the input will be ignored and the given parser will start parsing +/// at the next full byte. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::combinator::rest; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// +/// named!( parse<(u8, u8, &[u8])>, bits!( tuple!( +/// take_bits!(4u8), +/// take_bits!(8u8), +/// bytes!(rest::<_, (_, ErrorKind)>) +/// ))); +/// +/// let input = &[0xde, 0xad, 0xbe, 0xaf]; +/// +/// assert_eq!(parse( input ), Ok(( &[][..], (0xd, 0xea, &[0xbe, 0xaf][..]) ))); +/// # } +#[macro_export(local_inner_macros)] +macro_rules! bytes ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ({ + $crate::bits::bytesc($i, move |i| { $submac!(i, $($args)*) }) + }); + ($i:expr, $f:expr) => ( + bytes!($i, call!($f)) + ); +); + +/// Consumes the specified number of bits and returns them as the specified type. +/// +/// Signature: +/// `take_bits!(type, count) => ( (&[T], usize), U, usize) -> IResult<(&[T], usize), U>` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(bits_pair<(&[u8], usize), (u8, u8)>, pair!( take_bits!(4u8), take_bits!(4u8) ) ); +/// named!( take_pair<(u8, u8)>, bits!( bits_pair ) ); +/// +/// let input = vec![0xAB, 0xCD, 0xEF]; +/// let sl = &input[..]; +/// +/// assert_eq!(take_pair( sl ), Ok((&sl[1..], (0xA, 0xB))) ); +/// assert_eq!(take_pair( &sl[1..] ), Ok((&sl[2..], (0xC, 0xD))) ); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_bits ( + ($i:expr, $count:expr) => ( + { + let res: $crate::IResult<_, _> = $crate::bits::streaming::take($count)($i); + res + } + ); +); + +/// Matches the given bit pattern. +/// +/// Signature: +/// `tag_bits!(type, count, pattern) => ( (&[T], usize), U, usize, U) -> IResult<(&[T], usize), U>` +/// +/// The caller must specify the number of bits to consume. The matched value is included in the +/// result on success. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!( take_a, bits!( tag_bits!(4usize, 0xA) ) ); +/// +/// let input = vec![0xAB, 0xCD, 0xEF]; +/// let sl = &input[..]; +/// +/// assert_eq!(take_a( sl ), Ok((&sl[1..], 0xA)) ); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! tag_bits ( + ($i:expr, $count:expr, $p: expr) => ( + { + let res: $crate::IResult<_, _> = $crate::bits::streaming::tag($p, $count)($i); + res + } + ) +); + +#[cfg(test)] +mod tests { + use crate::lib::std::ops::{AddAssign, Shl, Shr}; + use crate::internal::{Err, Needed, IResult}; + use crate::error::ErrorKind; + + #[test] + fn take_bits() { + let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11]; + let sl = &input[..]; + + assert_eq!(take_bits!((sl, 0), 0u8), Ok(((sl, 0), 0))); + assert_eq!(take_bits!((sl, 0), 8u8), Ok(((&sl[1..], 0), 170))); + assert_eq!(take_bits!((sl, 0), 3u8), Ok(((&sl[0..], 3), 5))); + assert_eq!(take_bits!((sl, 0), 6u8), Ok(((&sl[0..], 6), 42))); + assert_eq!(take_bits!((sl, 1), 1u8), Ok(((&sl[0..], 2), 0))); + assert_eq!(take_bits!((sl, 1), 2u8), Ok(((&sl[0..], 3), 1))); + assert_eq!(take_bits!((sl, 1), 3u8), Ok(((&sl[0..], 4), 2))); + assert_eq!(take_bits!((sl, 6), 3u8), Ok(((&sl[1..], 1), 5))); + assert_eq!(take_bits!((sl, 0), 10u8), Ok(((&sl[1..], 2), 683))); + assert_eq!(take_bits!((sl, 0), 8u8), Ok(((&sl[1..], 0), 170))); + assert_eq!(take_bits!((sl, 6), 10u8), Ok(((&sl[2..], 0), 752))); + assert_eq!(take_bits!((sl, 6), 11u8), Ok(((&sl[2..], 1), 1504))); + assert_eq!(take_bits!((sl, 0), 20u8), Ok(((&sl[2..], 4), 700_163))); + assert_eq!(take_bits!((sl, 4), 20u8), Ok(((&sl[3..], 0), 716_851))); + let r: IResult<_,u32> = take_bits!((sl, 4), 22u8); + assert_eq!( + r, + Err(Err::Incomplete(Needed::Size(22))) + ); + } + + #[test] + fn tag_bits() { + let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11]; + let sl = &input[..]; + + assert_eq!(tag_bits!((sl, 0), 3u8, 0b101), Ok(((&sl[0..], 3), 5))); + assert_eq!(tag_bits!((sl, 0), 4u8, 0b1010), Ok(((&sl[0..], 4), 10))); + } + + named!(ch<(&[u8],usize),(u8,u8)>, + do_parse!( + tag_bits!(3u8, 0b101) >> + x: take_bits!(4u8) >> + y: take_bits!(5u8) >> + (x,y) + ) + ); + + #[test] + fn chain_bits() { + let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11]; + let sl = &input[..]; + assert_eq!(ch((&input[..], 0)), Ok(((&sl[1..], 4), (5, 15)))); + assert_eq!(ch((&input[..], 4)), Ok(((&sl[2..], 0), (7, 16)))); + assert_eq!(ch((&input[..1], 0)), Err(Err::Incomplete(Needed::Size(5)))); + } + + named!(ch_bytes<(u8, u8)>, bits!(ch)); + #[test] + fn bits_to_bytes() { + let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11]; + assert_eq!(ch_bytes(&input[..]), Ok((&input[2..], (5, 15)))); + assert_eq!(ch_bytes(&input[..1]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + ch_bytes(&input[1..]), + Err(Err::Error(error_position!(&input[1..], ErrorKind::TagBits))) + ); + } + + named!(bits_bytes_bs, bits!(bytes!(crate::combinator::rest::<_, (&[u8], ErrorKind)>))); + #[test] + fn bits_bytes() { + let input = [0b10_10_10_10]; + assert_eq!(bits_bytes_bs(&input[..]), Ok((&[][..], &[0b10_10_10_10][..]))); + } + + #[derive(PartialEq, Debug)] + struct FakeUint(u32); + + impl AddAssign for FakeUint { + fn add_assign(&mut self, other: FakeUint) { + *self = FakeUint(self.0 + other.0); + } + } + + impl Shr for FakeUint { + type Output = FakeUint; + + fn shr(self, shift: usize) -> FakeUint { + FakeUint(self.0 >> shift) + } + } + + impl Shl for FakeUint { + type Output = FakeUint; + + fn shl(self, shift: usize) -> FakeUint { + FakeUint(self.0 << shift) + } + } + + impl From for FakeUint { + fn from(i: u8) -> FakeUint { + FakeUint(u32::from(i)) + } + } + + #[test] + fn non_privitive_type() { + let input = [0b10_10_10_10, 0b11_11_00_00, 0b00_11_00_11]; + let sl = &input[..]; + + assert_eq!( + take_bits!((sl, 0), 20u8), + Ok(((&sl[2..], 4), FakeUint(700_163))) + ); + assert_eq!( + take_bits!((sl, 4), 20u8), + Ok(((&sl[3..], 0), FakeUint(716_851))) + ); + let r3: IResult<_, FakeUint> = take_bits!((sl, 4), 22u8); + assert_eq!( + r3, + Err(Err::Incomplete(Needed::Size(22))) + ); + } +} diff --git a/third_party/rust/nom/src/bits/mod.rs b/third_party/rust/nom/src/bits/mod.rs new file mode 100644 index 0000000000..a6b12f1a74 --- /dev/null +++ b/third_party/rust/nom/src/bits/mod.rs @@ -0,0 +1,119 @@ +//! bit level parsers +//! + +#[macro_use] +mod macros; + +pub mod streaming; +pub mod complete; + +use crate::error::{ParseError, ErrorKind}; +use crate::internal::{Err, IResult, Needed}; +use crate::lib::std::ops::RangeFrom; +use crate::traits::{Slice, ErrorConvert}; + + +/// Converts a byte-level input to a bit-level input, for consumption by a parser that uses bits. +/// +/// Afterwards, the input is converted back to a byte-level parser, with any remaining bits thrown +/// away. +/// +/// # Example +/// ```ignore +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// use nom::bits::bits; +/// use nom::bits::complete::take; +/// +/// fn take_4_bits(input: &[u8]) -> IResult<&[u8], u64> { +/// bits(take::<_, _, _, (_, _)>(4usize))(input) +/// } +/// +/// let input = vec![0xAB, 0xCD, 0xEF, 0x12]; +/// let sl = &input[..]; +/// +/// assert_eq!(take_4_bits( sl ), Ok( (&sl[1..], 0xA) )); +/// ``` +pub fn bits+ErrorConvert, E2: ParseError, P>(parser: P) -> impl Fn(I) -> IResult +where + I: Slice>, + P: Fn((I, usize)) -> IResult<(I, usize), O, E1>, +{ + move |input: I| match parser((input, 0)) { + Ok(((rest, offset), res)) => { + let byte_index = offset / 8 + if offset % 8 == 0 { 0 } else { 1 }; + Ok((rest.slice(byte_index..), res)) + } + Err(Err::Incomplete(n)) => Err(Err::Incomplete(n.map(|u| u / 8 + 1))), + Err(Err::Error(e)) => Err(Err::Error(e.convert())), + Err(Err::Failure(e)) => Err(Err::Failure(e.convert())), + } +} + +#[doc(hidden)] +pub fn bitsc+ErrorConvert, E2: ParseError, P>(input: I, parser: P) -> IResult +where + I: Slice>, + P: Fn((I, usize)) -> IResult<(I, usize), O, E1>, +{ + bits(parser)(input) +} + +/// Counterpart to bits, bytes transforms its bit stream input into a byte slice for the underlying +/// parser, allowing byte-slice parsers to work on bit streams. +/// +/// A partial byte remaining in the input will be ignored and the given parser will start parsing +/// at the next full byte. +/// +/// ```ignore +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// # use nom::combinator::rest; +/// # use nom::sequence::tuple; +/// use nom::bits::{bits, bytes, streaming::take_bits}; +/// +/// fn parse(input: &[u8]) -> IResult<&[u8], (u8, u8, &[u8])> { +/// bits(tuple(( +/// take_bits(4usize), +/// take_bits(8usize), +/// bytes(rest) +/// )))(input) +/// } +/// +/// let input = &[0xde, 0xad, 0xbe, 0xaf]; +/// +/// assert_eq!(parse( input ), Ok(( &[][..], (0xd, 0xea, &[0xbe, 0xaf][..]) ))); +/// ``` +pub fn bytes+ErrorConvert, E2: ParseError<(I, usize)>, P>(parser: P) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E2> +where + I: Slice> + Clone, + P: Fn(I) -> IResult, +{ + move |(input, offset): (I, usize)| { + let inner = if offset % 8 != 0 { + input.slice((1 + offset / 8)..) + } else { + input.slice((offset / 8)..) + }; + let i = (input.clone(), offset); + match parser(inner) { + Ok((rest, res)) => Ok(((rest, 0), res)), + Err(Err::Incomplete(Needed::Unknown)) => Err(Err::Incomplete(Needed::Unknown)), + Err(Err::Incomplete(Needed::Size(sz))) => Err(match sz.checked_mul(8) { + Some(v) => Err::Incomplete(Needed::Size(v)), + None => Err::Failure(E2::from_error_kind(i, ErrorKind::TooLarge)), + }), + Err(Err::Error(e)) => Err(Err::Error(e.convert())), + Err(Err::Failure(e)) => Err(Err::Failure(e.convert())), + } + } +} + +#[doc(hidden)] +pub fn bytesc+ErrorConvert, E2: ParseError<(I, usize)>, P>(input: (I, usize), parser: P) -> IResult<(I, usize), O, E2> +where + I: Slice> + Clone, + P: Fn(I) -> IResult, +{ + bytes(parser)(input) +} diff --git a/third_party/rust/nom/src/bits/streaming.rs b/third_party/rust/nom/src/bits/streaming.rs new file mode 100644 index 0000000000..5ab75961bb --- /dev/null +++ b/third_party/rust/nom/src/bits/streaming.rs @@ -0,0 +1,75 @@ +//! bit level parsers +//! + +use crate::error::{ErrorKind, ParseError}; +use crate::internal::{Err, IResult, Needed}; +use crate::lib::std::ops::{AddAssign, RangeFrom, Shl, Shr, Div}; +use crate::traits::{InputIter, InputLength, Slice, ToUsize}; + +/// generates a parser taking `count` bits +pub fn take>(count: C) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E> +where + I: Slice> + InputIter + InputLength, + C: ToUsize, + O: From + AddAssign + Shl + Shr, +{ + let count = count.to_usize(); + move |(input, bit_offset): (I, usize)| { + if count == 0 { + Ok(((input, bit_offset), 0u8.into())) + } else { + let cnt = (count + bit_offset).div(8); + if input.input_len() * 8 < count + bit_offset { + Err(Err::Incomplete(Needed::Size(count as usize))) + } else { + let mut acc:O = (0 as u8).into(); + let mut offset: usize = bit_offset; + let mut remaining: usize = count; + let mut end_offset: usize = 0; + + for byte in input.iter_elements().take(cnt + 1) { + if remaining == 0 { + break; + } + let val: O = if offset == 0 { + byte.into() + } else { + ((byte << offset) as u8 >> offset).into() + }; + + if remaining < 8 - offset { + acc += val >> (8 - offset - remaining); + end_offset = remaining + offset; + break; + } else { + acc += val << (remaining - (8 - offset)); + remaining -= 8 - offset; + offset = 0; + } + } + Ok(( (input.slice(cnt..), end_offset) , acc)) + } + } + } +} + +/// generates a parser taking `count` bits and comparing them to `pattern` +pub fn tag>(pattern: O, count: C) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E> +where + I: Slice> + InputIter + InputLength + Clone, + C: ToUsize, + O: From + AddAssign + Shl + Shr + PartialEq, +{ + let count = count.to_usize(); + move |input: (I, usize)| { + let inp = input.clone(); + + take(count)(input).and_then(|(i, o)| { + if pattern == o { + Ok((i, o)) + } else { + Err(Err::Error(error_position!(inp, ErrorKind::TagBits))) + } + }) + } +} diff --git a/third_party/rust/nom/src/branch/macros.rs b/third_party/rust/nom/src/branch/macros.rs new file mode 100644 index 0000000000..4c02461c04 --- /dev/null +++ b/third_party/rust/nom/src/branch/macros.rs @@ -0,0 +1,940 @@ +/// Try a list of parsers and return the result of the first successful one +/// +/// ```rust,ignore +/// alt!(I -> IResult | I -> IResult | ... | I -> IResult ) => I -> IResult +/// ``` +/// All the parsers must have the same return type. +/// +/// If one of the parsers returns `Incomplete`, `alt!` will return `Incomplete`, to retry +/// once you get more input. Note that it is better for performance to know the +/// minimum size of data you need before you get into `alt!`. +/// +/// The `alt!` combinator is used in the following way: +/// +/// ```rust,ignore +/// alt!(parser_1 | parser_2 | ... | parser_n) +/// ``` +/// +/// # Basic example +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// // Create a parser that will match either "dragon" or "beast" +/// named!( dragon_or_beast, alt!( tag!( "dragon" ) | tag!( "beast" ) ) ); +/// +/// // Given the input "dragon slayer", the parser will match "dragon" +/// // and the rest will be " slayer" +/// let (rest, result) = dragon_or_beast(b"dragon slayer").unwrap(); +/// assert_eq!(result, b"dragon"); +/// assert_eq!(rest, b" slayer"); +/// +/// // Given the input "beast of Gevaudan", the parser will match "beast" +/// // and the rest will be " of Gevaudan" +/// let (rest, result) = dragon_or_beast(&b"beast of Gevaudan"[..]).unwrap(); +/// assert_eq!(result, b"beast"); +/// assert_eq!(rest, b" of Gevaudan"); +/// # } +/// ``` +/// +/// # Manipulate results +/// +/// There exists another syntax for `alt!` that gives you the ability to +/// manipulate the result from each parser: +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// # +/// // We create an enum to represent our creatures +/// #[derive(Debug,PartialEq,Eq)] +/// enum Creature { +/// Dragon, +/// Beast, +/// Unknown(usize) +/// } +/// +/// // Let's make a helper function that returns true when not a space +/// // we are required to do this because the `take_while!` macro is limited +/// // to idents, so we can't negate `ìs_space` at the call site +/// fn is_not_space(c: u8) -> bool { ! nom::character::is_space(c) } +/// +/// // Our parser will return the `Dragon` variant when matching "dragon", +/// // the `Beast` variant when matching "beast" and otherwise it will consume +/// // the input until a space is found and return an `Unknown` creature with +/// // the size of it's name. +/// named!(creature, alt!( +/// tag!("dragon") => { |_| Creature::Dragon } | +/// tag!("beast") => { |_| Creature::Beast } | +/// take_while!(is_not_space) => { |r: &[u8]| Creature::Unknown(r.len()) } +/// // the closure takes the result as argument if the parser is successful +/// )); +/// +/// // Given the input "dragon slayer" the parser will return `Creature::Dragon` +/// // and the rest will be " slayer" +/// let (rest, result) = creature(b"dragon slayer").unwrap(); +/// assert_eq!(result, Creature::Dragon); +/// assert_eq!(rest, b" slayer"); +/// +/// // Given the input "beast of Gevaudan" the parser will return `Creature::Beast` +/// // and the rest will be " of Gevaudan" +/// let (rest, result) = creature(b"beast of Gevaudan").unwrap(); +/// assert_eq!(result, Creature::Beast); +/// assert_eq!(rest, b" of Gevaudan"); +/// +/// // Given the input "demon hunter" the parser will return `Creature::Unknown(5)` +/// // and the rest will be " hunter" +/// let (rest, result) = creature(b"demon hunter").unwrap(); +/// assert_eq!(result, Creature::Unknown(5)); +/// assert_eq!(rest, b" hunter"); +/// # } +/// ``` +/// +/// # Behaviour of `alt!` +/// +/// **BE CAREFUL** there is a case where the behaviour of `alt!` can be confusing: +/// +/// when the alternatives have different lengths, like this case: +/// +/// ```ignore +/// named!( test, alt!( tag!( "abcd" ) | tag!( "ef" ) | tag!( "ghi" ) | tag!( "kl" ) ) ); +/// ``` +/// +/// With this parser, if you pass `"abcd"` as input, the first alternative parses it correctly, +/// but if you pass `"efg"`, the first alternative will return `Incomplete`, since it needs an input +/// of 4 bytes. This behaviour of `alt!` is expected: if you get a partial input that isn't matched +/// by the first alternative, but would match if the input was complete, you want `alt!` to indicate +/// that it cannot decide with limited information. +/// +/// There are two ways to fix this behaviour. The first one consists in ordering the alternatives +/// by size, like this: +/// +/// ```ignore +/// named!( test, alt!( tag!( "ef" ) | tag!( "kl") | tag!( "ghi" ) | tag!( "abcd" ) ) ); +/// ``` +/// +/// With this solution, the largest alternative will be tested last. +/// +/// The other solution uses the `complete!` combinator, which transforms an `Incomplete` in an +/// `Error`. If one of the alternatives returns `Incomplete` but is wrapped by `complete!`, +/// `alt!` will try the next alternative. This is useful when you know that +/// you will not get partial input: +/// +/// ```ignore +/// named!( test, +/// alt!( +/// complete!( tag!( "abcd" ) ) | +/// complete!( tag!( "ef" ) ) | +/// complete!( tag!( "ghi" ) ) | +/// complete!( tag!( "kl" ) ) +/// ) +/// ); +/// ``` +/// +/// This behaviour of `alt!` can get especially confusing if multiple alternatives have different +/// sizes but a common prefix, like this: +/// +/// ```ignore +/// named!( test, alt!( tag!( "abcd" ) | tag!( "ab" ) | tag!( "ef" ) ) ); +/// ``` +/// +/// in that case, if you order by size, passing `"abcd"` as input will always be matched by the +/// smallest parser, so the solution using `complete!` is better suited. +/// +/// You can also nest multiple `alt!`, like this: +/// +/// ```ignore +/// named!( test, +/// alt!( +/// preceded!( +/// tag!("ab"), +/// alt!( +/// tag!( "cd" ) | +/// eof!() +/// ) +/// ) +/// | tag!( "ef" ) +/// ) +/// ); +/// ``` +/// +/// `preceded!` will first parse `"ab"` then, if successful, try the alternatives "cd", +/// or empty input (End Of File). If none of them work, `preceded!` will fail and +/// "ef" will be tested. +/// +#[macro_export(local_inner_macros)] +macro_rules! alt ( + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)* ) => ( + nom_compile_error!("alt uses '|' as separator, not ',': + + alt!( + tag!(\"abcd\") | + tag!(\"efgh\") | + tag!(\"ijkl\") + ) + "); + ); + (__impl $i:expr, $e:path, $($rest:tt)* ) => ( + alt!(__impl $i, call!($e) , $($rest)*); + ); + (__impl $i:expr, $e:path | $($rest:tt)*) => ( + alt!(__impl $i, call!($e) | $($rest)*); + ); + + (__impl $i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + let i_ = $i.clone(); + let res = $subrule!(i_, $($args)*); + match res { + Ok(o) => Ok(o), + Err(Err::Error(e)) => { + let out = alt!(__impl $i, $($rest)*); + + // Compile-time hack to ensure that res's E type is not under-specified. + // This all has no effect at runtime. + #[allow(dead_code)] + fn unify_types(_: &T, _: &T) {} + if let Err(Err::Error(ref e2)) = out { + unify_types(&e, e2); + } + + out + }, + Err(e) => Err(e), + } + } + ); + + (__impl $i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + let i_ = $i.clone(); + match $subrule!(i_, $($args)* ) { + Ok((i,o)) => Ok((i,$gen(o))), + Err(Err::Error(e)) => { + let out = alt!(__impl $i, $($rest)*); + + // Compile-time hack to ensure that res's E type is not under-specified. + // This all has no effect at runtime. + fn unify_types(_: &T, _: &T) {} + if let Err(Err::Error(ref e2)) = out { + unify_types(&e, e2); + } + + out + }, + Err(e) => Err(e), + } + } + ); + + (__impl $i:expr, $e:path => { $gen:expr } | $($rest:tt)*) => ( + alt!(__impl $i, call!($e) => { $gen } | $($rest)*); + ); + + (__impl $i:expr, __end) => ( + { + use $crate::{Err,error::ErrorKind}; + let e2 = ErrorKind::Alt; + let err = Err::Error(error_position!($i, e2)); + + Err(err) + } + ); + + ($i:expr, $($rest:tt)*) => ( + { + alt!(__impl $i, $($rest)* | __end) + } + ); +); + +/// `switch!(I -> IResult, P => I -> IResult | ... | P => I -> IResult ) => I -> IResult` +/// choose the next parser depending on the result of the first one, if successful, +/// and returns the result of the second parser +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => tag!("XYZ") | +/// b"efgh" => tag!("123") +/// ) +/// ); +/// +/// let a = b"abcdXYZ123"; +/// let b = b"abcdef"; +/// let c = b"efgh123"; +/// let d = b"blah"; +/// +/// assert_eq!(sw(&a[..]), Ok((&b"123"[..], &b"XYZ"[..]))); +/// assert_eq!(sw(&b[..]), Err(Err::Error(error_node_position!(&b"abcdef"[..], ErrorKind::Switch, +/// error_position!(&b"ef"[..], ErrorKind::Tag))))); +/// assert_eq!(sw(&c[..]), Ok((&b""[..], &b"123"[..]))); +/// assert_eq!(sw(&d[..]), Err(Err::Error(error_position!(&b"blah"[..], ErrorKind::Switch)))); +/// # } +/// ``` +/// +/// You can specify a default case like with a normal match, using `_` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => tag!("XYZ") | +/// _ => value!(&b"default"[..]) +/// ) +/// ); +/// +/// let a = b"abcdXYZ123"; +/// let b = b"blah"; +/// +/// assert_eq!(sw(&a[..]), Ok((&b"123"[..], &b"XYZ"[..]))); +/// assert_eq!(sw(&b[..]), Ok((&b""[..], &b"default"[..]))); +/// # } +/// ``` +/// +/// Due to limitations in Rust macros, it is not possible to have simple functions on the right hand +/// side of pattern, like this: +/// +/// ```ignore +/// named!(xyz, tag!("XYZ")); +/// named!(num, tag!("123")); +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => xyz | +/// b"efgh" => 123 +/// ) +/// ); +/// ``` +/// +/// If you want to pass your own functions instead, you can use the `call!` combinator as follows: +/// +/// ```ignore +/// named!(xyz, tag!("XYZ")); +/// named!(num, tag!("123")); +/// named!(sw, +/// switch!(take!(4), +/// b"abcd" => call!(xyz) | +/// b"efgh" => call!(num) +/// ) +/// ); +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! switch ( + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $( $($p:pat)|+ => $subrule:ident!( $($args2:tt)* ))|* ) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option::*; + use $crate::{Err,error::ErrorKind}; + + let i_ = $i.clone(); + match map!(i_, $submac!($($args)*), Some) { + Err(Err::Error(err)) => { + fn unify_types(_: &T, _: &T) {} + let e1 = ErrorKind::Switch; + let e2 = error_position!($i, e1.clone()); + unify_types(&err, &e2); + + Err(Err::Error(error_node_position!($i, e1, err))) + }, + Err(e) => Err(e), + Ok((i, o)) => { + + match o { + $($(Some($p) )|+ => match $subrule!(i, $($args2)*) { + Err(Err::Error(err)) => { + fn unify_types(_: &T, _: &T) {} + let e1 = ErrorKind::Switch; + let e2 = error_position!($i, e1.clone()); + unify_types(&err, &e2); + + Err(Err::Error(error_node_position!($i, e1, err))) + }, + Ok(o) => Ok(o), + Err(e) => Err(e), + }),*, + _ => Err(Err::convert(Err::Error(error_position!($i, ErrorKind::Switch)))) + } + } + } + } + ); + ($i:expr, $submac:ident!( $($args:tt)*), $($rest:tt)*) => ( + { + switch!(__impl $i, $submac!($($args)*), $($rest)*) + } + ); + ($i:expr, $e:path, $($rest:tt)*) => ( + { + switch!(__impl $i, call!($e), $($rest)*) + } + ); +); + +/// +/// +/// `permutation!(I -> IResult, I -> IResult, ... I -> IResult ) => I -> IResult` +/// applies its sub parsers in a sequence, but independent from their order +/// this parser will only succeed if all of its sub parsers succeed +/// +/// the tuple of results is in the same order as the parsers are declared +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind,Needed}; +/// # fn main() { +/// named!(perm<(&[u8], &[u8], &[u8])>, +/// permutation!(tag!("abcd"), tag!("efg"), tag!("hi")) +/// ); +/// +/// // whatever the order, if the parser succeeds, each +/// // tag should have matched correctly +/// let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]); +/// +/// let a = &b"abcdefghijk"[..]; +/// assert_eq!(perm(a), Ok((&b"jk"[..], expected))); +/// let b = &b"efgabcdhijkl"[..]; +/// assert_eq!(perm(b), Ok((&b"jkl"[..], expected))); +/// let c = &b"hiefgabcdjklm"[..]; +/// assert_eq!(perm(c), Ok((&b"jklm"[..], expected))); +/// +/// let d = &b"efgxyzabcdefghi"[..]; +/// assert_eq!(perm(d), Err(Err::Error(error_node_position!(&b"efgxyzabcdefghi"[..], ErrorKind::Permutation, +/// error_position!(&b"xyzabcdefghi"[..], ErrorKind::Permutation))))); +/// +/// let e = &b"efgabc"[..]; +/// assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(4)))); +/// # } +/// ``` +/// +/// If one of the child parsers is followed by a `?`, that parser is now +/// optional: +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind,Needed}; +/// # fn main() { +/// named!(perm<&str, (Option<&str>, &str, &str)>, +/// permutation!(tag!("abcd")?, tag!("efg"), tag!("hi")) +/// ); +/// +/// // whatever the order, if the parser succeeds, each +/// // tag should have matched correctly +/// let expected = (Some("abcd"), "efg", "hi"); +/// +/// let a = "abcdefghijk"; +/// assert_eq!(perm(a), Ok(("jk", expected))); +/// let b = "efgabcdhijkl"; +/// assert_eq!(perm(b), Ok(("jkl", expected))); +/// let c = "hiefgabcdjklm"; +/// assert_eq!(perm(c), Ok(("jklm", expected))); +/// +/// // if `abcd` is missing: +/// let expected = (None, "efg", "hi"); +/// +/// let a = "efghijk"; +/// assert_eq!(perm(a), Ok(("jk", expected))); +/// let b = "efghijkl"; +/// assert_eq!(perm(b), Ok(("jkl", expected))); +/// let c = "hiefgjklm"; +/// assert_eq!(perm(c), Ok(("jklm", expected))); +/// +/// let e = "efgabc"; +/// assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(4)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! permutation ( + ($i:expr, $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option::*; + use $crate::{Err,error::ErrorKind}; + + let mut res = permutation_init!((), $($rest)*); + let mut input = $i; + let mut error = None; + let mut needed = None; + + loop { + let mut all_done = true; + permutation_iterator!(0, input, all_done, needed, res, $($rest)*); + + //if we reach that part, it means none of the parsers were able to read anything + if !all_done { + //FIXME: should wrap the error returned by the child parser + error = Some(error_position!(input, ErrorKind::Permutation)); + } + break; + } + + if let Some(need) = needed { + Err(Err::convert(need)) + } else { + if let Some(unwrapped_res) = { permutation_unwrap!(0, (), res, $($rest)*) } { + Ok((input, unwrapped_res)) + } else { + if let Some(e) = error { + Err(Err::Error(error_node_position!($i, ErrorKind::Permutation, e))) + } else { + Err(Err::Error(error_position!($i, ErrorKind::Permutation))) + } + } + } + } + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! permutation_init ( + ((), $e:ident?, $($rest:tt)*) => ( + permutation_init!(($crate::lib::std::option::Option::None), $($rest)*) + ); + ((), $e:ident, $($rest:tt)*) => ( + permutation_init!(($crate::lib::std::option::Option::None), $($rest)*) + ); + + ((), $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => ( + permutation_init!(($crate::lib::std::option::Option::None), $($rest)*) + ); + ((), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + permutation_init!(($crate::lib::std::option::Option::None), $($rest)*) + ); + + (($($parsed:expr),*), $e:ident?, $($rest:tt)*) => ( + permutation_init!(($($parsed),* , $crate::lib::std::option::Option::None), $($rest)*); + ); + (($($parsed:expr),*), $e:ident, $($rest:tt)*) => ( + permutation_init!(($($parsed),* , $crate::lib::std::option::Option::None), $($rest)*); + ); + + (($($parsed:expr),*), $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => ( + permutation_init!(($($parsed),* , $crate::lib::std::option::Option::None), $($rest)*); + ); + (($($parsed:expr),*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + permutation_init!(($($parsed),* , $crate::lib::std::option::Option::None), $($rest)*); + ); + + (($($parsed:expr),*), $e:ident) => ( + ($($parsed),* , $crate::lib::std::option::Option::None) + ); + (($($parsed:expr),*), $e:ident?) => ( + ($($parsed),* , $crate::lib::std::option::Option::None) + ); + + (($($parsed:expr),*), $submac:ident!( $($args:tt)* )?) => ( + ($($parsed),* , $crate::lib::std::option::Option::None) + ); + (($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => ( + ($($parsed),* , $crate::lib::std::option::Option::None) + ); + (($($parsed:expr),*),) => ( + ($($parsed),*) + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! succ ( + (0, $submac:ident ! ($($rest:tt)*)) => ($submac!(1, $($rest)*)); + (1, $submac:ident ! ($($rest:tt)*)) => ($submac!(2, $($rest)*)); + (2, $submac:ident ! ($($rest:tt)*)) => ($submac!(3, $($rest)*)); + (3, $submac:ident ! ($($rest:tt)*)) => ($submac!(4, $($rest)*)); + (4, $submac:ident ! ($($rest:tt)*)) => ($submac!(5, $($rest)*)); + (5, $submac:ident ! ($($rest:tt)*)) => ($submac!(6, $($rest)*)); + (6, $submac:ident ! ($($rest:tt)*)) => ($submac!(7, $($rest)*)); + (7, $submac:ident ! ($($rest:tt)*)) => ($submac!(8, $($rest)*)); + (8, $submac:ident ! ($($rest:tt)*)) => ($submac!(9, $($rest)*)); + (9, $submac:ident ! ($($rest:tt)*)) => ($submac!(10, $($rest)*)); + (10, $submac:ident ! ($($rest:tt)*)) => ($submac!(11, $($rest)*)); + (11, $submac:ident ! ($($rest:tt)*)) => ($submac!(12, $($rest)*)); + (12, $submac:ident ! ($($rest:tt)*)) => ($submac!(13, $($rest)*)); + (13, $submac:ident ! ($($rest:tt)*)) => ($submac!(14, $($rest)*)); + (14, $submac:ident ! ($($rest:tt)*)) => ($submac!(15, $($rest)*)); + (15, $submac:ident ! ($($rest:tt)*)) => ($submac!(16, $($rest)*)); + (16, $submac:ident ! ($($rest:tt)*)) => ($submac!(17, $($rest)*)); + (17, $submac:ident ! ($($rest:tt)*)) => ($submac!(18, $($rest)*)); + (18, $submac:ident ! ($($rest:tt)*)) => ($submac!(19, $($rest)*)); + (19, $submac:ident ! ($($rest:tt)*)) => ($submac!(20, $($rest)*)); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! permutation_unwrap ( + ($it:tt, (), $res:ident, $e:ident?, $($rest:tt)*) => ( + succ!($it, permutation_unwrap!(($res.$it), $res, $($rest)*)); + ); + ($it:tt, (), $res:ident, $e:ident, $($rest:tt)*) => ({ + let res = $res.$it; + if res.is_some() { + succ!($it, permutation_unwrap!((res.unwrap()), $res, $($rest)*)) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, (), $res:ident, $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => ( + succ!($it, permutation_unwrap!(($res.$it), $res, $($rest)*)); + ); + ($it:tt, (), $res:ident, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ({ + let res = $res.$it; + if res.is_some() { + succ!($it, permutation_unwrap!((res.unwrap()), $res, $($rest)*)) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, ($($parsed:expr),*), $res:ident, $e:ident?, $($rest:tt)*) => ( + succ!($it, permutation_unwrap!(($($parsed),* , $res.$it), $res, $($rest)*)); + ); + ($it:tt, ($($parsed:expr),*), $res:ident, $e:ident, $($rest:tt)*) => ({ + let res = $res.$it; + if res.is_some() { + succ!($it, permutation_unwrap!(($($parsed),* , res.unwrap()), $res, $($rest)*)) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, ($($parsed:expr),*), $res:ident, $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => ( + succ!($it, permutation_unwrap!(($($parsed),* , $res.$it), $res, $($rest)*)); + ); + ($it:tt, ($($parsed:expr),*), $res:ident, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ({ + let res = $res.$it; + if res.is_some() { + succ!($it, permutation_unwrap!(($($parsed),* , res.unwrap()), $res, $($rest)*)) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, ($($parsed:expr),*), $res:ident?, $e:ident) => ( + $crate::lib::std::option::Option::Some(($($parsed),* , { $res.$it })) + ); + ($it:tt, ($($parsed:expr),*), $res:ident, $e:ident) => ({ + let res = $res.$it; + if res.is_some() { + $crate::lib::std::option::Option::Some(($($parsed),* , res.unwrap() )) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, ($($parsed:expr),*), $res:ident, $submac:ident!( $($args:tt)* )?) => ( + $crate::lib::std::option::Option::Some(($($parsed),* , { $res.$it })) + ); + ($it:tt, ($($parsed:expr),*), $res:ident, $submac:ident!( $($args:tt)* )) => ({ + let res = $res.$it; + if res.is_some() { + $crate::lib::std::option::Option::Some(($($parsed),* , res.unwrap() )) + } else { + $crate::lib::std::option::Option::None + } + }); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! permutation_iterator ( + ($it:tt,$i:expr, $all_done:expr, $needed:expr, $res:expr, $e:ident?, $($rest:tt)*) => ( + permutation_iterator!($it, $i, $all_done, $needed, $res, call!($e), $($rest)*); + ); + ($it:tt,$i:expr, $all_done:expr, $needed:expr, $res:expr, $e:ident, $($rest:tt)*) => ( + permutation_iterator!($it, $i, $all_done, $needed, $res, call!($e), $($rest)*); + ); + + ($it:tt, $i:expr, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => { + permutation_iterator!($it, $i, $all_done, $needed, $res, $submac!($($args)*) , $($rest)*); + }; + ($it:tt, $i:expr, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ({ + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option::*; + use $crate::Err; + + if $res.$it.is_none() { + match $submac!($i, $($args)*) { + Ok((i,o)) => { + $i = i; + $res.$it = Some(o); + continue; + }, + Err(Err::Error(_)) => { + $all_done = false; + }, + Err(e) => { + $needed = Some(e); + break; + } + }; + } + succ!($it, permutation_iterator!($i, $all_done, $needed, $res, $($rest)*)); + }); + + ($it:tt,$i:expr, $all_done:expr, $needed:expr, $res:expr, $e:ident?) => ( + permutation_iterator!($it, $i, $all_done, $needed, $res, call!($e)); + ); + ($it:tt,$i:expr, $all_done:expr, $needed:expr, $res:expr, $e:ident) => ( + permutation_iterator!($it, $i, $all_done, $needed, $res, call!($e)); + ); + + ($it:tt, $i:expr, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )?) => { + permutation_iterator!($it, $i, $all_done, $needed, $res, $submac!($($args)*)); + }; + ($it:tt, $i:expr, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option::*; + use $crate::Err; + + if $res.$it.is_none() { + match $submac!($i, $($args)*) { + Ok((i,o)) => { + $i = i; + $res.$it = Some(o); + continue; + }, + Err(Err::Error(_)) => { + $all_done = false; + }, + Err(e) => { + $needed = Some(e); + break; + } + }; + } + }); +); + +#[cfg(test)] +mod tests { + use crate::error::ErrorKind; + #[cfg(feature = "alloc")] + use crate::{ + error::ParseError, + lib::std::{ + fmt::Debug, + string::{String, ToString} + } + }; + use crate::internal::{Err, IResult, Needed}; + + // reproduce the tag and take macros, because of module import order + macro_rules! tag ( + ($i:expr, $inp: expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + + tag_bytes!($i,bytes) + } + ); + ); + + macro_rules! tag_bytes ( + ($i:expr, $bytes: expr) => ( + { + use $crate::lib::std::cmp::min; + + let len = $i.len(); + let blen = $bytes.len(); + let m = min(len, blen); + let reduced = &$i[..m]; + let b = &$bytes[..m]; + + let res: IResult<_,_,_> = if reduced != b { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(error_position!($i, e))) + } else if m < blen { + Err(Err::Incomplete(Needed::Size(blen))) + } else { + Ok((&$i[blen..], reduced)) + }; + res + } + ); + ); + + macro_rules! take( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res:IResult<&[u8],&[u8],_> = if $i.len() < cnt { + Err(Err::Incomplete(Needed::Size(cnt))) + } else { + Ok((&$i[cnt..],&$i[0..cnt])) + }; + res + } + ); + ); + + #[cfg(feature = "alloc")] + #[derive(Debug, Clone, PartialEq)] + pub struct ErrorStr(String); + + #[cfg(feature = "alloc")] + impl From for ErrorStr { + fn from(i: u32) -> Self { + ErrorStr(format!("custom error code: {}", i)) + } + } + + #[cfg(feature = "alloc")] + impl<'a> From<&'a str> for ErrorStr { + fn from(i: &'a str) -> Self { + ErrorStr(format!("custom error message: {}", i)) + } + } + + #[cfg(feature = "alloc")] + impl ParseError for ErrorStr { + fn from_error_kind(input: I, kind: ErrorKind) -> Self { + ErrorStr(format!("custom error message: ({:?}, {:?})", input, kind)) + } + + fn append(input: I, kind: ErrorKind, other: Self) -> Self { + ErrorStr(format!("custom error message: ({:?}, {:?}) - {:?}", input, kind, other)) + } + } + + #[cfg(feature = "alloc")] + #[test] + fn alt() { + fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + Ok((&b""[..], input)) + } + + #[allow(unused_variables)] + fn dont_work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + Err(Err::Error(ErrorStr("abcd".to_string()))) + } + + fn work2(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + Ok((input, &b""[..])) + } + + fn alt1(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + alt!(i, dont_work | dont_work) + } + fn alt2(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + alt!(i, dont_work | work) + } + fn alt3(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + alt!(i, dont_work | dont_work | work2 | dont_work) + } + //named!(alt1, alt!(dont_work | dont_work)); + //named!(alt2, alt!(dont_work | work)); + //named!(alt3, alt!(dont_work | dont_work | work2 | dont_work)); + + let a = &b"abcd"[..]; + assert_eq!(alt1(a), Err(Err::Error(error_position!(a, ErrorKind::Alt)))); + assert_eq!(alt2(a), Ok((&b""[..], a))); + assert_eq!(alt3(a), Ok((a, &b""[..]))); + + named!(alt4, alt!(tag!("abcd") | tag!("efgh"))); + let b = &b"efgh"[..]; + assert_eq!(alt4(a), Ok((&b""[..], a))); + assert_eq!(alt4(b), Ok((&b""[..], b))); + + // test the alternative syntax + named!(alt5, alt!(tag!("abcd") => { |_| false } | tag!("efgh") => { |_| true })); + assert_eq!(alt5(a), Ok((&b""[..], false))); + assert_eq!(alt5(b), Ok((&b""[..], true))); + + // compile-time test guarding against an underspecified E generic type (#474) + named!(alt_eof1, alt!(eof!() | eof!())); + named!(alt_eof2, alt!(eof!() => {|x| x} | eof!() => {|x| x})); + let _ = (alt_eof1, alt_eof2); + } + + #[test] + fn alt_incomplete() { + named!(alt1, alt!(tag!("a") | tag!("bc") | tag!("def"))); + + let a = &b""[..]; + assert_eq!(alt1(a), Err(Err::Incomplete(Needed::Size(1)))); + let a = &b"b"[..]; + assert_eq!(alt1(a), Err(Err::Incomplete(Needed::Size(2)))); + let a = &b"bcd"[..]; + assert_eq!(alt1(a), Ok((&b"d"[..], &b"bc"[..]))); + let a = &b"cde"[..]; + assert_eq!(alt1(a), Err(Err::Error(error_position!(a, ErrorKind::Alt)))); + let a = &b"de"[..]; + assert_eq!(alt1(a), Err(Err::Incomplete(Needed::Size(3)))); + let a = &b"defg"[..]; + assert_eq!(alt1(a), Ok((&b"g"[..], &b"def"[..]))); + } + + #[allow(unused_variables)] + #[test] + fn switch() { + named!( + sw, + switch!(take!(4), + b"abcd" | b"xxxx" => take!(2) | + b"efgh" => take!(4) + ) + ); + + let a = &b"abcdefgh"[..]; + assert_eq!(sw(a), Ok((&b"gh"[..], &b"ef"[..]))); + + let b = &b"efghijkl"[..]; + assert_eq!(sw(b), Ok((&b""[..], &b"ijkl"[..]))); + let c = &b"afghijkl"[..]; + assert_eq!(sw(c), Err(Err::Error(error_position!(&b"afghijkl"[..], ErrorKind::Switch)))); + + let a = &b"xxxxefgh"[..]; + assert_eq!(sw(a), Ok((&b"gh"[..], &b"ef"[..]))); + } + + #[test] + fn permutation() { + named!(perm<(&[u8], &[u8], &[u8])>, permutation!(tag!("abcd"), tag!("efg"), tag!("hi"))); + + let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]); + + let a = &b"abcdefghijk"[..]; + assert_eq!(perm(a), Ok((&b"jk"[..], expected))); + let b = &b"efgabcdhijk"[..]; + assert_eq!(perm(b), Ok((&b"jk"[..], expected))); + let c = &b"hiefgabcdjk"[..]; + assert_eq!(perm(c), Ok((&b"jk"[..], expected))); + + let d = &b"efgxyzabcdefghi"[..]; + assert_eq!( + perm(d), + Err(Err::Error(error_node_position!( + &b"efgxyzabcdefghi"[..], + ErrorKind::Permutation, + error_position!(&b"xyzabcdefghi"[..], ErrorKind::Permutation) + ))) + ); + + let e = &b"efgabc"[..]; + assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(4)))); + } + + /* + named!(does_not_compile, + alt!(tag!("abcd"), tag!("efgh")) + ); + */ +} diff --git a/third_party/rust/nom/src/branch/mod.rs b/third_party/rust/nom/src/branch/mod.rs new file mode 100644 index 0000000000..55d3633c8f --- /dev/null +++ b/third_party/rust/nom/src/branch/mod.rs @@ -0,0 +1,263 @@ +//! choice combinators + +#[macro_use] +mod macros; + +use crate::error::ErrorKind; +use crate::error::ParseError; +use crate::internal::{Err, IResult}; + +/// helper trait for the [alt()] combinator +/// +/// this trait is implemented for tuples of up to 21 elements +pub trait Alt { + /// tests each parser in the tuple and returns the result of the first one that succeeds + fn choice(&self, input: I) -> IResult; +} + +/// tests a list of parsers one by one until one succeeds +/// +/// It takes as argument a tuple of parsers. +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, Needed, IResult}; +/// use nom::character::complete::{alpha1, digit1}; +/// use nom::branch::alt; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// alt((alpha1, digit1))(input) +/// }; +/// +/// // the first parser, alpha1, recognizes the input +/// assert_eq!(parser("abc"), Ok(("", "abc"))); +/// +/// // the first parser returns an error, so alt tries the second one +/// assert_eq!(parser("123456"), Ok(("", "123456"))); +/// +/// // both parsers failed, and with the default error type, alt will return the last error +/// assert_eq!(parser(" "), Err(Err::Error(error_position!(" ", ErrorKind::Digit)))); +/// # } +/// ``` +/// +/// with a custom error type, it is possible to have alt return the error of the parser +/// that went the farthest in the input data +pub fn alt, List: Alt>(l: List) -> impl Fn(I) -> IResult { + move |i: I| l.choice(i) +} + +/// helper trait for the [permutation()] combinator +/// +/// this trait is implemented for tuples of up to 21 elements +pub trait Permutation { + /// tries to apply all parsers in the tuple in various orders until all of them succeed + fn permutation(&self, input: I) -> IResult; +} + +/// applies a list of parsers in any order +/// +/// permutation will succeed if all of the child parsers succeeded. +/// It takes as argument a tuple of parsers, and returns a +/// tuple of the parser results. +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, Needed, IResult}; +/// use nom::character::complete::{alpha1, digit1}; +/// use nom::branch::permutation; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, (&str, &str)> { +/// permutation((alpha1, digit1))(input) +/// }; +/// +/// // permutation recognizes alphabetic characters then digit +/// assert_eq!(parser("abc123"), Ok(("", ("abc", "123")))); +/// +/// // but also in inverse order +/// assert_eq!(parser("123abc"), Ok(("", ("abc", "123")))); +/// +/// // it will fail if one of the parsers failed +/// assert_eq!(parser("abc;"), Err(Err::Error(error_position!(";", ErrorKind::Permutation)))); +/// # } +/// ``` +pub fn permutation, List: Permutation>(l: List) -> impl Fn(I) -> IResult { + move |i: I| l.permutation(i) +} + +macro_rules! alt_trait( + ($first:ident $second:ident $($id: ident)+) => ( + alt_trait!(__impl $first $second; $($id)+); + ); + (__impl $($current:ident)*; $head:ident $($id: ident)+) => ( + alt_trait_impl!($($current)*); + + alt_trait!(__impl $($current)* $head; $($id)+); + ); + (__impl $($current:ident)*; $head:ident) => ( + alt_trait_impl!($($current)*); + alt_trait_impl!($($current)* $head); + ); +); + +macro_rules! alt_trait_impl( + ($($id:ident)+) => ( + impl< + Input: Clone, Output, Error: ParseError, + $($id: Fn(Input) -> IResult),+ + > Alt for ( $($id),+ ) { + + fn choice(&self, input: Input) -> IResult { + let mut err: Option = None; + alt_trait_inner!(0, self, input, err, $($id)+); + + Err(Err::Error(Error::append(input, ErrorKind::Alt, err.unwrap()))) + } + } + ); +); + +macro_rules! alt_trait_inner( + ($it:tt, $self:expr, $input:expr, $err:expr, $head:ident $($id:ident)+) => ( + match $self.$it($input.clone()) { + Err(Err::Error(e)) => { + $err = Some(match $err.take() { + None => e, + Some(prev) => prev.or(e), + }); + succ!($it, alt_trait_inner!($self, $input, $err, $($id)+)) + }, + res => return res, + } + ); + ($it:tt, $self:expr, $input:expr, $err:expr, $head:ident) => ( + match $self.$it($input.clone()) { + Err(Err::Error(e)) => { + $err = Some(match $err.take() { + None => e, + Some(prev) => prev.or(e), + }); + }, + res => return res, + } + ); +); + +alt_trait!(A B C D E F G H I J K L M N O P Q R S T U); + +macro_rules! permutation_trait( + ($name1:ident $ty1:ident, $name2:ident $ty2:ident) => ( + permutation_trait_impl!($name1 $ty1, $name2 $ty2); + ); + ($name1:ident $ty1:ident, $name2: ident $ty2:ident, $($name:ident $ty:ident),*) => ( + permutation_trait!(__impl $name1 $ty1, $name2 $ty2; $($name $ty),*); + ); + (__impl $($name:ident $ty: ident),+; $name1:ident $ty1:ident, $($name2:ident $ty2:ident),*) => ( + permutation_trait_impl!($($name $ty),+); + permutation_trait!(__impl $($name $ty),+ , $name1 $ty1; $($name2 $ty2),*); + ); + (__impl $($name:ident $ty: ident),+; $name1:ident $ty1:ident) => ( + permutation_trait_impl!($($name $ty),+); + permutation_trait_impl!($($name $ty),+, $name1 $ty1); + ); +); + +macro_rules! permutation_trait_impl( + ($($name:ident $ty: ident),+) => ( + impl< + Input: Clone, $($ty),+ , Error: ParseError, + $($name: Fn(Input) -> IResult),+ + > Permutation for ( $($name),+ ) { + + fn permutation(&self, mut input: Input) -> IResult { + let mut res = permutation_init!((), $($name),+); + + loop { + let mut all_done = true; + permutation_trait_inner!(0, self, input, res, all_done, $($name)+); + + //if we reach that part, it means none of the parsers were able to read anything + if !all_done { + //FIXME: should wrap the error returned by the child parser + return Err(Err::Error(error_position!(input, ErrorKind::Permutation))); + } + break; + } + + if let Some(unwrapped_res) = { permutation_trait_unwrap!(0, (), res, $($name),+) } { + Ok((input, unwrapped_res)) + } else { + Err(Err::Error(error_position!(input, ErrorKind::Permutation))) + } + } + } + ); +); + +macro_rules! permutation_trait_inner( + ($it:tt, $self:expr, $input:ident, $res:expr, $all_done:expr, $head:ident $($id:ident)+) => ({ + if $res.$it.is_none() { + match $self.$it($input.clone()) { + Ok((i,o)) => { + $input = i; + $res.$it = Some(o); + continue; + }, + Err(Err::Error(_)) => { + $all_done = false; + }, + Err(e) => { + return Err(e); + } + }; + } + succ!($it, permutation_trait_inner!($self, $input, $res, $all_done, $($id)+)); + }); + ($it:tt, $self:expr, $input:ident, $res:expr, $all_done:expr, $head:ident) => ({ + if $res.$it.is_none() { + match $self.$it($input.clone()) { + Ok((i,o)) => { + $input = i; + $res.$it = Some(o); + continue; + }, + Err(Err::Error(_)) => { + $all_done = false; + }, + Err(e) => { + return Err(e); + } + }; + } + }); +); + +macro_rules! permutation_trait_unwrap ( + ($it:tt, (), $res:ident, $e:ident, $($name:ident),+) => ({ + let res = $res.$it; + if res.is_some() { + succ!($it, permutation_trait_unwrap!((res.unwrap()), $res, $($name),+)) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, ($($parsed:expr),*), $res:ident, $e:ident, $($name:ident),+) => ({ + let res = $res.$it; + if res.is_some() { + succ!($it, permutation_trait_unwrap!(($($parsed),* , res.unwrap()), $res, $($name),+)) + } else { + $crate::lib::std::option::Option::None + } + }); + + ($it:tt, ($($parsed:expr),*), $res:ident, $name:ident) => ({ + let res = $res.$it; + if res.is_some() { + $crate::lib::std::option::Option::Some(($($parsed),* , res.unwrap() )) + } else { + $crate::lib::std::option::Option::None + } + }); +); + +permutation_trait!(FnA A, FnB B, FnC C, FnD D, FnE E, FnF F, FnG G, FnH H, FnI I, FnJ J, FnK K, FnL L, FnM M, FnN N, FnO O, FnP P, FnQ Q, FnR R, FnS S, FnT T, FnU U); diff --git a/third_party/rust/nom/src/bytes/complete.rs b/third_party/rust/nom/src/bytes/complete.rs new file mode 100644 index 0000000000..8bf42204ba --- /dev/null +++ b/third_party/rust/nom/src/bytes/complete.rs @@ -0,0 +1,658 @@ +//! parsers recognizing bytes streams, complete input version + +use crate::error::ErrorKind; +use crate::error::ParseError; +use crate::internal::{Err, IResult}; +use crate::lib::std::ops::RangeFrom; +use crate::lib::std::result::Result::*; +use crate::traits::{Compare, CompareResult, FindSubstring, FindToken, InputIter, InputLength, InputTake, InputTakeAtPosition, Slice, ToUsize}; + +/// Recognizes a pattern +/// +/// The input data will be compared to the tag combinator's argument and will return the part of +/// the input that matches the argument +/// +/// It will return `Err(Err::Error((_, ErrorKind::Tag)))` if the input doesn't match the pattern +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, &str> { +/// tag("Hello")(s) +/// } +/// +/// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); +/// assert_eq!(parser("Something"), Err(Err::Error(("Something", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// ``` +pub fn tag<'a, T: 'a, Input: 'a, Error: ParseError>(tag: T) -> impl Fn(Input) -> IResult +where + Input: InputTake + Compare, + T: InputLength + Clone, +{ + move |i: Input| { + let tag_len = tag.input_len(); + let t = tag.clone(); + let res: IResult<_, _, Error> = match i.compare(t) { + CompareResult::Ok => Ok(i.take_split(tag_len)), + _ => { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(Error::from_error_kind(i, e))) + } + }; + res + } +} + +/// Recognizes a case insensitive pattern +/// +/// The input data will be compared to the tag combinator's argument and will return the part of +/// the input that matches the argument with no regard to case +/// +/// It will return `Err(Err::Error((_, ErrorKind::Tag)))` if the input doesn't match the pattern +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::tag_no_case; +/// +/// fn parser(s: &str) -> IResult<&str, &str> { +/// tag_no_case("hello")(s) +/// } +/// +/// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); +/// assert_eq!(parser("hello, World!"), Ok((", World!", "hello"))); +/// assert_eq!(parser("HeLlO, World!"), Ok((", World!", "HeLlO"))); +/// assert_eq!(parser("Something"), Err(Err::Error(("Something", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// ``` +pub fn tag_no_case>(tag: T) -> impl Fn(Input) -> IResult +where + Input: InputTake + Compare, + T: InputLength + Clone, +{ + move |i: Input| { + let tag_len = tag.input_len(); + let t = tag.clone(); + + let res: IResult<_, _, Error> = match (i).compare_no_case(t) { + CompareResult::Ok => Ok(i.take_split(tag_len)), + _ => { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(Error::from_error_kind(i, e))) + } + }; + res + } +} + +/// Parse till certain characters are met +/// +/// The parser will return the longest slice till one of the characters of the combinator's argument are met. +/// +/// It doesn't consume the matched character, +/// +/// It will return a `Err::Error(("", ErrorKind::IsNot))` if the pattern wasn't met +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::is_not; +/// +/// fn not_space(s: &str) -> IResult<&str, &str> { +/// is_not(" \t\r\n")(s) +/// } +/// +/// assert_eq!(not_space("Hello, World!"), Ok((" World!", "Hello,"))); +/// assert_eq!(not_space("Sometimes\t"), Ok(("\t", "Sometimes"))); +/// assert_eq!(not_space("Nospace"), Ok(("", "Nospace"))); +/// assert_eq!(not_space(""), Err(Err::Error(("", ErrorKind::IsNot)))); +/// ``` +pub fn is_not>(arr: T) -> impl Fn(Input) -> IResult +where + Input: InputTakeAtPosition, + T: InputLength + FindToken<::Item>, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::IsNot; + i.split_at_position1_complete(|c| arr.find_token(c), e) + } +} + +/// Returns the longest slice of the matches the pattern +/// +/// The parser will return the longest slice consisting of the characters in provided in the +/// combinator's argument +/// +/// It will return a `Err(Err::Error((_, ErrorKind::IsA)))` if the pattern wasn't met +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::is_a; +/// +/// fn hex(s: &str) -> IResult<&str, &str> { +/// is_a("1234567890ABCDEF")(s) +/// } +/// +/// assert_eq!(hex("123 and voila"), Ok((" and voila", "123"))); +/// assert_eq!(hex("DEADBEEF and others"), Ok((" and others", "DEADBEEF"))); +/// assert_eq!(hex("BADBABEsomething"), Ok(("something", "BADBABE"))); +/// assert_eq!(hex("D15EA5E"), Ok(("", "D15EA5E"))); +/// assert_eq!(hex(""), Err(Err::Error(("", ErrorKind::IsA)))); +/// ``` +pub fn is_a>(arr: T) -> impl Fn(Input) -> IResult +where + Input: InputTakeAtPosition, + T: InputLength + FindToken<::Item>, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::IsA; + i.split_at_position1_complete(|c| !arr.find_token(c), e) + } +} + +/// Returns the longest input slice (if any) that matches the predicate +/// +/// The parser will return the longest slice that matches the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take_while; +/// use nom::character::is_alphabetic; +/// +/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// take_while(is_alphabetic)(s) +/// } +/// +/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(alpha(b"12345"), Ok((&b"12345"[..], &b""[..]))); +/// assert_eq!(alpha(b"latin"), Ok((&b""[..], &b"latin"[..]))); +/// assert_eq!(alpha(b""), Ok((&b""[..], &b""[..]))); +/// ``` +pub fn take_while>(cond: F) -> impl Fn(Input) -> IResult +where + Input: InputTakeAtPosition, + F: Fn(::Item) -> bool, +{ + move |i: Input| i.split_at_position_complete(|c| !cond(c)) +} + +/// Returns the longest (atleast 1) input slice that matches the predicate +/// +/// The parser will return the longest slice that matches the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// It will return an `Err(Err::Error((_, ErrorKind::TakeWhile1)))` if the pattern wasn't met +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take_while1; +/// use nom::character::is_alphabetic; +/// +/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// take_while1(is_alphabetic)(s) +/// } +/// +/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(alpha(b"latin"), Ok((&b""[..], &b"latin"[..]))); +/// assert_eq!(alpha(b"12345"), Err(Err::Error((&b"12345"[..], ErrorKind::TakeWhile1)))); +/// ``` +pub fn take_while1>(cond: F) -> impl Fn(Input) -> IResult +where + Input: InputTakeAtPosition, + F: Fn(::Item) -> bool, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::TakeWhile1; + i.split_at_position1_complete(|c| !cond(c), e) + } +} + +/// Returns the longest (m <= len <= n) input slice that matches the predicate +/// +/// The parser will return the longest slice that matches the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// It will return an `Err::Error((_, ErrorKind::TakeWhileMN))` if the pattern wasn't met or is out +/// of range (m <= len <= n) +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take_while_m_n; +/// use nom::character::is_alphabetic; +/// +/// fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// take_while_m_n(3, 6, is_alphabetic)(s) +/// } +/// +/// assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(short_alpha(b"lengthy"), Ok((&b"y"[..], &b"length"[..]))); +/// assert_eq!(short_alpha(b"latin"), Ok((&b""[..], &b"latin"[..]))); +/// assert_eq!(short_alpha(b"ed"), Err(Err::Error((&b"ed"[..], ErrorKind::TakeWhileMN)))); +/// assert_eq!(short_alpha(b"12345"), Err(Err::Error((&b"12345"[..], ErrorKind::TakeWhileMN)))); +/// ``` +pub fn take_while_m_n>(m: usize, n: usize, cond: F) -> impl Fn(Input) -> IResult +where + Input: InputTake + InputIter + InputLength + Slice>, + F: Fn(::Item) -> bool, +{ + move |i: Input| { + let input = i; + + match input.position(|c| !cond(c)) { + Some(idx) => { + if idx >= m { + if idx <= n { + let res: IResult<_, _, Error> = if let Some(index) = input.slice_index(idx) { + Ok(input.take_split(index)) + } else { + Err(Err::Error(Error::from_error_kind(input, ErrorKind::TakeWhileMN))) + }; + res + } else { + let res: IResult<_, _, Error> = if let Some(index) = input.slice_index(n) { + Ok(input.take_split(index)) + } else { + Err(Err::Error(Error::from_error_kind(input, ErrorKind::TakeWhileMN))) + }; + res + } + } else { + let e = ErrorKind::TakeWhileMN; + Err(Err::Error(Error::from_error_kind(input, e))) + } + } + None => { + let len = input.input_len(); + if len >= n { + match input.slice_index(n) { + Some(index) => Ok(input.take_split(index)), + None => Err(Err::Error(Error::from_error_kind(input, ErrorKind::TakeWhileMN))) + } + } else { + if len >= m && len <= n { + let res: IResult<_, _, Error> = Ok((input.slice(len..), input)); + res + } else { + let e = ErrorKind::TakeWhileMN; + Err(Err::Error(Error::from_error_kind(input, e))) + } + } + } + } + } +} + +/// Returns the longest input slice (if any) till a predicate is met +/// +/// The parser will return the longest slice till the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take_till; +/// +/// fn till_colon(s: &str) -> IResult<&str, &str> { +/// take_till(|c| c == ':')(s) +/// } +/// +/// assert_eq!(till_colon("latin:123"), Ok((":123", "latin"))); +/// assert_eq!(till_colon(":empty matched"), Ok((":empty matched", ""))); //allowed +/// assert_eq!(till_colon("12345"), Ok(("", "12345"))); +/// assert_eq!(till_colon(""), Ok(("", ""))); +/// ``` +pub fn take_till>(cond: F) -> impl Fn(Input) -> IResult +where + Input: InputTakeAtPosition, + F: Fn(::Item) -> bool, +{ + move |i: Input| i.split_at_position_complete(|c| cond(c)) +} + +/// Returns the longest (atleast 1) input slice till a predicate is met +/// +/// The parser will return the longest slice till the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// It will return `Err(Err::Error((_, ErrorKind::TakeTill1)))` if the input is empty or the +/// predicate matches the first input +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take_till1; +/// +/// fn till_colon(s: &str) -> IResult<&str, &str> { +/// take_till1(|c| c == ':')(s) +/// } +/// +/// assert_eq!(till_colon("latin:123"), Ok((":123", "latin"))); +/// assert_eq!(till_colon(":empty matched"), Err(Err::Error((":empty matched", ErrorKind::TakeTill1)))); +/// assert_eq!(till_colon("12345"), Ok(("", "12345"))); +/// assert_eq!(till_colon(""), Err(Err::Error(("", ErrorKind::TakeTill1)))); +/// ``` +pub fn take_till1>(cond: F) -> impl Fn(Input) -> IResult +where + Input: InputTakeAtPosition, + F: Fn(::Item) -> bool, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::TakeTill1; + i.split_at_position1_complete(|c| cond(c), e) + } +} + +/// Returns an input slice containing the first N input elements (Input[..N]) +/// +/// It will return `Err(Err::Error((_, ErrorKind::Eof)))` if the input is shorter than the argument +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take; +/// +/// fn take6(s: &str) -> IResult<&str, &str> { +/// take(6usize)(s) +/// } +/// +/// assert_eq!(take6("1234567"), Ok(("7", "123456"))); +/// assert_eq!(take6("things"), Ok(("", "things"))); +/// assert_eq!(take6("short"), Err(Err::Error(("short", ErrorKind::Eof)))); +/// assert_eq!(take6(""), Err(Err::Error(("", ErrorKind::Eof)))); +/// ``` +pub fn take>(count: C) -> impl Fn(Input) -> IResult +where + Input: InputIter + InputTake, + C: ToUsize, +{ + let c = count.to_usize(); + move |i: Input| match i.slice_index(c) { + None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), + Some(index) => Ok(i.take_split(index)), + } +} + +/// Returns the longest input slice till it matches the pattern. +/// +/// It doesn't consume the pattern. It will return `Err(Err::Error((_, ErrorKind::TakeUntil)))` +/// if the pattern wasn't met +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::complete::take_until; +/// +/// fn until_eof(s: &str) -> IResult<&str, &str> { +/// take_until("eof")(s) +/// } +/// +/// assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world"))); +/// assert_eq!(until_eof("hello, world"), Err(Err::Error(("hello, world", ErrorKind::TakeUntil)))); +/// assert_eq!(until_eof(""), Err(Err::Error(("", ErrorKind::TakeUntil)))); +/// ``` +pub fn take_until>(tag: T) -> impl Fn(Input) -> IResult +where + Input: InputTake + FindSubstring, + T: InputLength + Clone, +{ + move |i: Input| { + let t = tag.clone(); + let res: IResult<_, _, Error> = match i.find_substring(t) { + None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::TakeUntil))), + Some(index) => Ok(i.take_split(index)), + }; + res + } +} + +/// Matches a byte string with escaped characters. +/// +/// * The first argument matches the normal characters (it must not accept the control character), +/// * the second argument is the control character (like `\` in most languages), +/// * the third argument matches the escaped characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// # use nom::character::complete::digit1; +/// use nom::bytes::complete::escaped; +/// use nom::character::complete::one_of; +/// +/// fn esc(s: &str) -> IResult<&str, &str> { +/// escaped(digit1, '\\', one_of(r#""n\"#))(s) +/// } +/// +/// assert_eq!(esc("123;"), Ok((";", "123"))); +/// assert_eq!(esc(r#"12\"34;"#), Ok((";", r#"12\"34"#))); +/// ``` +/// +pub fn escaped(normal: F, control_char: char, escapable: G) -> impl Fn(Input) -> IResult +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice> + InputIter, + ::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult, + G: Fn(Input) -> IResult, + Error: ParseError, +{ + use crate::traits::AsChar; + + move |input: Input| { + let mut i = input.clone(); + + while i.input_len() > 0 { + match normal(i.clone()) { + Ok((i2, _)) => { + if i2.input_len() == 0 { + return Ok((input.slice(input.input_len()..), input)); + } else { + i = i2; + } + } + Err(Err::Error(_)) => { + // unwrap() should be safe here since index < $i.input_len() + if i.iter_elements().next().unwrap().as_char() == control_char { + let next = control_char.len_utf8(); + if next >= i.input_len() { + return Err(Err::Error(Error::from_error_kind(input, ErrorKind::Escaped))); + } else { + match escapable(i.slice(next..)) { + Ok((i2, _)) => { + if i2.input_len() == 0 { + return Ok((input.slice(input.input_len()..), input)); + } else { + i = i2; + } + } + Err(e) => return Err(e), + } + } + } else { + let index = input.offset(&i); + if index == 0 { + return Err(Err::Error(Error::from_error_kind(input, ErrorKind::Escaped))); + } + return Ok(input.take_split(index)); + } + } + Err(e) => { + return Err(e); + } + } + } + + Ok((input.slice(input.input_len()..), input)) + } +} + +#[doc(hidden)] +pub fn escapedc(i: Input, normal: F, control_char: char, escapable: G) -> IResult +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice> + InputIter, + ::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult, + G: Fn(Input) -> IResult, + Error: ParseError, +{ + escaped(normal, control_char, escapable)(i) +} + +/// Matches a byte string with escaped characters. +/// +/// * The first argument matches the normal characters (it must not match the control character), +/// * the second argument is the control character (like `\` in most languages), +/// * the third argument matches the escaped characters and transforms them. +/// +/// As an example, the chain `abc\tdef` could be `abc def` (it also consumes the control character) +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// # use std::str::from_utf8; +/// use nom::bytes::complete::escaped_transform; +/// use nom::character::complete::alpha1; +/// +/// fn parser(input: &str) -> IResult<&str, String> { +/// escaped_transform( +/// alpha1, +/// '\\', +/// |i:&str| alt!(i, +/// tag!("\\") => { |_| "\\" } +/// | tag!("\"") => { |_| "\"" } +/// | tag!("n") => { |_| "\n" } +/// ) +/// )(input) +/// } +/// +/// assert_eq!(parser("ab\\\"cd"), Ok(("", String::from("ab\"cd")))); +/// ``` +#[cfg(feature = "alloc")] +pub fn escaped_transform( + normal: F, + control_char: char, + transform: G, +) -> impl Fn(Input) -> IResult +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice> + InputIter, + Input: crate::traits::ExtendInto, + O1: crate::traits::ExtendInto, + O2: crate::traits::ExtendInto, + Output: core::iter::Extend<::Item>, + Output: core::iter::Extend<::Item>, + Output: core::iter::Extend<::Item>, + ::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult, + G: Fn(Input) -> IResult, + Error: ParseError, +{ + use crate::traits::AsChar; + + move |input: Input| { + let mut index = 0; + let mut res = input.new_builder(); + + let i = input.clone(); + + while index < i.input_len() { + let remainder = i.slice(index..); + match normal(remainder.clone()) { + Ok((i2, o)) => { + o.extend_into(&mut res); + if i2.input_len() == 0 { + return Ok((i.slice(i.input_len()..), res)); + } else { + index = input.offset(&i2); + } + } + Err(Err::Error(_)) => { + // unwrap() should be safe here since index < $i.input_len() + if remainder.iter_elements().next().unwrap().as_char() == control_char { + let next = index + control_char.len_utf8(); + let input_len = input.input_len(); + + if next >= input_len { + return Err(Err::Error(Error::from_error_kind(remainder, ErrorKind::EscapedTransform))); + } else { + match transform(i.slice(next..)) { + Ok((i2, o)) => { + o.extend_into(&mut res); + if i2.input_len() == 0 { + return Ok((i.slice(i.input_len()..), res)); + } else { + index = input.offset(&i2); + } + } + Err(e) => return Err(e), + } + } + } else { + if index == 0 { + return Err(Err::Error(Error::from_error_kind(remainder, ErrorKind::EscapedTransform))); + } + return Ok((remainder, res)); + } + } + Err(e) => return Err(e), + } + } + Ok((input.slice(index..), res)) + } +} + +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn escaped_transformc( + i: Input, + normal: F, + control_char: char, + transform: G, +) -> IResult +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice> + InputIter, + Input: crate::traits::ExtendInto, + O1: crate::traits::ExtendInto, + O2: crate::traits::ExtendInto, + Output: core::iter::Extend<::Item>, + Output: core::iter::Extend<::Item>, + Output: core::iter::Extend<::Item>, + ::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult, + G: Fn(Input) -> IResult, + Error: ParseError, +{ + escaped_transform(normal, control_char, transform)(i) + +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn complete_take_while_m_n_utf8_all_matching() { + let result: IResult<&str, &str> = super::take_while_m_n(1, 4, |c: char| c.is_alphabetic())("øn"); + assert_eq!(result, Ok(("", "øn"))); + } + + #[test] + fn complete_take_while_m_n_utf8_all_matching_substring() { + let result: IResult<&str, &str> = super::take_while_m_n(1, 1, |c: char| c.is_alphabetic())("øn"); + assert_eq!(result, Ok(("n", "ø"))); + } +} diff --git a/third_party/rust/nom/src/bytes/macros.rs b/third_party/rust/nom/src/bytes/macros.rs new file mode 100644 index 0000000000..1e24ba8851 --- /dev/null +++ b/third_party/rust/nom/src/bytes/macros.rs @@ -0,0 +1,938 @@ +//! Byte level parsers and combinators +//! +#[allow(unused_variables)] + +/// `tag!(&[T]: nom::AsBytes) => &[T] -> IResult<&[T], &[T]>` +/// declares a byte array as a suite to recognize +/// +/// consumes the recognized characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(x, tag!("abcd")); +/// let r = x(&b"abcdefgh"[..]); +/// assert_eq!(r, Ok((&b"efgh"[..], &b"abcd"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! tag ( + ($i:expr, $tag: expr) => ({ + $crate::bytes::streaming::tag($tag)($i) + }); +); + +/// `tag_no_case!(&[T]) => &[T] -> IResult<&[T], &[T]>` +/// declares a case insensitive ascii string as a suite to recognize +/// +/// consumes the recognized characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(test, tag_no_case!("ABcd")); +/// +/// let r = test(&b"aBCdefgh"[..]); +/// assert_eq!(r, Ok((&b"efgh"[..], &b"aBCd"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! tag_no_case ( + ($i:expr, $tag: expr) => ({ + $crate::bytes::streaming::tag_no_case($tag)($i) + }); +); + +/// `is_not!(&[T:AsBytes]) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest list of bytes that do not appear in the provided array +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!( not_space, is_not!( " \t\r\n" ) ); +/// +/// let r = not_space(&b"abcdefgh\nijkl"[..]); +/// assert_eq!(r, Ok((&b"\nijkl"[..], &b"abcdefgh"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! is_not ( + ($input:expr, $arr:expr) => ({ + $crate::bytes::streaming::is_not($arr)($input) + }); +); + +/// `is_a!(&[T]) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest list of bytes that appear in the provided array +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(abcd, is_a!( "abcd" )); +/// +/// let r1 = abcd(&b"aaaaefgh"[..]); +/// assert_eq!(r1, Ok((&b"efgh"[..], &b"aaaa"[..]))); +/// +/// let r2 = abcd(&b"dcbaefgh"[..]); +/// assert_eq!(r2, Ok((&b"efgh"[..], &b"dcba"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! is_a ( + ($input:expr, $arr:expr) => ({ + $crate::bytes::streaming::is_a($arr)($input) + }); +); + +/// `escaped!(T -> IResult, U, T -> IResult) => T -> IResult where T: InputIter, +/// U: AsChar` +/// matches a byte string with escaped characters. +/// +/// The first argument matches the normal characters (it must not accept the control character), +/// the second argument is the control character (like `\` in most languages), +/// the third argument matches the escaped characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::character::complete::digit1; +/// # fn main() { +/// named!(esc, escaped!(call!(digit1), '\\', one_of!("\"n\\"))); +/// assert_eq!(esc(&b"123;"[..]), Ok((&b";"[..], &b"123"[..]))); +/// assert_eq!(esc(&b"12\\\"34;"[..]), Ok((&b";"[..], &b"12\\\"34"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! escaped ( + ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( + { + escaped!($i, |i| $submac1!(i, $($args)*), $control_char, |i| $submac2!(i, $($args2)*)) + } + ); + ($i:expr, $normal:expr, $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( + { + escaped!($i, $normal, $control_char, |i| $submac2!(i, $($args2)*)) + } + ); + ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $escapable:expr ) => ( + { + escaped!($i, |i| $submac1!(i, $($args)*), $control_char, $escapable) + } + ); + ($i:expr, $normal:expr, $control_char: expr, $escapable:expr) => ( + { + $crate::bytes::complete::escapedc($i, $normal, $control_char, $escapable) + } + ); +); + +/// `escaped_transform!(&[T] -> IResult<&[T], &[T]>, T, &[T] -> IResult<&[T], &[T]>) => &[T] -> IResult<&[T], Vec>` +/// matches a byte string with escaped characters. +/// +/// The first argument matches the normal characters (it must not match the control character), +/// the second argument is the control character (like `\` in most languages), +/// the third argument matches the escaped characters and transforms them. +/// +/// As an example, the chain `abc\tdef` could be `abc def` (it also consumes the control character) +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::character::complete::alpha1; +/// # use nom::lib::std::str::from_utf8; +/// # fn main() { +/// fn to_s(i:Vec) -> String { +/// String::from_utf8_lossy(&i).into_owned() +/// } +/// +/// named!(transform < String >, +/// map!( +/// escaped_transform!(call!(alpha1), '\\', +/// alt!( +/// tag!("\\") => { |_| &b"\\"[..] } +/// | tag!("\"") => { |_| &b"\""[..] } +/// | tag!("n") => { |_| &b"\n"[..] } +/// ) +/// ), to_s +/// ) +/// ); +/// assert_eq!(transform(&b"ab\\\"cd"[..]), Ok((&b""[..], String::from("ab\"cd")))); +/// # } +/// ``` +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! escaped_transform ( + ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( + { + escaped_transform!($i, |i| $submac1!(i, $($args)*), $control_char, |i| $submac2!(i, $($args2)*)) + } + ); + ($i:expr, $normal:expr, $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => ( + { + escaped_transform!($i, $normal, $control_char, |i| $submac2!(i, $($args2)*)) + } + ); + ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $transform:expr ) => ( + { + escaped_transform!($i, |i| $submac1!(i, $($args)*), $control_char, $transform) + } + ); + ($i:expr, $normal:expr, $control_char: expr, $transform:expr) => ( + { + $crate::bytes::complete::escaped_transformc($i, $normal, $control_char, $transform) + } + ); +); + +/// `take_while!(T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest list of bytes until the provided function fails. +/// +/// The argument is either a function `T -> bool` or a macro returning a `bool`. +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::character::is_alphanumeric; +/// # fn main() { +/// named!( alpha, take_while!( is_alphanumeric ) ); +/// +/// let r = alpha(&b"abcd\nefgh"[..]); +/// assert_eq!(r, Ok((&b"\nefgh"[..], &b"abcd"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_while ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ({ + let res: $crate::IResult<_, _, _> = take_while!($input, (|c| $submac!(c, $($args)*))); + res + }); + ($input:expr, $f:expr) => ( + $crate::bytes::streaming::take_while($f)($input) + ); +); + +/// `take_while1!(T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest (non empty) list of bytes until the provided function fails. +/// +/// The argument is either a function `&[T] -> bool` or a macro returning a `bool` +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind}; +/// # use nom::character::is_alphanumeric; +/// # fn main() { +/// named!( alpha, take_while1!( is_alphanumeric ) ); +/// +/// let r = alpha(&b"abcd\nefgh"[..]); +/// assert_eq!(r, Ok((&b"\nefgh"[..], &b"abcd"[..]))); +/// let r = alpha(&b"\nefgh"[..]); +/// assert_eq!(r, Err(Err::Error(error_position!(&b"\nefgh"[..], ErrorKind::TakeWhile1)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_while1 ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ({ + let res: $crate::IResult<_, _, _> = take_while1!($input, (|c| $submac!(c, $($args)*))); + res + }); + ($input:expr, $f:expr) => ( + $crate::bytes::streaming::take_while1($f)($input) + ); +); + +/// `take_while_m_n!(m: usize, n: usize, T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns a list of bytes or characters for which the provided function returns true. +/// the returned list's size will be at least m, and at most n +/// +/// The argument is either a function `T -> bool` or a macro returning a `bool`. +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::character::is_alphanumeric; +/// # fn main() { +/// named!( alpha, take_while_m_n!(3, 6, is_alphanumeric ) ); +/// +/// let r = alpha(&b"abcd\nefgh"[..]); +/// assert_eq!(r, Ok((&b"\nefgh"[..], &b"abcd"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_while_m_n ( + ($input:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* )) => ({ + let res: $crate::IResult<_, _, _> = take_while_m_n!($input, $m, $n, (|c| $submac!(c, $($args)*))); + res + }); + ($input:expr, $m:expr, $n:expr, $f:expr) => ( + $crate::bytes::streaming::take_while_m_n($m, $n, $f)($input) + ); +); + +/// `take_till!(T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest list of bytes until the provided function succeeds +/// +/// The argument is either a function `&[T] -> bool` or a macro returning a `bool`. +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!( till_colon, take_till!(|ch| ch == b':') ); +/// +/// let r = till_colon(&b"abcd:efgh"[..]); +/// assert_eq!(r, Ok((&b":efgh"[..], &b"abcd"[..]))); +/// let r2 = till_colon(&b":abcdefgh"[..]); // empty match is allowed +/// assert_eq!(r2, Ok((&b":abcdefgh"[..], &b""[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_till ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ({ + let res: $crate::IResult<_, _, _> = take_till!($input, (|c| $submac!(c, $($args)*))); + res + }); + ($input:expr, $f:expr) => ( + $crate::bytes::streaming::take_till($f)($input) + ); +); + +/// `take_till1!(T -> bool) => &[T] -> IResult<&[T], &[T]>` +/// returns the longest non empty list of bytes until the provided function succeeds +/// +/// The argument is either a function `&[T] -> bool` or a macro returning a `bool`. +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind}; +/// # fn main() { +/// named!( till1_colon, take_till1!(|ch| ch == b':') ); +/// +/// let r = till1_colon(&b"abcd:efgh"[..]); +/// assert_eq!(r, Ok((&b":efgh"[..], &b"abcd"[..]))); +/// +/// let r2 = till1_colon(&b":abcdefgh"[..]); // empty match is error +/// assert_eq!(r2, Err(Err::Error(error_position!(&b":abcdefgh"[..], ErrorKind::TakeTill1)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_till1 ( + ($input:expr, $submac:ident!( $($args:tt)* )) => ({ + let res: $crate::IResult<_, _, _> = take_till1!($input, (|c| $submac!(c, $($args)*))); + res + }); + ($input:expr, $f:expr) => ( + $crate::bytes::streaming::take_till1($f)($input) + ); +); + +/// `take!(nb) => &[T] -> IResult<&[T], &[T]>` +/// generates a parser consuming the specified number of bytes +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// // Desmond parser +/// named!(take5, take!( 5 ) ); +/// +/// let a = b"abcdefgh"; +/// +/// assert_eq!(take5(&a[..]), Ok((&b"fgh"[..], &b"abcde"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take ( + ($i:expr, $count:expr) => ({ + let c = $count as usize; + let res: $crate::IResult<_,_,_> = $crate::bytes::streaming::take(c)($i); + res + }); +); + +/// `take_str!(nb) => &[T] -> IResult<&[T], &str>` +/// same as take! but returning a &str +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(take5( &[u8] ) -> &str, take_str!( 5 ) ); +/// +/// let a = b"abcdefgh"; +/// +/// assert_eq!(take5(&a[..]), Ok((&b"fgh"[..], "abcde"))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_str ( + ( $i:expr, $size:expr ) => ( + { + let input: &[u8] = $i; + + map_res!(input, take!($size), $crate::lib::std::str::from_utf8) + } + ); +); + +/// `take_until!(tag) => &[T] -> IResult<&[T], &[T]>` +/// consumes data until it finds the specified tag. +/// +/// The remainder still contains the tag. +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(x, take_until!("foo")); +/// let r = x(&b"abcd foo efgh"[..]); +/// assert_eq!(r, Ok((&b"foo efgh"[..], &b"abcd "[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_until ( + ($i:expr, $substr:expr) => ({ + let res: $crate::IResult<_,_,_> = $crate::bytes::streaming::take_until($substr)($i); + res + }); +); + +/// `take_until1!(tag) => &[T] -> IResult<&[T], &[T]>` +/// consumes data (at least one byte) until it finds the specified tag +/// +/// The remainder still contains the tag. +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(x, take_until1!("foo")); +/// +/// let r = x(&b"abcd foo efgh"[..]); +/// +/// assert_eq!(r, Ok((&b"foo efgh"[..], &b"abcd "[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! take_until1 ( + ($i:expr, $substr:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option::*; + use $crate::{Err,Needed,IResult,error::ErrorKind}; + use $crate::InputLength; + use $crate::FindSubstring; + use $crate::InputTake; + let input = $i; + + let res: IResult<_,_> = match input.find_substring($substr) { + None => { + Err(Err::Incomplete(Needed::Size(1 + $substr.input_len()))) + }, + Some(0) => { + let e = ErrorKind::TakeUntil; + Err(Err::Error(error_position!($i, e))) + }, + Some(index) => { + Ok($i.take_split(index)) + }, + }; + res + } + ); +); + +#[cfg(test)] +mod tests { + use crate::internal::{Err, Needed, IResult}; + #[cfg(feature = "alloc")] + use crate::lib::std::string::String; + #[cfg(feature = "alloc")] + use crate::lib::std::vec::Vec; + use crate::character::streaming::{alpha1 as alpha, alphanumeric1 as alphanumeric, digit1 as digit, hex_digit1 as hex_digit, multispace1 as multispace, oct_digit1 as oct_digit, space1 as space}; + use crate::error::ErrorKind; + use crate::character::is_alphabetic; + + #[cfg(feature = "alloc")] + macro_rules! one_of ( + ($i:expr, $inp: expr) => ( + { + use $crate::Err; + use $crate::Slice; + use $crate::AsChar; + use $crate::FindToken; + use $crate::InputIter; + + match ($i).iter_elements().next().map(|c| { + $inp.find_token(c) + }) { + None => Err::<_,_>(Err::Incomplete(Needed::Size(1))), + Some(false) => Err(Err::Error(error_position!($i, ErrorKind::OneOf))), + //the unwrap should be safe here + Some(true) => Ok(($i.slice(1..), $i.iter_elements().next().unwrap().as_char())) + } + } + ); + ); + + #[test] + fn is_a() { + named!(a_or_b, is_a!(&b"ab"[..])); + + let a = &b"abcd"[..]; + assert_eq!(a_or_b(a), Ok((&b"cd"[..], &b"ab"[..]))); + + let b = &b"bcde"[..]; + assert_eq!(a_or_b(b), Ok((&b"cde"[..], &b"b"[..]))); + + let c = &b"cdef"[..]; + assert_eq!(a_or_b(c), Err(Err::Error(error_position!(c, ErrorKind::IsA)))); + + let d = &b"bacdef"[..]; + assert_eq!(a_or_b(d), Ok((&b"cdef"[..], &b"ba"[..]))); + } + + #[test] + fn is_not() { + named!(a_or_b, is_not!(&b"ab"[..])); + + let a = &b"cdab"[..]; + assert_eq!(a_or_b(a), Ok((&b"ab"[..], &b"cd"[..]))); + + let b = &b"cbde"[..]; + assert_eq!(a_or_b(b), Ok((&b"bde"[..], &b"c"[..]))); + + let c = &b"abab"[..]; + assert_eq!(a_or_b(c), Err(Err::Error(error_position!(c, ErrorKind::IsNot)))); + + let d = &b"cdefba"[..]; + assert_eq!(a_or_b(d), Ok((&b"ba"[..], &b"cdef"[..]))); + + let e = &b"e"[..]; + assert_eq!(a_or_b(e), Err(Err::Incomplete(Needed::Size(1)))); + } + + #[cfg(feature = "alloc")] + #[allow(unused_variables)] + #[test] + fn escaping() { + named!(esc, escaped!(call!(alpha), '\\', one_of!("\"n\\"))); + assert_eq!(esc(&b"abcd;"[..]), Ok((&b";"[..], &b"abcd"[..]))); + assert_eq!(esc(&b"ab\\\"cd;"[..]), Ok((&b";"[..], &b"ab\\\"cd"[..]))); + assert_eq!(esc(&b"\\\"abcd;"[..]), Ok((&b";"[..], &b"\\\"abcd"[..]))); + assert_eq!(esc(&b"\\n;"[..]), Ok((&b";"[..], &b"\\n"[..]))); + assert_eq!(esc(&b"ab\\\"12"[..]), Ok((&b"12"[..], &b"ab\\\""[..]))); + assert_eq!(esc(&b"AB\\"[..]), Err(Err::Error(error_position!(&b"AB\\"[..], ErrorKind::Escaped)))); + assert_eq!( + esc(&b"AB\\A"[..]), + Err(Err::Error(error_node_position!( + &b"AB\\A"[..], + ErrorKind::Escaped, + error_position!(&b"A"[..], ErrorKind::OneOf) + ))) + ); + + named!(esc2, escaped!(call!(digit), '\\', one_of!("\"n\\"))); + assert_eq!(esc2(&b"12\\nnn34"[..]), Ok((&b"nn34"[..], &b"12\\n"[..]))); + } + + #[cfg(feature = "alloc")] + #[test] + fn escaping_str() { + named!(esc<&str, &str>, escaped!(call!(alpha), '\\', one_of!("\"n\\"))); + assert_eq!(esc("abcd;"), Ok((";", "abcd"))); + assert_eq!(esc("ab\\\"cd;"), Ok((";", "ab\\\"cd"))); + assert_eq!(esc("\\\"abcd;"), Ok((";", "\\\"abcd"))); + assert_eq!(esc("\\n;"), Ok((";", "\\n"))); + assert_eq!(esc("ab\\\"12"), Ok(("12", "ab\\\""))); + assert_eq!(esc("AB\\"), Err(Err::Error(error_position!("AB\\", ErrorKind::Escaped)))); + assert_eq!( + esc("AB\\A"), + Err(Err::Error(error_node_position!( + "AB\\A", + ErrorKind::Escaped, + error_position!("A", ErrorKind::OneOf) + ))) + ); + + named!(esc2<&str, &str>, escaped!(call!(digit), '\\', one_of!("\"n\\"))); + assert_eq!(esc2("12\\nnn34"), Ok(("nn34", "12\\n"))); + + named!(esc3<&str, &str>, escaped!(call!(alpha), '\u{241b}', one_of!("\"n"))); + assert_eq!(esc3("ab␛ncd;"), Ok((";", "ab␛ncd"))); + } + + #[cfg(feature = "alloc")] + fn to_s(i: Vec) -> String { + String::from_utf8_lossy(&i).into_owned() + } + + #[cfg(feature = "alloc")] + #[test] + fn escape_transform() { + use crate::lib::std::str; + + named!( + esc, + map!( + escaped_transform!( + alpha, + '\\', + alt!( + tag!("\\") => { |_| &b"\\"[..] } + | tag!("\"") => { |_| &b"\""[..] } + | tag!("n") => { |_| &b"\n"[..] } + ) + ), + to_s + ) + ); + + assert_eq!(esc(&b"abcd;"[..]), Ok((&b";"[..], String::from("abcd")))); + assert_eq!(esc(&b"ab\\\"cd;"[..]), Ok((&b";"[..], String::from("ab\"cd")))); + assert_eq!(esc(&b"\\\"abcd;"[..]), Ok((&b";"[..], String::from("\"abcd")))); + assert_eq!(esc(&b"\\n;"[..]), Ok((&b";"[..], String::from("\n")))); + assert_eq!(esc(&b"ab\\\"12"[..]), Ok((&b"12"[..], String::from("ab\"")))); + assert_eq!(esc(&b"AB\\"[..]), Err(Err::Error(error_position!(&b"\\"[..], ErrorKind::EscapedTransform)))); + assert_eq!( + esc(&b"AB\\A"[..]), + Err(Err::Error(error_node_position!( + &b"AB\\A"[..], + ErrorKind::EscapedTransform, + error_position!(&b"A"[..], ErrorKind::Alt) + ))) + ); + + named!( + esc2, + map!( + escaped_transform!( + call!(alpha), + '&', + alt!( + tag!("egrave;") => { |_| str::as_bytes("è") } + | tag!("agrave;") => { |_| str::as_bytes("à") } + ) + ), + to_s + ) + ); + assert_eq!(esc2(&b"abèDEF;"[..]), Ok((&b";"[..], String::from("abèDEF")))); + assert_eq!(esc2(&b"abèDàEF;"[..]), Ok((&b";"[..], String::from("abèDàEF")))); + } + + #[cfg(feature = "std")] + #[test] + fn escape_transform_str() { + named!(esc<&str, String>, escaped_transform!(alpha, '\\', + alt!( + tag!("\\") => { |_| "\\" } + | tag!("\"") => { |_| "\"" } + | tag!("n") => { |_| "\n" } + )) + ); + + assert_eq!(esc("abcd;"), Ok((";", String::from("abcd")))); + assert_eq!(esc("ab\\\"cd;"), Ok((";", String::from("ab\"cd")))); + assert_eq!(esc("\\\"abcd;"), Ok((";", String::from("\"abcd")))); + assert_eq!(esc("\\n;"), Ok((";", String::from("\n")))); + assert_eq!(esc("ab\\\"12"), Ok(("12", String::from("ab\"")))); + assert_eq!(esc("AB\\"), Err(Err::Error(error_position!("\\", ErrorKind::EscapedTransform)))); + assert_eq!( + esc("AB\\A"), + Err(Err::Error(error_node_position!( + "AB\\A", + ErrorKind::EscapedTransform, + error_position!("A", ErrorKind::Alt) + ))) + ); + + named!(esc2<&str, String>, escaped_transform!(alpha, '&', + alt!( + tag!("egrave;") => { |_| "è" } + | tag!("agrave;") => { |_| "à" } + )) + ); + assert_eq!(esc2("abèDEF;"), Ok((";", String::from("abèDEF")))); + assert_eq!(esc2("abèDàEF;"), Ok((";", String::from("abèDàEF")))); + + named!(esc3<&str, String>, escaped_transform!(alpha, '␛', + alt!( + tag!("0") => { |_| "\0" } | + tag!("n") => { |_| "\n" }))); + assert_eq!(esc3("a␛0bc␛n"), Ok(("", String::from("a\0bc\n")))); + } + + #[test] + fn take_str_test() { + let a = b"omnomnom"; + + let res: IResult<_,_,(&[u8], ErrorKind)> = take_str!(&a[..], 5u32); + assert_eq!(res, Ok((&b"nom"[..], "omnom"))); + + let res: IResult<_,_,(&[u8], ErrorKind)> = take_str!(&a[..], 9u32); + assert_eq!(res, Err(Err::Incomplete(Needed::Size(9)))); + } + + #[test] + fn take_until_incomplete() { + named!(y, take_until!("end")); + assert_eq!(y(&b"nd"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(y(&b"123"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(y(&b"123en"[..]), Err(Err::Incomplete(Needed::Size(3)))); + } + + #[test] + fn take_until_incomplete_s() { + named!(ys<&str, &str>, take_until!("end")); + assert_eq!(ys("123en"), Err(Err::Incomplete(Needed::Size(3)))); + } + + #[test] + fn recognize() { + named!(x, recognize!(delimited!(tag!("")))); + let r = x(&b" aaa"[..]); + assert_eq!(r, Ok((&b" aaa"[..], &b""[..]))); + + let semicolon = &b";"[..]; + + named!(ya, recognize!(alpha)); + let ra = ya(&b"abc;"[..]); + assert_eq!(ra, Ok((semicolon, &b"abc"[..]))); + + named!(yd, recognize!(digit)); + let rd = yd(&b"123;"[..]); + assert_eq!(rd, Ok((semicolon, &b"123"[..]))); + + named!(yhd, recognize!(hex_digit)); + let rhd = yhd(&b"123abcDEF;"[..]); + assert_eq!(rhd, Ok((semicolon, &b"123abcDEF"[..]))); + + named!(yod, recognize!(oct_digit)); + let rod = yod(&b"1234567;"[..]); + assert_eq!(rod, Ok((semicolon, &b"1234567"[..]))); + + named!(yan, recognize!(alphanumeric)); + let ran = yan(&b"123abc;"[..]); + assert_eq!(ran, Ok((semicolon, &b"123abc"[..]))); + + named!(ys, recognize!(space)); + let rs = ys(&b" \t;"[..]); + assert_eq!(rs, Ok((semicolon, &b" \t"[..]))); + + named!(yms, recognize!(multispace)); + let rms = yms(&b" \t\r\n;"[..]); + assert_eq!(rms, Ok((semicolon, &b" \t\r\n"[..]))); + } + + #[test] + fn take_while() { + named!(f, take_while!(is_alphabetic)); + let a = b""; + let b = b"abcd"; + let c = b"abcd123"; + let d = b"123"; + + assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&b[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&c[..]), Ok((&d[..], &b[..]))); + assert_eq!(f(&d[..]), Ok((&d[..], &a[..]))); + } + + #[test] + fn take_while1() { + named!(f, take_while1!(is_alphabetic)); + let a = b""; + let b = b"abcd"; + let c = b"abcd123"; + let d = b"123"; + + assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&b[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&c[..]), Ok((&b"123"[..], &b[..]))); + assert_eq!(f(&d[..]), Err(Err::Error(error_position!(&d[..], ErrorKind::TakeWhile1)))); + } + + #[test] + fn take_while_m_n() { + named!(x, take_while_m_n!(2, 4, is_alphabetic)); + let a = b""; + let b = b"a"; + let c = b"abc"; + let d = b"abc123"; + let e = b"abcde"; + let f = b"123"; + + assert_eq!(x(&a[..]), Err(Err::Incomplete(Needed::Size(2)))); + assert_eq!(x(&b[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(x(&c[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(x(&d[..]), Ok((&b"123"[..], &c[..]))); + assert_eq!(x(&e[..]), Ok((&b"e"[..], &b"abcd"[..]))); + assert_eq!(x(&f[..]), Err(Err::Error(error_position!(&f[..], ErrorKind::TakeWhileMN)))); + } + + #[test] + fn take_till() { + + named!(f, take_till!(is_alphabetic)); + let a = b""; + let b = b"abcd"; + let c = b"123abcd"; + let d = b"123"; + + assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&b[..]), Ok((&b"abcd"[..], &b""[..]))); + assert_eq!(f(&c[..]), Ok((&b"abcd"[..], &b"123"[..]))); + assert_eq!(f(&d[..]), Err(Err::Incomplete(Needed::Size(1)))); + } + + #[test] + fn take_till1() { + + named!(f, take_till1!(is_alphabetic)); + let a = b""; + let b = b"abcd"; + let c = b"123abcd"; + let d = b"123"; + + assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&b[..]), Err(Err::Error(error_position!(&b[..], ErrorKind::TakeTill1)))); + assert_eq!(f(&c[..]), Ok((&b"abcd"[..], &b"123"[..]))); + assert_eq!(f(&d[..]), Err(Err::Incomplete(Needed::Size(1)))); + } + + #[test] + fn take_while_utf8() { + named!(f<&str,&str>, take_while!(|c:char| { c != '點' })); + + assert_eq!(f(""), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f("abcd"), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f("abcd點"), Ok(("點", "abcd"))); + assert_eq!(f("abcd點a"), Ok(("點a", "abcd"))); + + named!(g<&str,&str>, take_while!(|c:char| { c == '點' })); + + assert_eq!(g(""), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(g("點abcd"), Ok(("abcd", "點"))); + assert_eq!(g("點點點a"), Ok(("a", "點點點"))); + } + + #[test] + fn take_till_utf8() { + named!(f<&str,&str>, take_till!(|c:char| { c == '點' })); + + assert_eq!(f(""), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f("abcd"), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f("abcd點"), Ok(("點", "abcd"))); + assert_eq!(f("abcd點a"), Ok(("點a", "abcd"))); + + named!(g<&str,&str>, take_till!(|c:char| { c != '點' })); + + assert_eq!(g(""), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(g("點abcd"), Ok(("abcd", "點"))); + assert_eq!(g("點點點a"), Ok(("a", "點點點"))); + } + + #[test] + fn take_utf8() { + named!(f<&str,&str>, take!(3)); + + assert_eq!(f(""), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(f("ab"), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(f("點"), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(f("ab點cd"), Ok(("cd", "ab點"))); + assert_eq!(f("a點bcd"), Ok(("cd", "a點b"))); + assert_eq!(f("a點b"), Ok(("", "a點b"))); + + named!(g<&str,&str>, take_while!(|c:char| { c == '點' })); + + assert_eq!(g(""), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(g("點abcd"), Ok(("abcd", "點"))); + assert_eq!(g("點點點a"), Ok(("a", "點點點"))); + } + + #[test] + fn take_while_m_n_utf8() { + named!(parser<&str, &str>, take_while_m_n!(1, 1, |c| c == 'A' || c == '😃')); + assert_eq!(parser("A!"), Ok(("!", "A"))); + assert_eq!(parser("😃!"), Ok(("!", "😃"))); + } + + #[test] + fn take_while_m_n_utf8_full_match() { + named!(parser<&str, &str>, take_while_m_n!(1, 1, |c: char| c.is_alphabetic())); + assert_eq!(parser("øn"), Ok(("n", "ø"))); + } + + #[cfg(nightly)] + use test::Bencher; + + #[cfg(nightly)] + #[bench] + fn take_while_bench(b: &mut Bencher) { + + named!(f, take_while!(is_alphabetic)); + b.iter(|| f(&b"abcdefghijklABCDEejfrfrjgro12aa"[..])); + } + + #[test] + #[cfg(feature = "std")] + fn recognize_take_while() { + use crate::character::is_alphanumeric; + named!(x, take_while!(is_alphanumeric)); + named!(y, recognize!(x)); + assert_eq!(x(&b"ab."[..]), Ok((&b"."[..], &b"ab"[..]))); + println!("X: {:?}", x(&b"ab"[..])); + assert_eq!(y(&b"ab."[..]), Ok((&b"."[..], &b"ab"[..]))); + } + + #[test] + fn length_bytes() { + use crate::number::streaming::le_u8; + named!(x, length_data!(le_u8)); + assert_eq!(x(b"\x02..>>"), Ok((&b">>"[..], &b".."[..]))); + assert_eq!(x(b"\x02.."), Ok((&[][..], &b".."[..]))); + assert_eq!(x(b"\x02."), Err(Err::Incomplete(Needed::Size(2)))); + assert_eq!(x(b"\x02"), Err(Err::Incomplete(Needed::Size(2)))); + + named!(y, do_parse!(tag!("magic") >> b: length_data!(le_u8) >> (b))); + assert_eq!(y(b"magic\x02..>>"), Ok((&b">>"[..], &b".."[..]))); + assert_eq!(y(b"magic\x02.."), Ok((&[][..], &b".."[..]))); + assert_eq!(y(b"magic\x02."), Err(Err::Incomplete(Needed::Size(2)))); + assert_eq!(y(b"magic\x02"), Err(Err::Incomplete(Needed::Size(2)))); + } + + #[cfg(feature = "alloc")] + #[test] + fn case_insensitive() { + named!(test, tag_no_case!("ABcd")); + assert_eq!(test(&b"aBCdefgh"[..]), Ok((&b"efgh"[..], &b"aBCd"[..]))); + assert_eq!(test(&b"abcdefgh"[..]), Ok((&b"efgh"[..], &b"abcd"[..]))); + assert_eq!(test(&b"ABCDefgh"[..]), Ok((&b"efgh"[..], &b"ABCD"[..]))); + assert_eq!(test(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(test(&b"Hello"[..]), Err(Err::Error(error_position!(&b"Hello"[..], ErrorKind::Tag)))); + assert_eq!(test(&b"Hel"[..]), Err(Err::Error(error_position!(&b"Hel"[..], ErrorKind::Tag)))); + + named!(test2<&str, &str>, tag_no_case!("ABcd")); + assert_eq!(test2("aBCdefgh"), Ok(("efgh", "aBCd"))); + assert_eq!(test2("abcdefgh"), Ok(("efgh", "abcd"))); + assert_eq!(test2("ABCDefgh"), Ok(("efgh", "ABCD"))); + assert_eq!(test2("ab"), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(test2("Hello"), Err(Err::Error(error_position!(&"Hello"[..], ErrorKind::Tag)))); + assert_eq!(test2("Hel"), Err(Err::Error(error_position!(&"Hel"[..], ErrorKind::Tag)))); + } + + #[test] + fn tag_fixed_size_array() { + named!(test, tag!([0x42])); + named!(test2, tag!(&[0x42])); + let input = [0x42, 0x00]; + assert_eq!(test(&input), Ok((&b"\x00"[..], &b"\x42"[..]))); + assert_eq!(test2(&input), Ok((&b"\x00"[..], &b"\x42"[..]))); + } +} diff --git a/third_party/rust/nom/src/bytes/mod.rs b/third_party/rust/nom/src/bytes/mod.rs new file mode 100644 index 0000000000..13b20126f4 --- /dev/null +++ b/third_party/rust/nom/src/bytes/mod.rs @@ -0,0 +1,7 @@ +//! parsers recognizing bytes streams + +#[macro_use] +mod macros; +pub mod streaming; +pub mod complete; + diff --git a/third_party/rust/nom/src/bytes/streaming.rs b/third_party/rust/nom/src/bytes/streaming.rs new file mode 100644 index 0000000000..f6e6706888 --- /dev/null +++ b/third_party/rust/nom/src/bytes/streaming.rs @@ -0,0 +1,645 @@ +//! parsers recognizing bytes streams, streaming version + +use crate::error::ErrorKind; +use crate::error::ParseError; +use crate::internal::{Err, IResult, Needed}; +use crate::lib::std::ops::RangeFrom; +use crate::lib::std::result::Result::*; +use crate::traits::{Compare, CompareResult, FindSubstring, FindToken, InputIter, InputLength, InputTake, InputTakeAtPosition, Slice, ToUsize}; + +/// Recognizes a pattern +/// +/// The input data will be compared to the tag combinator's argument and will return the part of +/// the input that matches the argument +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::tag; +/// +/// fn parser(s: &str) -> IResult<&str, &str> { +/// tag("Hello")(s) +/// } +/// +/// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); +/// assert_eq!(parser("Something"), Err(Err::Error(("Something", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Incomplete(Needed::Size(5)))); +/// ``` +pub fn tag<'a, T: 'a, Input: 'a, Error: ParseError>(tag: T) -> impl Fn(Input) -> IResult +where + Input: InputTake + Compare, + T: InputLength + Clone, +{ + move |i: Input| { + let tag_len = tag.input_len(); + let t = tag.clone(); + + let res: IResult<_, _, Error> = match i.compare(t) { + CompareResult::Ok => Ok(i.take_split(tag_len)), + CompareResult::Incomplete => Err(Err::Incomplete(Needed::Size(tag_len))), + CompareResult::Error => { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(Error::from_error_kind(i, e))) + } + }; + res + } +} + +/// Recognizes a case insensitive pattern +/// +/// The input data will be compared to the tag combinator's argument and will return the part of +/// the input that matches the argument with no regard to case +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::tag_no_case; +/// +/// fn parser(s: &str) -> IResult<&str, &str> { +/// tag_no_case("hello")(s) +/// } +/// +/// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); +/// assert_eq!(parser("hello, World!"), Ok((", World!", "hello"))); +/// assert_eq!(parser("HeLlO, World!"), Ok((", World!", "HeLlO"))); +/// assert_eq!(parser("Something"), Err(Err::Error(("Something", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Incomplete(Needed::Size(5)))); +/// ``` +pub fn tag_no_case>(tag: T) -> impl Fn(Input) -> IResult +where + Input: InputTake + Compare, + T: InputLength + Clone, +{ + move |i: Input| { + let tag_len = tag.input_len(); + let t = tag.clone(); + + let res: IResult<_, _, Error> = match (i).compare_no_case(t) { + CompareResult::Ok => Ok(i.take_split(tag_len)), + CompareResult::Incomplete => Err(Err::Incomplete(Needed::Size(tag_len))), + CompareResult::Error => { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(Error::from_error_kind(i, e))) + } + }; + res + } +} + +/// Parse till certain characters are met +/// +/// The parser will return the longest slice till one of the characters of the combinator's argument are met. +/// +/// It doesn't consume the matched character, +/// +/// It will return a `Err::Incomplete(Needed::Size(1))` if the pattern wasn't met +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::is_not; +/// +/// fn not_space(s: &str) -> IResult<&str, &str> { +/// is_not(" \t\r\n")(s) +/// } +/// +/// assert_eq!(not_space("Hello, World!"), Ok((" World!", "Hello,"))); +/// assert_eq!(not_space("Sometimes\t"), Ok(("\t", "Sometimes"))); +/// assert_eq!(not_space("Nospace"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(not_space(""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +pub fn is_not>(arr: T) -> impl Fn(Input) -> IResult +where + Input: InputTakeAtPosition, + T: InputLength + FindToken<::Item>, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::IsNot; + i.split_at_position1(|c| arr.find_token(c), e) + } +} + +/// Returns the longest slice of the matches the pattern +/// +/// The parser will return the longest slice consisting of the characters in provided in the +/// combinator's argument +/// +/// # Streaming specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(1))` if the pattern wasn't met +/// or if the pattern reaches the end of the input +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::is_a; +/// +/// fn hex(s: &str) -> IResult<&str, &str> { +/// is_a("1234567890ABCDEF")(s) +/// } +/// +/// assert_eq!(hex("123 and voila"), Ok((" and voila", "123"))); +/// assert_eq!(hex("DEADBEEF and others"), Ok((" and others", "DEADBEEF"))); +/// assert_eq!(hex("BADBABEsomething"), Ok(("something", "BADBABE"))); +/// assert_eq!(hex("D15EA5E"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(hex(""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +pub fn is_a>(arr: T) -> impl Fn(Input) -> IResult +where + Input: InputTakeAtPosition, + T: InputLength + FindToken<::Item>, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::IsA; + i.split_at_position1(|c| !arr.find_token(c), e) + } +} + +/// Returns the longest input slice (if any) that matches the predicate +/// +/// The parser will return the longest slice that matches the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(1))` if the pattern reaches the end of the input +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take_while; +/// use nom::character::is_alphabetic; +/// +/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// take_while(is_alphabetic)(s) +/// } +/// +/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(alpha(b"12345"), Ok((&b"12345"[..], &b""[..]))); +/// assert_eq!(alpha(b"latin"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(alpha(b""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +pub fn take_while>(cond: F) -> impl Fn(Input) -> IResult +where + Input: InputTakeAtPosition, + F: Fn(::Item) -> bool, +{ + move |i: Input| i.split_at_position(|c| !cond(c)) +} + +/// Returns the longest (atleast 1) input slice that matches the predicate +/// +/// The parser will return the longest slice that matches the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// It will return an `Err(Err::Error((_, ErrorKind::TakeWhile1)))` if the pattern wasn't met +/// +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(1))` or if the pattern reaches the end of the input. +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take_while1; +/// use nom::character::is_alphabetic; +/// +/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// take_while1(is_alphabetic)(s) +/// } +/// +/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(alpha(b"latin"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(alpha(b"12345"), Err(Err::Error((&b"12345"[..], ErrorKind::TakeWhile1)))); +/// ``` +pub fn take_while1>(cond: F) -> impl Fn(Input) -> IResult +where + Input: InputTakeAtPosition, + F: Fn(::Item) -> bool, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::TakeWhile1; + i.split_at_position1(|c| !cond(c), e) + } +} + +/// Returns the longest (m <= len <= n) input slice that matches the predicate +/// +/// The parser will return the longest slice that matches the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// It will return an `Err::Error((_, ErrorKind::TakeWhileMN))` if the pattern wasn't met +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(1))` if the pattern reaches the end of the input or is too short. +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take_while_m_n; +/// use nom::character::is_alphabetic; +/// +/// fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// take_while_m_n(3, 6, is_alphabetic)(s) +/// } +/// +/// assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); +/// assert_eq!(short_alpha(b"lengthy"), Ok((&b"y"[..], &b"length"[..]))); +/// assert_eq!(short_alpha(b"latin"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(short_alpha(b"ed"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(short_alpha(b"12345"), Err(Err::Error((&b"12345"[..], ErrorKind::TakeWhileMN)))); +/// ``` +pub fn take_while_m_n>(m: usize, n: usize, cond: F) -> impl Fn(Input) -> IResult +where + Input: InputTake + InputIter + InputLength + Slice>, + F: Fn(::Item) -> bool, +{ + move |i: Input| { + let input = i; + + match input.position(|c| !cond(c)) { + Some(idx) => { + if idx >= m { + if idx <= n { + let res: IResult<_, _, Error> = if let Some(index) = input.slice_index(idx) { + Ok(input.take_split(index)) + } else { + Err(Err::Error(Error::from_error_kind(input, ErrorKind::TakeWhileMN))) + }; + res + } else { + let res: IResult<_, _, Error> = if let Some(index) = input.slice_index(n) { + Ok(input.take_split(index)) + } else { + Err(Err::Error(Error::from_error_kind(input, ErrorKind::TakeWhileMN))) + }; + res + } + } else { + let e = ErrorKind::TakeWhileMN; + Err(Err::Error(Error::from_error_kind(input, e))) + } + } + None => { + let len = input.input_len(); + if len >= n { + match input.slice_index(n) { + Some(index) => Ok(input.take_split(index)), + None => Err(Err::Error(Error::from_error_kind(input, ErrorKind::TakeWhileMN))) + } + } else { + let needed = if m > len { m - len } else { 1 }; + Err(Err::Incomplete(Needed::Size(needed))) + } + } + } + } +} + +/// Returns the longest input slice (if any) till a predicate is met +/// +/// The parser will return the longest slice till the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(1))` if the match reaches the +/// end of input or if there was not match +/// +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take_till; +/// +/// fn till_colon(s: &str) -> IResult<&str, &str> { +/// take_till(|c| c == ':')(s) +/// } +/// +/// assert_eq!(till_colon("latin:123"), Ok((":123", "latin"))); +/// assert_eq!(till_colon(":empty matched"), Ok((":empty matched", ""))); //allowed +/// assert_eq!(till_colon("12345"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(till_colon(""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +pub fn take_till>(cond: F) -> impl Fn(Input) -> IResult +where + Input: InputTakeAtPosition, + F: Fn(::Item) -> bool, +{ + move |i: Input| i.split_at_position(|c| cond(c)) +} + +/// Returns the longest (atleast 1) input slice till a predicate is met +/// +/// The parser will return the longest slice till the given predicate *(a function that +/// takes the input and returns a bool)* +/// +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(1))` if the match reaches the +/// end of input or if there was not match +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take_till1; +/// +/// fn till_colon(s: &str) -> IResult<&str, &str> { +/// take_till1(|c| c == ':')(s) +/// } +/// +/// assert_eq!(till_colon("latin:123"), Ok((":123", "latin"))); +/// assert_eq!(till_colon(":empty matched"), Err(Err::Error((":empty matched", ErrorKind::TakeTill1)))); +/// assert_eq!(till_colon("12345"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(till_colon(""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +pub fn take_till1>(cond: F) -> impl Fn(Input) -> IResult +where + Input: InputTakeAtPosition, + F: Fn(::Item) -> bool, +{ + move |i: Input| { + let e: ErrorKind = ErrorKind::TakeTill1; + i.split_at_position1(|c| cond(c), e) + } +} + +/// Returns an input slice containing the first N input elements (Input[..N]) +/// +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(N))` where N is the +/// argument if the input is less than the length provided +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take; +/// +/// fn take6(s: &str) -> IResult<&str, &str> { +/// take(6usize)(s) +/// } +/// +/// assert_eq!(take6("1234567"), Ok(("7", "123456"))); +/// assert_eq!(take6("things"), Ok(("", "things"))); +/// assert_eq!(take6("short"), Err(Err::Incomplete(Needed::Size(6)))); //N doesn't change +/// assert_eq!(take6(""), Err(Err::Incomplete(Needed::Size(6)))); +/// ``` +pub fn take>(count: C) -> impl Fn(Input) -> IResult +where + Input: InputIter + InputTake, + C: ToUsize, +{ + let c = count.to_usize(); + move |i: Input| match i.slice_index(c) { + None => Err(Err::Incomplete(Needed::Size(c))), + Some(index) => Ok(i.take_split(index)), + } +} + +/// Returns the longest input slice till it matches the pattern. +/// +/// It doesn't consume the pattern +/// +/// # Streaming Specific +/// *Streaming version* will return a `Err::Incomplete(Needed::Size(N))` if the input doesn't +/// contain the pattern or if the input is smaller than the pattern +/// # Example +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::bytes::streaming::take_until; +/// +/// fn until_eof(s: &str) -> IResult<&str, &str> { +/// take_until("eof")(s) +/// } +/// +/// assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world"))); +/// assert_eq!(until_eof("hello, world"), Err(Err::Incomplete(Needed::Size(3)))); +/// assert_eq!(until_eof(""), Err(Err::Incomplete(Needed::Size(3)))); +/// ``` +pub fn take_until>(tag: T) -> impl Fn(Input) -> IResult +where + Input: InputTake + FindSubstring, + T: InputLength + Clone, +{ + move |i: Input| { + let len = tag.input_len(); + let t = tag.clone(); + + let res: IResult<_, _, Error> = match i.find_substring(t) { + None => Err(Err::Incomplete(Needed::Size(len))), + Some(index) => Ok(i.take_split(index)), + }; + res + } +} + +/// Matches a byte string with escaped characters. +/// +/// * The first argument matches the normal characters (it must not accept the control character), +/// * the second argument is the control character (like `\` in most languages), +/// * the third argument matches the escaped characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// # use nom::character::complete::digit1; +/// use nom::bytes::streaming::escaped; +/// use nom::character::streaming::one_of; +/// +/// fn esc(s: &str) -> IResult<&str, &str> { +/// escaped(digit1, '\\', one_of("\"n\\"))(s) +/// } +/// +/// assert_eq!(esc("123;"), Ok((";", "123"))); +/// assert_eq!(esc("12\\\"34;"), Ok((";", "12\\\"34"))); +/// ``` +/// +pub fn escaped(normal: F, control_char: char, escapable: G) -> impl Fn(Input) -> IResult +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice> + InputIter, + ::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult, + G: Fn(Input) -> IResult, + Error: ParseError, +{ + use crate::traits::AsChar; + + move |input: Input| { + let mut i = input.clone(); + + while i.input_len() > 0 { + match normal(i.clone()) { + Ok((i2, _)) => { + if i2.input_len() == 0 { + return Err(Err::Incomplete(Needed::Unknown)); + } else { + i = i2; + } + } + Err(Err::Error(_)) => { + // unwrap() should be safe here since index < $i.input_len() + if i.iter_elements().next().unwrap().as_char() == control_char { + let next = control_char.len_utf8(); + if next >= i.input_len() { + return Err(Err::Incomplete(Needed::Size(1))); + } else { + match escapable(i.slice(next..)) { + Ok((i2, _)) => { + if i2.input_len() == 0 { + return Err(Err::Incomplete(Needed::Unknown)); + } else { + i = i2; + } + } + Err(e) => return Err(e), + } + } + } else { + let index = input.offset(&i); + return Ok(input.take_split(index)); + } + } + Err(e) => { + return Err(e); + } + } + } + + Err(Err::Incomplete(Needed::Unknown)) + } +} + +#[doc(hidden)] +pub fn escapedc(i: Input, normal: F, control_char: char, escapable: G) -> IResult +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice> + InputIter, + ::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult, + G: Fn(Input) -> IResult, + Error: ParseError, +{ + escaped(normal, control_char, escapable)(i) +} + +/// Matches a byte string with escaped characters. +/// +/// * The first argument matches the normal characters (it must not match the control character), +/// * the second argument is the control character (like `\` in most languages), +/// * the third argument matches the escaped characters and transforms them. +/// +/// As an example, the chain `abc\tdef` could be `abc def` (it also consumes the control character) +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// # use std::str::from_utf8; +/// use nom::bytes::streaming::escaped_transform; +/// use nom::character::streaming::alpha1; +/// +/// fn parser(input: &str) -> IResult<&str, String> { +/// escaped_transform( +/// alpha1, +/// '\\', +/// |i:&str| alt!(i, +/// tag!("\\") => { |_| "\\" } +/// | tag!("\"") => { |_| "\"" } +/// | tag!("n") => { |_| "\n" } +/// ) +/// )(input) +/// } +/// +/// assert_eq!(parser("ab\\\"cd\""), Ok(("\"", String::from("ab\"cd")))); +/// ``` +#[cfg(feature = "alloc")] +pub fn escaped_transform( + normal: F, + control_char: char, + transform: G, +) -> impl Fn(Input) -> IResult +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice> + InputIter, + Input: crate::traits::ExtendInto, + O1: crate::traits::ExtendInto, + O2: crate::traits::ExtendInto, + Output: core::iter::Extend<::Item>, + Output: core::iter::Extend<::Item>, + Output: core::iter::Extend<::Item>, + ::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult, + G: Fn(Input) -> IResult, + Error: ParseError, +{ + use crate::traits::AsChar; + + move |input: Input| { + let mut index = 0; + let mut res = input.new_builder(); + + let i = input.clone(); + + while index < i.input_len() { + let remainder = i.slice(index..); + match normal(remainder.clone()) { + Ok((i2, o)) => { + o.extend_into(&mut res); + if i2.input_len() == 0 { + return Err(Err::Incomplete(Needed::Unknown)); + } else { + index = input.offset(&i2); + } + } + Err(Err::Error(_)) => { + // unwrap() should be safe here since index < $i.input_len() + if remainder.iter_elements().next().unwrap().as_char() == control_char { + let next = index + control_char.len_utf8(); + let input_len = input.input_len(); + + if next >= input_len { + return Err(Err::Incomplete(Needed::Unknown)); + } else { + match transform(i.slice(next..)) { + Ok((i2, o)) => { + o.extend_into(&mut res); + if i2.input_len() == 0 { + return Err(Err::Incomplete(Needed::Unknown)); + } else { + index = input.offset(&i2); + } + } + Err(e) => return Err(e), + } + } + } else { + return Ok((remainder, res)); + } + } + Err(e) => return Err(e), + } + } + Err(Err::Incomplete(Needed::Unknown)) + } +} + +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn escaped_transformc( + i: Input, + normal: F, + control_char: char, + transform: G, +) -> IResult +where + Input: Clone + crate::traits::Offset + InputLength + InputTake + InputTakeAtPosition + Slice> + InputIter, + Input: crate::traits::ExtendInto, + O1: crate::traits::ExtendInto, + O2: crate::traits::ExtendInto, + Output: core::iter::Extend<::Item>, + Output: core::iter::Extend<::Item>, + Output: core::iter::Extend<::Item>, + ::Item: crate::traits::AsChar, + F: Fn(Input) -> IResult, + G: Fn(Input) -> IResult, + Error: ParseError, +{ + escaped_transform(normal, control_char, transform)(i) + +} diff --git a/third_party/rust/nom/src/character/complete.rs b/third_party/rust/nom/src/character/complete.rs new file mode 100644 index 0000000000..beddc3fa43 --- /dev/null +++ b/third_party/rust/nom/src/character/complete.rs @@ -0,0 +1,1082 @@ +//! Character specific parsers and combinators, complete input version. +//! +//! Functions recognizing specific characters. + +use crate::internal::{Err, IResult}; +use crate::error::ParseError; +use crate::lib::std::ops::{Range, RangeFrom, RangeTo}; +use crate::traits::{AsChar, FindToken, InputIter, InputLength, InputTakeAtPosition, Slice}; +use crate::traits::{Compare, CompareResult}; +use crate::error::ErrorKind; + +/// Recognizes one character. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind}; +/// # use nom::character::complete::char; +/// # fn main() { +/// assert_eq!(char::<_, (&str, ErrorKind)>('a')("abc"), Ok(("bc", 'a'))); +/// assert_eq!(char::<_, (&str, ErrorKind)>('a')("bc"), Err(Err::Error(("bc", ErrorKind::Char)))); +/// assert_eq!(char::<_, (&str, ErrorKind)>('a')(""), Err(Err::Error(("", ErrorKind::Char)))); +/// # } +/// ``` +pub fn char>(c: char) -> impl Fn(I) -> IResult +where + I: Slice> + InputIter, + ::Item: AsChar, +{ + move |i: I| match (i).iter_elements().next().map(|t| { + let b = t.as_char() == c; + (&c, b) + }) { + Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())), + _ => Err(Err::Error(Error::from_char(i, c))), + } +} + +/// Recognizes one of the provided characters. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind}; +/// # use nom::character::complete::one_of; +/// # fn main() { +/// assert_eq!(one_of::<_, _, (&str, ErrorKind)>("abc")("b"), Ok(("", 'b'))); +/// assert_eq!(one_of::<_, _, (&str, ErrorKind)>("a")("bc"), Err(Err::Error(("bc", ErrorKind::OneOf)))); +/// assert_eq!(one_of::<_, _, (&str, ErrorKind)>("a")(""), Err(Err::Error(("", ErrorKind::OneOf)))); +/// # } +/// ``` +pub fn one_of>(list: T) -> impl Fn(I) -> IResult +where + I: Slice> + InputIter, + ::Item: AsChar + Copy, + T: FindToken<::Item>, +{ + move |i: I| match (i).iter_elements().next().map(|c| (c, list.find_token(c))) { + Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())), + _ => Err(Err::Error(Error::from_error_kind(i, ErrorKind::OneOf))), + } +} + +/// Recognizes a character that is not in the provided characters. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind}; +/// # use nom::character::complete::none_of; +/// # fn main() { +/// assert_eq!(none_of::<_, _, (&str, ErrorKind)>("abc")("z"), Ok(("", 'z'))); +/// assert_eq!(none_of::<_, _, (&str, ErrorKind)>("ab")("a"), Err(Err::Error(("a", ErrorKind::NoneOf)))); +/// assert_eq!(none_of::<_, _, (&str, ErrorKind)>("a")(""), Err(Err::Error(("", ErrorKind::NoneOf)))); +/// # } +/// ``` +pub fn none_of>(list: T) -> impl Fn(I) -> IResult +where + I: Slice> + InputIter, + ::Item: AsChar + Copy, + T: FindToken<::Item>, +{ + move |i: I| match (i).iter_elements().next().map(|c| (c, !list.find_token(c))) { + Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())), + _ => Err(Err::Error(Error::from_error_kind(i, ErrorKind::NoneOf))), + } +} + +/// Recognizes the string "\r\n". +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult}; +/// # use nom::character::complete::crlf; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// crlf(input) +/// } +/// +/// assert_eq!(parser("\r\nc"), Ok(("c", "\r\n"))); +/// assert_eq!(parser("ab\r\nc"), Err(Err::Error(("ab\r\nc", ErrorKind::CrLf)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::CrLf)))); +/// # } +/// ``` +pub fn crlf>(input: T) -> IResult +where + T: Slice> + Slice> + Slice>, + T: InputIter, + T: Compare<&'static str>, +{ + match input.compare("\r\n") { + //FIXME: is this the right index? + CompareResult::Ok => Ok((input.slice(2..), input.slice(0..2))), + _ => { + let e: ErrorKind = ErrorKind::CrLf; + Err(Err::Error(E::from_error_kind(input, e))) + } + } +} + +//FIXME: there's still an incomplete +/// Recognizes a string of any char except '\r' or '\n'. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::not_line_ending; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// not_line_ending(input) +/// } +/// +/// assert_eq!(parser("ab\r\nc"), Ok(("\r\nc", "ab"))); +/// assert_eq!(parser("abc"), Ok(("", "abc"))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn not_line_ending>(input: T) -> IResult +where + T: Slice> + Slice> + Slice>, + T: InputIter + InputLength, + T: Compare<&'static str>, + ::Item: AsChar, + ::Item: AsChar, +{ + match input.position(|item| { + let c = item.as_char(); + c == '\r' || c == '\n' + }) { + None => { + Ok((input.slice(input.input_len()..), input)) + } + Some(index) => { + let mut it = input.slice(index..).iter_elements(); + let nth = it.next().unwrap().as_char(); + if nth == '\r' { + let sliced = input.slice(index..); + let comp = sliced.compare("\r\n"); + match comp { + //FIXME: calculate the right index + CompareResult::Ok => Ok((input.slice(index..), input.slice(..index))), + _ => { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(E::from_error_kind(input, e))) + } + } + } else { + Ok((input.slice(index..), input.slice(..index))) + } + } + } +} + +/// Recognizes an end of line (both '\n' and '\r\n'). +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::line_ending; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// line_ending(input) +/// } +/// +/// assert_eq!(parser("\r\nc"), Ok(("c", "\r\n"))); +/// assert_eq!(parser("ab\r\nc"), Err(Err::Error(("ab\r\nc", ErrorKind::CrLf)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::CrLf)))); +/// # } +/// ``` +pub fn line_ending>(input: T) -> IResult +where + T: Slice> + Slice> + Slice>, + T: InputIter + InputLength, + T: Compare<&'static str>, +{ + match input.compare("\n") { + CompareResult::Ok => Ok((input.slice(1..), input.slice(0..1))), + CompareResult::Incomplete => Err(Err::Error(E::from_error_kind(input, ErrorKind::CrLf))), + CompareResult::Error => { + match input.compare("\r\n") { + //FIXME: is this the right index? + CompareResult::Ok => Ok((input.slice(2..), input.slice(0..2))), + _ => Err(Err::Error(E::from_error_kind(input, ErrorKind::CrLf))), + } + } + } +} + +/// Matches a newline character '\n'. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::newline; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, char> { +/// newline(input) +/// } +/// +/// assert_eq!(parser("\nc"), Ok(("c", '\n'))); +/// assert_eq!(parser("\r\nc"), Err(Err::Error(("\r\nc", ErrorKind::Char)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Char)))); +/// # } +/// ``` +pub fn newline>(input: I) -> IResult +where + I: Slice> + InputIter, + ::Item: AsChar, +{ + char('\n')(input) +} + +/// Matches a tab character '\t'. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::tab; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, char> { +/// tab(input) +/// } +/// +/// assert_eq!(parser("\tc"), Ok(("c", '\t'))); +/// assert_eq!(parser("\r\nc"), Err(Err::Error(("\r\nc", ErrorKind::Char)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Char)))); +/// # } +/// ``` +pub fn tab>(input: I) -> IResult +where + I: Slice> + InputIter, + ::Item: AsChar, +{ + char('\t')(input) +} + +/// Matches one byte as a character. Note that the input type will +/// accept a `str`, but not a `&[u8]`, unlike many other nom parsers. +/// +/// *complete version*: Will return an error if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{character::complete::anychar, Err, error::ErrorKind, IResult}; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, char> { +/// anychar(input) +/// } +/// +/// assert_eq!(parser("abc"), Ok(("bc",'a'))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Eof)))); +/// # } +/// ``` +pub fn anychar>(input: T) -> IResult +where + T: InputIter + InputLength + Slice>, + ::Item: AsChar, +{ + let mut it = input.iter_indices(); + match it.next() { + None => Err(Err::Error(E::from_error_kind(input, ErrorKind::Eof))), + Some((_, c)) => match it.next() { + None => Ok((input.slice(input.input_len()..), c.as_char())), + Some((idx, _)) => Ok((input.slice(idx..), c.as_char())), + }, + } +} + +/// Recognizes zero or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z +/// +/// *complete version*: Will return the whole input if no terminating token is found (a non +/// alphabetic character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::alpha0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// alpha0(input) +/// } +/// +/// assert_eq!(parser("ab1c"), Ok(("1c", "ab"))); +/// assert_eq!(parser("1c"), Ok(("1c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn alpha0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position_complete(|item| !item.is_alpha()) +} + +/// Recognizes one or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non alphabetic character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::alpha1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// alpha1(input) +/// } +/// +/// assert_eq!(parser("aB1c"), Ok(("1c", "aB"))); +/// assert_eq!(parser("1c"), Err(Err::Error(("1c", ErrorKind::Alpha)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Alpha)))); +/// # } +/// ``` +pub fn alpha1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position1_complete(|item| !item.is_alpha(), ErrorKind::Alpha) +} + +/// Recognizes zero or more ASCII numerical characters: 0-9 +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::digit0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// digit0(input) +/// } +/// +/// assert_eq!(parser("21c"), Ok(("c", "21"))); +/// assert_eq!(parser("21"), Ok(("", "21"))); +/// assert_eq!(parser("a21c"), Ok(("a21c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn digit0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position_complete(|item| !item.is_dec_digit()) +} + +/// Recognizes one or more ASCII numerical characters: 0-9 +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::digit1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// digit1(input) +/// } +/// +/// assert_eq!(parser("21c"), Ok(("c", "21"))); +/// assert_eq!(parser("c1"), Err(Err::Error(("c1", ErrorKind::Digit)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Digit)))); +/// # } +/// ``` +pub fn digit1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position1_complete(|item| !item.is_dec_digit(), ErrorKind::Digit) +} + +/// Recognizes zero or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f +/// +/// *complete version*: Will return the whole input if no terminating token is found (a non hexadecimal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::hex_digit0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// hex_digit0(input) +/// } +/// +/// assert_eq!(parser("21cZ"), Ok(("Z", "21c"))); +/// assert_eq!(parser("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn hex_digit0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position_complete(|item| !item.is_hex_digit()) +} +/// Recognizes one or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non hexadecimal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::hex_digit1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// hex_digit1(input) +/// } +/// +/// assert_eq!(parser("21cZ"), Ok(("Z", "21c"))); +/// assert_eq!(parser("H2"), Err(Err::Error(("H2", ErrorKind::HexDigit)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::HexDigit)))); +/// # } +/// ``` +pub fn hex_digit1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position1_complete(|item| !item.is_hex_digit(), ErrorKind::HexDigit) +} + +/// Recognizes zero or more octal characters: 0-7 +/// +/// *complete version*: Will return the whole input if no terminating token is found (a non octal +/// digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::oct_digit0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// oct_digit0(input) +/// } +/// +/// assert_eq!(parser("21cZ"), Ok(("cZ", "21"))); +/// assert_eq!(parser("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn oct_digit0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position_complete(|item| !item.is_oct_digit()) +} + +/// Recognizes one or more octal characters: 0-7 +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non octal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::oct_digit1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// oct_digit1(input) +/// } +/// +/// assert_eq!(parser("21cZ"), Ok(("cZ", "21"))); +/// assert_eq!(parser("H2"), Err(Err::Error(("H2", ErrorKind::OctDigit)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::OctDigit)))); +/// # } +/// ``` +pub fn oct_digit1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position1_complete(|item| !item.is_oct_digit(), ErrorKind::OctDigit) +} + +/// Recognizes zero or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z +/// +/// *complete version*: Will return the whole input if no terminating token is found (a non +/// alphanumerical character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::alphanumeric0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// alphanumeric0(input) +/// } +/// +/// assert_eq!(parser("21cZ%1"), Ok(("%1", "21cZ"))); +/// assert_eq!(parser("&Z21c"), Ok(("&Z21c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn alphanumeric0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position_complete(|item| !item.is_alphanum()) +} + +/// Recognizes one or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non alphanumerical character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::alphanumeric1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// alphanumeric1(input) +/// } +/// +/// assert_eq!(parser("21cZ%1"), Ok(("%1", "21cZ"))); +/// assert_eq!(parser("&H2"), Err(Err::Error(("&H2", ErrorKind::AlphaNumeric)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::AlphaNumeric)))); +/// # } +/// ``` +pub fn alphanumeric1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position1_complete(|item| !item.is_alphanum(), ErrorKind::AlphaNumeric) +} + +/// Recognizes zero or more spaces and tabs. +/// +/// *complete version*: Will return the whole input if no terminating token is found (a non space +/// character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::space0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// space0(input) +/// } +/// +/// assert_eq!(parser(" \t21c"), Ok(("21c", " \t"))); +/// assert_eq!(parser("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn space0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar + Clone, +{ + input.split_at_position_complete(|item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t') + }) +} + +/// Recognizes one or more spaces and tabs. +/// +/// *complete version*: Will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non space character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::space1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// space1(input) +/// } +/// +/// assert_eq!(parser(" \t21c"), Ok(("21c", " \t"))); +/// assert_eq!(parser("H2"), Err(Err::Error(("H2", ErrorKind::Space)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Space)))); +/// # } +/// ``` +pub fn space1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar + Clone, +{ + input.split_at_position1_complete( + |item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t') + }, + ErrorKind::Space, + ) +} + +/// Recognizes zero or more spaces, tabs, carriage returns and line feeds. +/// +/// *complete version*: will return the whole input if no terminating token is found (a non space +/// character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::multispace0; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// multispace0(input) +/// } +/// +/// assert_eq!(parser(" \t\n\r21c"), Ok(("21c", " \t\n\r"))); +/// assert_eq!(parser("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(parser(""), Ok(("", ""))); +/// # } +/// ``` +pub fn multispace0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar + Clone, +{ + input.split_at_position_complete(|item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t' || c == '\r' || c == '\n') + }) +} + +/// Recognizes one or more spaces, tabs, carriage returns and line feeds. +/// +/// *complete version*: will return an error if there's not enough input data, +/// or the whole input if no terminating token is found (a non space character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::complete::multispace1; +/// # fn main() { +/// fn parser(input: &str) -> IResult<&str, &str> { +/// multispace1(input) +/// } +/// +/// assert_eq!(parser(" \t\n\r21c"), Ok(("21c", " \t\n\r"))); +/// assert_eq!(parser("H2"), Err(Err::Error(("H2", ErrorKind::MultiSpace)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::MultiSpace)))); +/// # } +/// ``` +pub fn multispace1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar + Clone, +{ + input.split_at_position1_complete( + |item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t' || c == '\r' || c == '\n') + }, + ErrorKind::MultiSpace, + ) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::internal::Err; + + macro_rules! assert_parse( + ($left: expr, $right: expr) => { + let res: $crate::IResult<_, _, (_, ErrorKind)> = $left; + assert_eq!(res, $right); + }; + ); + + + #[test] + fn character() { + let empty: &[u8] = b""; + let a: &[u8] = b"abcd"; + let b: &[u8] = b"1234"; + let c: &[u8] = b"a123"; + let d: &[u8] = "azé12".as_bytes(); + let e: &[u8] = b" "; + let f: &[u8] = b" ;"; + //assert_eq!(alpha1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + assert_parse!(alpha1(a), Ok((empty, a))); + assert_eq!( + alpha1(b), + Err(Err::Error((b, ErrorKind::Alpha))) + ); + assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &b"a"[..]))); + assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12".as_bytes(), &b"az"[..]))); + assert_eq!( + digit1(a), + Err(Err::Error((a, ErrorKind::Digit))) + ); + assert_eq!(digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); + assert_eq!( + digit1(c), + Err(Err::Error((c, ErrorKind::Digit))) + ); + assert_eq!( + digit1(d), + Err(Err::Error((d, ErrorKind::Digit))) + ); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(a), Ok((empty, a))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(c), Ok((empty, c))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12".as_bytes(), &b"a"[..]))); + assert_eq!( + hex_digit1(e), + Err(Err::Error((e, ErrorKind::HexDigit))) + ); + assert_eq!( + oct_digit1(a), + Err(Err::Error((a, ErrorKind::OctDigit))) + ); + assert_eq!(oct_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); + assert_eq!( + oct_digit1(c), + Err(Err::Error((c, ErrorKind::OctDigit))) + ); + assert_eq!( + oct_digit1(d), + Err(Err::Error((d, ErrorKind::OctDigit))) + ); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(a), Ok((empty, a))); + //assert_eq!(fix_error!(b,(), alphanumeric), Ok((empty, b))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(c), Ok((empty, c))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(d), Ok(("é12".as_bytes(), &b"az"[..]))); + assert_eq!(space1::<_, (_, ErrorKind)>(e), Ok((empty, e))); + assert_eq!(space1::<_, (_, ErrorKind)>(f), Ok((&b";"[..], &b" "[..]))); + } + + #[cfg(feature = "alloc")] + #[test] + fn character_s() { + let empty = ""; + let a = "abcd"; + let b = "1234"; + let c = "a123"; + let d = "azé12"; + let e = " "; + assert_eq!(alpha1::<_, (_, ErrorKind)>(a), Ok((empty, a))); + assert_eq!( + alpha1(b), + Err(Err::Error((b, ErrorKind::Alpha))) + ); + assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &"a"[..]))); + assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", &"az"[..]))); + assert_eq!( + digit1(a), + Err(Err::Error((a, ErrorKind::Digit))) + ); + assert_eq!(digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); + assert_eq!( + digit1(c), + Err(Err::Error((c, ErrorKind::Digit))) + ); + assert_eq!( + digit1(d), + Err(Err::Error((d, ErrorKind::Digit))) + ); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(a), Ok((empty, a))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(c), Ok((empty, c))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", &"a"[..]))); + assert_eq!( + hex_digit1(e), + Err(Err::Error((e, ErrorKind::HexDigit))) + ); + assert_eq!( + oct_digit1(a), + Err(Err::Error((a, ErrorKind::OctDigit))) + ); + assert_eq!(oct_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b))); + assert_eq!( + oct_digit1(c), + Err(Err::Error((c, ErrorKind::OctDigit))) + ); + assert_eq!( + oct_digit1(d), + Err(Err::Error((d, ErrorKind::OctDigit))) + ); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(a), Ok((empty, a))); + //assert_eq!(fix_error!(b,(), alphanumeric), Ok((empty, b))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(c), Ok((empty, c))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(d), Ok(("é12", "az"))); + assert_eq!(space1::<_, (_, ErrorKind)>(e), Ok((empty, e))); + } + + use crate::traits::Offset; + #[test] + fn offset() { + let a = &b"abcd;"[..]; + let b = &b"1234;"[..]; + let c = &b"a123;"[..]; + let d = &b" \t;"[..]; + let e = &b" \t\r\n;"[..]; + let f = &b"123abcDEF;"[..]; + + match alpha1::<_, (_, ErrorKind)>(a) { + Ok((i, _)) => { + assert_eq!(a.offset(i) + i.len(), a.len()); + } + _ => panic!("wrong return type in offset test for alpha"), + } + match digit1::<_, (_, ErrorKind)>(b) { + Ok((i, _)) => { + assert_eq!(b.offset(i) + i.len(), b.len()); + } + _ => panic!("wrong return type in offset test for digit"), + } + match alphanumeric1::<_, (_, ErrorKind)>(c) { + Ok((i, _)) => { + assert_eq!(c.offset(i) + i.len(), c.len()); + } + _ => panic!("wrong return type in offset test for alphanumeric"), + } + match space1::<_, (_, ErrorKind)>(d) { + Ok((i, _)) => { + assert_eq!(d.offset(i) + i.len(), d.len()); + } + _ => panic!("wrong return type in offset test for space"), + } + match multispace1::<_, (_, ErrorKind)>(e) { + Ok((i, _)) => { + assert_eq!(e.offset(i) + i.len(), e.len()); + } + _ => panic!("wrong return type in offset test for multispace"), + } + match hex_digit1::<_, (_, ErrorKind)>(f) { + Ok((i, _)) => { + assert_eq!(f.offset(i) + i.len(), f.len()); + } + _ => panic!("wrong return type in offset test for hex_digit"), + } + match oct_digit1::<_, (_, ErrorKind)>(f) { + Ok((i, _)) => { + assert_eq!(f.offset(i) + i.len(), f.len()); + } + _ => panic!("wrong return type in offset test for oct_digit"), + } + } + + #[test] + fn is_not_line_ending_bytes() { + let a: &[u8] = b"ab12cd\nefgh"; + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(a), Ok((&b"\nefgh"[..], &b"ab12cd"[..]))); + + let b: &[u8] = b"ab12cd\nefgh\nijkl"; + assert_eq!( + not_line_ending::<_, (_, ErrorKind)>(b), + Ok((&b"\nefgh\nijkl"[..], &b"ab12cd"[..])) + ); + + let c: &[u8] = b"ab12cd\r\nefgh\nijkl"; + assert_eq!( + not_line_ending::<_, (_, ErrorKind)>(c), + Ok((&b"\r\nefgh\nijkl"[..], &b"ab12cd"[..])) + ); + + let d: &[u8] = b"ab12cd"; + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(d), Ok((&[][..], &d[..]))); + } + + #[test] + fn is_not_line_ending_str() { + + /* + let a: &str = "ab12cd\nefgh"; + assert_eq!(not_line_ending(a), Ok((&"\nefgh"[..], &"ab12cd"[..]))); + + let b: &str = "ab12cd\nefgh\nijkl"; + assert_eq!(not_line_ending(b), Ok((&"\nefgh\nijkl"[..], &"ab12cd"[..]))); + + let c: &str = "ab12cd\r\nefgh\nijkl"; + assert_eq!(not_line_ending(c), Ok((&"\r\nefgh\nijkl"[..], &"ab12cd"[..]))); + + let d = "βèƒôřè\nÂßÇáƒƭèř"; + assert_eq!(not_line_ending(d), Ok((&"\nÂßÇáƒƭèř"[..], &"βèƒôřè"[..]))); + + let e = "βèƒôřè\r\nÂßÇáƒƭèř"; + assert_eq!(not_line_ending(e), Ok((&"\r\nÂßÇáƒƭèř"[..], &"βèƒôřè"[..]))); + */ + + let f = "βèƒôřè\rÂßÇáƒƭèř"; + assert_eq!( + not_line_ending(f), + Err(Err::Error((f, ErrorKind::Tag))) + ); + + let g2: &str = "ab12cd"; + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(g2), Ok(("", g2))); + } + + #[test] + fn hex_digit_test() { + let i = &b"0123456789abcdefABCDEF;"[..]; + assert_parse!(hex_digit1(i), Ok((&b";"[..], &i[..i.len() - 1]))); + + let i = &b"g"[..]; + assert_parse!( + hex_digit1(i), + Err(Err::Error(error_position!(i, ErrorKind::HexDigit))) + ); + + let i = &b"G"[..]; + assert_parse!( + hex_digit1(i), + Err(Err::Error(error_position!(i, ErrorKind::HexDigit))) + ); + + assert!(crate::character::is_hex_digit(b'0')); + assert!(crate::character::is_hex_digit(b'9')); + assert!(crate::character::is_hex_digit(b'a')); + assert!(crate::character::is_hex_digit(b'f')); + assert!(crate::character::is_hex_digit(b'A')); + assert!(crate::character::is_hex_digit(b'F')); + assert!(!crate::character::is_hex_digit(b'g')); + assert!(!crate::character::is_hex_digit(b'G')); + assert!(!crate::character::is_hex_digit(b'/')); + assert!(!crate::character::is_hex_digit(b':')); + assert!(!crate::character::is_hex_digit(b'@')); + assert!(!crate::character::is_hex_digit(b'\x60')); + } + + #[test] + fn oct_digit_test() { + let i = &b"01234567;"[..]; + assert_parse!(oct_digit1(i), Ok((&b";"[..], &i[..i.len() - 1]))); + + let i = &b"8"[..]; + assert_parse!( + oct_digit1(i), + Err(Err::Error(error_position!(i, ErrorKind::OctDigit))) + ); + + assert!(crate::character::is_oct_digit(b'0')); + assert!(crate::character::is_oct_digit(b'7')); + assert!(!crate::character::is_oct_digit(b'8')); + assert!(!crate::character::is_oct_digit(b'9')); + assert!(!crate::character::is_oct_digit(b'a')); + assert!(!crate::character::is_oct_digit(b'A')); + assert!(!crate::character::is_oct_digit(b'/')); + assert!(!crate::character::is_oct_digit(b':')); + assert!(!crate::character::is_oct_digit(b'@')); + assert!(!crate::character::is_oct_digit(b'\x60')); + } + + #[test] + fn full_line_windows() { + //let not_line_ending = |i:&[u8]| take_while(|c| c != b'\r' && c != b'\n')(i); + + named!( + take_full_line<(&[u8], &[u8])>, + tuple!(not_line_ending, line_ending) + ); + let input = b"abc\r\n"; + let output = take_full_line(input); + assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\r\n"[..])))); + } + + #[test] + fn full_line_unix() { + //let not_line_ending = |i:&[u8]| take_while(|c| c != b'\n')(i); + named!( + take_full_line<(&[u8], &[u8])>, + tuple!(not_line_ending, line_ending) + ); + let input = b"abc\n"; + let output = take_full_line(input); + assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\n"[..])))); + } + + #[test] + fn check_windows_lineending() { + let input = b"\r\n"; + let output = line_ending(&input[..]); + assert_parse!(output, Ok((&b""[..], &b"\r\n"[..]))); + } + + #[test] + fn check_unix_lineending() { + let input = b"\n"; + let output = line_ending(&input[..]); + assert_parse!(output, Ok((&b""[..], &b"\n"[..]))); + } + + #[test] + fn cr_lf() { + assert_parse!(crlf(&b"\r\na"[..]), Ok((&b"a"[..], &b"\r\n"[..]))); + assert_parse!(crlf(&b"\r"[..]), Err(Err::Error(error_position!(&b"\r"[..], ErrorKind::CrLf)))); + assert_parse!( + crlf(&b"\ra"[..]), + Err(Err::Error(error_position!(&b"\ra"[..], ErrorKind::CrLf))) + ); + + assert_parse!(crlf("\r\na"), Ok(("a", "\r\n"))); + assert_parse!(crlf("\r"), Err(Err::Error(error_position!(&"\r"[..], ErrorKind::CrLf)))); + assert_parse!( + crlf("\ra"), + Err(Err::Error(error_position!("\ra", ErrorKind::CrLf))) + ); + } + + #[test] + fn end_of_line() { + assert_parse!(line_ending(&b"\na"[..]), Ok((&b"a"[..], &b"\n"[..]))); + assert_parse!(line_ending(&b"\r\na"[..]), Ok((&b"a"[..], &b"\r\n"[..]))); + assert_parse!(line_ending(&b"\r"[..]), Err(Err::Error(error_position!(&b"\r"[..], ErrorKind::CrLf)))); + assert_parse!( + line_ending(&b"\ra"[..]), + Err(Err::Error(error_position!(&b"\ra"[..], ErrorKind::CrLf))) + ); + + assert_parse!(line_ending("\na"), Ok(("a", "\n"))); + assert_parse!(line_ending("\r\na"), Ok(("a", "\r\n"))); + assert_parse!(line_ending("\r"), Err(Err::Error(error_position!(&"\r"[..], ErrorKind::CrLf)))); + assert_parse!( + line_ending("\ra"), + Err(Err::Error(error_position!("\ra", ErrorKind::CrLf))) + ); + } +} diff --git a/third_party/rust/nom/src/character/macros.rs b/third_party/rust/nom/src/character/macros.rs new file mode 100644 index 0000000000..097ee409fe --- /dev/null +++ b/third_party/rust/nom/src/character/macros.rs @@ -0,0 +1,112 @@ +/// Character level parsers + +/// matches one of the provided characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(simple, one_of!(&b"abc"[..])); +/// assert_eq!(simple(b"a123"), Ok((&b"123"[..], 'a'))); +/// +/// named!(a_or_b<&str, char>, one_of!("ab汉")); +/// assert_eq!(a_or_b("汉jiosfe"), Ok(("jiosfe", '汉'))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! one_of ( + ($i:expr, $inp: expr) => ( $crate::character::streaming::one_of($inp)($i) ); +); + +/// matches anything but the provided characters +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind}; +/// # fn main() { +/// named!(no_letter_a, none_of!(&b"abc"[..])); +/// assert_eq!(no_letter_a(b"123"), Ok((&b"23"[..], '1'))); +/// +/// named!(err_on_single_quote, none_of!(&b"'"[..])); +/// assert_eq!(err_on_single_quote(b"'jiosfe"), Err(Err::Error(error_position!(&b"'jiosfe"[..], ErrorKind::NoneOf)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! none_of ( + ($i:expr, $inp: expr) => ( $crate::character::streaming::none_of($inp)($i) ); +); + +/// matches one character: `char!(char) => &[u8] -> IResult<&[u8], char> +/// +/// # Example +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind}; +/// # fn main() { +/// named!(match_letter_a, char!('a')); +/// assert_eq!(match_letter_a(b"abc"), Ok((&b"bc"[..],'a'))); +/// +/// assert_eq!(match_letter_a(b"123cdef"), Err(Err::Error(error_position!(&b"123cdef"[..], ErrorKind::Char)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! char ( + ($i:expr, $c: expr) => ( $crate::character::streaming::char($c)($i) ); +); + +#[cfg(test)] +mod tests { + use crate::internal::Err; + use crate::error::ErrorKind; + + #[test] + fn one_of() { + named!(f, one_of!("ab")); + + let a = &b"abcd"[..]; + assert_eq!(f(a), Ok((&b"bcd"[..], 'a'))); + + let b = &b"cde"[..]; + assert_eq!(f(b), Err(Err::Error(error_position!(b, ErrorKind::OneOf)))); + + named!(utf8(&str) -> char, + one_of!("+\u{FF0B}")); + + assert!(utf8("+").is_ok()); + assert!(utf8("\u{FF0B}").is_ok()); + } + + #[test] + fn none_of() { + named!(f, none_of!("ab")); + + let a = &b"abcd"[..]; + assert_eq!(f(a), Err(Err::Error(error_position!(a, ErrorKind::NoneOf)))); + + let b = &b"cde"[..]; + assert_eq!(f(b), Ok((&b"de"[..], 'c'))); + } + + #[test] + fn char() { + named!(f, char!('c')); + + let a = &b"abcd"[..]; + assert_eq!(f(a), Err(Err::Error(error_position!(a, ErrorKind::Char)))); + + let b = &b"cde"[..]; + assert_eq!(f(b), Ok((&b"de"[..], 'c'))); + } + + #[test] + fn char_str() { + named!(f<&str, char>, char!('c')); + + let a = &"abcd"[..]; + assert_eq!(f(a), Err(Err::Error(error_position!(a, ErrorKind::Char)))); + + let b = &"cde"[..]; + assert_eq!(f(b), Ok((&"de"[..], 'c'))); + } +} diff --git a/third_party/rust/nom/src/character/mod.rs b/third_party/rust/nom/src/character/mod.rs new file mode 100644 index 0000000000..3f0899aa8c --- /dev/null +++ b/third_party/rust/nom/src/character/mod.rs @@ -0,0 +1,101 @@ +//! character specific parsers and combinators +//! +//! functions recognizing specific characters + +#[macro_use] +mod macros; + +pub mod streaming; +pub mod complete; + +/// Tests if byte is ASCII alphabetic: A-Z, a-z +/// +/// # Example +/// +/// ``` +/// # use nom::character::is_alphabetic; +/// assert_eq!(is_alphabetic(b'9'), false); +/// assert_eq!(is_alphabetic(b'a'), true); +/// ``` +#[inline] +pub fn is_alphabetic(chr: u8) -> bool { + (chr >= 0x41 && chr <= 0x5A) || (chr >= 0x61 && chr <= 0x7A) +} + +/// Tests if byte is ASCII digit: 0-9 +/// +/// # Example +/// +/// ``` +/// # use nom::character::is_digit; +/// assert_eq!(is_digit(b'a'), false); +/// assert_eq!(is_digit(b'9'), true); +/// ``` +#[inline] +pub fn is_digit(chr: u8) -> bool { + chr >= 0x30 && chr <= 0x39 +} + +/// Tests if byte is ASCII hex digit: 0-9, A-F, a-f +/// +/// # Example +/// +/// ``` +/// # use nom::character::is_hex_digit; +/// assert_eq!(is_hex_digit(b'a'), true); +/// assert_eq!(is_hex_digit(b'9'), true); +/// assert_eq!(is_hex_digit(b'A'), true); +/// assert_eq!(is_hex_digit(b'x'), false); +/// ``` +#[inline] +pub fn is_hex_digit(chr: u8) -> bool { + (chr >= 0x30 && chr <= 0x39) || (chr >= 0x41 && chr <= 0x46) || (chr >= 0x61 && chr <= 0x66) +} + +/// Tests if byte is ASCII octal digit: 0-7 +/// +/// # Example +/// +/// ``` +/// # use nom::character::is_oct_digit; +/// assert_eq!(is_oct_digit(b'a'), false); +/// assert_eq!(is_oct_digit(b'9'), false); +/// assert_eq!(is_oct_digit(b'6'), true); +/// ``` +#[inline] +pub fn is_oct_digit(chr: u8) -> bool { + chr >= 0x30 && chr <= 0x37 +} + +/// Tests if byte is ASCII alphanumeric: A-Z, a-z, 0-9 +/// +/// # Example +/// +/// ``` +/// # use nom::character::is_alphanumeric; +/// assert_eq!(is_alphanumeric(b'-'), false); +/// assert_eq!(is_alphanumeric(b'a'), true); +/// assert_eq!(is_alphanumeric(b'9'), true); +/// assert_eq!(is_alphanumeric(b'A'), true); +/// ``` +#[inline] +pub fn is_alphanumeric(chr: u8) -> bool { + is_alphabetic(chr) || is_digit(chr) +} + +/// Tests if byte is ASCII space or tab +/// +/// # Example +/// +/// ``` +/// # use nom::character::is_space; +/// assert_eq!(is_space(b'\n'), false); +/// assert_eq!(is_space(b'\r'), false); +/// assert_eq!(is_space(b' '), true); +/// assert_eq!(is_space(b'\t'), true); +/// ``` +#[inline] +pub fn is_space(chr: u8) -> bool { + chr == b' ' || chr == b'\t' +} + diff --git a/third_party/rust/nom/src/character/streaming.rs b/third_party/rust/nom/src/character/streaming.rs new file mode 100644 index 0000000000..f62daceeb4 --- /dev/null +++ b/third_party/rust/nom/src/character/streaming.rs @@ -0,0 +1,1010 @@ +//! character specific parsers and combinators, streaming version +//! +//! functions recognizing specific characters + +use crate::internal::{Err, IResult, Needed}; +use crate::error::ParseError; +use crate::lib::std::ops::{Range, RangeFrom, RangeTo}; +use crate::traits::{AsChar, FindToken, InputIter, InputLength, InputTakeAtPosition, Slice}; +use crate::traits::{Compare, CompareResult}; + +use crate::error::ErrorKind; + +/// Recognizes one character. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::character::streaming::char; +/// # fn main() { +/// assert_eq!(char::<_, (_, ErrorKind)>('a')(&b"abc"[..]), Ok((&b"bc"[..], 'a'))); +/// assert_eq!(char::<_, (_, ErrorKind)>('a')(&b"bc"[..]), Err(Err::Error((&b"bc"[..], ErrorKind::Char)))); +/// assert_eq!(char::<_, (_, ErrorKind)>('a')(&b""[..]), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn char>(c: char) -> impl Fn(I) -> IResult +where + I: Slice> + InputIter, + ::Item: AsChar, +{ + move |i: I| match (i).iter_elements().next().map(|t| { + let b = t.as_char() == c; + (&c, b) + }) { + None => Err(Err::Incomplete(Needed::Size(1))), + Some((_, false)) => { + Err(Err::Error(Error::from_char(i, c))) + } + Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())), + } +} + +/// Recognizes one of the provided characters. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::character::streaming::one_of; +/// # fn main() { +/// assert_eq!(one_of::<_, _, (_, ErrorKind)>("abc")("b"), Ok(("", 'b'))); +/// assert_eq!(one_of::<_, _, (_, ErrorKind)>("a")("bc"), Err(Err::Error(("bc", ErrorKind::OneOf)))); +/// assert_eq!(one_of::<_, _, (_, ErrorKind)>("a")(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn one_of>(list: T) -> impl Fn(I) -> IResult +where + I: Slice> + InputIter, + ::Item: AsChar + Copy, + T: FindToken<::Item>, +{ + move |i: I| match (i).iter_elements().next().map(|c| (c, list.find_token(c))) { + None => Err(Err::Incomplete(Needed::Size(1))), + Some((_, false)) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::OneOf))), + Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())), + } +} + +/// Recognizes a character that is not in the provided characters. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::character::streaming::none_of; +/// # fn main() { +/// assert_eq!(none_of::<_, _, (_, ErrorKind)>("abc")("z"), Ok(("", 'z'))); +/// assert_eq!(none_of::<_, _, (_, ErrorKind)>("ab")("a"), Err(Err::Error(("a", ErrorKind::NoneOf)))); +/// assert_eq!(none_of::<_, _, (_, ErrorKind)>("a")(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn none_of>(list: T) -> impl Fn(I) -> IResult +where + I: Slice> + InputIter, + ::Item: AsChar + Copy, + T: FindToken<::Item>, +{ + move |i: I| match (i).iter_elements().next().map(|c| (c, !list.find_token(c))) { + None => Err(Err::Incomplete(Needed::Size(1))), + Some((_, false)) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::NoneOf))), + Some((c, true)) => Ok((i.slice(c.len()..), c.as_char())), + } +} + +/// Recognizes the string "\r\n". +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::crlf; +/// # fn main() { +/// assert_eq!(crlf::<_, (_, ErrorKind)>("\r\nc"), Ok(("c", "\r\n"))); +/// assert_eq!(crlf::<_, (_, ErrorKind)>("ab\r\nc"), Err(Err::Error(("ab\r\nc", ErrorKind::CrLf)))); +/// assert_eq!(crlf::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(2)))); +/// # } +/// ``` +pub fn crlf>(input: T) -> IResult +where + T: Slice> + Slice> + Slice>, + T: InputIter, + T: Compare<&'static str>, +{ + match input.compare("\r\n") { + //FIXME: is this the right index? + CompareResult::Ok => Ok((input.slice(2..), input.slice(0..2))), + CompareResult::Incomplete => Err(Err::Incomplete(Needed::Size(2))), + CompareResult::Error => { + let e: ErrorKind = ErrorKind::CrLf; + Err(Err::Error(E::from_error_kind(input, e))) + } + } +} + +/// Recognizes a string of any char except '\r' or '\n'. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::not_line_ending; +/// # fn main() { +/// assert_eq!(not_line_ending::<_, (_, ErrorKind)>("ab\r\nc"), Ok(("\r\nc", "ab"))); +/// assert_eq!(not_line_ending::<_, (_, ErrorKind)>("abc"), Err(Err::Incomplete(Needed::Unknown))); +/// assert_eq!(not_line_ending::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Unknown))); +/// # } +/// ``` +pub fn not_line_ending>(input: T) -> IResult +where + T: Slice> + Slice> + Slice>, + T: InputIter + InputLength, + T: Compare<&'static str>, + ::Item: AsChar, + ::Item: AsChar, +{ + match input.position(|item| { + let c = item.as_char(); + c == '\r' || c == '\n' + }) { + None => { + Err(Err::Incomplete(Needed::Unknown)) + } + Some(index) => { + let mut it = input.slice(index..).iter_elements(); + let nth = it.next().unwrap().as_char(); + if nth == '\r' { + let sliced = input.slice(index..); + let comp = sliced.compare("\r\n"); + match comp { + //FIXME: calculate the right index + CompareResult::Incomplete => Err(Err::Incomplete(Needed::Unknown)), + CompareResult::Error => { + let e: ErrorKind = ErrorKind::Tag; + Err(Err::Error(E::from_error_kind(input, e))) + } + CompareResult::Ok => Ok((input.slice(index..), input.slice(..index))), + } + } else { + Ok((input.slice(index..), input.slice(..index))) + } + } + } +} + +/// Recognizes an end of line (both '\n' and '\r\n'). +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::line_ending; +/// # fn main() { +/// assert_eq!(line_ending::<_, (_, ErrorKind)>("\r\nc"), Ok(("c", "\r\n"))); +/// assert_eq!(line_ending::<_, (_, ErrorKind)>("ab\r\nc"), Err(Err::Error(("ab\r\nc", ErrorKind::CrLf)))); +/// assert_eq!(line_ending::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn line_ending>(input: T) -> IResult +where + T: Slice> + Slice> + Slice>, + T: InputIter + InputLength, + T: Compare<&'static str>, +{ + match input.compare("\n") { + CompareResult::Ok => Ok((input.slice(1..), input.slice(0..1))), + CompareResult::Incomplete => Err(Err::Incomplete(Needed::Size(1))), + CompareResult::Error => { + match input.compare("\r\n") { + //FIXME: is this the right index? + CompareResult::Ok => Ok((input.slice(2..), input.slice(0..2))), + CompareResult::Incomplete => Err(Err::Incomplete(Needed::Size(2))), + CompareResult::Error => Err(Err::Error(E::from_error_kind(input, ErrorKind::CrLf))), + } + } + } +} + +/// Matches a newline character '\\n'. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::newline; +/// # fn main() { +/// assert_eq!(newline::<_, (_, ErrorKind)>("\nc"), Ok(("c", '\n'))); +/// assert_eq!(newline::<_, (_, ErrorKind)>("\r\nc"), Err(Err::Error(("\r\nc", ErrorKind::Char)))); +/// assert_eq!(newline::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn newline>(input: I) -> IResult +where + I: Slice> + InputIter, + ::Item: AsChar, +{ + char('\n')(input) +} + +/// Matches a tab character '\t'. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::tab; +/// # fn main() { +/// assert_eq!(tab::<_, (_, ErrorKind)>("\tc"), Ok(("c", '\t'))); +/// assert_eq!(tab::<_, (_, ErrorKind)>("\r\nc"), Err(Err::Error(("\r\nc", ErrorKind::Char)))); +/// assert_eq!(tab::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn tab>(input: I) -> IResult +where + I: Slice> + InputIter, + ::Item: AsChar, +{ + char('\t')(input) +} + +/// Matches one byte as a character. Note that the input type will +/// accept a `str`, but not a `&[u8]`, unlike many other nom parsers. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data. +/// +/// # Example +/// +/// ``` +/// # use nom::{character::streaming::anychar, Err, error::ErrorKind, IResult, Needed}; +/// # fn main() { +/// assert_eq!(anychar::<_, (_, ErrorKind)>("abc"), Ok(("bc",'a'))); +/// assert_eq!(anychar::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn anychar>(input: T) -> IResult +where + T: InputIter + InputLength + Slice>, + ::Item: AsChar, +{ + let mut it = input.iter_indices(); + match it.next() { + None => Err(Err::Incomplete(Needed::Size(1))), + Some((_, c)) => match it.next() { + None => Ok((input.slice(input.input_len()..), c.as_char())), + Some((idx, _)) => Ok((input.slice(idx..), c.as_char())), + }, + } +} + +/// Recognizes zero or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non alphabetic character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::alpha0; +/// # fn main() { +/// assert_eq!(alpha0::<_, (_, ErrorKind)>("ab1c"), Ok(("1c", "ab"))); +/// assert_eq!(alpha0::<_, (_, ErrorKind)>("1c"), Ok(("1c", ""))); +/// assert_eq!(alpha0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn alpha0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position(|item| !item.is_alpha()) +} + +/// Recognizes one or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non alphabetic character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::alpha1; +/// # fn main() { +/// assert_eq!(alpha1::<_, (_, ErrorKind)>("aB1c"), Ok(("1c", "aB"))); +/// assert_eq!(alpha1::<_, (_, ErrorKind)>("1c"), Err(Err::Error(("1c", ErrorKind::Alpha)))); +/// assert_eq!(alpha1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn alpha1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position1(|item| !item.is_alpha(), ErrorKind::Alpha) +} + +/// Recognizes zero or more ASCII numerical characters: 0-9 +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::digit0; +/// # fn main() { +/// assert_eq!(digit0::<_, (_, ErrorKind)>("21c"), Ok(("c", "21"))); +/// assert_eq!(digit0::<_, (_, ErrorKind)>("a21c"), Ok(("a21c", ""))); +/// assert_eq!(digit0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn digit0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position(|item| !item.is_dec_digit()) +} + +/// Recognizes one or more ASCII numerical characters: 0-9 +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::digit1; +/// # fn main() { +/// assert_eq!(digit1::<_, (_, ErrorKind)>("21c"), Ok(("c", "21"))); +/// assert_eq!(digit1::<_, (_, ErrorKind)>("c1"), Err(Err::Error(("c1", ErrorKind::Digit)))); +/// assert_eq!(digit1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn digit1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position1(|item| !item.is_dec_digit(), ErrorKind::Digit) +} + +/// Recognizes zero or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non hexadecimal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::hex_digit0; +/// # fn main() { +/// assert_eq!(hex_digit0::<_, (_, ErrorKind)>("21cZ"), Ok(("Z", "21c"))); +/// assert_eq!(hex_digit0::<_, (_, ErrorKind)>("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(hex_digit0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn hex_digit0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position(|item| !item.is_hex_digit()) +} + +/// Recognizes one or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non hexadecimal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::hex_digit1; +/// # fn main() { +/// assert_eq!(hex_digit1::<_, (_, ErrorKind)>("21cZ"), Ok(("Z", "21c"))); +/// assert_eq!(hex_digit1::<_, (_, ErrorKind)>("H2"), Err(Err::Error(("H2", ErrorKind::HexDigit)))); +/// assert_eq!(hex_digit1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn hex_digit1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position1(|item| !item.is_hex_digit(), ErrorKind::HexDigit) +} + +/// Recognizes zero or more octal characters: 0-7 +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non octal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::oct_digit0; +/// # fn main() { +/// assert_eq!(oct_digit0::<_, (_, ErrorKind)>("21cZ"), Ok(("cZ", "21"))); +/// assert_eq!(oct_digit0::<_, (_, ErrorKind)>("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(oct_digit0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn oct_digit0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position(|item| !item.is_oct_digit()) +} + +/// Recognizes one or more octal characters: 0-7 +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non octal digit character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::oct_digit1; +/// # fn main() { +/// assert_eq!(oct_digit1::<_, (_, ErrorKind)>("21cZ"), Ok(("cZ", "21"))); +/// assert_eq!(oct_digit1::<_, (_, ErrorKind)>("H2"), Err(Err::Error(("H2", ErrorKind::OctDigit)))); +/// assert_eq!(oct_digit1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn oct_digit1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position1(|item| !item.is_oct_digit(), ErrorKind::OctDigit) +} + +/// Recognizes zero or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non alphanumerical character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::alphanumeric0; +/// # fn main() { +/// assert_eq!(alphanumeric0::<_, (_, ErrorKind)>("21cZ%1"), Ok(("%1", "21cZ"))); +/// assert_eq!(alphanumeric0::<_, (_, ErrorKind)>("&Z21c"), Ok(("&Z21c", ""))); +/// assert_eq!(alphanumeric0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn alphanumeric0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position(|item| !item.is_alphanum()) +} + +/// Recognizes one or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non alphanumerical character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::alphanumeric1; +/// # fn main() { +/// assert_eq!(alphanumeric1::<_, (_, ErrorKind)>("21cZ%1"), Ok(("%1", "21cZ"))); +/// assert_eq!(alphanumeric1::<_, (_, ErrorKind)>("&H2"), Err(Err::Error(("&H2", ErrorKind::AlphaNumeric)))); +/// assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn alphanumeric1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar, +{ + input.split_at_position1(|item| !item.is_alphanum(), ErrorKind::AlphaNumeric) +} + +/// Recognizes zero or more spaces and tabs. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non space character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::space0; +/// # fn main() { +/// assert_eq!(space0::<_, (_, ErrorKind)>(" \t21c"), Ok(("21c", " \t"))); +/// assert_eq!(space0::<_, (_, ErrorKind)>("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(space0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn space0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar + Clone, +{ + input.split_at_position(|item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t') + }) +} +/// Recognizes one or more spaces and tabs. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non space character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::space1; +/// # fn main() { +/// assert_eq!(space1::<_, (_, ErrorKind)>(" \t21c"), Ok(("21c", " \t"))); +/// assert_eq!(space1::<_, (_, ErrorKind)>("H2"), Err(Err::Error(("H2", ErrorKind::Space)))); +/// assert_eq!(space1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn space1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar + Clone, +{ + input.split_at_position1( + |item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t') + }, + ErrorKind::Space, + ) +} + +/// Recognizes zero or more spaces, tabs, carriage returns and line feeds. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non space character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::multispace0; +/// # fn main() { +/// assert_eq!(multispace0::<_, (_, ErrorKind)>(" \t\n\r21c"), Ok(("21c", " \t\n\r"))); +/// assert_eq!(multispace0::<_, (_, ErrorKind)>("Z21c"), Ok(("Z21c", ""))); +/// assert_eq!(multispace0::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn multispace0>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar + Clone, +{ + input.split_at_position(|item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t' || c == '\r' || c == '\n') + }) +} + +/// Recognizes one or more spaces, tabs, carriage returns and line feeds. +/// +/// *streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there's not enough input data, +/// or if no terminating token is found (a non space character). +/// +/// # Example +/// +/// ``` +/// # use nom::{Err, error::ErrorKind, IResult, Needed}; +/// # use nom::character::streaming::multispace1; +/// # fn main() { +/// assert_eq!(multispace1::<_, (_, ErrorKind)>(" \t\n\r21c"), Ok(("21c", " \t\n\r"))); +/// assert_eq!(multispace1::<_, (_, ErrorKind)>("H2"), Err(Err::Error(("H2", ErrorKind::MultiSpace)))); +/// assert_eq!(multispace1::<_, (_, ErrorKind)>(""), Err(Err::Incomplete(Needed::Size(1)))); +/// # } +/// ``` +pub fn multispace1>(input: T) -> IResult +where + T: InputTakeAtPosition, + ::Item: AsChar + Clone, +{ + input.split_at_position1( + |item| { + let c = item.clone().as_char(); + !(c == ' ' || c == '\t' || c == '\r' || c == '\n') + }, + ErrorKind::MultiSpace, + ) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::internal::{Err, Needed}; + use crate::error::ErrorKind; + + macro_rules! assert_parse( + ($left: expr, $right: expr) => { + let res: $crate::IResult<_, _, (_, ErrorKind)> = $left; + assert_eq!(res, $right); + }; + ); + + #[test] + fn anychar_str() { + use super::anychar; + assert_eq!(anychar::<_, (&str, ErrorKind)>("Ә"), Ok(("", 'Ә'))); + } + + #[test] + fn character() { + let a: &[u8] = b"abcd"; + let b: &[u8] = b"1234"; + let c: &[u8] = b"a123"; + let d: &[u8] = "azé12".as_bytes(); + let e: &[u8] = b" "; + let f: &[u8] = b" ;"; + //assert_eq!(alpha1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + assert_parse!(alpha1(a), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + alpha1(b), + Err(Err::Error((b, ErrorKind::Alpha))) + ); + assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &b"a"[..]))); + assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12".as_bytes(), &b"az"[..]))); + assert_eq!( + digit1(a), + Err(Err::Error((a, ErrorKind::Digit))) + ); + assert_eq!(digit1::<_, (_, ErrorKind)>(b), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + digit1(c), + Err(Err::Error((c, ErrorKind::Digit))) + ); + assert_eq!( + digit1(d), + Err(Err::Error((d, ErrorKind::Digit))) + ); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(b), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(c), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12".as_bytes(), &b"a"[..]))); + assert_eq!( + hex_digit1(e), + Err(Err::Error((e, ErrorKind::HexDigit))) + ); + assert_eq!( + oct_digit1(a), + Err(Err::Error((a, ErrorKind::OctDigit))) + ); + assert_eq!(oct_digit1::<_, (_, ErrorKind)>(b), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + oct_digit1(c), + Err(Err::Error((c, ErrorKind::OctDigit))) + ); + assert_eq!( + oct_digit1(d), + Err(Err::Error((d, ErrorKind::OctDigit))) + ); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + //assert_eq!(fix_error!(b,(), alphanumeric1), Ok((empty, b))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(c), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(d), Ok(("é12".as_bytes(), &b"az"[..]))); + assert_eq!(space1::<_, (_, ErrorKind)>(e), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(space1::<_, (_, ErrorKind)>(f), Ok((&b";"[..], &b" "[..]))); + } + + #[cfg(feature = "alloc")] + #[test] + fn character_s() { + let a = "abcd"; + let b = "1234"; + let c = "a123"; + let d = "azé12"; + let e = " "; + assert_eq!(alpha1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + alpha1(b), + Err(Err::Error((b, ErrorKind::Alpha))) + ); + assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &"a"[..]))); + assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", &"az"[..]))); + assert_eq!( + digit1(a), + Err(Err::Error((a, ErrorKind::Digit))) + ); + assert_eq!(digit1::<_, (_, ErrorKind)>(b), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + digit1(c), + Err(Err::Error((c, ErrorKind::Digit))) + ); + assert_eq!( + digit1(d), + Err(Err::Error((d, ErrorKind::Digit))) + ); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(b), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(c), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", &"a"[..]))); + assert_eq!( + hex_digit1(e), + Err(Err::Error((e, ErrorKind::HexDigit))) + ); + assert_eq!( + oct_digit1(a), + Err(Err::Error((a, ErrorKind::OctDigit))) + ); + assert_eq!(oct_digit1::<_, (_, ErrorKind)>(b), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!( + oct_digit1(c), + Err(Err::Error((c, ErrorKind::OctDigit))) + ); + assert_eq!( + oct_digit1(d), + Err(Err::Error((d, ErrorKind::OctDigit))) + ); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(a), Err(Err::Incomplete(Needed::Size(1)))); + //assert_eq!(fix_error!(b,(), alphanumeric1), Ok((empty, b))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(c), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(d), Ok(("é12", "az"))); + assert_eq!(space1::<_, (_, ErrorKind)>(e), Err(Err::Incomplete(Needed::Size(1)))); + } + + use crate::traits::Offset; + #[test] + fn offset() { + let a = &b"abcd;"[..]; + let b = &b"1234;"[..]; + let c = &b"a123;"[..]; + let d = &b" \t;"[..]; + let e = &b" \t\r\n;"[..]; + let f = &b"123abcDEF;"[..]; + + match alpha1::<_, (_, ErrorKind)>(a) { + Ok((i, _)) => { + assert_eq!(a.offset(i) + i.len(), a.len()); + } + _ => panic!("wrong return type in offset test for alpha"), + } + match digit1::<_, (_, ErrorKind)>(b) { + Ok((i, _)) => { + assert_eq!(b.offset(i) + i.len(), b.len()); + } + _ => panic!("wrong return type in offset test for digit"), + } + match alphanumeric1::<_, (_, ErrorKind)>(c) { + Ok((i, _)) => { + assert_eq!(c.offset(i) + i.len(), c.len()); + } + _ => panic!("wrong return type in offset test for alphanumeric"), + } + match space1::<_, (_, ErrorKind)>(d) { + Ok((i, _)) => { + assert_eq!(d.offset(i) + i.len(), d.len()); + } + _ => panic!("wrong return type in offset test for space"), + } + match multispace1::<_, (_, ErrorKind)>(e) { + Ok((i, _)) => { + assert_eq!(e.offset(i) + i.len(), e.len()); + } + _ => panic!("wrong return type in offset test for multispace"), + } + match hex_digit1::<_, (_, ErrorKind)>(f) { + Ok((i, _)) => { + assert_eq!(f.offset(i) + i.len(), f.len()); + } + _ => panic!("wrong return type in offset test for hex_digit"), + } + match oct_digit1::<_, (_, ErrorKind)>(f) { + Ok((i, _)) => { + assert_eq!(f.offset(i) + i.len(), f.len()); + } + _ => panic!("wrong return type in offset test for oct_digit"), + } + } + + #[test] + fn is_not_line_ending_bytes() { + let a: &[u8] = b"ab12cd\nefgh"; + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(a), Ok((&b"\nefgh"[..], &b"ab12cd"[..]))); + + let b: &[u8] = b"ab12cd\nefgh\nijkl"; + assert_eq!( + not_line_ending::<_, (_, ErrorKind)>(b), + Ok((&b"\nefgh\nijkl"[..], &b"ab12cd"[..])) + ); + + let c: &[u8] = b"ab12cd\r\nefgh\nijkl"; + assert_eq!( + not_line_ending::<_, (_, ErrorKind)>(c), + Ok((&b"\r\nefgh\nijkl"[..], &b"ab12cd"[..])) + ); + + let d: &[u8] = b"ab12cd"; + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(d), Err(Err::Incomplete(Needed::Unknown))); + } + + #[test] + fn is_not_line_ending_str() { + /* + let a: &str = "ab12cd\nefgh"; + assert_eq!(not_line_ending(a), Ok((&"\nefgh"[..], &"ab12cd"[..]))); + + let b: &str = "ab12cd\nefgh\nijkl"; + assert_eq!(not_line_ending(b), Ok((&"\nefgh\nijkl"[..], &"ab12cd"[..]))); + + let c: &str = "ab12cd\r\nefgh\nijkl"; + assert_eq!(not_line_ending(c), Ok((&"\r\nefgh\nijkl"[..], &"ab12cd"[..]))); + + let d = "βèƒôřè\nÂßÇáƒƭèř"; + assert_eq!(not_line_ending(d), Ok((&"\nÂßÇáƒƭèř"[..], &"βèƒôřè"[..]))); + + let e = "βèƒôřè\r\nÂßÇáƒƭèř"; + assert_eq!(not_line_ending(e), Ok((&"\r\nÂßÇáƒƭèř"[..], &"βèƒôřè"[..]))); + */ + + let f = "βèƒôřè\rÂßÇáƒƭèř"; + assert_eq!( + not_line_ending(f), + Err(Err::Error((f, ErrorKind::Tag))) + ); + + let g2: &str = "ab12cd"; + assert_eq!(not_line_ending::<_, (_, ErrorKind)>(g2), Err(Err::Incomplete(Needed::Unknown))); + } + + #[test] + fn hex_digit_test() { + let i = &b"0123456789abcdefABCDEF;"[..]; + assert_parse!(hex_digit1(i), Ok((&b";"[..], &i[..i.len() - 1]))); + + let i = &b"g"[..]; + assert_parse!( + hex_digit1(i), + Err(Err::Error(error_position!(i, ErrorKind::HexDigit))) + ); + + let i = &b"G"[..]; + assert_parse!( + hex_digit1(i), + Err(Err::Error(error_position!(i, ErrorKind::HexDigit))) + ); + + assert!(crate::character::is_hex_digit(b'0')); + assert!(crate::character::is_hex_digit(b'9')); + assert!(crate::character::is_hex_digit(b'a')); + assert!(crate::character::is_hex_digit(b'f')); + assert!(crate::character::is_hex_digit(b'A')); + assert!(crate::character::is_hex_digit(b'F')); + assert!(!crate::character::is_hex_digit(b'g')); + assert!(!crate::character::is_hex_digit(b'G')); + assert!(!crate::character::is_hex_digit(b'/')); + assert!(!crate::character::is_hex_digit(b':')); + assert!(!crate::character::is_hex_digit(b'@')); + assert!(!crate::character::is_hex_digit(b'\x60')); + } + + #[test] + fn oct_digit_test() { + let i = &b"01234567;"[..]; + assert_parse!(oct_digit1(i), Ok((&b";"[..], &i[..i.len() - 1]))); + + let i = &b"8"[..]; + assert_parse!( + oct_digit1(i), + Err(Err::Error(error_position!(i, ErrorKind::OctDigit))) + ); + + assert!(crate::character::is_oct_digit(b'0')); + assert!(crate::character::is_oct_digit(b'7')); + assert!(!crate::character::is_oct_digit(b'8')); + assert!(!crate::character::is_oct_digit(b'9')); + assert!(!crate::character::is_oct_digit(b'a')); + assert!(!crate::character::is_oct_digit(b'A')); + assert!(!crate::character::is_oct_digit(b'/')); + assert!(!crate::character::is_oct_digit(b':')); + assert!(!crate::character::is_oct_digit(b'@')); + assert!(!crate::character::is_oct_digit(b'\x60')); + } + + #[test] + fn full_line_windows() { + named!( + take_full_line<(&[u8], &[u8])>, + tuple!(not_line_ending, line_ending) + ); + let input = b"abc\r\n"; + let output = take_full_line(input); + assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\r\n"[..])))); + } + + #[test] + fn full_line_unix() { + named!( + take_full_line<(&[u8], &[u8])>, + tuple!(not_line_ending, line_ending) + ); + let input = b"abc\n"; + let output = take_full_line(input); + assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\n"[..])))); + } + + #[test] + fn check_windows_lineending() { + let input = b"\r\n"; + let output = line_ending(&input[..]); + assert_parse!(output, Ok((&b""[..], &b"\r\n"[..]))); + } + + #[test] + fn check_unix_lineending() { + let input = b"\n"; + let output = line_ending(&input[..]); + assert_parse!(output, Ok((&b""[..], &b"\n"[..]))); + } + + #[test] + fn cr_lf() { + assert_parse!(crlf(&b"\r\na"[..]), Ok((&b"a"[..], &b"\r\n"[..]))); + assert_parse!(crlf(&b"\r"[..]), Err(Err::Incomplete(Needed::Size(2)))); + assert_parse!( + crlf(&b"\ra"[..]), + Err(Err::Error(error_position!(&b"\ra"[..], ErrorKind::CrLf))) + ); + + assert_parse!(crlf("\r\na"), Ok(("a", "\r\n"))); + assert_parse!(crlf("\r"), Err(Err::Incomplete(Needed::Size(2)))); + assert_parse!( + crlf("\ra"), + Err(Err::Error(error_position!("\ra", ErrorKind::CrLf))) + ); + } + + #[test] + fn end_of_line() { + assert_parse!(line_ending(&b"\na"[..]), Ok((&b"a"[..], &b"\n"[..]))); + assert_parse!(line_ending(&b"\r\na"[..]), Ok((&b"a"[..], &b"\r\n"[..]))); + assert_parse!(line_ending(&b"\r"[..]), Err(Err::Incomplete(Needed::Size(2)))); + assert_parse!( + line_ending(&b"\ra"[..]), + Err(Err::Error(error_position!(&b"\ra"[..], ErrorKind::CrLf))) + ); + + assert_parse!(line_ending("\na"), Ok(("a", "\n"))); + assert_parse!(line_ending("\r\na"), Ok(("a", "\r\n"))); + assert_parse!(line_ending("\r"), Err(Err::Incomplete(Needed::Size(2)))); + assert_parse!( + line_ending("\ra"), + Err(Err::Error(error_position!("\ra", ErrorKind::CrLf))) + ); + } +} diff --git a/third_party/rust/nom/src/combinator/macros.rs b/third_party/rust/nom/src/combinator/macros.rs new file mode 100644 index 0000000000..59df06f660 --- /dev/null +++ b/third_party/rust/nom/src/combinator/macros.rs @@ -0,0 +1,1201 @@ +//! Macro combinators +//! +//! Macros are used to make combination easier, +//! since they often do not depend on the type +//! of the data they manipulate or return. +//! +//! There is a trick to make them easier to assemble, +//! combinators are defined like this: +//! +//! ```ignore +//! macro_rules! tag ( +//! ($i:expr, $inp: expr) => ( +//! { +//! ... +//! } +//! ); +//! ); +//! ``` +//! +//! But when used in other combinators, are Used +//! like this: +//! +//! ```ignore +//! named!(my_function, tag!("abcd")); +//! ``` +//! +//! Internally, other combinators will rewrite +//! that call to pass the input as first argument: +//! +//! ```ignore +//! macro_rules! named ( +//! ($name:ident, $submac:ident!( $($args:tt)* )) => ( +//! fn $name<'a>( i: &'a [u8] ) -> IResult<'a,&[u8], &[u8]> { +//! $submac!(i, $($args)*) +//! } +//! ); +//! ); +//! ``` +//! +//! If you want to call a combinator directly, you can +//! do it like this: +//! +//! ```ignore +//! let res = { tag!(input, "abcd"); } +//! ``` +//! +//! Combinators must have a specific variant for +//! non-macro arguments. Example: passing a function +//! to take_while! instead of another combinator. +//! +//! ```ignore +//! macro_rules! take_while( +//! ($input:expr, $submac:ident!( $($args:tt)* )) => ( +//! { +//! ... +//! } +//! ); +//! +//! // wrap the function in a macro to pass it to the main implementation +//! ($input:expr, $f:expr) => ( +//! take_while!($input, call!($f)); +//! ); +//! ); +//! ``` +#[allow(unused_variables)] + +/// Makes a function from a parser combination +/// +/// The type can be set up if the compiler needs +/// more information +/// +/// Function-like declaration: +/// ``` +/// # use nom::{named, tag}; +/// named!(my_function( &[u8] ) -> &[u8], tag!("abcd")); +/// ``` +/// Alternative declaration. First type parameter is input, second is output: +/// ``` +/// # use nom::{named, tag}; +/// named!(my_function<&[u8], &[u8]>, tag!("abcd")); +/// ``` +/// This one will have `&[u8]` as input type, `&[u8]` as output type: +/// ``` +/// # use nom::{named, tag}; +/// named!(my_function, tag!("abcd")); +/// ``` +/// Will use `&[u8]` as output type: +/// ``` +/// # use nom::{named, tag}; +/// named!(my_function<&[u8]>, tag!("abcd")); +/// ``` +/// Prefix them with 'pub' to make the functions public: +/// ``` +/// # use nom::{named, tag}; +/// named!(pub my_function, tag!("abcd")); +/// ``` +/// Prefix them with 'pub(crate)' to make the functions public within the crate: +/// ``` +/// # use nom::{named, tag}; +/// named!(pub(crate) my_function, tag!("abcd")); +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! named ( + (#$($args:tt)*) => ( + named_attr!(#$($args)*); + ); + ($vis:vis $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( + $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, ($i, $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); + ($vis:vis $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( + $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> { + $submac!(i, $($args)*) + } + ); + ($vis:vis $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( + $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, ($i, $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); + ($vis:vis $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + $vis fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, (&[u8], $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); + ($vis:vis $name:ident, $submac:ident!( $($args:tt)* )) => ( + $vis fn $name( i: &[u8] ) -> $crate::IResult<&[u8], &[u8], (&[u8], $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); +); + +/// Makes a function from a parser combination with arguments. +/// +/// ```ignore +/// //takes [`&[u8]`] as input +/// named_args!(tagged(open_tag: &[u8], close_tag: &[u8])<&str>, +/// delimited!(tag!(open_tag), map_res!(take!(4), str::from_utf8), tag!(close_tag)) +/// ); + +/// //takes `&str` as input +/// named_args!(tagged(open_tag: &str, close_tag: &str)<&str, &str>, +/// delimited!(tag!(open_tag), take!(4), tag!(close_tag)) +/// ); +/// ``` +/// +/// Note: if using arguments that way gets hard to read, it is always +/// possible to write the equivalent parser definition manually, like +/// this: +/// +/// ```ignore +/// fn tagged(input: &[u8], open_tag: &[u8], close_tag: &[u8]) -> IResult<&[u8], &str> { +/// // the first combinator in the tree gets the input as argument. It is then +/// // passed from one combinator to the next through macro rewriting +/// delimited!(input, +/// tag!(open_tag), take!(4), tag!(close_tag) +/// ) +/// ); +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! named_args { + ($vis:vis $func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { + $vis fn $func_name(input: &[u8], $( $arg : $typ ),*) -> $crate::IResult<&[u8], $return_type> { + $submac!(input, $($args)*) + } + }; + + ($vis:vis $func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { + $vis fn $func_name<'this_is_probably_unique_i_hope_please, 'a>( + input: &'this_is_probably_unique_i_hope_please [u8], $( $arg : $typ ),*) -> + $crate::IResult<&'this_is_probably_unique_i_hope_please [u8], $return_type> + { + $submac!(input, $($args)*) + } + }; + + ($vis:vis $func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $input_type:ty, $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { + $vis fn $func_name(input: $input_type, $( $arg : $typ ),*) -> $crate::IResult<$input_type, $return_type> { + $submac!(input, $($args)*) + } + }; + + ($vis:vis $func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $input_type:ty, $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { + $vis fn $func_name<'a>( + input: $input_type, $( $arg : $typ ),*) + -> $crate::IResult<$input_type, $return_type> + { + $submac!(input, $($args)*) + } + }; +} + +/// Makes a function from a parser combination, with attributes +/// +/// The usage of this macro is almost identical to `named!`, except that +/// you also pass attributes to be attached to the generated function. +/// This is ideal for adding documentation to your parser. +/// +/// Create my_function as if you wrote it with the doc comment /// My Func: +/// ``` +/// # use nom::{named_attr, tag}; +/// named_attr!(#[doc = "My Func"], my_function( &[u8] ) -> &[u8], tag!("abcd")); +/// ``` +/// Also works for pub functions, and multiple lines: +/// ``` +/// # use nom::{named_attr, tag}; +/// named_attr!(#[doc = "My Func\nRecognise abcd"], pub my_function, tag!("abcd")); +/// ``` +/// Multiple attributes can be passed if required: +/// ``` +/// # use nom::{named_attr, tag}; +/// named_attr!(#[doc = "My Func"] #[inline(always)], pub my_function, tag!("abcd")); +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! named_attr ( + ($(#[$attr:meta])*, $vis:vis $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + $vis fn $name( i: $i ) -> $crate::IResult<$i,$o, ($i, $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, $vis:vis $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, $vis:vis $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, ($i, $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, $vis:vis $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + $vis fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, (&[u8], $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); + ($(#[$attr:meta])*, $vis:vis $name:ident, $submac:ident!( $($args:tt)* )) => ( + $(#[$attr])* + $vis fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<&[u8], &[u8], (&[u8], $crate::error::ErrorKind)> { + $submac!(i, $($args)*) + } + ); +); + +/// Used to wrap common expressions and function as macros +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// # fn main() { +/// fn take_wrapper(input: &[u8], i: u8) -> IResult<&[u8], &[u8]> { take!(input, i * 10) } +/// +/// // will make a parser taking 20 bytes +/// named!(parser, call!(take_wrapper, 2)); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! call ( + ($i:expr, $fun:expr) => ( $fun( $i ) ); + ($i:expr, $fun:expr, $($args:expr),* ) => ( $fun( $i, $($args),* ) ); +); + +//FIXME: error rewrite +/// Prevents backtracking if the child parser fails +/// +/// This parser will do an early return instead of sending +/// its result to the parent parser. +/// +/// If another `return_error!` combinator is present in the parent +/// chain, the error will be wrapped and another early +/// return will be made. +/// +/// This makes it easy to build report on which parser failed, +/// where it failed in the input, and the chain of parsers +/// that led it there. +/// +/// Additionally, the error chain contains number identifiers +/// that can be matched to provide useful error messages. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(err_test<&[u8], &[u8]>, alt!( +/// tag!("abcd") | +/// preceded!(tag!("efgh"), return_error!(ErrorKind::Eof, +/// do_parse!( +/// tag!("ijkl") >> +/// res: return_error!(ErrorKind::Tag, tag!("mnop")) >> +/// (res) +/// ) +/// ) +/// ) +/// )); +/// let a = &b"efghblah"[..]; +/// let b = &b"efghijklblah"[..]; +/// let c = &b"efghijklmnop"[..]; +/// +/// let blah = &b"blah"[..]; +/// +/// let res_a = err_test(a); +/// let res_b = err_test(b); +/// let res_c = err_test(c); +/// assert_eq!(res_a, Err(Err::Failure(error_node_position!(blah, ErrorKind::Eof, error_position!(blah, ErrorKind::Tag))))); +/// assert_eq!(res_b, Err(Err::Failure(error_node_position!(&b"ijklblah"[..], ErrorKind::Eof, +/// error_node_position!(blah, ErrorKind::Tag, error_position!(blah, ErrorKind::Tag)))) +/// )); +/// # } +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! return_error ( + ($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + let i_ = $i.clone(); + let cl = || { + $submac!(i_, $($args)*) + }; + + match cl() { + Err(Err::Incomplete(x)) => Err(Err::Incomplete(x)), + Ok((i, o)) => Ok((i, o)), + Err(Err::Error(e)) | Err(Err::Failure(e)) => { + return Err(Err::Failure($crate::error::append_error($i, $code, e))) + } + } + } + ); + ($i:expr, $code:expr, $f:expr) => ( + return_error!($i, $code, call!($f)); + ); + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + let i_ = $i.clone(); + let cl = || { + $submac!(i_, $($args)*) + }; + + match cl() { + Err(Err::Incomplete(x)) => Err(Err::Incomplete(x)), + Ok((i, o)) => Ok((i, o)), + Err(Err::Error(e)) | Err(Err::Failure(e)) => { + return Err(Err::Failure(e)) + } + } + } + ); + ($i:expr, $f:expr) => ( + return_error!($i, call!($f)); + ); +); + +//FIXME: error rewrite +/// Add an error if the child parser fails +/// +/// While `return_error!` does an early return and avoids backtracking, +/// add_return_error! backtracks normally. It just provides more context +/// for an error +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::collections; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(err_test, add_return_error!(ErrorKind::Tag, tag!("abcd"))); +/// +/// let a = &b"efghblah"[..]; +/// let res_a = err_test(a); +/// assert_eq!(res_a, Err(Err::Error(error_node_position!(a, ErrorKind::Tag, error_position!(a, ErrorKind::Tag))))); +/// # } +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! add_return_error ( + ($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind}; + + match $submac!($i, $($args)*) { + Ok((i, o)) => Ok((i, o)), + Err(Err::Error(e)) => { + Err(Err::Error(error_node_position!($i, $code, e))) + }, + Err(Err::Failure(e)) => { + Err(Err::Failure(error_node_position!($i, $code, e))) + }, + Err(e) => Err(e), + } + } + ); + ($i:expr, $code:expr, $f:expr) => ( + add_return_error!($i, $code, call!($f)); + ); +); + +/// replaces a `Incomplete` returned by the child parser +/// with an `Error` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::collections; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(take_5, complete!(take!(5))); +/// +/// let a = &b"abcd"[..]; +/// let res_a = take_5(a); +/// assert_eq!(res_a, Err(Err::Error(error_position!(a, ErrorKind::Complete)))); +/// # } +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! complete ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::completec($i, move |i| { $submac!(i, $($args)*) }) + ); + ($i:expr, $f:expr) => ( + complete!($i, call!($f)); + ); +); + +/// A bit like `std::try!`, this macro will return the remaining input and +/// parsed value if the child parser returned `Ok`, and will do an early +/// return for the `Err` side. +/// +/// this can provide more flexibility than `do_parse!` if needed +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # use nom::IResult; +/// +/// fn take_add(input:&[u8], size: u8) -> IResult<&[u8], &[u8]> { +/// let (i1, length) = try_parse!(input, map_opt!(nom::number::streaming::be_u8, |sz| size.checked_add(sz))); +/// let (i2, data) = try_parse!(i1, take!(length)); +/// return Ok((i2, data)); +/// } +/// # fn main() { +/// let arr1 = [1, 2, 3, 4, 5]; +/// let r1 = take_add(&arr1[..], 1); +/// assert_eq!(r1, Ok((&[4,5][..], &[2,3][..]))); +/// +/// let arr2 = [0xFE, 2, 3, 4, 5]; +/// // size is overflowing +/// let r1 = take_add(&arr2[..], 42); +/// assert_eq!(r1, Err(Err::Error(error_position!(&[254, 2,3,4,5][..], ErrorKind::MapOpt)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! try_parse ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match $submac!($i, $($args)*) { + Ok((i,o)) => (i,o), + Err(e) => return Err(e), + } + }); + ($i:expr, $f:expr) => ( + try_parse!($i, call!($f)) + ); +); + +/// `map!(I -> IResult, O -> P) => I -> IResult` +/// +/// maps a function on the result of a parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// # fn main() { +/// +/// named!(parse<&str, usize>, map!(digit1, |s| s.len())); +/// +/// // the parser will count how many characters were returned by digit1 +/// assert_eq!(parse("123456"), Ok(("", 6))); +/// +/// // this will fail if digit1 fails +/// assert_eq!(parse("abc"), Err(Err::Error(error_position!("abc", ErrorKind::Digit)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! map( + // Internal parser, do not use directly + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + $crate::combinator::mapc($i, move |i| {$submac!(i, $($args)*)}, $g) + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + map!(__impl $i, $submac!($($args)*), $g); + ); + ($i:expr, $f:expr, $g:expr) => ( + map!(__impl $i, call!($f), $g); + ); +); + +/// `map_res!(I -> IResult, O -> Result

) => I -> IResult` +/// maps a function returning a Result on the output of a parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// # fn main() { +/// +/// named!(parse<&str, u8>, map_res!(digit1, |s: &str| s.parse::())); +/// +/// // the parser will convert the result of digit1 to a number +/// assert_eq!(parse("123"), Ok(("", 123))); +/// +/// // this will fail if digit1 fails +/// assert_eq!(parse("abc"), Err(Err::Error(error_position!("abc", ErrorKind::Digit)))); +/// +/// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) +/// assert_eq!(parse("123456"), Err(Err::Error(error_position!("123456", ErrorKind::MapRes)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! map_res ( + // Internal parser, do not use directly + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + $crate::combinator::map_resc($i, move |i| {$submac!(i, $($args)*)}, move |i| {$submac2!(i, $($args2)*)}) + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + map_res!(__impl $i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + map_res!(__impl $i, $submac!($($args)*), $submac2!($($args2)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + map_res!(__impl $i, call!($f), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + map_res!(__impl $i, call!($f), $submac!($($args)*)); + ); +); + +/// `map_opt!(I -> IResult, O -> Option

) => I -> IResult` +/// maps a function returning an Option on the output of a parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// # fn main() { +/// +/// named!(parser<&str, u8>, map_opt!(digit1, |s: &str| s.parse::().ok())); +/// +/// // the parser will convert the result of digit1 to a number +/// assert_eq!(parser("123"), Ok(("", 123))); +/// +/// // this will fail if digit1 fails +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Digit)))); +/// +/// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) +/// assert_eq!(parser("123456"), Err(Err::Error(("123456", ErrorKind::MapOpt)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! map_opt ( + // Internal parser, do not use directly + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + $crate::combinator::map_optc($i, move |i| {$submac!(i, $($args)*)}, move |i| {$submac2!(i, $($args2)*)}) + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + map_opt!(__impl $i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + map_opt!(__impl $i, $submac!($($args)*), $submac2!($($args2)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + map_opt!(__impl $i, call!($f), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + map_opt!(__impl $i, call!($f), $submac!($($args)*)); + ); +); + +/// `parse_to!(O) => I -> IResult` +/// uses the `parse` method from `std::str::FromStr` to convert the current +/// input to the specified type +/// +/// this will completely consume the input +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// # fn main() { +/// +/// named!(parser<&str, u8>, parse_to!(u8)); +/// +/// assert_eq!(parser("123"), Ok(("", 123))); +/// +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::ParseTo)))); +/// +/// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) +/// assert_eq!(parser("123456"), Err(Err::Error(("123456", ErrorKind::ParseTo)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! parse_to ( + ($i:expr, $t:ty ) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option; + use $crate::lib::std::option::Option::*; + use $crate::{Err,error::ErrorKind}; + + use $crate::ParseTo; + use $crate::Slice; + use $crate::InputLength; + + let res: Option<$t> = ($i).parse_to(); + match res { + Some(output) => Ok(($i.slice($i.input_len()..), output)), + None => Err(Err::Error($crate::error::make_error($i, ErrorKind::ParseTo))) + } + } + ); +); + +/// `verify!(I -> IResult, O -> bool) => I -> IResult` +/// returns the result of the child parser if it satisfies a verification function +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(check, verify!(nom::number::streaming::be_u32, |val: &u32| *val < 3)); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! verify ( + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + $crate::combinator::verifyc($i, |i| $submac!(i, $($args)*), $g) + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + $crate::combinator::verifyc($i, |i| $submac!(i, $($args)*), |&o| $submac2!(o, $($args2)*)) + ); + ($i:expr, $f:expr, $g:expr) => ( + $crate::combinator::verify($f, $g)($i) + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::verify($f, |&o| $submac!(o, $($args)*))($i) + ); +); + +/// `value!(T, R -> IResult ) => R -> IResult` +/// +/// or `value!(T) => R -> IResult` +/// +/// If the child parser was successful, return the value. +/// If no child parser is provided, always return the value +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(x, value!(42, delimited!(tag!("")))); +/// named!(y, delimited!(tag!(""))); +/// let r = x(&b" aaa"[..]); +/// assert_eq!(r, Ok((&b" aaa"[..], 42))); +/// +/// let r2 = y(&b" aaa"[..]); +/// assert_eq!(r2, Ok((&b" aaa"[..], 42))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! value ( + ($i:expr, $res:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::valuec($i, $res, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $res:expr, $f:expr) => ( + $crate::combinator::valuec($i, $res, $f) + ); + ($i:expr, $res:expr) => ( + Ok(($i, $res)) + ); +); + +/// `opt!(I -> IResult) => I -> IResult>` +/// make the underlying parser optional +/// +/// returns an Option of the returned type. This parser returns `Some(result)` if the child parser +/// succeeds,`None` if it fails, and `Incomplete` if it did not have enough data to decide +/// +/// *Warning*: if you are using `opt` for some kind of optional ending token (like an end of line), +/// you should combine it with `complete` to make sure it works. +/// +/// As an example, `opt!(tag!("\r\n"))` will return `Incomplete` if it receives an empty input, +/// because `tag` does not have enough input to decide. +/// On the contrary, `opt!(complete!(tag!("\r\n")))` would return `None` as produced value, +/// since `complete!` transforms an `Incomplete` in an `Error`. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!( o<&[u8], Option<&[u8]> >, opt!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdef"; +/// let b = b"bcdefg"; +/// assert_eq!(o(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); +/// assert_eq!(o(&b[..]), Ok((&b"bcdefg"[..], None))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! opt( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + $crate::combinator::optc($i, |i| $submac!(i, $($args)*)) + } + ); + ($i:expr, $f:expr) => ( + $crate::combinator::opt($f)($i) + ); +); + +/// `opt_res!(I -> IResult) => I -> IResult>` +/// make the underlying parser optional +/// +/// returns a Result, with Err containing the parsing error +/// +/// ```ignore +/// # #[macro_use] extern crate nom; +/// # use nom::ErrorKind; +/// # fn main() { +/// named!( o<&[u8], Result<&[u8], nom::Err<&[u8]> > >, opt_res!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdef"; +/// let b = b"bcdefg"; +/// assert_eq!(o(&a[..]), Ok((&b"ef"[..], Ok(&b"abcd"[..]))); +/// assert_eq!(o(&b[..]), Ok((&b"bcdefg"[..], Err(error_position!(&b[..], ErrorKind::Tag)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! opt_res ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + Ok((i,o)) => Ok((i, Ok(o))), + Err(Err::Error(e)) => Ok(($i, Err(Err::Error(e)))), + // in case of failure, we return a real error + Err(e) => Err(e) + } + } + ); + ($i:expr, $f:expr) => ( + opt_res!($i, call!($f)); + ); +); + +/// `cond!(bool, I -> IResult) => I -> IResult>` +/// Conditional combinator +/// +/// Wraps another parser and calls it if the +/// condition is met. This combinator returns +/// an Option of the return type of the child +/// parser. +/// +/// This is especially useful if a parser depends +/// on the value returned by a preceding parser in +/// a `do_parse!`. +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// # fn main() { +/// fn f_true(i: &[u8]) -> IResult<&[u8], Option<&[u8]>> { +/// cond!(i, true, tag!("abcd")) +/// } +/// +/// fn f_false(i: &[u8]) -> IResult<&[u8], Option<&[u8]>> { +/// cond!(i, false, tag!("abcd")) +/// } +/// +/// let a = b"abcdef"; +/// assert_eq!(f_true(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); +/// +/// assert_eq!(f_false(&a[..]), Ok((&b"abcdef"[..], None))); +/// # } +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! cond( + ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::condc($i, $cond, |i| $submac!(i, $($args)*) ) + ); + ($i:expr, $cond:expr, $f:expr) => ( + $crate::combinator::cond($cond, $f)($i) + ); +); + +/// `peek!(I -> IResult) => I -> IResult` +/// returns a result without consuming the input +/// +/// the embedded parser may return Err(Err::Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(ptag, peek!( tag!( "abcd" ) ) ); +/// +/// let r = ptag(&b"abcdefgh"[..]); +/// assert_eq!(r, Ok((&b"abcdefgh"[..], &b"abcd"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! peek( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::peekc($i, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr) => ( + $crate::combinator::peek($f)($i) + ); +); + +/// `not!(I -> IResult) => I -> IResult` +/// returns a result only if the embedded parser returns Error or Err(Err::Incomplete) +/// does not consume the input +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(not_e, do_parse!( +/// res: tag!("abc") >> +/// not!(char!('e')) >> +/// (res) +/// )); +/// +/// let r = not_e(&b"abcd"[..]); +/// assert_eq!(r, Ok((&b"d"[..], &b"abc"[..]))); +/// +/// let r2 = not_e(&b"abce"[..]); +/// assert_eq!(r2, Err(Err::Error(error_position!(&b"e"[..], ErrorKind::Not)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! not( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::notc($i, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr) => ( + $crate::combinator::not($f)($i) + ); +); + +/// `tap!(name: I -> IResult => { block }) => I -> IResult` +/// allows access to the parser's result without affecting it +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::str; +/// # fn main() { +/// named!(ptag, tap!(res: tag!( "abcd" ) => { println!("recognized {}", str::from_utf8(res).unwrap()) } ) ); +/// +/// let r = ptag(&b"abcdefgh"[..]); +/// assert_eq!(r, Ok((&b"efgh"[..], &b"abcd"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! tap ( + ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,Needed,IResult}; + + match $submac!($i, $($args)*) { + Ok((i,o)) => { + let $name = o; + $e; + Ok((i, $name)) + }, + Err(e) => Err(Err::convert(e)), + } + } + ); + ($i:expr, $name: ident: $f:expr => $e:expr) => ( + tap!($i, $name: call!($f) => $e); + ); +); + +/// `eof!()` returns its input if it is at the end of input data +/// +/// When we're at the end of the data, this combinator +/// will succeed +/// +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use std::str; +/// # use nom::{Err, error::ErrorKind}; +/// # fn main() { +/// named!(parser, eof!()); +/// +/// assert_eq!(parser(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); +/// assert_eq!(parser(&b""[..]), Ok((&b""[..], &b""[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! eof ( + ($i:expr,) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind}; + + use $crate::InputLength; + if ($i).input_len() == 0 { + Ok(($i, $i)) + } else { + Err(Err::Error(error_position!($i, ErrorKind::Eof))) + } + } + ); +); + +/// `exact!()` will fail if the child parser does not consume the whole data +/// +/// TODO: example +#[macro_export(local_inner_macros)] +macro_rules! exact ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ({ + terminated!($i, $submac!( $($args)*), eof!()) + }); + ($i:expr, $f:expr) => ( + exact!($i, call!($f)); + ); +); + +/// `recognize!(I -> IResult ) => I -> IResult` +/// if the child parser was successful, return the consumed input as produced value +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(x, recognize!(delimited!(tag!("")))); +/// let r = x(&b" aaa"[..]); +/// assert_eq!(r, Ok((&b" aaa"[..], &b""[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! recognize ( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::combinator::recognizec($i, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr) => ( + $crate::combinator::recognize($f)($i) + ); +); + +#[cfg(test)] +mod tests { + use crate::internal::{Err, IResult, Needed}; + use crate::error::ParseError; + use crate::error::ErrorKind; + #[cfg(feature = "alloc")] + use crate::lib::std::boxed::Box; + + // reproduce the tag and take macros, because of module import order + macro_rules! tag ( + ($i:expr, $tag: expr) => ({ + use $crate::lib::std::result::Result::*; + use $crate::{Err,Needed,IResult,error::ErrorKind}; + use $crate::{Compare,CompareResult,InputLength,Slice}; + + let res: IResult<_,_> = match ($i).compare($tag) { + CompareResult::Ok => { + let blen = $tag.input_len(); + Ok(($i.slice(blen..), $i.slice(..blen))) + }, + CompareResult::Incomplete => { + Err(Err::Incomplete(Needed::Size($tag.input_len()))) + }, + CompareResult::Error => { + let e:ErrorKind = ErrorKind::Tag; + Err(Err::Error($crate::error::make_error($i, e))) + } + }; + res + }); + ); + + macro_rules! take( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res:IResult<&[u8],&[u8]> = if $i.len() < cnt { + Err($crate::Err::Incomplete($crate::Needed::Size(cnt))) + } else { + Ok((&$i[cnt..],&$i[0..cnt])) + }; + res + } + ); + ); + + mod pub_named_mod { + named!(pub tst, tag!("abcd")); + } + + #[test] + fn pub_named_test() { + let a = &b"abcd"[..]; + let res = pub_named_mod::tst(a); + assert_eq!(res, Ok((&b""[..], a))); + } + + mod pub_crate_named_mod { + named!(pub(crate) tst, tag!("abcd")); + } + + #[test] + fn pub_crate_named_test() { + let a = &b"abcd"[..]; + let res = pub_crate_named_mod::tst(a); + assert_eq!(res, Ok((&b""[..], a))); + } + + #[test] + fn apply_test() { + fn sum2(a: u8, b: u8) -> u8 { + a + b + } + fn sum3(a: u8, b: u8, c: u8) -> u8 { + a + b + c + } + let a = call!(1, sum2, 2); + let b = call!(1, sum3, 2, 3); + + assert_eq!(a, 3); + assert_eq!(b, 6); + } + + #[test] + fn opt() { + named!(opt_abcd<&[u8],Option<&[u8]> >, opt!(tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"bcdefg"[..]; + let c = &b"ab"[..]; + assert_eq!(opt_abcd(a), Ok((&b"ef"[..], Some(&b"abcd"[..])))); + assert_eq!(opt_abcd(b), Ok((&b"bcdefg"[..], None))); + assert_eq!(opt_abcd(c), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + fn opt_res() { + named!(opt_res_abcd<&[u8], Result<&[u8], Err<(&[u8], ErrorKind)>> >, opt_res!(tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"bcdefg"[..]; + let c = &b"ab"[..]; + assert_eq!(opt_res_abcd(a), Ok((&b"ef"[..], Ok(&b"abcd"[..])))); + assert_eq!( + opt_res_abcd(b), + Ok(( + &b"bcdefg"[..], + Err(Err::Error(error_position!(b, ErrorKind::Tag))) + )) + ); + assert_eq!(opt_res_abcd(c), Err(Err::Incomplete(Needed::Size(4)))); + } + + use crate::lib::std::convert::From; + #[derive(Debug, PartialEq)] + pub struct CustomError(&'static str); + impl From<(I, ErrorKind)> for CustomError { + fn from(_: (I, ErrorKind)) -> Self { + CustomError("test") + } + } + + impl ParseError for CustomError { + fn from_error_kind(_: I, _: ErrorKind) -> Self { + CustomError("from_error_kind") + } + + fn append(_: I, _: ErrorKind, _: CustomError) -> Self { + CustomError("append") + } + } + + + #[test] + #[cfg(feature = "alloc")] + fn cond() { + fn f_true(i: &[u8]) -> IResult<&[u8], Option<&[u8]>, CustomError> { + fix_error!(i, CustomError, cond!(true, tag!("abcd"))) + } + + fn f_false(i: &[u8]) -> IResult<&[u8], Option<&[u8]>, CustomError> { + fix_error!(i, CustomError, cond!(false, tag!("abcd"))) + } + + assert_eq!(f_true(&b"abcdef"[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); + assert_eq!(f_true(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(f_true(&b"xxx"[..]), Err(Err::Error(CustomError("test")))); + + assert_eq!(f_false(&b"abcdef"[..]), Ok((&b"abcdef"[..], None))); + assert_eq!(f_false(&b"ab"[..]), Ok((&b"ab"[..], None))); + assert_eq!(f_false(&b"xxx"[..]), Ok((&b"xxx"[..], None))); + } + + #[test] + #[cfg(feature = "alloc")] + fn cond_wrapping() { + // Test that cond!() will wrap a given identifier in the call!() macro. + named!(tag_abcd, tag!("abcd")); + fn f_true(i: &[u8]) -> IResult<&[u8], Option<&[u8]>, CustomError> { + fix_error!(i, CustomError, cond!(true, tag_abcd)) + } + + fn f_false(i: &[u8]) -> IResult<&[u8], Option<&[u8]>, CustomError> { + fix_error!(i, CustomError, cond!(false, tag_abcd)) + } + + assert_eq!(f_true(&b"abcdef"[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); + assert_eq!(f_true(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(f_true(&b"xxx"[..]), Err(Err::Error(CustomError("test")))); + + assert_eq!(f_false(&b"abcdef"[..]), Ok((&b"abcdef"[..], None))); + assert_eq!(f_false(&b"ab"[..]), Ok((&b"ab"[..], None))); + assert_eq!(f_false(&b"xxx"[..]), Ok((&b"xxx"[..], None))); + } + + #[test] + fn peek() { + named!(peek_tag<&[u8],&[u8]>, peek!(tag!("abcd"))); + + assert_eq!(peek_tag(&b"abcdef"[..]), Ok((&b"abcdef"[..], &b"abcd"[..]))); + assert_eq!(peek_tag(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!( + peek_tag(&b"xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn not() { + named!(not_aaa<()>, not!(tag!("aaa"))); + assert_eq!( + not_aaa(&b"aaa"[..]), + Err(Err::Error(error_position!(&b"aaa"[..], ErrorKind::Not))) + ); + assert_eq!(not_aaa(&b"aa"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(not_aaa(&b"abcd"[..]), Ok((&b"abcd"[..], ()))); + } + + #[test] + fn verify() { + named!(test, verify!(take!(5), |slice: &[u8]| slice[0] == b'a')); + assert_eq!(test(&b"bcd"[..]), Err(Err::Incomplete(Needed::Size(5)))); + assert_eq!( + test(&b"bcdefg"[..]), + Err(Err::Error(error_position!( + &b"bcdefg"[..], + ErrorKind::Verify + ))) + ); + assert_eq!(test(&b"abcdefg"[..]), Ok((&b"fg"[..], &b"abcde"[..]))); + } + + #[test] + fn parse_to() { + let res: IResult<_, _, (&str, ErrorKind)> = parse_to!("ab", usize); + + assert_eq!( + res, + Err(Err::Error(error_position!( + "ab", + ErrorKind::ParseTo + ))) + ); + + let res: IResult<_, _, (&str, ErrorKind)> = parse_to!("42", usize); + + assert_eq!(res, Ok(("", 42))); + //assert_eq!(ErrorKind::convert(ErrorKind::ParseTo), ErrorKind::ParseTo::); + } + +} diff --git a/third_party/rust/nom/src/combinator/mod.rs b/third_party/rust/nom/src/combinator/mod.rs new file mode 100644 index 0000000000..933bed834b --- /dev/null +++ b/third_party/rust/nom/src/combinator/mod.rs @@ -0,0 +1,859 @@ +//! general purpose combinators + +#![allow(unused_imports)] + +#[cfg(feature = "alloc")] +use crate::lib::std::boxed::Box; + +#[cfg(feature = "std")] +use crate::lib::std::fmt::Debug; +use crate::internal::*; +use crate::error::ParseError; +use crate::traits::{AsChar, InputIter, InputLength, InputTakeAtPosition, ParseTo}; +use crate::lib::std::ops::{Range, RangeFrom, RangeTo}; +use crate::lib::std::borrow::Borrow; +use crate::traits::{Compare, CompareResult, Offset, Slice}; +use crate::error::ErrorKind; +use crate::lib::std::mem::transmute; + +#[macro_use] +mod macros; + +/// Return the remaining input +/// +/// ```rust +/// # use nom::error::ErrorKind; +/// use nom::combinator::rest; +/// assert_eq!(rest::<_,(_, ErrorKind)>("abc"), Ok(("", "abc"))); +/// assert_eq!(rest::<_,(_, ErrorKind)>(""), Ok(("", ""))); +/// ``` +#[inline] +pub fn rest>(input: T) -> IResult +where + T: Slice> + Slice> + Slice>, + T: InputLength, +{ + Ok((input.slice(input.input_len()..), input)) +} + +/// Return the length of the remaining input +/// +/// ```rust +/// # use nom::error::ErrorKind; +/// use nom::combinator::rest_len; +/// assert_eq!(rest_len::<_,(_, ErrorKind)>("abc"), Ok(("abc", 3))); +/// assert_eq!(rest_len::<_,(_, ErrorKind)>(""), Ok(("", 0))); +/// ``` +#[inline] +pub fn rest_len>(input: T) -> IResult +where + T: Slice> + Slice> + Slice>, + T: InputLength, +{ + let len = input.input_len(); + Ok((input, len)) +} + +/// maps a function on the result of a parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// use nom::combinator::map; +/// # fn main() { +/// +/// let parse = map(digit1, |s: &str| s.len()); +/// +/// // the parser will count how many characters were returned by digit1 +/// assert_eq!(parse("123456"), Ok(("", 6))); +/// +/// // this will fail if digit1 fails +/// assert_eq!(parse("abc"), Err(Err::Error(("abc", ErrorKind::Digit)))); +/// # } +/// ``` +pub fn map, F, G>(first: F, second: G) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(O1) -> O2, +{ + move |input: I| { + let (input, o1) = first(input)?; + Ok((input, second(o1))) + } +} + +#[doc(hidden)] +pub fn mapc, F, G>(input: I, first: F, second: G) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(O1) -> O2, +{ + map(first, second)(input) +} + +/// applies a function returning a Result over the result of a parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// use nom::combinator::map_res; +/// # fn main() { +/// +/// let parse = map_res(digit1, |s: &str| s.parse::()); +/// +/// // the parser will convert the result of digit1 to a number +/// assert_eq!(parse("123"), Ok(("", 123))); +/// +/// // this will fail if digit1 fails +/// assert_eq!(parse("abc"), Err(Err::Error(("abc", ErrorKind::Digit)))); +/// +/// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) +/// assert_eq!(parse("123456"), Err(Err::Error(("123456", ErrorKind::MapRes)))); +/// # } +/// ``` +pub fn map_res, E2, F, G>(first: F, second: G) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(O1) -> Result, +{ + move |input: I| { + let i = input.clone(); + let (input, o1) = first(input)?; + match second(o1) { + Ok(o2) => Ok((input, o2)), + Err(_) => Err(Err::Error(E::from_error_kind(i, ErrorKind::MapRes))), + } + } +} + +#[doc(hidden)] +pub fn map_resc, E2, F, G>(input: I, first: F, second: G) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(O1) -> Result, +{ + map_res(first, second)(input) +} + +/// applies a function returning an Option over the result of a parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// use nom::combinator::map_opt; +/// # fn main() { +/// +/// let parse = map_opt(digit1, |s: &str| s.parse::().ok()); +/// +/// // the parser will convert the result of digit1 to a number +/// assert_eq!(parse("123"), Ok(("", 123))); +/// +/// // this will fail if digit1 fails +/// assert_eq!(parse("abc"), Err(Err::Error(("abc", ErrorKind::Digit)))); +/// +/// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) +/// assert_eq!(parse("123456"), Err(Err::Error(("123456", ErrorKind::MapOpt)))); +/// # } +/// ``` +pub fn map_opt, F, G>(first: F, second: G) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(O1) -> Option, +{ + move |input: I| { + let i = input.clone(); + let (input, o1) = first(input)?; + match second(o1) { + Some(o2) => Ok((input, o2)), + None => Err(Err::Error(E::from_error_kind(i, ErrorKind::MapOpt))), + } + } +} + +#[doc(hidden)] +pub fn map_optc, F, G>(input: I, first: F, second: G) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(O1) -> Option, +{ + map_opt(first, second)(input) +} + +/// applies a parser over the result of another one +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::character::complete::digit1; +/// use nom::bytes::complete::take; +/// use nom::combinator::map_parser; +/// # fn main() { +/// +/// let parse = map_parser(take(5u8), digit1); +/// +/// assert_eq!(parse("12345"), Ok(("", "12345"))); +/// assert_eq!(parse("123ab"), Ok(("", "123"))); +/// assert_eq!(parse("123"), Err(Err::Error(("123", ErrorKind::Eof)))); +/// # } +/// ``` +pub fn map_parser, F, G>(first: F, second: G) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(O1) -> IResult, + O1: InputLength, +{ + move |input: I| { + let (input, o1) = first(input)?; + let (_, o2) = second(o1)?; + Ok((input, o2)) + } +} + +#[doc(hidden)] +pub fn map_parserc, F, G>(input: I, first: F, second: G) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(O1) -> IResult, + O1: InputLength, +{ + map_parser(first, second)(input) +} + +/// creates a new parser from the output of the first parser, then apply that parser over the rest of the input +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::bytes::complete::take; +/// use nom::number::complete::be_u8; +/// use nom::combinator::flat_map; +/// # fn main() { +/// +/// let parse = flat_map(be_u8, take); +/// +/// assert_eq!(parse(&[2, 0, 1, 2][..]), Ok((&[2][..], &[0, 1][..]))); +/// assert_eq!(parse(&[4, 0, 1, 2][..]), Err(Err::Error((&[0, 1, 2][..], ErrorKind::Eof)))); +/// # } +/// ``` +pub fn flat_map, F, G, H>(first: F, second: G) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(O1) -> H, + H: Fn(I) -> IResult +{ + move |input: I| { + let (input, o1) = first(input)?; + second(o1)(input) + } +} + +/// optional parser: will return None if not successful +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::opt; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// fn parser(i: &str) -> IResult<&str, Option<&str>> { +/// opt(alpha1)(i) +/// } +/// +/// assert_eq!(parser("abcd;"), Ok((";", Some("abcd")))); +/// assert_eq!(parser("123;"), Ok(("123;", None))); +/// # } +/// ``` +pub fn opt, F>(f: F) -> impl Fn(I) -> IResult, E> +where + F: Fn(I) -> IResult, +{ + move |input: I| { + let i = input.clone(); + match f(input) { + Ok((i, o)) => Ok((i, Some(o))), + Err(Err::Error(_)) => Ok((i, None)), + Err(e) => Err(e), + } + } +} + +#[doc(hidden)] +pub fn optc, F>(input: I, f: F) -> IResult, E> +where + F: Fn(I) -> IResult, +{ + opt(f)(input) +} + +/// calls the parser if the condition is met +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::cond; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// fn parser(b: bool, i: &str) -> IResult<&str, Option<&str>> { +/// cond(b, alpha1)(i) +/// } +/// +/// assert_eq!(parser(true, "abcd;"), Ok((";", Some("abcd")))); +/// assert_eq!(parser(false, "abcd;"), Ok(("abcd;", None))); +/// assert_eq!(parser(true, "123;"), Err(Err::Error(("123;", ErrorKind::Alpha)))); +/// assert_eq!(parser(false, "123;"), Ok(("123;", None))); +/// # } +/// ``` +pub fn cond, F>(b: bool, f: F) -> impl Fn(I) -> IResult, E> +where + F: Fn(I) -> IResult, +{ + move |input: I| { + if b { + match f(input) { + Ok((i, o)) => Ok((i, Some(o))), + Err(e) => Err(e), + } + } else { + Ok((input, None)) + } + } +} + +#[doc(hidden)] +pub fn condc, F>(input: I, b: bool, f: F) -> IResult, E> +where + F: Fn(I) -> IResult, +{ + cond(b, f)(input) +} + +/// tries to apply its parser without consuming the input +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::peek; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// let parser = peek(alpha1); +/// +/// assert_eq!(parser("abcd;"), Ok(("abcd;", "abcd"))); +/// assert_eq!(parser("123;"), Err(Err::Error(("123;", ErrorKind::Alpha)))); +/// # } +/// ``` +pub fn peek, F>(f: F) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, +{ + move |input: I| { + let i = input.clone(); + match f(input) { + Ok((_, o)) => Ok((i, o)), + Err(e) => Err(e), + } + } +} + +#[doc(hidden)] +pub fn peekc, F>(input: I, f: F) -> IResult +where + F: Fn(I) -> IResult, +{ + peek(f)(input) +} + +/// transforms Incomplete into Error +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::bytes::streaming::take; +/// use nom::combinator::complete; +/// # fn main() { +/// +/// let parser = complete(take(5u8)); +/// +/// assert_eq!(parser("abcdefg"), Ok(("fg", "abcde"))); +/// assert_eq!(parser("abcd"), Err(Err::Error(("abcd", ErrorKind::Complete)))); +/// # } +/// ``` +pub fn complete, F>(f: F) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, +{ + move |input: I| { + let i = input.clone(); + match f(input) { + Err(Err::Incomplete(_)) => { + Err(Err::Error(E::from_error_kind(i, ErrorKind::Complete))) + }, + rest => rest + } + } +} + +#[doc(hidden)] +pub fn completec, F>(input: I, f: F) -> IResult +where + F: Fn(I) -> IResult, +{ + complete(f)(input) +} + +/// succeeds if all the input has been consumed by its child parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::all_consuming; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// let parser = all_consuming(alpha1); +/// +/// assert_eq!(parser("abcd"), Ok(("", "abcd"))); +/// assert_eq!(parser("abcd;"),Err(Err::Error((";", ErrorKind::Eof)))); +/// assert_eq!(parser("123abcd;"),Err(Err::Error(("123abcd;", ErrorKind::Alpha)))); +/// # } +/// ``` +pub fn all_consuming, F>(f: F) -> impl Fn(I) -> IResult +where + I: InputLength, + F: Fn(I) -> IResult, +{ + move |input: I| { + let (input, res) = f(input)?; + if input.input_len() == 0 { + Ok((input, res)) + } else { + Err(Err::Error(E::from_error_kind(input, ErrorKind::Eof))) + } + } +} + +/// returns the result of the child parser if it satisfies a verification function +/// +/// the verification function takes as argument a reference to the output of the +/// parser +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::verify; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// let parser = verify(alpha1, |s: &str| s.len() == 4); +/// +/// assert_eq!(parser("abcd"), Ok(("", "abcd"))); +/// assert_eq!(parser("abcde"), Err(Err::Error(("abcde", ErrorKind::Verify)))); +/// assert_eq!(parser("123abcd;"),Err(Err::Error(("123abcd;", ErrorKind::Alpha)))); +/// # } +/// ``` +pub fn verify, F, G>(first: F, second: G) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(&O2) -> bool, + O1: Borrow, + O2: ?Sized, +{ + move |input: I| { + let i = input.clone(); + let (input, o) = first(input)?; + + if second(o.borrow()) { + Ok((input, o)) + } else { + Err(Err::Error(E::from_error_kind(i, ErrorKind::Verify))) + } + } +} + +#[doc(hidden)] +pub fn verifyc, F, G>(input: I, first: F, second: G) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(&O2) -> bool, + O1: Borrow, + O2: ?Sized, +{ + verify(first, second)(input) +} + +/// returns the provided value if the child parser succeeds +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::value; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// let parser = value(1234, alpha1); +/// +/// assert_eq!(parser("abcd"), Ok(("", 1234))); +/// assert_eq!(parser("123abcd;"), Err(Err::Error(("123abcd;", ErrorKind::Alpha)))); +/// # } +/// ``` +pub fn value, F>(val: O1, parser: F) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, +{ + move |input: I| { + parser(input).map(|(i, _)| (i, val.clone())) + } +} + +#[doc(hidden)] +pub fn valuec, F>(input: I, val: O1, parser: F) -> IResult +where + F: Fn(I) -> IResult, +{ + value(val, parser)(input) +} + +/// succeeds if the child parser returns an error +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::not; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// let parser = not(alpha1); +/// +/// assert_eq!(parser("123"), Ok(("123", ()))); +/// assert_eq!(parser("abcd"), Err(Err::Error(("abcd", ErrorKind::Not)))); +/// # } +/// ``` +pub fn not, F>(parser: F) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, +{ + move |input: I| { + let i = input.clone(); + match parser(input) { + Ok(_) => Err(Err::Error(E::from_error_kind(i, ErrorKind::Not))), + Err(Err::Error(_)) => Ok((i, ())), + Err(e) => Err(e), + } + } +} + +#[doc(hidden)] +pub fn notc, F>(input: I, parser: F) -> IResult +where + F: Fn(I) -> IResult, +{ + not(parser)(input) +} + +/// if the child parser was successful, return the consumed input as produced value +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::recognize; +/// use nom::character::complete::{char, alpha1}; +/// use nom::sequence::separated_pair; +/// # fn main() { +/// +/// let parser = recognize(separated_pair(alpha1, char(','), alpha1)); +/// +/// assert_eq!(parser("abcd,efgh"), Ok(("", "abcd,efgh"))); +/// assert_eq!(parser("abcd;"),Err(Err::Error((";", ErrorKind::Char)))); +/// # } +/// ``` +pub fn recognize>, O, E: ParseError, F>(parser: F) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, +{ + move |input: I| { + let i = input.clone(); + match parser(i) { + Ok((i, _)) => { + let index = input.offset(&i); + Ok((i, input.slice(..index))) + }, + Err(e) => Err(e), + } + } +} + +#[doc(hidden)] +pub fn recognizec>, O, E: ParseError, F>(input: I, parser: F) -> IResult +where + F: Fn(I) -> IResult, +{ + recognize(parser)(input) +} + +/// transforms an error to failure +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,error::ErrorKind, IResult}; +/// use nom::combinator::cut; +/// use nom::character::complete::alpha1; +/// # fn main() { +/// +/// let parser = cut(alpha1); +/// +/// assert_eq!(parser("abcd;"), Ok((";", "abcd"))); +/// assert_eq!(parser("123;"), Err(Err::Failure(("123;", ErrorKind::Alpha)))); +/// # } +/// ``` +pub fn cut>, O, E: ParseError, F>(parser: F) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, +{ + move |input: I| { + let i = input.clone(); + match parser(i) { + Err(Err::Error(e)) => Err(Err::Failure(e)), + rest => rest, + } + } +} + +#[doc(hidden)] +pub fn cutc>, O, E: ParseError, F>(input: I, parser: F) -> IResult +where + F: Fn(I) -> IResult, +{ + cut(parser)(input) +} + +/// creates an iterator from input data and a parser +/// +/// call the iterator's [finish] method to get the remaining input if successful, +/// or the error value if we encountered an error +/// +/// ```rust +/// use nom::{combinator::iterator, IResult, bytes::complete::tag, character::complete::alpha1, sequence::terminated}; +/// use std::collections::HashMap; +/// +/// let data = "abc|defg|hijkl|mnopqr|123"; +/// let mut it = iterator(data, terminated(alpha1, tag("|"))); +/// +/// let parsed = it.map(|v| (v, v.len())).collect::>(); +/// let res: IResult<_,_> = it.finish(); +/// +/// assert_eq!(parsed, [("abc", 3usize), ("defg", 4), ("hijkl", 5), ("mnopqr", 6)].iter().cloned().collect()); +/// assert_eq!(res, Ok(("123", ()))); +/// ``` +pub fn iterator(input: Input, f: F) -> ParserIterator +where + F: Fn(Input) -> IResult, + Error: ParseError { + + ParserIterator { + iterator: f, + input, + state: State::Running, + } +} + +/// main structure associated to the [iterator] function +pub struct ParserIterator { + iterator: F, + input: I, + state: State, +} + +impl ParserIterator { + /// returns the remaining input if parsing was successful, or the error if we encountered an error + pub fn finish(self) -> IResult { + match &self.state { + State::Running | State::Done => Ok((self.input.clone(), ())), + State::Failure(e) => Err(Err::Failure(e.clone())), + State::Incomplete(i) => Err(Err::Incomplete(i.clone())), + } + } +} + +impl<'a, Input ,Output ,Error, F> core::iter::Iterator for &'a mut ParserIterator + where + F: Fn(Input) -> IResult, + Input: Clone +{ + type Item = Output; + + fn next(&mut self) -> Option { + if let State::Running = self.state { + let input = self.input.clone(); + + match (self.iterator)(input) { + Ok((i, o)) => { + self.input = i; + Some(o) + }, + Err(Err::Error(_)) => { + self.state = State::Done; + None + }, + Err(Err::Failure(e)) => { + self.state = State::Failure(e); + None + }, + Err(Err::Incomplete(i)) => { + self.state = State::Incomplete(i); + None + }, + } + } else { + None + } + } +} + +enum State { + Running, + Done, + Failure(E), + Incomplete(Needed), +} + + +#[cfg(test)] +mod tests { + use super::*; + use crate::internal::{Err, IResult, Needed}; + use crate::error::ParseError; + use crate::bytes::complete::take; + use crate::number::complete::be_u8; + + macro_rules! assert_parse( + ($left: expr, $right: expr) => { + let res: $crate::IResult<_, _, (_, ErrorKind)> = $left; + assert_eq!(res, $right); + }; + ); + + /*#[test] + fn t1() { + let v1:Vec = vec![1,2,3]; + let v2:Vec = vec![4,5,6]; + let d = Ok((&v1[..], &v2[..])); + let res = d.flat_map(print); + assert_eq!(res, Ok((&v2[..], ()))); + }*/ + + + /* + #[test] + fn end_of_input() { + let not_over = &b"Hello, world!"[..]; + let is_over = &b""[..]; + named!(eof_test, eof!()); + + let res_not_over = eof_test(not_over); + assert_eq!(res_not_over, Err(Err::Error(error_position!(not_over, ErrorKind::Eof)))); + + let res_over = eof_test(is_over); + assert_eq!(res_over, Ok((is_over, is_over))); + } + */ + + #[test] + fn rest_on_slices() { + let input: &[u8] = &b"Hello, world!"[..]; + let empty: &[u8] = &b""[..]; + assert_parse!(rest(input), Ok((empty, input))); + } + + #[test] + fn rest_on_strs() { + let input: &str = "Hello, world!"; + let empty: &str = ""; + assert_parse!(rest(input), Ok((empty, input))); + } + + #[test] + fn rest_len_on_slices() { + let input: &[u8] = &b"Hello, world!"[..]; + assert_parse!(rest_len(input), Ok((input, input.len()))); + } + + use crate::lib::std::convert::From; + impl From for CustomError { + fn from(_: u32) -> Self { + CustomError + } + } + + impl ParseError for CustomError { + fn from_error_kind(_: I, _: ErrorKind) -> Self { + CustomError + } + + fn append(_: I, _: ErrorKind, _: CustomError) -> Self { + CustomError + } + } + + struct CustomError; + #[allow(dead_code)] + fn custom_error(input: &[u8]) -> IResult<&[u8], &[u8], CustomError> { + //fix_error!(input, CustomError, alphanumeric) + crate::character::streaming::alphanumeric1(input) + } + + #[test] + fn test_flat_map() { + let input: &[u8] = &[3, 100, 101, 102, 103, 104][..]; + assert_parse!(flat_map(be_u8, take)(input), Ok((&[103, 104][..], &[100, 101, 102][..]))); + } + + #[test] + fn test_map_opt() { + let input: &[u8] = &[50][..]; + assert_parse!(map_opt(be_u8, |u| if u < 20 {Some(u)} else {None})(input), Err(Err::Error((&[50][..], ErrorKind::MapOpt)))); + assert_parse!(map_opt(be_u8, |u| if u > 20 {Some(u)} else {None})(input), Ok((&[][..], 50))); + } + + #[test] + fn test_map_parser() { + let input: &[u8] = &[100, 101, 102, 103, 104][..]; + assert_parse!(map_parser(take(4usize), take(2usize))(input), Ok((&[104][..], &[100, 101][..]))); + } + + #[test] + fn test_all_consuming() { + let input: &[u8] = &[100, 101, 102][..]; + assert_parse!(all_consuming(take(2usize))(input), Err(Err::Error((&[102][..], ErrorKind::Eof)))); + assert_parse!(all_consuming(take(3usize))(input), Ok((&[][..], &[100, 101, 102][..]))); + } + + #[test] + #[allow(unused)] + fn test_verify_ref() { + use crate::bytes::complete::take; + + let parser1 = verify(take(3u8), |s: &[u8]| s == &b"abc"[..]); + + assert_eq!(parser1(&b"abcd"[..]), Ok((&b"d"[..], &b"abc"[..]))); + assert_eq!(parser1(&b"defg"[..]), Err(Err::Error((&b"defg"[..], ErrorKind::Verify)))); + + fn parser2(i: &[u8]) -> IResult<&[u8], u32> { + verify(crate::number::streaming::be_u32, |val: &u32| *val < 3)(i) + } + } + + #[test] + #[cfg(feature = "alloc")] + fn test_verify_alloc() { + use crate::bytes::complete::take; + let parser1 = verify(map(take(3u8), |s: &[u8]| s.to_vec()), |s: &[u8]| s == &b"abc"[..]); + + assert_eq!(parser1(&b"abcd"[..]), Ok((&b"d"[..], (&b"abc").to_vec()))); + assert_eq!(parser1(&b"defg"[..]), Err(Err::Error((&b"defg"[..], ErrorKind::Verify)))); + } +} diff --git a/third_party/rust/nom/src/error.rs b/third_party/rust/nom/src/error.rs new file mode 100644 index 0000000000..150516121f --- /dev/null +++ b/third_party/rust/nom/src/error.rs @@ -0,0 +1,776 @@ +//! Error management +//! +//! Parsers are generic over their error type, requiring that it implements +//! the `error::ParseError` trait. + +/// this trait must be implemented by the error type of a nom parser +/// +/// There are already implementations of it for `(Input, ErrorKind)` +/// and `VerboseError`. +/// +/// It provides methods to create an error from some combinators, +/// and combine existing errors in combinators like `alt` +pub trait ParseError: Sized { + /// creates an error from the input position and an [ErrorKind] + fn from_error_kind(input: I, kind: ErrorKind) -> Self; + + /// combines an existing error with a new one created from the input + /// position and an [ErrorKind]. This is useful when backtracking + /// through a parse tree, accumulating error context on the way + fn append(input: I, kind: ErrorKind, other: Self) -> Self; + + /// creates an error from an input position and an expected character + fn from_char(input: I, _: char) -> Self { + Self::from_error_kind(input, ErrorKind::Char) + } + + /// combines two existing error. This function is used to compare errors + /// generated in various branches of [alt] + fn or(self, other: Self) -> Self { + other + } + + /// create a new error from an input position, a static string and an existing error. + /// This is used mainly in the [context] combinator, to add user friendly information + /// to errors when backtracking through a parse tree + fn add_context(_input: I, _ctx: &'static str, other: Self) -> Self { + other + } +} + +impl ParseError for (I, ErrorKind) { + fn from_error_kind(input: I, kind: ErrorKind) -> Self { + (input, kind) + } + + fn append(_: I, _: ErrorKind, other: Self) -> Self { + other + } +} + +impl ParseError for () { + fn from_error_kind(_: I, _: ErrorKind) -> Self {} + + fn append(_: I, _: ErrorKind, _: Self) -> Self {} +} + +/// creates an error from the input position and an [ErrorKind] +pub fn make_error>(input: I, kind: ErrorKind) -> E { + E::from_error_kind(input, kind) +} + +/// combines an existing error with a new one created from the input +/// position and an [ErrorKind]. This is useful when backtracking +/// through a parse tree, accumulating error context on the way +pub fn append_error>(input: I, kind: ErrorKind, other: E) -> E { + E::append(input, kind, other) +} + +/// this error type accumulates errors and their position when backtracking +/// through a parse tree. With some post processing (cf `examples/json.rs`), +/// it can be used to display user friendly error messages +#[cfg(feature = "alloc")] +#[derive(Clone, Debug, PartialEq)] +pub struct VerboseError { + /// list of errors accumulated by `VerboseError`, containing the affected + /// part of input data, and some context + pub errors: crate::lib::std::vec::Vec<(I, VerboseErrorKind)>, +} + +#[cfg(feature = "alloc")] +#[derive(Clone, Debug, PartialEq)] +/// error context for `VerboseError` +pub enum VerboseErrorKind { + /// static string added by the `context` function + Context(&'static str), + /// indicates which character was expected by the `char` function + Char(char), + /// error kind given by various nom parsers + Nom(ErrorKind), +} + +#[cfg(feature = "alloc")] +impl ParseError for VerboseError { + fn from_error_kind(input: I, kind: ErrorKind) -> Self { + VerboseError { + errors: vec![(input, VerboseErrorKind::Nom(kind))], + } + } + + fn append(input: I, kind: ErrorKind, mut other: Self) -> Self { + other.errors.push((input, VerboseErrorKind::Nom(kind))); + other + } + + fn from_char(input: I, c: char) -> Self { + VerboseError { + errors: vec![(input, VerboseErrorKind::Char(c))], + } + } + + fn add_context(input: I, ctx: &'static str, mut other: Self) -> Self { + other.errors.push((input, VerboseErrorKind::Context(ctx))); + other + } +} + +use crate::internal::{Err, IResult}; + +/// create a new error from an input position, a static string and an existing error. +/// This is used mainly in the [context] combinator, to add user friendly information +/// to errors when backtracking through a parse tree +pub fn context, F, O>(context: &'static str, f: F) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, +{ + move |i: I| match f(i.clone()) { + Ok(o) => Ok(o), + Err(Err::Incomplete(i)) => Err(Err::Incomplete(i)), + Err(Err::Error(e)) => Err(Err::Error(E::add_context(i, context, e))), + Err(Err::Failure(e)) => Err(Err::Failure(E::add_context(i, context, e))), + } +} + +/// transforms a `VerboseError` into a trace with input position information +#[cfg(feature = "alloc")] +pub fn convert_error(input: &str, e: VerboseError<&str>) -> crate::lib::std::string::String { + use crate::lib::std::fmt::Write; + use crate::traits::Offset; + + let mut result = crate::lib::std::string::String::new(); + + for (i, (substring, kind)) in e.errors.iter().enumerate() { + let offset = input.offset(substring); + + if input.is_empty() { + match kind { + VerboseErrorKind::Char(c) => write!(&mut result, "{}: expected '{}', got empty input\n\n", i, c), + VerboseErrorKind::Context(s) => write!(&mut result, "{}: in {}, got empty input\n\n", i, s), + VerboseErrorKind::Nom(e) => write!(&mut result, "{}: in {:?}, got empty input\n\n", i, e), + } + } else { + let prefix = &input.as_bytes()[..offset]; + + // Count the number of newlines in the first `offset` bytes of input + let line_number = prefix.iter().filter(|&&b| b == b'\n').count() + 1; + + // Find the line that includes the subslice: + // Find the *last* newline before the substring starts + let line_begin = prefix.iter().rev().position(|&b| b == b'\n').map(|pos| offset - pos).unwrap_or(0); + + // Find the full line after that newline + let line = input[line_begin..].lines().next().unwrap_or(&input[line_begin..]).trim_end(); + + // The (1-indexed) column number is the offset of our substring into that line + let column_number = line.offset(substring) + 1; + + match kind { + VerboseErrorKind::Char(c) => if let Some(actual) = substring.chars().next() { + write!( + &mut result, + "{i}: at line {line_number}:\n\ + {line}\n\ + {caret:>column$}\n\ + expected '{expected}', found {actual}\n\n", + i = i, + line_number = line_number, + line = line, + caret = '^', + column = column_number, + expected = c, + actual = actual, + ) + } else { + write!( + &mut result, + "{i}: at line {line_number}:\n\ + {line}\n\ + {caret:>column$}\n\ + expected '{expected}', got end of input\n\n", + i = i, + line_number = line_number, + line = line, + caret = '^', + column = column_number, + expected = c, + ) + }, + VerboseErrorKind::Context(s) => write!( + &mut result, + "{i}: at line {line_number}, in {context}:\n\ + {line}\n\ + {caret:>column$}\n\n", + i = i, + line_number = line_number, + context = s, + line = line, + caret = '^', + column = column_number, + ), + VerboseErrorKind::Nom(e) => write!( + &mut result, + "{i}: at line {line_number}, in {nom_err:?}:\n\ + {line}\n\ + {caret:>column$}\n\n", + i = i, + line_number = line_number, + nom_err = e, + line = line, + caret = '^', + column = column_number, + ), + } + } + // Because `write!` to a `String` is infallible, this `unwrap` is fine. + .unwrap(); + } + + result +} + +/// indicates which parser returned an error +#[cfg_attr(rustfmt, rustfmt_skip)] +#[derive(Debug,PartialEq,Eq,Hash,Clone,Copy)] +#[allow(deprecated,missing_docs)] +pub enum ErrorKind { + Tag, + MapRes, + MapOpt, + Alt, + IsNot, + IsA, + SeparatedList, + SeparatedNonEmptyList, + Many0, + Many1, + ManyTill, + Count, + TakeUntil, + LengthValue, + TagClosure, + Alpha, + Digit, + HexDigit, + OctDigit, + AlphaNumeric, + Space, + MultiSpace, + LengthValueFn, + Eof, + Switch, + TagBits, + OneOf, + NoneOf, + Char, + CrLf, + RegexpMatch, + RegexpMatches, + RegexpFind, + RegexpCapture, + RegexpCaptures, + TakeWhile1, + Complete, + Fix, + Escaped, + EscapedTransform, + NonEmpty, + ManyMN, + Not, + Permutation, + Verify, + TakeTill1, + TakeWhileMN, + ParseTo, + TooLarge, + Many0Count, + Many1Count, + Float, +} + +#[cfg_attr(rustfmt, rustfmt_skip)] +#[allow(deprecated)] +/// converts an ErrorKind to a number +pub fn error_to_u32(e: &ErrorKind) -> u32 { + match *e { + ErrorKind::Tag => 1, + ErrorKind::MapRes => 2, + ErrorKind::MapOpt => 3, + ErrorKind::Alt => 4, + ErrorKind::IsNot => 5, + ErrorKind::IsA => 6, + ErrorKind::SeparatedList => 7, + ErrorKind::SeparatedNonEmptyList => 8, + ErrorKind::Many1 => 9, + ErrorKind::Count => 10, + ErrorKind::TakeUntil => 12, + ErrorKind::LengthValue => 15, + ErrorKind::TagClosure => 16, + ErrorKind::Alpha => 17, + ErrorKind::Digit => 18, + ErrorKind::AlphaNumeric => 19, + ErrorKind::Space => 20, + ErrorKind::MultiSpace => 21, + ErrorKind::LengthValueFn => 22, + ErrorKind::Eof => 23, + ErrorKind::Switch => 27, + ErrorKind::TagBits => 28, + ErrorKind::OneOf => 29, + ErrorKind::NoneOf => 30, + ErrorKind::Char => 40, + ErrorKind::CrLf => 41, + ErrorKind::RegexpMatch => 42, + ErrorKind::RegexpMatches => 43, + ErrorKind::RegexpFind => 44, + ErrorKind::RegexpCapture => 45, + ErrorKind::RegexpCaptures => 46, + ErrorKind::TakeWhile1 => 47, + ErrorKind::Complete => 48, + ErrorKind::Fix => 49, + ErrorKind::Escaped => 50, + ErrorKind::EscapedTransform => 51, + ErrorKind::NonEmpty => 56, + ErrorKind::ManyMN => 57, + ErrorKind::HexDigit => 59, + ErrorKind::OctDigit => 61, + ErrorKind::Many0 => 62, + ErrorKind::Not => 63, + ErrorKind::Permutation => 64, + ErrorKind::ManyTill => 65, + ErrorKind::Verify => 66, + ErrorKind::TakeTill1 => 67, + ErrorKind::TakeWhileMN => 69, + ErrorKind::ParseTo => 70, + ErrorKind::TooLarge => 71, + ErrorKind::Many0Count => 72, + ErrorKind::Many1Count => 73, + ErrorKind::Float => 74, + } +} + +impl ErrorKind { + #[cfg_attr(rustfmt, rustfmt_skip)] + #[allow(deprecated)] + /// converts an ErrorKind to a text description + pub fn description(&self) -> &str { + match *self { + ErrorKind::Tag => "Tag", + ErrorKind::MapRes => "Map on Result", + ErrorKind::MapOpt => "Map on Option", + ErrorKind::Alt => "Alternative", + ErrorKind::IsNot => "IsNot", + ErrorKind::IsA => "IsA", + ErrorKind::SeparatedList => "Separated list", + ErrorKind::SeparatedNonEmptyList => "Separated non empty list", + ErrorKind::Many0 => "Many0", + ErrorKind::Many1 => "Many1", + ErrorKind::Count => "Count", + ErrorKind::TakeUntil => "Take until", + ErrorKind::LengthValue => "Length followed by value", + ErrorKind::TagClosure => "Tag closure", + ErrorKind::Alpha => "Alphabetic", + ErrorKind::Digit => "Digit", + ErrorKind::AlphaNumeric => "AlphaNumeric", + ErrorKind::Space => "Space", + ErrorKind::MultiSpace => "Multiple spaces", + ErrorKind::LengthValueFn => "LengthValueFn", + ErrorKind::Eof => "End of file", + ErrorKind::Switch => "Switch", + ErrorKind::TagBits => "Tag on bitstream", + ErrorKind::OneOf => "OneOf", + ErrorKind::NoneOf => "NoneOf", + ErrorKind::Char => "Char", + ErrorKind::CrLf => "CrLf", + ErrorKind::RegexpMatch => "RegexpMatch", + ErrorKind::RegexpMatches => "RegexpMatches", + ErrorKind::RegexpFind => "RegexpFind", + ErrorKind::RegexpCapture => "RegexpCapture", + ErrorKind::RegexpCaptures => "RegexpCaptures", + ErrorKind::TakeWhile1 => "TakeWhile1", + ErrorKind::Complete => "Complete", + ErrorKind::Fix => "Fix", + ErrorKind::Escaped => "Escaped", + ErrorKind::EscapedTransform => "EscapedTransform", + ErrorKind::NonEmpty => "NonEmpty", + ErrorKind::ManyMN => "Many(m, n)", + ErrorKind::HexDigit => "Hexadecimal Digit", + ErrorKind::OctDigit => "Octal digit", + ErrorKind::Not => "Negation", + ErrorKind::Permutation => "Permutation", + ErrorKind::ManyTill => "ManyTill", + ErrorKind::Verify => "predicate verification", + ErrorKind::TakeTill1 => "TakeTill1", + ErrorKind::TakeWhileMN => "TakeWhileMN", + ErrorKind::ParseTo => "Parse string to the specified type", + ErrorKind::TooLarge => "Needed data size is too large", + ErrorKind::Many0Count => "Count occurrence of >=0 patterns", + ErrorKind::Many1Count => "Count occurrence of >=1 patterns", + ErrorKind::Float => "Float", + } + } +} + +/// creates a parse error from a `nom::ErrorKind` +/// and the position in the input +#[allow(unused_variables)] +#[macro_export(local_inner_macros)] +macro_rules! error_position( + ($input:expr, $code:expr) => ({ + $crate::error::make_error($input, $code) + }); +); + +/// creates a parse error from a `nom::ErrorKind`, +/// the position in the input and the next error in +/// the parsing tree. +#[allow(unused_variables)] +#[macro_export(local_inner_macros)] +macro_rules! error_node_position( + ($input:expr, $code:expr, $next:expr) => ({ + $crate::error::append_error($input, $code, $next) + }); +); + +/* + +#[cfg(feature = "std")] +use $crate::lib::std::any::Any; +#[cfg(feature = "std")] +use $crate::lib::std::{error,fmt}; +#[cfg(feature = "std")] +impl error::Error for Err { + fn description(&self) -> &str { + self.description() + } +} + +#[cfg(feature = "std")] +impl fmt::Display for Err { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.description()) + } +} +*/ + +//FIXME: error rewrite +/// translate parser result from IResult to IResult with a custom type +/// +/// ``` +/// # //FIXME +/// # #[macro_use] extern crate nom; +/// # use nom::IResult; +/// # use std::convert::From; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// # /* +/// # // will add a Custom(42) error to the error chain +/// # named!(err_test, add_return_error!(ErrorKind::Custom(42u32), tag!("abcd"))); +/// # +/// # #[derive(Debug,Clone,PartialEq)] +/// # pub struct ErrorStr(String); +/// # +/// # // Convert to IResult<&[u8], &[u8], ErrorStr> +/// # impl From for ErrorStr { +/// # fn from(i: u32) -> Self { +/// # ErrorStr(format!("custom error code: {}", i)) +/// # } +/// # } +/// # +/// # named!(parser<&[u8], &[u8], ErrorStr>, +/// # fix_error!(ErrorStr, err_test) +/// # ); +/// # +/// # let a = &b"efghblah"[..]; +/// # assert_eq!(parser(a), Err(Err::Error(Context::Code(a, ErrorKind::Custom(ErrorStr("custom error code: 42".to_string())))))); +/// # */ +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! fix_error ( + ($i:expr, $t:ty, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + match $submac!($i, $($args)*) { + Ok((i,o)) => Ok((i,o)), + Err(e) => { + let e2 = match e { + Err::Error(err) => { + Err::Error(err.into()) + }, + Err::Failure(err) => { + Err::Failure(err.into()) + }, + Err::Incomplete(e) => Err::Incomplete(e), + }; + Err(e2) + } + } + } + ); + ($i:expr, $t:ty, $f:expr) => ( + fix_error!($i, $t, call!($f)); + ); +); + +/// `flat_map!(R -> IResult, S -> IResult) => R -> IResult` +/// +/// combines a parser R -> IResult and +/// a parser S -> IResult to return another +/// parser R -> IResult +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind}; +/// use nom::number::complete::recognize_float; +/// +/// named!(parser<&str, f64>, flat_map!(recognize_float, parse_to!(f64))); +/// +/// assert_eq!(parser("123.45;"), Ok((";", 123.45))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! flat_map( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + flat_map!(__impl $i, $submac!($($args)*), $submac2!($($args2)*)); + ); + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + flat_map!(__impl $i, $submac!($($args)*), call!($g)); + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + flat_map!(__impl $i, call!($f), $submac!($($args)*)); + ); + ($i:expr, $f:expr, $g:expr) => ( + flat_map!(__impl $i, call!($f), call!($g)); + ); + (__impl $i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + $crate::combinator::map_parserc($i, move |i| {$submac!(i, $($args)*)}, move |i| {$submac2!(i, $($args2)*)}) + ); +); + +#[cfg(test)] +#[cfg(feature = "alloc")] +mod tests { + use super::*; + use crate::character::complete::char; + + #[test] + fn convert_error_panic() { + let input = ""; + + let result: IResult<_, _, VerboseError<&str>> = char('x')(input); + } +} + +/* +#[cfg(feature = "alloc")] +use lib::std::{vec::Vec, collections::HashMap}; + +#[cfg(feature = "std")] +use lib::std::hash::Hash; + +#[cfg(feature = "std")] +pub fn add_error_pattern<'a, I: Clone + Hash + Eq, O, E: Clone + Hash + Eq>( + h: &mut HashMap, &'a str>, + e: VerboseError, + message: &'a str, +) -> bool { + h.insert(e, message); + true +} + +pub fn slice_to_offsets(input: &[u8], s: &[u8]) -> (usize, usize) { + let start = input.as_ptr(); + let off1 = s.as_ptr() as usize - start as usize; + let off2 = off1 + s.len(); + (off1, off2) +} + +#[cfg(feature = "std")] +pub fn prepare_errors(input: &[u8], e: VerboseError<&[u8]>) -> Option> { + let mut v: Vec<(ErrorKind, usize, usize)> = Vec::new(); + + for (p, kind) in e.errors.drain(..) { + let (o1, o2) = slice_to_offsets(input, p); + v.push((kind, o1, o2)); + } + + v.reverse(); + Some(v) +} + +#[cfg(feature = "std")] +pub fn print_error(input: &[u8], res: VerboseError<&[u8]>) { + if let Some(v) = prepare_errors(input, res) { + let colors = generate_colors(&v); + println!("parser codes: {}", print_codes(&colors, &HashMap::new())); + println!("{}", print_offsets(input, 0, &v)); + } else { + println!("not an error"); + } +} + +#[cfg(feature = "std")] +pub fn generate_colors(v: &[(ErrorKind, usize, usize)]) -> HashMap { + let mut h: HashMap = HashMap::new(); + let mut color = 0; + + for &(ref c, _, _) in v.iter() { + h.insert(error_to_u32(c), color + 31); + color = color + 1 % 7; + } + + h +} + +pub fn code_from_offset(v: &[(ErrorKind, usize, usize)], offset: usize) -> Option { + let mut acc: Option<(u32, usize, usize)> = None; + for &(ref ek, s, e) in v.iter() { + let c = error_to_u32(ek); + if s <= offset && offset <= e { + if let Some((_, start, end)) = acc { + if start <= s && e <= end { + acc = Some((c, s, e)); + } + } else { + acc = Some((c, s, e)); + } + } + } + if let Some((code, _, _)) = acc { + return Some(code); + } else { + return None; + } +} + +#[cfg(feature = "alloc")] +pub fn reset_color(v: &mut Vec) { + v.push(0x1B); + v.push(b'['); + v.push(0); + v.push(b'm'); +} + +#[cfg(feature = "alloc")] +pub fn write_color(v: &mut Vec, color: u8) { + v.push(0x1B); + v.push(b'['); + v.push(1); + v.push(b';'); + let s = color.to_string(); + let bytes = s.as_bytes(); + v.extend(bytes.iter().cloned()); + v.push(b'm'); +} + +#[cfg(feature = "std")] +#[cfg_attr(feature = "cargo-clippy", allow(implicit_hasher))] +pub fn print_codes(colors: &HashMap, names: &HashMap) -> String { + let mut v = Vec::new(); + for (code, &color) in colors { + if let Some(&s) = names.get(code) { + let bytes = s.as_bytes(); + write_color(&mut v, color); + v.extend(bytes.iter().cloned()); + } else { + let s = code.to_string(); + let bytes = s.as_bytes(); + write_color(&mut v, color); + v.extend(bytes.iter().cloned()); + } + reset_color(&mut v); + v.push(b' '); + } + reset_color(&mut v); + + String::from_utf8_lossy(&v[..]).into_owned() +} + +#[cfg(feature = "std")] +pub fn print_offsets(input: &[u8], from: usize, offsets: &[(ErrorKind, usize, usize)]) -> String { + let mut v = Vec::with_capacity(input.len() * 3); + let mut i = from; + let chunk_size = 8; + let mut current_code: Option = None; + let mut current_code2: Option = None; + + let colors = generate_colors(&offsets); + + for chunk in input.chunks(chunk_size) { + let s = format!("{:08x}", i); + for &ch in s.as_bytes().iter() { + v.push(ch); + } + v.push(b'\t'); + + let mut k = i; + let mut l = i; + for &byte in chunk { + if let Some(code) = code_from_offset(&offsets, k) { + if let Some(current) = current_code { + if current != code { + reset_color(&mut v); + current_code = Some(code); + if let Some(&color) = colors.get(&code) { + write_color(&mut v, color); + } + } + } else { + current_code = Some(code); + if let Some(&color) = colors.get(&code) { + write_color(&mut v, color); + } + } + } + v.push(CHARS[(byte >> 4) as usize]); + v.push(CHARS[(byte & 0xf) as usize]); + v.push(b' '); + k = k + 1; + } + + reset_color(&mut v); + + if chunk_size > chunk.len() { + for _ in 0..(chunk_size - chunk.len()) { + v.push(b' '); + v.push(b' '); + v.push(b' '); + } + } + v.push(b'\t'); + + for &byte in chunk { + if let Some(code) = code_from_offset(&offsets, l) { + if let Some(current) = current_code2 { + if current != code { + reset_color(&mut v); + current_code2 = Some(code); + if let Some(&color) = colors.get(&code) { + write_color(&mut v, color); + } + } + } else { + current_code2 = Some(code); + if let Some(&color) = colors.get(&code) { + write_color(&mut v, color); + } + } + } + if (byte >= 32 && byte <= 126) || byte >= 128 { + v.push(byte); + } else { + v.push(b'.'); + } + l = l + 1; + } + reset_color(&mut v); + + v.push(b'\n'); + i = i + chunk_size; + } + + String::from_utf8_lossy(&v[..]).into_owned() +} +*/ diff --git a/third_party/rust/nom/src/internal.rs b/third_party/rust/nom/src/internal.rs new file mode 100644 index 0000000000..401f98a906 --- /dev/null +++ b/third_party/rust/nom/src/internal.rs @@ -0,0 +1,185 @@ +//! Basic types to build the parsers + +use self::Needed::*; +use crate::error::ErrorKind; + +/// Holds the result of parsing functions +/// +/// It depends on I, the input type, O, the output type, and E, the error type (by default u32) +/// +/// The `Ok` side is a pair containing the remainder of the input (the part of the data that +/// was not parsed) and the produced value. The `Err` side contains an instance of `nom::Err`. +/// +pub type IResult = Result<(I, O), Err>; + +/// Contains information on needed data if a parser returned `Incomplete` +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Needed { + /// needs more data, but we do not know how much + Unknown, + /// contains the required data size + Size(usize), +} + +impl Needed { + /// indicates if we know how many bytes we need + pub fn is_known(&self) -> bool { + *self != Unknown + } + + /// Maps a `Needed` to `Needed` by applying a function to a contained `Size` value. + #[inline] + pub fn map usize>(self, f: F) -> Needed { + match self { + Unknown => Unknown, + Size(n) => Size(f(n)), + } + } +} + +/// The `Err` enum indicates the parser was not successful +/// +/// It has three cases: +/// +/// * `Incomplete` indicates that more data is needed to decide. The `Needed` enum +/// can contain how many additional bytes are necessary. If you are sure your parser +/// is working on full data, you can wrap your parser with the `complete` combinator +/// to transform that case in `Error` +/// * `Error` means some parser did not succeed, but another one might (as an example, +/// when testing different branches of an `alt` combinator) +/// * `Failure` indicates an unrecoverable error. As an example, if you recognize a prefix +/// to decide on the next parser to apply, and that parser fails, you know there's no need +/// to try other parsers, you were already in the right branch, so the data is invalid +/// +#[derive(Debug, Clone, PartialEq)] +pub enum Err { + /// There was not enough data + Incomplete(Needed), + /// The parser had an error (recoverable) + Error(E), + /// The parser had an unrecoverable error: we got to the right + /// branch and we know other branches won't work, so backtrack + /// as fast as possible + Failure(E), +} + +impl Err { + /// tests if the result is Incomplete + pub fn is_incomplete(&self) -> bool { + if let Err::Incomplete(_) = self { + true + } else { + false + } + } + + /// Applies the given function to the inner error + pub fn map(self, f: F) -> Err + 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)), + } + } + + /// automatically converts between errors if the underlying type supports it + pub fn convert(e: Err) -> Self + where E: From + { + e.map(Into::into) + } +} + +impl Err<(T, ErrorKind)> { + /// maps `Err<(T, ErrorKind)>` to `Err<(U, ErrorKind)>` with the given F: T -> U + pub fn map_input(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)), + } + } +} + +#[cfg(feature = "std")] +impl Err<(&[u8], ErrorKind)> { + /// Obtaining ownership + pub fn to_owned(self) -> Err<(Vec, ErrorKind)> { + self.map_input(ToOwned::to_owned) + } +} + +#[cfg(feature = "std")] +impl Err<(&str, ErrorKind)> { + /// automatically converts between errors if the underlying type supports it + pub fn to_owned(self) -> Err<(String, ErrorKind)> { + self.map_input(ToOwned::to_owned) + } +} + +impl Eq for Err {} + +#[cfg(feature = "std")] +use std::fmt; + +#[cfg(feature = "std")] +impl fmt::Display for Err +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 Error for Err +where + E: fmt::Debug, +{ + fn source(&self) -> Option<&(dyn Error + 'static)> { + None // no underlying error + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::error::ErrorKind; + + #[doc(hidden)] + #[macro_export] + macro_rules! assert_size ( + ($t:ty, $sz:expr) => ( + assert_eq!(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, 16); + assert_size!(Err, 24); + assert_size!(ErrorKind, 1); + } + + #[test] + fn err_map_test() { + let e = Err::Error(1); + assert_eq!(e.map(|v| v + 1), Err::Error(2)); + } + +} diff --git a/third_party/rust/nom/src/lib.rs b/third_party/rust/nom/src/lib.rs new file mode 100644 index 0000000000..c37410d7d1 --- /dev/null +++ b/third_party/rust/nom/src/lib.rs @@ -0,0 +1,513 @@ +//! # nom, eating data byte by byte +//! +//! nom is a parser combinator library with a focus on safe parsing, +//! streaming patterns, and as much as possible zero copy. +//! +//! ## Example +//! +//! ```rust +//! extern crate nom; +//! +//! use nom::{ +//! IResult, +//! bytes::complete::{tag, take_while_m_n}, +//! combinator::map_res, +//! sequence::tuple}; +//! +//! #[derive(Debug,PartialEq)] +//! pub struct Color { +//! pub red: u8, +//! pub green: u8, +//! pub blue: u8, +//! } +//! +//! fn from_hex(input: &str) -> Result { +//! u8::from_str_radix(input, 16) +//! } +//! +//! fn is_hex_digit(c: char) -> bool { +//! c.is_digit(16) +//! } +//! +//! fn hex_primary(input: &str) -> IResult<&str, u8> { +//! map_res( +//! take_while_m_n(2, 2, is_hex_digit), +//! from_hex +//! )(input) +//! } +//! +//! fn hex_color(input: &str) -> IResult<&str, Color> { +//! let (input, _) = tag("#")(input)?; +//! let (input, (red, green, blue)) = tuple((hex_primary, hex_primary, hex_primary))(input)?; +//! +//! Ok((input, Color { red, green, blue })) +//! } +//! +//! fn main() { +//! assert_eq!(hex_color("#2F14DF"), Ok(("", Color { +//! red: 47, +//! green: 20, +//! blue: 223, +//! }))); +//! } +//! ``` +//! +//! The code is available on [Github](https://github.com/Geal/nom) +//! +//! There are a few [guides](https://github.com/Geal/nom/tree/master/doc) with more details +//! about [the design of nom macros](https://github.com/Geal/nom/blob/master/doc/how_nom_macros_work.md), +//! [how to write parsers](https://github.com/Geal/nom/blob/master/doc/making_a_new_parser_from_scratch.md), +//! or the [error management system](https://github.com/Geal/nom/blob/master/doc/error_management.md). +//! +//! **Looking for a specific combinator? Read the +//! ["choose a combinator" guide](https://github.com/Geal/nom/blob/master/doc/choosing_a_combinator.md)** +//! +//! If you are upgrading to nom 5.0, please read the +//! [migration document](https://github.com/Geal/nom/blob/master/doc/upgrading_to_nom_5.md). +//! +//! See also the [FAQ](https://github.com/Geal/nom/blob/master/doc/FAQ.md). +//! +//! ## Parser combinators +//! +//! Parser combinators are an approach to parsers that is very different from +//! software like [lex](https://en.wikipedia.org/wiki/Lex_(software)) and +//! [yacc](https://en.wikipedia.org/wiki/Yacc). Instead of writing the grammar +//! in a separate syntax and generating the corresponding code, you use very small +//! functions with very specific purposes, like "take 5 bytes", or "recognize the +//! word 'HTTP'", and assemble them in meaningful patterns like "recognize +//! 'HTTP', then a space, then a version". +//! The resulting code is small, and looks like the grammar you would have +//! written with other parser approaches. +//! +//! This gives us a few advantages: +//! +//! - the parsers are small and easy to write +//! - the parsers components are easy to reuse (if they're general enough, please add them to nom!) +//! - the parsers components are easy to test separately (unit tests and property-based tests) +//! - the parser combination code looks close to the grammar you would have written +//! - you can build partial parsers, specific to the data you need at the moment, and ignore the rest +//! +//! Here is an example of one such parser, to recognize text between parentheses: +//! +//! ```rust +//! use nom::{ +//! IResult, +//! sequence::delimited, +//! // see the "streaming/complete" paragraph lower for an explanation of these submodules +//! character::complete::char, +//! bytes::complete::is_not +//! }; +//! +//! fn parens(input: &str) -> IResult<&str, &str> { +//! delimited(char('('), is_not(")"), char(')'))(input) +//! } +//! ``` +//! +//! It defines a function named `parens` which will recognize a sequence of the +//! character `(`, the longest byte array not containing `)`, then the character +//! `)`, and will return the byte array in the middle. +//! +//! Here is another parser, written without using nom's combinators this time: +//! +//! ```rust +//! #[macro_use] +//! extern crate nom; +//! +//! use nom::{IResult, Err, Needed}; +//! +//! # fn main() { +//! fn take4(i: &[u8]) -> IResult<&[u8], &[u8]>{ +//! if i.len() < 4 { +//! Err(Err::Incomplete(Needed::Size(4))) +//! } else { +//! Ok((&i[4..], &i[0..4])) +//! } +//! } +//! # } +//! ``` +//! +//! This function takes a byte array as input, and tries to consume 4 bytes. +//! Writing all the parsers manually, like this, is dangerous, despite Rust's +//! safety features. There are still a lot of mistakes one can make. That's why +//! nom provides a list of function and macros to help in developing parsers. +//! +//! With functions, you would write it like this: +//! +//! ```rust +//! use nom::{IResult, bytes::streaming::take}; +//! fn take4(input: &str) -> IResult<&str, &str> { +//! take(4u8)(input) +//! } +//! ``` +//! +//! With macros, you would write it like this: +//! +//! ```rust +//! #[macro_use] +//! extern crate nom; +//! +//! # fn main() { +//! named!(take4, take!(4)); +//! # } +//! ``` +//! +//! nom has used macros for combinators from versions 1 to 4, and from version +//! 5, it proposes new combinators as functions, but still allows the macros style +//! (macros have been rewritten to use the functions under the hood). +//! For new parsers, we recommend using the functions instead of macros, since +//! rustc messages will be much easier to understand. +//! +//! +//! A parser in nom is a function which, for an input type `I`, an output type `O` +//! and an optional error type `E`, will have the following signature: +//! +//! ```rust,ignore +//! fn parser(input: I) -> IResult; +//! ``` +//! +//! Or like this, if you don't want to specify a custom error type (it will be `u32` by default): +//! +//! ```rust,ignore +//! fn parser(input: I) -> IResult; +//! ``` +//! +//! `IResult` is an alias for the `Result` type: +//! +//! ```rust +//! use nom::{Needed, error::ErrorKind}; +//! +//! type IResult = Result<(I, O), Err>; +//! +//! enum Err { +//! Incomplete(Needed), +//! Error(E), +//! Failure(E), +//! } +//! ``` +//! +//! It can have the following values: +//! +//! - a correct result `Ok((I,O))` with the first element being the remaining of the input (not parsed yet), and the second the output value; +//! - an error `Err(Err::Error(c))` with `c` an error that can be built from the input position and a parser specific error +//! - an error `Err(Err::Incomplete(Needed))` indicating that more input is necessary. `Needed` can indicate how much data is needed +//! - an error `Err(Err::Failure(c))`. It works like the `Error` case, except it indicates an unrecoverable error: we cannot backtrack and test another parser +//! +//! Please refer to the ["choose a combinator" guide](https://github.com/Geal/nom/blob/master/doc/choosing_a_combinator.md) for an exhaustive list of parsers. +//! See also the rest of the documentation [here](https://github.com/Geal/nom/blob/master/doc). +//! . +//! +//! ## Making new parsers with function combinators +//! +//! nom is based on functions that generate parsers, with a signature like +//! this: `(arguments) -> impl Fn(Input) -> IResult`. +//! The arguments of a combinator can be direct values (like `take` which uses +//! a number of bytes or character as argument) or even other parsers (like +//! `delimited` which takes as argument 3 parsers, and returns the result of +//! the second one if all are successful). +//! +//! Here are some examples: +//! +//! ```rust +//! use nom::IResult; +//! use nom::bytes::complete::{tag, take}; +//! fn abcd_parser(i: &str) -> IResult<&str, &str> { +//! tag("abcd")(i) // will consume bytes if the input begins with "abcd" +//! } +//! +//! fn take_10(i: &[u8]) -> IResult<&[u8], &[u8]> { +//! take(10u8)(i) // will consume and return 10 bytes of input +//! } +//! ``` +//! +//! ## Combining parsers +//! +//! There are higher level patterns, like the **`alt`** combinator, which +//! provides a choice between multiple parsers. If one branch fails, it tries +//! the next, and returns the result of the first parser that succeeds: +//! +//! ```rust +//! use nom::IResult; +//! use nom::branch::alt; +//! use nom::bytes::complete::tag; +//! +//! let alt_tags = alt((tag("abcd"), tag("efgh"))); +//! +//! assert_eq!(alt_tags(&b"abcdxxx"[..]), Ok((&b"xxx"[..], &b"abcd"[..]))); +//! assert_eq!(alt_tags(&b"efghxxx"[..]), Ok((&b"xxx"[..], &b"efgh"[..]))); +//! assert_eq!(alt_tags(&b"ijklxxx"[..]), Err(nom::Err::Error((&b"ijklxxx"[..], nom::error::ErrorKind::Tag)))); +//! ``` +//! +//! The **`opt`** combinator makes a parser optional. If the child parser returns +//! an error, **`opt`** will still succeed and return None: +//! +//! ```rust +//! use nom::{IResult, combinator::opt, bytes::complete::tag}; +//! fn abcd_opt(i: &[u8]) -> IResult<&[u8], Option<&[u8]>> { +//! opt(tag("abcd"))(i) +//! } +//! +//! assert_eq!(abcd_opt(&b"abcdxxx"[..]), Ok((&b"xxx"[..], Some(&b"abcd"[..])))); +//! assert_eq!(abcd_opt(&b"efghxxx"[..]), Ok((&b"efghxxx"[..], None))); +//! ``` +//! +//! **`many0`** applies a parser 0 or more times, and returns a vector of the aggregated results: +//! +//! ```rust +//! # #[macro_use] extern crate nom; +//! # #[cfg(feature = "alloc")] +//! # fn main() { +//! use nom::{IResult, multi::many0, bytes::complete::tag}; +//! use std::str; +//! +//! fn multi(i: &str) -> IResult<&str, Vec<&str>> { +//! many0(tag("abcd"))(i) +//! } +//! +//! let a = "abcdef"; +//! let b = "abcdabcdef"; +//! let c = "azerty"; +//! assert_eq!(multi(a), Ok(("ef", vec!["abcd"]))); +//! assert_eq!(multi(b), Ok(("ef", vec!["abcd", "abcd"]))); +//! assert_eq!(multi(c), Ok(("azerty", Vec::new()))); +//! # } +//! # #[cfg(not(feature = "alloc"))] +//! # fn main() {} +//! ``` +//! +//! Here are some basic combining macros available: +//! +//! - **`opt`**: will make the parser optional (if it returns the `O` type, the new parser returns `Option`) +//! - **`many0`**: will apply the parser 0 or more times (if it returns the `O` type, the new parser returns `Vec`) +//! - **`many1`**: will apply the parser 1 or more times +//! +//! There are more complex (and more useful) parsers like `tuple!`, which is +//! used to apply a series of parsers then assemble their results. +//! +//! Example with `tuple`: +//! +//! ```rust +//! # #[macro_use] extern crate nom; +//! # fn main() { +//! use nom::{error::ErrorKind, Needed, +//! number::streaming::be_u16, +//! bytes::streaming::{tag, take}, +//! sequence::tuple}; +//! +//! let tpl = tuple((be_u16, take(3u8), tag("fg"))); +//! +//! assert_eq!( +//! tpl(&b"abcdefgh"[..]), +//! Ok(( +//! &b"h"[..], +//! (0x6162u16, &b"cde"[..], &b"fg"[..]) +//! )) +//! ); +//! assert_eq!(tpl(&b"abcde"[..]), Err(nom::Err::Incomplete(Needed::Size(2)))); +//! let input = &b"abcdejk"[..]; +//! assert_eq!(tpl(input), Err(nom::Err::Error((&input[5..], ErrorKind::Tag)))); +//! # } +//! ``` +//! +//! But you can also use a sequence of combinators written in imperative style, +//! thanks to the `?` operator: +//! +//! ```rust +//! # #[macro_use] extern crate nom; +//! # fn main() { +//! use nom::{IResult, bytes::complete::tag}; +//! +//! #[derive(Debug, PartialEq)] +//! struct A { +//! a: u8, +//! b: u8 +//! } +//! +//! fn ret_int1(i:&[u8]) -> IResult<&[u8], u8> { Ok((i,1)) } +//! fn ret_int2(i:&[u8]) -> IResult<&[u8], u8> { Ok((i,2)) } +//! +//! fn f(i: &[u8]) -> IResult<&[u8], A> { +//! // if successful, the parser returns `Ok((remaining_input, output_value))` that we can destructure +//! let (i, _) = tag("abcd")(i)?; +//! let (i, a) = ret_int1(i)?; +//! let (i, _) = tag("efgh")(i)?; +//! let (i, b) = ret_int2(i)?; +//! +//! Ok((i, A { a, b })) +//! } +//! +//! let r = f(b"abcdefghX"); +//! assert_eq!(r, Ok((&b"X"[..], A{a: 1, b: 2}))); +//! # } +//! ``` +//! +//! ## Streaming / Complete +//! +//! Some of nom's modules have `streaming` or `complete` submodules. They hold +//! different variants of the same combinators. +//! +//! A streaming parser assumes that we might not have all of the input data. +//! This can happen with some network protocol or large file parsers, where the +//! input buffer can be full and need to be resized or refilled. +//! +//! A complete parser assumes that we already have all of the input data. +//! This will be the common case with small files that can be read entirely to +//! memory. +//! +//! Here is how it works in practice: +//! +//! ```rust +//! use nom::{IResult, Err, Needed, error::ErrorKind, bytes, character}; +//! +//! fn take_streaming(i: &[u8]) -> IResult<&[u8], &[u8]> { +//! bytes::streaming::take(4u8)(i) +//! } +//! +//! fn take_complete(i: &[u8]) -> IResult<&[u8], &[u8]> { +//! bytes::complete::take(4u8)(i) +//! } +//! +//! // both parsers will take 4 bytes as expected +//! assert_eq!(take_streaming(&b"abcde"[..]), Ok((&b"e"[..], &b"abcd"[..]))); +//! assert_eq!(take_complete(&b"abcde"[..]), Ok((&b"e"[..], &b"abcd"[..]))); +//! +//! // if the input is smaller than 4 bytes, the streaming parser +//! // will return `Incomplete` to indicate that we need more data +//! assert_eq!(take_streaming(&b"abc"[..]), Err(Err::Incomplete(Needed::Size(4)))); +//! +//! // but the complete parser will return an error +//! assert_eq!(take_complete(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); +//! +//! // the alpha0 function recognizes 0 or more alphabetic characters +//! fn alpha0_streaming(i: &str) -> IResult<&str, &str> { +//! character::streaming::alpha0(i) +//! } +//! +//! fn alpha0_complete(i: &str) -> IResult<&str, &str> { +//! character::complete::alpha0(i) +//! } +//! +//! // if there's a clear limit to the recognized characters, both parsers work the same way +//! assert_eq!(alpha0_streaming("abcd;"), Ok((";", "abcd"))); +//! assert_eq!(alpha0_complete("abcd;"), Ok((";", "abcd"))); +//! +//! // but when there's no limit, the streaming version returns `Incomplete`, because it cannot +//! // know if more input data should be recognized. The whole input could be "abcd;", or +//! // "abcde;" +//! assert_eq!(alpha0_streaming("abcd"), Err(Err::Incomplete(Needed::Size(1)))); +//! +//! // while the complete version knows that all of the data is there +//! assert_eq!(alpha0_complete("abcd"), Ok(("", "abcd"))); +//! ``` +//! **Going further:** read the [guides](https://github.com/Geal/nom/tree/master/doc)! +#![cfg_attr(all(not(feature = "std"), feature = "alloc"), feature(alloc))] +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(feature = "cargo-clippy", allow(doc_markdown))] +#![cfg_attr(nightly, feature(test))] +#![deny(missing_docs)] +#![warn(missing_doc_code_examples)] + +#[cfg(all(not(feature = "std"), feature = "alloc"))] +#[macro_use] +extern crate alloc; +#[cfg(feature = "regexp_macros")] +#[macro_use] +extern crate lazy_static; +extern crate memchr; +#[cfg(feature = "regexp")] +pub extern crate regex; +#[cfg(feature = "lexical")] +extern crate lexical_core; +#[cfg(nightly)] +extern crate test; +#[cfg(test)] +extern crate doc_comment; + +//FIXME: reactivate doctest once https://github.com/rust-lang/rust/issues/62210 is done +//#[cfg(doctest)] +//doc_comment::doctest!("../README.md"); + +/// Lib module to re-export everything needed from `std` or `core`/`alloc`. This is how `serde` does +/// it, albeit there it is not public. +pub mod lib { + /// `std` facade allowing `std`/`core` to be interchangeable. Reexports `alloc` crate optionally, + /// as well as `core` or `std` + #[cfg(not(feature = "std"))] + /// internal std exports for no_std compatibility + pub mod std { + #[cfg(feature = "alloc")] + #[cfg_attr(feature = "alloc", macro_use)] + pub use alloc::{boxed, string, vec}; + + pub use core::{cmp, convert, fmt, iter, mem, ops, option, result, slice, str, borrow}; + + /// internal reproduction of std prelude + pub mod prelude { + pub use core::prelude as v1; + } + } + + #[cfg(feature = "std")] + /// internal std exports for no_std compatibility + pub mod std { + pub use std::{alloc, boxed, cmp, collections, convert, fmt, hash, iter, mem, ops, option, result, slice, str, string, vec, borrow}; + + /// internal reproduction of std prelude + pub mod prelude { + pub use std::prelude as v1; + } + } + + #[cfg(feature = "regexp")] + pub use regex; +} + +pub use self::traits::*; +pub use self::util::*; +pub use self::internal::*; +pub use self::methods::*; +pub use self::bits::*; +pub use self::whitespace::*; + +#[cfg(feature = "regexp")] +pub use self::regexp::*; +pub use self::str::*; + +#[macro_use] +mod util; + +#[macro_use] +pub mod error; + +#[macro_use] +mod internal; +mod traits; +#[macro_use] +pub mod combinator; +#[macro_use] +pub mod branch; +#[macro_use] +pub mod sequence; +#[macro_use] +pub mod multi; +#[macro_use] +pub mod methods; + +#[macro_use] +pub mod bytes; +#[macro_use] +pub mod bits; + +#[macro_use] +pub mod character; + +#[macro_use] +pub mod whitespace; + +#[cfg(feature = "regexp")] +#[macro_use] +mod regexp; + +mod str; + +#[macro_use] +pub mod number; diff --git a/third_party/rust/nom/src/methods.rs b/third_party/rust/nom/src/methods.rs new file mode 100644 index 0000000000..520f1e8bfd --- /dev/null +++ b/third_party/rust/nom/src/methods.rs @@ -0,0 +1,20 @@ +//! method combinators + +/// do not use: method combinators moved to the nom-methods crate +#[macro_export] +macro_rules! method ( + ($($args:tt)*) => (compile_error!("method combinators moved to the nom-methods crate");); +); + +/// do not use: method combinators moved to the nom-methods crate +#[macro_export] +macro_rules! call_m ( + ($($args:tt)*) => (compile_error!("method combinators moved to the nom-methods crate");); +); + +/// do not use: method combinators moved to the nom-methods crate +#[macro_export] +macro_rules! apply_m ( + ($($args:tt)*) => (compile_error!("method combinators moved to the nom-methods crate");); +); + diff --git a/third_party/rust/nom/src/multi/macros.rs b/third_party/rust/nom/src/multi/macros.rs new file mode 100644 index 0000000000..10b66b8424 --- /dev/null +++ b/third_party/rust/nom/src/multi/macros.rs @@ -0,0 +1,988 @@ +//! Parsers for applying parsers multiple times + +/// `separated_list!(I -> IResult, I -> IResult) => I -> IResult>` +/// separated_list(sep, X) returns a Vec +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::separated_list; +/// use nom::bytes::complete::tag; +/// +/// # fn main() { +/// named!(parser<&str, Vec<&str>>, separated_list!(tag("|"), tag("abc"))); +/// +/// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); +/// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"]))); +/// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"]))); +/// assert_eq!(parser(""), Ok(("", vec![]))); +/// assert_eq!(parser("def|abc"), Ok(("def|abc", vec![]))); +/// # } +/// ``` +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! separated_list( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + separated_list!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_list!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_list!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::multi::separated_listc($i, $f, $g) + ); +); + +/// `separated_nonempty_list!(I -> IResult, I -> IResult) => I -> IResult>` +/// separated_nonempty_list(sep, X) returns a Vec +/// +/// it will return an error if there is no element in the list +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::separated_nonempty_list; +/// use nom::bytes::complete::tag; +/// +/// # fn main() { +/// named!(parser<&str, Vec<&str>>, separated_nonempty_list!(tag("|"), tag("abc"))); +/// +/// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); +/// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"]))); +/// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"]))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("def|abc"), Err(Err::Error(("def|abc", ErrorKind::Tag)))); +/// # } +/// ``` +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! separated_nonempty_list( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + separated_nonempty_list!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_nonempty_list!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_nonempty_list!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::multi::separated_nonempty_listc($i, $f, $g) + ); +); + +/// `many0!(I -> IResult) => I -> IResult>` +/// Applies the parser 0 or more times and returns the list of results in a Vec. +/// +/// The embedded parser may return Incomplete. +/// +/// `many0` will only return `Error` if the embedded parser does not consume any input +/// (to avoid infinite loops). +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, many0!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]),Ok((&b"efgh"[..], res))); +/// assert_eq!(multi(&b[..]),Ok((&b"azerty"[..], Vec::new()))); +/// # } +/// ``` +/// +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! many0( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + many0!($i, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr) => ( + $crate::multi::many0c($i, $f) + ); +); + +/// `many1!(I -> IResult) => I -> IResult>` +/// Applies the parser 1 or more times and returns the list of results in a Vec +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, many1!( tag!( "abcd" ) ) ); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]), Ok((&b"efgh"[..], res))); +/// assert_eq!(multi(&b[..]), Err(Err::Error(error_position!(&b[..], ErrorKind::Tag)))); +/// # } +/// ``` +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! many1( + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + many1!($i, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr) => ( + $crate::multi::many1c($i, $f) + ); +); + +/// `many_till!(I -> IResult, I -> IResult) => I -> IResult, P)>` +/// Applies the first parser until the second applies. Returns a tuple containing the list +/// of results from the first in a Vec and the result of the second. +/// +/// The first embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( tag!( "abcd" ), tag!( "efgh" ) ) ); +/// +/// let a = b"abcdabcdefghabcd"; +/// let b = b"efghabcd"; +/// let c = b"azerty"; +/// +/// let res_a = (vec![&b"abcd"[..], &b"abcd"[..]], &b"efgh"[..]); +/// let res_b: (Vec<&[u8]>, &[u8]) = (Vec::new(), &b"efgh"[..]); +/// assert_eq!(multi(&a[..]),Ok((&b"abcd"[..], res_a))); +/// assert_eq!(multi(&b[..]),Ok((&b"abcd"[..], res_b))); +/// assert_eq!(multi(&c[..]), Err(Err::Error(error_node_position!(&c[..], ErrorKind::ManyTill, +/// error_position!(&c[..], ErrorKind::Tag))))); +/// # } +/// ``` +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! many_till( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + many_till!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + many_till!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + many_till!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::multi::many_tillc($i, $f, $g) + ); +); + +/// `many_m_n!(usize, usize, I -> IResult) => I -> IResult>` +/// Applies the parser between m and n times (n included) and returns the list of +/// results in a Vec +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, tag!( "abcd" ) ) ); +/// +/// let a = b"abcdefgh"; +/// let b = b"abcdabcdefgh"; +/// let c = b"abcdabcdabcdabcdabcdefgh"; +/// +/// assert_eq!(multi(&a[..]), Err(Err::Error(error_position!(&b"efgh"[..], ErrorKind::Tag)))); +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&b[..]),Ok((&b"efgh"[..], res))); +/// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&c[..]),Ok((&b"abcdefgh"[..], res2))); +/// # } +/// ``` +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! many_m_n( + ($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* )) => ( + many_m_n!($i, $m, $n, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $m:expr, $n: expr, $f:expr) => ( + $crate::multi::many_m_nc($i, $m, $n, $f) + ); +); + +/// `many0_count!(I -> IResult) => I -> IResult` +/// Applies the parser 0 or more times and returns the number of times the parser was applied. +/// +/// `many0_count` will only return `Error` if the embedded parser does not consume any input +/// (to avoid infinite loops). +/// +/// ``` +/// #[macro_use] extern crate nom; +/// use nom::character::streaming::digit1; +/// +/// named!(number<&[u8], usize>, many0_count!(pair!(digit1, tag!(",")))); +/// +/// fn main() { +/// assert_eq!(number(&b"123,45,abc"[..]), Ok((&b"abc"[..], 2))); +/// } +/// ``` +/// +#[macro_export] +macro_rules! many0_count { + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::multi::many0_countc($i, |i| $submac!(i, $($args)*)) + ); + + ($i:expr, $f:expr) => ( + $crate::multi::many0_countc($i, $f) + ); +} + +/// `many1_count!(I -> IResult) => I -> IResult` +/// Applies the parser 1 or more times and returns the number of times the parser was applied. +/// +/// ``` +/// #[macro_use] extern crate nom; +/// use nom::character::streaming::digit1; +/// +/// named!(number<&[u8], usize>, many1_count!(pair!(digit1, tag!(",")))); +/// +/// fn main() { +/// assert_eq!(number(&b"123,45,abc"[..]), Ok((&b"abc"[..], 2))); +/// } +/// ``` +/// +#[macro_export] +macro_rules! many1_count { + ($i:expr, $submac:ident!( $($args:tt)* )) => ( + $crate::multi::many1_countc($i, |i| $submac!(i, $($args)*)) + ); + + ($i:expr, $f:expr) => ( + $crate::multi::many1_countc($i, $f) + ); +} + +/// `count!(I -> IResult, nb) => I -> IResult>` +/// Applies the child parser a specified number of times +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(counter< Vec<&[u8]> >, count!( tag!( "abcd" ), 2 ) ); +/// +/// let a = b"abcdabcdabcdef"; +/// let b = b"abcdefgh"; +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// +/// assert_eq!(counter(&a[..]),Ok((&b"abcdef"[..], res))); +/// assert_eq!(counter(&b[..]), Err(Err::Error(error_position!(&b"efgh"[..], ErrorKind::Tag)))); +/// # } +/// ``` +/// +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! count( + ($i:expr, $submac:ident!( $($args:tt)* ), $count: expr) => ( + count!($i, |i| $submac!(i, $($args)*), $count) + ); + ($i:expr, $f:expr, $count: expr) => ( + $crate::multi::count($f, $count)($i) + ); +); + +/// `length_count!(I -> IResult, I -> IResult) => I -> IResult>` +/// gets a number from the first parser, then applies the second parser that many times +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// # use nom::error::ErrorKind; +/// use nom::number::complete::be_u8; +/// # fn main() { +/// named!(parser>, length_count!(be_u8, tag!("abc"))); +/// +/// assert_eq!(parser(&b"\x02abcabcabc"[..]), Ok(((&b"abc"[..], vec![&b"abc"[..], &b"abc"[..]])))); +/// assert_eq!(parser(&b"\x04abcabcabc"[..]), Err(Err::Incomplete(Needed::Size(3)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +#[cfg(feature = "alloc")] +macro_rules! length_count( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + match $submac!($i, $($args)*) { + Err(e) => Err(Err::convert(e)), + Ok((i, o)) => { + match count!(i, $submac2!($($args2)*), o as usize) { + Err(e) => Err(Err::convert(e)), + Ok((i2, o2)) => Ok((i2, o2)) + } + } + } + } + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + length_count!($i, $submac!($($args)*), call!($g)); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + length_count!($i, call!($f), $submac!($($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + length_count!($i, call!($f), call!($g)); + ); +); + +/// `length_data!(I -> IResult) => O` +/// +/// `length_data` gets a number from the first parser, then takes a subslice of the input +/// of that size and returns that subslice +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// # use nom::error::ErrorKind; +/// use nom::number::complete::be_u8; +/// # fn main() { +/// named!(parser, length_data!(be_u8)); +/// +/// assert_eq!(parser(&b"\x06abcabcabc"[..]), Ok((&b"abc"[..], &b"abcabc"[..]))); +/// assert_eq!(parser(&b"\x06abc"[..]), Err(Err::Incomplete(Needed::Size(6)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! length_data( + ($i:expr, $submac:ident!( $($args:tt)* )) => ({ + $crate::multi::length_data(|i| $submac!(i, $($args)*))($i) + }); + + ($i:expr, $f:expr) => ( + $crate::multi::length_data($f)($i) + ); +); + +/// `length_value!(I -> IResult, I -> IResult) => I -> IResult` +/// +/// Gets a number from the first parser, takes a subslice of the input of that size, +/// then applies the second parser on that subslice. If the second parser returns +/// `Incomplete`, `length_value` will return an error +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// # use nom::error::ErrorKind; +/// use nom::number::complete::be_u8; +/// use nom::character::complete::alpha0; +/// use nom::bytes::complete::tag; +/// # fn main() { +/// named!(parser, length_value!(be_u8, alpha0)); +/// +/// assert_eq!(parser(&b"\x06abcabcabc"[..]), Ok((&b"abc"[..], &b"abcabc"[..]))); +/// assert_eq!(parser(&b"\x06abc"[..]), Err(Err::Incomplete(Needed::Size(6)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! length_value( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + length_value!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + length_value!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + length_value!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::multi::length_valuec($i, $f, $g); + ); +); + +/// `fold_many0!(I -> IResult, R, Fn(R, O) -> R) => I -> IResult` +/// Applies the parser 0 or more times and folds the list of return values +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, +/// fold_many0!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// })); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]),Ok((&b"efgh"[..], res))); +/// assert_eq!(multi(&b[..]),Ok((&b"azerty"[..], Vec::new()))); +/// # } +/// ``` +/// 0 or more +#[macro_export(local_inner_macros)] +macro_rules! fold_many0( + ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $fold_f:expr) => ( + fold_many0!($i, |i| $submac!(i, $($args)*), $init, $fold_f) + ); + ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( + $crate::multi::fold_many0($f, $init, $fold_f)($i) + ); +); + +/// `fold_many1!(I -> IResult, R, Fn(R, O) -> R) => I -> IResult` +/// Applies the parser 1 or more times and folds the list of return values +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, +/// fold_many1!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// })); +/// +/// let a = b"abcdabcdefgh"; +/// let b = b"azerty"; +/// +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&a[..]),Ok((&b"efgh"[..], res))); +/// assert_eq!(multi(&b[..]), Err(Err::Error(error_position!(&b[..], ErrorKind::Many1)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! fold_many1( + ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $fold_f:expr) => ( + fold_many1!($i, |i| $submac!(i, $($args)*), $init, $fold_f) + ); + ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( + $crate::multi::fold_many1c($i, $f, $init, $fold_f) + ); + ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( + fold_many1!($i, call!($f), $init, $fold_f); + ); +); + +/// `fold_many_m_n!(usize, usize, I -> IResult, R, Fn(R, O) -> R) => I -> IResult` +/// Applies the parser between m and n times (n included) and folds the list of return value +/// +/// the embedded parser may return Incomplete +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # fn main() { +/// named!(multi<&[u8], Vec<&[u8]> >, +/// fold_many_m_n!(2, 4, tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// })); +/// +/// let a = b"abcdefgh"; +/// let b = b"abcdabcdefgh"; +/// let c = b"abcdabcdabcdabcdabcdefgh"; +/// +/// assert_eq!(multi(&a[..]), Err(Err::Error(error_position!(&a[..], ErrorKind::ManyMN)))); +/// let res = vec![&b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&b[..]),Ok((&b"efgh"[..], res))); +/// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; +/// assert_eq!(multi(&c[..]),Ok((&b"abcdefgh"[..], res2))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! fold_many_m_n( + ($i:expr, $m:expr, $n:expr, $submac:ident!( $($args:tt)* ), $init:expr, $fold_f:expr) => ( + fold_many_m_n!($i, $m, $n, |i| $submac!(i, $($args)*), $init, $fold_f) + ); + ($i:expr, $m:expr, $n:expr, $f:expr, $init:expr, $fold_f:expr) => ( + $crate::multi::fold_many_m_nc($i, $m, $n, $f, $init, $fold_f) + ); +); + +#[cfg(test)] +mod tests { + use crate::internal::{Err, IResult, Needed}; + use crate::error::ParseError; + use crate::lib::std::str::{self, FromStr}; + #[cfg(feature = "alloc")] + use crate::lib::std::vec::Vec; + use crate::character::streaming::digit1 as digit; + use crate::number::streaming::{be_u16, be_u8}; + use crate::error::ErrorKind; + + // reproduce the tag and take macros, because of module import order + macro_rules! tag ( + ($i:expr, $inp: expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + + tag_bytes!($i,bytes) + } + ); + ); + + macro_rules! tag_bytes ( + ($i:expr, $bytes: expr) => ( + { + use $crate::lib::std::cmp::min; + let len = $i.len(); + let blen = $bytes.len(); + let m = min(len, blen); + let reduced = &$i[..m]; + let b = &$bytes[..m]; + + let res: IResult<_,_,_> = if reduced != b { + Err($crate::Err::Error($crate::error::make_error($i, $crate::error::ErrorKind::Tag))) + } else if m < blen { + Err($crate::Err::Incomplete(Needed::Size(blen))) + } else { + Ok((&$i[blen..], reduced)) + }; + res + } + ); + ); + + #[test] + #[cfg(feature = "alloc")] + fn separated_list() { + named!(multi<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!("abcd"))); + named!(multi_empty<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!(""))); + named!(multi_longsep<&[u8],Vec<&[u8]> >, separated_list!(tag!(".."), tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"abcd,abcdef"[..]; + let c = &b"azerty"[..]; + let d = &b",,abc"[..]; + let e = &b"abcd,abcd,ef"[..]; + let f = &b"abc"[..]; + let g = &b"abcd."[..]; + let h = &b"abcd,abc"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Ok((&b"ef"[..], res1))); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Ok((&b"ef"[..], res2))); + assert_eq!(multi(c), Ok((&b"azerty"[..], Vec::new()))); + assert_eq!(multi_empty(d), Err(Err::Error(error_position!(d, ErrorKind::SeparatedList)))); + //let res3 = vec![&b""[..], &b""[..], &b""[..]]; + //assert_eq!(multi_empty(d),Ok((&b"abc"[..], res3))); + let res4 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(e), Ok((&b",ef"[..], res4))); + + assert_eq!(multi(f), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(multi_longsep(g), Err(Err::Incomplete(Needed::Size(2)))); + assert_eq!(multi(h), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn separated_nonempty_list() { + named!(multi<&[u8],Vec<&[u8]> >, separated_nonempty_list!(tag!(","), tag!("abcd"))); + named!(multi_longsep<&[u8],Vec<&[u8]> >, separated_nonempty_list!(tag!(".."), tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"abcd,abcdef"[..]; + let c = &b"azerty"[..]; + let d = &b"abcd,abcd,ef"[..]; + + let f = &b"abc"[..]; + let g = &b"abcd."[..]; + let h = &b"abcd,abc"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Ok((&b"ef"[..], res1))); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Ok((&b"ef"[..], res2))); + assert_eq!(multi(c), Err(Err::Error(error_position!(c, ErrorKind::Tag)))); + let res3 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(d), Ok((&b",ef"[..], res3))); + + assert_eq!(multi(f), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(multi_longsep(g), Err(Err::Incomplete(Needed::Size(2)))); + assert_eq!(multi(h), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn many0() { + named!(tag_abcd, tag!("abcd")); + named!(tag_empty, tag!("")); + named!( multi<&[u8],Vec<&[u8]> >, many0!(tag_abcd) ); + named!( multi_empty<&[u8],Vec<&[u8]> >, many0!(tag_empty) ); + + assert_eq!(multi(&b"abcdef"[..]), Ok((&b"ef"[..], vec![&b"abcd"[..]]))); + assert_eq!(multi(&b"abcdabcdefgh"[..]), Ok((&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]]))); + assert_eq!(multi(&b"azerty"[..]), Ok((&b"azerty"[..], Vec::new()))); + assert_eq!(multi(&b"abcdab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(multi(&b"abcd"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(multi(&b""[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!( + multi_empty(&b"abcdef"[..]), + Err(Err::Error(error_position!(&b"abcdef"[..], ErrorKind::Many0))) + ); + } + + #[cfg(nightly)] + use test::Bencher; + + #[cfg(nightly)] + #[bench] + fn many0_bench(b: &mut Bencher) { + named!(multi<&[u8],Vec<&[u8]> >, many0!(tag!("abcd"))); + b.iter(|| multi(&b"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"[..])); + } + + #[test] + #[cfg(feature = "alloc")] + fn many1() { + named!(multi<&[u8],Vec<&[u8]> >, many1!(tag!("abcd"))); + + let a = &b"abcdef"[..]; + let b = &b"abcdabcdefgh"[..]; + let c = &b"azerty"[..]; + let d = &b"abcdab"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Ok((&b"ef"[..], res1))); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Ok((&b"efgh"[..], res2))); + assert_eq!(multi(c), Err(Err::Error(error_position!(c, ErrorKind::Tag)))); + assert_eq!(multi(d), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn many_till() { + named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( tag!( "abcd" ), tag!( "efgh" ) ) ); + + let a = b"abcdabcdefghabcd"; + let b = b"efghabcd"; + let c = b"azerty"; + + let res_a = (vec![&b"abcd"[..], &b"abcd"[..]], &b"efgh"[..]); + let res_b: (Vec<&[u8]>, &[u8]) = (Vec::new(), &b"efgh"[..]); + assert_eq!(multi(&a[..]), Ok((&b"abcd"[..], res_a))); + assert_eq!(multi(&b[..]), Ok((&b"abcd"[..], res_b))); + assert_eq!( + multi(&c[..]), + Err(Err::Error(error_node_position!( + &c[..], + ErrorKind::ManyTill, + error_position!(&c[..], ErrorKind::Tag) + ))) + ); + } + + #[test] + #[cfg(feature = "std")] + fn infinite_many() { + fn tst(input: &[u8]) -> IResult<&[u8], &[u8]> { + println!("input: {:?}", input); + Err(Err::Error(error_position!(input, ErrorKind::Tag))) + } + + // should not go into an infinite loop + named!(multi0<&[u8],Vec<&[u8]> >, many0!(tst)); + let a = &b"abcdef"[..]; + assert_eq!(multi0(a), Ok((a, Vec::new()))); + + named!(multi1<&[u8],Vec<&[u8]> >, many1!(tst)); + let a = &b"abcdef"[..]; + assert_eq!(multi1(a), Err(Err::Error(error_position!(a, ErrorKind::Tag)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn many_m_n() { + named!(multi<&[u8],Vec<&[u8]> >, many_m_n!(2, 4, tag!("Abcd"))); + + let a = &b"Abcdef"[..]; + let b = &b"AbcdAbcdefgh"[..]; + let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; + let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; + let e = &b"AbcdAb"[..]; + + assert_eq!(multi(a), Err(Err::Error(error_position!(&b"ef"[..], ErrorKind::Tag)))); + let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(b), Ok((&b"efgh"[..], res1))); + let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(c), Ok((&b"efgh"[..], res2))); + let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(d), Ok((&b"Abcdefgh"[..], res3))); + assert_eq!(multi(e), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn count() { + const TIMES: usize = 2; + named!(tag_abc, tag!("abc")); + named!( cnt_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); + + assert_eq!(cnt_2(&b"abcabcabcdef"[..]), Ok((&b"abcdef"[..], vec![&b"abc"[..], &b"abc"[..]]))); + assert_eq!(cnt_2(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(cnt_2(&b"abcab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(cnt_2(&b"xxx"[..]), Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))); + assert_eq!( + cnt_2(&b"xxxabcabcdef"[..]), + Err(Err::Error(error_position!(&b"xxxabcabcdef"[..], ErrorKind::Tag))) + ); + assert_eq!( + cnt_2(&b"abcxxxabcdef"[..]), + Err(Err::Error(error_position!(&b"xxxabcdef"[..], ErrorKind::Tag))) + ); + } + + #[test] + #[cfg(feature = "alloc")] + fn count_zero() { + const TIMES: usize = 0; + named!(tag_abc, tag!("abc")); + named!( counter_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); + + let done = &b"abcabcabcdef"[..]; + let parsed_done = Vec::new(); + let rest = done; + let incomplete_1 = &b"ab"[..]; + let parsed_incompl_1 = Vec::new(); + let incomplete_2 = &b"abcab"[..]; + let parsed_incompl_2 = Vec::new(); + let error = &b"xxx"[..]; + let error_remain = &b"xxx"[..]; + let parsed_err = Vec::new(); + let error_1 = &b"xxxabcabcdef"[..]; + let parsed_err_1 = Vec::new(); + let error_1_remain = &b"xxxabcabcdef"[..]; + let error_2 = &b"abcxxxabcdef"[..]; + let parsed_err_2 = Vec::new(); + let error_2_remain = &b"abcxxxabcdef"[..]; + + assert_eq!(counter_2(done), Ok((rest, parsed_done))); + assert_eq!(counter_2(incomplete_1), Ok((incomplete_1, parsed_incompl_1))); + assert_eq!(counter_2(incomplete_2), Ok((incomplete_2, parsed_incompl_2))); + assert_eq!(counter_2(error), Ok((error_remain, parsed_err))); + assert_eq!(counter_2(error_1), Ok((error_1_remain, parsed_err_1))); + assert_eq!(counter_2(error_2), Ok((error_2_remain, parsed_err_2))); + } + + #[derive(Debug, Clone, PartialEq)] + pub struct NilError; + + impl From<(I,ErrorKind)> for NilError { + fn from(_: (I, ErrorKind)) -> Self { + NilError + } + } + + impl ParseError for NilError { + fn from_error_kind(_: I, _: ErrorKind) -> NilError { + NilError + } + fn append(_: I, _: ErrorKind, _: NilError) -> NilError { + NilError + } + } + + named!(pub number, map_res!( + map_res!( + digit, + str::from_utf8 + ), + FromStr::from_str + )); + + #[test] + #[cfg(feature = "alloc")] + fn length_count() { + named!(tag_abc, tag!(&b"abc"[..])); + named!( cnt<&[u8], Vec<&[u8]> >, length_count!(number, tag_abc) ); + + assert_eq!(cnt(&b"2abcabcabcdef"[..]), Ok((&b"abcdef"[..], vec![&b"abc"[..], &b"abc"[..]]))); + assert_eq!(cnt(&b"2ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(cnt(&b"3abcab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(cnt(&b"xxx"[..]), Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Digit)))); + assert_eq!( + cnt(&b"2abcxxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn length_data() { + named!( take<&[u8], &[u8]>, length_data!(number) ); + + assert_eq!(take(&b"6abcabcabcdef"[..]), Ok((&b"abcdef"[..], &b"abcabc"[..]))); + assert_eq!(take(&b"3ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(take(&b"xxx"[..]), Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Digit)))); + assert_eq!(take(&b"2abcxxx"[..]), Ok((&b"cxxx"[..], &b"ab"[..]))); + } + + #[test] + fn length_value_test() { + named!(length_value_1<&[u8], u16 >, length_value!(be_u8, be_u16)); + named!(length_value_2<&[u8], (u8, u8) >, length_value!(be_u8, tuple!(be_u8, be_u8))); + + let i1 = [0, 5, 6]; + assert_eq!(length_value_1(&i1), Err(Err::Error(error_position!(&b""[..], ErrorKind::Complete)))); + assert_eq!(length_value_2(&i1), Err(Err::Error(error_position!(&b""[..], ErrorKind::Complete)))); + + let i2 = [1, 5, 6, 3]; + assert_eq!( + length_value_1(&i2), + Err(Err::Error(error_position!(&i2[1..2], ErrorKind::Complete))) + ); + assert_eq!( + length_value_2(&i2), + Err(Err::Error(error_position!(&i2[1..2], ErrorKind::Complete))) + ); + + let i3 = [2, 5, 6, 3, 4, 5, 7]; + assert_eq!(length_value_1(&i3), Ok((&i3[3..], 1286))); + assert_eq!(length_value_2(&i3), Ok((&i3[3..], (5, 6)))); + + let i4 = [3, 5, 6, 3, 4, 5]; + assert_eq!(length_value_1(&i4), Ok((&i4[4..], 1286))); + assert_eq!(length_value_2(&i4), Ok((&i4[4..], (5, 6)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn fold_many0() { + fn fold_into_vec(mut acc: Vec, item: T) -> Vec { + acc.push(item); + acc + }; + named!(tag_abcd, tag!("abcd")); + named!(tag_empty, tag!("")); + named!( multi<&[u8],Vec<&[u8]> >, fold_many0!(tag_abcd, Vec::new(), fold_into_vec) ); + named!( multi_empty<&[u8],Vec<&[u8]> >, fold_many0!(tag_empty, Vec::new(), fold_into_vec) ); + + assert_eq!(multi(&b"abcdef"[..]), Ok((&b"ef"[..], vec![&b"abcd"[..]]))); + assert_eq!(multi(&b"abcdabcdefgh"[..]), Ok((&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]]))); + assert_eq!(multi(&b"azerty"[..]), Ok((&b"azerty"[..], Vec::new()))); + assert_eq!(multi(&b"abcdab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(multi(&b"abcd"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(multi(&b""[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!( + multi_empty(&b"abcdef"[..]), + Err(Err::Error(error_position!(&b"abcdef"[..], ErrorKind::Many0))) + ); + } + + #[test] + #[cfg(feature = "alloc")] + fn fold_many1() { + fn fold_into_vec(mut acc: Vec, item: T) -> Vec { + acc.push(item); + acc + }; + named!(multi<&[u8],Vec<&[u8]> >, fold_many1!(tag!("abcd"), Vec::new(), fold_into_vec)); + + let a = &b"abcdef"[..]; + let b = &b"abcdabcdefgh"[..]; + let c = &b"azerty"[..]; + let d = &b"abcdab"[..]; + + let res1 = vec![&b"abcd"[..]]; + assert_eq!(multi(a), Ok((&b"ef"[..], res1))); + let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; + assert_eq!(multi(b), Ok((&b"efgh"[..], res2))); + assert_eq!(multi(c), Err(Err::Error(error_position!(c, ErrorKind::Many1)))); + assert_eq!(multi(d), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + #[cfg(feature = "alloc")] + fn fold_many_m_n() { + fn fold_into_vec(mut acc: Vec, item: T) -> Vec { + acc.push(item); + acc + }; + named!(multi<&[u8],Vec<&[u8]> >, fold_many_m_n!(2, 4, tag!("Abcd"), Vec::new(), fold_into_vec)); + + let a = &b"Abcdef"[..]; + let b = &b"AbcdAbcdefgh"[..]; + let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; + let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; + let e = &b"AbcdAb"[..]; + + assert_eq!(multi(a), Err(Err::Error(error_position!(a, ErrorKind::ManyMN)))); + let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(b), Ok((&b"efgh"[..], res1))); + let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(c), Ok((&b"efgh"[..], res2))); + let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; + assert_eq!(multi(d), Ok((&b"Abcdefgh"[..], res3))); + assert_eq!(multi(e), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[test] + fn many0_count() { + named!( + count0_nums(&[u8]) -> usize, + many0_count!(pair!(digit, tag!(","))) + ); + + assert_eq!(count0_nums(&b"123,junk"[..]), Ok((&b"junk"[..], 1))); + + assert_eq!(count0_nums(&b"123,45,junk"[..]), Ok((&b"junk"[..], 2))); + + assert_eq!(count0_nums(&b"1,2,3,4,5,6,7,8,9,0,junk"[..]), Ok((&b"junk"[..], 10))); + + assert_eq!(count0_nums(&b"hello"[..]), Ok((&b"hello"[..], 0))); + } + + #[test] + fn many1_count() { + named!( + count1_nums(&[u8]) -> usize, + many1_count!(pair!(digit, tag!(","))) + ); + + assert_eq!(count1_nums(&b"123,45,junk"[..]), Ok((&b"junk"[..], 2))); + + assert_eq!(count1_nums(&b"1,2,3,4,5,6,7,8,9,0,junk"[..]), Ok((&b"junk"[..], 10))); + + assert_eq!( + count1_nums(&b"hello"[..]), + Err(Err::Error(error_position!(&b"hello"[..], ErrorKind::Many1Count))) + ); + } + +} diff --git a/third_party/rust/nom/src/multi/mod.rs b/third_party/rust/nom/src/multi/mod.rs new file mode 100644 index 0000000000..e8f26dd771 --- /dev/null +++ b/third_party/rust/nom/src/multi/mod.rs @@ -0,0 +1,997 @@ +//! combinators applying their child parser multiple times + +#[macro_use] +mod macros; + +use crate::internal::{Err, IResult, Needed}; +use crate::error::ParseError; +use crate::traits::{InputLength, InputTake, ToUsize}; +#[cfg(feature = "alloc")] +use crate::lib::std::vec::Vec; +use crate::error::ErrorKind; + +/// Repeats the embedded parser until it fails +/// and returns the results in a `Vec`. +/// +/// # Arguments +/// * `f` The parser to apply. +/// +/// *Note*: if the parser passed to `many0` accepts empty inputs +/// (like `alpha0` or `digit0`), `many0` will return an error, +/// to prevent going into an infinite loop +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::many0; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// many0(tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); +/// assert_eq!(parser("123123"), Ok(("123123", vec![]))); +/// assert_eq!(parser(""), Ok(("", vec![]))); +/// ``` +#[cfg(feature = "alloc")] +pub fn many0(f: F) -> impl Fn(I) -> IResult, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + E: ParseError, +{ + move |i: I| { + let mut acc = crate::lib::std::vec::Vec::with_capacity(4); + let mut i = i.clone(); + loop { + match f(i.clone()) { + Err(Err::Error(_)) => return Ok((i, acc)), + Err(e) => return Err(e), + Ok((i1, o)) => { + if i1 == i { + return Err(Err::Error(E::from_error_kind(i, ErrorKind::Many0))); + } + + i = i1; + acc.push(o); + } + } + } + } +} +// this implementation is used for type inference issues in macros +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn many0c(input: I, f: F) -> IResult, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + E: ParseError, +{ + many0(f)(input) +} + +/// Runs the embedded parser until it fails and +/// returns the results in a `Vec`. Fails if +/// the embedded parser does not produce at least +/// one result. +/// +/// # Arguments +/// * `f` The parser to apply. +/// +/// *Note*: if the parser passed to `many1` accepts empty inputs +/// (like `alpha0` or `digit0`), `many1` will return an error, +/// to prevent going into an infinite loop +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::many1; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// many1(tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); +/// assert_eq!(parser("123123"), Err(Err::Error(("123123", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// ``` +#[cfg(feature = "alloc")] +pub fn many1(f: F) -> impl Fn(I) -> IResult, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + E: ParseError, +{ + move |i: I| { + let mut i = i.clone(); + match f(i.clone()) { + Err(Err::Error(err)) => return Err(Err::Error(E::append(i, ErrorKind::Many1, err))), + Err(e) => return Err(e), + Ok((i1, o)) => { + let mut acc = crate::lib::std::vec::Vec::with_capacity(4); + acc.push(o); + i = i1; + + loop { + match f(i.clone()) { + Err(Err::Error(_)) => return Ok((i, acc)), + Err(e) => return Err(e), + Ok((i1, o)) => { + if i1 == i { + return Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1))); + } + + i = i1; + acc.push(o); + } + } + } + } + } + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn many1c(input: I, f: F) -> IResult, E> +where + I: Clone + Copy + PartialEq, + F: Fn(I) -> IResult, + E: ParseError, +{ + many1(f)(input) +} + +/// Applies the parser `f` until the parser `g` produces +/// a result. Returns a pair consisting of the results of +/// `f` in a `Vec` and the result of `g`. +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::many_till; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, (Vec<&str>, &str)> { +/// many_till(tag("abc"), tag("end"))(s) +/// }; +/// +/// assert_eq!(parser("abcabcend"), Ok(("", (vec!["abc", "abc"], "end")))); +/// assert_eq!(parser("abc123end"), Err(Err::Error(("123end", ErrorKind::Tag)))); +/// assert_eq!(parser("123123end"), Err(Err::Error(("123123end", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("abcendefg"), Ok(("efg", (vec!["abc"], "end")))); +/// ``` +#[cfg(feature = "alloc")] +pub fn many_till(f: F, g: G) -> impl Fn(I) -> IResult, P), E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, + E: ParseError, +{ + move |i: I| { + let mut res = crate::lib::std::vec::Vec::new(); + let mut i = i.clone(); + loop { + match g(i.clone()) { + Ok((i1, o)) => return Ok((i1, (res, o))), + Err(Err::Error(_)) => { + match f(i.clone()) { + Err(Err::Error(err)) => + return Err(Err::Error(E::append(i, ErrorKind::ManyTill, err))), + Err(e) => return Err(e), + Ok((i1, o)) => { + // loop trip must always consume (otherwise infinite loops) + if i1 == i { + return Err(Err::Error(E::from_error_kind(i1, ErrorKind::ManyTill))); + } + + res.push(o); + i = i1; + } + } + }, + Err(e) => return Err(e), + } + } + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn many_tillc(i: I, f: F, g: G) -> IResult, P), E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, + E: ParseError, +{ + many_till(f, g)(i) +} + +/// Alternates between two parsers to produce +/// a list of elements. +/// # Arguments +/// * `sep` Parses the separator between list elements. +/// * `f` Parses the elements of the list. +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::separated_list; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// separated_list(tag("|"), tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); +/// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"]))); +/// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"]))); +/// assert_eq!(parser(""), Ok(("", vec![]))); +/// assert_eq!(parser("def|abc"), Ok(("def|abc", vec![]))); +/// ``` +#[cfg(feature = "alloc")] +pub fn separated_list(sep: G, f: F) -> impl Fn(I) -> IResult, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, + E: ParseError, +{ + move |i: I| { + let mut res = Vec::new(); + let mut i = i.clone(); + + match f(i.clone()) { + Err(Err::Error(_)) => return Ok((i, res)), + Err(e) => return Err(e), + Ok((i1, o)) => { + if i1 == i { + return Err(Err::Error(E::from_error_kind(i1, ErrorKind::SeparatedList))); + } + + res.push(o); + i = i1; + } + } + + loop { + match sep(i.clone()) { + Err(Err::Error(_)) => return Ok((i, res)), + Err(e) => return Err(e), + Ok((i1, _)) => { + if i1 == i { + return Err(Err::Error(E::from_error_kind(i1, ErrorKind::SeparatedList))); + } + + match f(i1.clone()) { + Err(Err::Error(_)) => return Ok((i, res)), + Err(e) => return Err(e), + Ok((i2, o)) => { + if i2 == i { + return Err(Err::Error(E::from_error_kind(i2, ErrorKind::SeparatedList))); + } + + res.push(o); + i = i2; + } + } + } + } + } + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn separated_listc(i: I, sep: G, f: F) -> IResult, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, + E: ParseError, +{ + separated_list(sep, f)(i) +} + +/// Alternates between two parsers to produce +/// a list of elements. Fails if the element +/// parser does not produce at least one element. +/// # Arguments +/// * `sep` Parses the separator between list elements. +/// * `f` Parses the elements of the list. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::separated_nonempty_list; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// separated_nonempty_list(tag("|"), tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); +/// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"]))); +/// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"]))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("def|abc"), Err(Err::Error(("def|abc", ErrorKind::Tag)))); +/// ``` +#[cfg(feature = "alloc")] +pub fn separated_nonempty_list(sep: G, f: F) -> impl Fn(I) -> IResult, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, + E: ParseError, +{ + move |i: I| { + let mut res = Vec::new(); + let mut i = i.clone(); + + // Parse the first element + match f(i.clone()) { + Err(e)=> return Err(e), + Ok((i1, o)) => { + if i1 == i { + return Err(Err::Error(E::from_error_kind(i1, ErrorKind::SeparatedList))); + } + + res.push(o); + i = i1; + } + } + + loop { + match sep(i.clone()) { + Err(Err::Error(_)) => return Ok((i, res)), + Err(e) => return Err(e), + Ok((i1, _)) => { + if i1 == i { + return Err(Err::Error(E::from_error_kind(i1, ErrorKind::SeparatedList))); + } + + match f(i1.clone()) { + Err(Err::Error(_)) => return Ok((i, res)), + Err(e) => return Err(e), + Ok((i2, o)) => { + if i2 == i { + return Err(Err::Error(E::from_error_kind(i2, ErrorKind::SeparatedList))); + } + + res.push(o); + i = i2; + } + } + } + } + } + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn separated_nonempty_listc(i: I, sep: G, f: F) -> IResult, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, + E: ParseError, +{ + separated_nonempty_list(sep, f)(i) +} + +/// Repeats the embedded parser `n` times or until it fails +/// and returns the results in a `Vec`. Fails if the +/// embedded parser does not succeed at least `m` times. +/// # Arguments +/// * `m` The minimum number of iterations. +/// * `n` The maximum number of iterations. +/// * `f` The parser to apply. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::many_m_n; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// many_m_n(0, 2, tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); +/// assert_eq!(parser("123123"), Ok(("123123", vec![]))); +/// assert_eq!(parser(""), Ok(("", vec![]))); +/// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"]))); +/// ``` +#[cfg(feature = "alloc")] +pub fn many_m_n(m: usize, n: usize, f: F) -> impl Fn(I) -> IResult, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + E: ParseError, +{ + move |i: I| { + let mut res = crate::lib::std::vec::Vec::with_capacity(m); + let mut input = i.clone(); + let mut count: usize = 0; + + if n == 0 { + return Ok((i, vec!())) + } + + loop { + let _i = input.clone(); + match f(_i) { + Ok((i, o)) => { + // do not allow parsers that do not consume input (causes infinite loops) + if i == input { + return Err(Err::Error(E::from_error_kind(input, ErrorKind::ManyMN))); + } + + res.push(o); + input = i; + count += 1; + + if count == n { + return Ok((input, res)); + } + } + Err(Err::Error(e)) => { + if count < m { + return Err(Err::Error(E::append(input, ErrorKind::ManyMN, e))); + } else { + return Ok((input, res)); + } + } + Err(e) => { + return Err(e); + } + } + } + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub fn many_m_nc(i: I, m: usize, n: usize, f: F) -> IResult, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + E: ParseError, +{ + many_m_n(m, n, f)(i) +} + +/// Repeats the embedded parser until it fails +/// and returns the number of successful iterations. +/// # Arguments +/// * `f` The parser to apply. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::many0_count; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, usize> { +/// many0_count(tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", 2))); +/// assert_eq!(parser("abc123"), Ok(("123", 1))); +/// assert_eq!(parser("123123"), Ok(("123123", 0))); +/// assert_eq!(parser(""), Ok(("", 0))); +/// ``` +pub fn many0_count(f: F) -> impl Fn(I) -> IResult +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + E: ParseError, +{ + move |i: I| { + let mut input = i.clone(); + let mut count = 0; + + loop { + let input_ = input.clone(); + match f(input_) { + Ok((i, _)) => { + // loop trip must always consume (otherwise infinite loops) + if i == input { + return Err(Err::Error(E::from_error_kind(input, ErrorKind::Many0Count))); + } + + input = i; + count += 1; + } + + Err(Err::Error(_)) => return Ok((input, count)), + + Err(e) => return Err(e), + } + } + } +} + +#[doc(hidden)] +pub fn many0_countc(i: I, f: F) -> IResult +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + E: ParseError, +{ + many0_count(f)(i) +} + +/// Repeats the embedded parser until it fails +/// and returns the number of successful iterations. +/// Fails if the embedded parser does not succeed +/// at least once. +/// # Arguments +/// * `f` The parser to apply. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::many1_count; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, usize> { +/// many1_count(tag("abc"))(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", 2))); +/// assert_eq!(parser("abc123"), Ok(("123", 1))); +/// assert_eq!(parser("123123"), Err(Err::Error(("123123", ErrorKind::Many1Count)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Many1Count)))); +/// ``` +pub fn many1_count(f: F) -> impl Fn(I) -> IResult +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + E: ParseError, +{ + move |i: I| { + let i_ = i.clone(); + match f(i_) { + Err(Err::Error(_)) => Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1Count))), + Err(i) => Err(i), + Ok((i1, _)) => { + let mut count = 1; + let mut input = i1; + + loop { + let input_ = input.clone(); + match f(input_) { + Err(Err::Error(_)) => return Ok((input, count)), + Err(e) => return Err(e), + Ok((i, _)) => { + if i == input { + return Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1Count))); + } + + count += 1; + input = i; + } + } + } + } + } + } +} + +#[doc(hidden)] +pub fn many1_countc(i: I, f: F) -> IResult +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + E: ParseError, +{ + many1_count(f)(i) +} + +/// Runs the embedded parser a specified number +/// of times. Returns the results in a `Vec`. +/// # Arguments +/// * `f` The parser to apply. +/// * `count` How often to apply the parser. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::count; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// count(tag("abc"), 2)(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Err(Err::Error(("123", ErrorKind::Tag)))); +/// assert_eq!(parser("123123"), Err(Err::Error(("123123", ErrorKind::Tag)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"]))); +/// ``` +#[cfg(feature = "alloc")] +pub fn count(f: F, count: usize) -> impl Fn(I) -> IResult, E> +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + E: ParseError, +{ + move |i: I | { + let mut input = i.clone(); + let mut res = crate::lib::std::vec::Vec::new(); + + for _ in 0..count { + let input_ = input.clone(); + match f(input_) { + Ok((i, o)) => { + res.push(o); + input = i; + } + Err(Err::Error(e)) => { + return Err(Err::Error(E::append(i, ErrorKind::Count, e))); + } + Err(e) => { + return Err(e); + } + } + } + + Ok((input, res)) + } +} + +/// Applies a parser until it fails and accumulates +/// the results using a given function and initial value. +/// # Arguments +/// * `f` The parser to apply. +/// * `init` The initial value. +/// * `g` The function that combines a result of `f` with +/// the current accumulator. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::fold_many0; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// fold_many0( +/// tag("abc"), +/// Vec::new(), +/// |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// } +/// )(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); +/// assert_eq!(parser("123123"), Ok(("123123", vec![]))); +/// assert_eq!(parser(""), Ok(("", vec![]))); +/// ``` +pub fn fold_many0(f: F, init: R, g: G) -> impl Fn(I) -> IResult +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + G: Fn(R, O) -> R, + E: ParseError, + R: Clone, +{ + move |i: I| { + let mut res = init.clone(); + let mut input = i.clone(); + + loop { + let i_ = input.clone(); + match f(i_) { + Ok((i, o)) => { + // loop trip must always consume (otherwise infinite loops) + if i == input { + return Err(Err::Error(E::from_error_kind(input, ErrorKind::Many0))); + } + + res = g(res, o); + input = i; + } + Err(Err::Error(_)) => { + return Ok((input, res)); + } + Err(e) => { + return Err(e); + } + } + } + } +} + +#[doc(hidden)] +pub fn fold_many0c(i: I, f: F, init: R, g: G) -> IResult +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + G: Fn(R, O) -> R, + E: ParseError, + R: Clone, +{ + fold_many0(f, init, g)(i) +} + +/// Applies a parser until it fails and accumulates +/// the results using a given function and initial value. +/// Fails if the embedded parser does not succeed at least +/// once. +/// # Arguments +/// * `f` The parser to apply. +/// * `init` The initial value. +/// * `g` The function that combines a result of `f` with +/// the current accumulator. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::fold_many1; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// fold_many1( +/// tag("abc"), +/// Vec::new(), +/// |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// } +/// )(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); +/// assert_eq!(parser("123123"), Err(Err::Error(("123123", ErrorKind::Many1)))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Many1)))); +/// ``` +pub fn fold_many1(f: F, init: R, g: G) -> impl Fn(I) -> IResult +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + G: Fn(R, O) -> R, + E: ParseError, + R: Clone, +{ + move |i: I| { + let _i = i.clone(); + let init = init.clone(); + match f(_i) { + Err(Err::Error(_)) => Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1))), + Err(e) => return Err(e), + Ok((i1, o1)) => { + let mut acc = g(init, o1); + let mut input = i1; + + loop { + let _input = input.clone(); + match f(_input) { + Err(Err::Error(_)) => { + break; + } + Err(e) => return Err(e), + Ok((i, o)) => { + if i == input { + return Err(Err::Failure(E::from_error_kind(i, ErrorKind::Many1))); + } + + acc = g(acc, o); + input = i; + } + } + } + + Ok((input, acc)) + } + } + } +} + +#[doc(hidden)] +pub fn fold_many1c(i: I, f: F, init: R, g: G) -> IResult +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + G: Fn(R, O) -> R, + E: ParseError, + R: Clone, +{ + fold_many1(f, init, g)(i) +} + +/// Applies a parser `n` times or until it fails and accumulates +/// the results using a given function and initial value. +/// Fails if the embedded parser does not succeed at least `m` +/// times. +/// # Arguments +/// * `m` The minimum number of iterations. +/// * `n` The maximum number of iterations. +/// * `f` The parser to apply. +/// * `init` The initial value. +/// * `g` The function that combines a result of `f` with +/// the current accumulator. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// use nom::multi::fold_many_m_n; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &str) -> IResult<&str, Vec<&str>> { +/// fold_many_m_n( +/// 0, +/// 2, +/// tag("abc"), +/// Vec::new(), +/// |mut acc: Vec<_>, item| { +/// acc.push(item); +/// acc +/// } +/// )(s) +/// } +/// +/// assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"]))); +/// assert_eq!(parser("abc123"), Ok(("123", vec!["abc"]))); +/// assert_eq!(parser("123123"), Ok(("123123", vec![]))); +/// assert_eq!(parser(""), Ok(("", vec![]))); +/// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"]))); +/// ``` +pub fn fold_many_m_n(m: usize, n: usize, f: F, init: R, g: G) -> impl Fn(I) ->IResult +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + G: Fn(R, O) -> R, + E: ParseError, + R: Clone, +{ + move |i: I| { + let mut acc = init.clone(); + let mut input = i.clone(); + for count in 0..n { + let _input = input.clone(); + match f(_input) { + Ok((i, o)) => { + // do not allow parsers that do not consume input (causes infinite loops) + if i == input { + return Err(Err::Error(E::from_error_kind(i, ErrorKind::ManyMN))); + } + + acc = g(acc, o); + input = i; + } + //FInputXMError: handle failure properly + Err(Err::Error(_)) => if count < m { + return Err(Err::Error(E::from_error_kind(i, ErrorKind::ManyMN))); + } else { + break; + } + Err(e) => return Err(e), + } + } + + Ok((input, acc)) + } +} + +#[doc(hidden)] +pub fn fold_many_m_nc(i: I, m: usize, n: usize, f: F, init: R, g: G) -> IResult +where + I: Clone + PartialEq, + F: Fn(I) -> IResult, + G: Fn(R, O) -> R, + E: ParseError, + R: Clone, +{ + fold_many_m_n(m, n, f, init, g)(i) +} + +/// Gets a number from the parser and returns a +/// subslice of the input of that size. +/// If the parser returns Incomplete, +/// length_data will return an error. +/// # Arguments +/// * `f` The parser to apply. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u16; +/// use nom::multi::length_data; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// length_data(be_u16)(s) +/// } +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"efg"[..], &b"abc"[..]))); +/// assert_eq!(parser(b"\x00\x03"), Err(Err::Incomplete(Size(3)))); +/// ``` +pub fn length_data(f: F) -> impl Fn(I) -> IResult +where + I: Clone + InputLength + InputTake, + N: Copy + ToUsize, + F: Fn(I) -> IResult, + E: ParseError, +{ + move |i: I| { + let (i, length) = f(i)?; + + let length: usize = length.to_usize(); + + if i.input_len() < length { + Err(Err::Incomplete(Needed::Size(length))) + } else { + Ok(i.take_split(length)) + } + } +} + +/// Gets a number from the first parser, +/// takes a subslice of the input of that size, +/// then applies the second parser on that subslice. +/// If the second parser returns Incomplete, +/// length_value will return an error. +/// # Arguments +/// * `f` The parser to apply. +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, error::ErrorKind, Needed, IResult}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u16; +/// use nom::multi::length_value; +/// use nom::bytes::complete::tag; +/// +/// fn parser(s: &[u8]) -> IResult<&[u8], &[u8]> { +/// length_value(be_u16, tag("abc"))(s) +/// } +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"efg"[..], &b"abc"[..]))); +/// assert_eq!(parser(b"\x00\x03123123"), Err(Err::Error((&b"123"[..], ErrorKind::Tag)))); +/// assert_eq!(parser(b"\x00\x03"), Err(Err::Incomplete(Size(3)))); +/// ``` +pub fn length_value(f: F, g: G) -> impl Fn(I) -> IResult +where + I: Clone + InputLength + InputTake, + N: Copy + ToUsize, + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, + E: ParseError, +{ + move |i: I| { + let (i, length) = f(i)?; + + let length: usize = length.to_usize(); + + if i.input_len() < length { + Err(Err::Incomplete(Needed::Size(length))) + } else { + let (rest, i) = i.take_split(length); + match g(i.clone()) { + Err(Err::Incomplete(_)) => + Err(Err::Error(E::from_error_kind(i, ErrorKind::Complete))), + Err(e) => Err(e), + Ok((_, o)) => Ok((rest,o)), + } + } + } +} + +#[doc(hidden)] +pub fn length_valuec(i: I, f: F, g: G) -> IResult +where + I: Clone + InputLength + InputTake, + N: Copy + ToUsize, + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, + E: ParseError, +{ + length_value(f, g)(i) +} diff --git a/third_party/rust/nom/src/number/complete.rs b/third_party/rust/nom/src/number/complete.rs new file mode 100644 index 0000000000..b8d423271c --- /dev/null +++ b/third_party/rust/nom/src/number/complete.rs @@ -0,0 +1,1198 @@ +//! parsers recognizing numbers, complete input version + +use crate::internal::*; +use crate::error::ParseError; +use crate::traits::{AsChar, InputIter, InputLength, InputTakeAtPosition}; +use crate::lib::std::ops::{RangeFrom, RangeTo}; +use crate::traits::{Offset, Slice}; +use crate::error::{ErrorKind, make_error}; +use crate::character::complete::{char, digit1}; +use crate::combinator::{opt, cut, map, recognize}; +use crate::branch::alt; +use crate::sequence::{tuple, pair}; + +/// Recognizes an unsigned 1 byte integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u8; +/// +/// let parser = |s| { +/// be_u8(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"\x03abcefg"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Error((&[][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_u8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> { + if i.len() < 1 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + Ok((&i[1..], i[0])) + } +} + +/// Recognizes a big endian unsigned 2 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u16; +/// +/// let parser = |s| { +/// be_u16(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"abcefg"[..], 0x0003))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_u16<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> { + if i.len() < 2 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[0] as u16) << 8) + i[1] as u16; + Ok((&i[2..], res)) + } +} + +/// Recognizes a big endian unsigned 3 byte integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u24; +/// +/// let parser = |s| { +/// be_u24(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05abcefg"), Ok((&b"abcefg"[..], 0x000305))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_u24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 3 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[0] as u32) << 16) + ((i[1] as u32) << 8) + (i[2] as u32); + Ok((&i[3..], res)) + } +} + +/// Recognizes a big endian unsigned 4 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u32; +/// +/// let parser = |s| { +/// be_u32(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05\x07abcefg"), Ok((&b"abcefg"[..], 0x00030507))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_u32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 4 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[0] as u32) << 24) + ((i[1] as u32) << 16) + ((i[2] as u32) << 8) + i[3] as u32; + Ok((&i[4..], res)) + } +} + +/// Recognizes a big endian unsigned 8 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u64; +/// +/// let parser = |s| { +/// be_u64(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x0001020304050607))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_u64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> { + if i.len() < 8 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[0] as u64) << 56) + ((i[1] as u64) << 48) + ((i[2] as u64) << 40) + ((i[3] as u64) << 32) + ((i[4] as u64) << 24) + + ((i[5] as u64) << 16) + ((i[6] as u64) << 8) + i[7] as u64; + Ok((&i[8..], res)) + } +} + +/// Recognizes a big endian unsigned 16 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_u128; +/// +/// let parser = |s| { +/// be_u128(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn be_u128<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> { + if i.len() < 16 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[0] as u128) << 120) + + ((i[1] as u128) << 112) + + ((i[2] as u128) << 104) + + ((i[3] as u128) << 96) + + ((i[4] as u128) << 88) + + ((i[5] as u128) << 80) + + ((i[6] as u128) << 72) + + ((i[7] as u128) << 64) + + ((i[8] as u128) << 56) + + ((i[9] as u128) << 48) + + ((i[10] as u128) << 40) + + ((i[11] as u128) << 32) + + ((i[12] as u128) << 24) + + ((i[13] as u128) << 16) + + ((i[14] as u128) << 8) + + i[15] as u128; + Ok((&i[16..], res)) + } +} + +/// Recognizes a signed 1 byte integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_i8; +/// +/// let parser = |s| { +/// be_i8(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"\x03abcefg"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Error((&[][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_i8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> { + map!(i, be_u8, |x| x as i8) +} + +/// Recognizes a big endian signed 2 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_i16; +/// +/// let parser = |s| { +/// be_i16(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"abcefg"[..], 0x0003))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> { + map!(i, be_u16, |x| x as i16) +} + +/// Recognizes a big endian signed 3 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_i24; +/// +/// let parser = |s| { +/// be_i24(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05abcefg"), Ok((&b"abcefg"[..], 0x000305))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_i24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + // Same as the unsigned version but we need to sign-extend manually here + map!(i, be_u24, |x| if x & 0x80_00_00 != 0 { + (x | 0xff_00_00_00) as i32 + } else { + x as i32 + }) +} + +/// Recognizes a big endian signed 4 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_i32; +/// +/// let parser = |s| { +/// be_i32(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05\x07abcefg"), Ok((&b"abcefg"[..], 0x00030507))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_i32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + map!(i, be_u32, |x| x as i32) +} + +/// Recognizes a big endian signed 8 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_i64; +/// +/// let parser = |s| { +/// be_i64(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x0001020304050607))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_i64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> { + map!(i, be_u64, |x| x as i64) +} + +/// Recognizes a big endian signed 16 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_i128; +/// +/// let parser = |s| { +/// be_i128(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn be_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> { + map!(i, be_u128, |x| x as i128) +} + +/// Recognizes an unsigned 1 byte integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_u8; +/// +/// let parser = |s| { +/// le_u8(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"\x03abcefg"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Error((&[][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_u8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> { + if i.len() < 1 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + Ok((&i[1..], i[0])) + } +} + +/// Recognizes a little endian unsigned 2 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_u16; +/// +/// let parser = |s| { +/// le_u16(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"abcefg"[..], 0x0300))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_u16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> { + if i.len() < 2 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[1] as u16) << 8) + i[0] as u16; + Ok((&i[2..], res)) + } +} + +/// Recognizes a little endian unsigned 3 byte integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_u24; +/// +/// let parser = |s| { +/// le_u24(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05abcefg"), Ok((&b"abcefg"[..], 0x050300))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_u24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 3 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = (i[0] as u32) + ((i[1] as u32) << 8) + ((i[2] as u32) << 16); + Ok((&i[3..], res)) + } +} + +/// Recognizes a little endian unsigned 4 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_u32; +/// +/// let parser = |s| { +/// le_u32(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05\x07abcefg"), Ok((&b"abcefg"[..], 0x07050300))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_u32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 4 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[3] as u32) << 24) + ((i[2] as u32) << 16) + ((i[1] as u32) << 8) + i[0] as u32; + Ok((&i[4..], res)) + } +} + +/// Recognizes a little endian unsigned 8 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_u64; +/// +/// let parser = |s| { +/// le_u64(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x0706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_u64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> { + if i.len() < 8 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[7] as u64) << 56) + ((i[6] as u64) << 48) + ((i[5] as u64) << 40) + ((i[4] as u64) << 32) + ((i[3] as u64) << 24) + + ((i[2] as u64) << 16) + ((i[1] as u64) << 8) + i[0] as u64; + Ok((&i[8..], res)) + } +} + +/// Recognizes a little endian unsigned 16 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_u128; +/// +/// let parser = |s| { +/// le_u128(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn le_u128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> { + if i.len() < 16 { + Err(Err::Error(make_error(i, ErrorKind::Eof))) + } else { + let res = ((i[15] as u128) << 120) + + ((i[14] as u128) << 112) + + ((i[13] as u128) << 104) + + ((i[12] as u128) << 96) + + ((i[11] as u128) << 88) + + ((i[10] as u128) << 80) + + ((i[9] as u128) << 72) + + ((i[8] as u128) << 64) + + ((i[7] as u128) << 56) + + ((i[6] as u128) << 48) + + ((i[5] as u128) << 40) + + ((i[4] as u128) << 32) + + ((i[3] as u128) << 24) + + ((i[2] as u128) << 16) + + ((i[1] as u128) << 8) + + i[0] as u128; + Ok((&i[16..], res)) + } +} + +/// Recognizes a signed 1 byte integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_i8; +/// +/// let parser = |s| { +/// le_i8(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"\x03abcefg"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Error((&[][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_i8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> { + map!(i, le_u8, |x| x as i8) +} + +/// Recognizes a little endian signed 2 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_i16; +/// +/// let parser = |s| { +/// le_i16(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03abcefg"), Ok((&b"abcefg"[..], 0x0300))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> { + map!(i, le_u16, |x| x as i16) +} + +/// Recognizes a little endian signed 3 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_i24; +/// +/// let parser = |s| { +/// le_i24(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05abcefg"), Ok((&b"abcefg"[..], 0x050300))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_i24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + // Same as the unsigned version but we need to sign-extend manually here + map!(i, le_u24, |x| if x & 0x80_00_00 != 0 { + (x | 0xff_00_00_00) as i32 + } else { + x as i32 + }) +} + +/// Recognizes a little endian signed 4 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_i32; +/// +/// let parser = |s| { +/// le_i32(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x03\x05\x07abcefg"), Ok((&b"abcefg"[..], 0x07050300))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_i32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + map!(i, le_u32, |x| x as i32) +} + +/// Recognizes a little endian signed 8 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_i64; +/// +/// let parser = |s| { +/// le_i64(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x0706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_i64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> { + map!(i, le_u64, |x| x as i64) +} + +/// Recognizes a little endian signed 16 bytes integer +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_i128; +/// +/// let parser = |s| { +/// le_i128(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"), Ok((&b"abcefg"[..], 0x07060504030201000706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Error((&[0x01][..], ErrorKind::Eof)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn le_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> { + map!(i, le_u128, |x| x as i128) +} + +/// Recognizes a big endian 4 bytes floating point number +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_f32; +/// +/// let parser = |s| { +/// be_f32(s) +/// }; +/// +/// assert_eq!(parser(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); +/// assert_eq!(parser(b"abc"), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> { + match be_u32(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f32::from_bits(o))), + } +} + +/// Recognizes a big endian 8 bytes floating point number +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::be_f64; +/// +/// let parser = |s| { +/// be_f64(s) +/// }; +/// +/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); +/// assert_eq!(parser(b"abc"), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn be_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> { + match be_u64(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f64::from_bits(o))), + } +} + +/// Recognizes a little endian 4 bytes floating point number +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_f32; +/// +/// let parser = |s| { +/// le_f32(s) +/// }; +/// +/// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5))); +/// assert_eq!(parser(b"abc"), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> { + match le_u32(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f32::from_bits(o))), + } +} + +/// Recognizes a little endian 8 bytes floating point number +/// +/// *complete version*: returns an error if there is not enough input data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::le_f64; +/// +/// let parser = |s| { +/// le_f64(s) +/// }; +/// +/// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..]), Ok((&b""[..], 12.5))); +/// assert_eq!(parser(b"abc"), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); +/// ``` +#[inline] +pub fn le_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> { + match le_u64(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f64::from_bits(o))), + } +} + +/// Recognizes a hex-encoded integer +/// +/// *complete version*: will parse until the end of input if it has less than 8 bytes +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::hex_u32; +/// +/// let parser = |s| { +/// hex_u32(s) +/// }; +/// +/// assert_eq!(parser(b"01AE"), Ok((&b""[..], 0x01AE))); +/// assert_eq!(parser(b"abc"), Ok((&b""[..], 0x0ABC))); +/// assert_eq!(parser(b"ggg"), Err(Err::Error((&b"ggg"[..], ErrorKind::IsA)))); +/// ``` +#[inline] +pub fn hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], u32, E> { + let (i, o) = crate::bytes::complete::is_a(&b"0123456789abcdefABCDEF"[..])(input)?; + // Do not parse more than 8 characters for a u32 + let (parsed, remaining) = if o.len() <= 8 { + (o, i) + } else { + (&input[..8], &input[8..]) + }; + + let res = parsed + .iter() + .rev() + .enumerate() + .map(|(k, &v)| { + let digit = v as char; + digit.to_digit(16).unwrap_or(0) << (k * 4) + }) + .sum(); + + Ok((remaining, res)) +} + +/// Recognizes floating point number in a byte string and returns the corresponding slice +/// +/// *complete version*: can parse until the end of input +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::recognize_float; +/// +/// let parser = |s| { +/// recognize_float(s) +/// }; +/// +/// assert_eq!(parser("11e-1"), Ok(("", "11e-1"))); +/// assert_eq!(parser("123E-02"), Ok(("", "123E-02"))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", "123"))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[allow(unused_imports)] +#[cfg_attr(rustfmt, rustfmt_skip)] +pub fn recognize_float>(input: T) -> IResult +where + T: Slice> + Slice>, + T: Clone + Offset, + T: InputIter, + ::Item: AsChar, + T: InputTakeAtPosition, + ::Item: AsChar, +{ + recognize( + tuple(( + opt(alt((char('+'), char('-')))), + alt(( + map(tuple((digit1, opt(pair(char('.'), opt(digit1))))), |_| ()), + map(tuple((char('.'), digit1)), |_| ()) + )), + opt(tuple(( + alt((char('e'), char('E'))), + opt(alt((char('+'), char('-')))), + cut(digit1) + ))) + )) + )(input) +} + +/// Recognizes floating point number in a byte string and returns a f32 +/// +/// *complete version*: can parse until the end of input +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::float; +/// +/// let parser = |s| { +/// float(s) +/// }; +/// +/// assert_eq!(parser("11e-1"), Ok(("", 1.1))); +/// assert_eq!(parser("123E-02"), Ok(("", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[cfg(not(feature = "lexical"))] +pub fn float>(input: T) -> IResult +where + T: Slice> + Slice>, + T: Clone + Offset, + T: InputIter + InputLength + crate::traits::ParseTo, + ::Item: AsChar, + T: InputTakeAtPosition, + ::Item: AsChar +{ + match recognize_float(input) { + Err(e) => Err(e), + Ok((i, s)) => match s.parse_to() { + Some(n) => Ok((i, n)), + None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float))) + } + } +} + +/// Recognizes floating point number in a byte string and returns a f32 +/// +/// *complete version*: can parse until the end of input +/// +/// this function uses the lexical-core crate for float parsing by default, you +/// can deactivate it by removing the "lexical" feature +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::float; +/// +/// let parser = |s| { +/// float(s) +/// }; +/// +/// assert_eq!(parser("1.1"), Ok(("", 1.1))); +/// assert_eq!(parser("123E-02"), Ok(("", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float)))); +/// ``` +#[cfg(feature = "lexical")] +pub fn float>(input: T) -> IResult +where + T: crate::traits::AsBytes + InputLength + Slice>, +{ + match ::lexical_core::parse_partial(input.as_bytes()) { + Ok((value, processed)) => Ok((input.slice(processed..), value)), + Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float))) + } +} + +/// Recognizes floating point number in a byte string and returns a f64 +/// +/// *complete version*: can parse until the end of input +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::double; +/// +/// let parser = |s| { +/// double(s) +/// }; +/// +/// assert_eq!(parser("11e-1"), Ok(("", 1.1))); +/// assert_eq!(parser("123E-02"), Ok(("", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[cfg(not(feature = "lexical"))] +pub fn double>(input: T) -> IResult +where + T: Slice> + Slice>, + T: Clone + Offset, + T: InputIter + InputLength + crate::traits::ParseTo, + ::Item: AsChar, + T: InputTakeAtPosition, + ::Item: AsChar +{ + match recognize_float(input) { + Err(e) => Err(e), + Ok((i, s)) => match s.parse_to() { + Some(n) => Ok((i, n)), + None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float))) + } + } +} + +/// Recognizes floating point number in a byte string and returns a f64 +/// +/// *complete version*: can parse until the end of input +/// +/// this function uses the lexical-core crate for float parsing by default, you +/// can deactivate it by removing the "lexical" feature +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::complete::double; +/// +/// let parser = |s| { +/// double(s) +/// }; +/// +/// assert_eq!(parser("1.1"), Ok(("", 1.1))); +/// assert_eq!(parser("123E-02"), Ok(("", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float)))); +/// ``` +#[cfg(feature = "lexical")] +pub fn double>(input: T) -> IResult +where + T: crate::traits::AsBytes + InputLength + Slice>, +{ + match ::lexical_core::parse_partial(input.as_bytes()) { + Ok((value, processed)) => Ok((input.slice(processed..), value)), + Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float))) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::internal::Err; + use crate::error::ErrorKind; + + macro_rules! assert_parse( + ($left: expr, $right: expr) => { + let res: $crate::IResult<_, _, (_, ErrorKind)> = $left; + assert_eq!(res, $right); + }; + ); + + #[test] + fn i8_tests() { + assert_parse!(be_i8(&[0x00]), Ok((&b""[..], 0))); + assert_parse!(be_i8(&[0x7f]), Ok((&b""[..], 127))); + assert_parse!(be_i8(&[0xff]), Ok((&b""[..], -1))); + assert_parse!(be_i8(&[0x80]), Ok((&b""[..], -128))); + } + + #[test] + fn i16_tests() { + assert_parse!(be_i16(&[0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(be_i16(&[0x7f, 0xff]), Ok((&b""[..], 32_767_i16))); + assert_parse!(be_i16(&[0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!(be_i16(&[0x80, 0x00]), Ok((&b""[..], -32_768_i16))); + } + + #[test] + fn u24_tests() { + assert_parse!(be_u24(&[0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(be_u24(&[0x00, 0xFF, 0xFF]), Ok((&b""[..], 65_535_u32))); + assert_parse!(be_u24(&[0x12, 0x34, 0x56]), Ok((&b""[..], 1_193_046_u32))); + } + + #[test] + fn i24_tests() { + assert_parse!(be_i24(&[0xFF, 0xFF, 0xFF]), Ok((&b""[..], -1_i32))); + assert_parse!(be_i24(&[0xFF, 0x00, 0x00]), Ok((&b""[..], -65_536_i32))); + assert_parse!(be_i24(&[0xED, 0xCB, 0xAA]), Ok((&b""[..], -1_193_046_i32))); + } + + #[test] + fn i32_tests() { + assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!( + be_i32(&[0x7f, 0xff, 0xff, 0xff]), + Ok((&b""[..], 2_147_483_647_i32)) + ); + assert_parse!(be_i32(&[0xff, 0xff, 0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!( + be_i32(&[0x80, 0x00, 0x00, 0x00]), + Ok((&b""[..], -2_147_483_648_i32)) + ); + } + + #[test] + fn i64_tests() { + assert_parse!( + be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], 9_223_372_036_854_775_807_i64)) + ); + assert_parse!( + be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], -9_223_372_036_854_775_808_i64)) + ); + } + + #[test] + #[cfg(stable_i128)] + fn i128_tests() { + assert_parse!( + be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + be_i128(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], 170_141_183_460_469_231_731_687_303_715_884_105_727_i128)) + ); + assert_parse!( + be_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + be_i128(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], -170_141_183_460_469_231_731_687_303_715_884_105_728_i128)) + ); + } + + #[test] + fn le_i8_tests() { + assert_parse!(le_i8(&[0x00]), Ok((&b""[..], 0))); + assert_parse!(le_i8(&[0x7f]), Ok((&b""[..], 127))); + assert_parse!(le_i8(&[0xff]), Ok((&b""[..], -1))); + assert_parse!(le_i8(&[0x80]), Ok((&b""[..], -128))); + } + + #[test] + fn le_i16_tests() { + assert_parse!(le_i16(&[0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(le_i16(&[0xff, 0x7f]), Ok((&b""[..], 32_767_i16))); + assert_parse!(le_i16(&[0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!(le_i16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16))); + } + + #[test] + fn le_u24_tests() { + assert_parse!(le_u24(&[0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(le_u24(&[0xFF, 0xFF, 0x00]), Ok((&b""[..], 65_535_u32))); + assert_parse!(le_u24(&[0x56, 0x34, 0x12]), Ok((&b""[..], 1_193_046_u32))); + } + + #[test] + fn le_i24_tests() { + assert_parse!(le_i24(&[0xFF, 0xFF, 0xFF]), Ok((&b""[..], -1_i32))); + assert_parse!(le_i24(&[0x00, 0x00, 0xFF]), Ok((&b""[..], -65_536_i32))); + assert_parse!(le_i24(&[0xAA, 0xCB, 0xED]), Ok((&b""[..], -1_193_046_i32))); + } + + #[test] + fn le_i32_tests() { + assert_parse!(le_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!( + le_i32(&[0xff, 0xff, 0xff, 0x7f]), + Ok((&b""[..], 2_147_483_647_i32)) + ); + assert_parse!(le_i32(&[0xff, 0xff, 0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!( + le_i32(&[0x00, 0x00, 0x00, 0x80]), + Ok((&b""[..], -2_147_483_648_i32)) + ); + } + + #[test] + fn le_i64_tests() { + assert_parse!( + le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]), + Ok((&b""[..], 9_223_372_036_854_775_807_i64)) + ); + assert_parse!( + le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), + Ok((&b""[..], -9_223_372_036_854_775_808_i64)) + ); + } + + #[test] + #[cfg(stable_i128)] + fn le_i128_tests() { + assert_parse!( + le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + le_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]), + Ok((&b""[..], 170_141_183_460_469_231_731_687_303_715_884_105_727_i128)) + ); + assert_parse!( + le_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), + Ok((&b""[..], -170_141_183_460_469_231_731_687_303_715_884_105_728_i128)) + ); + } + + #[test] + fn be_f32_tests() { + assert_parse!(be_f32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0_f32))); + assert_parse!( + be_f32(&[0x4d, 0x31, 0x1f, 0xd8]), + Ok((&b""[..], 185_728_392_f32)) + ); + } + + #[test] + fn be_f64_tests() { + assert_parse!( + be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0_f64)) + ); + assert_parse!( + be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00]), + Ok((&b""[..], 185_728_392_f64)) + ); + } + + #[test] + fn le_f32_tests() { + assert_parse!(le_f32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0_f32))); + assert_parse!( + le_f32(&[0xd8, 0x1f, 0x31, 0x4d]), + Ok((&b""[..], 185_728_392_f32)) + ); + } + + #[test] + fn le_f64_tests() { + assert_parse!( + le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0_f64)) + ); + assert_parse!( + le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41]), + Ok((&b""[..], 185_728_392_f64)) + ); + } + + #[test] + fn hex_u32_tests() { + assert_parse!( + hex_u32(&b";"[..]), + Err(Err::Error(error_position!(&b";"[..], ErrorKind::IsA))) + ); + assert_parse!(hex_u32(&b"ff;"[..]), Ok((&b";"[..], 255))); + assert_parse!(hex_u32(&b"1be2;"[..]), Ok((&b";"[..], 7138))); + assert_parse!(hex_u32(&b"c5a31be2;"[..]), Ok((&b";"[..], 3_315_801_058))); + assert_parse!(hex_u32(&b"C5A31be2;"[..]), Ok((&b";"[..], 3_315_801_058))); + assert_parse!(hex_u32(&b"00c5a31be2;"[..]), Ok((&b"e2;"[..], 12_952_347))); + assert_parse!( + hex_u32(&b"c5a31be201;"[..]), + Ok((&b"01;"[..], 3_315_801_058)) + ); + assert_parse!(hex_u32(&b"ffffffff;"[..]), Ok((&b";"[..], 4_294_967_295))); + assert_parse!(hex_u32(&b"0x1be2;"[..]), Ok((&b"x1be2;"[..], 0))); + assert_parse!(hex_u32(&b"12af"[..]), Ok((&b""[..], 0x12af))); + } + + #[test] + #[cfg(feature = "std")] + fn float_test() { + let mut test_cases = vec![ + "+3.14", + "3.14", + "-3.14", + "0", + "0.0", + "1.", + ".789", + "-.5", + "1e7", + "-1E-7", + ".3e-2", + "1.e4", + "1.2e4", + "12.34", + "-1.234E-12", + "-1.234e-12", + ]; + + for test in test_cases.drain(..) { + let expected32 = str::parse::(test).unwrap(); + let expected64 = str::parse::(test).unwrap(); + + println!("now parsing: {} -> {}", test, expected32); + + let larger = format!("{}", test); + assert_parse!(recognize_float(&larger[..]), Ok(("", test))); + + assert_parse!(float(larger.as_bytes()), Ok((&b""[..], expected32))); + assert_parse!(float(&larger[..]), Ok(("", expected32))); + + assert_parse!(double(larger.as_bytes()), Ok((&b""[..], expected64))); + assert_parse!(double(&larger[..]), Ok(("", expected64))); + } + + let remaining_exponent = "-1.234E-"; + assert_parse!( + recognize_float(remaining_exponent), + Err(Err::Failure(("", ErrorKind::Digit))) + ); + } + +} diff --git a/third_party/rust/nom/src/number/macros.rs b/third_party/rust/nom/src/number/macros.rs new file mode 100644 index 0000000000..cb8c2a9470 --- /dev/null +++ b/third_party/rust/nom/src/number/macros.rs @@ -0,0 +1,265 @@ +//! parsers recognizing numbers + +/// if the parameter is nom::Endianness::Big, parse a big endian u16 integer, +/// otherwise a little endian u16 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be, u16!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0001))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// +/// named!(le, u16!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! u16 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_u16($i) } else { $crate::number::streaming::le_u16($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian u32 integer, +/// otherwise a little endian u32 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be, u32!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x00010203))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// +/// named!(le, u32!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x03020100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! u32 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_u32($i) } else { $crate::number::streaming::le_u32($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian u64 integer, +/// otherwise a little endian u64 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be, u64!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0001020304050607))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// +/// named!(le, u64!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0706050403020100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! u64 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_u64($i) } else { $crate::number::streaming::le_u64($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian u128 integer, +/// otherwise a little endian u128 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be, u128!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x00010203040506070809101112131415))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// +/// named!(le, u128!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x15141312111009080706050403020100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +#[cfg(stable_i128)] +macro_rules! u128 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_u128($i) } else { $crate::number::streaming::le_u128($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian i16 integer, +/// otherwise a little endian i16 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be, i16!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0001))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// +/// named!(le, i16!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! i16 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_i16($i) } else { $crate::number::streaming::le_i16($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian i32 integer, +/// otherwise a little endian i32 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be, i32!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x00010203))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// +/// named!(le, i32!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x03020100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! i32 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_i32($i) } else { $crate::number::streaming::le_i32($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian i64 integer, +/// otherwise a little endian i64 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be, i64!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0001020304050607))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// +/// named!(le, i64!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0706050403020100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! i64 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_i64($i) } else { $crate::number::streaming::le_i64($i) } } );); + +/// if the parameter is nom::Endianness::Big, parse a big endian i64 integer, +/// otherwise a little endian i64 integer +/// +/// ```rust +/// # #[macro_use] extern crate nom; +/// # use nom::{Err, Needed}; +/// use nom::number::Endianness; +/// +/// # fn main() { +/// named!(be, i128!(Endianness::Big)); +/// +/// assert_eq!(be(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x00010203040506070809101112131415))); +/// assert_eq!(be(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// +/// named!(le, i128!(Endianness::Little)); +/// +/// assert_eq!(le(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x15141312111009080706050403020100))); +/// assert_eq!(le(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +#[cfg(stable_i128)] +macro_rules! i128 ( ($i:expr, $e:expr) => ( {if $crate::number::Endianness::Big == $e { $crate::number::streaming::be_i128($i) } else { $crate::number::streaming::le_i128($i) } } );); + +#[cfg(test)] +mod tests { + use crate::number::Endianness; + + #[test] + fn configurable_endianness() { + named!(be_tst16, u16!(Endianness::Big)); + named!(le_tst16, u16!(Endianness::Little)); + assert_eq!(be_tst16(&[0x80, 0x00]), Ok((&b""[..], 32_768_u16))); + assert_eq!(le_tst16(&[0x80, 0x00]), Ok((&b""[..], 128_u16))); + + named!(be_tst32, u32!(Endianness::Big)); + named!(le_tst32, u32!(Endianness::Little)); + assert_eq!( + be_tst32(&[0x12, 0x00, 0x60, 0x00]), + Ok((&b""[..], 302_014_464_u32)) + ); + assert_eq!( + le_tst32(&[0x12, 0x00, 0x60, 0x00]), + Ok((&b""[..], 6_291_474_u32)) + ); + + named!(be_tst64, u64!(Endianness::Big)); + named!(le_tst64, u64!(Endianness::Little)); + assert_eq!( + be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), + Ok((&b""[..], 1_297_142_246_100_992_000_u64)) + ); + assert_eq!( + le_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), + Ok((&b""[..], 36_028_874_334_666_770_u64)) + ); + + named!(be_tsti16, i16!(Endianness::Big)); + named!(le_tsti16, i16!(Endianness::Little)); + assert_eq!(be_tsti16(&[0x00, 0x80]), Ok((&b""[..], 128_i16))); + assert_eq!(le_tsti16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16))); + + named!(be_tsti32, i32!(Endianness::Big)); + named!(le_tsti32, i32!(Endianness::Little)); + assert_eq!( + be_tsti32(&[0x00, 0x12, 0x60, 0x00]), + Ok((&b""[..], 1_204_224_i32)) + ); + assert_eq!( + le_tsti32(&[0x00, 0x12, 0x60, 0x00]), + Ok((&b""[..], 6_296_064_i32)) + ); + + named!(be_tsti64, i64!(Endianness::Big)); + named!(le_tsti64, i64!(Endianness::Little)); + assert_eq!( + be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), + Ok((&b""[..], 71_881_672_479_506_432_i64)) + ); + assert_eq!( + le_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), + Ok((&b""[..], 36_028_874_334_732_032_i64)) + ); + } + + //FIXME + /* + #[test] + #[cfg(feature = "std")] + fn manual_configurable_endianness_test() { + let x = 1; + let int_parse: Box IResult<&[u8], u16, (&[u8], ErrorKind)>> = if x == 2 { + Box::new(be_u16) + } else { + Box::new(le_u16) + }; + println!("{:?}", int_parse(&b"3"[..])); + assert_eq!(int_parse(&[0x80, 0x00]), Ok((&b""[..], 128_u16))); + } + */ +} diff --git a/third_party/rust/nom/src/number/mod.rs b/third_party/rust/nom/src/number/mod.rs new file mode 100644 index 0000000000..9be4c38a45 --- /dev/null +++ b/third_party/rust/nom/src/number/mod.rs @@ -0,0 +1,17 @@ +//! parsers recognizing numbers + +#[macro_use] +mod macros; + +pub mod streaming; +pub mod complete; + +/// Configurable endianness +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Endianness { + /// big endian + Big, + /// little endian + Little, +} + diff --git a/third_party/rust/nom/src/number/streaming.rs b/third_party/rust/nom/src/number/streaming.rs new file mode 100644 index 0000000000..15a89a4976 --- /dev/null +++ b/third_party/rust/nom/src/number/streaming.rs @@ -0,0 +1,1207 @@ +//! parsers recognizing numbers, streaming version + +use crate::internal::*; +use crate::error::{ErrorKind, ParseError}; +use crate::traits::{AsChar, InputIter, InputLength, InputTakeAtPosition}; +use crate::lib::std::ops::{RangeFrom, RangeTo}; +use crate::traits::{Offset, Slice}; +use crate::character::streaming::{char, digit1}; +use crate::sequence::{pair, tuple}; +use crate::combinator::{cut, map, opt, recognize}; +use crate::branch::alt; + +/// Recognizes an unsigned 1 byte integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_u8; +/// +/// let parser = be_u8::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +#[inline] +pub fn be_u8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> { + if i.len() < 1 { + Err(Err::Incomplete(Needed::Size(1))) + } else { + Ok((&i[1..], i[0])) + } +} + +/// Recognizes a big endian unsigned 2 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_u16; +/// +/// let parser = |s| { +/// be_u16::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0001))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// ``` +#[inline] +pub fn be_u16<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> { + if i.len() < 2 { + Err(Err::Incomplete(Needed::Size(2))) + } else { + let res = ((i[0] as u16) << 8) + i[1] as u16; + Ok((&i[2..], res)) + } +} + +/// Recognizes a big endian unsigned 3 byte integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_u24; +/// +/// let parser = |s| { +/// be_u24::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x000102))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(3)))); +/// ``` +#[inline] +pub fn be_u24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 3 { + Err(Err::Incomplete(Needed::Size(3))) + } else { + let res = ((i[0] as u32) << 16) + ((i[1] as u32) << 8) + (i[2] as u32); + Ok((&i[3..], res)) + } +} + +/// Recognizes a big endian unsigned 4 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_u32; +/// +/// let parser = |s| { +/// be_u32::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x00010203))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// ``` +#[inline] +pub fn be_u32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 4 { + Err(Err::Incomplete(Needed::Size(4))) + } else { + let res = ((i[0] as u32) << 24) + ((i[1] as u32) << 16) + ((i[2] as u32) << 8) + i[3] as u32; + Ok((&i[4..], res)) + } +} + +/// Recognizes a big endian unsigned 8 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_u64; +/// +/// let parser = |s| { +/// be_u64::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0001020304050607))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// ``` +#[inline] +pub fn be_u64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> { + if i.len() < 8 { + Err(Err::Incomplete(Needed::Size(8))) + } else { + let res = ((i[0] as u64) << 56) + ((i[1] as u64) << 48) + ((i[2] as u64) << 40) + ((i[3] as u64) << 32) + ((i[4] as u64) << 24) + + ((i[5] as u64) << 16) + ((i[6] as u64) << 8) + i[7] as u64; + Ok((&i[8..], res)) + } +} + +/// Recognizes a big endian unsigned 16 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_u128; +/// +/// let parser = |s| { +/// be_u128::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x00010203040506070809101112131415))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn be_u128<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> { + if i.len() < 16 { + Err(Err::Incomplete(Needed::Size(16))) + } else { + let res = ((i[0] as u128) << 120) + + ((i[1] as u128) << 112) + + ((i[2] as u128) << 104) + + ((i[3] as u128) << 96) + + ((i[4] as u128) << 88) + + ((i[5] as u128) << 80) + + ((i[6] as u128) << 72) + + ((i[7] as u128) << 64) + + ((i[8] as u128) << 56) + + ((i[9] as u128) << 48) + + ((i[10] as u128) << 40) + + ((i[11] as u128) << 32) + + ((i[12] as u128) << 24) + + ((i[13] as u128) << 16) + + ((i[14] as u128) << 8) + + i[15] as u128; + Ok((&i[16..], res)) + } +} + +/// Recognizes a signed 1 byte integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_i8; +/// +/// let parser = be_i8::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +#[inline] +pub fn be_i8<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> { + map!(i, be_u8, |x| x as i8) +} + +/// Recognizes a big endian signed 2 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_i16; +/// +/// let parser = be_i16::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0001))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(2)))); +/// ``` +#[inline] +pub fn be_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> { + map!(i, be_u16, |x| x as i16) +} + +/// Recognizes a big endian signed 3 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_i24; +/// +/// let parser = be_i24::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x000102))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(3)))); +/// ``` +#[inline] +pub fn be_i24<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + // Same as the unsigned version but we need to sign-extend manually here + map!(i, be_u24, |x| if x & 0x80_00_00 != 0 { + (x | 0xff_00_00_00) as i32 + } else { + x as i32 + }) +} + +/// Recognizes a big endian signed 4 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_i32; +/// +/// let parser = be_i32::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x00010203))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(4)))); +/// ``` +#[inline] +pub fn be_i32<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + map!(i, be_u32, |x| x as i32) +} + +/// Recognizes a big endian signed 8 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_i64; +/// +/// let parser = be_i64::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0001020304050607))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// ``` +#[inline] +pub fn be_i64<'a, E: ParseError<&'a[u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> { + map!(i, be_u64, |x| x as i64) +} + +/// Recognizes a big endian signed 16 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_i128; +/// +/// let parser = be_i128::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x00010203040506070809101112131415))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn be_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> { + map!(i, be_u128, |x| x as i128) +} + +/// Recognizes an unsigned 1 byte integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_u8; +/// +/// let parser = le_u8::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +#[inline] +pub fn le_u8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u8, E> { + if i.len() < 1 { + Err(Err::Incomplete(Needed::Size(1))) + } else { + Ok((&i[1..], i[0])) + } +} + +/// Recognizes a little endian unsigned 2 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_u16; +/// +/// let parser = |s| { +/// le_u16::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// ``` +#[inline] +pub fn le_u16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u16, E> { + if i.len() < 2 { + Err(Err::Incomplete(Needed::Size(2))) + } else { + let res = ((i[1] as u16) << 8) + i[0] as u16; + Ok((&i[2..], res)) + } +} + +/// Recognizes a little endian unsigned 3 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_u24; +/// +/// let parser = |s| { +/// le_u24::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(3)))); +/// ``` +#[inline] +pub fn le_u24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 3 { + Err(Err::Incomplete(Needed::Size(3))) + } else { + let res = (i[0] as u32) + ((i[1] as u32) << 8) + ((i[2] as u32) << 16); + Ok((&i[3..], res)) + } +} + +/// Recognizes a little endian unsigned 4 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_u32; +/// +/// let parser = |s| { +/// le_u32::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x03020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// ``` +#[inline] +pub fn le_u32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u32, E> { + if i.len() < 4 { + Err(Err::Incomplete(Needed::Size(4))) + } else { + let res = ((i[3] as u32) << 24) + ((i[2] as u32) << 16) + ((i[1] as u32) << 8) + i[0] as u32; + Ok((&i[4..], res)) + } +} + +/// Recognizes a little endian unsigned 8 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_u64; +/// +/// let parser = |s| { +/// le_u64::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// ``` +#[inline] +pub fn le_u64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u64, E> { + if i.len() < 8 { + Err(Err::Incomplete(Needed::Size(8))) + } else { + let res = ((i[7] as u64) << 56) + ((i[6] as u64) << 48) + ((i[5] as u64) << 40) + ((i[4] as u64) << 32) + ((i[3] as u64) << 24) + + ((i[2] as u64) << 16) + ((i[1] as u64) << 8) + i[0] as u64; + Ok((&i[8..], res)) + } +} + +/// Recognizes a little endian unsigned 16 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_u128; +/// +/// let parser = |s| { +/// le_u128::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x15141312111009080706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn le_u128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], u128, E> { + if i.len() < 16 { + Err(Err::Incomplete(Needed::Size(16))) + } else { + let res = ((i[15] as u128) << 120) + + ((i[14] as u128) << 112) + + ((i[13] as u128) << 104) + + ((i[12] as u128) << 96) + + ((i[11] as u128) << 88) + + ((i[10] as u128) << 80) + + ((i[9] as u128) << 72) + + ((i[8] as u128) << 64) + + ((i[7] as u128) << 56) + + ((i[6] as u128) << 48) + + ((i[5] as u128) << 40) + + ((i[4] as u128) << 32) + + ((i[3] as u128) << 24) + + ((i[2] as u128) << 16) + + ((i[1] as u128) << 8) + + i[0] as u128; + Ok((&i[16..], res)) + } +} + +/// Recognizes a signed 1 byte integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_i8; +/// +/// let parser = le_i8::<(_, ErrorKind)>; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"\x01abcd"[..], 0x00))); +/// assert_eq!(parser(b""), Err(Err::Incomplete(Needed::Size(1)))); +/// ``` +#[inline] +pub fn le_i8<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i8, E> { + map!(i, le_u8, |x| x as i8) +} + +/// Recognizes a little endian signed 2 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_i16; +/// +/// let parser = |s| { +/// le_i16::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01abcd"), Ok((&b"abcd"[..], 0x0100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(2)))); +/// ``` +#[inline] +pub fn le_i16<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i16, E> { + map!(i, le_u16, |x| x as i16) +} + +/// Recognizes a little endian signed 3 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_i24; +/// +/// let parser = |s| { +/// le_i24::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02abcd"), Ok((&b"abcd"[..], 0x020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(3)))); +/// ``` +#[inline] +pub fn le_i24<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + // Same as the unsigned version but we need to sign-extend manually here + map!(i, le_u24, |x| if x & 0x80_00_00 != 0 { + (x | 0xff_00_00_00) as i32 + } else { + x as i32 + }) +} + +/// Recognizes a little endian signed 4 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_i32; +/// +/// let parser = |s| { +/// le_i32::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03abcd"), Ok((&b"abcd"[..], 0x03020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(4)))); +/// ``` +#[inline] +pub fn le_i32<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i32, E> { + map!(i, le_u32, |x| x as i32) +} + +/// Recognizes a little endian signed 8 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_i64; +/// +/// let parser = |s| { +/// le_i64::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07abcd"), Ok((&b"abcd"[..], 0x0706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(8)))); +/// ``` +#[inline] +pub fn le_i64<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i64, E> { + map!(i, le_u64, |x| x as i64) +} + +/// Recognizes a little endian signed 16 bytes integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_i128; +/// +/// let parser = |s| { +/// le_i128::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15abcd"), Ok((&b"abcd"[..], 0x15141312111009080706050403020100))); +/// assert_eq!(parser(b"\x01"), Err(Err::Incomplete(Needed::Size(16)))); +/// ``` +#[inline] +#[cfg(stable_i128)] +pub fn le_i128<'a, E: ParseError<&'a [u8]>>(i: &'a[u8]) -> IResult<&'a[u8], i128, E> { + map!(i, le_u128, |x| x as i128) +} + +/// Recognizes a big endian 4 bytes floating point number +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_f32; +/// +/// let parser = |s| { +/// be_f32::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00][..]), Ok((&b""[..], 2.640625))); +/// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(4)))); +/// ``` +#[inline] +pub fn be_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> { + match be_u32(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f32::from_bits(o))), + } +} + +/// Recognizes a big endian 8 bytes floating point number +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::be_f64; +/// +/// let parser = |s| { +/// be_f64::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); +/// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(8)))); +/// ``` +#[inline] +pub fn be_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> { + match be_u64(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f64::from_bits(o))), + } +} + +/// Recognizes a little endian 4 bytes floating point number +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_f32; +/// +/// let parser = |s| { +/// le_f32::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(&[0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 12.5))); +/// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(4)))); +/// ``` +#[inline] +pub fn le_f32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f32, E> { + match le_u32(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f32::from_bits(o))), + } +} + +/// Recognizes a little endian 8 bytes floating point number +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::le_f64; +/// +/// let parser = |s| { +/// le_f64::<(_, ErrorKind)>(s) +/// }; +/// +/// assert_eq!(parser(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x41][..]), Ok((&b""[..], 3145728.0))); +/// assert_eq!(parser(&[0x01]), Err(Err::Incomplete(Needed::Size(8)))); +/// ``` +#[inline] +pub fn le_f64<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], f64, E> { + match le_u64(input) { + Err(e) => Err(e), + Ok((i, o)) => Ok((i, f64::from_bits(o))), + } +} + +/// Recognizes a hex-encoded integer +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if there is not enough data +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::hex_u32; +/// +/// let parser = |s| { +/// hex_u32(s) +/// }; +/// +/// assert_eq!(parser(b"01AE;"), Ok((&b";"[..], 0x01AE))); +/// assert_eq!(parser(b"abc"), Err(Err::Incomplete(Needed::Size(1)))); +/// assert_eq!(parser(b"ggg"), Err(Err::Error((&b"ggg"[..], ErrorKind::IsA)))); +/// ``` +#[inline] +pub fn hex_u32<'a, E: ParseError<&'a [u8]>>(input: &'a[u8]) -> IResult<&'a[u8], u32, E> { + let (i, o) = crate::bytes::streaming::is_a(&b"0123456789abcdefABCDEF"[..])(input)?; + + // Do not parse more than 8 characters for a u32 + let (parsed, remaining) = if o.len() <= 8 { + (o, i) + } else { + (&input[..8], &input[8..]) + }; + + let res = parsed + .iter() + .rev() + .enumerate() + .map(|(k, &v)| { + let digit = v as char; + digit.to_digit(16).unwrap_or(0) << (k * 4) + }) + .sum(); + + Ok((remaining, res)) +} + +/// Recognizes a floating point number in text format and returns the corresponding part of the input +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::recognize_float; +/// +/// let parser = |s| { +/// recognize_float(s) +/// }; +/// +/// assert_eq!(parser("11e-1;"), Ok((";", "11e-1"))); +/// assert_eq!(parser("123E-02;"), Ok((";", "123E-02"))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", "123"))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[allow(unused_imports)] +#[cfg_attr(rustfmt, rustfmt_skip)] +pub fn recognize_float>(input: T) -> IResult +where + T: Slice> + Slice>, + T: Clone + Offset, + T: InputIter, + ::Item: AsChar, + T: InputTakeAtPosition, + ::Item: AsChar +{ + recognize( + tuple(( + opt(alt((char('+'), char('-')))), + alt(( + map(tuple((digit1, opt(pair(char('.'), opt(digit1))))), |_| ()), + map(tuple((char('.'), digit1)), |_| ()) + )), + opt(tuple(( + alt((char('e'), char('E'))), + opt(alt((char('+'), char('-')))), + cut(digit1) + ))) + )) + )(input) +} + +/// Recognizes floating point number in a byte string and returns a f32 +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::float; +/// +/// let parser = |s| { +/// float(s) +/// }; +/// +/// assert_eq!(parser("11e-1;"), Ok((";", 1.1))); +/// assert_eq!(parser("123E-02;"), Ok((";", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[cfg(not(feature = "lexical"))] +pub fn float>(input: T) -> IResult +where + T: Slice> + Slice>, + T: Clone + Offset, + T: InputIter + InputLength + crate::traits::ParseTo, + ::Item: AsChar, + T: InputTakeAtPosition, + ::Item: AsChar +{ + match recognize_float(input) { + Err(e) => Err(e), + Ok((i, s)) => match s.parse_to() { + Some(n) => Ok((i, n)), + None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float))) + } + } +} + +/// Recognizes floating point number in a byte string and returns a f32 +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::float; +/// +/// let parser = |s| { +/// float(s) +/// }; +/// +/// assert_eq!(parser("11e-1;"), Ok((";", 1.1))); +/// assert_eq!(parser("123E-02;"), Ok((";", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float)))); +/// ``` +/// +/// this function uses the lexical-core crate for float parsing by default, you +/// can deactivate it by removing the "lexical" feature +#[cfg(feature = "lexical")] +pub fn float>(input: T) -> IResult +where + T: crate::traits::AsBytes + InputLength + Slice>, +{ + match ::lexical_core::parse_partial(input.as_bytes()) { + Ok((value, processed)) => { + if processed == input.input_len() { + Err(Err::Incomplete(Needed::Unknown)) + } else { + Ok((input.slice(processed..), value)) + } + }, + Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float))) + } +} + +/// Recognizes floating point number in a byte string and returns a f64 +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::double; +/// +/// let parser = |s| { +/// double(s) +/// }; +/// +/// assert_eq!(parser("11e-1;"), Ok((";", 1.1))); +/// assert_eq!(parser("123E-02;"), Ok((";", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// ``` +#[cfg(not(feature = "lexical"))] +pub fn double>(input: T) -> IResult +where + T: Slice> + Slice>, + T: Clone + Offset, + T: InputIter + InputLength + crate::traits::ParseTo, + ::Item: AsChar, + T: InputTakeAtPosition, + ::Item: AsChar +{ + match recognize_float(input) { + Err(e) => Err(e), + Ok((i, s)) => match s.parse_to() { + Some(n) => Ok((i, n)), + None => Err(Err::Error(E::from_error_kind(i, ErrorKind::Float))) + } + } +} + +/// Recognizes floating point number in a byte string and returns a f64 +/// +/// *streaming version*: will return `Err(nom::Err::Incomplete(_))` if it reaches the end of input +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::number::streaming::double; +/// +/// let parser = |s| { +/// double(s) +/// }; +/// +/// assert_eq!(parser("11e-1;"), Ok((";", 1.1))); +/// assert_eq!(parser("123E-02;"), Ok((";", 1.23))); +/// assert_eq!(parser("123K-01"), Ok(("K-01", 123.0))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Float)))); +/// ``` +/// +/// this function uses the lexical-core crate for float parsing by default, you +/// can deactivate it by removing the "lexical" feature +#[cfg(feature = "lexical")] +pub fn double>(input: T) -> IResult +where + T: crate::traits::AsBytes + InputLength + Slice>, +{ + match ::lexical_core::parse_partial(input.as_bytes()) { + Ok((value, processed)) => { + if processed == input.input_len() { + Err(Err::Incomplete(Needed::Unknown)) + } else { + Ok((input.slice(processed..), value)) + } + }, + Err(_) => Err(Err::Error(E::from_error_kind(input, ErrorKind::Float))) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::internal::{Err, Needed}; + use crate::error::ErrorKind; + + macro_rules! assert_parse( + ($left: expr, $right: expr) => { + let res: $crate::IResult<_, _, (_, ErrorKind)> = $left; + assert_eq!(res, $right); + }; + ); + + #[test] + fn i8_tests() { + assert_parse!(be_i8(&[0x00]), Ok((&b""[..], 0))); + assert_parse!(be_i8(&[0x7f]), Ok((&b""[..], 127))); + assert_parse!(be_i8(&[0xff]), Ok((&b""[..], -1))); + assert_parse!(be_i8(&[0x80]), Ok((&b""[..], -128))); + } + + #[test] + fn i16_tests() { + assert_parse!(be_i16(&[0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(be_i16(&[0x7f, 0xff]), Ok((&b""[..], 32_767_i16))); + assert_parse!(be_i16(&[0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!(be_i16(&[0x80, 0x00]), Ok((&b""[..], -32_768_i16))); + } + + #[test] + fn u24_tests() { + assert_parse!(be_u24(&[0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(be_u24(&[0x00, 0xFF, 0xFF]), Ok((&b""[..], 65_535_u32))); + assert_parse!(be_u24(&[0x12, 0x34, 0x56]), Ok((&b""[..], 1_193_046_u32))); + } + + #[test] + fn i24_tests() { + assert_parse!(be_i24(&[0xFF, 0xFF, 0xFF]), Ok((&b""[..], -1_i32))); + assert_parse!(be_i24(&[0xFF, 0x00, 0x00]), Ok((&b""[..], -65_536_i32))); + assert_parse!(be_i24(&[0xED, 0xCB, 0xAA]), Ok((&b""[..], -1_193_046_i32))); + } + + #[test] + fn i32_tests() { + assert_parse!(be_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!( + be_i32(&[0x7f, 0xff, 0xff, 0xff]), + Ok((&b""[..], 2_147_483_647_i32)) + ); + assert_parse!(be_i32(&[0xff, 0xff, 0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!( + be_i32(&[0x80, 0x00, 0x00, 0x00]), + Ok((&b""[..], -2_147_483_648_i32)) + ); + } + + #[test] + fn i64_tests() { + assert_parse!( + be_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + be_i64(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], 9_223_372_036_854_775_807_i64)) + ); + assert_parse!( + be_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + be_i64(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], -9_223_372_036_854_775_808_i64)) + ); + } + + #[test] + #[cfg(stable_i128)] + fn i128_tests() { + assert_parse!( + be_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + be_i128(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], 170_141_183_460_469_231_731_687_303_715_884_105_727_i128)) + ); + assert_parse!( + be_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + be_i128(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], -170_141_183_460_469_231_731_687_303_715_884_105_728_i128)) + ); + } + + #[test] + fn le_i8_tests() { + assert_parse!(le_i8(&[0x00]), Ok((&b""[..], 0))); + assert_parse!(le_i8(&[0x7f]), Ok((&b""[..], 127))); + assert_parse!(le_i8(&[0xff]), Ok((&b""[..], -1))); + assert_parse!(le_i8(&[0x80]), Ok((&b""[..], -128))); + } + + #[test] + fn le_i16_tests() { + assert_parse!(le_i16(&[0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(le_i16(&[0xff, 0x7f]), Ok((&b""[..], 32_767_i16))); + assert_parse!(le_i16(&[0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!(le_i16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16))); + } + + #[test] + fn le_u24_tests() { + assert_parse!(le_u24(&[0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!(le_u24(&[0xFF, 0xFF, 0x00]), Ok((&b""[..], 65_535_u32))); + assert_parse!(le_u24(&[0x56, 0x34, 0x12]), Ok((&b""[..], 1_193_046_u32))); + } + + #[test] + fn le_i24_tests() { + assert_parse!(le_i24(&[0xFF, 0xFF, 0xFF]), Ok((&b""[..], -1_i32))); + assert_parse!(le_i24(&[0x00, 0x00, 0xFF]), Ok((&b""[..], -65_536_i32))); + assert_parse!(le_i24(&[0xAA, 0xCB, 0xED]), Ok((&b""[..], -1_193_046_i32))); + } + + #[test] + fn le_i32_tests() { + assert_parse!(le_i32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0))); + assert_parse!( + le_i32(&[0xff, 0xff, 0xff, 0x7f]), + Ok((&b""[..], 2_147_483_647_i32)) + ); + assert_parse!(le_i32(&[0xff, 0xff, 0xff, 0xff]), Ok((&b""[..], -1))); + assert_parse!( + le_i32(&[0x00, 0x00, 0x00, 0x80]), + Ok((&b""[..], -2_147_483_648_i32)) + ); + } + + #[test] + fn le_i64_tests() { + assert_parse!( + le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]), + Ok((&b""[..], 9_223_372_036_854_775_807_i64)) + ); + assert_parse!( + le_i64(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + le_i64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), + Ok((&b""[..], -9_223_372_036_854_775_808_i64)) + ); + } + + #[test] + #[cfg(stable_i128)] + fn le_i128_tests() { + assert_parse!( + le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0)) + ); + assert_parse!( + le_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]), + Ok((&b""[..], 170_141_183_460_469_231_731_687_303_715_884_105_727_i128)) + ); + assert_parse!( + le_i128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), + Ok((&b""[..], -1)) + ); + assert_parse!( + le_i128(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), + Ok((&b""[..], -170_141_183_460_469_231_731_687_303_715_884_105_728_i128)) + ); + } + + #[test] + fn be_f32_tests() { + assert_parse!(be_f32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0_f32))); + assert_parse!( + be_f32(&[0x4d, 0x31, 0x1f, 0xd8]), + Ok((&b""[..], 185_728_392_f32)) + ); + } + + #[test] + fn be_f64_tests() { + assert_parse!( + be_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0_f64)) + ); + assert_parse!( + be_f64(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00]), + Ok((&b""[..], 185_728_392_f64)) + ); + } + + #[test] + fn le_f32_tests() { + assert_parse!(le_f32(&[0x00, 0x00, 0x00, 0x00]), Ok((&b""[..], 0_f32))); + assert_parse!( + le_f32(&[0xd8, 0x1f, 0x31, 0x4d]), + Ok((&b""[..], 185_728_392_f32)) + ); + } + + #[test] + fn le_f64_tests() { + assert_parse!( + le_f64(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), + Ok((&b""[..], 0_f64)) + ); + assert_parse!( + le_f64(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41]), + Ok((&b""[..], 185_728_392_f64)) + ); + } + + #[test] + fn hex_u32_tests() { + assert_parse!( + hex_u32(&b";"[..]), + Err(Err::Error(error_position!(&b";"[..], ErrorKind::IsA))) + ); + assert_parse!(hex_u32(&b"ff;"[..]), Ok((&b";"[..], 255))); + assert_parse!(hex_u32(&b"1be2;"[..]), Ok((&b";"[..], 7138))); + assert_parse!(hex_u32(&b"c5a31be2;"[..]), Ok((&b";"[..], 3_315_801_058))); + assert_parse!(hex_u32(&b"C5A31be2;"[..]), Ok((&b";"[..], 3_315_801_058))); + assert_parse!(hex_u32(&b"00c5a31be2;"[..]), Ok((&b"e2;"[..], 12_952_347))); + assert_parse!( + hex_u32(&b"c5a31be201;"[..]), + Ok((&b"01;"[..], 3_315_801_058)) + ); + assert_parse!(hex_u32(&b"ffffffff;"[..]), Ok((&b";"[..], 4_294_967_295))); + assert_parse!(hex_u32(&b"0x1be2;"[..]), Ok((&b"x1be2;"[..], 0))); + assert_parse!(hex_u32(&b"12af"[..]), Err(Err::Incomplete(Needed::Size(1)))); + } + + #[test] + #[cfg(feature = "std")] + fn float_test() { + let mut test_cases = vec![ + "+3.14", + "3.14", + "-3.14", + "0", + "0.0", + "1.", + ".789", + "-.5", + "1e7", + "-1E-7", + ".3e-2", + "1.e4", + "1.2e4", + "12.34", + "-1.234E-12", + "-1.234e-12", + ]; + + for test in test_cases.drain(..) { + let expected32 = str::parse::(test).unwrap(); + let expected64 = str::parse::(test).unwrap(); + + println!("now parsing: {} -> {}", test, expected32); + + let larger = format!("{};", test); + assert_parse!(recognize_float(&larger[..]), Ok((";", test))); + + assert_parse!(float(larger.as_bytes()), Ok((&b";"[..], expected32))); + assert_parse!(float(&larger[..]), Ok((";", expected32))); + + assert_parse!(double(larger.as_bytes()), Ok((&b";"[..], expected64))); + assert_parse!(double(&larger[..]), Ok((";", expected64))); + } + + let remaining_exponent = "-1.234E-"; + assert_parse!( + recognize_float(remaining_exponent), + Err(Err::Incomplete(Needed::Size(1))) + ); + } + +} diff --git a/third_party/rust/nom/src/regexp.rs b/third_party/rust/nom/src/regexp.rs new file mode 100644 index 0000000000..fd165e1bbf --- /dev/null +++ b/third_party/rust/nom/src/regexp.rs @@ -0,0 +1,1090 @@ +#[doc(hidden)] +#[macro_export] +macro_rules! regex ( + ($re: ident, $s:expr) => ( + lazy_static! { + static ref $re: $crate::lib::regex::Regex = $crate::lib::regex::Regex::new($s).unwrap(); + } + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! regex_bytes ( + ($re: ident, $s:expr) => ( + lazy_static! { + static ref $re: $crate::lib::regex::bytes::Regex = $crate::lib::regex::bytes::Regex::new($s).unwrap(); + } + ); +); + +/// `re_match!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the whole input if a match is found +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_match ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::InputLength; + use $crate::Slice; + let re = $crate::lib::regex::Regex::new($re).unwrap(); + if re.is_match(&$i) { + Ok(($i.slice($i.input_len()..), $i)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatch))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_match_static!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the whole input if a match is found. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_match_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::InputLength; + use $crate::Slice; + regex!(RE, $re); + if RE.is_match(&$i) { + Ok(($i.slice($i.input_len()..), $i)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatch))); + res + } + } + ) +); + +/// `re_bytes_match!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the whole input if a match is found +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_match ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::InputLength; + use $crate::Slice; + let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); + if re.is_match(&$i) { + Ok(($i.slice($i.input_len()..), $i)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatch))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_match_static!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the whole input if a match is found. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_match_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::InputLength; + use $crate::Slice; + regex_bytes!(RE, $re); + if RE.is_match(&$i) { + Ok(($i.slice($i.input_len()..), $i)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatch))); + res + } + } + ) +); + +/// `re_find!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the first match +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_find ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::Regex::new($re).unwrap(); + if let Some(m) = re.find(&$i) { + Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end()))) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpFind))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_find_static!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the first match. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_find_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex!(RE, $re); + if let Some(m) = RE.find(&$i) { + Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end()))) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpFind))); + res + } + } + + ) +); + +/// `re_bytes_find!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the first match +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_find ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); + if let Some(m) = re.find(&$i) { + Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end()))) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpFind))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_find!(regexp) => &[T] -> IResult<&[T], &[T]>` +/// Returns the first match. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_find_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex_bytes!(RE, $re); + if let Some(m) = RE.find(&$i) { + Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end()))) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpFind))); + res + } + } + + ) +); + +/// `re_matches!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns all the matched parts +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_matches ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::Regex::new($re).unwrap(); + let v: Vec<_> = re.find_iter(&$i).map(|m| $i.slice(m.start()..m.end())).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatches))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_matches_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns all the matched parts. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_matches_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex!(RE, $re); + let v: Vec<_> = RE.find_iter(&$i).map(|m| $i.slice(m.start()..m.end())).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatches))); + res + } + } + ) +); + +/// `re_bytes_matches!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns all the matched parts +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_matches ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); + let v: Vec<_> = re.find_iter(&$i).map(|m| $i.slice(m.start()..m.end())).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatches))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_matches_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns all the matched parts. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_matches_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex_bytes!(RE, $re); + let v: Vec<_> = RE.find_iter(&$i).map(|m| $i.slice(m.start()..m.end())).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpMatches))); + res + } + } + ) +); + +/// `re_capture!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns the first capture group +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_capture ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::Regex::new($re).unwrap(); + if let Some(c) = re.captures(&$i) { + let v:Vec<_> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_capture_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns the first capture group. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_capture_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex!(RE, $re); + if let Some(c) = RE.captures(&$i) { + let v:Vec<_> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +/// `re_bytes_capture!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns the first capture group +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_capture ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); + if let Some(c) = re.captures(&$i) { + let v:Vec<_> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_capture_static!(regexp) => &[T] -> IResult<&[T], Vec<&[T]>>` +/// Returns the first capture group. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_capture_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex_bytes!(RE, $re); + if let Some(c) = RE.captures(&$i) { + let v:Vec<_> = c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect(); + let offset = { + let end = v.last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +/// `re_captures!(regexp) => &[T] -> IResult<&[T], Vec>>` +/// Returns all the capture groups +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_captures ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::Regex::new($re).unwrap(); + let v:Vec> = re.captures_iter(&$i) + .map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()) + .map(|m| $i.slice(m.start()..m.end())).collect()).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap().last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_captures_static!(regexp) => &[T] -> IResult<&[T], Vec>>` +/// Returns all the capture groups. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_captures_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex!(RE, $re); + let v:Vec> = RE.captures_iter(&$i) + .map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap().last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +/// `re_bytes_captures!(regexp) => &[T] -> IResult<&[T], Vec>>` +/// Returns all the capture groups +/// +/// requires the `regexp` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_captures ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + let re = $crate::lib::regex::bytes::Regex::new($re).unwrap(); + let v:Vec> = re.captures_iter(&$i) + .map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap().last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); + +#[cfg(feature = "regexp_macros")] +/// `re_bytes_captures_static!(regexp) => &[T] -> IResult<&[T], Vec>>` +/// Returns all the capture groups. Regular expression calculated at compile time +/// +/// requires the `regexp_macros` feature +#[macro_export(local_inner_macros)] +macro_rules! re_bytes_captures_static ( + ($i:expr, $re:expr) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::{Err,error::ErrorKind,IResult}; + + use $crate::Slice; + regex_bytes!(RE, $re); + let v:Vec> = RE.captures_iter(&$i) + .map(|c| c.iter().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|m| $i.slice(m.start()..m.end())).collect()).collect(); + if v.len() != 0 { + let offset = { + let end = v.last().unwrap().last().unwrap(); + end.as_ptr() as usize + end.len() - $i.as_ptr() as usize + }; + Ok(($i.slice(offset..), v)) + } else { + let res: IResult<_,_> = Err(Err::Error(error_position!($i, ErrorKind::RegexpCapture))); + res + } + } + ) +); +#[cfg(test)] +mod tests { + #[cfg(feature = "alloc")] + use crate::lib::std::vec::Vec; + use crate::error::ErrorKind; + use crate::internal::Err; + + #[test] + fn re_match() { + named!(rm<&str,&str>, re_match!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Ok(("", "2015-09-07"))); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpMatch + ),)) + ); + assert_eq!(rm("2015-09-07blah"), Ok(("", "2015-09-07blah"))); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_match_static() { + named!(rm<&str,&str>, re_match_static!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Ok(("", "2015-09-07"))); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpMatch + ),)) + ); + assert_eq!(rm("2015-09-07blah"), Ok(("", "2015-09-07blah"))); + } + + #[test] + fn re_find() { + named!(rm<&str,&str>, re_find!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Ok(("", "2015-09-07"))); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpFind + ),)) + ); + assert_eq!(rm("2015-09-07blah"), Ok(("blah", "2015-09-07"))); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_find_static() { + named!(rm<&str,&str>, re_find_static!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Ok(("", "2015-09-07"))); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpFind + ),)) + ); + assert_eq!(rm("2015-09-07blah"), Ok(("blah", "2015-09-07"))); + } + + #[cfg(feature = "alloc")] + #[test] + fn re_matches() { + named!(rm< &str,Vec<&str> >, re_matches!(r"\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Ok(("", vec!["2015-09-07"]))); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpMatches + ))) + ); + assert_eq!( + rm("aaa2015-09-07blah2015-09-09pouet"), + Ok(("pouet", vec!["2015-09-07", "2015-09-09"])) + ); + } + + #[cfg(feature = "regexp_macros")] + #[cfg(feature = "alloc")] + #[test] + fn re_matches_static() { + named!(rm< &str,Vec<&str> >, re_matches_static!(r"\d{4}-\d{2}-\d{2}")); + assert_eq!(rm("2015-09-07"), Ok(("", vec!["2015-09-07"]))); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpMatches + ))) + ); + assert_eq!( + rm("aaa2015-09-07blah2015-09-09pouet"), + Ok(("pouet", vec!["2015-09-07", "2015-09-09"])) + ); + } + + #[cfg(feature = "alloc")] + #[test] + fn re_capture() { + named!(rm< &str,Vec<&str> >, re_capture!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!( + rm("blah nom 0.3.11pouet"), + Ok(("pouet", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])) + ); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm("hello nom 0.3.11 world regex 0.1.41"), + Ok(( + " world regex 0.1.41", + vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"] + )) + ); + } + + #[cfg(feature = "alloc")] + #[cfg(feature = "regexp_macros")] + #[test] + fn re_capture_static() { + named!(rm< &str,Vec<&str> >, re_capture_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!( + rm("blah nom 0.3.11pouet"), + Ok(("pouet", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"])) + ); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm("hello nom 0.3.11 world regex 0.1.41"), + Ok(( + " world regex 0.1.41", + vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"] + )) + ); + } + + #[cfg(feature = "alloc")] + #[test] + fn re_captures() { + named!(rm< &str,Vec> >, re_captures!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!( + rm("blah nom 0.3.11pouet"), + Ok(( + "pouet", + vec![vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]] + )) + ); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm("hello nom 0.3.11 world regex 0.1.41 aaa"), + Ok(( + " aaa", + vec![ + vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"], + vec!["regex 0.1.41", "regex", "0.1.41", "0", "1", "41"], + ] + )) + ); + } + + #[cfg(feature = "alloc")] + #[cfg(feature = "regexp_macros")] + #[test] + fn re_captures_static() { + named!(rm< &str,Vec> >, re_captures_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))")); + assert_eq!( + rm("blah nom 0.3.11pouet"), + Ok(( + "pouet", + vec![vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]] + )) + ); + assert_eq!( + rm("blah"), + Err(Err::Error(error_position!( + &"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm("hello nom 0.3.11 world regex 0.1.41 aaa"), + Ok(( + " aaa", + vec![ + vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"], + vec!["regex 0.1.41", "regex", "0.1.41", "0", "1", "41"], + ] + )) + ); + } + + #[test] + fn re_bytes_match() { + named!(rm, re_bytes_match!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Ok((&b""[..], &b"2015-09-07"[..]))); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpMatch + ))) + ); + assert_eq!( + rm(&b"2015-09-07blah"[..]), + Ok((&b""[..], &b"2015-09-07blah"[..])) + ); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_match_static() { + named!(rm, re_bytes_match_static!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Ok((&b""[..], &b"2015-09-07"[..]))); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpMatch + ))) + ); + assert_eq!( + rm(&b"2015-09-07blah"[..]), + Ok((&b""[..], &b"2015-09-07blah"[..])) + ); + } + + #[test] + fn re_bytes_find() { + named!(rm, re_bytes_find!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Ok((&b""[..], &b"2015-09-07"[..]))); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpFind + ))) + ); + assert_eq!( + rm(&b"2015-09-07blah"[..]), + Ok((&b"blah"[..], &b"2015-09-07"[..])) + ); + } + + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_find_static() { + named!(rm, re_bytes_find_static!(r"^\d{4}-\d{2}-\d{2}")); + assert_eq!(rm(&b"2015-09-07"[..]), Ok((&b""[..], &b"2015-09-07"[..]))); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpFind + ))) + ); + assert_eq!( + rm(&b"2015-09-07blah"[..]), + Ok((&b"blah"[..], &b"2015-09-07"[..])) + ); + } + + #[cfg(feature = "alloc")] + #[test] + fn re_bytes_matches() { + named!(rm>, re_bytes_matches!(r"\d{4}-\d{2}-\d{2}")); + assert_eq!( + rm(&b"2015-09-07"[..]), + Ok((&b""[..], vec![&b"2015-09-07"[..]])) + ); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpMatches + ))) + ); + assert_eq!( + rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]), + Ok((&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]])) + ); + } + + #[cfg(feature = "alloc")] + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_matches_static() { + named!( + rm>, + re_bytes_matches_static!(r"\d{4}-\d{2}-\d{2}") + ); + assert_eq!( + rm(&b"2015-09-07"[..]), + Ok((&b""[..], vec![&b"2015-09-07"[..]])) + ); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpMatches + ))) + ); + assert_eq!( + rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]), + Ok((&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]])) + ); + } + + #[cfg(feature = "alloc")] + #[test] + fn re_bytes_capture() { + named!( + rm>, + re_bytes_capture!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))") + ); + assert_eq!( + rm(&b"blah nom 0.3.11pouet"[..]), + Ok(( + &b"pouet"[..], + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ] + )) + ); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm(&b"hello nom 0.3.11 world regex 0.1.41"[..]), + Ok(( + &b" world regex 0.1.41"[..], + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ] + )) + ); + } + + #[cfg(feature = "alloc")] + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_capture_static() { + named!( + rm>, + re_bytes_capture_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))") + ); + assert_eq!( + rm(&b"blah nom 0.3.11pouet"[..]), + Ok(( + &b"pouet"[..], + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ] + )) + ); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm(&b"hello nom 0.3.11 world regex 0.1.41"[..]), + Ok(( + &b" world regex 0.1.41"[..], + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ] + )) + ); + } + + #[cfg(feature = "alloc")] + #[test] + fn re_bytes_captures() { + named!( + rm>>, + re_bytes_captures!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))") + ); + assert_eq!( + rm(&b"blah nom 0.3.11pouet"[..]), + Ok(( + &b"pouet"[..], + vec![ + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ], + ] + )) + ); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm(&b"hello nom 0.3.11 world regex 0.1.41 aaa"[..]), + Ok(( + &b" aaa"[..], + vec![ + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ], + vec![ + &b"regex 0.1.41"[..], + &b"regex"[..], + &b"0.1.41"[..], + &b"0"[..], + &b"1"[..], + &b"41"[..], + ], + ] + )) + ); + } + + #[cfg(feature = "alloc")] + #[cfg(feature = "regexp_macros")] + #[test] + fn re_bytes_captures_static() { + named!( + rm>>, + re_bytes_captures_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))") + ); + assert_eq!( + rm(&b"blah nom 0.3.11pouet"[..]), + Ok(( + &b"pouet"[..], + vec![ + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ], + ] + )) + ); + assert_eq!( + rm(&b"blah"[..]), + Err(Err::Error(error_position!( + &b"blah"[..], + ErrorKind::RegexpCapture + ))) + ); + assert_eq!( + rm(&b"hello nom 0.3.11 world regex 0.1.41 aaa"[..]), + Ok(( + &b" aaa"[..], + vec![ + vec![ + &b"nom 0.3.11"[..], + &b"nom"[..], + &b"0.3.11"[..], + &b"0"[..], + &b"3"[..], + &b"11"[..], + ], + vec![ + &b"regex 0.1.41"[..], + &b"regex"[..], + &b"0.1.41"[..], + &b"0"[..], + &b"1"[..], + &b"41"[..], + ], + ] + )) + ); + } +} diff --git a/third_party/rust/nom/src/sequence/macros.rs b/third_party/rust/nom/src/sequence/macros.rs new file mode 100644 index 0000000000..a5a3351499 --- /dev/null +++ b/third_party/rust/nom/src/sequence/macros.rs @@ -0,0 +1,860 @@ +/// `tuple!(I->IResult, I->IResult, ... I->IResult) => I -> IResult` +/// chains parsers and assemble the sub results in a tuple. +/// +/// The input type `I` must implement `nom::InputLength`. +/// +/// This combinator will count how much data is consumed by every child parser +/// and take it into account if there is not enough data +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::error::ErrorKind; +/// # use nom::number::streaming::be_u16; +/// // the return type depends of the children parsers +/// named!(parser<&[u8], (u16, &[u8], &[u8]) >, +/// tuple!( +/// be_u16 , +/// take!(3), +/// tag!("fg") +/// ) +/// ); +/// +/// # fn main() { +/// assert_eq!( +/// parser(&b"abcdefgh"[..]), +/// Ok(( +/// &b"h"[..], +/// (0x6162u16, &b"cde"[..], &b"fg"[..]) +/// )) +/// ); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! tuple ( + ($i:expr, $($rest:tt)*) => ( + { + tuple_parser!($i, (), $($rest)*) + } + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! tuple_parser ( + ($i:expr, ($($parsed:tt),*), $e:path, $($rest:tt)*) => ( + tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*); + ); + ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + let i_ = $i.clone(); + + ( $submac!(i_, $($args)*) ).and_then(|(i,o)| { + let i_ = i.clone(); + tuple_parser!(i_, (o), $($rest)*) + }) + } + ); + ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + let i_ = $i.clone(); + + ( $submac!(i_, $($args)*) ).and_then(|(i,o)| { + let i_ = i.clone(); + tuple_parser!(i_, ($($parsed)* , o), $($rest)*) + }) + } + ); + ($i:expr, ($($parsed:tt),*), $e:path) => ( + tuple_parser!($i, ($($parsed),*), call!($e)); + ); + ($i:expr, (), $submac:ident!( $($args:tt)* )) => ( + { + let i_ = $i.clone(); + ( $submac!(i_, $($args)*) ).map(|(i,o)| (i, (o))) + } + ); + ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => ( + { + let i_ = $i.clone(); + ( $submac!(i_, $($args)*) ).map(|(i,o)| (i, ($($parsed),* , o))) + } + ); + ($i:expr, ($($parsed:expr),*)) => ( + { + $crate::lib::std::result::Result::Ok(($i, ($($parsed),*))) + } + ); +); + +/// `pair!(I -> IResult, I -> IResult) => I -> IResult` +/// pair returns a tuple of the results of its two child parsers of both succeed +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # use nom::character::complete::{alpha1, digit1}; +/// named!(parser<&str, (&str, &str)>, pair!(alpha1, digit1)); +/// +/// # fn main() { +/// assert_eq!(parser("abc123"), Ok(("", ("abc", "123")))); +/// assert_eq!(parser("123abc"), Err(Err::Error(("123abc", ErrorKind::Alpha)))); +/// assert_eq!(parser("abc;123"), Err(Err::Error((";123", ErrorKind::Digit)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! pair( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + pair!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + pair!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + pair!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::sequence::pairc($i, $f, $g) + ); +); + +/// `separated_pair!(I -> IResult, I -> IResult, I -> IResult) => I -> IResult` +/// separated_pair(X,sep,Y) returns a tuple of its first and third child parsers +/// if all 3 succeed +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # use nom::character::complete::{alpha1, digit1}; +/// named!(parser<&str, (&str, &str)>, separated_pair!(alpha1, char!(','), digit1)); +/// +/// # fn main() { +/// assert_eq!(parser("abc,123"), Ok(("", ("abc", "123")))); +/// assert_eq!(parser("123,abc"), Err(Err::Error(("123,abc", ErrorKind::Alpha)))); +/// assert_eq!(parser("abc;123"), Err(Err::Error((";123", ErrorKind::Char)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! separated_pair( + ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + separated_pair!($i, |i| $submac!(i, $($args)*), $($rest)*) + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + separated_pair!($i, $f, |i| $submac!(i, $($args)*), $($rest)*) + ); + ($i:expr, $f:expr, $g:expr, $submac:ident!( $($args:tt)* )) => ( + separated_pair!($i, $f, $g, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr, $g:expr, $h:expr) => ( + $crate::sequence::separated_pairc($i, $f, $g, $h) + ); +); + +/// `preceded!(I -> IResult, I -> IResult) => I -> IResult` +/// preceded returns the result of its second parser if both succeed +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # use nom::character::complete::{alpha1}; +/// named!(parser<&str, &str>, preceded!(char!('-'), alpha1)); +/// +/// # fn main() { +/// assert_eq!(parser("-abc"), Ok(("", "abc"))); +/// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Char)))); +/// assert_eq!(parser("-123"), Err(Err::Error(("123", ErrorKind::Alpha)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! preceded( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + preceded!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + preceded!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + preceded!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::sequence::precededc($i, $f, $g) + ); +); + +/// `terminated!(I -> IResult, I -> IResult) => I -> IResult` +/// terminated returns the result of its first parser if both succeed +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::Err; +/// # use nom::error::ErrorKind; +/// # use nom::character::complete::{alpha1}; +/// named!(parser<&str, &str>, terminated!(alpha1, char!(';'))); +/// +/// # fn main() { +/// assert_eq!(parser("abc;"), Ok(("", "abc"))); +/// assert_eq!(parser("abc,"), Err(Err::Error((",", ErrorKind::Char)))); +/// assert_eq!(parser("123;"), Err(Err::Error(("123;", ErrorKind::Alpha)))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! terminated( + ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + terminated!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) + ); + + ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( + terminated!($i, |i| $submac!(i, $($args)*), $g); + ); + + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( + terminated!($i, $f, |i| $submac!(i, $($args)*)); + ); + + ($i:expr, $f:expr, $g:expr) => ( + $crate::sequence::terminatedc($i, $f, $g) + ); +); + +/// `delimited!(I -> IResult, I -> IResult, I -> IResult) => I -> IResult` +/// delimited(opening, X, closing) returns X +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::character::complete::{alpha1}; +/// named!(parens, +/// delimited!( +/// tag!("("), +/// alpha1, +/// tag!(")") +/// ) +/// ); +/// +/// # fn main() { +/// let input = &b"(test)"[..]; +/// assert_eq!(parens(input), Ok((&b""[..], &b"test"[..]))); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! delimited( + ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + delimited!($i, |i| $submac!(i, $($args)*), $($rest)*) + ); + ($i:expr, $f:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + delimited!($i, $f, |i| $submac!(i, $($args)*), $($rest)*) + ); + ($i:expr, $f:expr, $g:expr, $submac:ident!( $($args:tt)* )) => ( + delimited!($i, $f, $g, |i| $submac!(i, $($args)*)) + ); + ($i:expr, $f:expr, $g:expr, $h:expr) => ( + $crate::sequence::delimitedc($i, $f, $g, $h) + ); +); + +/// `do_parse!(I->IResult >> I->IResult >> ... I->IResult , ( O ) ) => I -> IResult` +/// do_parse applies sub parsers in a sequence. +/// it can store intermediary results and make them available +/// for later parsers +/// +/// The input type `I` must implement `nom::InputLength`. +/// +/// This combinator will count how much data is consumed by every child parser +/// and take it into account if there is not enough data +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # use nom::{Err,Needed}; +/// use nom::number::streaming::be_u8; +/// +/// // this parser implements a common pattern in binary formats, +/// // the TAG-LENGTH-VALUE, where you first recognize a specific +/// // byte slice, then the next bytes indicate the length of +/// // the data, then you take that slice and return it +/// // +/// // here, we match the tag 42, take the length in the next byte +/// // and store it in `length`, then use `take!` with `length` +/// // to obtain the subslice that we store in `bytes`, then return +/// // `bytes` +/// // you can use other macro combinators inside do_parse (like the `tag` +/// // one here), or a function (like `be_u8` here), but you cannot use a +/// // module path (like `nom::be_u8`) there, because of limitations in macros +/// named!(tag_length_value, +/// do_parse!( +/// tag!( &[ 42u8 ][..] ) >> +/// length: be_u8 >> +/// bytes: take!(length) >> +/// (bytes) +/// ) +/// ); +/// +/// # fn main() { +/// let a: Vec = vec!(42, 2, 3, 4, 5); +/// let result_a: Vec = vec!(3, 4); +/// let rest_a: Vec = vec!(5); +/// assert_eq!(tag_length_value(&a[..]), Ok((&rest_a[..], &result_a[..]))); +/// +/// // here, the length is 5, but there are only 3 bytes afterwards (3, 4 and 5), +/// // so the parser will tell you that you need 7 bytes: one for the tag, +/// // one for the length, then 5 bytes +/// let b: Vec = vec!(42, 5, 3, 4, 5); +/// assert_eq!(tag_length_value(&b[..]), Err(Err::Incomplete(Needed::Size(5)))); +/// # } +/// ``` +/// +/// the result is a tuple, so you can return multiple sub results, like +/// this: +/// `do_parse!(I->IResult >> I->IResult >> ... I->IResult , ( O, P ) ) => I -> IResult` +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// use nom::number::streaming::be_u8; +/// named!(tag_length_value<(u8, &[u8])>, +/// do_parse!( +/// tag!( &[ 42u8 ][..] ) >> +/// length: be_u8 >> +/// bytes: take!(length) >> +/// (length, bytes) +/// ) +/// ); +/// +/// # fn main() { +/// # } +/// ``` +/// +#[macro_export(local_inner_macros)] +macro_rules! do_parse ( + (__impl $i:expr, ( $($rest:expr),* )) => ( + $crate::lib::std::result::Result::Ok(($i, ( $($rest),* ))) + ); + + (__impl $i:expr, $field:ident : $submac:ident!( $($args:tt)* ) ) => ( + do_parse!(__impl $i, $submac!( $($args)* )) + ); + + (__impl $i:expr, $submac:ident!( $($args:tt)* ) ) => ( + nom_compile_error!("do_parse is missing the return value. A do_parse call must end + with a return value between parenthesis, as follows: + + do_parse!( + a: tag!(\"abcd\") >> + b: tag!(\"efgh\") >> + + ( Value { a: a, b: b } ) + "); + ); + + (__impl $i:expr, $field:ident : $submac:ident!( $($args:tt)* ) ~ $($rest:tt)* ) => ( + nom_compile_error!("do_parse uses >> as separator, not ~"); + ); + (__impl $i:expr, $submac:ident!( $($args:tt)* ) ~ $($rest:tt)* ) => ( + nom_compile_error!("do_parse uses >> as separator, not ~"); + ); + (__impl $i:expr, $field:ident : $e:ident ~ $($rest:tt)*) => ( + do_parse!(__impl $i, $field: call!($e) ~ $($rest)*); + ); + (__impl $i:expr, $e:ident ~ $($rest:tt)*) => ( + do_parse!(__impl $i, call!($e) ~ $($rest)*); + ); + + (__impl $i:expr, $e:ident >> $($rest:tt)*) => ( + do_parse!(__impl $i, call!($e) >> $($rest)*); + ); + (__impl $i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + Err(e) => Err(e), + Ok((i,_)) => { + let i_ = i.clone(); + do_parse!(__impl i_, $($rest)*) + }, + } + } + ); + + (__impl $i:expr, $field:ident : $e:ident >> $($rest:tt)*) => ( + do_parse!(__impl $i, $field: call!($e) >> $($rest)*); + ); + + (__impl $i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + + let i_ = $i.clone(); + match $submac!(i_, $($args)*) { + Err(e) => Err(e), + Ok((i,o)) => { + let $field = o; + let i_ = i.clone(); + do_parse!(__impl i_, $($rest)*) + }, + } + } + ); + + // ending the chain + (__impl $i:expr, $e:ident >> ( $($rest:tt)* )) => ( + do_parse!(__impl $i, call!($e) >> ( $($rest)* )); + ); + + (__impl $i:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match $submac!($i, $($args)*) { + Err(e) => Err(e), + Ok((i,_)) => { + do_parse!(__finalize i, $($rest)*) + }, + } + }); + + (__impl $i:expr, $field:ident : $e:ident >> ( $($rest:tt)* )) => ( + do_parse!(__impl $i, $field: call!($e) >> ( $($rest)* ) ); + ); + + (__impl $i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match $submac!($i, $($args)*) { + Err(e) => Err(e), + Ok((i,o)) => { + let $field = o; + do_parse!(__finalize i, $($rest)*) + }, + } + }); + + (__finalize $i:expr, ( $o: expr )) => ({ + use $crate::lib::std::result::Result::Ok; + Ok(($i, $o)) + }); + + (__finalize $i:expr, ( $($rest:tt)* )) => ({ + use $crate::lib::std::result::Result::Ok; + Ok(($i, ( $($rest)* ))) + }); + + ($i:expr, $($rest:tt)*) => ( + { + do_parse!(__impl $i, $($rest)*) + } + ); + ($submac:ident!( $($args:tt)* ) >> $($rest:tt)* ) => ( + nom_compile_error!("if you are using do_parse outside of a named! macro, you must + pass the input data as first argument, like this: + + let res = do_parse!(input, + a: tag!(\"abcd\") >> + b: tag!(\"efgh\") >> + ( Value { a: a, b: b } ) + );"); + ); + ($e:ident! >> $($rest:tt)* ) => ( + do_parse!( call!($e) >> $($rest)*); + ); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! nom_compile_error ( + (( $($args:tt)* )) => ( compile_error!($($args)*) ); +); + +#[cfg(test)] +mod tests { + use crate::internal::{Err, IResult, Needed}; + use crate::number::streaming::be_u16; + use crate::error::ErrorKind; + + // reproduce the tag and take macros, because of module import order + macro_rules! tag ( + ($i:expr, $inp: expr) => ( + { + #[inline(always)] + fn as_bytes(b: &T) -> &[u8] { + b.as_bytes() + } + + let expected = $inp; + let bytes = as_bytes(&expected); + + tag_bytes!($i,bytes) + } + ); + ); + + macro_rules! tag_bytes ( + ($i:expr, $bytes: expr) => ( + { + use $crate::lib::std::cmp::min; + + let len = $i.len(); + let blen = $bytes.len(); + let m = min(len, blen); + let reduced = &$i[..m]; + let b = &$bytes[..m]; + + let res: IResult<_,_,_> = if reduced != b { + Err($crate::Err::Error(error_position!($i, $crate::error::ErrorKind::Tag))) + } else if m < blen { + Err($crate::Err::Incomplete(Needed::Size(blen))) + } else { + Ok((&$i[blen..], reduced)) + }; + res + } + ); + ); + + macro_rules! take ( + ($i:expr, $count:expr) => ( + { + let cnt = $count as usize; + let res:IResult<&[u8],&[u8],_> = if $i.len() < cnt { + Err($crate::Err::Incomplete(Needed::Size(cnt))) + } else { + Ok((&$i[cnt..],&$i[0..cnt])) + }; + res + } + ); + ); + + #[derive(PartialEq, Eq, Debug)] + struct B { + a: u8, + b: u8, + } + + #[derive(PartialEq, Eq, Debug)] + struct C { + a: u8, + b: Option, + } + + /*FIXME: convert code examples to new error management + use util::{add_error_pattern, error_to_list, print_error}; + + #[cfg(feature = "std")] + #[cfg_attr(rustfmt, rustfmt_skip)] + fn error_to_string(e: &Context) -> &'static str { + let v: Vec<(P, ErrorKind)> = error_to_list(e); + // do it this way if you can use slice patterns + //match &v[..] { + // [ErrorKind::Custom(42), ErrorKind::Tag] => "missing `ijkl` tag", + // [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] => "missing `mnop` tag after `ijkl`", + // _ => "unrecognized error" + //} + + let collected: Vec> = v.iter().map(|&(_, ref e)| e.clone()).collect(); + if &collected[..] == [ErrorKind::Custom(42), ErrorKind::Tag] { + "missing `ijkl` tag" + } else if &collected[..] == [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] { + "missing `mnop` tag after `ijkl`" + } else { + "unrecognized error" + } + } + + // do it this way if you can use box patterns + //use $crate::lib::std::str; + //fn error_to_string(e:Err) -> String + // match e { + // NodePosition(ErrorKind::Custom(42), i1, box Position(ErrorKind::Tag, i2)) => { + // format!("missing `ijkl` tag, found '{}' instead", str::from_utf8(i2).unwrap()) + // }, + // NodePosition(ErrorKind::Custom(42), i1, box NodePosition(ErrorKind::Custom(128), i2, box Position(ErrorKind::Tag, i3))) => { + // format!("missing `mnop` tag after `ijkl`, found '{}' instead", str::from_utf8(i3).unwrap()) + // }, + // _ => "unrecognized error".to_string() + // } + //} + */ + + #[cfg_attr(rustfmt, rustfmt_skip)] + #[allow(unused_variables)] + #[test] + fn add_err() { + named!(err_test, + preceded!( + tag!("efgh"), + add_return_error!( + //ErrorKind::Custom(42u32), + ErrorKind::Char, + do_parse!( + tag!("ijkl") >> + //res: add_return_error!(ErrorKind::Custom(128u32), tag!("mnop")) >> + res: add_return_error!(ErrorKind::Eof, tag!("mnop")) >> + (res) + ) + ) + ) + ); + let a = &b"efghblah"[..]; + let b = &b"efghijklblah"[..]; + let c = &b"efghijklmnop"[..]; + + let blah = &b"blah"[..]; + + let res_a = err_test(a); + let res_b = err_test(b); + let res_c = err_test(c); + assert_eq!(res_a, + Err(Err::Error(error_node_position!(blah, + //ErrorKind::Custom(42u32), + ErrorKind::Eof, + error_position!(blah, ErrorKind::Tag))))); + //assert_eq!(res_b, Err(Err::Error(error_node_position!(&b"ijklblah"[..], ErrorKind::Custom(42u32), + // error_node_position!(blah, ErrorKind::Custom(128u32), error_position!(blah, ErrorKind::Tag)))))); + assert_eq!(res_b, Err(Err::Error(error_node_position!(&b"ijklblah"[..], ErrorKind::Eof, + error_node_position!(blah, ErrorKind::Eof, error_position!(blah, ErrorKind::Tag)))))); + assert_eq!(res_c, Ok((&b""[..], &b"mnop"[..]))); + } + + #[cfg_attr(rustfmt, rustfmt_skip)] + #[test] + fn complete() { + named!(err_test, + do_parse!( + tag!("ijkl") >> + res: complete!(tag!("mnop")) >> + (res) + ) + ); + let a = &b"ijklmn"[..]; + + let res_a = err_test(a); + assert_eq!(res_a, + Err(Err::Error(error_position!(&b"mn"[..], ErrorKind::Complete)))); + } + + #[test] + fn pair() { + named!(tag_abc, tag!("abc")); + named!(tag_def, tag!("def")); + named!( pair_abc_def<&[u8],(&[u8], &[u8])>, pair!(tag_abc, tag_def) ); + + assert_eq!(pair_abc_def(&b"abcdefghijkl"[..]), Ok((&b"ghijkl"[..], (&b"abc"[..], &b"def"[..])))); + assert_eq!(pair_abc_def(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(pair_abc_def(&b"abcd"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!( + pair_abc_def(&b"xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + assert_eq!( + pair_abc_def(&b"xxxdef"[..]), + Err(Err::Error(error_position!(&b"xxxdef"[..], ErrorKind::Tag))) + ); + assert_eq!( + pair_abc_def(&b"abcxxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn separated_pair() { + named!(tag_abc, tag!("abc")); + named!(tag_def, tag!("def")); + named!(tag_separator, tag!(",")); + named!( sep_pair_abc_def<&[u8],(&[u8], &[u8])>, separated_pair!(tag_abc, tag_separator, tag_def) ); + + assert_eq!( + sep_pair_abc_def(&b"abc,defghijkl"[..]), + Ok((&b"ghijkl"[..], (&b"abc"[..], &b"def"[..]))) + ); + assert_eq!(sep_pair_abc_def(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(sep_pair_abc_def(&b"abc,d"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!( + sep_pair_abc_def(&b"xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + assert_eq!( + sep_pair_abc_def(&b"xxx,def"[..]), + Err(Err::Error(error_position!(&b"xxx,def"[..], ErrorKind::Tag))) + ); + assert_eq!( + sep_pair_abc_def(&b"abc,xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn preceded() { + named!(tag_abcd, tag!("abcd")); + named!(tag_efgh, tag!("efgh")); + named!( preceded_abcd_efgh<&[u8], &[u8]>, preceded!(tag_abcd, tag_efgh) ); + + assert_eq!(preceded_abcd_efgh(&b"abcdefghijkl"[..]), Ok((&b"ijkl"[..], &b"efgh"[..]))); + assert_eq!(preceded_abcd_efgh(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(preceded_abcd_efgh(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!( + preceded_abcd_efgh(&b"xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + assert_eq!( + preceded_abcd_efgh(&b"xxxxdef"[..]), + Err(Err::Error(error_position!(&b"xxxxdef"[..], ErrorKind::Tag))) + ); + assert_eq!( + preceded_abcd_efgh(&b"abcdxxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn terminated() { + named!(tag_abcd, tag!("abcd")); + named!(tag_efgh, tag!("efgh")); + named!( terminated_abcd_efgh<&[u8], &[u8]>, terminated!(tag_abcd, tag_efgh) ); + + assert_eq!(terminated_abcd_efgh(&b"abcdefghijkl"[..]), Ok((&b"ijkl"[..], &b"abcd"[..]))); + assert_eq!(terminated_abcd_efgh(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(terminated_abcd_efgh(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!( + terminated_abcd_efgh(&b"xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + assert_eq!( + terminated_abcd_efgh(&b"xxxxdef"[..]), + Err(Err::Error(error_position!(&b"xxxxdef"[..], ErrorKind::Tag))) + ); + assert_eq!( + terminated_abcd_efgh(&b"abcdxxxx"[..]), + Err(Err::Error(error_position!(&b"xxxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn delimited() { + named!(tag_abc, tag!("abc")); + named!(tag_def, tag!("def")); + named!(tag_ghi, tag!("ghi")); + named!( delimited_abc_def_ghi<&[u8], &[u8]>, delimited!(tag_abc, tag_def, tag_ghi) ); + + assert_eq!(delimited_abc_def_ghi(&b"abcdefghijkl"[..]), Ok((&b"jkl"[..], &b"def"[..]))); + assert_eq!(delimited_abc_def_ghi(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(delimited_abc_def_ghi(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(delimited_abc_def_ghi(&b"abcdefgh"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!( + delimited_abc_def_ghi(&b"xxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + assert_eq!( + delimited_abc_def_ghi(&b"xxxdefghi"[..]), + Err(Err::Error(error_position!(&b"xxxdefghi"[..], ErrorKind::Tag),)) + ); + assert_eq!( + delimited_abc_def_ghi(&b"abcxxxghi"[..]), + Err(Err::Error(error_position!(&b"xxxghi"[..], ErrorKind::Tag))) + ); + assert_eq!( + delimited_abc_def_ghi(&b"abcdefxxx"[..]), + Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn tuple_test() { + named!(tuple_3<&[u8], (u16, &[u8], &[u8]) >, + tuple!( be_u16 , take!(3), tag!("fg") ) ); + + assert_eq!(tuple_3(&b"abcdefgh"[..]), Ok((&b"h"[..], (0x6162u16, &b"cde"[..], &b"fg"[..])))); + assert_eq!(tuple_3(&b"abcd"[..]), Err(Err::Incomplete(Needed::Size(3)))); + assert_eq!(tuple_3(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(2)))); + assert_eq!( + tuple_3(&b"abcdejk"[..]), + Err(Err::Error(error_position!(&b"jk"[..], ErrorKind::Tag))) + ); + } + + #[test] + fn do_parse() { + fn ret_int1(i: &[u8]) -> IResult<&[u8], u8> { + Ok((i, 1)) + }; + fn ret_int2(i: &[u8]) -> IResult<&[u8], u8> { + Ok((i, 2)) + }; + + //trace_macros!(true); + named!(do_parser<&[u8], (u8, u8)>, + do_parse!( + tag!("abcd") >> + opt!(tag!("abcd")) >> + aa: ret_int1 >> + tag!("efgh") >> + bb: ret_int2 >> + tag!("efgh") >> + (aa, bb) + ) + ); + //named!(do_parser<&[u8], (u8, u8)>, + // do_parse!( + // tag!("abcd") >> aa: ret_int1 >> tag!("efgh") >> bb: ret_int2 >> tag!("efgh") >> (aa, bb) + // ) + //); + + //trace_macros!(false); + + assert_eq!(do_parser(&b"abcdabcdefghefghX"[..]), Ok((&b"X"[..], (1, 2)))); + assert_eq!(do_parser(&b"abcdefghefghX"[..]), Ok((&b"X"[..], (1, 2)))); + assert_eq!(do_parser(&b"abcdab"[..]), Err(Err::Incomplete(Needed::Size(4)))); + assert_eq!(do_parser(&b"abcdefghef"[..]), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[cfg_attr(rustfmt, rustfmt_skip)] + #[test] + fn do_parse_dependency() { + use crate::number::streaming::be_u8; + + named!(length_value, + do_parse!( + length: be_u8 >> + bytes: take!(length) >> + (bytes) + ) + ); + + let a = [2u8, 3, 4, 5]; + let res_a = [3u8, 4]; + assert_eq!(length_value(&a[..]), Ok((&a[3..], &res_a[..]))); + let b = [5u8, 3, 4, 5]; + assert_eq!(length_value(&b[..]), Err(Err::Incomplete(Needed::Size(5)))); + } + + /* + named!(does_not_compile, + do_parse!( + length: be_u8 >> + bytes: take!(length) + ) + ); + named!(does_not_compile_either, + do_parse!( + length: be_u8 ~ + bytes: take!(length) ~ + ( () ) + ) + ); + fn still_does_not_compile() { + let data = b"abcd"; + + let res = do_parse!( + tag!("abcd") >> + tag!("efgh") >> + ( () ) + ); + } + */ +} diff --git a/third_party/rust/nom/src/sequence/mod.rs b/third_party/rust/nom/src/sequence/mod.rs new file mode 100644 index 0000000000..a0eafb97bd --- /dev/null +++ b/third_party/rust/nom/src/sequence/mod.rs @@ -0,0 +1,313 @@ +//! combinators applying parsers in sequence + +#[macro_use] +mod macros; + +use crate::internal::IResult; +use crate::error::ParseError; + +/// Gets an object from the first parser, +/// then gets another object from the second parser. +/// +/// # Arguments +/// * `first` The first parser to apply. +/// * `second` The second parser to apply. +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::sequence::pair; +/// use nom::bytes::complete::tag; +/// +/// let parser = pair(tag("abc"), tag("efg")); +/// +/// assert_eq!(parser("abcefg"), Ok(("", ("abc", "efg")))); +/// assert_eq!(parser("abcefghij"), Ok(("hij", ("abc", "efg")))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); +/// ``` +pub fn pair, F, G>(first: F, second: G) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, +{ + move |input: I| { + let (input, o1) = first(input)?; + second(input).map(|(i, o2)| (i, (o1, o2))) + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +pub fn pairc, F, G>(input: I, first: F, second: G) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, +{ + pair(first, second)(input) +} + +/// Matches an object from the first parser and discards it, +/// then gets an object from the second parser. +/// +/// # Arguments +/// * `first` The opening parser. +/// * `second` The second parser to get object. +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::sequence::preceded; +/// use nom::bytes::complete::tag; +/// +/// let parser = preceded(tag("abc"), tag("efg")); +/// +/// assert_eq!(parser("abcefg"), Ok(("", "efg"))); +/// assert_eq!(parser("abcefghij"), Ok(("hij", "efg"))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); +/// ``` +pub fn preceded, F, G>(first: F, second: G) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, +{ + move |input: I| { + let (input, _) = first(input)?; + second(input) + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +pub fn precededc, F, G>(input: I, first: F, second: G) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, +{ + preceded(first, second)(input) +} + +/// Gets an object from the first parser, +/// then matches an object from the second parser and discards it. +/// +/// # Arguments +/// * `first` The first parser to apply. +/// * `second` The second parser to match an object. +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::sequence::terminated; +/// use nom::bytes::complete::tag; +/// +/// let parser = terminated(tag("abc"), tag("efg")); +/// +/// assert_eq!(parser("abcefg"), Ok(("", "abc"))); +/// assert_eq!(parser("abcefghij"), Ok(("hij", "abc"))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); +/// ``` +pub fn terminated, F, G>(first: F, second: G) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, +{ + move |input: I| { + let (input, o1) = first(input)?; + second(input).map(|(i, _)| (i, o1)) + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +pub fn terminatedc, F, G>(input: I, first: F, second: G) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, +{ + terminated(first, second)(input) +} + +/// Gets an object from the first parser, +/// then matches an object from the sep_parser and discards it, +/// then gets another object from the second parser. +/// +/// # Arguments +/// * `first` The first parser to apply. +/// * `sep` The separator parser to apply. +/// * `second` The second parser to apply. +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::sequence::separated_pair; +/// use nom::bytes::complete::tag; +/// +/// let parser = separated_pair(tag("abc"), tag("|"), tag("efg")); +/// +/// assert_eq!(parser("abc|efg"), Ok(("", ("abc", "efg")))); +/// assert_eq!(parser("abc|efghij"), Ok(("hij", ("abc", "efg")))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); +/// ``` +pub fn separated_pair, F, G, H>(first: F, sep: G, second: H) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, + H: Fn(I) -> IResult, +{ + move |input: I| { + let (input, o1) = first(input)?; + let (input, _) = sep(input)?; + second(input).map(|(i, o2)| (i, (o1, o2))) + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +pub fn separated_pairc, F, G, H>(input: I, first: F, sep: G, second: H) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, + H: Fn(I) -> IResult, +{ + separated_pair(first, sep, second)(input) +} + +/// Matches an object from the first parser, +/// then gets an object from the sep_parser, +/// then matches another object from the second parser. +/// +/// # Arguments +/// * `first` The first parser to apply. +/// * `sep` The separator parser to apply. +/// * `second` The second parser to apply. +/// ```rust +/// # use nom::{Err, error::ErrorKind, Needed}; +/// # use nom::Needed::Size; +/// use nom::sequence::delimited; +/// use nom::bytes::complete::tag; +/// +/// let parser = delimited(tag("abc"), tag("|"), tag("efg")); +/// +/// assert_eq!(parser("abc|efg"), Ok(("", "|"))); +/// assert_eq!(parser("abc|efghij"), Ok(("hij", "|"))); +/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); +/// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag)))); +/// ``` +pub fn delimited, F, G, H>(first: F, sep: G, second: H) -> impl Fn(I) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, + H: Fn(I) -> IResult, +{ + move |input: I| { + let (input, _) = first(input)?; + let (input, o2) = sep(input)?; + second(input).map(|(i, _)| (i, o2)) + } +} + +// this implementation is used for type inference issues in macros +#[doc(hidden)] +pub fn delimitedc, F, G, H>(input: I, first: F, sep: G, second: H) -> IResult +where + F: Fn(I) -> IResult, + G: Fn(I) -> IResult, + H: Fn(I) -> IResult, +{ + delimited(first, sep, second)(input) +} + +/// helper trait for the tuple combinator +/// +/// this trait is implemented for tuples of parsers of up to 21 elements +pub trait Tuple { + /// parses the input and returns a tuple of results of each parser + fn parse(&self, input: I) -> IResult; +} + +impl, F: Fn(Input) -> IResult > Tuple for (F,) { + fn parse(&self, input: Input) -> IResult { + self.0(input).map(|(i,o)| (i, (o,))) + } +} + +macro_rules! tuple_trait( + ($name1:ident $ty1:ident, $name2: ident $ty2:ident, $($name:ident $ty:ident),*) => ( + tuple_trait!(__impl $name1 $ty1, $name2 $ty2; $($name $ty),*); + ); + (__impl $($name:ident $ty: ident),+; $name1:ident $ty1:ident, $($name2:ident $ty2:ident),*) => ( + tuple_trait_impl!($($name $ty),+); + tuple_trait!(__impl $($name $ty),+ , $name1 $ty1; $($name2 $ty2),*); + ); + (__impl $($name:ident $ty: ident),+; $name1:ident $ty1:ident) => ( + tuple_trait_impl!($($name $ty),+); + tuple_trait_impl!($($name $ty),+, $name1 $ty1); + ); +); + +macro_rules! tuple_trait_impl( + ($($name:ident $ty: ident),+) => ( + impl< + Input: Clone, $($ty),+ , Error: ParseError, + $($name: Fn(Input) -> IResult),+ + > Tuple for ( $($name),+ ) { + + fn parse(&self, input: Input) -> IResult { + tuple_trait_inner!(0, self, input, (), $($name)+) + + } + } + ); +); + +macro_rules! tuple_trait_inner( + ($it:tt, $self:expr, $input:expr, (), $head:ident $($id:ident)+) => ({ + let (i, o) = $self.$it($input.clone())?; + + succ!($it, tuple_trait_inner!($self, i, ( o ), $($id)+)) + }); + ($it:tt, $self:expr, $input:expr, ($($parsed:tt)*), $head:ident $($id:ident)+) => ({ + let (i, o) = $self.$it($input.clone())?; + + succ!($it, tuple_trait_inner!($self, i, ($($parsed)* , o), $($id)+)) + }); + ($it:tt, $self:expr, $input:expr, ($($parsed:tt)*), $head:ident) => ({ + let (i, o) = $self.$it($input.clone())?; + + Ok((i, ($($parsed)* , o))) + }); +); + +tuple_trait!(FnA A, FnB B, FnC C, FnD D, FnE E, FnF F, FnG G, FnH H, FnI I, FnJ J, FnK K, FnL L, + FnM M, FnN N, FnO O, FnP P, FnQ Q, FnR R, FnS S, FnT T, FnU U); + +/// applies a tuple of parsers one by one and returns their results as a tuple +/// +/// ```rust +/// # use nom::{Err, error::ErrorKind}; +/// use nom::sequence::tuple; +/// use nom::character::complete::{alpha1, digit1}; +/// let parser = tuple((alpha1, digit1, alpha1)); +/// +/// assert_eq!(parser("abc123def"), Ok(("", ("abc", "123", "def")))); +/// assert_eq!(parser("123def"), Err(Err::Error(("123def", ErrorKind::Alpha)))); +/// ``` +pub fn tuple, List: Tuple>(l: List) -> impl Fn(I) -> IResult { + move |i: I| { + l.parse(i) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn single_element_tuples() { + use crate::character::complete::{alpha1, digit1}; + use crate::{Err, error::ErrorKind}; + + let parser = tuple((alpha1,)); + assert_eq!(parser("abc123def"), Ok(("123def", ("abc",)))); + assert_eq!(parser("123def"), Err(Err::Error(("123def", ErrorKind::Alpha)))); + } +} diff --git a/third_party/rust/nom/src/str.rs b/third_party/rust/nom/src/str.rs new file mode 100644 index 0000000000..71fb1a4aa1 --- /dev/null +++ b/third_party/rust/nom/src/str.rs @@ -0,0 +1,494 @@ +#[cfg(test)] +mod test { + use crate::{Err, error::ErrorKind, IResult}; + + #[test] + fn tagtr_succeed() { + const INPUT: &str = "Hello World!"; + const TAG: &str = "Hello"; + fn test(input: &str) -> IResult<&str, &str> { + tag!(input, TAG) + } + + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == " World!", + "Parser `tag` consumed leftover input." + ); + assert!( + output == TAG, + "Parser `tag` doesn't return the tag it matched on success. \ + Expected `{}`, got `{}`.", + TAG, + output + ); + } + other => panic!( + "Parser `tag` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn tagtr_incomplete() { + const INPUT: &str = "Hello"; + const TAG: &str = "Hello World!"; + + let res: IResult<_,_,(_, ErrorKind)> = tag!(INPUT, TAG); + match res { + Err(Err::Incomplete(_)) => (), + other => { + panic!( + "Parser `tag` didn't require more input when it should have. \ + Got `{:?}`.", + other + ); + } + }; + } + + #[test] + fn tagtr_error() { + const INPUT: &str = "Hello World!"; + const TAG: &str = "Random"; // TAG must be closer than INPUT. + + let res: IResult<_,_,(_, ErrorKind)> = tag!(INPUT, TAG); + match res { + Err(Err::Error(_)) => (), + other => { + panic!( + "Parser `tag` didn't fail when it should have. Got `{:?}`.`", + other + ); + } + }; + } + + #[test] + fn take_s_succeed() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &str = "βèƒôřèÂßÇ"; + const LEFTOVER: &str = "áƒƭèř"; + + let res: IResult<_,_,(_, ErrorKind)> = take!(INPUT, 9); + match res { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `take_s` consumed leftover input. Leftover `{}`.", + extra + ); + assert!( + output == CONSUMED, + "Parser `take_s` doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `take_s` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_until_succeed() { + const INPUT: &str = "βèƒôřèÂßÇ∂áƒƭèř"; + const FIND: &str = "ÂßÇ∂"; + const CONSUMED: &str = "βèƒôřè"; + const LEFTOVER: &str = "ÂßÇ∂áƒƭèř"; + + let res: IResult<_,_,(_, ErrorKind)> = take_until!(INPUT, FIND); + match res { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `take_until`\ + consumed leftover input. Leftover `{}`.", + extra + ); + assert!( + output == CONSUMED, + "Parser `take_until`\ + doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `take_until` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_s_incomplete() { + const INPUT: &str = "βèƒôřèÂßÇá"; + + let res: IResult<_,_,(_, ErrorKind)> = take!(INPUT, 13); + match res { + Err(Err::Incomplete(_)) => (), + other => panic!( + "Parser `take` didn't require more input when it should have. \ + Got `{:?}`.", + other + ), + } + } + + use crate::internal::Needed; + + fn is_alphabetic(c: char) -> bool { + (c as u8 >= 0x41 && c as u8 <= 0x5A) || (c as u8 >= 0x61 && c as u8 <= 0x7A) + } + + #[test] + fn take_while() { + named!(f<&str,&str>, take_while!(is_alphabetic)); + let a = ""; + let b = "abcd"; + let c = "abcd123"; + let d = "123"; + + assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&b[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&c[..]), Ok((&d[..], &b[..]))); + assert_eq!(f(&d[..]), Ok((&d[..], &a[..]))); + } + + #[test] + fn take_while1() { + named!(f<&str,&str>, take_while1!(is_alphabetic)); + let a = ""; + let b = "abcd"; + let c = "abcd123"; + let d = "123"; + + assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&b[..]), Err(Err::Incomplete(Needed::Size(1)))); + assert_eq!(f(&c[..]), Ok((&"123"[..], &b[..]))); + assert_eq!( + f(&d[..]), + Err(Err::Error(error_position!(&d[..], ErrorKind::TakeWhile1))) + ); + } + + #[test] + fn take_till_s_succeed() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &str = "βèƒôřèÂßÇ"; + const LEFTOVER: &str = "áƒƭèř"; + fn till_s(c: char) -> bool { + c == 'á' + } + fn test(input: &str) -> IResult<&str, &str> { + take_till!(input, till_s) + } + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `take_till` consumed leftover input." + ); + assert!( + output == CONSUMED, + "Parser `take_till` doesn't return the string it consumed on success. \ + Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `take_till` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_while_succeed_none() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &str = ""; + const LEFTOVER: &str = "βèƒôřèÂßÇáƒƭèř"; + fn while_s(c: char) -> bool { + c == '9' + } + fn test(input: &str) -> IResult<&str, &str> { + take_while!(input, while_s) + } + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `take_while` consumed leftover input." + ); + assert!( + output == CONSUMED, + "Parser `take_while` doesn't return the string it consumed on success. \ + Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `take_while` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn is_not_succeed() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const AVOID: &str = "£úçƙ¥á"; + const CONSUMED: &str = "βèƒôřèÂßÇ"; + const LEFTOVER: &str = "áƒƭèř"; + fn test(input: &str) -> IResult<&str, &str> { + is_not!(input, AVOID) + } + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `is_not` consumed leftover input. Leftover `{}`.", + extra + ); + assert!( + output == CONSUMED, + "Parser `is_not` doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `is_not` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_while_succeed_some() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &str = "βèƒôřèÂßÇ"; + const LEFTOVER: &str = "áƒƭèř"; + fn while_s(c: char) -> bool { + c == 'β' || c == 'è' || c == 'ƒ' || c == 'ô' || c == 'ř' || c == 'è' || c == 'Â' || c == 'ß' || c == 'Ç' + } + fn test(input: &str) -> IResult<&str, &str> { + take_while!(input, while_s) + } + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `take_while` consumed leftover input." + ); + assert!( + output == CONSUMED, + "Parser `take_while` doesn't return the string it consumed on success. \ + Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `take_while` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn is_not_fail() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const AVOID: &str = "βúçƙ¥"; + fn test(input: &str) -> IResult<&str, &str> { + is_not!(input, AVOID) + } + match test(INPUT) { + Err(Err::Error(_)) => (), + other => panic!( + "Parser `is_not` didn't fail when it should have. Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_while1_succeed() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const CONSUMED: &str = "βèƒôřèÂßÇ"; + const LEFTOVER: &str = "áƒƭèř"; + fn while1_s(c: char) -> bool { + c == 'β' || c == 'è' || c == 'ƒ' || c == 'ô' || c == 'ř' || c == 'è' || c == 'Â' || c == 'ß' || c == 'Ç' + } + fn test(input: &str) -> IResult<&str, &str> { + take_while1!(input, while1_s) + } + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `take_while1` consumed leftover input." + ); + assert!( + output == CONSUMED, + "Parser `take_while1` doesn't return the string it consumed on success. \ + Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `take_while1` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_until_incomplete() { + const INPUT: &str = "βèƒôřè"; + const FIND: &str = "βèƒôřèÂßÇ"; + + let res: IResult<_,_,(_, ErrorKind)> = take_until!(INPUT, FIND); + match res { + Err(Err::Incomplete(_)) => (), + other => panic!( + "Parser `take_until` didn't require more input when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn is_a_succeed() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const MATCH: &str = "βèƒôřèÂßÇ"; + const CONSUMED: &str = "βèƒôřèÂßÇ"; + const LEFTOVER: &str = "áƒƭèř"; + fn test(input: &str) -> IResult<&str, &str> { + is_a!(input, MATCH) + } + match test(INPUT) { + Ok((extra, output)) => { + assert!( + extra == LEFTOVER, + "Parser `is_a` consumed leftover input. Leftover `{}`.", + extra + ); + assert!( + output == CONSUMED, + "Parser `is_a` doens't return the string it consumed on success. Expected `{}`, got `{}`.", + CONSUMED, + output + ); + } + other => panic!( + "Parser `is_a` didn't succeed when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_while1_fail() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + fn while1_s(c: char) -> bool { + c == '9' + } + fn test(input: &str) -> IResult<&str, &str> { + take_while1!(input, while1_s) + } + match test(INPUT) { + Err(Err::Error(_)) => (), + other => panic!( + "Parser `take_while1` didn't fail when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + fn is_a_fail() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const MATCH: &str = "Ûñℓúçƙ¥"; + fn test(input: &str) -> IResult<&str, &str> { + is_a!(input, MATCH) + } + match test(INPUT) { + Err(Err::Error(_)) => (), + other => panic!( + "Parser `is_a` didn't fail when it should have. Got `{:?}`.", + other + ), + }; + } + + #[test] + fn take_until_error() { + const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; + const FIND: &str = "Ráñδô₥"; + + let res: IResult<_,_,(_, ErrorKind)> = take_until!(INPUT, FIND); + match res { + Err(Err::Incomplete(_)) => (), + other => panic!( + "Parser `take_until` didn't fail when it should have. \ + Got `{:?}`.", + other + ), + }; + } + + #[test] + #[cfg(feature = "alloc")] + fn recognize_is_a() { + let a = "aabbab"; + let b = "ababcd"; + + named!(f <&str,&str>, recognize!(many1!(complete!(alt!( tag!("a") | tag!("b") ))))); + + assert_eq!(f(&a[..]), Ok((&a[6..], &a[..]))); + assert_eq!(f(&b[..]), Ok((&b[4..], &b[..4]))); + } + + #[test] + fn utf8_indexing() { + named!(dot(&str) -> &str, + tag!(".") + ); + + let _ = dot("點"); + } + + #[cfg(feature = "alloc")] + #[test] + fn case_insensitive() { + named!(test<&str,&str>, tag_no_case!("ABcd")); + assert_eq!(test("aBCdefgh"), Ok(("efgh", "aBCd"))); + assert_eq!(test("abcdefgh"), Ok(("efgh", "abcd"))); + assert_eq!(test("ABCDefgh"), Ok(("efgh", "ABCD"))); + + named!(test2<&str,&str>, tag_no_case!("ABcd")); + assert_eq!(test2("aBCdefgh"), Ok(("efgh", "aBCd"))); + assert_eq!(test2("abcdefgh"), Ok(("efgh", "abcd"))); + assert_eq!(test2("ABCDefgh"), Ok(("efgh", "ABCD"))); + } +} diff --git a/third_party/rust/nom/src/traits.rs b/third_party/rust/nom/src/traits.rs new file mode 100644 index 0000000000..afd16ba251 --- /dev/null +++ b/third_party/rust/nom/src/traits.rs @@ -0,0 +1,1186 @@ +//! Traits input types have to implement to work with nom combinators +//! +use crate::internal::{Err, IResult, Needed}; +use crate::error::{ParseError, ErrorKind}; +use crate::lib::std::ops::{Range, RangeFrom, RangeFull, RangeTo}; +use crate::lib::std::iter::Enumerate; +use crate::lib::std::slice::Iter; +use crate::lib::std::iter::Map; +use crate::lib::std::str::Chars; +use crate::lib::std::str::CharIndices; +use crate::lib::std::str::FromStr; +use crate::lib::std::str::from_utf8; +use memchr; + +#[cfg(feature = "alloc")] +use crate::lib::std::string::String; +#[cfg(feature = "alloc")] +use crate::lib::std::vec::Vec; + +/// abstract method to calculate the input length +pub trait InputLength { + /// calculates the input length, as indicated by its name, + /// and the name of the trait itself + fn input_len(&self) -> usize; +} + +impl<'a, T> InputLength for &'a [T] { + #[inline] + fn input_len(&self) -> usize { + self.len() + } +} + +impl<'a> InputLength for &'a str { + #[inline] + fn input_len(&self) -> usize { + self.len() + } +} + +impl<'a> InputLength for (&'a [u8], usize) { + #[inline] + fn input_len(&self) -> usize { + //println!("bit input length for ({:?}, {}):", self.0, self.1); + //println!("-> {}", self.0.len() * 8 - self.1); + self.0.len() * 8 - self.1 + } +} + +/// useful functions to calculate the offset between slices and show a hexdump of a slice +pub trait Offset { + /// offset between the first byte of self and the first byte of the argument + fn offset(&self, second: &Self) -> usize; +} + +impl Offset for [u8] { + fn offset(&self, second: &Self) -> usize { + let fst = self.as_ptr(); + let snd = second.as_ptr(); + + snd as usize - fst as usize + } +} + +impl<'a> Offset for &'a [u8] { + fn offset(&self, second: &Self) -> usize { + let fst = self.as_ptr(); + let snd = second.as_ptr(); + + snd as usize - fst as usize + } +} + +impl Offset for str { + fn offset(&self, second: &Self) -> usize { + let fst = self.as_ptr(); + let snd = second.as_ptr(); + + snd as usize - fst as usize + } +} + +impl<'a> Offset for &'a str { + fn offset(&self, second: &Self) -> usize { + let fst = self.as_ptr(); + let snd = second.as_ptr(); + + snd as usize - fst as usize + } +} + +/// Helper trait for types that can be viewed as a byte slice +pub trait AsBytes { + /// casts the input type to a byte slice + fn as_bytes(&self) -> &[u8]; +} + +impl<'a> AsBytes for &'a str { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + ::as_bytes(self) + } +} + +impl AsBytes for str { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + self.as_ref() + } +} + +impl<'a> AsBytes for &'a [u8] { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + *self + } +} + +impl AsBytes for [u8] { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + self + } +} + +macro_rules! as_bytes_array_impls { + ($($N:expr)+) => { + $( + impl<'a> AsBytes for &'a [u8; $N] { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + *self + } + } + + impl AsBytes for [u8; $N] { + #[inline(always)] + fn as_bytes(&self) -> &[u8] { + self + } + } + )+ + }; +} + +as_bytes_array_impls! { + 0 1 2 3 4 5 6 7 8 9 + 10 11 12 13 14 15 16 17 18 19 + 20 21 22 23 24 25 26 27 28 29 + 30 31 32 +} + +/// transforms common types to a char for basic token parsing +pub trait AsChar { + /// makes a char from self + fn as_char(self) -> char; + + /// tests that self is an alphabetic character + /// + /// warning: for `&str` it recognizes alphabetic + /// characters outside of the 52 ASCII letters + fn is_alpha(self) -> bool; + + /// tests that self is an alphabetic character + /// or a decimal digit + fn is_alphanum(self) -> bool; + /// tests that self is a decimal digit + fn is_dec_digit(self) -> bool; + /// tests that self is an hex digit + fn is_hex_digit(self) -> bool; + /// tests that self is an octal digit + fn is_oct_digit(self) -> bool; + /// gets the len in bytes for self + fn len(self) -> usize; +} + +impl AsChar for u8 { + #[inline] + fn as_char(self) -> char { + self as char + } + #[inline] + fn is_alpha(self) -> bool { + (self >= 0x41 && self <= 0x5A) || (self >= 0x61 && self <= 0x7A) + } + #[inline] + fn is_alphanum(self) -> bool { + self.is_alpha() || self.is_dec_digit() + } + #[inline] + fn is_dec_digit(self) -> bool { + self >= 0x30 && self <= 0x39 + } + #[inline] + fn is_hex_digit(self) -> bool { + (self >= 0x30 && self <= 0x39) || (self >= 0x41 && self <= 0x46) || (self >= 0x61 && self <= 0x66) + } + #[inline] + fn is_oct_digit(self) -> bool { + self >= 0x30 && self <= 0x37 + } + #[inline] + fn len(self) -> usize { + 1 + } +} +impl<'a> AsChar for &'a u8 { + #[inline] + fn as_char(self) -> char { + *self as char + } + #[inline] + fn is_alpha(self) -> bool { + (*self >= 0x41 && *self <= 0x5A) || (*self >= 0x61 && *self <= 0x7A) + } + #[inline] + fn is_alphanum(self) -> bool { + self.is_alpha() || self.is_dec_digit() + } + #[inline] + fn is_dec_digit(self) -> bool { + *self >= 0x30 && *self <= 0x39 + } + #[inline] + fn is_hex_digit(self) -> bool { + (*self >= 0x30 && *self <= 0x39) || (*self >= 0x41 && *self <= 0x46) || (*self >= 0x61 && *self <= 0x66) + } + #[inline] + fn is_oct_digit(self) -> bool { + *self >= 0x30 && *self <= 0x37 + } + #[inline] + fn len(self) -> usize { + 1 + } +} + +impl AsChar for char { + #[inline] + fn as_char(self) -> char { + self + } + #[inline] + fn is_alpha(self) -> bool { + self.is_ascii_alphabetic() + } + #[inline] + fn is_alphanum(self) -> bool { + self.is_alpha() || self.is_dec_digit() + } + #[inline] + fn is_dec_digit(self) -> bool { + self.is_ascii_digit() + } + #[inline] + fn is_hex_digit(self) -> bool { + self.is_ascii_hexdigit() + } + #[inline] + fn is_oct_digit(self) -> bool { + self.is_digit(8) + } + #[inline] + fn len(self) -> usize { + self.len_utf8() + } +} + +impl<'a> AsChar for &'a char { + #[inline] + fn as_char(self) -> char { + *self + } + #[inline] + fn is_alpha(self) -> bool { + self.is_ascii_alphabetic() + } + #[inline] + fn is_alphanum(self) -> bool { + self.is_alpha() || self.is_dec_digit() + } + #[inline] + fn is_dec_digit(self) -> bool { + self.is_ascii_digit() + } + #[inline] + fn is_hex_digit(self) -> bool { + self.is_ascii_hexdigit() + } + #[inline] + fn is_oct_digit(self) -> bool { + self.is_digit(8) + } + #[inline] + fn len(self) -> usize { + self.len_utf8() + } +} + +/// abstracts common iteration operations on the input type +pub trait InputIter { + /// the current input type is a sequence of that `Item` type. + /// + /// example: `u8` for `&[u8]` or `char` for &str` + type Item; + /// an iterator over the input type, producing the item and its position + /// for use with [Slice]. If we're iterating over `&str`, the position + /// corresponds to the byte index of the character + type Iter: Iterator; + + /// an iterator over the input type, producing the item + type IterElem: Iterator; + + /// returns an iterator over the elements and their byte offsets + fn iter_indices(&self) -> Self::Iter; + /// returns an iterator over the elements + fn iter_elements(&self) -> Self::IterElem; + /// finds the byte position of the element + fn position

(&self, predicate: P) -> Option + where + P: Fn(Self::Item) -> bool; + /// get the byte offset from the element's position in the stream + fn slice_index(&self, count: usize) -> Option; +} + +/// abstracts slicing operations +pub trait InputTake: Sized { + /// returns a slice of `count` bytes. panics if count > length + fn take(&self, count: usize) -> Self; + /// split the stream at the `count` byte offset. panics if count > length + fn take_split(&self, count: usize) -> (Self, Self); +} + +fn star(r_u8: &u8) -> u8 { + *r_u8 +} + +impl<'a> InputIter for &'a [u8] { + type Item = u8; + type Iter = Enumerate; + type IterElem = Map, fn(&u8) -> u8>; + + #[inline] + fn iter_indices(&self) -> Self::Iter { + self.iter_elements().enumerate() + } + #[inline] + fn iter_elements(&self) -> Self::IterElem { + self.iter().map(star) + } + #[inline] + fn position

(&self, predicate: P) -> Option + where + P: Fn(Self::Item) -> bool, + { + self.iter().position(|b| predicate(*b)) + } + #[inline] + fn slice_index(&self, count: usize) -> Option { + if self.len() >= count { + Some(count) + } else { + None + } + } +} + +impl<'a> InputTake for &'a [u8] { + #[inline] + fn take(&self, count: usize) -> Self { + &self[0..count] + } + #[inline] + fn take_split(&self, count: usize) -> (Self, Self) { + let (prefix, suffix) = self.split_at(count); + (suffix, prefix) + } +} + +impl<'a> InputIter for &'a str { + type Item = char; + type Iter = CharIndices<'a>; + type IterElem = Chars<'a>; + #[inline] + fn iter_indices(&self) -> Self::Iter { + self.char_indices() + } + #[inline] + fn iter_elements(&self) -> Self::IterElem { + self.chars() + } + fn position

(&self, predicate: P) -> Option + where + P: Fn(Self::Item) -> bool, + { + for (o, c) in self.char_indices() { + if predicate(c) { + return Some(o); + } + } + None + } + #[inline] + fn slice_index(&self, count: usize) -> Option { + let mut cnt = 0; + for (index, _) in self.char_indices() { + if cnt == count { + return Some(index); + } + cnt += 1; + } + if cnt == count { + return Some(self.len()); + } + None + } +} + +impl<'a> InputTake for &'a str { + #[inline] + fn take(&self, count: usize) -> Self { + &self[..count] + } + + // return byte index + #[inline] + fn take_split(&self, count: usize) -> (Self, Self) { + (&self[count..], &self[..count]) + } +} + +/// Dummy trait used for default implementations (currently only used for `InputTakeAtPosition`). +/// +/// When implementing a custom input type, it is possible to use directly the +/// default implementation: if the input type implements `InputLength`, `InputIter`, +/// `InputTake` and `Clone`, you can implement `UnspecializedInput` and get +/// a default version of `InputTakeAtPosition`. +/// +/// For performance reasons, you might want to write a custom implementation of +/// `InputTakeAtPosition` (like the one for `&[u8]`). +pub trait UnspecializedInput {} + +/// methods to take as much input as possible until the provided function returns true for the current element +/// +/// a large part of nom's basic parsers are built using this trait +pub trait InputTakeAtPosition: Sized { + /// the current input type is a sequence of that `Item` type. + /// + /// example: `u8` for `&[u8]` or `char` for &str` + type Item; + + /// looks for the first element of the input type for which the condition returns true, + /// and returns the input up to this position + /// + /// *streaming version*: if no element is found matching the condition, this will return `Incomplete` + fn split_at_position>(&self, predicate: P) -> IResult + where + P: Fn(Self::Item) -> bool; + + /// looks for the first element of the input type for which the condition returns true + /// and returns the input up to this position + /// + /// fails if the produced slice is empty + /// + /// *streaming version*: if no element is found matching the condition, this will return `Incomplete` + fn split_at_position1>(&self, predicate: P, e: ErrorKind) -> IResult + where + P: Fn(Self::Item) -> bool; + + /// looks for the first element of the input type for which the condition returns true, + /// and returns the input up to this position + /// + /// *complete version*: if no element is found matching the condition, this will return the whole input + fn split_at_position_complete>(&self, predicate: P) -> IResult + where + P: Fn(Self::Item) -> bool; + + /// looks for the first element of the input type for which the condition returns true + /// and returns the input up to this position + /// + /// fails if the produced slice is empty + /// + /// *complete version*: if no element is found matching the condition, this will return the whole input + fn split_at_position1_complete>(&self, predicate: P, e: ErrorKind) -> IResult + where + P: Fn(Self::Item) -> bool; +} + +impl InputTakeAtPosition for T { + type Item = ::Item; + + fn split_at_position>(&self, predicate: P) -> IResult + where + P: Fn(Self::Item) -> bool, + { + match self.position(predicate) { + Some(n) => Ok(self.take_split(n)), + None => Err(Err::Incomplete(Needed::Size(1))), + } + } + + fn split_at_position1>(&self, predicate: P, e: ErrorKind) -> IResult + where + P: Fn(Self::Item) -> bool, + { + match self.position(predicate) { + Some(0) => Err(Err::Error(E::from_error_kind(self.clone(), e))), + Some(n) => Ok(self.take_split(n)), + None => Err(Err::Incomplete(Needed::Size(1))), + } + } + + fn split_at_position_complete>(&self, predicate: P) -> IResult + where P: Fn(Self::Item) -> bool { + match self.split_at_position(predicate) { + Err(Err::Incomplete(_)) => Ok(self.take_split(self.input_len())), + res => res, + } + } + + fn split_at_position1_complete>(&self, predicate: P, e: ErrorKind) -> IResult + where P: Fn(Self::Item) -> bool { + match self.split_at_position1(predicate, e) { + Err(Err::Incomplete(_)) => if self.input_len() == 0 { + Err(Err::Error(E::from_error_kind(self.clone(), e))) + } else { + Ok(self.take_split(self.input_len())) + } + res => res, + } + } +} + +impl<'a> InputTakeAtPosition for &'a [u8] { + type Item = u8; + + fn split_at_position>(&self, predicate: P) -> IResult + where + P: Fn(Self::Item) -> bool, + { + match (0..self.len()).find(|b| predicate(self[*b])) { + Some(i) => Ok((&self[i..], &self[..i])), + None => Err(Err::Incomplete(Needed::Size(1))), + } + } + + fn split_at_position1>(&self, predicate: P, e: ErrorKind) -> IResult + where + P: Fn(Self::Item) -> bool, + { + match (0..self.len()).find(|b| predicate(self[*b])) { + Some(0) => Err(Err::Error(E::from_error_kind(self, e))), + Some(i) => Ok((&self[i..], &self[..i])), + None => Err(Err::Incomplete(Needed::Size(1))), + } + } + + fn split_at_position_complete>(&self, predicate: P) -> IResult + where P: Fn(Self::Item) -> bool { + match (0..self.len()).find(|b| predicate(self[*b])) { + Some(i) => Ok((&self[i..], &self[..i])), + None => Ok(self.take_split(self.input_len())), + } + } + + fn split_at_position1_complete>(&self, predicate: P, e: ErrorKind) -> IResult + where P: Fn(Self::Item) -> bool { + match (0..self.len()).find(|b| predicate(self[*b])) { + Some(0) => Err(Err::Error(E::from_error_kind(self, e))), + Some(i) => Ok((&self[i..], &self[..i])), + None => { + if self.len() == 0 { + Err(Err::Error(E::from_error_kind(self, e))) + } else { + Ok(self.take_split(self.input_len())) + } + }, + } + } +} + +impl<'a> InputTakeAtPosition for &'a str { + type Item = char; + + fn split_at_position>(&self, predicate: P) -> IResult + where + P: Fn(Self::Item) -> bool, + { + match self.find(predicate) { + Some(i) => Ok((&self[i..], &self[..i])), + None => Err(Err::Incomplete(Needed::Size(1))), + } + } + + fn split_at_position1>(&self, predicate: P, e: ErrorKind) -> IResult + where + P: Fn(Self::Item) -> bool, + { + match self.find(predicate) { + Some(0) => Err(Err::Error(E::from_error_kind(self, e))), + Some(i) => Ok((&self[i..], &self[..i])), + None => Err(Err::Incomplete(Needed::Size(1))), + } + } + + fn split_at_position_complete>(&self, predicate: P) -> IResult + where P: Fn(Self::Item) -> bool { + match self.find(predicate) { + Some(i) => Ok((&self[i..], &self[..i])), + None => Ok(self.take_split(self.input_len())) + } + } + + fn split_at_position1_complete>(&self, predicate: P, e: ErrorKind) -> IResult + where P: Fn(Self::Item) -> bool { + match self.find(predicate) { + Some(0) => Err(Err::Error(E::from_error_kind(self, e))), + Some(i) => Ok((&self[i..], &self[..i])), + None => { + if self.len() == 0 { + Err(Err::Error(E::from_error_kind(self, e))) + } else { + Ok(self.take_split(self.input_len())) + } + }, + } + } +} + +/// indicates wether a comparison was successful, an error, or +/// if more data was needed +#[derive(Debug, PartialEq)] +pub enum CompareResult { + /// comparison was successful + Ok, + /// we need more data to be sure + Incomplete, + /// comparison failed + Error, +} + +/// abstracts comparison operations +pub trait Compare { + /// compares self to another value for equality + fn compare(&self, t: T) -> CompareResult; + /// compares self to another value for equality + /// independently of the case. + /// + /// warning: for `&str`, the comparison is done + /// by lowercasing both strings and comparing + /// the result. This is a temporary solution until + /// a better one appears + fn compare_no_case(&self, t: T) -> CompareResult; +} + +impl<'a, 'b> Compare<&'b [u8]> for &'a [u8] { + #[inline(always)] + fn compare(&self, t: &'b [u8]) -> CompareResult { + let pos = self.iter().zip(t.iter()).position(|(a, b)| a != b); + + match pos { + Some(_) => CompareResult::Error, + None => { + if self.len() >= t.len() { + CompareResult::Ok + } else { + CompareResult::Incomplete + } + } + } + + /* + let len = self.len(); + let blen = t.len(); + let m = if len < blen { len } else { blen }; + let reduced = &self[..m]; + let b = &t[..m]; + + if reduced != b { + CompareResult::Error + } else if m < blen { + CompareResult::Incomplete + } else { + CompareResult::Ok + } + */ + } + + #[inline(always)] + fn compare_no_case(&self, t: &'b [u8]) -> CompareResult { + let len = self.len(); + let blen = t.len(); + let m = if len < blen { len } else { blen }; + let reduced = &self[..m]; + let other = &t[..m]; + + if !reduced.iter().zip(other).all(|(a, b)| match (*a, *b) { + (0..=64, 0..=64) | (91..=96, 91..=96) | (123..=255, 123..=255) => a == b, + (65..=90, 65..=90) | (97..=122, 97..=122) | (65..=90, 97..=122) | (97..=122, 65..=90) => *a | 0b00_10_00_00 == *b | 0b00_10_00_00, + _ => false, + }) { + CompareResult::Error + } else if m < blen { + CompareResult::Incomplete + } else { + CompareResult::Ok + } + } +} + +impl<'a, 'b> Compare<&'b str> for &'a [u8] { + #[inline(always)] + fn compare(&self, t: &'b str) -> CompareResult { + self.compare(AsBytes::as_bytes(t)) + } + #[inline(always)] + fn compare_no_case(&self, t: &'b str) -> CompareResult { + self.compare_no_case(AsBytes::as_bytes(t)) + } +} + +impl<'a, 'b> Compare<&'b str> for &'a str { + #[inline(always)] + fn compare(&self, t: &'b str) -> CompareResult { + let pos = self.chars().zip(t.chars()).position(|(a, b)| a != b); + + match pos { + Some(_) => CompareResult::Error, + None => { + if self.len() >= t.len() { + CompareResult::Ok + } else { + CompareResult::Incomplete + } + } + } + } + + //FIXME: this version is too simple and does not use the current locale + #[inline(always)] + fn compare_no_case(&self, t: &'b str) -> CompareResult { + let pos = self + .chars() + .zip(t.chars()) + .position(|(a, b)| a.to_lowercase().zip(b.to_lowercase()).any(|(a, b)| a != b)); + + match pos { + Some(_) => CompareResult::Error, + None => { + if self.len() >= t.len() { + CompareResult::Ok + } else { + CompareResult::Incomplete + } + } + } + } +} + +/// look for a token in self +pub trait FindToken { + /// returns true if self contains the token + fn find_token(&self, token: T) -> bool; +} + +impl<'a> FindToken for &'a [u8] { + fn find_token(&self, token: u8) -> bool { + memchr::memchr(token, self).is_some() + } +} + +impl<'a> FindToken for &'a str { + fn find_token(&self, token: u8) -> bool { + self.as_bytes().find_token(token) + } +} + +impl<'a, 'b> FindToken<&'a u8> for &'b [u8] { + fn find_token(&self, token: &u8) -> bool { + memchr::memchr(*token, self).is_some() + } +} + +impl<'a, 'b> FindToken<&'a u8> for &'b str { + fn find_token(&self, token: &u8) -> bool { + self.as_bytes().find_token(token) + } +} + +impl<'a> FindToken for &'a [u8] { + fn find_token(&self, token: char) -> bool { + for i in self.iter() { + if token as u8 == *i { + return true; + } + } + false + } +} + +impl<'a> FindToken for &'a str { + fn find_token(&self, token: char) -> bool { + for i in self.chars() { + if token == i { + return true; + } + } + false + } +} + +/// look for a substring in self +pub trait FindSubstring { + /// returns the byte position of the substring if it is found + fn find_substring(&self, substr: T) -> Option; +} + +impl<'a, 'b> FindSubstring<&'b [u8]> for &'a [u8] { + fn find_substring(&self, substr: &'b [u8]) -> Option { + let substr_len = substr.len(); + + if substr_len == 0 { + // an empty substring is found at position 0 + // This matches the behavior of str.find(""). + Some(0) + } else if substr_len == 1 { + memchr::memchr(substr[0], self) + } else if substr_len > self.len() { + None + } else { + let max = self.len() - substr_len; + let mut offset = 0; + let mut haystack = &self[..]; + + while let Some(position) = memchr::memchr(substr[0], haystack) { + offset += position; + + if offset > max { + return None; + } + + if &haystack[position..position + substr_len] == substr { + return Some(offset); + } + + haystack = &haystack[position + 1..]; + offset += 1; + } + + None + } + } +} + +impl<'a, 'b> FindSubstring<&'b str> for &'a [u8] { + fn find_substring(&self, substr: &'b str) -> Option { + self.find_substring(AsBytes::as_bytes(substr)) + } +} + +impl<'a, 'b> FindSubstring<&'b str> for &'a str { + //returns byte index + fn find_substring(&self, substr: &'b str) -> Option { + self.find(substr) + } +} + +/// used to integrate str's parse() method +pub trait ParseTo { + /// succeeds if `parse()` succeeded. The byte slice implementation + /// will first convert it to a &str, then apply the `parse()` function + fn parse_to(&self) -> Option; +} + +impl<'a, R: FromStr> ParseTo for &'a [u8] { + fn parse_to(&self) -> Option { + from_utf8(self).ok().and_then(|s| s.parse().ok()) + } +} + +impl<'a, R: FromStr> ParseTo for &'a str { + fn parse_to(&self) -> Option { + self.parse().ok() + } +} + +/// slicing operations using ranges +/// +/// this trait is loosely based on +/// `Index`, but can actually return +/// something else than a `&[T]` or `&str` +pub trait Slice { + /// slices self according to the range argument + fn slice(&self, range: R) -> Self; +} + +macro_rules! impl_fn_slice { + ( $ty:ty ) => { + fn slice(&self, range:$ty) -> Self { + &self[range] + } + } +} + +macro_rules! slice_range_impl { + ( [ $for_type:ident ], $ty:ty ) => { + impl<'a, $for_type> Slice<$ty> for &'a [$for_type] { + impl_fn_slice!( $ty ); + } + }; + ( $for_type:ty, $ty:ty ) => { + impl<'a> Slice<$ty> for &'a $for_type { + impl_fn_slice!( $ty ); + } + } +} + +macro_rules! slice_ranges_impl { + ( [ $for_type:ident ] ) => { + slice_range_impl! {[$for_type], Range} + slice_range_impl! {[$for_type], RangeTo} + slice_range_impl! {[$for_type], RangeFrom} + slice_range_impl! {[$for_type], RangeFull} + }; + ( $for_type:ty ) => { + slice_range_impl! {$for_type, Range} + slice_range_impl! {$for_type, RangeTo} + slice_range_impl! {$for_type, RangeFrom} + slice_range_impl! {$for_type, RangeFull} + } +} + +slice_ranges_impl! {str} +slice_ranges_impl! {[T]} + +macro_rules! array_impls { + ($($N:expr)+) => { + $( + impl InputLength for [u8; $N] { + #[inline] + fn input_len(&self) -> usize { + self.len() + } + } + + impl<'a> InputLength for &'a [u8; $N] { + #[inline] + fn input_len(&self) -> usize { + self.len() + } + } + + impl<'a> Compare<[u8; $N]> for &'a [u8] { + #[inline(always)] + fn compare(&self, t: [u8; $N]) -> CompareResult { + self.compare(&t[..]) + } + + #[inline(always)] + fn compare_no_case(&self, t: [u8;$N]) -> CompareResult { + self.compare_no_case(&t[..]) + } + } + + impl<'a,'b> Compare<&'b [u8; $N]> for &'a [u8] { + #[inline(always)] + fn compare(&self, t: &'b [u8; $N]) -> CompareResult { + self.compare(&t[..]) + } + + #[inline(always)] + fn compare_no_case(&self, t: &'b [u8;$N]) -> CompareResult { + self.compare_no_case(&t[..]) + } + } + + impl FindToken for [u8; $N] { + fn find_token(&self, token: u8) -> bool { + memchr::memchr(token, &self[..]).is_some() + } + } + + impl<'a> FindToken<&'a u8> for [u8; $N] { + fn find_token(&self, token: &u8) -> bool { + memchr::memchr(*token, &self[..]).is_some() + } + } + )+ + }; +} + +array_impls! { + 0 1 2 3 4 5 6 7 8 9 + 10 11 12 13 14 15 16 17 18 19 + 20 21 22 23 24 25 26 27 28 29 + 30 31 32 +} + +/// abstracts something which can extend an `Extend` +/// used to build modified input slices in `escaped_transform` +pub trait ExtendInto { + + /// the current input type is a sequence of that `Item` type. + /// + /// example: `u8` for `&[u8]` or `char` for &str` + type Item; + + /// the type that will be produced + type Extender: Extend; + + /// create a new `Extend` of the correct type + fn new_builder(&self) -> Self::Extender; + /// accumulate the input into an accumulator + fn extend_into(&self, acc: &mut Self::Extender); +} + +#[cfg(feature = "alloc")] +impl ExtendInto for [u8] { + type Item = u8; + type Extender = Vec; + + #[inline] + fn new_builder(&self) -> Vec { + Vec::new() + } + #[inline] + fn extend_into(&self, acc: &mut Vec) { + acc.extend(self.iter().cloned()); + } +} + +#[cfg(feature = "alloc")] +impl ExtendInto for &[u8] { + type Item = u8; + type Extender = Vec; + + #[inline] + fn new_builder(&self) -> Vec { + Vec::new() + } + #[inline] + fn extend_into(&self, acc: &mut Vec) { + acc.extend(self.iter().cloned()); + } +} + + +#[cfg(feature = "alloc")] +impl ExtendInto for str { + type Item = char; + type Extender = String; + + #[inline] + fn new_builder(&self) -> String { + String::new() + } + #[inline] + fn extend_into(&self, acc: &mut String) { + acc.push_str(self); + } +} + +#[cfg(feature = "alloc")] +impl ExtendInto for &str { + type Item = char; + type Extender = String; + + #[inline] + fn new_builder(&self) -> String { + String::new() + } + #[inline] + fn extend_into(&self, acc: &mut String) { + acc.push_str(self); + } +} + +#[cfg(feature = "alloc")] +impl ExtendInto for char { + type Item = char; + type Extender = String; + + #[inline] + fn new_builder(&self) -> String { + String::new() + } + #[inline] + fn extend_into(&self, acc: &mut String) { + acc.push(*self); + } +} + +/// Helper trait to convert numbers to usize +/// +/// by default, usize implements `From` and `From` but not +/// `From` and `From` because that would be invalid on some +/// platforms. This trait implements the conversion for platforms +/// with 32 and 64 bits pointer platforms +pub trait ToUsize { + /// converts self to usize + fn to_usize(&self) -> usize; +} + +impl ToUsize for u8 { + #[inline] + fn to_usize(&self) -> usize { + *self as usize + } +} + +impl ToUsize for u16 { + #[inline] + fn to_usize(&self) -> usize { + *self as usize + } +} + +impl ToUsize for usize { + #[inline] + fn to_usize(&self) -> usize { + *self + } +} + +#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] +impl ToUsize for u32 { + #[inline] + fn to_usize(&self) -> usize { + *self as usize + } +} + +#[cfg(target_pointer_width = "64")] +impl ToUsize for u64 { + #[inline] + fn to_usize(&self) -> usize { + *self as usize + } +} + +/// equivalent From implementation to avoid orphan rules in bits parsers +pub trait ErrorConvert { + /// transform to another error type + fn convert(self) -> E; +} + +impl ErrorConvert<(I, ErrorKind)> for ((I, usize), ErrorKind) { + fn convert(self) -> (I, ErrorKind) { + ((self.0).0, self.1) + } +} + +impl ErrorConvert<((I, usize), ErrorKind)> for (I, ErrorKind) { + fn convert(self) -> ((I, usize), ErrorKind) { + ((self.0, 0), self.1) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_offset_u8() { + let s = b"abcd123"; + let a = &s[..]; + let b = &a[2..]; + let c = &a[..4]; + let d = &a[3..5]; + assert_eq!(a.offset(b), 2); + assert_eq!(a.offset(c), 0); + assert_eq!(a.offset(d), 3); + } + + #[test] + fn test_offset_str() { + let s = "abcřèÂßÇd123"; + let a = &s[..]; + let b = &a[7..]; + let c = &a[..5]; + let d = &a[5..9]; + assert_eq!(a.offset(b), 7); + assert_eq!(a.offset(c), 0); + assert_eq!(a.offset(d), 5); + } +} diff --git a/third_party/rust/nom/src/util.rs b/third_party/rust/nom/src/util.rs new file mode 100644 index 0000000000..488bbfb7d1 --- /dev/null +++ b/third_party/rust/nom/src/util.rs @@ -0,0 +1,214 @@ +#[cfg(feature = "std")] +use crate::internal::IResult; +#[cfg(feature = "std")] +use std::fmt::Debug; + +#[cfg(feature = "std")] +/// Helper trait to show a byte slice as a hex dump +pub trait HexDisplay { + /// Converts the value of `self` to a hex dump, returning the owned + /// string. + fn to_hex(&self, chunk_size: usize) -> String; + + /// Converts the value of `self` to a hex dump beginning at `from` address, returning the owned + /// string. + fn to_hex_from(&self, chunk_size: usize, from: usize) -> String; +} + +#[cfg(feature = "std")] +static CHARS: &'static [u8] = b"0123456789abcdef"; + +#[cfg(feature = "std")] +impl HexDisplay for [u8] { + #[allow(unused_variables)] + fn to_hex(&self, chunk_size: usize) -> String { + self.to_hex_from(chunk_size, 0) + } + + #[allow(unused_variables)] + fn to_hex_from(&self, chunk_size: usize, from: usize) -> String { + let mut v = Vec::with_capacity(self.len() * 3); + let mut i = from; + for chunk in self.chunks(chunk_size) { + let s = format!("{:08x}", i); + for &ch in s.as_bytes().iter() { + v.push(ch); + } + v.push(b'\t'); + + i += chunk_size; + + for &byte in chunk { + v.push(CHARS[(byte >> 4) as usize]); + v.push(CHARS[(byte & 0xf) as usize]); + v.push(b' '); + } + if chunk_size > chunk.len() { + for j in 0..(chunk_size - chunk.len()) { + v.push(b' '); + v.push(b' '); + v.push(b' '); + } + } + v.push(b'\t'); + + for &byte in chunk { + if (byte >= 32 && byte <= 126) || byte >= 128 { + v.push(byte); + } else { + v.push(b'.'); + } + } + v.push(b'\n'); + } + + String::from_utf8_lossy(&v[..]).into_owned() + } +} + +#[cfg(feature = "std")] +impl HexDisplay for str { + #[allow(unused_variables)] + fn to_hex(&self, chunk_size: usize) -> String { + self.to_hex_from(chunk_size, 0) + } + + #[allow(unused_variables)] + fn to_hex_from(&self, chunk_size: usize, from: usize) -> String { + self.as_bytes().to_hex_from(chunk_size, from) + } +} + +#[doc(hidden)] +#[macro_export] +macro_rules! nom_line ( + () => (line!()); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! nom_println ( + ($($args:tt)*) => (println!($($args)*)); +); + +#[doc(hidden)] +#[macro_export] +macro_rules! nom_stringify ( + ($($args:tt)*) => (stringify!($($args)*)); +); + + +/// Prints a message if the parser fails +/// +/// The message prints the `Error` or `Incomplete` +/// and the parser's calling code +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(f, dbg!( tag!( "abcd" ) ) ); +/// +/// let a = &b"efgh"[..]; +/// +/// // Will print the following message: +/// // Error(Position(0, [101, 102, 103, 104])) at l.5 by ' tag ! ( "abcd" ) ' +/// f(a); +/// # } +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! dbg ( + ($i: expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + let l = nom_line!(); + match $submac!($i, $($args)*) { + Err(e) => { + nom_println!("Err({:?}) at l.{} by ' {} '", e, l, nom_stringify!($submac!($($args)*))); + Err(e) + }, + a => a, + } + } + ); + + ($i:expr, $f:ident) => ( + dbg!($i, call!($f)); + ); +); + +/// Prints a message and the input if the parser fails +/// +/// The message prints the `Error` or `Incomplete` +/// and the parser's calling code. +/// +/// It also displays the input in hexdump format +/// +/// ```rust +/// use nom::{IResult, dbg_dmp, bytes::complete::tag}; +/// +/// fn f(i: &[u8]) -> IResult<&[u8], &[u8]> { +/// dbg_dmp(tag("abcd"), "tag")(i) +/// } +/// +/// let a = &b"efghijkl"[..]; +/// +/// // Will print the following message: +/// // Error(Position(0, [101, 102, 103, 104, 105, 106, 107, 108])) at l.5 by ' tag ! ( "abcd" ) ' +/// // 00000000 65 66 67 68 69 6a 6b 6c efghijkl +/// f(a); +/// ``` +#[cfg(feature = "std")] +pub fn dbg_dmp<'a, F, O, E: Debug>(f: F, context: &'static str) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], O, E> + where F: Fn(&'a [u8]) -> IResult<&'a [u8], O, E> { + move |i: &'a [u8]| { + match f(i) { + Err(e) => { + println!("{}: Error({:?}) at:\n{}", context, e, i.to_hex(8)); + Err(e) + }, + a => a, + } + } +} + +/// Prints a message and the input if the parser fails +/// +/// The message prints the `Error` or `Incomplete` +/// and the parser's calling code. +/// +/// It also displays the input in hexdump format +/// +/// ```ignore +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(f, dbg_dmp!( tag!( "abcd" ) ) ); +/// +/// let a = &b"efghijkl"[..]; +/// +/// // Will print the following message: +/// // Error(Position(0, [101, 102, 103, 104, 105, 106, 107, 108])) at l.5 by ' tag ! ( "abcd" ) ' +/// // 00000000 65 66 67 68 69 6a 6b 6c efghijkl +/// f(a); +/// # } +#[macro_export(local_inner_macros)] +#[cfg(feature = "std")] +macro_rules! dbg_dmp ( + ($i: expr, $submac:ident!( $($args:tt)* )) => ( + { + use $crate::HexDisplay; + let l = nom_line!(); + match $submac!($i, $($args)*) { + Err(e) => { + nom_println!("Error({:?}) at l.{} by ' {} '\n{}", e, l, nom_stringify!($submac!($($args)*)), $i.to_hex(8)); + Err(e) + }, + a => a, + } + } + ); + + ($i:expr, $f:ident) => ( + dbg_dmp!($i, call!($f)); + ); +); + diff --git a/third_party/rust/nom/src/whitespace.rs b/third_party/rust/nom/src/whitespace.rs new file mode 100644 index 0000000000..9ed3f55aad --- /dev/null +++ b/third_party/rust/nom/src/whitespace.rs @@ -0,0 +1,1077 @@ +//! Support for whitespace delimited formats +//! +//! a lot of textual formats allows spaces and other +//! types of separators between tokens. Handling it +//! manually with nom means wrapping all parsers +//! like this: +//! +//! ```ignore +//! named!(token, delimited!(space, tk, space)); +//! ``` +//! +//! To ease the development of such parsers, you +//! can use the whitespace parsing facility, which works +//! as follows: +//! +//! ``` +//! # #[macro_use] extern crate nom; +//! # fn main() { +//! named!(tuple<&[u8], (&[u8], &[u8]) >, +//! ws!(tuple!( take!(3), tag!("de") )) +//! ); +//! +//! assert_eq!( +//! tuple(&b" \t abc de fg"[..]), +//! Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..]))) +//! ); +//! # } +//! ``` +//! +//! The `ws!` combinator will modify the parser to +//! intersperse space parsers everywhere. By default, +//! it will consume the following characters: `" \t\r\n"`. +//! +//! If you want to modify that behaviour, you can make +//! your own whitespace wrapper. As an example, if +//! you don't want to consume ends of lines, only +//! spaces and tabs, you can do it like this: +//! +//! ``` +//! # #[macro_use] extern crate nom; +//! named!(pub space, eat_separator!(&b" \t"[..])); +//! +//! #[macro_export] +//! macro_rules! sp ( +//! ($i:expr, $($args:tt)*) => ( +//! { +//! use nom::Err; +//! +//! match sep!($i, space, $($args)*) { +//! Err(e) => Err(e), +//! Ok((i1,o)) => { +//! match space(i1) { +//! Err(e) => Err(Err::convert(e)), +//! Ok((i2,_)) => Ok((i2, o)) +//! } +//! } +//! } +//! } +//! ) +//! ); +//! +//! # fn main() { +//! named!(tuple<&[u8], (&[u8], &[u8]) >, +//! sp!(tuple!( take!(3), tag!("de") )) +//! ); +//! +//! assert_eq!( +//! tuple(&b" \t abc de fg"[..]), +//! Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..]))) +//! ); +//! # } +//! ``` +//! +//! This combinator works by replacing each combinator with +//! a version that supports wrapping with separator parsers. +//! It will not support the combinators you wrote in your +//! own code. You can still manually wrap them with the separator +//! you want, or you can copy the macros defined in src/whitespace.rs +//! and modify them to support a new combinator: +//! +//! * copy the combinator's code here, add the _sep suffix +//! * add the `$separator:expr` as second argument +//! * wrap any sub parsers with sep!($separator, $submac!($($args)*)) +//! * reference it in the definition of `sep!` as follows: +//! +//! ```ignore +//! ($i:expr, $separator:path, my_combinator ! ($($rest:tt)*) ) => { +//! wrap_sep!($i, +//! $separator, +//! my_combinator_sep!($separator, $($rest)*) +//! ) +//! }; +//! ``` +//! + +/// applies the separator parser before the other parser +#[macro_export(local_inner_macros)] +macro_rules! wrap_sep ( + ($i:expr, $separator:expr, $submac:ident!( $($args:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + use $crate::{Err,IResult}; + + fn unify_types(_: &IResult, _: &IResult) {} + + let sep_res = ($separator)($i); + match sep_res { + Ok((i1,_)) => { + let res = $submac!(i1, $($args)*); + unify_types(&sep_res, &res); + res + }, + Err(e) => Err(Err::convert(e)), + } + }); + ($i:expr, $separator:expr, $f:expr) => ( + wrap_sep!($i, $separator, call!($f)) + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! pair_sep ( + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + tuple!( + $i, + sep!($separator, $submac!($($args)*)), + sep!($separator, $submac2!($($args2)*)) + ) + ); + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $g:expr) => ( + pair_sep!($i, $separator, $submac!($($args)*), call!($g)); + ); + ($i:expr, $separator:path, $f:expr, $submac:ident!( $($args:tt)* )) => ( + pair_sep!($i, $separator, call!($f), $submac!($($args)*)); + ); + ($i:expr, $separator:path, $f:expr, $g:expr) => ( + pair_sep!($i, $separator, call!($f), call!($g)); + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! delimited_sep ( + ($i:expr, $separator:path, $submac1:ident!( $($args1:tt)* ), $($rest:tt)+) => ({ + use $crate::lib::std::result::Result::*; + + match tuple_sep!($i, $separator, (), $submac1!($($args1)*), $($rest)+) { + Err(e) => Err(e), + Ok((remaining, (_,o,_))) => { + Ok((remaining, o)) + } + } + }); + ($i:expr, $separator:path, $f:expr, $($rest:tt)+) => ( + delimited_sep!($i, $separator, call!($f), $($rest)+); + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! separated_pair_sep ( + ($i:expr, $separator:path, $submac1:ident!( $($args1:tt)* ), $($rest:tt)+) => ({ + use $crate::lib::std::result::Result::*; + + match tuple_sep!($i, $separator, (), $submac1!($($args1)*), $($rest)+) { + Err(e) => Err(e), + Ok((remaining, (o1,_,o2))) => { + Ok((remaining, (o1,o2))) + } + } + }); + ($i:expr, $separator:path, $f:expr, $($rest:tt)+) => ( + separated_pair_sep!($i, $separator, call!($f), $($rest)+); + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! preceded_sep ( + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match pair_sep!($i, $separator, $submac!($($args)*), $submac2!($($args2)*)) { + Err(e) => Err(e), + Ok((remaining, (_,o))) => { + Ok((remaining, o)) + } + } + }); + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $g:expr) => ( + preceded_sep!($i, $separator, $submac!($($args)*), call!($g)); + ); + ($i:expr, $separator:path, $f:expr, $submac:ident!( $($args:tt)* )) => ( + preceded_sep!($i, $separator, call!($f), $submac!($($args)*)); + ); + ($i:expr, $separator:path, $f:expr, $g:expr) => ( + preceded_sep!($i, $separator, call!($f), call!($g)); + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! terminated_sep ( + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match pair_sep!($i, $separator, $submac!($($args)*), $submac2!($($args2)*)) { + Err(e) => Err(e), + Ok((remaining, (o,_))) => { + Ok((remaining, o)) + } + } + }); + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $g:expr) => ( + terminated_sep!($i, $separator, $submac!($($args)*), call!($g)); + ); + ($i:expr, $separator:path, $f:expr, $submac:ident!( $($args:tt)* )) => ( + terminated_sep!($i, $separator, call!($f), $submac!($($args)*)); + ); + ($i:expr, $separator:path, $f:expr, $g:expr) => ( + terminated_sep!($i, $separator, call!($f), call!($g)); + ); +); + +/// Internal parser, do not use directly +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! tuple_sep ( + ($i:expr, $separator:path, ($($parsed:tt),*), $e:path, $($rest:tt)*) => ( + tuple_sep!($i, $separator, ($($parsed),*), call!($e), $($rest)*); + ); + ($i:expr, $separator:path, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,o)) => { + tuple_sep!(i, $separator, (o), $($rest)*) + } + } + } + ); + ($i:expr, $separator:path, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,o)) => { + tuple_sep!(i, $separator, ($($parsed)* , o), $($rest)*) + } + } + } + ); + ($i:expr, $separator:path, ($($parsed:tt),*), $e:path) => ( + tuple_sep!($i, $separator, ($($parsed),*), call!($e)); + ); + ($i:expr, $separator:path, (), $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,o)) => { + Ok((i, (o))) + } + } + } + ); + ($i:expr, $separator:path, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => ( + { + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,o)) => { + Ok((i, ($($parsed),* , o))) + } + } + } + ); + ($i:expr, $separator:path, ($($parsed:expr),*)) => ( + { + ::sts::result::Result::Ok(($i, ($($parsed),*))) + } + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! do_parse_sep ( + (__impl $i:expr, $separator:path, ( $($rest:expr),* )) => ( + $crate::lib::std::result::Result::Ok(($i, ( $($rest),* ))) + ); + + (__impl $i:expr, $separator:path, $e:ident >> $($rest:tt)*) => ( + do_parse_sep!(__impl $i, $separator, call!($e) >> $($rest)*); + ); + (__impl $i:expr, $separator:path, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,_)) => { + do_parse_sep!(__impl i, $separator, $($rest)*) + }, + } + } + ); + + (__impl $i:expr, $separator:path, $field:ident : $e:ident >> $($rest:tt)*) => ( + do_parse_sep!(__impl $i, $separator, $field: call!($e) >> $($rest)*); + ); + + (__impl $i:expr, $separator:path, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,o)) => { + let $field = o; + do_parse_sep!(__impl i, $separator, $($rest)*) + }, + } + } + ); + + // ending the chain + (__impl $i:expr, $separator:path, $e:ident >> ( $($rest:tt)* )) => ( + do_parse_sep!(__impl $i, $separator, call!($e) >> ( $($rest)* )); + ); + + (__impl $i:expr, $separator:path, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,_)) => { + Ok((i, ( $($rest)* ))) + }, + } + }); + + (__impl $i:expr, $separator:path, $field:ident : $e:ident >> ( $($rest:tt)* )) => ( + do_parse_sep!(__impl $i, $separator, $field: call!($e) >> ( $($rest)* ) ); + ); + + (__impl $i:expr, $separator:path, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(e) => Err(e), + Ok((i,o)) => { + let $field = o; + Ok((i, ( $($rest)* ))) + }, + } + }); + + ($i:expr, $separator:path, $($rest:tt)*) => ( + { + do_parse_sep!(__impl $i, $separator, $($rest)*) + } + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! permutation_sep ( + ($i:expr, $separator:path, $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::lib::std::option::Option::*; + use $crate::{Err,error::ErrorKind}; + + let mut res = permutation_init!((), $($rest)*); + let mut input = $i; + let mut error = None; + let mut needed = None; + + loop { + let mut all_done = true; + permutation_iterator_sep!(0, input, $separator, all_done, needed, res, $($rest)*); + + //if we reach that part, it means none of the parsers were able to read anything + if !all_done { + //FIXME: should wrap the error returned by the child parser + error = Option::Some(error_position!(input, ErrorKind::Permutation)); + } + break; + } + + if let Some(need) = needed { + Err(Err::convert(need)) + } else { + if let Some(unwrapped_res) = { permutation_unwrap!(0, (), res, $($rest)*) } { + Ok((input, unwrapped_res)) + } else { + if let Some(e) = error { + Err(Err::Error(error_node_position!($i, ErrorKind::Permutation, e))) + } else { + Err(Err::Error(error_position!($i, ErrorKind::Permutation))) + } + } + } + } + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! permutation_iterator_sep ( + ($it:tt,$i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $e:ident?, $($rest:tt)*) => ( + permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, call!($e), $($rest)*); + ); + ($it:tt,$i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $e:ident, $($rest:tt)*) => ( + permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, call!($e), $($rest)*); + ); + + ($it:tt, $i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => ({ + permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, $submac!($($args)*), $($rest)*); + }); + ($it:tt, $i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ({ + use $crate::lib::std::result::Result::*; + use $crate::Err; + + if $res.$it == $crate::lib::std::option::Option::None { + match {sep!($i, $separator, $submac!($($args)*))} { + Ok((i,o)) => { + $i = i; + $res.$it = $crate::lib::std::option::Option::Some(o); + continue; + }, + Err(Err::Error(_)) => { + $all_done = false; + }, + Err(e) => { + $needed = $crate::lib::std::option::Option::Some(e); + break; + } + }; + } + succ!($it, permutation_iterator_sep!($i, $separator, $all_done, $needed, $res, $($rest)*)); + }); + + ($it:tt,$i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $e:ident?) => ( + permutation_iterator_sep!($it, $i, $separator, $all_done, $res, call!($e)); + ); + ($it:tt,$i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $e:ident) => ( + permutation_iterator_sep!($it, $i, $separator, $all_done, $res, call!($e)); + ); + + ($it:tt, $i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )?) => ({ + permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, $submac!($($args)*)); + }); + ($it:tt, $i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )) => ({ + use $crate::lib::std::result::Result::*; + use $crate::Err; + + if $res.$it == $crate::lib::std::option::Option::None { + match sep!($i, $separator, $submac!($($args)*)) { + Ok((i,o)) => { + $i = i; + $res.$it = $crate::lib::std::option::Option::Some(o); + continue; + }, + Err(Err::Error(_)) => { + $all_done = false; + }, + Err(e) => { + $needed = $crate::lib::std::option::Option::Some(e); + break; + } + }; + } + }); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! alt_sep ( + (__impl $i:expr, $separator:path, $e:path | $($rest:tt)*) => ( + alt_sep!(__impl $i, $separator, call!($e) | $($rest)*); + ); + + (__impl $i:expr, $separator:path, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + let res = sep!($i, $separator, $subrule!($($args)*)); + match res { + Ok((_,_)) => res, + Err(Err::Error(_)) => alt_sep!(__impl $i, $separator, $($rest)*), + Err(e) => Err(e), + } + } + ); + + (__impl $i:expr, $separator:path, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + match sep!($i, $separator, $subrule!( $($args)* )) { + Ok((i,o)) => Ok((i,$gen(o))), + Err(Err::Error(_)) => { + alt_sep!(__impl $i, $separator, $($rest)+) + }, + Err(e) => Err(e), + } + } + ); + + (__impl $i:expr, $separator:path, $e:path => { $gen:expr } | $($rest:tt)*) => ( + alt_sep!(__impl $i, $separator, call!($e) => { $gen } | $($rest)*); + ); + + (__impl $i:expr, $separator:path, $e:path => { $gen:expr }) => ( + alt_sep!(__impl $i, $separator, call!($e) => { $gen }); + ); + + (__impl $i:expr, $separator:path, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + match sep!($i, $separator, $subrule!( $($args)* )) { + Ok((i,o)) => Ok((i,$gen(o))), + Err(Err::Error(e)) => { + fn unify_types(_: &T, _: &T) {} + let e2 = error_position!($i, $crate::error::ErrorKind::Alt); + unify_types(&e, &e2); + Err(Err::Error(e2)) + }, + Err(e) => Err(e), + } + } + ); + + (__impl $i:expr, $separator:path, $e:path) => ( + alt_sep!(__impl $i, $separator, call!($e)); + ); + + (__impl $i:expr, $separator:path, $subrule:ident!( $($args:tt)*)) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + match sep!($i, $separator, $subrule!( $($args)* )) { + Ok((i,o)) => Ok((i,o)), + Err(Err::Error(e)) => { + fn unify_types(_: &T, _: &T) {} + let e2 = error_position!($i, $crate::error::ErrorKind::Alt); + unify_types(&e, &e2); + Err(Err::Error(e2)) + }, + Err(e) => Err(e), + } + } + ); + + (__impl $i:expr) => ({ + use $crate::lib::std::result::Result::*; + use $crate::{Err,Needed,IResult}; + + Err(Err::Error(error_position!($i, $crate::error::ErrorKind::Alt))) + }); + + (__impl $i:expr, $separator:path) => ({ + use $crate::lib::std::result::Result::*; + use $crate::{Err,Needed,IResult}; + + Err(Err::Error(error_position!($i, $crate::error::ErrorKind::Alt))) + }); + + ($i:expr, $separator:path, $($rest:tt)*) => ( + { + alt_sep!(__impl $i, $separator, $($rest)*) + } + ); +); + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! switch_sep ( + (__impl $i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => ( + { + use $crate::lib::std::result::Result::*; + use $crate::Err; + + match sep!($i, $separator, $submac!($($args)*)) { + Err(Err::Error(e)) => Err(Err::Error(error_node_position!( + $i, $crate::error::ErrorKind::Switch, e + ))), + Err(Err::Failure(e)) => Err(Err::Failure( + error_node_position!($i, $crate::error::ErrorKind::Switch, e))), + Err(e) => Err(e), + Ok((i, o)) => { + match o { + $($p => match sep!(i, $separator, $subrule!($($args2)*)) { + Err(Err::Error(e)) => Err(Err::Error(error_node_position!( + $i, $crate::error::ErrorKind::Switch, e + ))), + Err(Err::Failure(e)) => Err(Err::Failure( + error_node_position!($i, $crate::error::ErrorKind::Switch, e))), + a => a, + }),*, + _ => Err(Err::Error(error_position!($i, $crate::error::ErrorKind::Switch))) + } + } + } + } + ); + ($i:expr, $separator:path, $submac:ident!( $($args:tt)*), $($rest:tt)*) => ( + { + switch_sep!(__impl $i, $separator, $submac!($($args)*), $($rest)*) + } + ); + ($i:expr, $separator:path, $e:path, $($rest:tt)*) => ( + { + switch_sep!(__impl $i, $separator, call!($e), $($rest)*) + } + ); +); + +#[doc(hidden)] +#[cfg(feature = "alloc")] +#[macro_export(local_inner_macros)] +macro_rules! separated_list_sep ( + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( + separated_list!( + $i, + sep!($separator, $submac!($($args)*)), + sep!($separator, $submac2!($($args2)*)) + ) + ); + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $g:expr) => ( + separated_list_sep!($i, $separator, $submac!($($args)*), call!($g)); + ); + ($i:expr, $separator:path, $f:expr, $submac:ident!( $($args:tt)* )) => ( + separated_list_sep!($i, $separator, call!($f), $submac!($($args)*)); + ); + ($i:expr, $separator:path, $f:expr, $g:expr) => ( + separated_list_sep!($i, $separator, call!($f), call!($g)); + ); +); + +/// helper macros to build a separator parser +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// named!(pub space, eat_separator!(&b" \t"[..])); +/// # fn main() {} +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! eat_separator ( + ($i:expr, $arr:expr) => ( + { + use $crate::{FindToken, InputTakeAtPosition}; + let input = $i; + input.split_at_position(|c| !$arr.find_token(c)) + } + ); +); + +/// sep is the parser rewriting macro for whitespace separated formats +/// +/// it takes as argument a space eating function and a parser tree, +/// and will intersperse the space parser everywhere +/// +/// ```ignore +/// #[macro_export(local_inner_macros)] +/// macro_rules! ws ( +/// ($i:expr, $($args:tt)*) => ( +/// { +/// use sp; +/// sep!($i, sp, $($args)*) +/// } +/// ) +/// ); +/// ``` +#[macro_export(local_inner_macros)] +macro_rules! sep ( + ($i:expr, $separator:path, tuple ! ($($rest:tt)*) ) => { + tuple_sep!($i, $separator, (), $($rest)*) + }; + ($i:expr, $separator:path, pair ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + pair_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, delimited ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + delimited_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, separated_pair ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + separated_pair_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, preceded ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + preceded_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, terminated ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + terminated_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, do_parse ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + do_parse_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, permutation ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + permutation_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, alt ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + alt_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, switch ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + switch_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, separated_list ! ($($rest:tt)*) ) => { + wrap_sep!($i, + $separator, + separated_list_sep!($separator, $($rest)*) + ) + }; + ($i:expr, $separator:path, many0 ! ($($rest:tt)*) ) => { + many0!($i, wrap_sep!($separator, $($rest)*)) + }; + ($i:expr, $separator:path, many1 ! ($($rest:tt)*) ) => { + many1!($i, wrap_sep!($separator, $($rest)*)) + }; + ($i:expr, $separator:path, return_error!( $($args:tt)* )) => { + return_error!($i, wrap_sep!($separator, $($args)*)) + }; +//FIXME: missing separated_nonempty_list, +// many_till, many_m_n, count, count_fixed, fold_many0, fold_many1, +// fold_many_m_n + ($i:expr, $separator:path, $submac:ident!( $($args:tt)* )) => { + wrap_sep!($i, $separator, $submac!($($args)*)) + }; + ($i:expr, $separator:path, $f:expr) => { + wrap_sep!($i, $separator, call!($f)) + }; +); + +/// `ws!(I -> IResult) => I -> IResult` +/// +/// transforms a parser to automatically consume +/// whitespace between each token. By default, +/// it takes the following characters: `" \t\r\n"`. +/// +/// If you need a whitespace parser consuming a +/// different set of characters, you can make +/// your own by reusing the `sep!` combinator. +/// +/// To use `ws!`, pass your parser as argument: +/// +/// ``` +/// # #[macro_use] extern crate nom; +/// # fn main() { +/// named!(tuple<&[u8], (&[u8], &[u8]) >, +/// ws!(tuple!( take!(3), tag!("de") )) +/// ); +/// +/// assert_eq!( +/// tuple(&b" \t abc de fg"[..]), +/// Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..]))) +/// ); +/// # } +/// ``` +/// +#[macro_export(local_inner_macros)] +#[deprecated(since = "5.0.0", note = "whitespace parsing only works with macros and will not be updated anymore")] +macro_rules! ws ( + ($i:expr, $($args:tt)*) => ( + { + use $crate::Err; + use $crate::lib::std::result::Result::*; + use $crate::character::complete::multispace0; + + match sep!($i, multispace0, $($args)*) { + Err(e) => Err(e), + Ok((i1,o)) => { + match (multispace0)(i1) { + Err(e) => Err(Err::convert(e)), + Ok((i2,_)) => Ok((i2, o)) + } + } + } + } + ) +); + +#[cfg(test)] +#[allow(dead_code)] +mod tests { + #[cfg(feature = "alloc")] + use crate::{ + error::ParseError, + lib::std::{ + string::{String, ToString}, + fmt::Debug + } + }; + use crate::internal::{Err, IResult, Needed}; + use crate::character::complete::multispace0 as sp; + use crate::error::ErrorKind; + + #[test] + fn spaaaaace() { + assert_eq!(sp::<_,(_,ErrorKind)>(&b" \t abc "[..]), Ok((&b"abc "[..], &b" \t "[..]))); + } + + #[test] + fn tag() { + named!(abc, ws!(tag!("abc"))); + + assert_eq!(abc(&b" \t abc def"[..]), Ok((&b"def"[..], &b"abc"[..]))); + } + + #[test] + fn pair() { + named!(pair_2<&[u8], (&[u8], &[u8]) >, + ws!(pair!( take!(3), tag!("de") )) + ); + + assert_eq!( + pair_2(&b" \t abc de fg"[..]), + Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..]))) + ); + } + + #[test] + fn preceded() { + named!(prec<&[u8], &[u8] >, + ws!(preceded!( take!(3), tag!("de") )) + ); + + assert_eq!(prec(&b" \t abc de fg"[..]), Ok((&b"fg"[..], &b"de"[..]))); + } + + #[test] + fn terminated() { + named!(term<&[u8], &[u8] >, + ws!(terminated!( take!(3), tag!("de") )) + ); + + assert_eq!(term(&b" \t abc de fg"[..]), Ok((&b"fg"[..], &b"abc"[..]))); + } + + #[test] + fn tuple() { + //trace_macros!(true); + named!(tuple_2<&[u8], (&[u8], &[u8]) >, + ws!(tuple!( take!(3), tag!("de") )) + ); + //trace_macros!(false); + + assert_eq!( + tuple_2(&b" \t abc de fg"[..]), + Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..]))) + ); + } + + #[test] + fn levels() { + //trace_macros!(true); + named!(level_2<&[u8], (&[u8], (&[u8], &[u8])) >, + ws!(pair!(take!(3), tuple!( tag!("de"), tag!("fg ") ))) + ); + //trace_macros!(false); + + assert_eq!( + level_2(&b" \t abc de fg \t hi "[..]), + Ok((&b"hi "[..], (&b"abc"[..], (&b"de"[..], &b"fg "[..])))) + ); + } + + #[test] + fn do_parse() { + fn ret_int1(i: &[u8]) -> IResult<&[u8], u8> { + Ok((i, 1)) + }; + fn ret_int2(i: &[u8]) -> IResult<&[u8], u8> { + Ok((i, 2)) + }; + + //trace_macros!(true); + named!(do_parser<&[u8], (u8, u8)>, + ws!(do_parse!( + tag!("abcd") >> + opt!(tag!("abcd")) >> + aa: ret_int1 >> + tag!("efgh") >> + bb: ret_int2 >> + tag!("efgh") >> + (aa, bb) + )) + ); + + //trace_macros!(false); + + assert_eq!( + do_parser(&b"abcd abcd\tefghefghX"[..]), + Ok((&b"X"[..], (1, 2))) + ); + assert_eq!( + do_parser(&b"abcd\tefgh efgh X"[..]), + Ok((&b"X"[..], (1, 2))) + ); + assert_eq!( + do_parser(&b"abcd ab"[..]), + Err(Err::Incomplete(Needed::Size(4))) + ); + assert_eq!( + do_parser(&b" abcd\tefgh\tef"[..]), + Err(Err::Incomplete(Needed::Size(4))) + ); + } + + #[test] + fn permutation() { + //trace_macros!(true); + named!( + perm<(&[u8], &[u8], &[u8])>, + ws!(permutation!(tag!("abcd"), tag!("efg"), tag!("hi"))) + ); + //trace_macros!(false); + + let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]); + + let a = &b"abcd\tefg \thijk"[..]; + assert_eq!(perm(a), Ok((&b"jk"[..], expected))); + let b = &b" efg \tabcdhi jk"[..]; + assert_eq!(perm(b), Ok((&b"jk"[..], expected))); + let c = &b" hi efg\tabcdjk"[..]; + assert_eq!(perm(c), Ok((&b"jk"[..], expected))); + + let d = &b"efg xyzabcdefghi"[..]; + assert_eq!( + perm(d), + Err(Err::Error(error_node_position!( + &b"efg xyzabcdefghi"[..], + ErrorKind::Permutation, + error_position!(&b" xyzabcdefghi"[..], ErrorKind::Permutation) + ))) + ); + + let e = &b" efg \tabc"[..]; + assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(4)))); + } + + #[cfg(feature = "alloc")] + #[derive(Debug, Clone, PartialEq)] + pub struct ErrorStr(String); + + #[cfg(feature = "alloc")] + impl<'a> From<(&'a[u8], ErrorKind)> for ErrorStr { + fn from(i: (&'a[u8], ErrorKind)) -> Self { + ErrorStr(format!("custom error code: {:?}", i)) + } + } + + #[cfg(feature = "alloc")] + impl<'a> From<(&'a str, ErrorKind)> for ErrorStr { + fn from(i: (&'a str, ErrorKind)) -> Self { + ErrorStr(format!("custom error message: {:?}", i)) + } + } + + #[cfg(feature = "alloc")] + impl ParseError for ErrorStr { + fn from_error_kind(input: I, kind: ErrorKind) -> Self { + ErrorStr(format!("custom error message: ({:?}, {:?})", input, kind)) + } + + fn append(input: I, kind: ErrorKind, other: Self) -> Self { + ErrorStr(format!("custom error message: ({:?}, {:?}) - {:?}", input, kind, other)) + } + } + + #[cfg(feature = "alloc")] + #[test] + fn alt() { + fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + Ok((&b""[..], input)) + } + + #[allow(unused_variables)] + fn dont_work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + Err(Err::Error(ErrorStr("abcd".to_string()))) + } + + fn work2(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + Ok((input, &b""[..])) + } + + fn alt1(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + alt!(i, dont_work | dont_work) + } + fn alt2(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + alt!(i, dont_work | work) + } + fn alt3(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> { + alt!(i, dont_work | dont_work | work2 | dont_work) + } + + let a = &b"\tabcd"[..]; + assert_eq!( + alt1(a), + Err(Err::Error(error_position!(a, ErrorKind::Alt))) + ); + assert_eq!(alt2(a), Ok((&b""[..], a))); + assert_eq!(alt3(a), Ok((a, &b""[..]))); + + } + + named!(str_parse(&str) -> &str, ws!(tag!("test"))); + #[allow(unused_variables)] + #[test] + fn str_test() { + assert_eq!(str_parse(" \n test\t a\nb"), Ok(("a\nb", "test"))); + } + + // test whitespace parser generation for alt + named!(space, tag!(" ")); + #[cfg(feature = "alloc")] + named!(pipeline_statement<&[u8], ()>, + ws!( + do_parse!( + tag!("pipeline") >> + attributes: delimited!(char!('{'), + separated_list!(char!(','), alt!( + space | + space + )), + char!('}')) >> + + ({ + let _ = attributes; + () + }) + ) + ) + ); + + #[cfg(feature = "alloc")] + named!( + fail<&[u8]>, + map!(many_till!(take!(1), ws!(tag!("."))), |(r, _)| r[0]) + ); +} -- cgit v1.2.3