move from IResult to Result

this will make it easier to be compatible with other crates like
error_chain, and we can reuse a lot of code coming for free with
Result. Incomplete is merged into the error side. I still do not
consider it to be an error,but this side will also contain unrecoverable
errors at some point (errors that cause alt and others to return
instead of testing the next branch), so it will be easier to put it in
this enum.
This commit is contained in:
Geoffroy Couprie 2017-09-07 14:28:33 +02:00
parent 460041a9cd
commit 6a15807cd4
27 changed files with 1668 additions and 1640 deletions

View File

@ -24,7 +24,7 @@
/// let input = vec![0b10101010, 0b11110000, 0b00110011];
/// let sl = &input[..];
///
/// assert_eq!(take_3_bits( sl ), Done(&sl[1..], 5) );
/// assert_eq!(take_3_bits( sl ),Ok((&sl[1..], 5)) );
/// # }
#[macro_export]
macro_rules! bits (
@ -45,24 +45,24 @@ macro_rules! bits_impl (
{
let input = ($i, 0usize);
match $submac!(input, $($args)*) {
$crate::IResult::Error(e) => {
::std::result::Result::Err($crate::Err::Error(e)) => {
let err = match e {
$crate::Err::Code(k) | $crate::Err::Node(k, _) => $crate::Err::Code(k),
$crate::Err::Position(k, (i,b)) | $crate::Err::NodePosition(k, (i,b), _) => {
$crate::Err::Position(k, &i[b/8..])
}
};
$crate::IResult::Error(err)
::std::result::Result::Err($crate::Err::Error(err))
}
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => {
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => {
//println!("bits parser returned Needed::Size({})", i);
$crate::IResult::Incomplete($crate::Needed::Size(i / 8 + 1))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i / 8 + 1)))
},
$crate::IResult::Done((i, bit_index), o) => {
::std::result::Result::Ok(((i, bit_index), o)) => {
let byte_index = bit_index / 8 + if bit_index % 8 == 0 { 0 } else { 1 } ;
//println!("bit index=={} => byte index=={}", bit_index, byte_index);
$crate::IResult::Done(&i[byte_index..], o)
::std::result::Result::Ok((&i[byte_index..], o))
}
}
}
@ -78,18 +78,18 @@ macro_rules! bits_impl (
{
let input = ($i, 0usize);
match $submac!(input, $($args)*) {
$crate::IResult::Error(e) => {
$crate::IResult::Error(e)
::std::result::Result::Err($crate::Err::Error(e)) => {
::std::result::Result::Err($crate::Err::Error(e))
}
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => {
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => {
//println!("bits parser returned Needed::Size({})", i);
$crate::IResult::Incomplete($crate::Needed::Size(i / 8 + 1))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i / 8 + 1)))
},
$crate::IResult::Done((i, bit_index), o) => {
::std::result::Result::Ok(((i, bit_index), o)) => {
let byte_index = bit_index / 8 + if bit_index % 8 == 0 { 0 } else { 1 } ;
//println!("bit index=={} => byte index=={}", bit_index, byte_index);
$crate::IResult::Done(&i[byte_index..], o)
::std::result::Result::Ok((&i[byte_index..], o))
}
}
}
@ -114,7 +114,7 @@ macro_rules! bits_impl (
///
/// let input = &[0xde, 0xad, 0xbe, 0xaf];
///
/// assert_eq!(parse( input ), Done(&[][..], (0xd, 0xea, &[0xbe, 0xaf][..])));
/// assert_eq!(parse( input ),Ok((&[][..], (0xd, 0xea, &[0xbe, 0xaf][..]))));
/// # }
#[macro_export]
macro_rules! bytes (
@ -142,21 +142,21 @@ macro_rules! bytes_impl (
}
match $submac!(inp, $($args)*) {
$crate::IResult::Error(e) => {
::std::result::Result::Err($crate::Err::Error(e)) => {
let err = match e {
$crate::Err::Code(k) | $crate::Err::Node(k, _) => $crate::Err::Code(k),
$crate::Err::Position(k, i) | $crate::Err::NodePosition(k, i, _) => {
$crate::Err::Position(k, (i, 0))
}
};
$crate::IResult::Error(err)
::std::result::Result::Err($crate::Err::Error(err))
}
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => {
$crate::IResult::Incomplete($crate::Needed::Size(i * 8))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => {
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i * 8)))
},
$crate::IResult::Done(i, o) => {
$crate::IResult::Done((i, 0), o)
::std::result::Result::Ok((i, o)) => {
::std::result::Result::Ok(((i, 0), o))
}
}
}
@ -179,15 +179,15 @@ macro_rules! bytes_impl (
}
match $submac!(inp, $($args)*) {
$crate::IResult::Error(e) => {
$crate::IResult::Error(e)
::std::result::Result::Err($crate::Err::Error(e)) => {
::std::result::Result::Err($crate::Err::Error(e))
}
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => {
$crate::IResult::Incomplete($crate::Needed::Size(i * 8))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => {
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i * 8)))
},
$crate::IResult::Done(i, o) => {
$crate::IResult::Done((i, 0), o)
::std::result::Result::Ok((i, o)) => {
::std::result::Result::Ok(((i, 0), o))
}
}
}
@ -206,8 +206,8 @@ macro_rules! bytes_impl (
/// let input = vec![0b10101010, 0b11110000, 0b00110011];
/// let sl = &input[..];
///
/// assert_eq!(take_pair( sl ), Done(&sl[1..], (5, 10)) );
/// assert_eq!(take_pair( &sl[1..] ), Done(&sl[2..], (7, 16)) );
/// assert_eq!(take_pair( sl ), Ok((&sl[1..], (5, 10))) );
/// assert_eq!(take_pair( &sl[1..] ),Ok((&sl[2..], (7, 16))) );
/// # }
/// ```
#[macro_export]
@ -219,12 +219,12 @@ macro_rules! take_bits (
//println!("taking {} bits from {:?}", $count, $i);
let (input, bit_offset) = $i;
let res : $crate::IResult<(&[u8],usize), $t> = if $count == 0 {
$crate::IResult::Done( (input, bit_offset), (0 as u8).into())
::std::result::Result::Ok(( (input, bit_offset), (0 as u8).into()))
} else {
let cnt = ($count as usize + bit_offset).div(8);
if input.len() * 8 < $count as usize + bit_offset {
//println!("returning incomplete: {}", $count as usize + bit_offset);
$crate::IResult::Incomplete($crate::Needed::Size($count as usize))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($count as usize)))
} else {
let mut acc:$t = (0 as u8).into();
let mut offset: usize = bit_offset;
@ -251,7 +251,7 @@ macro_rules! take_bits (
offset = 0;
}
}
$crate::IResult::Done( (&input[cnt..], end_offset) , acc)
::std::result::Result::Ok(( (&input[cnt..], end_offset) , acc))
}
};
res
@ -265,17 +265,17 @@ macro_rules! tag_bits (
($i:expr, $t:ty, $count:expr, $p: pat) => (
{
match take_bits!($i, $t, $count) {
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Done(i, o) => {
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Ok((i, o)) => {
if let $p = o {
let res: $crate::IResult<(&[u8],usize),$t> = $crate::IResult::Done(i, o);
let res: $crate::IResult<(&[u8],usize),$t> = ::std::result::Result::Ok((i, o));
res
} else {
$crate::IResult::Error(error_position!($crate::ErrorKind::TagBits, $i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TagBits, $i)))
}
},
_ => {
$crate::IResult::Error(error_position!($crate::ErrorKind::TagBits, $i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TagBits, $i)))
}
}
}
@ -285,7 +285,8 @@ macro_rules! tag_bits (
#[cfg(test)]
mod tests {
use std::ops::{Shr,Shl,AddAssign};
use internal::{IResult,Needed};
use internal::Needed;
use simple_errors::Err;
use ErrorKind;
#[test]
@ -293,21 +294,21 @@ mod tests {
let input = [0b10101010, 0b11110000, 0b00110011];
let sl = &input[..];
assert_eq!(take_bits!( (sl, 0), u8, 0 ), IResult::Done((sl, 0), 0));
assert_eq!(take_bits!( (sl, 0), u8, 8 ), IResult::Done((&sl[1..], 0), 170));
assert_eq!(take_bits!( (sl, 0), u8, 3 ), IResult::Done((&sl[0..], 3), 5));
assert_eq!(take_bits!( (sl, 0), u8, 6 ), IResult::Done((&sl[0..], 6), 42));
assert_eq!(take_bits!( (sl, 1), u8, 1 ), IResult::Done((&sl[0..], 2), 0));
assert_eq!(take_bits!( (sl, 1), u8, 2 ), IResult::Done((&sl[0..], 3), 1));
assert_eq!(take_bits!( (sl, 1), u8, 3 ), IResult::Done((&sl[0..], 4), 2));
assert_eq!(take_bits!( (sl, 6), u8, 3 ), IResult::Done((&sl[1..], 1), 5));
assert_eq!(take_bits!( (sl, 0), u16, 10 ), IResult::Done((&sl[1..], 2), 683));
assert_eq!(take_bits!( (sl, 0), u16, 8 ), IResult::Done((&sl[1..], 0), 170));
assert_eq!(take_bits!( (sl, 6), u16, 10 ), IResult::Done((&sl[2..], 0), 752));
assert_eq!(take_bits!( (sl, 6), u16, 11 ), IResult::Done((&sl[2..], 1), 1504));
assert_eq!(take_bits!( (sl, 0), u32, 20 ), IResult::Done((&sl[2..], 4), 700163));
assert_eq!(take_bits!( (sl, 4), u32, 20 ), IResult::Done((&sl[3..], 0), 716851));
assert_eq!(take_bits!( (sl, 4), u32, 22 ), IResult::Incomplete(Needed::Size(22)));
assert_eq!(take_bits!( (sl, 0), u8, 0 ), Ok(((sl, 0), 0)));
assert_eq!(take_bits!( (sl, 0), u8, 8 ), Ok(((&sl[1..], 0), 170)));
assert_eq!(take_bits!( (sl, 0), u8, 3 ), Ok(((&sl[0..], 3), 5)));
assert_eq!(take_bits!( (sl, 0), u8, 6 ), Ok(((&sl[0..], 6), 42)));
assert_eq!(take_bits!( (sl, 1), u8, 1 ), Ok(((&sl[0..], 2), 0)));
assert_eq!(take_bits!( (sl, 1), u8, 2 ), Ok(((&sl[0..], 3), 1)));
assert_eq!(take_bits!( (sl, 1), u8, 3 ), Ok(((&sl[0..], 4), 2)));
assert_eq!(take_bits!( (sl, 6), u8, 3 ), Ok(((&sl[1..], 1), 5)));
assert_eq!(take_bits!( (sl, 0), u16, 10 ), Ok(((&sl[1..], 2), 683)));
assert_eq!(take_bits!( (sl, 0), u16, 8 ), Ok(((&sl[1..], 0), 170)));
assert_eq!(take_bits!( (sl, 6), u16, 10 ), Ok(((&sl[2..], 0), 752)));
assert_eq!(take_bits!( (sl, 6), u16, 11 ), Ok(((&sl[2..], 1), 1504)));
assert_eq!(take_bits!( (sl, 0), u32, 20 ), Ok(((&sl[2..], 4), 700163)));
assert_eq!(take_bits!( (sl, 4), u32, 20 ), Ok(((&sl[3..], 0), 716851)));
assert_eq!(take_bits!( (sl, 4), u32, 22 ), Err(Err::Incomplete(Needed::Size(22))));
}
#[test]
@ -315,8 +316,8 @@ mod tests {
let input = [0b10101010, 0b11110000, 0b00110011];
let sl = &input[..];
assert_eq!(tag_bits!( (sl, 0), u8, 3, 0b101), IResult::Done((&sl[0..], 3), 5));
assert_eq!(tag_bits!( (sl, 0), u8, 4, 0b1010), IResult::Done((&sl[0..], 4), 10));
assert_eq!(tag_bits!( (sl, 0), u8, 3, 0b101), Ok(((&sl[0..], 3), 5)));
assert_eq!(tag_bits!( (sl, 0), u8, 4, 0b1010), Ok(((&sl[0..], 4), 10)));
}
named!(ch<(&[u8],usize),(u8,u8)>,
@ -332,18 +333,18 @@ mod tests {
fn chain_bits() {
let input = [0b10101010, 0b11110000, 0b00110011];
let sl = &input[..];
assert_eq!(ch((&input[..],0)), IResult::Done((&sl[1..], 4), (5,15)));
assert_eq!(ch((&input[..],4)), IResult::Done((&sl[2..], 0), (7,16)));
assert_eq!(ch((&input[..1],0)), IResult::Incomplete(Needed::Size(12)));
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(12))));
}
named!(ch_bytes<(u8,u8)>, bits!(ch));
#[test]
fn bits_to_bytes() {
let input = [0b10101010, 0b11110000, 0b00110011];
assert_eq!(ch_bytes(&input[..]), IResult::Done(&input[2..], (5,15)));
assert_eq!(ch_bytes(&input[..1]), IResult::Incomplete(Needed::Size(2)));
assert_eq!(ch_bytes(&input[1..]), IResult::Error(error_position!(ErrorKind::TagBits, &input[1..])));
assert_eq!(ch_bytes(&input[..]), Ok( (&input[2..], (5,15))) );
assert_eq!(ch_bytes(&input[..1]), Err(Err::Incomplete(Needed::Size(2))));
assert_eq!(ch_bytes(&input[1..]), Err(Err::Error(error_position!(ErrorKind::TagBits, &input[1..]))));
}
#[derive(PartialEq,Debug)]
@ -387,8 +388,8 @@ mod tests {
let input = [0b10101010, 0b11110000, 0b00110011];
let sl = &input[..];
assert_eq!(take_bits!( (sl, 0), FakeUint, 20 ), IResult::Done((&sl[2..], 4), FakeUint(700163)));
assert_eq!(take_bits!( (sl, 4), FakeUint, 20 ), IResult::Done((&sl[3..], 0), FakeUint(716851)));
assert_eq!(take_bits!( (sl, 4), FakeUint, 22 ), IResult::Incomplete(Needed::Size(22)));
assert_eq!(take_bits!( (sl, 0), FakeUint, 20 ), Ok( ((&sl[2..], 4), FakeUint(700163))) );
assert_eq!(take_bits!( (sl, 4), FakeUint, 20 ), Ok( ((&sl[3..], 0), FakeUint(716851))) );
assert_eq!(take_bits!( (sl, 4), FakeUint, 22 ), Err(Err::Incomplete(Needed::Size(22))) );
}
}

View File

@ -190,15 +190,15 @@ macro_rules! alt (
let i_ = $i.clone();
let res = $subrule!(i_, $($args)*);
match res {
$crate::IResult::Done(_,_) => res,
$crate::IResult::Incomplete(_) => res,
$crate::IResult::Error(e) => {
::std::result::Result::Ok((_,_)) => res,
::std::result::Result::Err($crate::Err::Incomplete(_)) => res,
::std::result::Result::Err($crate::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, _: &T) {}
if let $crate::IResult::Error(ref e2) = out {
if let ::std::result::Result::Err($crate::Err::Error(ref e2)) = out {
unify_types(&e, e2);
}
@ -212,15 +212,15 @@ macro_rules! alt (
{
let i_ = $i.clone();
match $subrule!(i_, $($args)* ) {
$crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)),
$crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x),
$crate::IResult::Error(e) => {
::std::result::Result::Ok((i,o)) => ::std::result::Result::Ok((i,$gen(o))),
::std::result::Result::Err($crate::Err::Incomplete(x)) => ::std::result::Result::Err($crate::Err::Incomplete(x)),
::std::result::Result::Err($crate::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, _: &T) {}
if let $crate::IResult::Error(ref e2) = out {
if let ::std::result::Result::Err($crate::Err::Error(ref e2)) = out {
unify_types(&e, e2);
}
@ -235,7 +235,7 @@ macro_rules! alt (
);
(__impl $i:expr, __end) => (
$crate::IResult::Error(error_position!($crate::ErrorKind::Alt,$i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Alt,$i)))
);
($i:expr, $($rest:tt)*) => (
@ -278,11 +278,11 @@ macro_rules! alt_complete (
let i_ = $i.clone();
let res = complete!(i_, $subrule!($($args)*));
match res {
$crate::IResult::Done(_,_) => res,
::std::result::Result::Ok((_,_)) => res,
e => {
let out = alt_complete!($i, $($rest)*);
if let (&$crate::IResult::Error(ref e1), &$crate::IResult::Error(ref e2)) = (&e, &out) {
if let (&::std::result::Result::Err($crate::Err::Error(ref e1)), &::std::result::Result::Err($crate::Err::Error(ref e2))) = (&e, &out) {
// 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, _: &T) {}
@ -299,11 +299,11 @@ macro_rules! alt_complete (
{
let i_ = $i.clone();
match complete!(i_, $subrule!($($args)*)) {
$crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)),
::std::result::Result::Ok((i,o)) => ::std::result::Result::Ok((i,$gen(o))),
e => {
let out = alt_complete!($i, $($rest)*);
if let (&$crate::IResult::Error(ref e1), &$crate::IResult::Error(ref e2)) = (&e, &out) {
if let (&::std::result::Result::Err($crate::Err::Error(ref e1)), &::std::result::Result::Err($crate::Err::Error(ref e2))) = (&e, &out) {
// 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, _: &T) {}
@ -362,10 +362,10 @@ macro_rules! alt_complete (
/// let c = b"efgh123";
/// let d = b"blah";
///
/// assert_eq!(sw(&a[..]), Done(&b"123"[..], &b"XYZ"[..]));
/// assert_eq!(sw(&a[..]), Ok((&b"123"[..], &b"XYZ"[..])));
/// assert_eq!(sw(&b[..]), Error(error_node_position!(ErrorKind::Switch, &b"abcdef"[..],
/// error_position!(ErrorKind::Tag, &b"ef"[..]))));
/// assert_eq!(sw(&c[..]), Done(&b""[..], &b"123"[..]));
/// assert_eq!(sw(&c[..]), Ok((&b""[..], &b"123"[..])));
/// assert_eq!(sw(&d[..]), Error(error_position!(ErrorKind::Switch, &b"blah"[..])));
/// # }
/// ```
@ -386,8 +386,8 @@ macro_rules! alt_complete (
/// let a = b"abcdXYZ123";
/// let b = b"blah";
///
/// assert_eq!(sw(&a[..]), Done(&b"123"[..], &b"XYZ"[..]));
/// assert_eq!(sw(&b[..]), Done(&b""[..], &b"default"[..]));
/// assert_eq!(sw(&a[..]), Ok((&b"123"[..], &b"XYZ"[..])));
/// assert_eq!(sw(&b[..]), Ok((&b""[..], &b"default"[..])));
/// # }
/// ```
///
@ -422,19 +422,19 @@ macro_rules! switch (
{
let i_ = $i.clone();
match map!(i_, $submac!($($args)*), |o| Some(o)) {
$crate::IResult::Error(e) => $crate::IResult::Error(error_node_position!(
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(error_node_position!(
$crate::ErrorKind::Switch, $i, e
)),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Done(i, o) => {
))),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Ok((i, o)) => {
match o {
$(Some($p) => match $subrule!(i, $($args2)*) {
$crate::IResult::Error(e) => $crate::IResult::Error(error_node_position!(
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(error_node_position!(
$crate::ErrorKind::Switch, $i, e
)),
))),
a => a,
}),*,
_ => $crate::IResult::Error(error_position!($crate::ErrorKind::Switch,$i))
_ => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Switch,$i)))
}
}
}
@ -474,11 +474,11 @@ macro_rules! switch (
/// let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]);
///
/// let a = &b"abcdefghijk"[..];
/// assert_eq!(perm(a), Done(&b"jk"[..], expected));
/// assert_eq!(perm(a), Ok((&b"jk"[..], expected)));
/// let b = &b"efgabcdhijkl"[..];
/// assert_eq!(perm(b), Done(&b"jkl"[..], expected));
/// assert_eq!(perm(b), Ok((&b"jkl"[..], expected)));
/// let c = &b"hiefgabcdjklm"[..];
/// assert_eq!(perm(c), Done(&b"jklm"[..], expected));
/// assert_eq!(perm(c), Ok((&b"jklm"[..], expected)));
///
/// let d = &b"efgxyzabcdefghi"[..];
/// assert_eq!(perm(d), Error(error_position!(ErrorKind::Permutation, &b"xyzabcdefghi"[..])));
@ -510,21 +510,21 @@ macro_rules! permutation (
if let ::std::option::Option::Some(need) = needed {
if let $crate::Needed::Size(sz) = need {
$crate::IResult::Incomplete(
::std::result::Result::Err($crate::Err::Incomplete(
$crate::Needed::Size(
$crate::InputLength::input_len(&($i)) -
$crate::InputLength::input_len(&input) +
sz
)
)
))
} else {
$crate::IResult::Incomplete($crate::Needed::Unknown)
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown))
}
} else if let ::std::option::Option::Some(e) = error {
$crate::IResult::Error(e)
::std::result::Result::Err($crate::Err::Error(e))
} else {
let unwrapped_res = permutation_unwrap!(0, (), res, $($rest)*);
$crate::IResult::Done(input, unwrapped_res)
::std::result::Result::Ok((input, unwrapped_res))
}
}
);
@ -640,15 +640,15 @@ macro_rules! permutation_iterator (
($it:tt, $i:expr, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
if acc!($it, $res) == ::std::option::Option::None {
match $submac!($i, $($args)*) {
$crate::IResult::Done(i,o) => {
::std::result::Result::Ok((i,o)) => {
$i = i;
acc!($it, $res) = ::std::option::Option::Some(o);
continue;
},
$crate::IResult::Error(_) => {
::std::result::Result::Err($crate::Err::Error(_)) => {
$all_done = false;
},
$crate::IResult::Incomplete(i) => {
::std::result::Result::Err($crate::Err::Incomplete(i)) => {
$needed = ::std::option::Option::Some(i);
break;
}
@ -662,15 +662,15 @@ macro_rules! permutation_iterator (
($it:tt, $i:expr, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )) => {
if acc!($it, $res) == ::std::option::Option::None {
match $submac!($i, $($args)*) {
$crate::IResult::Done(i,o) => {
::std::result::Result::Ok((i,o)) => {
$i = i;
acc!($it, $res) = ::std::option::Option::Some(o);
continue;
},
$crate::IResult::Error(_) => {
::std::result::Result::Err($crate::Err::Error(_)) => {
$all_done = false;
},
$crate::IResult::Incomplete(i) => {
::std::result::Result::Err($crate::Err::Incomplete(i)) => {
$needed = ::std::option::Option::Some(i);
break;
}
@ -682,8 +682,9 @@ macro_rules! permutation_iterator (
#[cfg(test)]
mod tests {
use internal::{Needed,IResult};
use internal::IResult::*;
//use internal::IResult::*;
use util::ErrorKind;
use simple_errors::Err;
// reproduce the tag and take macros, because of module import order
macro_rules! tag (
@ -713,11 +714,11 @@ mod tests {
let b = &$bytes[..m];
let res: $crate::IResult<_,_> = if reduced != b {
$crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Tag, $i)))
} else if m < blen {
$crate::IResult::Incomplete($crate::Needed::Size(blen))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(blen)))
} else {
$crate::IResult::Done(&$i[blen..], reduced)
::std::result::Result::Ok((&$i[blen..], reduced))
};
res
}
@ -729,9 +730,9 @@ mod tests {
{
let cnt = $count as usize;
let res:$crate::IResult<&[u8],&[u8]> = if $i.len() < cnt {
$crate::IResult::Incomplete($crate::Needed::Size(cnt))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(cnt)))
} else {
$crate::IResult::Done(&$i[cnt..],&$i[0..cnt])
::std::result::Result::Ok((&$i[cnt..],&$i[0..cnt]))
};
res
}
@ -741,16 +742,16 @@ mod tests {
#[test]
fn alt() {
fn work(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> {
Done(&b""[..], input)
Ok((&b""[..], input))
}
#[allow(unused_variables)]
fn dont_work(input: &[u8]) -> IResult<&[u8],&[u8],&'static str> {
Error(error_code!(ErrorKind::Custom("abcd")))
Err(Err::Error(error_code!(ErrorKind::Custom("abcd"))))
}
fn work2(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> {
Done(input, &b""[..])
Ok((input, &b""[..]))
}
fn alt1(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> {
@ -767,19 +768,19 @@ mod tests {
//named!(alt3, alt!(dont_work | dont_work | work2 | dont_work));
let a = &b"abcd"[..];
assert_eq!(alt1(a), Error(error_position!(ErrorKind::Alt, a)));
assert_eq!(alt2(a), Done(&b""[..], a));
assert_eq!(alt3(a), Done(a, &b""[..]));
assert_eq!(alt1(a), Err(Err::Error(error_position!(ErrorKind::Alt, a))));
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), Done(&b""[..], a));
assert_eq!(alt4(b), Done(&b""[..], b));
assert_eq!(alt4(a), Ok((&b""[..], a)));
assert_eq!(alt4(b), Ok((&b""[..], b)));
// test the alternative syntax
named!(alt5<bool>, alt!(tag!("abcd") => { |_| false } | tag!("efgh") => { |_| true }));
assert_eq!(alt5(a), Done(&b""[..], false));
assert_eq!(alt5(b), Done(&b""[..], 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!()));
@ -793,17 +794,17 @@ mod tests {
named!(alt1, alt!(tag!("a") | tag!("bc") | tag!("def")));
let a = &b""[..];
assert_eq!(alt1(a), Incomplete(Needed::Size(1)));
assert_eq!(alt1(a), Err(Err::Incomplete(Needed::Size(1))));
let a = &b"b"[..];
assert_eq!(alt1(a), Incomplete(Needed::Size(2)));
assert_eq!(alt1(a), Err(Err::Incomplete(Needed::Size(2))));
let a = &b"bcd"[..];
assert_eq!(alt1(a), Done(&b"d"[..], &b"bc"[..]));
assert_eq!(alt1(a), Ok((&b"d"[..], &b"bc"[..])));
let a = &b"cde"[..];
assert_eq!(alt1(a), Error(error_position!(ErrorKind::Alt, a)));
assert_eq!(alt1(a), Err(Err::Error(error_position!(ErrorKind::Alt, a))));
let a = &b"de"[..];
assert_eq!(alt1(a), Incomplete(Needed::Size(3)));
assert_eq!(alt1(a), Err(Err::Incomplete(Needed::Size(3))));
let a = &b"defg"[..];
assert_eq!(alt1(a), Done(&b"g"[..], &b"def"[..]));
assert_eq!(alt1(a), Ok((&b"g"[..], &b"def"[..])));
}
#[test]
@ -813,11 +814,11 @@ mod tests {
);
let a = &b""[..];
assert_eq!(ac(a), Incomplete(Needed::Size(2)));
assert_eq!(ac(a), Err(Err::Incomplete(Needed::Size(2))));
let a = &b"ef"[..];
assert_eq!(ac(a), Done(&b""[..], &b"ef"[..]));
assert_eq!(ac(a), Ok((&b""[..], &b"ef"[..])));
let a = &b"cde"[..];
assert_eq!(ac(a), Error(error_position!(ErrorKind::Alt, a)));
assert_eq!(ac(a), Err(Err::Error(error_position!(ErrorKind::Alt, a))));
}
#[allow(unused_variables)]
@ -831,12 +832,12 @@ mod tests {
);
let a = &b"abcdefgh"[..];
assert_eq!(sw(a), Done(&b"gh"[..], &b"ef"[..]));
assert_eq!(sw(a), Ok((&b"gh"[..], &b"ef"[..])));
let b = &b"efghijkl"[..];
assert_eq!(sw(b), Done(&b""[..], &b"ijkl"[..]));
assert_eq!(sw(b), Ok((&b""[..], &b"ijkl"[..])));
let c = &b"afghijkl"[..];
assert_eq!(sw(c), Error(error_position!(ErrorKind::Switch, &b"afghijkl"[..])));
assert_eq!(sw(c), Err(Err::Error(error_position!(ErrorKind::Switch, &b"afghijkl"[..]))));
}
#[test]
@ -850,17 +851,17 @@ mod tests {
let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]);
let a = &b"abcdefghijk"[..];
assert_eq!(perm(a), Done(&b"jk"[..], expected));
assert_eq!(perm(a), Ok((&b"jk"[..], expected)));
let b = &b"efgabcdhijk"[..];
assert_eq!(perm(b), Done(&b"jk"[..], expected));
assert_eq!(perm(b), Ok((&b"jk"[..], expected)));
let c = &b"hiefgabcdjk"[..];
assert_eq!(perm(c), Done(&b"jk"[..], expected));
assert_eq!(perm(c), Ok((&b"jk"[..], expected)));
let d = &b"efgxyzabcdefghi"[..];
assert_eq!(perm(d), Error(error_position!(ErrorKind::Permutation, &b"xyzabcdefghi"[..])));
assert_eq!(perm(d), Err(Err::Error(error_position!(ErrorKind::Permutation, &b"xyzabcdefghi"[..]))));
let e = &b"efgabc"[..];
assert_eq!(perm(e), Incomplete(Needed::Size(7)));
assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(7))));
}
/*

View File

@ -13,7 +13,7 @@
/// # fn main() {
/// named!(x, tag!("abcd"));
/// let r = x(&b"abcdefgh"[..]);
/// assert_eq!(r, Done(&b"efgh"[..], &b"abcd"[..]));
/// assert_eq!(r, Ok((&b"efgh"[..], &b"abcd"[..])));
/// # }
/// ```
#[macro_export]
@ -24,13 +24,13 @@ macro_rules! tag (
let res: $crate::IResult<_,_> = match ($i).compare($tag) {
CompareResult::Ok => {
let blen = $tag.input_len();
$crate::IResult::Done($i.slice(blen..), $i.slice(..blen))
::std::result::Result::Ok(($i.slice(blen..), $i.slice(..blen)))
},
CompareResult::Incomplete => {
$crate::IResult::Incomplete($crate::Needed::Size($tag.input_len()))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($tag.input_len())))
},
CompareResult::Error => {
$crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Tag, $i)))
}
};
res
@ -50,7 +50,7 @@ macro_rules! tag (
/// named!(test, tag_no_case!("ABcd"));
///
/// let r = test(&b"aBCdefgh"[..]);
/// assert_eq!(r, Done(&b"efgh"[..], &b"aBCd"[..]));
/// assert_eq!(r, Ok((&b"efgh"[..], &b"aBCd"[..])));
/// # }
/// ```
#[macro_export]
@ -61,13 +61,13 @@ macro_rules! tag_no_case (
let res: $crate::IResult<_,_> = match ($i).compare_no_case($tag) {
CompareResult::Ok => {
let blen = $tag.input_len();
$crate::IResult::Done($i.slice(blen..), $i.slice(..blen))
::std::result::Result::Ok(($i.slice(blen..), $i.slice(..blen)))
},
CompareResult::Incomplete => {
$crate::IResult::Incomplete($crate::Needed::Size($tag.input_len()))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($tag.input_len())))
},
CompareResult::Error => {
$crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Tag, $i)))
}
};
res
@ -86,7 +86,7 @@ macro_rules! tag_no_case (
/// named!( not_space, is_not!( " \t\r\n" ) );
///
/// let r = not_space(&b"abcdefgh\nijkl"[..]);
/// assert_eq!(r, Done(&b"\nijkl"[..], &b"abcdefgh"[..]));
/// assert_eq!(r, Ok((&b"\nijkl"[..], &b"abcdefgh"[..])));
/// # }
/// ```
#[macro_export]
@ -101,13 +101,13 @@ macro_rules! is_not(
let res: $crate::IResult<_,_> = match $input.position(|c| {
c.find_token($arr)
}) {
Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::IsNot,$input)),
Some(0) => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::IsNot,$input))),
Some(n) => {
let res = $crate::IResult::Done($input.slice(n..), $input.slice(..n));
let res = ::std::result::Result::Ok(($input.slice(n..), $input.slice(..n)));
res
},
None => {
$crate::IResult::Done($input.slice($input.input_len()..), $input)
::std::result::Result::Ok(($input.slice($input.input_len()..), $input))
}
};
res
@ -125,10 +125,10 @@ macro_rules! is_not(
/// named!(abcd, is_a!( "abcd" ));
///
/// let r1 = abcd(&b"aaaaefgh"[..]);
/// assert_eq!(r1, Done(&b"efgh"[..], &b"aaaa"[..]));
/// assert_eq!(r1, Ok((&b"efgh"[..], &b"aaaa"[..])));
///
/// let r2 = abcd(&b"dcbaefgh"[..]);
/// assert_eq!(r2, Done(&b"efgh"[..], &b"dcba"[..]));
/// assert_eq!(r2, Ok((&b"efgh"[..], &b"dcba"[..])));
/// # }
/// ```
#[macro_export]
@ -143,13 +143,13 @@ macro_rules! is_a (
let res: $crate::IResult<_,_> = match $input.position(|c| {
!c.find_token($arr)
}) {
Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::IsA,$input)),
Some(0) => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::IsA,$input))),
Some(n) => {
let res: $crate::IResult<_,_> = $crate::IResult::Done($input.slice(n..), $input.slice(..n));
let res: $crate::IResult<_,_, u32> = ::std::result::Result::Ok(($input.slice(n..), $input.slice(..n)));
res
},
None => {
$crate::IResult::Done($input.slice(($input).input_len()..), $input)
::std::result::Result::Ok(($input.slice(($input).input_len()..), $input))
}
};
res
@ -169,8 +169,8 @@ macro_rules! is_a (
/// # use nom::alpha;
/// # fn main() {
/// named!(esc, escaped!(call!(alpha), '\\', one_of!("\"n\\")));
/// assert_eq!(esc(&b"abcd"[..]), Done(&b""[..], &b"abcd"[..]));
/// assert_eq!(esc(&b"ab\\\"cd"[..]), Done(&b""[..], &b"ab\\\"cd"[..]));
/// assert_eq!(esc(&b"abcd"[..]), Ok((&b""[..], &b"abcd"[..])));
/// assert_eq!(esc(&b"ab\\\"cd"[..]), Ok((&b""[..], &b"ab\\\"cd"[..])));
/// # }
/// ```
#[macro_export]
@ -180,52 +180,52 @@ macro_rules! escaped (
{
use $crate::InputLength;
use $crate::Slice;
let cl = || -> $crate::IResult<_,_,_> {
let cl = || -> $crate::IResult<_,_,u32> {
use $crate::Offset;
let mut index = 0;
while index < $i.input_len() {
match $normal!($i.slice(index..), $($args)*) {
$crate::IResult::Done(i, _) => {
::std::result::Result::Ok((i, _)) => {
if i.is_empty() {
return $crate::IResult::Done($i.slice($i.input_len()..), $i)
return ::std::result::Result::Ok(($i.slice($i.input_len()..), $i))
} else {
index = $i.offset(i);
}
},
$crate::IResult::Incomplete(i) => {
return $crate::IResult::Incomplete(i)
::std::result::Result::Err($crate::Err::Incomplete(i)) => {
return ::std::result::Result::Err($crate::Err::Incomplete(i))
},
$crate::IResult::Error(e) => {
::std::result::Result::Err($crate::Err::Error(e)) => {
if $i[index] == $control_char as u8 {
if index + 1 >= $i.input_len() {
return $crate::IResult::Incomplete($crate::Needed::Unknown)
return ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown))
} else {
match $escapable!($i.slice(index+1..), $($args2)*) {
$crate::IResult::Done(i,_) => {
::std::result::Result::Ok((i,_)) => {
if i.is_empty() {
return $crate::IResult::Done($i.slice($i.input_len()..), $i)
return ::std::result::Result::Ok(($i.slice($i.input_len()..), $i))
} else {
index = $i.offset(i);
}
},
$crate::IResult::Incomplete(i) => return $crate::IResult::Incomplete(i),
$crate::IResult::Error(e2) => return $crate::IResult::Error(e2)
::std::result::Result::Err($crate::Err::Incomplete(i)) => return ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Err($crate::Err::Error(e2)) => return ::std::result::Result::Err($crate::Err::Error(e2))
}
}
} else {
return $crate::IResult::Done($i.slice(index..), $i.slice(..index));
return ::std::result::Result::Ok(($i.slice(index..), $i.slice(..index)));
}
}
}
}
$crate::IResult::Done(&$i[index..], &$i[..index])
::std::result::Result::Ok((&$i[index..], &$i[..index]))
};
match cl() {
$crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x),
$crate::IResult::Done(i, o) => $crate::IResult::Done(i, o),
$crate::IResult::Error(e) => {
return $crate::IResult::Error(error_node_position!($crate::ErrorKind::Escaped, $i, e))
::std::result::Result::Err($crate::Err::Incomplete(x)) => ::std::result::Result::Err($crate::Err::Incomplete(x)),
::std::result::Result::Ok((i, o)) => ::std::result::Result::Ok((i, o)),
::std::result::Result::Err($crate::Err::Error(e)) => {
return ::std::result::Result::Err($crate::Err::Error(error_node_position!($crate::ErrorKind::Escaped, $i, e)))
}
}
}
@ -285,7 +285,7 @@ macro_rules! escaped (
/// ), to_s
/// )
/// );
/// assert_eq!(transform(&b"ab\\\"cd"[..]), Done(&b""[..], String::from("ab\"cd")));
/// assert_eq!(transform(&b"ab\\\"cd"[..]), Ok((&b""[..], String::from("ab\"cd"))));
/// # }
/// ```
#[macro_export]
@ -300,45 +300,45 @@ macro_rules! escaped_transform (
let mut res = Vec::new();
while index < $i.input_len() {
if let $crate::IResult::Done(i,o) = $normal!($i.slice(index..), $($args)*) {
if let ::std::result::Result::Ok((i,o)) = $normal!($i.slice(index..), $($args)*) {
res.extend(o.iter().cloned());
if i.is_empty() {
return $crate::IResult::Done($i.slice($i.input_len()..), res);
return ::std::result::Result::Ok(($i.slice($i.input_len())..), res);
} else {
index = $i.offset(i);
}
} else if $i[index] == $control_char as u8 {
if index + 1 >= $i.input_len() {
return $crate::IResult::Error(error_position!($crate::ErrorKind::EscapedTransform,$i.slice(index..)));
return ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::EscapedTransform,$i.slice(index..))));
} else {
match $transform!($i.slice(index+1..), $($args2)*) {
$crate::IResult::Done(i,o) => {
::std::result::Result::Ok((i,o)) => {
res.extend(o.iter().cloned());
if i.is_empty() {
return $crate::IResult::Done($i.slice($i.input_len()..), res)
return ::std::result::Result::Ok(($i.slice($i.input_len()..), res))
} else {
index = $i.offset(i);
}
},
$crate::IResult::Incomplete(i) => return $crate::IResult::Incomplete(i),
$crate::IResult::Error(e) => return $crate::IResult::Error(e)
::std::result::Result::Err($crate::Err::Incomplete(i)) => return ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Err($crate::Err::Error(e)) => return ::std::result::Result::Err($crate::Err::Error(e))
}
}
} else {
if index == 0 {
return $crate::IResult::Error(error_position!($crate::ErrorKind::EscapedTransform,$i.slice(index..)))
return ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::EscapedTransform,$i.slice(index..))))
} else {
return $crate::IResult::Done($i.slice(index..), res)
return ::std::result::Result::Ok(($i.slice(index..), res))
}
}
}
$crate::IResult::Done($i.slice(index..), res)
::std::result::Result::Ok(($i.slice(index..), res))
};
match cl() {
$crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x),
$crate::IResult::Done(i, o) => $crate::IResult::Done(i, o),
$crate::IResult::Error(e) => {
return $crate::IResult::Error(error_node_position!($crate::ErrorKind::EscapedTransform, $i, e))
::std::result::Result::Err($crate::Err::Incomplete(x)) => ::std::result::Result::Err($crate::Err::Incomplete(x)),
::std::result::Result::Ok((i, o)) => ::std::result::Result::Ok((i, o)),
::std::result::Result::Err($crate::Err::Error(e)) => {
return ::std::result::Result::Err($crate::Err::Error(error_node_position!($crate::ErrorKind::EscapedTransform, $i, e)))
}
}
}
@ -379,7 +379,7 @@ macro_rules! escaped_transform (
/// named!( alpha, take_while!( is_alphanumeric ) );
///
/// let r = alpha(&b"abcd\nefgh"[..]);
/// assert_eq!(r, Done(&b"\nefgh"[..], &b"abcd"[..]));
/// assert_eq!(r, Ok((&b"\nefgh"[..], &b"abcd"[..])));
/// # }
/// ```
#[macro_export]
@ -391,11 +391,11 @@ macro_rules! take_while (
match input.position(|c| !$submac!(c, $($args)*)) {
Some(n) => {
let res:$crate::IResult<_,_> = $crate::IResult::Done(input.slice(n..), input.slice(..n));
let res:$crate::IResult<_,_> = ::std::result::Result::Ok((input.slice(n..), input.slice(..n)));
res
},
None => {
$crate::IResult::Done(input.slice(input.input_len()..), input)
::std::result::Result::Ok((input.slice(input.input_len()..), input))
}
}
}
@ -419,15 +419,15 @@ macro_rules! take_while1 (
use $crate::InputIter;
use $crate::Slice;
if input.input_len() == 0 {
$crate::IResult::Incomplete($crate::Needed::Size(1))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(1)))
} else {
match input.position(|c| !$submac!(c, $($args)*)) {
Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::TakeWhile1,input)),
Some(0) => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TakeWhile1,input))),
Some(n) => {
$crate::IResult::Done(input.slice(n..), input.slice(..n))
::std::result::Result::Ok((input.slice(n..), input.slice(..n)))
},
None => {
$crate::IResult::Done(input.slice(input.input_len()..), input)
::std::result::Result::Ok((input.slice(input.input_len()..), input))
}
}
}
@ -452,8 +452,8 @@ macro_rules! take_till (
use $crate::InputIter;
use $crate::Slice;
match input.position(|c| $submac!(c, $($args)*)) {
Some(n) => $crate::IResult::Done(input.slice(n..), input.slice(..n)),
None => $crate::IResult::Done(input.slice(input.input_len()..), input)
Some(n) => ::std::result::Result::Ok((input.slice(n..), input.slice(..n))),
None => ::std::result::Result::Ok((input.slice(input.input_len()..), input))
}
}
);
@ -476,12 +476,12 @@ macro_rules! take_till1 (
use $crate::InputIter;
use $crate::Slice;
if input.input_len() == 0 {
$crate::IResult::Incomplete($crate::Needed::Size(1))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(1)))
} else {
match input.position(|c| $submac!(c, $($args)*)) {
Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::TakeTill1,input)),
Some(n) => $crate::IResult::Done(input.slice(n..), input.slice(..n)),
None => $crate::IResult::Done(input.slice(input.input_len()..), input)
Some(0) => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TakeTill1,input))),
Some(n) => ::std::result::Result::Ok((input.slice(n..), input.slice(..n))),
None => ::std::result::Result::Ok((input.slice(input.input_len()..), input))
}
}
}
@ -503,7 +503,7 @@ macro_rules! take_till1 (
///
/// let a = b"abcdefgh";
///
/// assert_eq!(take5(&a[..]), Done(&b"fgh"[..], &b"abcde"[..]));
/// assert_eq!(take5(&a[..]), Ok((&b"fgh"[..], &b"abcde"[..])));
/// # }
/// ```
#[macro_export]
@ -517,9 +517,9 @@ macro_rules! take (
let cnt = $count as usize;
let res: $crate::IResult<_,_> = match input.slice_index(cnt) {
None => $crate::IResult::Incomplete($crate::Needed::Size(cnt)),
None => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(cnt))),
//FIXME: use the InputTake trait
Some(index) => $crate::IResult::Done(input.slice(index..), input.slice(..index))
Some(index) => ::std::result::Result::Ok((input.slice(index..), input.slice(..index)))
};
res
}
@ -550,14 +550,14 @@ macro_rules! take_until_and_consume (
use $crate::Slice;
let res: $crate::IResult<_,_> = if $substr.input_len() > $i.input_len() {
$crate::IResult::Incomplete($crate::Needed::Size($substr.input_len()))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($substr.input_len())))
} else {
match ($i).find_substring($substr) {
None => {
$crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilAndConsume,$i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TakeUntilAndConsume,$i)))
},
Some(index) => {
$crate::IResult::Done($i.slice(index+$substr.input_len()..), $i.slice(0..index))
::std::result::Result::Ok(($i.slice(index+$substr.input_len()..), $i.slice(0..index)))
},
}
};
@ -575,14 +575,14 @@ macro_rules! take_until_and_consume1 (
use $crate::InputLength;
let res: $crate::IResult<_,_> = if 1 + $substr.input_len() > $i.input_len() {
$crate::IResult::Incomplete($crate::Needed::Size($substr.input_len()))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($substr.input_len())))
} else {
match ($i).find_substring($substr) {
None => {
$crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilAndConsume,$i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TakeUntilAndConsume,$i)))
},
Some(index) => {
$crate::IResult::Done($i.slice(index+$substr.input_len()..), $i.slice(0..index))
::std::result::Result::Ok(($i.slice(index+$substr.input_len()..), $i.slice(0..index)))
},
}
};
@ -602,14 +602,14 @@ macro_rules! take_until (
use $crate::Slice;
let res: $crate::IResult<_,_> = if $substr.input_len() > $i.input_len() {
$crate::IResult::Incomplete($crate::Needed::Size($substr.input_len()))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($substr.input_len())))
} else {
match ($i).find_substring($substr) {
None => {
$crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntil,$i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TakeUntil,$i)))
},
Some(index) => {
$crate::IResult::Done($i.slice(index..), $i.slice(0..index))
::std::result::Result::Ok(($i.slice(index..), $i.slice(0..index)))
},
}
};
@ -629,14 +629,14 @@ macro_rules! take_until1 (
use $crate::Slice;
let res: $crate::IResult<_,_> = if 1+$substr.input_len() > $i.input_len() {
$crate::IResult::Incomplete($crate::Needed::Size($substr.input_len()))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($substr.input_len())))
} else {
match ($i).find_substring($substr) {
None => {
$crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntil,$i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TakeUntil,$i)))
},
Some(index) => {
$crate::IResult::Done($i.slice(index..), $i.slice(0..index))
::std::result::Result::Ok(($i.slice(index..), $i.slice(0..index)))
},
}
};
@ -657,18 +657,18 @@ macro_rules! take_until_either_and_consume (
use $crate::Slice;
if $input.input_len() == 0 {
$crate::IResult::Incomplete($crate::Needed::Size(1))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(1)))
} else {
let res: $crate::IResult<_,_> = match $input.position(|c| {
c.find_token($arr)
}) {
Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilEitherAndConsume,$input)),
Some(0) => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TakeUntilEitherAndConsume,$input))),
Some(n) => {
let res = $crate::IResult::Done($input.slice(n+1..), $input.slice(..n));
let res = ::std::result::Result::Ok(($input.slice(n+1..), $input.slice(..n)));
res
},
None => {
$crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilEitherAndConsume,$input))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TakeUntilEitherAndConsume,$input)))
}
};
res
@ -688,18 +688,18 @@ macro_rules! take_until_either (
use $crate::Slice;
if $input.input_len() == 0 {
$crate::IResult::Incomplete($crate::Needed::Size(1))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(1)))
} else {
let res: $crate::IResult<_,_> = match $input.position(|c| {
c.find_token($arr)
}) {
Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilEither,$input)),
Some(0) => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TakeUntilEither,$input))),
Some(n) => {
let res = $crate::IResult::Done($input.slice(n..), $input.slice(..n));
let res = ::std::result::Result::Ok(($input.slice(n..), $input.slice(..n)));
res
},
None => {
$crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilEither,$input))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TakeUntilEither,$input)))
}
};
res
@ -726,15 +726,15 @@ macro_rules! length_bytes(
#[cfg(test)]
mod tests {
use internal::Needed;
use internal::IResult::*;
use util::ErrorKind;
use nom::{alpha, digit, hex_digit, oct_digit, alphanumeric, space, multispace};
use simple_errors::Err;
macro_rules! one_of (
($i:expr, $inp: expr) => (
{
if $i.is_empty() {
$crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1))
::std::result::Result::Err::<_,_>($crate::Err::Incomplete($crate::Needed::Size(1)))
} else {
#[inline(always)]
fn as_bytes<T: $crate::AsBytes>(b: &T) -> &[u8] {
@ -753,7 +753,7 @@ mod tests {
($i:expr, $bytes: expr) => (
{
if $i.is_empty() {
$crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1))
::std::result::Result::Err::<_,_>($crate::Err::Incomplete($crate::Needed::Size(1)))
} else {
let mut found = false;
@ -765,9 +765,9 @@ mod tests {
}
if found {
$crate::IResult::Done(&$i[1..], $i[0] as char)
::std::result::Result::Ok((&$i[1..], $i[0] as char))
} else {
$crate::IResult::Error(error_position!($crate::ErrorKind::OneOf, $i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::OneOf, $i)))
}
}
}
@ -779,16 +779,16 @@ mod tests {
named!(a_or_b, is_a!(&b"ab"[..]));
let a = &b"abcd"[..];
assert_eq!(a_or_b(a), Done(&b"cd"[..], &b"ab"[..]));
assert_eq!(a_or_b(a), Ok((&b"cd"[..], &b"ab"[..])));
let b = &b"bcde"[..];
assert_eq!(a_or_b(b), Done(&b"cde"[..], &b"b"[..]));
assert_eq!(a_or_b(b), Ok((&b"cde"[..], &b"b"[..])));
let c = &b"cdef"[..];
assert_eq!(a_or_b(c), Error(error_position!(ErrorKind::IsA,c)));
assert_eq!(a_or_b(c), Err(Err::Error(error_position!(ErrorKind::IsA,c))));
let d = &b"bacdef"[..];
assert_eq!(a_or_b(d), Done(&b"cdef"[..], &b"ba"[..]));
assert_eq!(a_or_b(d), Ok((&b"cdef"[..], &b"ba"[..])));
}
#[test]
@ -796,39 +796,39 @@ mod tests {
named!(a_or_b, is_not!(&b"ab"[..]));
let a = &b"cdab"[..];
assert_eq!(a_or_b(a), Done(&b"ab"[..], &b"cd"[..]));
assert_eq!(a_or_b(a), Ok((&b"ab"[..], &b"cd"[..])));
let b = &b"cbde"[..];
assert_eq!(a_or_b(b), Done(&b"bde"[..], &b"c"[..]));
assert_eq!(a_or_b(b), Ok((&b"bde"[..], &b"c"[..])));
let c = &b"abab"[..];
assert_eq!(a_or_b(c), Error(error_position!(ErrorKind::IsNot,c)));
assert_eq!(a_or_b(c), Err(Err::Error(error_position!(ErrorKind::IsNot,c))));
let d = &b"cdefba"[..];
assert_eq!(a_or_b(d), Done(&b"ba"[..], &b"cdef"[..]));
assert_eq!(a_or_b(d), Ok((&b"ba"[..], &b"cdef"[..])));
let e = &b"e"[..];
assert_eq!(a_or_b(e), Done(&b""[..], &b"e"[..]));
assert_eq!(a_or_b(e), Ok((&b""[..], &b"e"[..])));
let f = &b"fghi"[..];
assert_eq!(a_or_b(f), Done(&b""[..], &b"fghi"[..]));
assert_eq!(a_or_b(f), Ok((&b""[..], &b"fghi"[..])));
}
#[allow(unused_variables)]
#[test]
fn escaping() {
named!(esc, escaped!(call!(alpha), '\\', one_of!("\"n\\")));
assert_eq!(esc(&b"abcd"[..]), Done(&b""[..], &b"abcd"[..]));
assert_eq!(esc(&b"ab\\\"cd"[..]), Done(&b""[..], &b"ab\\\"cd"[..]));
assert_eq!(esc(&b"\\\"abcd"[..]), Done(&b""[..], &b"\\\"abcd"[..]));
assert_eq!(esc(&b"\\n"[..]), Done(&b""[..], &b"\\n"[..]));
assert_eq!(esc(&b"ab\\\"12"[..]), Done(&b"12"[..], &b"ab\\\""[..]));
assert_eq!(esc(&b"AB\\"[..]), Incomplete(Needed::Unknown));
assert_eq!(esc(&b"AB\\A"[..]), Error(error_node_position!(ErrorKind::Escaped, &b"AB\\A"[..],
error_position!(ErrorKind::OneOf, &b"A"[..]))));
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::Incomplete(Needed::Unknown)));
assert_eq!(esc(&b"AB\\A"[..]), Err(Err::Error(error_node_position!(ErrorKind::Escaped, &b"AB\\A"[..],
error_position!(ErrorKind::OneOf, &b"A"[..])))));
named!(esc2, escaped!(call!(digit), '\\', one_of!("\"n\\")));
assert_eq!(esc2(&b"12\\nnn34"[..]), Done(&b"nn34"[..], &b"12\\n"[..]));
assert_eq!(esc2(&b"12\\nnn34"[..]), Ok((&b"nn34"[..], &b"12\\n"[..])));
}
#[cfg(feature = "verbose-errors")]
@ -849,12 +849,12 @@ mod tests {
)), to_s)
);
assert_eq!(esc(&b"abcd"[..]), Done(&b""[..], String::from("abcd")));
assert_eq!(esc(&b"ab\\\"cd"[..]), Done(&b""[..], String::from("ab\"cd")));
assert_eq!(esc(&b"\\\"abcd"[..]), Done(&b""[..], String::from("\"abcd")));
assert_eq!(esc(&b"\\n"[..]), Done(&b""[..], String::from("\n")));
assert_eq!(esc(&b"ab\\\"12"[..]), Done(&b"12"[..], String::from("ab\"")));
assert_eq!(esc(&b"AB\\"[..]), Error(error_node_position!(ErrorKind::EscapedTransform, &b"AB\\"[..], error_position!(ErrorKind::EscapedTransform, &b"\\"[..]))));
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_node_position!(ErrorKind::EscapedTransform, &b"AB\\"[..], error_position!(ErrorKind::EscapedTransform, &b"\\"[..])))));
assert_eq!(esc(&b"AB\\A"[..]), Error(error_node_position!(ErrorKind::EscapedTransform, &b"AB\\A"[..],
error_position!(ErrorKind::Alt, &b"A"[..]))));
@ -867,26 +867,26 @@ mod tests {
| tag!("agrave;") => { |_| str::as_bytes("à") }
)), to_s)
);
assert_eq!(esc2(&b"ab&egrave;DEF"[..]), Done(&b""[..], String::from("abèDEF")));
assert_eq!(esc2(&b"ab&egrave;D&agrave;EF"[..]), Done(&b""[..], String::from("abèDàEF")));
assert_eq!(esc2(&b"ab&egrave;DEF"[..]), Ok((&b""[..], String::from("abèDEF"))));
assert_eq!(esc2(&b"ab&egrave;D&agrave;EF"[..]), Ok((&b""[..], String::from("abèDàEF"))));
}
#[test]
fn issue_84() {
let r0 = is_a!(&b"aaaaefgh"[..], "abcd");
assert_eq!(r0, Done(&b"efgh"[..], &b"aaaa"[..]));
assert_eq!(r0, Ok((&b"efgh"[..], &b"aaaa"[..])));
let r1 = is_a!(&b"aaaa"[..], "abcd");
assert_eq!(r1, Done(&b""[..], &b"aaaa"[..]));
assert_eq!(r1, Ok((&b""[..], &b"aaaa"[..])));
let r2 = is_a!(&b"1"[..], "123456789");
assert_eq!(r2, Done(&b""[..], &b"1"[..]));
assert_eq!(r2, Ok((&b""[..], &b"1"[..])));
}
#[test]
fn take_str_test() {
let a = b"omnomnom";
assert_eq!(take_str!(&a[..], 5), Done(&b"nom"[..], "omnom"));
assert_eq!(take_str!(&a[..], 9), Incomplete(Needed::Size(9)));
assert_eq!(take_str!(&a[..], 5), Ok((&b"nom"[..], "omnom")));
assert_eq!(take_str!(&a[..], 9), Err(Err::Incomplete(Needed::Size(9))));
}
#[test]
@ -894,20 +894,20 @@ mod tests {
fn take_until_test() {
named!(x, take_until_and_consume!("efgh"));
let r = x(&b"abcdabcdefghijkl"[..]);
assert_eq!(r, Done(&b"ijkl"[..], &b"abcdabcd"[..]));
assert_eq!(r, Ok((&b"ijkl"[..], &b"abcdabcd"[..])));
println!("Done 1\n");
let r2 = x(&b"abcdabcdefgh"[..]);
assert_eq!(r2, Done(&b""[..], &b"abcdabcd"[..]));
assert_eq!(r2, Ok((&b""[..], &b"abcdabcd"[..])));
println!("Done 2\n");
let r3 = x(&b"abcefg"[..]);
assert_eq!(r3, Error(error_position!(ErrorKind::TakeUntilAndConsume, &b"abcefg"[..])));
assert_eq!(r3, Err(Err::Error(error_position!(ErrorKind::TakeUntilAndConsume, &b"abcefg"[..]))));
assert_eq!(
x(&b"ab"[..]),
Incomplete(Needed::Size(4))
Err(Err::Incomplete(Needed::Size(4)))
);
}
@ -916,7 +916,7 @@ mod tests {
named!(x, take_until_either!("!."));
assert_eq!(
x(&b"123!abc"[..]),
Done(&b"!abc"[..], &b"123"[..])
Ok((&b"!abc"[..], &b"123"[..]))
);
}
@ -925,7 +925,7 @@ mod tests {
named!(x, take_until_either!("!."));
assert_eq!(
x(&b"123"[..]),
Error(error_position!(ErrorKind::TakeUntilEither, &b"123"[..]))
Err(Err::Error(error_position!(ErrorKind::TakeUntilEither, &b"123"[..])))
);
}
@ -934,7 +934,7 @@ mod tests {
named!(x, take_until_either_and_consume!("!."));
assert_eq!(
x(&b"123.abc"[..]),
Done(&b"abc"[..], &b"123"[..])
Ok((&b"abc"[..], &b"123"[..]))
);
}
@ -944,11 +944,11 @@ mod tests {
named!(y, take_until!("end"));
assert_eq!(
y(&b"nd"[..]),
Incomplete(Needed::Size(3))
Err(Err::Incomplete(Needed::Size(3)))
);
assert_eq!(
y(&b"123"[..]),
Error(error_position!(ErrorKind::TakeUntil, &b"123"[..]))
Err(Err::Error(error_position!(ErrorKind::TakeUntil, &b"123"[..])))
);
}
@ -956,37 +956,37 @@ mod tests {
fn recognize() {
named!(x, recognize!(delimited!(tag!("<!--"), take!(5), tag!("-->"))));
let r = x(&b"<!-- abc --> aaa"[..]);
assert_eq!(r, Done(&b" aaa"[..], &b"<!-- abc -->"[..]));
assert_eq!(r, Ok((&b" aaa"[..], &b"<!-- abc -->"[..])));
let empty = &b""[..];
named!(ya, recognize!(alpha));
let ra = ya(&b"abc"[..]);
assert_eq!(ra, Done(empty, &b"abc"[..]));
assert_eq!(ra, Ok((empty, &b"abc"[..])));
named!(yd, recognize!(digit));
let rd = yd(&b"123"[..]);
assert_eq!(rd, Done(empty, &b"123"[..]));
assert_eq!(rd, Ok((empty, &b"123"[..])));
named!(yhd, recognize!(hex_digit));
let rhd = yhd(&b"123abcDEF"[..]);
assert_eq!(rhd, Done(empty, &b"123abcDEF"[..]));
assert_eq!(rhd, Ok((empty, &b"123abcDEF"[..])));
named!(yod, recognize!(oct_digit));
let rod = yod(&b"1234567"[..]);
assert_eq!(rod, Done(empty, &b"1234567"[..]));
assert_eq!(rod, Ok((empty, &b"1234567"[..])));
named!(yan, recognize!(alphanumeric));
let ran = yan(&b"123abc"[..]);
assert_eq!(ran, Done(empty, &b"123abc"[..]));
assert_eq!(ran, Ok((empty, &b"123abc"[..])));
named!(ys, recognize!(space));
let rs = ys(&b" \t"[..]);
assert_eq!(rs, Done(empty, &b" \t"[..]));
assert_eq!(rs, Ok((empty, &b" \t"[..])));
named!(yms, recognize!(multispace));
let rms = yms(&b" \t\r\n"[..]);
assert_eq!(rms, Done(empty, &b" \t\r\n"[..]));
assert_eq!(rms, Ok((empty, &b" \t\r\n"[..])));
}
#[test]
@ -998,10 +998,10 @@ mod tests {
let c = b"abcd123";
let d = b"123";
assert_eq!(f(&a[..]), Done(&a[..], &a[..]));
assert_eq!(f(&b[..]), Done(&a[..], &b[..]));
assert_eq!(f(&c[..]), Done(&d[..], &b[..]));
assert_eq!(f(&d[..]), Done(&d[..], &a[..]));
assert_eq!(f(&a[..]), Ok((&a[..], &a[..])));
assert_eq!(f(&b[..]), Ok((&a[..], &b[..])));
assert_eq!(f(&c[..]), Ok((&d[..], &b[..])));
assert_eq!(f(&d[..]), Ok((&d[..], &a[..])));
}
#[test]
@ -1013,10 +1013,10 @@ mod tests {
let c = b"abcd123";
let d = b"123";
assert_eq!(f(&a[..]), Incomplete(Needed::Size(1)));
assert_eq!(f(&b[..]), Done(&a[..], &b[..]));
assert_eq!(f(&c[..]), Done(&b"123"[..], &b[..]));
assert_eq!(f(&d[..]), Error(error_position!(ErrorKind::TakeWhile1, &d[..])));
assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1))));
assert_eq!(f(&b[..]), Ok((&a[..], &b[..])));
assert_eq!(f(&c[..]), Ok((&b"123"[..], &b[..])));
assert_eq!(f(&d[..]), Err(Err::Error(error_position!(ErrorKind::TakeWhile1, &d[..]))));
}
#[test]
@ -1028,10 +1028,10 @@ mod tests {
let c = b"123abcd";
let d = b"123";
assert_eq!(f(&a[..]), Done(&b""[..], &b""[..]));
assert_eq!(f(&b[..]), Done(&b"abcd"[..], &b""[..]));
assert_eq!(f(&c[..]), Done(&b"abcd"[..], &b"123"[..]));
assert_eq!(f(&d[..]), Done(&b""[..], &b"123"[..]));
assert_eq!(f(&a[..]), Ok((&b""[..], &b""[..])));
assert_eq!(f(&b[..]), Ok((&b"abcd"[..], &b""[..])));
assert_eq!(f(&c[..]), Ok((&b"abcd"[..], &b"123"[..])));
assert_eq!(f(&d[..]), Ok((&b""[..], &b"123"[..])));
}
#[test]
@ -1043,10 +1043,10 @@ mod tests {
let c = b"123abcd";
let d = b"123";
assert_eq!(f(&a[..]), Incomplete(Needed::Size(1)));
assert_eq!(f(&b[..]), Error(error_position!(ErrorKind::TakeTill1, &b[..])));
assert_eq!(f(&c[..]), Done(&b"abcd"[..], &b"123"[..]));
assert_eq!(f(&d[..]), Done(&b""[..], &b"123"[..]));
assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1))));
assert_eq!(f(&b[..]), Err(Err::Error(error_position!(ErrorKind::TakeTill1, &b[..]))));
assert_eq!(f(&c[..]), Ok((&b"abcd"[..], &b"123"[..])));
assert_eq!(f(&d[..]), Ok((&b""[..], &b"123"[..])));
}
#[cfg(feature = "nightly")]
@ -1068,44 +1068,44 @@ mod tests {
use nom::is_alphanumeric;
named!(x, take_while!(is_alphanumeric));
named!(y, recognize!(x));
assert_eq!(x(&b"ab"[..]), Done(&[][..], &b"ab"[..]));
assert_eq!(x(&b"ab"[..]), Ok((&[][..], &b"ab"[..])));
println!("X: {:?}", x(&b"ab"[..]));
assert_eq!(y(&b"ab"[..]), Done(&[][..], &b"ab"[..]));
assert_eq!(y(&b"ab"[..]), Ok((&[][..], &b"ab"[..])));
}
#[test]
fn length_bytes() {
use nom::le_u8;
named!(x, length_bytes!(le_u8));
assert_eq!(x(b"\x02..>>"), Done(&b">>"[..], &b".."[..]));
assert_eq!(x(b"\x02.."), Done(&[][..], &b".."[..]));
assert_eq!(x(b"\x02."), Incomplete(Needed::Size(3)));
assert_eq!(x(b"\x02"), Incomplete(Needed::Size(3)));
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(3))));
assert_eq!(x(b"\x02"), Err(Err::Incomplete(Needed::Size(3))));
named!(y, do_parse!(tag!("magic") >> b: length_bytes!(le_u8) >> (b)));
assert_eq!(y(b"magic\x02..>>"), Done(&b">>"[..], &b".."[..]));
assert_eq!(y(b"magic\x02.."), Done(&[][..], &b".."[..]));
assert_eq!(y(b"magic\x02."), Incomplete(Needed::Size(8)));
assert_eq!(y(b"magic\x02"), Incomplete(Needed::Size(8)));
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(8))));
assert_eq!(y(b"magic\x02"), Err(Err::Incomplete(Needed::Size(8))));
}
#[test]
fn case_insensitive() {
named!(test, tag_no_case!("ABcd"));
assert_eq!(test(&b"aBCdefgh"[..]), Done(&b"efgh"[..], &b"aBCd"[..]));
assert_eq!(test(&b"abcdefgh"[..]), Done(&b"efgh"[..], &b"abcd"[..]));
assert_eq!(test(&b"ABCDefgh"[..]), Done(&b"efgh"[..], &b"ABCD"[..]));
assert_eq!(test(&b"ab"[..]), Incomplete(Needed::Size(4)));
assert_eq!(test(&b"Hello"[..]), Error(error_position!(ErrorKind::Tag, &b"Hello"[..])));
assert_eq!(test(&b"Hel"[..]), Error(error_position!(ErrorKind::Tag, &b"Hel"[..])));
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!(ErrorKind::Tag, &b"Hello"[..]))));
assert_eq!(test(&b"Hel"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"Hel"[..]))));
named!(test2<&str, &str>, tag_no_case!("ABcd"));
assert_eq!(test2("aBCdefgh"), Done("efgh", "aBCd"));
assert_eq!(test2("abcdefgh"), Done("efgh", "abcd"));
assert_eq!(test2("ABCDefgh"), Done("efgh", "ABCD"));
assert_eq!(test2("ab"), Incomplete(Needed::Size(4)));
assert_eq!(test2("Hello"), Error(error_position!(ErrorKind::Tag, &"Hello"[..])));
assert_eq!(test2("Hel"), Error(error_position!(ErrorKind::Tag, &"Hel"[..])));
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!(ErrorKind::Tag, &"Hello"[..]))));
assert_eq!(test2("Hel"), Err(Err::Error(error_position!(ErrorKind::Tag, &"Hel"[..]))));
}
#[test]
@ -1113,7 +1113,7 @@ mod tests {
named!(test, tag!([0x42]));
named!(test2, tag!(&[0x42]));
let input = [0x42, 0x00];
assert_eq!(test(&input), Done(&b"\x00"[..], &b"\x42"[..]));
assert_eq!(test2(&input), Done(&b"\x00"[..], &b"\x42"[..]));
assert_eq!(test(&input), Ok((&b"\x00"[..], &b"\x42"[..])));
assert_eq!(test2(&input), Ok((&b"\x00"[..], &b"\x42"[..])));
}
}

View File

@ -30,10 +30,10 @@ macro_rules! one_of (
match ($i).iter_elements().next().map(|c| {
(c, c.find_token($inp))
}) {
None => $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)),
Some((_, false)) => $crate::IResult::Error(error_position!($crate::ErrorKind::OneOf, $i)),
None => ::std::result::Result::Err::<_,_>(Err::Incomplete($crate::Needed::Size(1))),
Some((_, false)) => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::OneOf, $i))),
//the unwrap should be safe here
Some((c, true)) => $crate::IResult::Done($i.slice(c.len()..), $i.iter_elements().next().unwrap().as_char())
Some((c, true)) => ::std::result::Result::Ok(( $i.slice(c.len()..), $i.iter_elements().next().unwrap().as_char() ))
}
}
);
@ -52,10 +52,10 @@ macro_rules! none_of (
match ($i).iter_elements().next().map(|c| {
(c, !c.find_token($inp))
}) {
None => $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)),
Some((_, false)) => $crate::IResult::Error(error_position!($crate::ErrorKind::NoneOf, $i)),
None => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(1))),
Some((_, false)) => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::NoneOf, $i))),
//the unwrap should be safe here
Some((c, true)) => $crate::IResult::Done($i.slice(c.len()..), $i.iter_elements().next().unwrap().as_char())
Some((c, true)) => ::std::result::Result::Ok(( $i.slice(c.len()..), $i.iter_elements().next().unwrap().as_char() ))
}
}
);
@ -73,10 +73,10 @@ macro_rules! char (
match ($i).iter_elements().next().map(|c| {
(c, c.as_char() == $c)
}) {
None => $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1)),
Some((_, false)) => $crate::IResult::Error(error_position!($crate::ErrorKind::Char, $i)),
None => ::std::result::Result::Err::<_,_>($crate::Err::Incomplete($crate::Needed::Size(1))),
Some((_, false)) => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Char, $i))),
//the unwrap should be safe here
Some((c, true)) => $crate::IResult::Done($i.slice(c.len()..), $i.iter_elements().next().unwrap().as_char())
Some((c, true)) => ::std::result::Result::Ok(( $i.slice(c.len()..), $i.iter_elements().next().unwrap().as_char() ))
}
}
);
@ -89,16 +89,17 @@ named!(#[doc="Matches a tab character '\\t'"], pub tab<char>, char!('\t'));
pub fn anychar<T>(input: T) -> IResult<T, char> where
T: InputIter+InputLength+Slice<RangeFrom<usize>>,
<T as InputIter>::Item: AsChar {
use simple_errors::Err;
if input.input_len() == 0 {
IResult::Incomplete(Needed::Size(1))
Err(Err::Incomplete(Needed::Size(1)))
} else {
IResult::Done(input.slice(1..), input.iter_elements().next().expect("slice should contain at least one element").as_char())
Ok((input.slice(1..), input.iter_elements().next().expect("slice should contain at least one element").as_char()))
}
}
#[cfg(test)]
mod tests {
use internal::IResult::*;
use simple_errors::Err;
use util::ErrorKind;
#[test]
@ -106,16 +107,16 @@ mod tests {
named!(f<char>, one_of!("ab"));
let a = &b"abcd"[..];
assert_eq!(f(a), Done(&b"bcd"[..], 'a'));
assert_eq!(f(a),Ok((&b"bcd"[..], 'a')));
let b = &b"cde"[..];
assert_eq!(f(b), Error(error_position!(ErrorKind::OneOf, b)));
assert_eq!(f(b), Err(Err::Error(error_position!(ErrorKind::OneOf, b))));
named!(utf8(&str) -> char,
one_of!("+\u{FF0B}"));
assert!(utf8("+").is_done());
assert!(utf8("\u{FF0B}").is_done());
assert!(utf8("+").is_ok());
assert!(utf8("\u{FF0B}").is_ok());
}
#[test]
@ -123,10 +124,10 @@ mod tests {
named!(f<char>, none_of!("ab"));
let a = &b"abcd"[..];
assert_eq!(f(a), Error(error_position!(ErrorKind::NoneOf, a)));
assert_eq!(f(a), Err(Err::Error(error_position!(ErrorKind::NoneOf, a))));
let b = &b"cde"[..];
assert_eq!(f(b), Done(&b"de"[..], 'c'));
assert_eq!(f(b),Ok((&b"de"[..], 'c')));
}
#[test]
@ -134,10 +135,10 @@ mod tests {
named!(f<char>, char!('c'));
let a = &b"abcd"[..];
assert_eq!(f(a), Error(error_position!(ErrorKind::Char, a)));
assert_eq!(f(a), Err(Err::Error(error_position!(ErrorKind::Char, a))));
let b = &b"cde"[..];
assert_eq!(f(b), Done(&b"de"[..], 'c'));
assert_eq!(f(b),Ok((&b"de"[..], 'c')));
}
}

View File

@ -1,6 +1,6 @@
//! Basic types to build the parsers
use self::IResult::*;
//use self::IResult::*;
use self::Needed::*;
#[cfg(not(feature = "std"))]
@ -68,6 +68,9 @@ pub enum IResult<I,O,E=u32> {
///
/// It depends on I, the input type, O, the output type, and E, the error type (by default u32)
///
pub type IResult<I,O,E=u32> = Result<(I,O), Err<E>>;
/*
#[derive(Debug,PartialEq,Eq,Clone)]
pub enum IResult<I,O,E=u32> {
/// indicates a correct parsing, the first field containing the rest of the unparsed data, the second field contains the parsed data
@ -76,8 +79,9 @@ pub enum IResult<I,O,E=u32> {
Error(Err<E>),
/// Incomplete contains a Needed, an enum than can represent a known quantity of input data, or unknown
Incomplete(Needed)
}
}*/
/*
#[cfg(feature = "verbose-errors")]
/// This is the same as IResult, but without Done
///
@ -244,7 +248,7 @@ impl<'a,I,E> GetOutput<&'a str> for IResult<I,&'a str,E> {
_ => None
}
}
}
}*/
#[cfg(feature = "verbose-errors")]
/// creates a parse error from a `nom::ErrorKind`
@ -377,10 +381,11 @@ mod tests {
use util::ErrorKind;
const REST: [u8; 0] = [];
const DONE: IResult<&'static [u8], u32> = IResult::Done(&REST, 5);
const ERROR: IResult<&'static [u8], u32> = IResult::Error(error_code!(ErrorKind::Tag));
const INCOMPLETE: IResult<&'static [u8], u32> = IResult::Incomplete(Needed::Unknown);
const DONE: IResult<&'static [u8], u32> = Ok((&REST, 5));
const ERROR: IResult<&'static [u8], u32> = Err(Err::Error(error_code!(ErrorKind::Tag)));
const INCOMPLETE: IResult<&'static [u8], u32> = Err(Err::Incomplete(Needed::Unknown));
/*
#[test]
fn iresult_or() {
assert_eq!(DONE.or(ERROR), DONE);
@ -512,4 +517,5 @@ mod tests {
assert_eq!(INCOMPLETE.to_full_result(), Err(IError::Incomplete(Needed::Unknown)));
assert_eq!(ERROR.to_full_result(), Err(IError::Error(error_code!(ErrorKind::Tag))));
}
*/
}

View File

@ -441,6 +441,7 @@ pub use self::branch::*;
pub use self::sequence::*;
pub use self::multi::*;
pub use self::methods::*;
pub use self::bytes::*;
pub use self::bits::*;
@ -451,11 +452,11 @@ pub use self::whitespace::*;
#[cfg(feature = "regexp")]
pub use self::regexp::*;
/*
#[cfg(feature = "std")]
#[cfg(feature = "stream")]
pub use self::stream::*;
*/
pub use self::str::*;
#[macro_use] mod util;
@ -471,6 +472,7 @@ mod traits;
#[macro_use] mod sequence;
#[macro_use] mod multi;
#[macro_use] pub mod methods;
#[macro_use] mod bytes;
#[macro_use] pub mod bits;
@ -483,9 +485,9 @@ pub mod whitespace;
#[cfg(feature = "regexp")]
#[macro_use] mod regexp;
#[macro_use]
/*#[macro_use]
#[cfg(feature = "std")]
#[cfg(feature = "stream")]
mod stream;
*/
mod str;

View File

@ -358,10 +358,10 @@ macro_rules! return_error (
};
match cl() {
$crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x),
$crate::IResult::Done(i, o) => $crate::IResult::Done(i, o),
$crate::IResult::Error(e) => {
return $crate::IResult::Error(error_node_position!($code, $i, e))
::std::result::Result::Err($crate::Err::Incomplete(x)) => ::std::result::Result::Err($crate::Err::Incomplete(x)),
::std::result::Result::Ok((i, o)) => ::std::result::Result::Ok((i, o)),
::std::result::Result::Err($crate::Err::Error(e)) => {
return ::std::result::Result::Err($crate::Err::Error(error_node_position!($code, $i, e)))
}
}
}
@ -398,10 +398,10 @@ macro_rules! add_return_error (
($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => (
{
match $submac!($i, $($args)*) {
$crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x),
$crate::IResult::Done(i, o) => $crate::IResult::Done(i, o),
$crate::IResult::Error(e) => {
$crate::IResult::Error(error_node_position!($code, $i, e))
::std::result::Result::Err($crate::Err::Incomplete(x)) => ::std::result::Result::Err($crate::Err::Incomplete(x)),
::std::result::Result::Ok((i, o)) => ::std::result::Result::Ok((i, o)),
::std::result::Result::Err($crate::Err::Error(e)) => {
::std::result::Result::Err($crate::Err::Error(error_node_position!($code, $i, e)))
}
}
}
@ -436,10 +436,10 @@ macro_rules! complete (
{
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Done(i, o) => $crate::IResult::Done(i, o),
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete(_) => {
$crate::IResult::Error(error_position!($crate::ErrorKind::Complete, $i))
::std::result::Result::Ok((i, o)) => ::std::result::Result::Ok((i, o)),
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete(_)) => {
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Complete, $i)))
},
}
}
@ -455,7 +455,7 @@ macro_rules! complete (
///
/// ```
/// # #[macro_use] extern crate nom;
/// # use nom::IResult::{self, Done, Error};
/// # use nom::IResult::{self, Ok(, Error};
/// # #[cfg(feature = "verbose-errors")]
/// # use nom::Err::Position;
/// # use nom::{be_u8,ErrorKind};
@ -464,12 +464,12 @@ macro_rules! complete (
/// let (i1, sz) = try_parse!(input, be_u8);
/// let (i2, length) = try_parse!(i1, expr_opt!(size.checked_add(sz)));
/// let (i3, data) = try_parse!(i2, take!(length));
/// return Done(i3, data);
/// return Ok((i3, data);
/// }
/// # fn main() {
/// let arr1 = [1, 2, 3, 4, 5];
/// let r1 = take_add(&arr1[..], 1);
/// assert_eq!(r1, Done(&[4,5][..], &[2,3][..]));
/// assert_eq!(r1, Ok((&[4,5][..], &[2,3][..]));
///
/// let arr2 = [0xFE, 2, 3, 4, 5];
/// // size is overflowing
@ -481,9 +481,9 @@ macro_rules! complete (
macro_rules! try_parse (
($i:expr, $submac:ident!( $($args:tt)* )) => (
match $submac!($i, $($args)*) {
$crate::IResult::Done(i,o) => (i,o),
$crate::IResult::Error(e) => return $crate::IResult::Error(e),
$crate::IResult::Incomplete(i) => return $crate::IResult::Incomplete(i)
::std::result::Result::Ok((i,o)) => (i,o),
::std::result::Result::Err($crate::Err::Error(e)) => return ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => return ::std::result::Result::Err($crate::Err::Incomplete(i))
}
);
($i:expr, $f:expr) => (
@ -502,10 +502,10 @@ macro_rules! map(
f(t)
}
match $submac!($i, $($args)*) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)),
$crate::IResult::Done(i, o) => $crate::IResult::Done(i, _unify($g, o))
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))),
::std::result::Result::Ok((i, o)) => ::std::result::Result::Ok((i, _unify($g, o)))
}
}
);
@ -526,12 +526,12 @@ macro_rules! map_res (
{
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)),
$crate::IResult::Done(i, o) => match $submac2!(o, $($args2)*) {
Ok(output) => $crate::IResult::Done(i, output),
Err(_) => $crate::IResult::Error(error_position!($crate::ErrorKind::MapRes, $i))
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))),
::std::result::Result::Ok((i, o)) => match $submac2!(o, $($args2)*) {
Ok(output) => ::std::result::Result::Ok((i, output)),
Err(_) => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::MapRes, $i)))
}
}
}
@ -559,12 +559,12 @@ macro_rules! map_opt (
{
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)),
$crate::IResult::Done(i, o) => match $submac2!(o, $($args2)*) {
::std::option::Option::Some(output) => $crate::IResult::Done(i, output),
::std::option::Option::None => $crate::IResult::Error(error_position!($crate::ErrorKind::MapOpt, $i))
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))),
::std::result::Result::Ok((i, o)) => match $submac2!(o, $($args2)*) {
::std::option::Option::Some(output) => ::std::result::Result::Ok((i, output)),
::std::option::Option::None => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::MapOpt, $i)))
}
}
}
@ -596,8 +596,8 @@ macro_rules! parse_to (
use $crate::Slice;
use $crate::InputLength;
match ($i).parse_to() {
::std::option::Option::Some(output) => $crate::IResult::Done($i.slice(..$i.input_len()), output),
::std::option::Option::None => $crate::IResult::Error(error_position!($crate::ErrorKind::MapOpt, $i))
::std::option::Option::Some(output) => ::std::result::Result::Ok(($i.slice(..$i.input_len()), output)),
::std::option::Option::None => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::MapOpt, $i)))
}
}
);
@ -621,13 +621,13 @@ macro_rules! verify (
{
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)),
$crate::IResult::Done(i, o) => if $submac2!(o, $($args2)*) {
$crate::IResult::Done(i, o)
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))),
::std::result::Result::Ok((i, o)) => if $submac2!(o, $($args2)*) {
::std::result::Result::Ok((i, o))
} else {
$crate::IResult::Error(error_position!($crate::ErrorKind::Verify, $i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Verify, $i)))
}
}
}
@ -660,10 +660,10 @@ macro_rules! verify (
/// named!(x<u8>, value!(42, delimited!(tag!("<!--"), take!(5), tag!("-->"))));
/// named!(y<u8>, delimited!(tag!("<!--"), value!(42), tag!("-->")));
/// let r = x(&b"<!-- abc --> aaa"[..]);
/// assert_eq!(r, Done(&b" aaa"[..], 42));
/// assert_eq!(r, Ok((&b" aaa"[..], 42));
///
/// let r2 = y(&b"<!----> aaa"[..]);
/// assert_eq!(r2, Done(&b" aaa"[..], 42));
/// assert_eq!(r2, Ok((&b" aaa"[..], 42));
/// # }
/// ```
#[macro_export]
@ -671,12 +671,12 @@ macro_rules! value (
($i:expr, $res:expr, $submac:ident!( $($args:tt)* )) => (
{
match $submac!($i, $($args)*) {
$crate::IResult::Done(i,_) => {
let res: $crate::IResult<_,_> = $crate::IResult::Done(i, $res);
::std::result::Result::Ok((i,_)) => {
let res: $crate::IResult<_,_> = ::std::result::Result::Ok((i, $res));
res
},
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i)
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i))
}
}
);
@ -685,7 +685,7 @@ macro_rules! value (
);
($i:expr, $res:expr) => (
{
let res: $crate::IResult<_,_> = $crate::IResult::Done($i, $res);
let res: $crate::IResult<_,_> = ::std::result::Result::Ok(($i, $res));
res
}
);
@ -700,8 +700,8 @@ macro_rules! expr_res (
($i:expr, $e:expr) => (
{
match $e {
Ok(output) => $crate::IResult::Done($i, output),
Err(_) => $crate::IResult::Error(error_position!($crate::ErrorKind::ExprRes, $i))
Ok(output) => ::std::result::Result::Ok(($i, output)),
Err(_) => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::ExprRes, $i)))
}
}
);
@ -714,7 +714,7 @@ macro_rules! expr_res (
///
/// ```
/// # #[macro_use] extern crate nom;
/// # use nom::IResult::{self, Done, Error};
/// # use nom::IResult::{self, Ok(, Error};
/// # #[cfg(feature = "verbose-errors")]
/// # use nom::Err::Position;
/// # use nom::{be_u8,ErrorKind};
@ -730,7 +730,7 @@ macro_rules! expr_res (
/// # fn main() {
/// let arr1 = [1, 2, 3, 4, 5];
/// let r1 = take_add(&arr1[..], 1);
/// assert_eq!(r1, Done(&[4,5][..], &[2,3][..]));
/// assert_eq!(r1, Ok((&[4,5][..], &[2,3][..]));
///
/// let arr2 = [0xFE, 2, 3, 4, 5];
/// // size is overflowing
@ -743,8 +743,8 @@ macro_rules! expr_opt (
($i:expr, $e:expr) => (
{
match $e {
::std::option::Option::Some(output) => $crate::IResult::Done($i, output),
::std::option::Option::None => $crate::IResult::Error(error_position!($crate::ErrorKind::ExprOpt, $i))
::std::option::Option::Some(output) => ::std::result::Result::Ok(($i, output)),
::std::option::Option::None => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::ExprOpt, $i)))
}
}
);
@ -764,8 +764,8 @@ macro_rules! expr_opt (
///
/// let a = b"abcdef";
/// let b = b"bcdefg";
/// assert_eq!(o(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..])));
/// assert_eq!(o(&b[..]), Done(&b"bcdefg"[..], None));
/// assert_eq!(o(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])));
/// assert_eq!(o(&b[..]), Ok((&b"bcdefg"[..], None));
/// # }
/// ```
#[macro_export]
@ -774,10 +774,10 @@ macro_rules! opt(
{
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
::std::result::Result::Ok((i,o)) => ::std::result::Result::Ok((i, ::std::option::Option::Some(o))),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
_ => {
let res: $crate::IResult<_,_> = $crate::IResult::Done($i, ::std::option::Option::None);
let res: $crate::IResult<_,_> = ::std::result::Result::Ok(($i, ::std::option::Option::None));
res
},
}
@ -804,8 +804,8 @@ macro_rules! opt(
///
/// let a = b"abcdef";
/// let b = b"bcdefg";
/// assert_eq!(o(&a[..]), Done(&b"ef"[..], Ok(&b"abcd"[..])));
/// assert_eq!(o(&b[..]), Done(&b"bcdefg"[..], Err(error_position!(ErrorKind::Tag, &b[..]))));
/// assert_eq!(o(&a[..]), Ok((&b"ef"[..], Ok(&b"abcd"[..])));
/// assert_eq!(o(&b[..]), Ok((&b"bcdefg"[..], Err(error_position!(ErrorKind::Tag, &b[..]))));
/// # }
/// ```
#[macro_export]
@ -814,9 +814,9 @@ macro_rules! opt_res (
{
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::result::Result::Ok(o)),
$crate::IResult::Error(e) => $crate::IResult::Done($i, ::std::result::Result::Err(e)),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i)
::std::result::Result::Ok((i,o)) => ::std::result::Result::Ok((i, ::std::result::Result::Ok(o))),
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Ok(($i, ::std::result::Result::Err($crate::Err::Error(e)))),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i))
}
}
);
@ -848,13 +848,13 @@ macro_rules! opt_res (
/// );
///
/// let a = b"abcdef";
/// assert_eq!(f(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..])));
/// assert_eq!(f(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])));
///
/// let b2 = false;
/// let f2:Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8],
/// cond!( b2, tag!("abcd") ))
/// );
/// assert_eq!(f2(&a[..]), Done(&b"abcdef"[..], None));
/// assert_eq!(f2(&a[..]), Ok((&b"abcdef"[..], None));
/// # }
/// ```
///
@ -864,12 +864,12 @@ macro_rules! cond_with_error(
{
if $cond {
match $submac!($i, $($args)*) {
$crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)),
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i)
::std::result::Result::Ok((i,o)) => ::std::result::Result::Ok((i, ::std::option::Option::Some(o))),
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i))
}
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Done($i, ::std::option::Option::None);
let res: ::std::result::Result<_,_> = ::std::result::Result::Ok(($i, ::std::option::Option::None));
res
}
}
@ -902,13 +902,13 @@ macro_rules! cond_with_error(
/// );
///
/// let a = b"abcdef";
/// assert_eq!(f(&a[..]), Done(&b"ef"[..], Some(&b"abcd"[..])));
/// assert_eq!(f(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])));
///
/// let b2 = false;
/// let f2:Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8],
/// cond!( b2, tag!("abcd") ))
/// );
/// assert_eq!(f2(&a[..]), Done(&b"abcdef"[..], None));
/// assert_eq!(f2(&a[..]), Ok((&b"abcdef"[..], None));
/// # }
/// ```
///
@ -919,15 +919,15 @@ macro_rules! cond(
if $cond {
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Done(i,o) => $crate::IResult::Done(i, ::std::option::Option::Some(o)),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Error(_) => {
let res: $crate::IResult<_,_> = $crate::IResult::Done($i, ::std::option::Option::None);
::std::result::Result::Ok((i,o)) => ::std::result::Result::Ok((i, ::std::option::Option::Some(o))),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Err($crate::Err::Error(_)) => {
let res: $crate::IResult<_,_,u32> = ::std::result::Result::Ok(($i, ::std::option::Option::None));
res
},
}
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Done($i, ::std::option::Option::None);
let res: ::std::result::Result<_,_> = ::std::result::Result::Ok(($i, ::std::option::Option::None));
res
}
}
@ -959,7 +959,7 @@ macro_rules! cond(
/// );
///
/// let a = b"abcdef";
/// assert_eq!(f(&a[..]), Done(&b"ef"[..], &b"abcd"[..]));
/// assert_eq!(f(&a[..]), Ok((&b"ef"[..], &b"abcd"[..]));
///
/// let b2 = false;
/// let f2 = closure!(&'static[u8],
@ -975,12 +975,12 @@ macro_rules! cond_reduce(
{
if $cond {
match $submac!($i, $($args)*) {
$crate::IResult::Done(i,o) => $crate::IResult::Done(i, o),
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i)
::std::result::Result::Ok((i,o)) => ::std::result::Result::Ok((i, o)),
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i))
}
} else {
$crate::IResult::Error(error_position!($crate::ErrorKind::CondReduce, $i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::CondReduce, $i)))
}
}
);
@ -992,7 +992,7 @@ macro_rules! cond_reduce(
/// `peek!(I -> IResult<I,O>) => I -> IResult<I, O>`
/// returns a result without consuming the input
///
/// the embedded parser may return Incomplete
/// the embedded parser may return Err($crate::Err::Incomplete
///
/// ```
/// # #[macro_use] extern crate nom;
@ -1001,7 +1001,7 @@ macro_rules! cond_reduce(
/// named!(ptag, peek!( tag!( "abcd" ) ) );
///
/// let r = ptag(&b"abcdefgh"[..]);
/// assert_eq!(r, Done(&b"abcdefgh"[..], &b"abcd"[..]));
/// assert_eq!(r, Ok((&b"abcdefgh"[..], &b"abcd"[..]));
/// # }
/// ```
#[macro_export]
@ -1010,9 +1010,9 @@ macro_rules! peek(
{
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Done(_,o) => $crate::IResult::Done($i, o),
$crate::IResult::Error(a) => $crate::IResult::Error(a),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i)
::std::result::Result::Ok((_,o)) => ::std::result::Result::Ok(($i, o)),
::std::result::Result::Err($crate::Err::Error(a)) => ::std::result::Result::Err($crate::Err::Error(a)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
}
}
);
@ -1022,7 +1022,7 @@ macro_rules! peek(
);
/// `not!(I -> IResult<I,O>) => I -> IResult<I, O>`
/// returns a result only if the embedded parser returns Error or Incomplete
/// returns a result only if the embedded parser returns Error or Err($crate::Err::Incomplete
/// does not consume the input
///
/// ```
@ -1039,7 +1039,7 @@ macro_rules! peek(
/// ));
///
/// let r = not_e(&b"abcd"[..]);
/// assert_eq!(r, Done(&b"d"[..], &b"abc"[..]));
/// assert_eq!(r, Ok((&b"d"[..], &b"abc"[..]));
///
/// let r2 = not_e(&b"abce"[..]);
/// assert_eq!(r2, Error(error_position!(ErrorKind::Not, &b"e"[..])));
@ -1052,9 +1052,8 @@ macro_rules! not(
use $crate::Slice;
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Done(_, _) => $crate::IResult::Error(error_position!($crate::ErrorKind::Not, $i)),
$crate::IResult::Error(_) => $crate::IResult::Done($i, ($i).slice(..0)),
$crate::IResult::Incomplete(_) => $crate::IResult::Done($i, ($i).slice(..0))
::std::result::Result::Ok(_) => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Not, $i))),
::std::result::Result::Err(_) => ::std::result::Result::Ok(($i, ($i).slice(..0))),
}
}
);
@ -1074,7 +1073,7 @@ macro_rules! not(
/// named!(ptag, tap!(res: tag!( "abcd" ) => { println!("recognized {}", str::from_utf8(res).unwrap()) } ) );
///
/// let r = ptag(&b"abcdefgh"[..]);
/// assert_eq!(r, Done(&b"efgh"[..], &b"abcd"[..]));
/// assert_eq!(r, Ok((&b"efgh"[..], &b"abcd"[..]));
/// # }
/// ```
#[macro_export]
@ -1082,13 +1081,13 @@ macro_rules! tap (
($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => (
{
match $submac!($i, $($args)*) {
$crate::IResult::Done(i,o) => {
::std::result::Result::Ok((i,o)) => {
let $name = o;
$e;
$crate::IResult::Done(i, $name)
::std::result::Result::Ok((i, $name))
},
$crate::IResult::Error(a) => $crate::IResult::Error(a),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i)
::std::result::Result::Err($crate::Err::Error(a)) => ::std::result::Result::Err($crate::Err::Error(a)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i))
}
}
);
@ -1108,9 +1107,9 @@ macro_rules! eof (
{
use $crate::InputLength;
if ($i).input_len() == 0 {
$crate::IResult::Done($i, $i)
::std::result::Result::Ok(($i, $i))
} else {
$crate::IResult::Error(error_position!($crate::ErrorKind::Eof, $i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Eof, $i)))
}
}
);
@ -1125,7 +1124,7 @@ macro_rules! eof (
/// # fn main() {
/// named!(x, recognize!(delimited!(tag!("<!--"), take!(5), tag!("-->"))));
/// let r = x(&b"<!-- abc --> aaa"[..]);
/// assert_eq!(r, Done(&b" aaa"[..], &b"<!-- abc -->"[..]));
/// assert_eq!(r, Ok((&b" aaa"[..], &b"<!-- abc -->"[..]));
/// # }
/// ```
#[macro_export]
@ -1136,12 +1135,12 @@ macro_rules! recognize (
use $crate::Slice;
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Done(i,_) => {
::std::result::Result::Ok((i,_)) => {
let index = (&$i).offset(&i);
$crate::IResult::Done(i, ($i).slice(..index))
::std::result::Result::Ok((i, ($i).slice(..index)))
},
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i)
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i))
}
}
);
@ -1157,10 +1156,10 @@ mod tests {
#[cfg(feature = "verbose-errors")]
use verbose_errors::Err;
#[cfg(not(feature = "verbose-errors"))]
//#[cfg(not(feature = "verbose-errors"))]
use simple_errors::Err;
use internal::IResult::*;
//use internal::IResult::*;
use util::ErrorKind;
// reproduce the tag and take macros, because of module import order
@ -1190,12 +1189,12 @@ mod tests {
let reduced = &$i[..m];
let b = &$bytes[..m];
let res: $crate::IResult<_,_> = if reduced != b {
$crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i))
let res: $crate::IResult<_,_,u32> = if reduced != b {
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Tag, $i)))
} else if m < blen {
$crate::IResult::Incomplete($crate::Needed::Size(blen))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(blen)))
} else {
$crate::IResult::Done(&$i[blen..], reduced)
::std::result::Result::Ok((&$i[blen..], reduced))
};
res
}
@ -1207,9 +1206,9 @@ mod tests {
{
let cnt = $count as usize;
let res:$crate::IResult<&[u8],&[u8]> = if $i.len() < cnt {
$crate::IResult::Incomplete($crate::Needed::Size(cnt))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(cnt)))
} else {
$crate::IResult::Done(&$i[cnt..],&$i[0..cnt])
::std::result::Result::Ok((&$i[cnt..],&$i[0..cnt]))
};
res
}
@ -1225,7 +1224,7 @@ mod tests {
fn pub_named_test() {
let a = &b"abcd"[..];
let res = pub_named_mod::tst(a);
assert_eq!(res, Done(&b""[..], a));
assert_eq!(res, Ok((&b""[..], a)));
}
#[test]
@ -1246,9 +1245,9 @@ mod tests {
let a = &b"abcdef"[..];
let b = &b"bcdefg"[..];
let c = &b"ab"[..];
assert_eq!(opt_abcd(a), Done(&b"ef"[..], Some(&b"abcd"[..])));
assert_eq!(opt_abcd(b), Done(&b"bcdefg"[..], None));
assert_eq!(opt_abcd(c), Incomplete(Needed::Size(4)));
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))));
}
#[cfg(feature = "verbose-errors")]
@ -1259,9 +1258,9 @@ mod tests {
let a = &b"abcdef"[..];
let b = &b"bcdefg"[..];
let c = &b"ab"[..];
assert_eq!(opt_res_abcd(a), Done(&b"ef"[..], Ok(&b"abcd"[..])));
assert_eq!(opt_res_abcd(b), Done(&b"bcdefg"[..], Err(error_position!(ErrorKind::Tag, b))));
assert_eq!(opt_res_abcd(c), Incomplete(Needed::Size(4)));
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!(ErrorKind::Tag, b))))));
assert_eq!(opt_res_abcd(c), Err(Err::Incomplete(Needed::Size(4))));
}
#[cfg(not(feature = "verbose-errors"))]
@ -1272,9 +1271,9 @@ mod tests {
let a = &b"abcdef"[..];
let b = &b"bcdefg"[..];
let c = &b"ab"[..];
assert_eq!(opt_res_abcd(a), Done(&b"ef"[..], Ok(&b"abcd"[..])));
assert_eq!(opt_res_abcd(b), Done(&b"bcdefg"[..], Err(error_position!(ErrorKind::Tag, b))));
assert_eq!(opt_res_abcd(c), Incomplete(Needed::Size(4)));
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!(ErrorKind::Tag, b))))));
assert_eq!(opt_res_abcd(c), Err(Err::Incomplete(Needed::Size(4))));
}
#[test]
@ -1286,13 +1285,13 @@ mod tests {
Box::new(closure!(&'static [u8], fix_error!(&str, cond!( false, tag!("abcd") ) )));
//let f_false = closure!(&'static [u8], cond!( false, tag!("abcd") ) );
assert_eq!(f_true(&b"abcdef"[..]), Done(&b"ef"[..], Some(&b"abcd"[..])));
assert_eq!(f_true(&b"ab"[..]), Incomplete(Needed::Size(4)));
assert_eq!(f_true(&b"xxx"[..]), Done(&b"xxx"[..], None));
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"[..]), Ok((&b"xxx"[..], None)));
assert_eq!(f_false(&b"abcdef"[..]), Done(&b"abcdef"[..], None));
assert_eq!(f_false(&b"ab"[..]), Done(&b"ab"[..], None));
assert_eq!(f_false(&b"xxx"[..]), Done(&b"xxx"[..], None));
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]
@ -1306,37 +1305,37 @@ mod tests {
Box::new(closure!(&'static [u8], fix_error!(&str, cond!( false, tag_abcd ) )));
//let f_false = closure!(&'static [u8], cond!( b2, tag!("abcd") ) );
assert_eq!(f_true(&b"abcdef"[..]), Done(&b"ef"[..], Some(&b"abcd"[..])));
assert_eq!(f_true(&b"ab"[..]), Incomplete(Needed::Size(4)));
assert_eq!(f_true(&b"xxx"[..]), Done(&b"xxx"[..], None));
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"[..]), Ok((&b"xxx"[..], None)));
assert_eq!(f_false(&b"abcdef"[..]), Done(&b"abcdef"[..], None));
assert_eq!(f_false(&b"ab"[..]), Done(&b"ab"[..], None));
assert_eq!(f_false(&b"xxx"[..]), Done(&b"xxx"[..], None));
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"[..]), Done(&b"abcdef"[..], &b"abcd"[..]));
assert_eq!(peek_tag(&b"ab"[..]), Incomplete(Needed::Size(4)));
assert_eq!(peek_tag(&b"xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..])));
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!(ErrorKind::Tag, &b"xxx"[..]))));
}
#[test]
fn not() {
named!(not_aaa, not!(tag!("aaa")));
assert_eq!(not_aaa(&b"aaa"[..]), Error(error_position!(ErrorKind::Not, &b"aaa"[..])));
assert_eq!(not_aaa(&b"aa"[..]), Done(&b"aa"[..], &b""[..]));
assert_eq!(not_aaa(&b"abcd"[..]), Done(&b"abcd"[..], &b""[..]));
assert_eq!(not_aaa(&b"aaa"[..]), Err(Err::Error(error_position!(ErrorKind::Not, &b"aaa"[..]))));
assert_eq!(not_aaa(&b"aa"[..]), Ok((&b"aa"[..], &b""[..])));
assert_eq!(not_aaa(&b"abcd"[..]), Ok((&b"abcd"[..], &b""[..])));
}
#[test]
fn verify() {
named!(test, verify!(take!(5), |slice: &[u8]| slice[0] == 'a' as u8));
assert_eq!(test(&b"bcd"[..]), Incomplete(Needed::Size(5)));
assert_eq!(test(&b"bcdefg"[..]), Error(error_position!(ErrorKind::Verify, &b"bcdefg"[..])));
assert_eq!(test(&b"abcdefg"[..]), Done(&b"fg"[..], &b"abcde"[..]));
assert_eq!(test(&b"bcd"[..]), Err(Err::Incomplete(Needed::Size(5))));
assert_eq!(test(&b"bcdefg"[..]), Err(Err::Error(error_position!(ErrorKind::Verify, &b"bcdefg"[..]))));
assert_eq!(test(&b"abcdefg"[..]), Ok((&b"fg"[..], &b"abcde"[..])));
}
}

View File

@ -282,19 +282,19 @@ macro_rules! apply_m (
#[cfg(test)]
mod tests {
use internal::IResult::*;
use simple_errors::Err;
// reproduce the tag_s and take_s macros, because of module import order
macro_rules! tag_s (
($i:expr, $tag: expr) => (
{
let res: $crate::IResult<_,_> = if $tag.len() > $i.len() {
$crate::IResult::Incomplete($crate::Needed::Size($tag.len()))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($tag.len())))
//} else if &$i[0..$tag.len()] == $tag {
} else if ($i).starts_with($tag) {
$crate::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()])
::std::result::Result::Ok((&$i[$tag.len()..], &$i[0..$tag.len()]))
} else {
$crate::IResult::Error(error_position!($crate::ErrorKind::TagStr, $i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::TagStr, $i)))
};
res
}
@ -306,7 +306,7 @@ mod tests {
{
let cnt = $count as usize;
let res: $crate::IResult<_,_> = if $i.chars().count() < cnt {
$crate::IResult::Incomplete($crate::Needed::Size(cnt))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(cnt)))
} else {
let mut offset = $i.len();
let mut count = 0;
@ -317,7 +317,7 @@ mod tests {
}
count += 1;
}
$crate::IResult::Done(&$i[offset..], &$i[..offset])
::std::result::Result::Ok((&$i[offset..], &$i[..offset]))
};
res
}
@ -368,7 +368,7 @@ mod tests {
let leftover: &str = "δèƒϱλïJƙ";
let(_, res) = p.tag_abc(input);
match res {
Done(extra, output) => { assert!(extra == leftover, "`Parser.tag_abc` consumed leftover input. leftover: {}", extra);
Ok((extra, output)) => { assert!(extra == leftover, "`Parser.tag_abc` consumed leftover input. leftover: {}", extra);
assert!(output == consumed, "`Parser.tag_abc` doesnt return the string it consumed \
on success. Expected `{}`, got `{}`.", consumed, output);
},
@ -385,7 +385,7 @@ mod tests {
let leftover: &str = "èƒϱλïJƙ";
let(_, res) = p.tag_bcd(input);
match res {
Done(extra, output) => { assert!(extra == leftover, "`Parser.tag_bcd` consumed leftover input. leftover: {}", extra);
Ok((extra, output)) => { assert!(extra == leftover, "`Parser.tag_bcd` consumed leftover input. leftover: {}", extra);
assert!(output == consumed, "`Parser.tag_bcd` doesn't return the string it consumed \
on success. Expected `{}`, got `{}`.", consumed, output);
},
@ -402,7 +402,7 @@ mod tests {
let leftover: &str = "ƙ₥ñôƥ9řƨ";
let(_, res) = p.tag_hij(input);
match res {
Done(extra, output) => { assert!(extra == leftover, "`Parser.tag_hij` consumed leftover input. leftover: {}", extra);
Ok((extra, output)) => { assert!(extra == leftover, "`Parser.tag_hij` consumed leftover input. leftover: {}", extra);
assert!(output == consumed, "`Parser.tag_hij` doesn't return the string it consumed \
on success. Expected `{}`, got `{}`.", consumed, output);
},
@ -419,7 +419,7 @@ mod tests {
let leftover: &str = "₥ñôƥ9řƨ";
let(_, res) = p.tag_ijk(input);
match res {
Done(extra, output) => { assert!(extra == leftover, "`Parser.tag_ijk` consumed leftover input. leftover: {}", extra);
Ok((extra, output)) => { assert!(extra == leftover, "`Parser.tag_ijk` consumed leftover input. leftover: {}", extra);
assert!(output == consumed, "`Parser.tag_ijk` doesn't return the string it consumed \
on success. Expected `{}`, got `{}`.", consumed, output);
},
@ -435,7 +435,7 @@ mod tests {
let leftover: &str = "δèƒϱλïJƙ";
let(_, res) = p.simple_call(input);
match res {
Done(extra, output) => { assert!(extra == leftover, "`Parser.simple_call` consumed leftover input. leftover: {}", extra);
Ok((extra, output)) => { assert!(extra == leftover, "`Parser.simple_call` consumed leftover input. leftover: {}", extra);
assert!(output == consumed, "`Parser.simple_call` doesn't return the string it consumed \
on success. Expected `{}`, got `{}`.", consumed, output);
},
@ -453,7 +453,7 @@ mod tests {
let(tmp, res) = p.use_apply(input);
p = tmp;
match res {
Done(extra, output) => { assert!(extra == leftover, "`Parser.use_apply` consumed leftover input. leftover: {}", extra);
Ok((extra, output)) => { assert!(extra == leftover, "`Parser.use_apply` consumed leftover input. leftover: {}", extra);
assert!(output == consumed, "`Parser.use_apply` doesn't return the string it was supposed to \
on success. Expected `{}`, got `{}`.", leftover, output);
assert!(p.bcd == "βçδ", "Parser.use_apply didn't modify the parser field correctly: {}", p.bcd);
@ -470,7 +470,7 @@ mod tests {
let consumed: &str = "ж¥ƺ";
let(_, res) = p.simple_peek(input);
match res {
Done(extra, output) => { assert!(extra == input, "`Parser.simple_peek` consumed leftover input. leftover: {}", extra);
Ok((extra, output)) => { assert!(extra == input, "`Parser.simple_peek` consumed leftover input. leftover: {}", extra);
assert!(output == consumed, "`Parser.simple_peek` doesn't return the string it consumed \
on success. Expected `{}`, got `{}`.", consumed, output);
},
@ -488,7 +488,7 @@ mod tests {
let(tmp, res) = p.simple_chain(input);
p = tmp;
match res {
Done(extra, out) => { assert!(extra == leftover, "`Parser.simple_chain` consumed leftover input. leftover: {}", extra);
Ok((extra, out)) => { assert!(extra == leftover, "`Parser.simple_chain` consumed leftover input. leftover: {}", extra);
assert!(out == output, "`Parser.simple_chain` doesn't return the string it was supposed to \
on success. Expected `{}`, got `{}`.", output, out);
assert!(p.bcd == "βçδ", "Parser.simple_chain didn't modify the parser field correctly: {}", p.bcd);

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -31,9 +31,9 @@ macro_rules! re_match (
use $crate::Slice;
let re = ::regex::Regex::new($re).unwrap();
if re.is_match($i) {
$crate::IResult::Done($i.slice($i.input_len()..), $i)
::std::result::Result::Ok(($i.slice($i.input_len()..), $i))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatch));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpMatch)));
res
}
}
@ -53,9 +53,9 @@ macro_rules! re_match_static (
use $crate::Slice;
regex!(RE, $re);
if RE.is_match($i) {
$crate::IResult::Done($i.slice($i.input_len()..), $i)
::std::result::Result::Ok(($i.slice($i.input_len()..), $i))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatch));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpMatch)));
res
}
}
@ -74,9 +74,9 @@ macro_rules! re_bytes_match (
use $crate::Slice;
let re = ::regex::bytes::Regex::new($re).unwrap();
if re.is_match($i) {
$crate::IResult::Done($i.slice($i.input_len()..), $i)
::std::result::Result::Ok(($i.slice($i.input_len()..), $i))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatch));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpMatch)));
res
}
}
@ -96,9 +96,9 @@ macro_rules! re_bytes_match_static (
use $crate::Slice;
regex_bytes!(RE, $re);
if RE.is_match($i) {
$crate::IResult::Done($i.slice($i.input_len()..), $i)
::std::result::Result::Ok(($i.slice($i.input_len()..), $i))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatch));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpMatch)));
res
}
}
@ -116,9 +116,9 @@ macro_rules! re_find (
use $crate::Slice;
let re = ::regex::Regex::new($re).unwrap();
if let Some(m) = re.find($i) {
$crate::IResult::Done($i.slice(m.end()..), $i.slice(m.start()..m.end()))
::std::result::Result::Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end())))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpFind));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpFind)));
res
}
}
@ -137,9 +137,9 @@ macro_rules! re_find_static (
use $crate::Slice;
regex!(RE, $re);
if let Some(m) = RE.find($i) {
$crate::IResult::Done($i.slice(m.end()..), $i.slice(m.start()..m.end()))
::std::result::Result::Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end())))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpFind));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpFind)));
res
}
}
@ -158,9 +158,9 @@ macro_rules! re_bytes_find (
use $crate::Slice;
let re = ::regex::bytes::Regex::new($re).unwrap();
if let Some(m) = re.find($i) {
$crate::IResult::Done($i.slice(m.end()..), $i.slice(m.start()..m.end()))
::std::result::Result::Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end())))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpFind));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpFind)));
res
}
}
@ -179,9 +179,9 @@ macro_rules! re_bytes_find_static (
use $crate::Slice;
regex_bytes!(RE, $re);
if let Some(m) = RE.find($i) {
$crate::IResult::Done($i.slice(m.end()..), $i.slice(m.start()..m.end()))
::std::result::Result::Ok(($i.slice(m.end()..), $i.slice(m.start()..m.end())))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpFind));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpFind)));
res
}
}
@ -205,9 +205,9 @@ macro_rules! re_matches (
let end = v.last().unwrap();
end.as_ptr() as usize + end.len() - $i.as_ptr() as usize
};
$crate::IResult::Done($i.slice(offset..), v)
::std::result::Result::Ok(($i.slice(offset..), v))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatches));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpMatches)));
res
}
}
@ -231,9 +231,9 @@ macro_rules! re_matches_static (
let end = v.last().unwrap();
end.as_ptr() as usize + end.len() - $i.as_ptr() as usize
};
$crate::IResult::Done($i.slice(offset..), v)
::std::result::Result::Ok(($i.slice(offset..), v))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatches));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpMatches)));
res
}
}
@ -256,9 +256,9 @@ macro_rules! re_bytes_matches (
let end = v.last().unwrap();
end.as_ptr() as usize + end.len() - $i.as_ptr() as usize
};
$crate::IResult::Done($i.slice(offset..), v)
::std::result::Result::Ok(($i.slice(offset..), v))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatches));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpMatches)));
res
}
}
@ -282,9 +282,9 @@ macro_rules! re_bytes_matches_static (
let end = v.last().unwrap();
end.as_ptr() as usize + end.len() - $i.as_ptr() as usize
};
$crate::IResult::Done($i.slice(offset..), v)
::std::result::Result::Ok(($i.slice(offset..), v))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpMatches));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpMatches)));
res
}
}
@ -307,9 +307,9 @@ macro_rules! re_capture (
let end = v.last().unwrap();
end.as_ptr() as usize + end.len() - $i.as_ptr() as usize
};
$crate::IResult::Done($i.slice(offset..), v)
::std::result::Result::Ok(($i.slice(offset..), v))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpCapture)));
res
}
}
@ -333,9 +333,9 @@ macro_rules! re_capture_static (
let end = v.last().unwrap();
end.as_ptr() as usize + end.len() - $i.as_ptr() as usize
};
$crate::IResult::Done($i.slice(offset..), v)
::std::result::Result::Ok(($i.slice(offset..), v))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpCapture)));
res
}
}
@ -358,9 +358,9 @@ macro_rules! re_bytes_capture (
let end = v.last().unwrap();
end.as_ptr() as usize + end.len() - $i.as_ptr() as usize
};
$crate::IResult::Done($i.slice(offset..), v)
::std::result::Result::Ok(($i.slice(offset..), v))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpCapture)));
res
}
}
@ -384,9 +384,9 @@ macro_rules! re_bytes_capture_static (
let end = v.last().unwrap();
end.as_ptr() as usize + end.len() - $i.as_ptr() as usize
};
$crate::IResult::Done($i.slice(offset..), v)
::std::result::Result::Ok(($i.slice(offset..), v))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpCapture)));
res
}
}
@ -409,9 +409,9 @@ macro_rules! re_captures (
let end = v.last().unwrap().last().unwrap();
end.as_ptr() as usize + end.len() - $i.as_ptr() as usize
};
$crate::IResult::Done($i.slice(offset..), v)
::std::result::Result::Ok(($i.slice(offset..), v))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpCapture)));
res
}
}
@ -435,9 +435,9 @@ macro_rules! re_captures_static (
let end = v.last().unwrap().last().unwrap();
end.as_ptr() as usize + end.len() - $i.as_ptr() as usize
};
$crate::IResult::Done($i.slice(offset..), v)
::std::result::Result::Ok(($i.slice(offset..), v))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpCapture)));
res
}
}
@ -460,9 +460,9 @@ macro_rules! re_bytes_captures (
let end = v.last().unwrap().last().unwrap();
end.as_ptr() as usize + end.len() - $i.as_ptr() as usize
};
$crate::IResult::Done($i.slice(offset..), v)
::std::result::Result::Ok(($i.slice(offset..), v))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpCapture)));
res
}
}
@ -486,9 +486,9 @@ macro_rules! re_bytes_captures_static (
let end = v.last().unwrap().last().unwrap();
end.as_ptr() as usize + end.len() - $i.as_ptr() as usize
};
$crate::IResult::Done($i.slice(offset..), v)
::std::result::Result::Ok(($i.slice(offset..), v))
} else {
let res: $crate::IResult<_,_> = $crate::IResult::Error(error_code!($crate::ErrorKind::RegexpCapture));
let res: $crate::IResult<_,_> = ::std::result::Result::Err($crate::Err::Error(error_code!($crate::ErrorKind::RegexpCapture)));
res
}
}
@ -502,76 +502,76 @@ mod tests {
#[test]
fn re_match() {
named!(rm<&str,&str>, re_match!(r"^\d{4}-\d{2}-\d{2}"));
assert_eq!(rm("2015-09-07"), Done("", "2015-09-07"));
assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpMatch)));
assert_eq!(rm("2015-09-07blah"), Done("", "2015-09-07blah"));
assert_eq!(rm("2015-09-07"),Ok(("", "2015-09-07")));
assert_eq!(rm("blah"), Err(Err::Error(error_code!(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"), Done("", "2015-09-07"));
assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpMatch)));
assert_eq!(rm("2015-09-07blah"), Done("", "2015-09-07blah"));
assert_eq!(rm("2015-09-07"),Ok(("", "2015-09-07")));
assert_eq!(rm("blah"), Err(Err::Error(error_code!(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"), Done("", "2015-09-07"));
assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpFind)));
assert_eq!(rm("2015-09-07blah"), Done("blah", "2015-09-07"));
assert_eq!(rm("2015-09-07"),Ok(("", "2015-09-07")));
assert_eq!(rm("blah"), Err(Err::Error(error_code!(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"), Done("", "2015-09-07"));
assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpFind)));
assert_eq!(rm("2015-09-07blah"), Done("blah", "2015-09-07"));
assert_eq!(rm("2015-09-07"),Ok(("", "2015-09-07")));
assert_eq!(rm("blah"), Err(Err::Error(error_code!(ErrorKind::RegexpFind))));
assert_eq!(rm("2015-09-07blah"),Ok(("blah", "2015-09-07")));
}
#[test]
fn re_matches() {
named!(rm< &str,Vec<&str> >, re_matches!(r"\d{4}-\d{2}-\d{2}"));
assert_eq!(rm("2015-09-07"), Done("", vec!["2015-09-07"]));
assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpMatches)));
assert_eq!(rm("aaa2015-09-07blah2015-09-09pouet"), Done("pouet", vec!["2015-09-07", "2015-09-09"]));
assert_eq!(rm("2015-09-07"),Ok(("", vec!["2015-09-07"])));
assert_eq!(rm("blah"), Err(Err::Error(error_code!(ErrorKind::RegexpMatches))));
assert_eq!(rm("aaa2015-09-07blah2015-09-09pouet"),Ok(("pouet", vec!["2015-09-07", "2015-09-09"])));
}
#[cfg(feature = "regexp_macros")]
#[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"), Done("", vec!["2015-09-07"]));
assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpMatches)));
assert_eq!(rm("aaa2015-09-07blah2015-09-09pouet"), Done("pouet", vec!["2015-09-07", "2015-09-09"]));
assert_eq!(rm("2015-09-07"),Ok(("", vec!["2015-09-07"])));
assert_eq!(rm("blah"), Err(Err::Error(error_code!(ErrorKind::RegexpMatches))));
assert_eq!(rm("aaa2015-09-07blah2015-09-09pouet"),Ok(("pouet", vec!["2015-09-07", "2015-09-09"])));
}
#[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"), Done("pouet", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]));
assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpCapture)));
assert_eq!(rm("hello nom 0.3.11 world regex 0.1.41"), Done(" world regex 0.1.41", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]));
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_code!(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 = "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"), Done("pouet", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]));
assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpCapture)));
assert_eq!(rm("hello nom 0.3.11 world regex 0.1.41"), Done(" world regex 0.1.41", vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]));
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_code!(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"])));
}
#[test]
fn re_captures() {
named!(rm< &str,Vec<Vec<&str>> >, re_captures!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))"));
assert_eq!(rm("blah nom 0.3.11pouet"), Done("pouet", vec![vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]]));
assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpCapture)));
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_code!(ErrorKind::RegexpCapture))));
assert_eq!(rm("hello nom 0.3.11 world regex 0.1.41 aaa"), Done(" 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"],
@ -582,8 +582,8 @@ mod tests {
#[test]
fn re_captures_static() {
named!(rm< &str,Vec<Vec<&str>> >, re_captures_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))"));
assert_eq!(rm("blah nom 0.3.11pouet"), Done("pouet", vec![vec!["nom 0.3.11", "nom", "0.3.11", "0", "3", "11"]]));
assert_eq!(rm("blah"), Error(error_code!(ErrorKind::RegexpCapture)));
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_code!(ErrorKind::RegexpCapture))));
assert_eq!(rm("hello nom 0.3.11 world regex 0.1.41 aaa"), Done(" 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"],
@ -593,76 +593,76 @@ mod tests {
#[test]
fn re_bytes_match() {
named!(rm, re_bytes_match!(r"^\d{4}-\d{2}-\d{2}"));
assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], &b"2015-09-07"[..]));
assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpMatch)));
assert_eq!(rm(&b"2015-09-07blah"[..]), Done(&b""[..], &b"2015-09-07blah"[..]));
assert_eq!(rm(&b"2015-09-07"[..]),Ok((&b""[..], &b"2015-09-07"[..])));
assert_eq!(rm(&b"blah"[..]), Err(Err::Error(error_code!(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"[..]), Done(&b""[..], &b"2015-09-07"[..]));
assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpMatch)));
assert_eq!(rm(&b"2015-09-07blah"[..]), Done(&b""[..], &b"2015-09-07blah"[..]));
assert_eq!(rm(&b"2015-09-07"[..]),Ok((&b""[..], &b"2015-09-07"[..])));
assert_eq!(rm(&b"blah"[..]), Err(Err::Error(error_code!(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"[..]), Done(&b""[..], &b"2015-09-07"[..]));
assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpFind)));
assert_eq!(rm(&b"2015-09-07blah"[..]), Done(&b"blah"[..], &b"2015-09-07"[..]));
assert_eq!(rm(&b"2015-09-07"[..]),Ok((&b""[..], &b"2015-09-07"[..])));
assert_eq!(rm(&b"blah"[..]), Err(Err::Error(error_code!(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"[..]), Done(&b""[..], &b"2015-09-07"[..]));
assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpFind)));
assert_eq!(rm(&b"2015-09-07blah"[..]), Done(&b"blah"[..], &b"2015-09-07"[..]));
assert_eq!(rm(&b"2015-09-07"[..]),Ok((&b""[..], &b"2015-09-07"[..])));
assert_eq!(rm(&b"blah"[..]), Err(Err::Error(error_code!(ErrorKind::RegexpFind))));
assert_eq!(rm(&b"2015-09-07blah"[..]),Ok((&b"blah"[..], &b"2015-09-07"[..])));
}
#[test]
fn re_bytes_matches() {
named!(rm<Vec<&[u8]> >, re_bytes_matches!(r"\d{4}-\d{2}-\d{2}"));
assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], vec![&b"2015-09-07"[..]]));
assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpMatches)));
assert_eq!(rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]), Done(&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]]));
assert_eq!(rm(&b"2015-09-07"[..]),Ok((&b""[..], vec![&b"2015-09-07"[..]])));
assert_eq!(rm(&b"blah"[..]), Err(Err::Error(error_code!(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 = "regexp_macros")]
#[test]
fn re_bytes_matches_static() {
named!(rm<Vec<&[u8]> >, re_bytes_matches_static!(r"\d{4}-\d{2}-\d{2}"));
assert_eq!(rm(&b"2015-09-07"[..]), Done(&b""[..], vec![&b"2015-09-07"[..]]));
assert_eq!(rm(&b"blah"[..]), Error(error_code!(ErrorKind::RegexpMatches)));
assert_eq!(rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]), Done(&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]]));
assert_eq!(rm(&b"2015-09-07"[..]),Ok((&b""[..], vec![&b"2015-09-07"[..]])));
assert_eq!(rm(&b"blah"[..]), Err(Err::Error(error_code!(ErrorKind::RegexpMatches))));
assert_eq!(rm(&b"aaa2015-09-07blah2015-09-09pouet"[..]),Ok((&b"pouet"[..], vec![&b"2015-09-07"[..], &b"2015-09-09"[..]])));
}
#[test]
fn re_bytes_capture() {
named!(rm<Vec<&[u8]> >, re_bytes_capture!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))"));
assert_eq!(rm(&b"blah nom 0.3.11pouet"[..]), Done(&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"[..]), Error(error_code!(ErrorKind::RegexpCapture)));
assert_eq!(rm(&b"hello nom 0.3.11 world regex 0.1.41"[..]), Done(&b" world regex 0.1.41"[..], vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]]));
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_code!(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 = "regexp_macros")]
#[test]
fn re_bytes_capture_static() {
named!(rm< Vec<&[u8]> >, re_bytes_capture_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))"));
assert_eq!(rm(&b"blah nom 0.3.11pouet"[..]), Done(&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"[..]), Error(error_code!(ErrorKind::RegexpCapture)));
assert_eq!(rm(&b"hello nom 0.3.11 world regex 0.1.41"[..]), Done(&b" world regex 0.1.41"[..], vec![&b"nom 0.3.11"[..], &b"nom"[..], &b"0.3.11"[..], &b"0"[..], &b"3"[..], &b"11"[..]]));
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_code!(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"[..]])));
}
#[test]
fn re_bytes_captures() {
named!(rm< Vec<Vec<&[u8]>> >, re_bytes_captures!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))"));
assert_eq!(rm(&b"blah nom 0.3.11pouet"[..]), Done(&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"[..]), Error(error_code!(ErrorKind::RegexpCapture)));
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_code!(ErrorKind::RegexpCapture))));
assert_eq!(rm(&b"hello nom 0.3.11 world regex 0.1.41 aaa"[..]), Done(&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"[..]],
@ -673,8 +673,8 @@ mod tests {
#[test]
fn re_bytes_captures_static() {
named!(rm< Vec<Vec<&[u8]>> >, re_bytes_captures_static!(r"([[:alpha:]]+)\s+((\d+).(\d+).(\d+))"));
assert_eq!(rm(&b"blah nom 0.3.11pouet"[..]), Done(&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"[..]), Error(error_code!(ErrorKind::RegexpCapture)));
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_code!(ErrorKind::RegexpCapture))));
assert_eq!(rm(&b"hello nom 0.3.11 world regex 0.1.41 aaa"[..]), Done(&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"[..]],

View File

@ -52,18 +52,18 @@ macro_rules! tuple_parser (
{
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Error(e) =>
$crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) =>
$crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => {
::std::result::Result::Err($crate::Err::Error(e)) =>
::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => {
let (needed,overflowed) = $consumed.overflowing_add(i);
match overflowed {
true => $crate::IResult::Incomplete($crate::Needed::Unknown),
false => $crate::IResult::Incomplete($crate::Needed::Size(needed)),
true => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
false => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(needed))),
}
},
$crate::IResult::Done(i,o) => {
::std::result::Result::Ok((i,o)) => {
let i_ = i.clone();
tuple_parser!(i_,
$consumed + ($crate::InputLength::input_len(&($i)) -
@ -76,18 +76,18 @@ macro_rules! tuple_parser (
{
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Error(e) =>
$crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) =>
$crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => {
::std::result::Result::Err($crate::Err::Error(e)) =>
::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => {
let (needed,overflowed) = $consumed.overflowing_add(i);
match overflowed {
true => $crate::IResult::Incomplete($crate::Needed::Unknown),
false => $crate::IResult::Incomplete($crate::Needed::Size(needed)),
true => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
false => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(needed))),
}
},
$crate::IResult::Done(i,o) => {
::std::result::Result::Ok((i,o)) => {
let i_ = i.clone();
tuple_parser!(i_,
$consumed + ($crate::InputLength::input_len(&($i)) -
@ -103,19 +103,19 @@ macro_rules! tuple_parser (
{
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Error(e) =>
$crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) =>
$crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => {
::std::result::Result::Err($crate::Err::Error(e)) =>
::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => {
let (needed,overflowed) = $consumed.overflowing_add(i);
match overflowed {
true => $crate::IResult::Incomplete($crate::Needed::Unknown),
false => $crate::IResult::Incomplete($crate::Needed::Size(needed)),
true => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
false => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(needed))),
}
},
$crate::IResult::Done(i,o) => {
$crate::IResult::Done(i, (o))
::std::result::Result::Ok((i,o)) => {
::std::result::Result::Ok((i, (o)))
}
}
}
@ -123,26 +123,26 @@ macro_rules! tuple_parser (
($i:expr, $consumed:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => (
{
match $submac!($i, $($args)*) {
$crate::IResult::Error(e) =>
$crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) =>
$crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => {
::std::result::Result::Err($crate::Err::Error(e)) =>
::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => {
let (needed,overflowed) = $consumed.overflowing_add(i);
match overflowed {
true => $crate::IResult::Incomplete($crate::Needed::Unknown),
false => $crate::IResult::Incomplete($crate::Needed::Size(needed)),
true => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
false => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(needed))),
}
},
$crate::IResult::Done(i,o) => {
$crate::IResult::Done(i, ($($parsed),* , o))
::std::result::Result::Ok((i,o)) => {
::std::result::Result::Ok((i, ($($parsed),* , o)))
}
}
}
);
($i:expr, $consumed:expr, ($($parsed:expr),*)) => (
{
$crate::IResult::Done($i, ($($parsed),*))
::std::result::Result::Ok(($i, ($($parsed),*)))
}
);
);
@ -178,10 +178,10 @@ macro_rules! separated_pair(
($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => (
{
match tuple_parser!($i, 0usize, (), $submac!($($args)*), $($rest)*) {
$crate::IResult::Error(a) => $crate::IResult::Error(a),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Done(i1, (o1, _, o2)) => {
$crate::IResult::Done(i1, (o1, o2))
::std::result::Result::Err($crate::Err::Error(a)) => ::std::result::Result::Err($crate::Err::Error(a)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Ok((i1, (o1, _, o2))) => {
::std::result::Result::Ok((i1, (o1, o2)))
}
}
}
@ -199,10 +199,10 @@ macro_rules! preceded(
($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => (
{
match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
$crate::IResult::Error(a) => $crate::IResult::Error(a),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Done(remaining, (_,o)) => {
$crate::IResult::Done(remaining, o)
::std::result::Result::Err($crate::Err::Error(a)) => ::std::result::Result::Err($crate::Err::Error(a)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Ok((remaining, (_,o))) => {
::std::result::Result::Ok((remaining, o))
}
}
}
@ -228,10 +228,10 @@ macro_rules! terminated(
($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => (
{
match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
$crate::IResult::Error(a) => $crate::IResult::Error(a),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Done(remaining, (o,_)) => {
$crate::IResult::Done(remaining, o)
::std::result::Result::Err($crate::Err::Error(a)) => ::std::result::Result::Err($crate::Err::Error(a)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Ok((remaining, (o,_))) => {
::std::result::Result::Ok((remaining, o))
}
}
}
@ -266,7 +266,7 @@ macro_rules! terminated(
///
/// # fn main() {
/// let input = &b"(test)"[..];
/// assert_eq!(bracketed(input), Done(&b""[..], &b"test"[..]));
/// assert_eq!(bracketed(input), Ok((&b""[..], &b"test"[..])));
/// # }
/// ```
#[macro_export]
@ -274,10 +274,10 @@ macro_rules! delimited(
($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => (
{
match tuple_parser!($i, 0usize, (), $submac!($($args)*), $($rest)*) {
$crate::IResult::Error(a) => $crate::IResult::Error(a),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Done(i1, (_, o, _)) => {
$crate::IResult::Done(i1, o)
::std::result::Result::Err($crate::Err::Error(a)) => ::std::result::Result::Err($crate::Err::Error(a)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Ok((i1, (_, o, _))) => {
::std::result::Result::Ok((i1, o))
}
}
}
@ -326,7 +326,7 @@ macro_rules! delimited(
/// let a: Vec<u8> = vec!(42, 2, 3, 4, 5);
/// let result_a: Vec<u8> = vec!(3, 4);
/// let rest_a: Vec<u8> = vec!(5);
/// assert_eq!(tag_length_value(&a[..]), Done(&rest_a[..], &result_a[..]));
/// 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,
@ -361,7 +361,7 @@ macro_rules! delimited(
#[macro_export]
macro_rules! do_parse (
(__impl $i:expr, $consumed:expr, ( $($rest:expr),* )) => (
$crate::IResult::Done($i, ( $($rest),* ))
::std::result::Result::Ok(($i, ( $($rest),* )))
);
(__impl $i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) ) => (
@ -400,17 +400,17 @@ macro_rules! do_parse (
{
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) =>
$crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => {
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => {
let (needed,overflowed) = $consumed.overflowing_add(i);
match overflowed {
true => $crate::IResult::Incomplete($crate::Needed::Unknown),
false => $crate::IResult::Incomplete($crate::Needed::Size(needed)),
true => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
false => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(needed))),
}
},
$crate::IResult::Done(i,_) => {
::std::result::Result::Ok((i,_)) => {
let i_ = i.clone();
do_parse!(__impl i_,
$consumed + ($crate::InputLength::input_len(&($i)) -
@ -428,17 +428,17 @@ macro_rules! do_parse (
{
let i_ = $i.clone();
match $submac!(i_, $($args)*) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) =>
$crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => {
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => {
let (needed,overflowed) = $consumed.overflowing_add(i);
match overflowed {
true => $crate::IResult::Incomplete($crate::Needed::Unknown),
false => $crate::IResult::Incomplete($crate::Needed::Size(needed)),
true => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
false => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(needed))),
}
},
$crate::IResult::Done(i,o) => {
::std::result::Result::Ok((i,o)) => {
let $field = o;
let i_ = i.clone();
do_parse!(__impl i_,
@ -456,18 +456,18 @@ macro_rules! do_parse (
(__impl $i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => (
match $submac!($i, $($args)*) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) =>
$crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => {
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => {
let (needed,overflowed) = $consumed.overflowing_add(i);
match overflowed {
true => $crate::IResult::Incomplete($crate::Needed::Unknown),
false => $crate::IResult::Incomplete($crate::Needed::Size(needed)),
true => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
false => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(needed))),
}
},
$crate::IResult::Done(i,_) => {
$crate::IResult::Done(i, ( $($rest)* ))
::std::result::Result::Ok((i,_)) => {
::std::result::Result::Ok((i, ( $($rest)* )))
},
}
);
@ -478,19 +478,19 @@ macro_rules! do_parse (
(__impl $i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => (
match $submac!($i, $($args)*) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) =>
$crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => {
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => {
let (needed,overflowed) = $consumed.overflowing_add(i);
match overflowed {
true => $crate::IResult::Incomplete($crate::Needed::Unknown),
false => $crate::IResult::Incomplete($crate::Needed::Size(needed)),
true => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
false => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(needed))),
}
},
$crate::IResult::Done(i,o) => {
::std::result::Result::Ok((i,o)) => {
let $field = o;
$crate::IResult::Done(i, ( $($rest)* ))
::std::result::Result::Ok((i, ( $($rest)* )))
},
}
);
@ -518,10 +518,11 @@ macro_rules! do_parse (
#[cfg(test)]
mod tests {
use internal::{Needed,IResult};
use internal::IResult::*;
use util::ErrorKind;
use nom::be_u16;
use simple_errors::Err;
#[cfg(feature = "verbose-errors")]
use verbose_errors::Err;
@ -553,11 +554,11 @@ mod tests {
let b = &$bytes[..m];
let res: $crate::IResult<_,_> = if reduced != b {
$crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Tag, $i)))
} else if m < blen {
$crate::IResult::Incomplete($crate::Needed::Size(blen))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(blen)))
} else {
$crate::IResult::Done(&$i[blen..], reduced)
::std::result::Result::Ok((&$i[blen..], reduced))
};
res
}
@ -569,9 +570,9 @@ mod tests {
{
let cnt = $count as usize;
let res:$crate::IResult<&[u8],&[u8]> = if $i.len() < cnt {
$crate::IResult::Incomplete($crate::Needed::Size(cnt))
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(cnt)))
} else {
$crate::IResult::Done(&$i[cnt..],&$i[0..cnt])
::std::result::Result::Ok((&$i[cnt..],&$i[0..cnt]))
};
res
}
@ -655,7 +656,7 @@ mod tests {
let res_c = err_test(c);
assert_eq!(res_a, Error(error_node_position!(ErrorKind::Custom(42), blah, error_position!(ErrorKind::Tag, blah))));
assert_eq!(res_b, Error(error_node_position!(ErrorKind::Custom(42), &b"ijklblah"[..], error_node_position!(ErrorKind::Custom(128), blah, error_position!(ErrorKind::Tag, blah)))));
assert_eq!(res_c, Done(&b""[..], &b"mnop"[..]));
assert_eq!(res_c, Ok((&b""[..], &b"mnop"[..])));
// Merr-like error matching
let mut err_map = collections::HashMap::new();
@ -707,9 +708,9 @@ mod tests {
let res_a = err_test(a);
let res_b = err_test(b);
let res_c = err_test(c);
assert_eq!(res_a, Error(error_node_position!(ErrorKind::Custom(42), blah, error_position!(ErrorKind::Tag, blah))));
assert_eq!(res_b, Error(error_node_position!(ErrorKind::Custom(42), &b"ijklblah"[..], error_node_position!(ErrorKind::Custom(128), blah, error_position!(ErrorKind::Tag, blah)))));
assert_eq!(res_c, Done(&b""[..], &b"mnop"[..]));
assert_eq!(res_a, Err(Err::Error(error_node_position!(ErrorKind::Custom(42), blah, error_position!(ErrorKind::Tag, blah)))));
assert_eq!(res_b, Err(Err::Error(error_node_position!(ErrorKind::Custom(42), &b"ijklblah"[..], error_node_position!(ErrorKind::Custom(128), blah, error_position!(ErrorKind::Tag, blah))))));
assert_eq!(res_c, Ok((&b""[..], &b"mnop"[..])));
}
#[test]
@ -724,7 +725,7 @@ mod tests {
let a = &b"ijklmn"[..];
let res_a = err_test(a);
assert_eq!(res_a, Error(error_position!(ErrorKind::Complete, &b"mn"[..])));
assert_eq!(res_a, Err(Err::Error(error_position!(ErrorKind::Complete, &b"mn"[..]))));
}
#[test]
@ -733,12 +734,12 @@ mod tests {
named!( tag_def, tag!("def") );
named!( pair_abc_def<&[u8],(&[u8], &[u8])>, pair!(tag_abc, tag_def) );
assert_eq!(pair_abc_def(&b"abcdefghijkl"[..]), Done(&b"ghijkl"[..], (&b"abc"[..], &b"def"[..])));
assert_eq!(pair_abc_def(&b"ab"[..]), Incomplete(Needed::Size(3)));
assert_eq!(pair_abc_def(&b"abcd"[..]), Incomplete(Needed::Size(6)));
assert_eq!(pair_abc_def(&b"xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..])));
assert_eq!(pair_abc_def(&b"xxxdef"[..]), Error(error_position!(ErrorKind::Tag, &b"xxxdef"[..])));
assert_eq!(pair_abc_def(&b"abcxxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..])));
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(6))));
assert_eq!(pair_abc_def(&b"xxx"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))));
assert_eq!(pair_abc_def(&b"xxxdef"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxxdef"[..]))));
assert_eq!(pair_abc_def(&b"abcxxx"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))));
}
#[test]
@ -748,12 +749,12 @@ mod tests {
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"[..]), Done(&b"ghijkl"[..], (&b"abc"[..], &b"def"[..])));
assert_eq!(sep_pair_abc_def(&b"ab"[..]), Incomplete(Needed::Size(3)));
assert_eq!(sep_pair_abc_def(&b"abc,d"[..]), Incomplete(Needed::Size(7)));
assert_eq!(sep_pair_abc_def(&b"xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..])));
assert_eq!(sep_pair_abc_def(&b"xxx,def"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx,def"[..])));
assert_eq!(sep_pair_abc_def(&b"abc,xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..])));
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(7))));
assert_eq!(sep_pair_abc_def(&b"xxx"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))));
assert_eq!(sep_pair_abc_def(&b"xxx,def"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxx,def"[..]))));
assert_eq!(sep_pair_abc_def(&b"abc,xxx"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))));
}
#[test]
@ -762,12 +763,12 @@ mod tests {
named!( tag_efgh, tag!("efgh") );
named!( preceded_abcd_efgh<&[u8], &[u8]>, preceded!(tag_abcd, tag_efgh) );
assert_eq!(preceded_abcd_efgh(&b"abcdefghijkl"[..]), Done(&b"ijkl"[..], &b"efgh"[..]));
assert_eq!(preceded_abcd_efgh(&b"ab"[..]), Incomplete(Needed::Size(4)));
assert_eq!(preceded_abcd_efgh(&b"abcde"[..]), Incomplete(Needed::Size(8)));
assert_eq!(preceded_abcd_efgh(&b"xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..])));
assert_eq!(preceded_abcd_efgh(&b"xxxxdef"[..]), Error(error_position!(ErrorKind::Tag, &b"xxxxdef"[..])));
assert_eq!(preceded_abcd_efgh(&b"abcdxxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..])));
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(8))));
assert_eq!(preceded_abcd_efgh(&b"xxx"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))));
assert_eq!(preceded_abcd_efgh(&b"xxxxdef"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxxxdef"[..]))));
assert_eq!(preceded_abcd_efgh(&b"abcdxxx"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))));
}
#[test]
@ -776,12 +777,12 @@ mod tests {
named!( tag_efgh, tag!("efgh") );
named!( terminated_abcd_efgh<&[u8], &[u8]>, terminated!(tag_abcd, tag_efgh) );
assert_eq!(terminated_abcd_efgh(&b"abcdefghijkl"[..]), Done(&b"ijkl"[..], &b"abcd"[..]));
assert_eq!(terminated_abcd_efgh(&b"ab"[..]), Incomplete(Needed::Size(4)));
assert_eq!(terminated_abcd_efgh(&b"abcde"[..]), Incomplete(Needed::Size(8)));
assert_eq!(terminated_abcd_efgh(&b"xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..])));
assert_eq!(terminated_abcd_efgh(&b"xxxxdef"[..]), Error(error_position!(ErrorKind::Tag, &b"xxxxdef"[..])));
assert_eq!(terminated_abcd_efgh(&b"abcdxxxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxxx"[..])));
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(8))));
assert_eq!(terminated_abcd_efgh(&b"xxx"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))));
assert_eq!(terminated_abcd_efgh(&b"xxxxdef"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxxxdef"[..]))));
assert_eq!(terminated_abcd_efgh(&b"abcdxxxx"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxxx"[..]))));
}
#[test]
@ -791,14 +792,14 @@ mod tests {
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"[..]), Done(&b"jkl"[..], &b"def"[..]));
assert_eq!(delimited_abc_def_ghi(&b"ab"[..]), Incomplete(Needed::Size(3)));
assert_eq!(delimited_abc_def_ghi(&b"abcde"[..]), Incomplete(Needed::Size(6)));
assert_eq!(delimited_abc_def_ghi(&b"abcdefgh"[..]), Incomplete(Needed::Size(9)));
assert_eq!(delimited_abc_def_ghi(&b"xxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..])));
assert_eq!(delimited_abc_def_ghi(&b"xxxdefghi"[..]), Error(error_position!(ErrorKind::Tag, &b"xxxdefghi"[..])));
assert_eq!(delimited_abc_def_ghi(&b"abcxxxghi"[..]), Error(error_position!(ErrorKind::Tag, &b"xxxghi"[..])));
assert_eq!(delimited_abc_def_ghi(&b"abcdefxxx"[..]), Error(error_position!(ErrorKind::Tag, &b"xxx"[..])));
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(6))));
assert_eq!(delimited_abc_def_ghi(&b"abcdefgh"[..]), Err(Err::Incomplete(Needed::Size(9))));
assert_eq!(delimited_abc_def_ghi(&b"xxx"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))));
assert_eq!(delimited_abc_def_ghi(&b"xxxdefghi"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxxdefghi"[..]))));
assert_eq!(delimited_abc_def_ghi(&b"abcxxxghi"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxxghi"[..]))));
assert_eq!(delimited_abc_def_ghi(&b"abcdefxxx"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"xxx"[..]))));
}
#[test]
@ -806,16 +807,16 @@ mod tests {
named!(tuple_3<&[u8], (u16, &[u8], &[u8]) >,
tuple!( be_u16 , take!(3), tag!("fg") ) );
assert_eq!(tuple_3(&b"abcdefgh"[..]), Done(&b"h"[..], (0x6162u16, &b"cde"[..], &b"fg"[..])));
assert_eq!(tuple_3(&b"abcd"[..]), Incomplete(Needed::Size(5)));
assert_eq!(tuple_3(&b"abcde"[..]), Incomplete(Needed::Size(7)));
assert_eq!(tuple_3(&b"abcdejk"[..]), Error(error_position!(ErrorKind::Tag, &b"jk"[..])));
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(5))));
assert_eq!(tuple_3(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(7))));
assert_eq!(tuple_3(&b"abcdejk"[..]), Err(Err::Error(error_position!(ErrorKind::Tag, &b"jk"[..]))));
}
#[test]
fn do_parse() {
fn ret_int1(i:&[u8]) -> IResult<&[u8], u8> { Done(i,1) };
fn ret_int2(i:&[u8]) -> IResult<&[u8], u8> { Done(i,2) };
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)>,
@ -837,10 +838,10 @@ mod tests {
//trace_macros!(false);
assert_eq!(do_parser(&b"abcdabcdefghefghX"[..]), Done(&b"X"[..], (1, 2)));
assert_eq!(do_parser(&b"abcdefghefghX"[..]), Done(&b"X"[..], (1, 2)));
assert_eq!(do_parser(&b"abcdab"[..]), Incomplete(Needed::Size(8)));
assert_eq!(do_parser(&b"abcdefghef"[..]), Incomplete(Needed::Size(12)));
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(8))));
assert_eq!(do_parser(&b"abcdefghef"[..]), Err(Err::Incomplete(Needed::Size(12))));
}
#[test]
@ -857,9 +858,9 @@ mod tests {
let a = [2u8, 3, 4, 5];
let res_a = [3u8, 4];
assert_eq!(length_value(&a[..]), Done(&a[3..], &res_a[..]));
assert_eq!(length_value(&a[..]), Ok((&a[3..], &res_a[..])));
let b = [5u8, 3, 4, 5];
assert_eq!(length_value(&b[..]), Incomplete(Needed::Size(6)));
assert_eq!(length_value(&b[..]), Err(Err::Incomplete(Needed::Size(6))));
}
/*

View File

@ -16,11 +16,17 @@
//! Please note that the verbose error management is a bit slower
//! than the simple one.
use util::ErrorKind;
use internal::{IResult, IError};
use internal::IResult::*;
//use internal::{IResult, IError, Needed};
//use internal::IResult::*;
use internal::Needed;
pub type Err<E=u32> = ErrorKind<E>;
#[derive(Debug,Clone,PartialEq)]
pub enum Err<E=u32> {
Incomplete(Needed),
Error(ErrorKind<E>),
}
/*
impl<I,O,E> IResult<I,O,E> {
/// Maps a `IResult<I, O, E>` to `IResult<I, O, N>` by appling a function
/// to a contained `Error` value, leaving `Done` and `Incomplete` value
@ -81,6 +87,7 @@ impl<E: fmt::Debug> fmt::Display for Err<E> {
write!(f, "{}", self.description())
}
}
*/
/// translate parser result from IResult<I,O,u32> to IResult<I,O,E> with a custom type
///
@ -105,11 +112,11 @@ macro_rules! fix_error (
($i:expr, $t:ty, $submac:ident!( $($args:tt)* )) => (
{
match $submac!($i, $($args)*) {
$crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x),
$crate::IResult::Done(i, o) => $crate::IResult::Done(i, o),
$crate::IResult::Error(_) => {
::std::result::Result::Err($crate::Err::Incomplete(x)) => ::std::result::Result::Err($crate::Err::Incomplete(x)),
::std::result::Result::Ok((i, o)) => ::std::result::Result::Ok((i, o)),
::std::result::Result::Err($crate::Err::Error(_)) => {
let e: $crate::ErrorKind<$t> = $crate::ErrorKind::Fix;
$crate::IResult::Error(e)
::std::result::Result::Err($crate::Err::Error(e))
}
}
}
@ -129,14 +136,14 @@ macro_rules! flat_map(
($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => (
{
match $submac!($i, $($args)*) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i)),
$crate::IResult::Done(i, o) => match $submac2!(o, $($args2)*) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(ref i2)) => $crate::IResult::Incomplete($crate::Needed::Size(*i2)),
$crate::IResult::Done(_, o2) => $crate::IResult::Done(i, o2)
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))),
::std::result::Result::Ok((i, o)) => match $submac2!(o, $($args2)*) {
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(ref i2))) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(*i2))),
::std::result::Result::Ok((_, o2)) => ::std::result::Result::Ok((i, o2))
}
}
}

View File

@ -14,7 +14,7 @@
/// tag_s!(input, "abcd")
/// }
/// let r = test("abcdefgh");
/// assert_eq!(r, Done("efgh", "abcd"));
/// assert_eq!(r,Ok(("efgh", "abcd")));
/// # }
/// ```
#[macro_export]
@ -39,7 +39,7 @@ macro_rules! tag_s (
/// tag_no_case_s!(input, "ABcd")
/// }
/// let r = test("aBCdefgh");
/// assert_eq!(r, Done("efgh", "aBCd"));
/// assert_eq!(r,Ok(("efgh", "aBCd")));
/// # }
/// ```
#[macro_export]
@ -63,11 +63,11 @@ macro_rules! tag_no_case_s (
///
/// let a = "abcdefgh";
///
/// assert_eq!(take5(a), Done("fgh", "abcde"));
/// assert_eq!(take5(a),Ok(("fgh", "abcde")));
///
/// let b = "12345";
///
/// assert_eq!(take5(b), Done("", "12345"));
/// assert_eq!(take5(b),Ok(("", "12345")));
/// # }
/// ```
#[macro_export]
@ -92,7 +92,7 @@ macro_rules! take_s (
/// named!( not_space<&str,&str>, is_not_s!( " \t\r\n" ) );
///
/// let r = not_space("abcdefgh\nijkl");
/// assert_eq!(r, Done("\nijkl", "abcdefgh"));
/// assert_eq!(r,Ok(("\nijkl", "abcdefgh")));
/// # }
/// ```
#[macro_export]
@ -114,10 +114,10 @@ macro_rules! is_not_s (
/// named!(abcd<&str, &str>, is_a_s!( "abcd" ));
///
/// let r1 = abcd("aaaaefgh");
/// assert_eq!(r1, Done("efgh", "aaaa"));
/// assert_eq!(r1,Ok(("efgh", "aaaa")));
///
/// let r2 = abcd("dcbaefgh");
/// assert_eq!(r2, Done("efgh", "dcba"));
/// assert_eq!(r2,Ok(("efgh", "dcba")));
/// # }
/// ```
#[macro_export]
@ -144,7 +144,7 @@ macro_rules! is_a_s (
/// named!( alpha<&str,&str>, take_while_s!( alphabetic ) );
///
/// let r = alpha("abcd\nefgh");
/// assert_eq!(r, Done("\nefgh", "abcd"));
/// assert_eq!(r,Ok(("\nefgh", "abcd")));
/// # }
/// ```
#[macro_export]
@ -172,7 +172,7 @@ macro_rules! take_while_s (
/// named!( alpha<&str,&str>, take_while1_s!( alphabetic ) );
///
/// let r = alpha("abcd\nefgh");
/// assert_eq!(r, Done("\nefgh", "abcd"));
/// assert_eq!(r,Ok(("\nefgh", "abcd")));
/// # }
/// ```
#[macro_export]
@ -243,6 +243,7 @@ macro_rules! take_until_s (
#[cfg(test)]
mod test {
use ::IResult;
use simple_errors::Err;
#[test]
fn tag_str_succeed() {
@ -253,7 +254,7 @@ mod test {
}
match test(INPUT) {
IResult::Done(extra, output) => {
Ok((extra, output)) => {
assert!(extra == " World!", "Parser `tag_s` consumed leftover input.");
assert!(output == TAG,
"Parser `tag_s` doesn't return the tag it matched on success. \
@ -270,7 +271,7 @@ mod test {
const TAG: &'static str = "Hello World!";
match tag_s!(INPUT, TAG) {
IResult::Incomplete(_) => (),
Err(Err::Incomplete(_)) => (),
other => {
panic!("Parser `tag_s` didn't require more input when it should have. \
Got `{:?}`.", other);
@ -284,7 +285,7 @@ mod test {
const TAG: &'static str = "Random"; // TAG must be closer than INPUT.
match tag_s!(INPUT, TAG) {
IResult::Error(_) => (),
Err(Err::Error(_)) => (),
other => {
panic!("Parser `tag_s` didn't fail when it should have. Got `{:?}`.`", other);
},
@ -298,7 +299,7 @@ mod test {
const LEFTOVER: &'static str = "áƒƭèř";
match take_s!(INPUT, 9) {
IResult::Done(extra, output) => {
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 `{}`.",
@ -317,7 +318,7 @@ mod test {
const LEFTOVER: &'static str = "ÂßÇ∂áƒƭèř";
match take_until_s!(INPUT, FIND) {
IResult::Done(extra, output) => {
Ok((extra, output)) => {
assert!(extra == LEFTOVER, "Parser `take_until_s`\
consumed leftover input. Leftover `{}`.", extra);
assert!(output == CONSUMED, "Parser `take_until_s`\
@ -334,13 +335,12 @@ mod test {
const INPUT: &'static str = "βèƒôřèÂßÇá";
match take_s!(INPUT, 13) {
IResult::Incomplete(_) => (),
Err(Err::Incomplete(_)) => (),
other => panic!("Parser `take_s` didn't require more input when it should have. \
Got `{:?}`.", other),
}
}
use internal::IResult::{Done, Error, Incomplete};
use internal::Needed;
use util::ErrorKind;
@ -355,10 +355,10 @@ mod test {
let c = "abcd123";
let d = "123";
assert_eq!(f(&a[..]), Done(&a[..], &a[..]));
assert_eq!(f(&b[..]), Done(&a[..], &b[..]));
assert_eq!(f(&c[..]), Done(&d[..], &b[..]));
assert_eq!(f(&d[..]), Done(&d[..], &a[..]));
assert_eq!(f(&a[..]),Ok((&a[..], &a[..])));
assert_eq!(f(&b[..]),Ok((&a[..], &b[..])));
assert_eq!(f(&c[..]),Ok((&d[..], &b[..])));
assert_eq!(f(&d[..]),Ok((&d[..], &a[..])));
}
#[test]
@ -369,10 +369,10 @@ mod test {
let c = "abcd123";
let d = "123";
assert_eq!(f(&a[..]), Incomplete(Needed::Size(1)));
assert_eq!(f(&b[..]), Done(&a[..], &b[..]));
assert_eq!(f(&c[..]), Done(&"123"[..], &b[..]));
assert_eq!(f(&d[..]), Error(error_position!(ErrorKind::TakeWhile1, &d[..])));
assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1))));
assert_eq!(f(&b[..]),Ok((&a[..], &b[..])));
assert_eq!(f(&c[..]),Ok((&"123"[..], &b[..])));
assert_eq!(f(&d[..]), Err(Err::Error(error_position!(ErrorKind::TakeWhile1, &d[..]))));
}
#[test]
@ -387,7 +387,7 @@ mod test {
take_till_s!(input, till_s)
}
match test(INPUT) {
IResult::Done(extra, output) => {
Ok((extra, output)) => {
assert!(extra == LEFTOVER, "Parser `take_till_s` consumed leftover input.");
assert!(output == CONSUMED,
"Parser `take_till_s` doesn't return the string it consumed on success. \
@ -410,7 +410,7 @@ mod test {
take_while_s!(input, while_s)
}
match test(INPUT) {
IResult::Done(extra, output) => {
Ok((extra, output)) => {
assert!(extra == LEFTOVER, "Parser `take_while_s` consumed leftover input.");
assert!(output == CONSUMED,
"Parser `take_while_s` doesn't return the string it consumed on success. \
@ -431,7 +431,7 @@ mod test {
is_not_s!(input, AVOID)
}
match test(INPUT) {
IResult::Done(extra, output) => {
Ok((extra, output)) => {
assert!(extra == LEFTOVER, "Parser `is_not_s` consumed leftover input. Leftover `{}`.", extra);
assert!(output == CONSUMED,
"Parser `is_not_s` doens't return the string it consumed on success. Expected `{}`, got `{}`.",
@ -450,7 +450,7 @@ mod test {
const LEFTOVER: &'static str = "áƒƭèř";
match take_until_and_consume_s!(INPUT, FIND) {
IResult::Done(extra, output) => {
Ok((extra, output)) => {
assert!(extra == LEFTOVER, "Parser `take_until_and_consume_s`\
consumed leftover input. Leftover `{}`.", extra);
assert!(output == OUTPUT, "Parser `take_until_and_consume_s`\
@ -475,7 +475,7 @@ mod test {
take_while_s!(input, while_s)
}
match test(INPUT) {
IResult::Done(extra, output) => {
Ok((extra, output)) => {
assert!(extra == LEFTOVER, "Parser `take_while_s` consumed leftover input.");
assert!(output == CONSUMED,
"Parser `take_while_s` doesn't return the string it consumed on success. \
@ -494,7 +494,7 @@ mod test {
is_not_s!(input, AVOID)
}
match test(INPUT) {
IResult::Error(_) => (),
Err(Err::Error(_)) => (),
other => panic!("Parser `is_not_s` didn't fail when it should have. Got `{:?}`.", other),
};
}
@ -512,7 +512,7 @@ mod test {
take_while1_s!(input, while1_s)
}
match test(INPUT) {
IResult::Done(extra, output) => {
Ok((extra, output)) => {
assert!(extra == LEFTOVER, "Parser `take_while1_s` consumed leftover input.");
assert!(output == CONSUMED,
"Parser `take_while1_s` doesn't return the string it consumed on success. \
@ -529,7 +529,7 @@ mod test {
const FIND: &'static str = "βèƒôřèÂßÇ";
match take_until_and_consume_s!(INPUT, FIND) {
IResult::Incomplete(_) => (),
Err(Err::Incomplete(_)) => (),
other => panic!("Parser `take_until_and_consume_s` didn't require more input when it should have. \
Got `{:?}`.", other),
};
@ -541,7 +541,7 @@ mod test {
const FIND: &'static str = "βèƒôřèÂßÇ";
match take_until_s!(INPUT, FIND) {
IResult::Incomplete(_) => (),
Err(Err::Incomplete(_)) => (),
other => panic!("Parser `take_until_s` didn't require more input when it should have. \
Got `{:?}`.", other),
};
@ -557,7 +557,7 @@ mod test {
is_a_s!(input, MATCH)
}
match test(INPUT) {
IResult::Done(extra, output) => {
Ok((extra, output)) => {
assert!(extra == LEFTOVER, "Parser `is_a_s` consumed leftover input. Leftover `{}`.", extra);
assert!(output == CONSUMED,
"Parser `is_a_s` doens't return the string it consumed on success. Expected `{}`, got `{}`.",
@ -578,7 +578,7 @@ mod test {
take_while1_s!(input, while1_s)
}
match test(INPUT) {
IResult::Error(_) => (),
Err(Err::Error(_)) => (),
other => panic!("Parser `take_while1_s` didn't fail when it should have. \
Got `{:?}`.", other),
};
@ -592,7 +592,7 @@ mod test {
is_a_s!(input, MATCH)
}
match test(INPUT) {
IResult::Error(_) => (),
Err(Err::Error(_)) => (),
other => panic!("Parser `is_a_s` didn't fail when it should have. Got `{:?}`.", other),
};
}
@ -603,7 +603,7 @@ mod test {
const FIND: &'static str = "Ráñδô₥";
match take_until_and_consume_s!(INPUT, FIND) {
IResult::Error(_) => (),
Err(Err::Error(_)) => (),
other => panic!("Parser `take_until_and_consume_s` didn't fail when it should have. \
Got `{:?}`.", other),
};
@ -615,7 +615,7 @@ mod test {
const FIND: &'static str = "Ráñδô₥";
match take_until_s!(INPUT, FIND) {
IResult::Error(_) => (),
Err(Err::Error(_)) => (),
other => panic!("Parser `take_until_and_consume_s` didn't fail when it should have. \
Got `{:?}`.", other),
};
@ -629,8 +629,8 @@ mod test {
named!(f <&str,&str>, recognize!(many1!(alt!( tag_s!("a") | tag_s!("b") ))));
assert_eq!(f(&a[..]), Done(&a[6..], &a[..]));
assert_eq!(f(&b[..]), Done(&b[4..], &b[..4]));
assert_eq!(f(&a[..]),Ok((&a[6..], &a[..])));
assert_eq!(f(&b[..]),Ok((&b[4..], &b[..4])));
}
@ -646,13 +646,13 @@ mod test {
#[test]
fn case_insensitive() {
named!(test<&str,&str>, tag_no_case!("ABcd"));
assert_eq!(test("aBCdefgh"), Done("efgh", "aBCd"));
assert_eq!(test("abcdefgh"), Done("efgh", "abcd"));
assert_eq!(test("ABCDefgh"), Done("efgh", "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"), Done("efgh", "aBCd"));
assert_eq!(test2("abcdefgh"), Done("efgh", "abcd"));
assert_eq!(test2("ABCDefgh"), Done("efgh", "ABCD"));
assert_eq!(test2("aBCdefgh"),Ok(("efgh", "aBCd")));
assert_eq!(test2("abcdefgh"),Ok(("efgh", "abcd")));
assert_eq!(test2("ABCDefgh"),Ok(("efgh", "ABCD")));
}
}

View File

@ -23,7 +23,7 @@
//!
//! assert_eq!(
//! tuple(&b" \t abc de fg"[..]),
//! Done(&b"fg"[..], (&b"abc"[..], &b"de"[..]))
//! Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..])))
//! );
//! # }
//! ```
@ -58,7 +58,7 @@
//!
//! assert_eq!(
//! tuple(&b" \t abc de fg"[..]),
//! Done(&b"fg"[..], (&b"abc"[..], &b"de"[..]))
//! Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..])))
//! );
//! # }
//! ```
@ -88,19 +88,19 @@
macro_rules! wrap_sep (
($i:expr, $separator:expr, $submac:ident!( $($args:tt)* )) => (
match ($separator)($i) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Done(i1,_) => {
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Ok((i1,_)) => {
match $submac!(i1, $($args)*) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i1)))),
$crate::IResult::Done(i2,o) => {
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i + ($crate::InputLength::input_len(&($i))) - $crate::InputLength::input_len(&i1)))),
::std::result::Result::Ok((i2,o)) => {
match ($separator)(i2) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size(i + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i2)))),
$crate::IResult::Done(i3,_) => $crate::IResult::Done(i3, o)
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i + ($crate::InputLength::input_len(&($i))) - $crate::InputLength::input_len(&i2)))),
::std::result::Result::Ok((i3,_)) => ::std::result::Result::Ok((i3, o))
}
}
}
@ -138,10 +138,10 @@ macro_rules! pair_sep (
macro_rules! delimited_sep (
($i:expr, $separator:ident, $submac1:ident!( $($args1:tt)* ), $($rest:tt)+) => (
match tuple_sep!($i, $separator, 0usize, (), $submac1!($($args1)*), $($rest)*) {
$crate::IResult::Error(a) => $crate::IResult::Error(a),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Done(remaining, (_,o,_)) => {
$crate::IResult::Done(remaining, o)
::std::result::Result::Err($crate::Err::Error(a)) => ::std::result::Result::Err($crate::Err::Error(a)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Ok((remaining, (_,o,_))) => {
::std::result::Result::Ok((remaining, o))
}
}
);
@ -155,10 +155,10 @@ macro_rules! delimited_sep (
macro_rules! separated_pair_sep (
($i:expr, $separator:ident, $submac1:ident!( $($args1:tt)* ), $($rest:tt)+) => (
match tuple_sep!($i, $separator, 0usize, (), $submac1!($($args1)*), $($rest)*) {
$crate::IResult::Error(a) => $crate::IResult::Error(a),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Done(remaining, (o1,_,o2)) => {
$crate::IResult::Done(remaining, (o1,o2))
::std::result::Result::Err($crate::Err::Error(a)) => ::std::result::Result::Err($crate::Err::Error(a)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Ok((remaining, (o1,_,o2))) => {
::std::result::Result::Ok((remaining, (o1,o2)))
}
}
);
@ -172,10 +172,10 @@ macro_rules! separated_pair_sep (
macro_rules! preceded_sep (
($i:expr, $separator:ident, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => (
match pair_sep!($i, $separator, $submac!($($args)*), $submac2!($($args2)*)) {
$crate::IResult::Error(a) => $crate::IResult::Error(a),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Done(remaining, (_,o)) => {
$crate::IResult::Done(remaining, o)
::std::result::Result::Err($crate::Err::Error(a)) => ::std::result::Result::Err($crate::Err::Error(a)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Ok((remaining, (_,o))) => {
::std::result::Result::Ok((remaining, o))
}
}
);
@ -195,10 +195,10 @@ macro_rules! preceded_sep (
macro_rules! terminated_sep (
($i:expr, $separator:ident, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => (
match pair_sep!($i, $separator, $submac!($($args)*), $submac2!($($args2)*)) {
$crate::IResult::Error(a) => $crate::IResult::Error(a),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Done(remaining, (o,_)) => {
$crate::IResult::Done(remaining, o)
::std::result::Result::Err($crate::Err::Error(a)) => ::std::result::Result::Err($crate::Err::Error(a)),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Ok((remaining, (o,_))) => {
::std::result::Result::Ok((remaining, o))
}
}
);
@ -223,10 +223,10 @@ macro_rules! tuple_sep (
($i:expr, $separator:ident, $consumed:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => (
{
match sep!($i, $separator, $submac!($($args)*)) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)),
$crate::IResult::Done(i,o) => {
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($consumed + i))),
::std::result::Result::Ok((i,o)) => {
tuple_sep!(i, $separator, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i)), (o), $($rest)*)
}
}
@ -235,10 +235,10 @@ macro_rules! tuple_sep (
($i:expr, $separator:ident, $consumed:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => (
{
match sep!($i, $separator, $submac!($($args)*)) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)),
$crate::IResult::Done(i,o) => {
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($consumed + i))),
::std::result::Result::Ok((i,o)) => {
tuple_sep!(i, $separator, $consumed + ($crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i)), ($($parsed)* , o), $($rest)*)
}
}
@ -250,11 +250,11 @@ macro_rules! tuple_sep (
($i:expr, $separator:ident, $consumed:expr, (), $submac:ident!( $($args:tt)* )) => (
{
match sep!($i, $separator, $submac!($($args)*)) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)),
$crate::IResult::Done(i,o) => {
$crate::IResult::Done(i, (o))
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($consumed + i))),
::std::result::Result::Ok((i,o)) => {
::std::result::Result::Ok((i, (o)))
}
}
}
@ -262,18 +262,18 @@ macro_rules! tuple_sep (
($i:expr, $separator:ident, $consumed:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => (
{
match sep!($i, $separator, $submac!($($args)*)) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) => $crate::IResult::Incomplete($crate::Needed::Size($consumed + i)),
$crate::IResult::Done(i,o) => {
$crate::IResult::Done(i, ($($parsed),* , o))
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) => ::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($consumed + i))),
::std::result::Result::Ok((i,o)) => {
::std::result::Result::Ok((i, ($($parsed),* , o)))
}
}
}
);
($i:expr, $separator:ident, $consumed:expr, ($($parsed:expr),*)) => (
{
$crate::IResult::Done($i, ($($parsed),*))
::std::result::Result::Ok(($i, ($($parsed),*)))
}
);
);
@ -282,7 +282,7 @@ macro_rules! tuple_sep (
#[macro_export]
macro_rules! do_parse_sep (
(__impl $i:expr, $separator:ident, $consumed:expr, ( $($rest:expr),* )) => (
$crate::IResult::Done($i, ( $($rest),* ))
::std::result::Result::Ok(($i, ( $($rest),* )))
);
(__impl $i:expr, $separator:ident, $consumed:expr, $e:ident >> $($rest:tt)*) => (
@ -291,12 +291,12 @@ macro_rules! do_parse_sep (
(__impl $i:expr, $separator:ident, $consumed:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => (
{
match sep!($i, $separator, $submac!($($args)*)) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) =>
$crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) =>
$crate::IResult::Incomplete($crate::Needed::Size($consumed + i)),
$crate::IResult::Done(i,_) => {
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($consumed + i))),
::std::result::Result::Ok((i,_)) => {
do_parse_sep!(__impl i, $separator,
$consumed + ($crate::InputLength::input_len(&($i)) -
$crate::InputLength::input_len(&i)), $($rest)*)
@ -312,12 +312,12 @@ macro_rules! do_parse_sep (
(__impl $i:expr, $separator:ident, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => (
{
match sep!($i, $separator, $submac!($($args)*)) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) =>
$crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) =>
$crate::IResult::Incomplete($crate::Needed::Size($consumed + i)),
$crate::IResult::Done(i,o) => {
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($consumed + i))),
::std::result::Result::Ok((i,o)) => {
let $field = o;
do_parse_sep!(__impl i, $separator,
$consumed + ($crate::InputLength::input_len(&($i)) -
@ -334,13 +334,13 @@ macro_rules! do_parse_sep (
(__impl $i:expr, $separator:ident, $consumed:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => (
match sep!($i, $separator, $submac!($($args)*)) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) =>
$crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) =>
$crate::IResult::Incomplete($crate::Needed::Size($consumed + i)),
$crate::IResult::Done(i,_) => {
$crate::IResult::Done(i, ( $($rest)* ))
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($consumed + i))),
::std::result::Result::Ok((i,_)) => {
::std::result::Result::Ok((i, ( $($rest)* )))
},
}
);
@ -351,14 +351,14 @@ macro_rules! do_parse_sep (
(__impl $i:expr, $separator:ident, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => (
match sep!($i, $separator, $submac!($($args)*)) {
$crate::IResult::Error(e) => $crate::IResult::Error(e),
$crate::IResult::Incomplete($crate::Needed::Unknown) =>
$crate::IResult::Incomplete($crate::Needed::Unknown),
$crate::IResult::Incomplete($crate::Needed::Size(i)) =>
$crate::IResult::Incomplete($crate::Needed::Size($consumed + i)),
$crate::IResult::Done(i,o) => {
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(e)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown)),
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size(i))) =>
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Size($consumed + i))),
::std::result::Result::Ok((i,o)) => {
let $field = o;
$crate::IResult::Done(i, ( $($rest)* ))
::std::result::Result::Ok((i, ( $($rest)* )))
},
}
);
@ -394,21 +394,21 @@ macro_rules! permutation_sep (
if let ::std::option::Option::Some(need) = needed {
if let $crate::Needed::Size(sz) = need {
$crate::IResult::Incomplete(
::std::result::Result::Err($crate::Err::Incomplete(
$crate::Needed::Size(
$crate::InputLength::input_len(&($i)) -
$crate::InputLength::input_len(&input) +
sz
)
)
))
} else {
$crate::IResult::Incomplete($crate::Needed::Unknown)
::std::result::Result::Err($crate::Err::Incomplete($crate::Needed::Unknown))
}
} else if let ::std::option::Option::Some(e) = error {
$crate::IResult::Error(e)
::std::result::Result::Err($crate::Err::Error(e))
} else {
let unwrapped_res = permutation_unwrap!(0, (), res, $($rest)*);
$crate::IResult::Done(input, unwrapped_res)
::std::result::Result::Ok((input, unwrapped_res))
}
}
);
@ -423,15 +423,15 @@ macro_rules! permutation_iterator_sep (
($it:tt, $i:expr, $separator:ident, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
if acc!($it, $res) == ::std::option::Option::None {
match sep!($i, $separator, $submac!($($args)*)) {
$crate::IResult::Done(i,o) => {
::std::result::Result::Ok((i,o)) => {
$i = i;
acc!($it, $res) = ::std::option::Option::Some(o);
continue;
},
$crate::IResult::Error(_) => {
::std::result::Result::Err($crate::Err::Error(_)) => {
$all_done = false;
},
$crate::IResult::Incomplete(i) => {
::std::result::Result::Err($crate::Err::Incomplete(i)) => {
$needed = ::std::option::Option::Some(i);
break;
}
@ -445,15 +445,15 @@ macro_rules! permutation_iterator_sep (
($it:tt, $i:expr, $separator:ident, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )) => {
if acc!($it, $res) == ::std::option::Option::None {
match sep!($i, $separator, $submac!($($args)*)) {
$crate::IResult::Done(i,o) => {
::std::result::Result::Ok((i,o)) => {
$i = i;
acc!($it, $res) = ::std::option::Option::Some(o);
continue;
},
$crate::IResult::Error(_) => {
::std::result::Result::Err($crate::Err::Error(_)) => {
$all_done = false;
},
$crate::IResult::Incomplete(i) => {
::std::result::Result::Err($crate::Err::Incomplete(i)) => {
$needed = ::std::option::Option::Some(i);
break;
}
@ -473,8 +473,8 @@ macro_rules! alt_sep (
{
let res = sep!($i, $separator, $subrule!($($args)*));
match res {
$crate::IResult::Done(_,_) => res,
$crate::IResult::Incomplete(_) => res,
::std::result::Result::Ok((_,_)) => res,
::std::result::Result::Err($crate::Err::Incomplete(_)) => res,
_ => alt_sep!(__impl $i, $separator, $($rest)*)
}
}
@ -483,9 +483,9 @@ macro_rules! alt_sep (
(__impl $i:expr, $separator:ident, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => (
{
match sep!($i, $separator, $subrule!( $($args)* )) {
$crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)),
$crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x),
$crate::IResult::Error(_) => {
::std::result::Result::Ok((i,o)) => ::std::result::Result::Ok((i,$gen(o))),
::std::result::Result::Err($crate::Err::Incomplete(x)) => ::std::result::Result::Err($crate::Err::Incomplete(x)),
::std::result::Result::Err($crate::Err::Error(_)) => {
alt_sep!(__impl $i, $separator, $($rest)*)
}
}
@ -503,10 +503,10 @@ macro_rules! alt_sep (
(__impl $i:expr, $separator:ident, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => (
{
match sep!($i, $separator, $subrule!( $($args)* )) {
$crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)),
$crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x),
$crate::IResult::Error(_) => {
$crate::IResult::Error(error_position!($crate::ErrorKind::Alt,$i))
::std::result::Result::Ok((i,o)) => ::std::result::Result::Ok((i,$gen(o))),
::std::result::Result::Err($crate::Err::Incomplete(x)) => ::std::result::Result::Err($crate::Err::Incomplete(x)),
::std::result::Result::Err($crate::Err::Error(_)) => {
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Alt,$i)))
}
}
}
@ -519,21 +519,21 @@ macro_rules! alt_sep (
(__impl $i:expr, $separator:ident, $subrule:ident!( $($args:tt)*)) => (
{
match sep!($i, $separator, $subrule!( $($args)* )) {
$crate::IResult::Done(i,o) => $crate::IResult::Done(i,o),
$crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x),
$crate::IResult::Error(_) => {
$crate::IResult::Error(error_position!($crate::ErrorKind::Alt,$i))
::std::result::Result::Ok((i,o)) => ::std::result::Result::Ok((i,o)),
::std::result::Result::Err($crate::Err::Incomplete(x)) => ::std::result::Result::Err($crate::Err::Incomplete(x)),
::std::result::Result::Err($crate::Err::Error(_)) => {
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Alt,$i)))
}
}
}
);
(__impl $i:expr) => (
$crate::IResult::Error(error_position!($crate::ErrorKind::Alt,$i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Alt,$i)))
);
(__impl $i:expr, $separator:ident) => (
$crate::IResult::Error(error_position!($crate::ErrorKind::Alt,$i))
::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Alt,$i)))
);
($i:expr, $separator:ident, $($rest:tt)*) => (
@ -554,7 +554,7 @@ macro_rules! alt_complete_sep (
{
let res = complete!($i, sep!($separator, $subrule!($($args)*)));
match res {
$crate::IResult::Done(_,_) => res,
::std::result::Result::Ok((_,_)) => res,
_ => alt_complete_sep!($i, $separator, $($rest)*),
}
}
@ -563,7 +563,7 @@ macro_rules! alt_complete_sep (
($i:expr, $separator:ident, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => (
{
match complete!($i, sep!($separator, $subrule!($($args)*))) {
$crate::IResult::Done(i,o) => $crate::IResult::Done(i,$gen(o)),
::std::result::Result::Ok((i,o)) => ::std::result::Result::Ok((i,$gen(o))),
_ => alt_complete_sep!($i, $separator, $($rest)*),
}
}
@ -598,19 +598,19 @@ macro_rules! switch_sep (
(__impl $i:expr, $separator:ident, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => (
{
match sep!($i, $separator, $submac!($($args)*)) {
$crate::IResult::Error(e) => $crate::IResult::Error(error_node_position!(
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(error_node_position!(
$crate::ErrorKind::Switch, $i, e
)),
$crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
$crate::IResult::Done(i, o) => {
))),
::std::result::Result::Err($crate::Err::Incomplete(i)) => ::std::result::Result::Err($crate::Err::Incomplete(i)),
::std::result::Result::Ok((i, o)) => {
match o {
$($p => match sep!(i, $separator, $subrule!($($args2)*)) {
$crate::IResult::Error(e) => $crate::IResult::Error(error_node_position!(
::std::result::Result::Err($crate::Err::Error(e)) => ::std::result::Result::Err($crate::Err::Error(error_node_position!(
$crate::ErrorKind::Switch, $i, e
)),
))),
a => a,
}),*,
_ => $crate::IResult::Error(error_position!($crate::ErrorKind::Switch,$i))
_ => ::std::result::Result::Err($crate::Err::Error(error_position!($crate::ErrorKind::Switch,$i)))
}
}
}
@ -663,7 +663,7 @@ macro_rules! eat_separator (
{
use $crate::{AsChar,InputLength,InputIter,Slice};
if ($i).input_len() == 0 {
$crate::IResult::Done(($i).slice(0..), ($i).slice(0..0))
::std::result::Result::Ok((($i).slice(0..), ($i).slice(0..0)))
} else {
match ($i).iter_indices().position(|(_, item)| {
let i = item.as_char();
@ -673,10 +673,10 @@ macro_rules! eat_separator (
true
}) {
::std::option::Option::Some(index) => {
$crate::IResult::Done(($i).slice(index..), ($i).slice(..index))
::std::result::Result::Ok((($i).slice(index..), ($i).slice(..index)))
},
::std::option::Option::None => {
$crate::IResult::Done(($i).slice(($i).input_len()..), ($i))
::std::result::Result::Ok((($i).slice(($i).input_len()..), ($i)))
}
}
}
@ -820,7 +820,7 @@ pub fn sp<'a,T>(input:T) -> IResult<T, T> where
///
/// assert_eq!(
/// tuple(&b" \t abc de fg"[..]),
/// Done(&b"fg"[..], (&b"abc"[..], &b"de"[..]))
/// Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..])))
/// );
/// # }
/// ```
@ -838,21 +838,21 @@ macro_rules! ws (
#[cfg(test)]
#[allow(dead_code)]
mod tests {
use internal::IResult::*;
use internal::{IResult,Needed};
use simple_errors::Err;
use super::sp;
use util::ErrorKind;
#[test]
fn spaaaaace() {
assert_eq!(sp(&b" \t abc "[..]), Done(&b"abc "[..], &b" \t "[..]));
assert_eq!(sp(&b" \t abc "[..]), Ok((&b"abc "[..], &b" \t "[..])));
}
#[test]
fn tag() {
named!(abc, ws!(tag!("abc")));
assert_eq!(abc(&b" \t abc def"[..]), Done(&b"def"[..], &b"abc"[..]));
assert_eq!(abc(&b" \t abc def"[..]), Ok((&b"def"[..], &b"abc"[..])));
}
#[test]
@ -861,7 +861,7 @@ mod tests {
ws!(pair!( take!(3), tag!("de") ))
);
assert_eq!(pair_2(&b" \t abc de fg"[..]), Done(&b"fg"[..], (&b"abc"[..], &b"de"[..])));
assert_eq!(pair_2(&b" \t abc de fg"[..]), Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..]))));
}
#[test]
@ -870,7 +870,7 @@ mod tests {
ws!(preceded!( take!(3), tag!("de") ))
);
assert_eq!(prec(&b" \t abc de fg"[..]), Done(&b"fg"[..], &b"de"[..]));
assert_eq!(prec(&b" \t abc de fg"[..]), Ok((&b"fg"[..], &b"de"[..])));
}
#[test]
@ -879,7 +879,7 @@ mod tests {
ws!(terminated!( take!(3), tag!("de") ))
);
assert_eq!(term(&b" \t abc de fg"[..]), Done(&b"fg"[..], &b"abc"[..]));
assert_eq!(term(&b" \t abc de fg"[..]), Ok((&b"fg"[..], &b"abc"[..])));
}
#[test]
@ -890,7 +890,7 @@ mod tests {
);
//trace_macros!(false);
assert_eq!(tuple_2(&b" \t abc de fg"[..]), Done(&b"fg"[..], (&b"abc"[..], &b"de"[..])));
assert_eq!(tuple_2(&b" \t abc de fg"[..]), Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..]))));
}
#[test]
@ -901,13 +901,13 @@ mod tests {
);
//trace_macros!(false);
assert_eq!(level_2(&b" \t abc de fg \t hi "[..]), Done(&b"hi "[..], (&b"abc"[..], (&b"de"[..], &b"fg "[..]))));
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> { Done(i,1) };
fn ret_int2(i:&[u8]) -> IResult<&[u8], u8> { Done(i,2) };
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)>,
@ -924,10 +924,10 @@ mod tests {
//trace_macros!(false);
assert_eq!(do_parser(&b"abcd abcd\tefghefghX"[..]), Done(&b"X"[..], (1, 2)));
assert_eq!(do_parser(&b"abcd\tefgh efgh X"[..]), Done(&b"X"[..], (1, 2)));
assert_eq!(do_parser(&b"abcd ab"[..]), Incomplete(Needed::Size(10)));
assert_eq!(do_parser(&b" abcd\tefgh\tef"[..]), Incomplete(Needed::Size(15)));
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(10))));
assert_eq!(do_parser(&b" abcd\tefgh\tef"[..]), Err(Err::Incomplete(Needed::Size(15))));
}
#[test]
@ -941,32 +941,32 @@ mod tests {
let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]);
let a = &b"abcd\tefg \thijk"[..];
assert_eq!(perm(a), Done(&b"jk"[..], expected));
assert_eq!(perm(a),Ok((&b"jk"[..], expected)));
let b = &b" efg \tabcdhi jk"[..];
assert_eq!(perm(b), Done(&b"jk"[..], expected));
assert_eq!(perm(b),Ok((&b"jk"[..], expected)));
let c = &b" hi efg\tabcdjk"[..];
assert_eq!(perm(c), Done(&b"jk"[..], expected));
assert_eq!(perm(c),Ok((&b"jk"[..], expected)));
let d = &b"efg xyzabcdefghi"[..];
assert_eq!(perm(d), Error(error_position!(ErrorKind::Permutation, &b"xyzabcdefghi"[..])));
assert_eq!(perm(d), Err(Err::Error(error_position!(ErrorKind::Permutation, &b"xyzabcdefghi"[..]))));
let e = &b" efg \tabc"[..];
assert_eq!(perm(e), Incomplete(Needed::Size(10)));
assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(10))));
}
#[test]
fn alt() {
fn work(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> {
Done(&b""[..], input)
Ok((&b""[..], input))
}
#[allow(unused_variables)]
fn dont_work(input: &[u8]) -> IResult<&[u8],&[u8],&'static str> {
Error(error_code!(ErrorKind::Custom("abcd")))
Err(Err::Error(error_code!(ErrorKind::Custom("abcd"))))
}
fn work2(input: &[u8]) -> IResult<&[u8],&[u8], &'static str> {
Done(input, &b""[..])
Ok((input, &b""[..]))
}
fn alt1(i:&[u8]) -> IResult<&[u8],&[u8], &'static str> {
@ -980,19 +980,19 @@ mod tests {
}
let a = &b"\tabcd"[..];
assert_eq!(alt1(a), Error(error_position!(ErrorKind::Alt, a)));
assert_eq!(alt2(a), Done(&b""[..], a));
assert_eq!(alt3(a), Done(a, &b""[..]));
assert_eq!(alt1(a), Err(Err::Error(error_position!(ErrorKind::Alt, a))));
assert_eq!(alt2(a),Ok((&b""[..], a)));
assert_eq!(alt3(a),Ok((a, &b""[..])));
named!(alt4, ws!(alt!(tag!("abcd") | tag!("efgh"))));
let b = &b" efgh "[..];
assert_eq!(alt4(a), Done(&b""[..], &b"abcd"[..]));
assert_eq!(alt4(b), Done(&b""[..], &b"efgh"[..]));
assert_eq!(alt4(a),Ok((&b""[..], &b"abcd"[..])));
assert_eq!(alt4(b),Ok((&b""[..], &b"efgh"[..])));
// test the alternative syntax
named!(alt5<bool>, ws!(alt!(tag!("abcd") => { |_| false } | tag!("efgh") => { |_| true })));
assert_eq!(alt5(a), Done(&b""[..], false));
assert_eq!(alt5(b), Done(&b""[..], true));
assert_eq!(alt5(a),Ok((&b""[..], false)));
assert_eq!(alt5(b),Ok((&b""[..], true)));
}
#[test]
@ -1002,11 +1002,11 @@ mod tests {
);
let a = &b""[..];
assert_eq!(ac(a), Incomplete(Needed::Size(2)));
assert_eq!(ac(a), Err(Err::Incomplete(Needed::Size(2))));
let a = &b" \tef "[..];
assert_eq!(ac(a), Done(&b""[..], &b"ef"[..]));
assert_eq!(ac(a),Ok((&b""[..], &b"ef"[..])));
let a = &b" cde"[..];
assert_eq!(ac(a), Error(error_position!(ErrorKind::Alt, &a[1..])));
assert_eq!(ac(a), Err(Err::Error(error_position!(ErrorKind::Alt, &a[1..]))));
}
#[allow(unused_variables)]
@ -1020,19 +1020,19 @@ mod tests {
);
let a = &b" abcd ef gh"[..];
assert_eq!(sw(a), Done(&b"gh"[..], &b"ef"[..]));
assert_eq!(sw(a),Ok((&b"gh"[..], &b"ef"[..])));
let b = &b"\tefgh ijkl "[..];
assert_eq!(sw(b), Done(&b""[..], &b"ijkl"[..]));
assert_eq!(sw(b),Ok((&b""[..], &b"ijkl"[..])));
let c = &b"afghijkl"[..];
assert_eq!(sw(c), Error(error_position!(ErrorKind::Switch, &b"afghijkl"[..])));
assert_eq!(sw(c), Err(Err::Error(error_position!(ErrorKind::Switch, &b"afghijkl"[..]))));
}
named!(str_parse(&str) -> &str, ws!(tag!("test")));
#[allow(unused_variables)]
#[test]
fn str_test() {
assert_eq!(str_parse(" \n test\t a\nb"), Done("a\nb", "test"));
assert_eq!(str_parse(" \n test\t a\nb"),Ok(("a\nb", "test")));
}
// test whitespace parser generation for alt

View File

@ -58,30 +58,30 @@ named!(expr <i64>, do_parse!(
#[test]
fn factor_test() {
assert_eq!(factor(&b"3"[..]), IResult::Done(&b""[..], 3));
assert_eq!(factor(&b" 12"[..]), IResult::Done(&b""[..], 12));
assert_eq!(factor(&b"537 "[..]), IResult::Done(&b""[..], 537));
assert_eq!(factor(&b" 24 "[..]), IResult::Done(&b""[..], 24));
assert_eq!(factor(&b"3"[..]), Ok((&b""[..], 3)));
assert_eq!(factor(&b" 12"[..]), Ok((&b""[..], 12)));
assert_eq!(factor(&b"537 "[..]), Ok((&b""[..], 537)));
assert_eq!(factor(&b" 24 "[..]), Ok((&b""[..], 24)));
}
#[test]
fn term_test() {
assert_eq!(term(&b" 12 *2 / 3"[..]), IResult::Done(&b""[..], 8));
assert_eq!(term(&b" 2* 3 *2 *2 / 3"[..]), IResult::Done(&b""[..], 8));
assert_eq!(term(&b" 48 / 3/2"[..]), IResult::Done(&b""[..], 8));
assert_eq!(term(&b" 12 *2 / 3"[..]), Ok((&b""[..], 8)));
assert_eq!(term(&b" 2* 3 *2 *2 / 3"[..]), Ok((&b""[..], 8)));
assert_eq!(term(&b" 48 / 3/2"[..]), Ok((&b""[..], 8)));
}
#[test]
fn expr_test() {
assert_eq!(expr(&b" 1 + 2 "[..]), IResult::Done(&b""[..], 3));
assert_eq!(expr(&b" 12 + 6 - 4+ 3"[..]), IResult::Done(&b""[..], 17));
assert_eq!(expr(&b" 1 + 2*3 + 4"[..]), IResult::Done(&b""[..], 11));
assert_eq!(expr(&b" 1 + 2 "[..]), Ok((&b""[..], 3)));
assert_eq!(expr(&b" 12 + 6 - 4+ 3"[..]), Ok((&b""[..], 17)));
assert_eq!(expr(&b" 1 + 2*3 + 4"[..]), Ok((&b""[..], 11)));
}
#[test]
fn parens_test() {
assert_eq!(expr(&b" ( 2 )"[..]), IResult::Done(&b""[..], 2));
assert_eq!(expr(&b" 2* ( 3 + 4 ) "[..]), IResult::Done(&b""[..], 14));
assert_eq!(expr(&b" 2*2 / ( 5 - 1) + 3"[..]), IResult::Done(&b""[..], 4));
assert_eq!(expr(&b" ( 2 )"[..]), Ok((&b""[..], 2)));
assert_eq!(expr(&b" 2* ( 3 + 4 ) "[..]), Ok((&b""[..], 14)));
assert_eq!(expr(&b" 2*2 / ( 5 - 1) + 3"[..]), Ok((&b""[..], 4)));
}

View File

@ -108,30 +108,34 @@ named!(expr< Expr >, do_parse!(
(fold_exprs(initial, remainder))
));
/*
#[test]
fn factor_test() {
assert_eq!(factor(&b" 3 "[..]).map(|x| format!("{:?}", x)),
IResult::Done(&b""[..], String::from("3")));
Ok((&b""[..], String::from("3"))));
}
#[test]
fn term_test() {
assert_eq!(term(&b" 3 * 5 "[..]).map(|x| format!("{:?}", x)),
IResult::Done(&b""[..], String::from("(3 * 5)")));
Ok( (&b""[..], String::from("(3 * 5)")) ));
}
#[test]
fn expr_test() {
assert_eq!(expr(&b" 1 + 2 * 3 "[..]).map(|x| format!("{:?}", x)),
IResult::Done(&b""[..], String::from("(1 + (2 * 3))")));
Ok( (&b""[..], String::from("(1 + (2 * 3))")) ));
assert_eq!(expr(&b" 1 + 2 * 3 / 4 - 5 "[..]).map(|x| format!("{:?}", x)),
IResult::Done(&b""[..], String::from("((1 + ((2 * 3) / 4)) - 5)")));
Ok( (&b""[..], String::from("((1 + ((2 * 3) / 4)) - 5)")) ));
assert_eq!(expr(&b" 72 / 2 / 3 "[..]).map(|x| format!("{:?}", x)),
IResult::Done(&b""[..], String::from("((72 / 2) / 3)")));
Ok( (&b""[..], String::from("((72 / 2)) / 3)")) ));
}
#[test]
fn parens_test() {
assert_eq!(expr(&b" ( 1 + 2 ) * 3 "[..]).map(|x| format!("{:?}", x)),
IResult::Done(&b""[..], String::from("([(1 + 2)] * 3)")));
assert_eq!(
expr(&b" ( 1 + 2 ) * 3 "[..]).map(|x| format!("{:?}", x)),
Ok( (&b""[..], String::from("([(1 + 2)] * 3)")) )
);
}
*/

View File

@ -31,16 +31,16 @@ named!(float <f32>, map!(
#[test]
fn unsigned_float_test() {
assert_eq!(unsigned_float(&b"123.456"[..]), IResult::Done(&b""[..], 123.456));
assert_eq!(unsigned_float(&b"0.123"[..]), IResult::Done(&b""[..], 0.123));
assert_eq!(unsigned_float(&b"123.0"[..]), IResult::Done(&b""[..], 123.0));
assert_eq!(unsigned_float(&b"123."[..]), IResult::Done(&b""[..], 123.0));
assert_eq!(unsigned_float(&b".123"[..]), IResult::Done(&b""[..], 0.123));
assert_eq!(unsigned_float(&b"123.456"[..]), Ok((&b""[..], 123.456)));
assert_eq!(unsigned_float(&b"0.123"[..]), Ok((&b""[..], 0.123)));
assert_eq!(unsigned_float(&b"123.0"[..]), Ok((&b""[..], 123.0)));
assert_eq!(unsigned_float(&b"123."[..]), Ok((&b""[..], 123.0)));
assert_eq!(unsigned_float(&b".123"[..]), Ok((&b""[..], 0.123)));
}
#[test]
fn float_test() {
assert_eq!(float(&b"123.456"[..]), IResult::Done(&b""[..], 123.456));
assert_eq!(float(&b"+123.456"[..]), IResult::Done(&b""[..], 123.456));
assert_eq!(float(&b"-123.456"[..]), IResult::Done(&b""[..], -123.456));
assert_eq!(float(&b"123.456"[..]), Ok((&b""[..], 123.456)));
assert_eq!(float(&b"+123.456"[..]), Ok((&b""[..], 123.456)));
assert_eq!(float(&b"-123.456"[..]), Ok((&b""[..], -123.456)));
}

View File

@ -77,11 +77,11 @@ key = value2"[..];
let res = category(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
Ok((i, o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_category, "category"));
assert_eq!(res, Ok((ini_without_category, "category")));
}
#[test]
@ -94,11 +94,11 @@ key = value2"[..];
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
Ok((i, (o1, o2))) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
}
@ -112,11 +112,11 @@ key = value2"[..];
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
Ok((i, (o1, o2))) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
}
#[test]
@ -129,11 +129,11 @@ key = value2"[..];
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
Ok((i, (o1, o2))) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
}
#[test]
@ -149,14 +149,14 @@ key = value2
let res = keys_and_values(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
Ok((i, ref o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
let mut expected: HashMap<&str, &str> = HashMap::new();
expected.insert("parameter", "value");
expected.insert("key", "value2");
assert_eq!(res, IResult::Done(ini_without_key_value, expected));
assert_eq!(res, Ok((ini_without_key_value, expected)));
}
#[test]
@ -174,14 +174,14 @@ key = value2
let res = category_and_keys(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
Ok((i, ref o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
let mut expected_h: HashMap<&str, &str> = HashMap::new();
expected_h.insert("parameter", "value");
expected_h.insert("key", "value2");
assert_eq!(res, IResult::Done(ini_after_parser, ("abcd", expected_h)));
assert_eq!(res, Ok((ini_after_parser, ("abcd", expected_h))));
}
#[test]
@ -202,7 +202,7 @@ key4 = value4
let res = categories(ini_file);
//println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
Ok((i, ref o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
@ -215,5 +215,5 @@ key4 = value4
let mut expected_h: HashMap<&str, HashMap<&str, &str>> = HashMap::new();
expected_h.insert("abcd", expected_1);
expected_h.insert("category", expected_2);
assert_eq!(res, IResult::Done(ini_after_parser, expected_h));
assert_eq!(res, Ok((ini_after_parser, expected_h)));
}

View File

@ -1,7 +1,7 @@
#[macro_use]
extern crate nom;
use nom::IResult;
use nom::{Err,IResult};
use std::collections::HashMap;
@ -62,11 +62,11 @@ named!(keys_and_values_aggregator<&str, Vec<(&str,&str)> >, many0!(key_value));
fn keys_and_values(input:&str) -> IResult<&str, HashMap<&str, &str> > {
match keys_and_values_aggregator(input) {
IResult::Done(i,tuple_vec) => {
IResult::Done(i, tuple_vec.into_iter().collect())
Ok((i,tuple_vec)) => {
Ok((i, tuple_vec.into_iter().collect()))
},
IResult::Incomplete(a) => IResult::Incomplete(a),
IResult::Error(a) => IResult::Error(a)
Err(Err::Incomplete(a)) => Err(Err::Incomplete(a)),
Err(Err::Error(a)) => Err(Err::Error(a))
}
}
@ -79,11 +79,11 @@ named!(categories_aggregator<&str, Vec<(&str, HashMap<&str,&str>)> >, many0!(cat
fn categories(input: &str) -> IResult<&str, HashMap<&str, HashMap<&str, &str> > > {
match categories_aggregator(input) {
IResult::Done(i,tuple_vec) => {
IResult::Done(i, tuple_vec.into_iter().collect())
Ok((i,tuple_vec)) => {
Ok((i, tuple_vec.into_iter().collect()))
},
IResult::Incomplete(a) => IResult::Incomplete(a),
IResult::Error(a) => IResult::Error(a)
Err(Err::Incomplete(a)) => Err(Err::Incomplete(a)),
Err(Err::Error(a)) => Err(Err::Error(a))
}
}
@ -101,11 +101,11 @@ key = value2";
let res = category(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, o) => println!("i: {} | o: {:?}", i, o),
Ok((i, o)) => println!("i: {} | o: {:?}", i, o),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_category, "category"));
assert_eq!(res, Ok((ini_without_category, "category")));
}
#[test]
@ -118,11 +118,11 @@ key = value2";
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
}
#[test]
@ -135,11 +135,11 @@ key = value2";
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
}
#[test]
@ -152,11 +152,11 @@ key = value2";
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
}
#[test]
@ -172,14 +172,14 @@ key = value2
let res = keys_and_values(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {} | o: {:?}", i, o),
Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o),
_ => println!("error")
}
let mut expected: HashMap<&str, &str> = HashMap::new();
expected.insert("parameter", "value");
expected.insert("key", "value2");
assert_eq!(res, IResult::Done(ini_without_key_value, expected));
assert_eq!(res, Ok((ini_without_key_value, expected)));
}
#[test]
@ -197,14 +197,14 @@ key = value2
let res = category_and_keys(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {} | o: {:?}", i, o),
Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o),
_ => println!("error")
}
let mut expected_h: HashMap<&str, &str> = HashMap::new();
expected_h.insert("parameter", "value");
expected_h.insert("key", "value2");
assert_eq!(res, IResult::Done(ini_after_parser, ("abcd", expected_h)));
assert_eq!(res, Ok((ini_after_parser, ("abcd", expected_h))));
}
#[test]
@ -223,7 +223,7 @@ key4 = value4
let res = categories(ini_file);
//println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {} | o: {:?}", i, o),
Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o),
_ => println!("error")
}
@ -236,5 +236,5 @@ key4 = value4
let mut expected_h: HashMap<&str, HashMap<&str, &str>> = HashMap::new();
expected_h.insert("abcd", expected_1);
expected_h.insert("category", expected_2);
assert_eq!(res, IResult::Done("", expected_h));
assert_eq!(res, Ok(("", expected_h)));
}

View File

@ -4,7 +4,7 @@
#[macro_use]
extern crate nom;
use nom::{IResult,Needed,space,be_u16,le_u64};
use nom::{Err,IResult,Needed,space,be_u16,le_u64};
#[allow(dead_code)]
struct Range {
@ -14,9 +14,9 @@ struct Range {
pub fn take_char(input: &[u8]) -> IResult<&[u8], char> {
if input.len() > 0 {
IResult::Done(&input[1..], input[0] as char)
Ok((&input[1..], input[0] as char))
} else {
IResult::Incomplete(Needed::Size(1))
Err(Err::Incomplete(Needed::Size(1)))
}
}
@ -95,11 +95,11 @@ mod parse_int {
#[test]
fn issue_142(){
let subject = parse_ints(&b"12 34 5689"[..]);
let expected = IResult::Done(&b""[..], vec![12, 34, 5689]);
let expected = Ok((&b""[..], vec![12, 34, 5689]));
assert_eq!(subject, expected);
let subject = parse_ints(&b"12 34 5689 "[..]);
let expected = IResult::Done(&b" "[..], vec![12, 34, 5689]);
let expected = Ok((&b" "[..], vec![12, 34, 5689]));
assert_eq!(subject, expected)
}
}
@ -133,8 +133,8 @@ fn take_till_issue() {
take_till!(call!(|_| true))
);
assert_eq!(nothing(b""), IResult::Done(&b""[..], &b""[..]));
assert_eq!(nothing(b"abc"), IResult::Done(&b"abc"[..], &b""[..]));
assert_eq!(nothing(b""), Ok((&b""[..], &b""[..])));
assert_eq!(nothing(b"abc"), Ok((&b"abc"[..], &b""[..])));
}
named!(issue_498< Vec<&[u8]> >, separated_nonempty_list!( opt!(space), tag!("abcd") ));

View File

@ -5,24 +5,25 @@
extern crate nom;
use nom::{HexDisplay,Offset,Needed,IResult,be_u16,be_u32,be_u64,be_f32,ErrorKind};
use nom::{Consumer,ConsumerState,Move,Input,Producer,FileProducer,FileProducerState};
use nom::IResult::*;
//use nom::{Consumer,ConsumerState,Move,Input,Producer,FileProducer,FileProducerState};
//use nom::IResult;
use nom::Err;
use std::str;
use std::io::SeekFrom;
fn mp4_box(input:&[u8]) -> IResult<&[u8], &[u8]> {
match be_u32(input) {
Done(i, offset) => {
Ok((i, offset)) => {
let sz: usize = offset as usize;
if i.len() >= sz - 4 {
Done(&i[(sz-4)..], &i[0..(sz-4)])
Ok((&i[(sz-4)..], &i[0..(sz-4)]))
} else {
Incomplete(Needed::Size(offset as usize + 4))
Err(Err::Incomplete(Needed::Size(offset as usize + 4)))
}
}
Error(e) => Error(e),
Incomplete(e) => Incomplete(e)
Err(Err::Error(e)) => Err(Err::Error(e)),
Err(Err::Incomplete(e)) => Err(Err::Incomplete(e))
}
}
@ -253,20 +254,20 @@ named!(filetype_parser<&[u8], FileType>,
fn mvhd_box(input:&[u8]) -> IResult<&[u8],MvhdBox> {
let res = if input.len() < 100 {
Incomplete(Needed::Size(100))
Err(Err::Incomplete(Needed::Size(100)))
} else if input.len() == 100 {
mvhd32(input)
} else if input.len() == 112 {
mvhd64(input)
} else {
Error(error_position!(ErrorKind::Custom(32),input))
Err(Err::Error(error_position!(ErrorKind::Custom(32),input)))
};
println!("res: {:?}", res);
res
}
fn unknown_box_type(input:&[u8]) -> IResult<&[u8], MP4BoxType> {
Done(input, MP4BoxType::Unknown)
Ok((input, MP4BoxType::Unknown))
}
//named!(box_type<&[u8], MP4BoxType>,
@ -315,6 +316,7 @@ named!(moov_header<&[u8],MP4BoxHeader>,
)
);
/*
#[derive(Debug,PartialEq,Eq)]
enum MP4State {
Main,
@ -340,22 +342,22 @@ impl MP4Consumer {
Input::Empty => ConsumerState::Continue(Move::Consume(0)),
Input::Element(sl) | Input::Eof(Some(sl)) => {
match box_header(sl) {
Done(i, header) => {
Ok((i, header)) => {
match header.tag {
MP4BoxType::Ftyp => {
println!("-> FTYP");
match filetype_parser(&i[0..(header.length as usize - 8)]) {
Done(rest, filetype_header) => {
Ok((rest, filetype_header)) => {
println!("filetype header: {:?}", filetype_header);
//return ConsumerState::Await(header.length as usize, header.length as usize - 8);
return ConsumerState::Continue(Move::Consume(sl.offset(rest)));
}
Error(a) => {
Err(Err::Error(a)) => {
println!("ftyp parsing error: {:?}", a);
assert!(false);
return ConsumerState::Error(());
},
Incomplete(n) => {
Err(Err::Incomplete(n)) => {
println!("ftyp incomplete -> await: {}", sl.len());
return ConsumerState::Continue(Move::Await(n));
//return ConsumerState::Await(0, input.len() + 100);
@ -381,12 +383,12 @@ impl MP4Consumer {
}
return ConsumerState::Continue(Move::Seek(SeekFrom::Current((header.length) as i64)))
},
Error(a) => {
Err(Err::Error(a)) => {
println!("mp4 parsing error: {:?}", a);
assert!(false);
return ConsumerState::Error(());
},
Incomplete(i) => {
Err(Err::Incomplete(i)) => {
// FIXME: incomplete should send the required size
println!("mp4 incomplete -> await: {}", sl.len());
return ConsumerState::Continue(Move::Await(i));
@ -408,7 +410,7 @@ impl MP4Consumer {
return ConsumerState::Continue(Move::Consume(0));
}
match moov_header(sl) {
Done(i, header) => {
Ok((i, header)) => {
match header.tag {
MP4BoxType::Mvhd => {
println!("-> MVHD");
@ -435,13 +437,13 @@ impl MP4Consumer {
println!("remaining moov_bytes: {}", self.moov_bytes);
return ConsumerState::Continue(Move::Seek(SeekFrom::Current((header.length) as i64)))
},
Error(a) => {
Err(Err::Error(a)) => {
println!("moov parsing error: {:?}", a);
println!("data:\n{}", sl.to_hex(8));
assert!(false);
return ConsumerState::Error(());
},
Incomplete(i) => {
Err(Err::Incomplete(i)) => {
println!("moov incomplete -> await: {}", sl.len());
return ConsumerState::Continue(Move::Await(i));
}
@ -523,6 +525,6 @@ fn small_test() {
fn big_bunny_test() {
explore_mp4_file("assets/bigbuckbunny.mp4");
}
*/

View File

@ -14,7 +14,7 @@ named!(read_lines <Vec<&str> >, many0!(read_line));
#[test]
fn read_lines_test() {
let res = IResult::Done(&b""[..], vec!["Duck", "Dog", "Cow"]);
let res = Ok((&b""[..], vec!["Duck", "Dog", "Cow"]));
assert_eq!(read_lines(&b"Duck\nDog\nCow\n"[..]), res);
assert_eq!(read_lines(&b"Duck\nDog\nCow"[..]), res);

View File

@ -79,30 +79,30 @@ named!(expr <i64>, do_parse!(
#[test]
fn factor_test() {
assert_eq!(factor(&b"3"[..]), IResult::Done(&b""[..], 3));
assert_eq!(factor(&b" 12"[..]), IResult::Done(&b""[..], 12));
assert_eq!(factor(&b"537 "[..]), IResult::Done(&b""[..], 537));
assert_eq!(factor(&b" 24 "[..]), IResult::Done(&b""[..], 24));
assert_eq!(factor(&b"3"[..]), Ok((&b""[..], 3)));
assert_eq!(factor(&b" 12"[..]), Ok((&b""[..], 12)));
assert_eq!(factor(&b"537 "[..]), Ok((&b""[..], 537)));
assert_eq!(factor(&b" 24 "[..]), Ok((&b""[..], 24)));
}
#[test]
fn term_test() {
assert_eq!(term(&b" 12 *2 / 3"[..]), IResult::Done(&b""[..], 8));
assert_eq!(term(&b" 2* 3 *2 *2 / 3"[..]), IResult::Done(&b""[..], 8));
assert_eq!(term(&b" 48 / 3/2"[..]), IResult::Done(&b""[..], 8));
assert_eq!(term(&b" 12 *2 / 3"[..]), Ok((&b""[..], 8)));
assert_eq!(term(&b" 2* 3 *2 *2 / 3"[..]), Ok((&b""[..], 8)));
assert_eq!(term(&b" 48 / 3/2"[..]), Ok((&b""[..], 8)));
}
#[test]
fn expr_test() {
assert_eq!(expr(&b" 1 + 2 "[..]), IResult::Done(&b""[..], 3));
assert_eq!(expr(&b" 12 + 6 - 4+ 3"[..]), IResult::Done(&b""[..], 17));
assert_eq!(expr(&b" 1 + 2*3 + 4"[..]), IResult::Done(&b""[..], 11));
assert_eq!(expr(&b" 1 + 2 "[..]), Ok((&b""[..], 3)));
assert_eq!(expr(&b" 12 + 6 - 4+ 3"[..]), Ok((&b""[..], 17)));
assert_eq!(expr(&b" 1 + 2*3 + 4"[..]), Ok((&b""[..], 11)));
}
#[test]
fn parens_test() {
assert_eq!(expr(&b" ( 2 )"[..]), IResult::Done(&b""[..], 2));
assert_eq!(expr(&b" 2* ( 3 + 4 ) "[..]), IResult::Done(&b""[..], 14));
assert_eq!(expr(&b" 2*2 / ( 5 - 1) + 3"[..]), IResult::Done(&b""[..], 4));
assert_eq!(expr(&b" ( 2 )"[..]), Ok((&b""[..], 2)));
assert_eq!(expr(&b" 2* ( 3 + 4 ) "[..]), Ok((&b""[..], 14)));
assert_eq!(expr(&b" 2*2 / ( 5 - 1) + 3"[..]), Ok((&b""[..], 4)));
}

View File

@ -1,5 +1,5 @@
#![cfg(feature = "stream")]
/*
#[macro_use]
extern crate nom;
@ -144,6 +144,7 @@ fn no_nomnom() {
assert_eq!(c.counter, 0);
assert_eq!(c.state, State::Done);
}
*/
/*
#[test]

View File

@ -1,7 +1,7 @@
#[macro_use]
extern crate nom;
use nom::{IResult,Needed,be_u8,be_u64};
use nom::{Err,IResult,Needed,be_u8,be_u64};
// Parser definition
@ -21,12 +21,12 @@ named!(parser02<&[u8],(&[u8],&[u8])>,
#[test]
fn overflow_incomplete_do_parse() {
assert_eq!(parser01(&b"3"[..]), IResult::Incomplete(Needed::Unknown));
assert_eq!(parser01(&b"3"[..]), Err(Err::Incomplete(Needed::Unknown)));
}
#[test]
fn overflow_incomplete_tuple() {
assert_eq!(parser02(&b"3"[..]), IResult::Incomplete(Needed::Unknown));
assert_eq!(parser02(&b"3"[..]), Err(Err::Incomplete(Needed::Unknown)));
}
#[test]
@ -34,7 +34,7 @@ fn overflow_incomplete_length_bytes() {
named!(multi<&[u8], Vec<&[u8]> >, many0!( length_bytes!(be_u64) ) );
// Trigger an overflow in length_bytes
assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff\xaa"[..]), IResult::Incomplete(Needed::Unknown));
assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff\xaa"[..]), Err(Err::Incomplete(Needed::Unknown)));
}
#[test]
@ -42,7 +42,7 @@ fn overflow_incomplete_many0() {
named!(multi<&[u8], Vec<&[u8]> >, many0!( length_bytes!(be_u64) ) );
// Trigger an overflow in many0
assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), IResult::Incomplete(Needed::Unknown));
assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), Err(Err::Incomplete(Needed::Unknown)));
}
#[test]
@ -51,7 +51,7 @@ fn overflow_incomplete_many1() {
named!(multi<&[u8], Vec<&[u8]> >, many1!( length_bytes!(be_u64) ) );
// Trigger an overflow in many1
assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), IResult::Incomplete(Needed::Unknown));
assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), Err(Err::Incomplete(Needed::Unknown)));
}
#[test]
@ -59,7 +59,7 @@ fn overflow_incomplete_many_till() {
named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( length_bytes!(be_u64), tag!("abc") ) );
// Trigger an overflow in many_till
assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), IResult::Incomplete(Needed::Unknown));
assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), Err(Err::Incomplete(Needed::Unknown)));
}
#[test]
@ -67,33 +67,33 @@ fn overflow_incomplete_many_m_n() {
named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, length_bytes!(be_u64) ) );
// Trigger an overflow in many_m_n
assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), IResult::Incomplete(Needed::Unknown));
assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), Err(Err::Incomplete(Needed::Unknown)));
}
#[test]
fn overflow_incomplete_count() {
named!(counter<&[u8], Vec<&[u8]> >, count!( length_bytes!(be_u64), 2 ) );
assert_eq!(counter(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), IResult::Incomplete(Needed::Unknown));
assert_eq!(counter(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), Err(Err::Incomplete(Needed::Unknown)));
}
#[test]
fn overflow_incomplete_count_fixed() {
named!(counter< [&[u8]; 2] >, count_fixed!( &[u8], length_bytes!(be_u64), 2 ) );
assert_eq!(counter(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), IResult::Incomplete(Needed::Unknown));
assert_eq!(counter(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]), Err(Err::Incomplete(Needed::Unknown)));
}
#[test]
fn overflow_incomplete_length_count() {
named!(multi<&[u8], Vec<&[u8]> >, length_count!( be_u8, length_bytes!(be_u64) ) );
assert_eq!(multi(&b"\x04\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xee\xaa"[..]), IResult::Incomplete(Needed::Unknown));
assert_eq!(multi(&b"\x04\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xee\xaa"[..]), Err(Err::Incomplete(Needed::Unknown)));
}
#[test]
fn overflow_incomplete_length_data() {
named!(multi<&[u8], Vec<&[u8]> >, many0!( length_data!(be_u64) ) );
assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff\xaa"[..]), IResult::Incomplete(Needed::Unknown));
assert_eq!(multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff\xaa"[..]), Err(Err::Incomplete(Needed::Unknown)));
}

View File

@ -3,11 +3,12 @@
#[macro_use]
extern crate nom;
use nom::{IResult,Producer,FileProducer,not_line_ending};
use nom::{IResult,not_line_ending};
use std::str;
use std::fmt::Debug;
/*
#[test]
#[allow(unused_must_use)]
fn tag() {
@ -22,10 +23,11 @@ fn tag() {
}
});
}
*/
pub fn print<T: Debug>(input: T) -> IResult<T,()> {
println!("{:?}", input);
IResult::Done(input, ())
Ok((input, ()))
}
@ -34,11 +36,11 @@ fn is_not() {
//is_not!(foo b"\r\n");
named!(foo<&[u8],&[u8]>, is_not!(&b"\r\n"[..]));
let a = &b"ab12cd\nefgh"[..];
assert_eq!(foo(a), IResult::Done(&b"\nefgh"[..], &b"ab12cd"[..]));
assert_eq!(foo(a), Ok((&b"\nefgh"[..], &b"ab12cd"[..])));
}
#[test]
fn exported_public_method_defined_by_macro() {
let a = &b"ab12cd\nefgh"[..];
assert_eq!(not_line_ending(a), IResult::Done(&b"\nefgh"[..], &b"ab12cd"[..]));
assert_eq!(not_line_ending(a), Ok((&b"\nefgh"[..], &b"ab12cd"[..])));
}