Created
February 15, 2019 11:05
-
-
Save Geal/f56f95f3d64dfb21b283f884d2d92af0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] | |
extern crate nom; | |
use nom::{digit, be_u32, IResult, Err}; | |
named!(first<u32>, flat_map!(digit, parse_to!(u32))); | |
named!(second<u32>, call!(be_u32)); | |
/*fn or<'a, 'b, F>(input: &'a [u8], left: F, right: F) -> IResult<&'a [u8], u32> | |
where F: &'b Fn(&'a [u8]) -> IResult<&'a [u8], u32> { | |
match left(input) { | |
Err(Err::Error(_)) => { | |
right(input) | |
}, | |
rest => rest | |
} | |
} | |
fn parser(input: &[u8]) -> IResult<&[u8], u32> { | |
or(input, &first, &second) | |
} | |
*/ | |
//named!(parser<u32>, alt!(first | second)); | |
fn or<'a, 'b>(input: &'a [u8], fns: &'b[&'b Fn(&'a [u8]) -> IResult<&'a [u8], u32>]) -> IResult<&'a [u8], u32> { | |
let left = fns[0]; | |
let right = fns[1]; | |
match left(input) { | |
Err(Err::Error(_)) => { | |
right(input) | |
}, | |
rest => rest | |
} | |
} | |
fn parser(input: &[u8]) -> IResult<&[u8], u32> { | |
or(input, &[&first, &second]) | |
} | |
fn main() { | |
let data = b"1234;"; | |
let res = parser(&data[..]); | |
println!("res: {:?}", res); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment