nom::opt! [] [src]

macro_rules! opt(
  ($i:expr, $submac:ident!( $($args:tt)* )) => (
    {
      match $submac!($i, $($args)*) {
        $crate::IResult::Done(i,o)     => $crate::IResult::Done(i, Some(o)),
        $crate::IResult::Error(_)      => $crate::IResult::Done($i, None),
        $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i)
      }
    }
  );
  ($i:expr, $f:expr) => (
    opt!($i, call!($f));
  );
);

opt!(I -> IResult<I,O>) => I -> IResult<I, Option<O>> make the underlying parser optional

returns an Option of the returned type. This parser never fails

 named!( o<&[u8], Option<&[u8]> >, opt!( tag!( "abcd" ) ) );

 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));