nom::alt! [] [src]

macro_rules! alt {
    ($i:expr, $($rest:tt)*) => { ... };
}

alt!(I -> IResult<I,O> | I -> IResult<I,O> | ... | I -> IResult<I,O> ) => I -> IResult<I, O> try a list of parsers, return the result of the first successful one

If one of the parser returns Incomplete, alt will return Incomplete, to retry once you get more input. Note that it is better for performance to know the minimum size of data you need before you get into alt.

 named!( test, alt!( tag!( "abcd" ) | tag!( "efgh" ) ) );
 let r1 = test(b"abcdefgh");
 assert_eq!(r1, Done(&b"efgh"[..], &b"abcd"[..]));
 let r2 = test(&b"efghijkl"[..]);
 assert_eq!(r2, Done(&b"ijkl"[..], &b"efgh"[..]));

There is another syntax for alt allowing a block to manipulate the result:

    #[derive(Debug,PartialEq,Eq)]
    enum Tagged {
      Abcd,
      Efgh,
      Took(usize)
    }
    named!(test<Tagged>, alt!(
        tag!("abcd") => { |_|          Tagged::Abcd }
      | tag!("efgh") => { |_|          Tagged::Efgh }
      | take!(5)     => { |res: &[u8]| Tagged::Took(res.len()) } // the closure takes the result as argument if the parser is successful
    ));
    let r1 = test(b"abcdefgh");
    assert_eq!(r1, Done(&b"efgh"[..], Tagged::Abcd));
    let r2 = test(&b"efghijkl"[..]);
    assert_eq!(r2, Done(&b"ijkl"[..], Tagged::Efgh));
    let r3 = test(&b"mnopqrst"[..]);
    assert_eq!(r3, Done(&b"rst"[..],  Tagged::Took(5)));