From 5163a1ff5effaef20220e232e2b25665f52d1004 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Wed, 5 Sep 2018 22:43:15 -0500 Subject: [PATCH] REFACTOR: Split the macros into a combinators and a matchers namespace. --- src/{macros.rs => combinators.rs} | 30 +----------------------------- src/lib.rs | 4 +++- src/matchers.rs | 29 +++++++++++++++++++++++++++++ src/test.rs | 25 +++++++++++++++++-------- 4 files changed, 50 insertions(+), 38 deletions(-) rename src/{macros.rs => combinators.rs} (93%) create mode 100644 src/matchers.rs diff --git a/src/macros.rs b/src/combinators.rs similarity index 93% rename from src/macros.rs rename to src/combinators.rs index c3b6f59..b3e858e 100644 --- a/src/macros.rs +++ b/src/combinators.rs @@ -1,32 +1,4 @@ -//! Contains the helper macros for abortable-parser. - -/// Convenience macro for looking for a specific text token in a byte input stream. -#[macro_export] -macro_rules! text_token { - ($i:expr, $e:expr) => {{ - use $crate::Error; - use $crate::Result; - let mut _i = $i.clone(); - let mut count = 0; - for expected in $e.bytes() { - let item = match _i.next() { - Some(item) => item, - None => break, - }; - if item == &expected { - count += 1; - } - } - if count == $e.len() { - Result::Complete(_i.clone(), $e) - } else { - Result::Fail(Error::new(format!("Expected {} but didn't get it.", $e), &$i)) - } - }}; -} - - -// FIXME(jwall): We need peek!. +//! Contains combinators that can assemble other mathers or combinators into more complex grammars. /// Turns a matcher into it's inverse, only succeeding if the the matcher returns a Fail. /// Does not consume it's input and only returns (). diff --git a/src/lib.rs b/src/lib.rs index efd5b42..e9339c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -125,7 +125,9 @@ impl Result { pub use iter::SliceIter; #[macro_use] -pub mod macros; +pub mod combinators; +#[macro_use] +pub mod matchers; pub mod iter; #[cfg(test)] diff --git a/src/matchers.rs b/src/matchers.rs new file mode 100644 index 0000000..c985a8d --- /dev/null +++ b/src/matchers.rs @@ -0,0 +1,29 @@ +//! Contains matchers for matching specific patterns or tokens. + +/// Convenience macro for looking for a specific text token in a byte input stream. +#[macro_export] +macro_rules! text_token { + ($i:expr, $e:expr) => {{ + use $crate::Error; + use $crate::Result; + let mut _i = $i.clone(); + let mut count = 0; + for expected in $e.bytes() { + let item = match _i.next() { + Some(item) => item, + None => break, + }; + if item == &expected { + count += 1; + } + } + if count == $e.len() { + Result::Complete(_i.clone(), $e) + } else { + Result::Fail(Error::new( + format!("Expected {} but didn't get it.", $e), + &$i, + )) + } + }}; +} diff --git a/src/test.rs b/src/test.rs index b27270a..9956874 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,4 +1,4 @@ -use std::fmt::{Display, Debug}; +use std::fmt::{Debug, Display}; use super::{InputIter, Offsetable, Result}; use iter::SliceIter; @@ -33,13 +33,18 @@ fn test_slice_iter() { assert_eq!('o' as u8, out[2]); } -fn will_fail(i: I) -> Result -where I: InputIter, C: Debug + Display { +fn will_fail(i: I) -> Result +where + I: InputIter, + C: Debug + Display, +{ Result::Fail(super::Error::new("AAAAHHH!!!".to_string(), &i)) } -fn parse_byte<'a, I>(mut i: I) -> Result -where I: InputIter { +fn parse_byte<'a, I>(mut i: I) -> Result +where + I: InputIter, +{ match i.next() { Some(b) => Result::Complete(i, *b), None => Result::Incomplete(i.get_offset()), @@ -47,12 +52,16 @@ where I: InputIter { } fn will_not_complete<'a, I>(_: I) -> Result -where I: InputIter { +where + I: InputIter, +{ Result::Incomplete(0) } -fn parse_three<'a, I>(i: I) -> Result -where I: InputIter { +fn parse_three<'a, I>(i: I) -> Result +where + I: InputIter, +{ let mut _i = i.clone(); let mut out = String::new(); loop {