nom::alt! [] [src]

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

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

Incomplete results are ignored

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