mirror of
https://github.com/zaphar/abortable_parser.git
synced 2025-07-21 20:29:49 -04:00
REFACTOR: Split the macros into a combinators and a matchers namespace.
This commit is contained in:
parent
b8534bc717
commit
5163a1ff5e
@ -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 ().
|
@ -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
29
src/matchers.rs
Normal 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,
|
||||
))
|
||||
}
|
||||
}};
|
||||
}
|
25
src/test.rs
25
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, C>(i: I) -> Result<I, String, String>
|
||||
where I: InputIter<Item=C>, C: Debug + Display {
|
||||
fn will_fail<I, C>(i: I) -> Result<I, String, String>
|
||||
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> {
|
||||
fn parse_byte<'a, I>(mut i: I) -> Result<I, u8, String>
|
||||
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> {
|
||||
fn parse_three<'a, I>(i: I) -> Result<I, String, String>
|
||||
where
|
||||
I: InputIter<Item = &'a u8>,
|
||||
{
|
||||
let mut _i = i.clone();
|
||||
let mut out = String::new();
|
||||
loop {
|
||||
|
Loading…
x
Reference in New Issue
Block a user