[][src]Macro nom::opt

macro_rules! opt {
    ($i:expr, $submac:ident!( $($args:tt)* )) => { ... };
    ($i:expr, $f:expr) => { ... };
}

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

returns an Option of the returned type. This parser returns Some(result) if the child parser succeeds,None if it fails, and Incomplete if it did not have enough data to decide

Warning: if you are using opt for some kind of optional ending token (like an end of line), you should combine it with complete to make sure it works.

As an example, opt!(tag!("\r\n")) will return Incomplete if it receives an empty input, because tag does not have enough input to decide. On the contrary, opt!(complete!(tag!("\r\n"))) would return None as produced value, since complete! transforms an Incomplete in an Error.

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

 let a = b"abcdef";
 let b = b"bcdefg";
 assert_eq!(o(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..]))));
 assert_eq!(o(&b[..]), Ok((&b"bcdefg"[..], None)));