REFACTOR: Split the macros into a combinators and a matchers namespace.

This commit is contained in:
Jeremy Wall 2018-09-05 22:43:15 -05:00
parent b8534bc717
commit 5163a1ff5e
4 changed files with 50 additions and 38 deletions

View File

@ -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 ().

View File

@ -125,7 +125,9 @@ impl<I: InputIter, O, E: Display + Debug> Result<I, O, E> {
pub use iter::SliceIter;
#[macro_use]
pub mod macros;
pub mod combinators;
#[macro_use]
pub mod matchers;
pub mod iter;
#[cfg(test)]

29
src/matchers.rs Normal file
View File

@ -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,
))
}
}};
}

View File

@ -1,4 +1,4 @@
use std::fmt::{Display, Debug};
use std::fmt::{Debug, Display};
use super::{InputIter, Offsetable, Result};
use iter::SliceIter;
@ -34,12 +34,17 @@ fn test_slice_iter() {
}
fn will_fail<I, C>(i: I) -> Result<I, String, String>
where I: InputIter<Item=C>, C: Debug + Display {
where
I: InputIter<Item = C>,
C: Debug + Display,
{
Result::Fail(super::Error::new("AAAAHHH!!!".to_string(), &i))
}
fn parse_byte<'a, I>(mut i: I) -> Result<I, u8, String>
where I: InputIter<Item=&'a u8> {
where
I: InputIter<Item = &'a u8>,
{
match i.next() {
Some(b) => Result::Complete(i, *b),
None => Result::Incomplete(i.get_offset()),
@ -47,12 +52,16 @@ where I: InputIter<Item=&'a u8> {
}
fn will_not_complete<'a, I>(_: I) -> Result<I, String, String>
where I: InputIter<Item=&'a u8> {
where
I: InputIter<Item = &'a u8>,
{
Result::Incomplete(0)
}
fn parse_three<'a, I>(i: I) -> Result<I, String, String>
where I: InputIter<Item=&'a u8> {
where
I: InputIter<Item = &'a u8>,
{
let mut _i = i.clone();
let mut out = String::new();
loop {