Created
February 23, 2016 21:02
-
-
Save Geal/6f3c9a7e81fdb8623628 to your computer and use it in GitHub Desktop.
custom backtracking with nom
This file contains hidden or 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::IResult; | |
macro_rules! n ( | |
($name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( | |
fn $name( i: $i ) -> std::result::Result<nom::IResult<$i,$o,u32>, nom::Err<$i, u32>> { | |
std::result::Result::Ok($submac!(i, $($args)*)) | |
} | |
); | |
($name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( | |
fn $name( i: $i ) -> std::result::Result<nom::IResult<$i, $o, $e>, nom::Err<$i, $e>> { | |
std::result::Result::Ok($submac!(i, $($args)*)) | |
} | |
); | |
($name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( | |
fn $name( i: $i ) -> std::result::Result<nom::IResult<$i, $o, u32>, nom::Err<$i, u32>> { | |
std::result::Result::Ok($submac!(i, $($args)*)) | |
} | |
); | |
($name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( | |
fn $name<'a>( i: &'a[u8] ) -> std::result::Result<nom::IResult<&'a [u8], $o, u32>, nom::Err<&'a [u8], u32>> { | |
std::result::Result::Ok($submac!(i, $($args)*)) | |
} | |
); | |
($name:ident, $submac:ident!( $($args:tt)* )) => ( | |
fn $name( i: &[u8] ) -> std::result::Result<nom::IResult<&[u8], &[u8], u32>, nom::Err<&[u8], u32>> { | |
std::result::Result::Ok($submac!(i, $($args)*)) | |
} | |
); | |
(pub $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( | |
pub fn $name( i: $i ) -> std::result::Result<nom::IResult<$i,$o, u32>, nom::Err<$i, u32>> { | |
std::result::Result::Ok($submac!(i, $($args)*)) | |
} | |
); | |
(pub $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( | |
pub fn $name( i: $i ) -> std::result::Result<nom::IResult<$i, $o, $e>, nom::Err<$i, $e>> { | |
std::result::Result::Ok($submac!(i, $($args)*)) | |
} | |
); | |
(pub $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( | |
pub fn $name( i: $i ) -> std::result::Result<nom::IResult<$i, $o, u32>, nom::Err<$i, u32>> { | |
std::result::Result::Ok($submac!(i, $($args)*)) | |
} | |
); | |
(pub $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( | |
pub fn $name( i: &[u8] ) -> std::result::Result<nom::IResult<&[u8], $o, u32>, nom::Err<&[u8], u32>> { | |
std::result::Result::Ok($submac!(i, $($args)*)) | |
} | |
); | |
(pub $name:ident, $submac:ident!( $($args:tt)* )) => ( | |
pub fn $name<'a>( i: &'a [u8] ) -> std::result::Result<nom::IResult<&[u8], &[u8], u32>, nom::Err<&[u8], u32>> { | |
std::result::Result::Ok($submac!(i, $($args)*)) | |
} | |
); | |
); | |
macro_rules! cut ( | |
($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( | |
{ | |
let cl = || { | |
Ok($submac!($i, $($args)*)) | |
}; | |
match cl() { | |
std::result::Result::Ok(nom::IResult::Incomplete(x)) => nom::IResult::Incomplete(x), | |
std::result::Result::Ok(nom::IResult::Done(i, o)) => nom::IResult::Done(i, o), | |
std::result::Result::Ok(nom::IResult::Error(e)) | std::result::Result::Err(e) => { | |
return std::result::Result::Err(nom::Err::NodePosition($code, $i, Box::new(e))) | |
} | |
} | |
} | |
); | |
($i:expr, $code:expr, $f:expr) => ( | |
cut!($i, $code, call!($f)); | |
); | |
); | |
macro_rules! c ( | |
($i:expr, $f:expr) => ( | |
{ | |
match $f($i) { | |
std::result::Result::Ok(nom::IResult::Incomplete(x)) => nom::IResult::Incomplete(x), | |
std::result::Result::Ok(nom::IResult::Done(i, o)) => nom::IResult::Done(i, o), | |
std::result::Result::Ok(nom::IResult::Error(e)) => nom::IResult::Error(e), | |
std::result::Result::Err(e) => { | |
return std::result::Result::Err(e) | |
} | |
} | |
} | |
); | |
); | |
n!(pub foo< bool >, | |
chain!( | |
tag!("a") ~ | |
cut!(nom::ErrorKind::Custom(42),tag!("b")) , | |
|| { true } | |
) | |
); | |
n!(pub foos< Vec<bool> >, | |
delimited!( | |
tag!("("), | |
many0!(c!(foo)), | |
tag!(")") | |
) | |
); | |
#[test] | |
fn test_ok() { | |
let r = foos(b"(abab)"); | |
println!("result: {:?}", r); | |
match r { | |
Ok(nom::IResult::Done(_,result)) => assert_eq!(result,vec![true,true]), | |
res => panic!("Oops {:?}.",res) | |
} | |
} | |
#[test] | |
fn test_err() { | |
let r = foos(b"(ac)"); | |
println!("result: {:?}", r); | |
match r { | |
Ok(nom::IResult::Error(nom::Err::Position(kind,_))) => assert_eq!(kind,nom::ErrorKind::Custom(42)), | |
res => panic!("Oops, {:?}",res) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment