mirror of
https://github.com/zaphar/abortable_parser.git
synced 2025-07-22 20:39:50 -04:00
MAINT: cargo fmt
This commit is contained in:
parent
e1698065c6
commit
e833730fbb
@ -1,6 +1,6 @@
|
||||
//! Contains combinators that can assemble other matchers or combinators into more complex grammars.
|
||||
|
||||
use super::{InputIter, Error, Result};
|
||||
use super::{Error, InputIter, Result};
|
||||
|
||||
/// Turns a `Result` to it's inverse.
|
||||
///
|
||||
@ -13,12 +13,15 @@ pub fn not<I, O>(i: I, result: Result<I, O>) -> Result<I, ()>
|
||||
where
|
||||
I: InputIter,
|
||||
{
|
||||
match result {
|
||||
Result::Complete(i, _) => Result::Fail(Error::new("Matched on input when we shouldn't have.".to_string(), &i)),
|
||||
Result::Abort(e) => Result::Abort(e),
|
||||
Result::Incomplete(offset) => Result::Incomplete(offset),
|
||||
Result::Fail(_) => Result::Complete(i, ()),
|
||||
}
|
||||
match result {
|
||||
Result::Complete(i, _) => Result::Fail(Error::new(
|
||||
"Matched on input when we shouldn't have.".to_string(),
|
||||
&i,
|
||||
)),
|
||||
Result::Abort(e) => Result::Abort(e),
|
||||
Result::Incomplete(offset) => Result::Incomplete(offset),
|
||||
Result::Fail(_) => Result::Complete(i, ()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns a matcher into it's inverse, only succeeding if the the matcher returns a Fail.
|
||||
@ -109,7 +112,7 @@ macro_rules! run {
|
||||
/// The `must!` macro provided syntactice sugar for using this combinator.
|
||||
pub fn must<I, O>(result: Result<I, O>) -> Result<I, O>
|
||||
where
|
||||
I: InputIter
|
||||
I: InputIter,
|
||||
{
|
||||
match result {
|
||||
Result::Complete(i, o) => Result::Complete(i, o),
|
||||
@ -117,7 +120,6 @@ where
|
||||
Result::Fail(e) => Result::Abort(e),
|
||||
Result::Abort(e) => Result::Abort(e),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Turns `Result::Fail` into `Result::Abort`.
|
||||
@ -181,14 +183,14 @@ macro_rules! wrap_err {
|
||||
/// The `trap!` macro provides syntactic sugar for using this combinator.
|
||||
pub fn trap<I, O>(result: Result<I, O>) -> Result<I, O>
|
||||
where
|
||||
I: InputIter
|
||||
I: InputIter,
|
||||
{
|
||||
match result {
|
||||
Result::Complete(i, o) => Result::Complete(i, o),
|
||||
Result::Incomplete(offset) => Result::Incomplete(offset),
|
||||
Result::Fail(e) => Result::Fail(e),
|
||||
Result::Abort(e) => Result::Fail(e),
|
||||
}
|
||||
match result {
|
||||
Result::Complete(i, o) => Result::Complete(i, o),
|
||||
Result::Incomplete(offset) => Result::Incomplete(offset),
|
||||
Result::Fail(e) => Result::Fail(e),
|
||||
Result::Abort(e) => Result::Fail(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns `Result::Abort` into `Result::Fail` allowing you to trap and then convert any `Result::Abort`
|
||||
@ -227,11 +229,11 @@ where
|
||||
M: Into<String>,
|
||||
{
|
||||
match result {
|
||||
Result::Complete(i, o) => Result::Complete(i, o),
|
||||
Result::Incomplete(ref offset) => Result::Abort(Error::new(msg, offset)),
|
||||
Result::Fail(e) => Result::Abort(e),
|
||||
Result::Abort(e) => Result::Abort(e),
|
||||
}
|
||||
Result::Complete(i, o) => Result::Complete(i, o),
|
||||
Result::Incomplete(ref offset) => Result::Abort(Error::new(msg, offset)),
|
||||
Result::Fail(e) => Result::Abort(e),
|
||||
Result::Abort(e) => Result::Abort(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns `Result::Fail` and `Result::Incomplete` into `Result::Abort`.
|
||||
@ -474,20 +476,14 @@ where
|
||||
I: InputIter,
|
||||
{
|
||||
match result {
|
||||
Result::Complete(i, o) => {
|
||||
Result::Complete(i, Some(o))
|
||||
}
|
||||
// Incomplete could still work possibly parse.
|
||||
Result::Incomplete(i) => {
|
||||
Result::Incomplete(i)
|
||||
}
|
||||
// Fail just means it didn't match.
|
||||
Result::Fail(_) => {
|
||||
Result::Complete(iter, None)
|
||||
},
|
||||
// Aborts are hard failures that the parser can't recover from.
|
||||
Result::Abort(e) => Result::Abort(e),
|
||||
}
|
||||
Result::Complete(i, o) => Result::Complete(i, Some(o)),
|
||||
// Incomplete could still work possibly parse.
|
||||
Result::Incomplete(i) => Result::Incomplete(i),
|
||||
// Fail just means it didn't match.
|
||||
Result::Fail(_) => Result::Complete(iter, None),
|
||||
// Aborts are hard failures that the parser can't recover from.
|
||||
Result::Abort(e) => Result::Abort(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Treats a sub parser as optional. It returns Some(output) for a successful match
|
||||
@ -697,20 +693,16 @@ macro_rules! discard {
|
||||
}
|
||||
|
||||
/// Matches and returns any ascii charactar whitespace byte.
|
||||
pub fn ascii_ws<'a, I: InputIter<Item=&'a u8>>(mut i: I) -> Result<I, u8> {
|
||||
pub fn ascii_ws<'a, I: InputIter<Item = &'a u8>>(mut i: I) -> Result<I, u8> {
|
||||
match i.next() {
|
||||
Some(b) => {
|
||||
match b {
|
||||
b'\r' => Result::Complete(i, *b),
|
||||
b'\n' => Result::Complete(i, *b),
|
||||
b'\t' => Result::Complete(i, *b),
|
||||
b' ' => Result::Complete(i, *b),
|
||||
_ => Result::Fail(Error::new("Not whitespace", &i)),
|
||||
}
|
||||
Some(b) => match b {
|
||||
b'\r' => Result::Complete(i, *b),
|
||||
b'\n' => Result::Complete(i, *b),
|
||||
b'\t' => Result::Complete(i, *b),
|
||||
b' ' => Result::Complete(i, *b),
|
||||
_ => Result::Fail(Error::new("Not whitespace", &i)),
|
||||
},
|
||||
None => {
|
||||
Result::Fail(Error::new("Unexpected End Of Input", &i))
|
||||
}
|
||||
None => Result::Fail(Error::new("Unexpected End Of Input", &i)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
use iter::StrIter;
|
||||
use super::{Result, eoi, ascii_ws};
|
||||
|
||||
make_fn!(proto<StrIter, &str>,
|
||||
use super::{ascii_ws, eoi, Result};
|
||||
use iter::StrIter;
|
||||
|
||||
make_fn!(proto<StrIter, &str>,
|
||||
do_each!(
|
||||
proto => until!(text_token!("://")),
|
||||
_ => must!(text_token!("://")),
|
||||
@ -9,18 +10,18 @@
|
||||
)
|
||||
);
|
||||
|
||||
make_fn!(domain<StrIter, &str>,
|
||||
make_fn!(domain<StrIter, &str>,
|
||||
until!(either!(
|
||||
discard!(text_token!("/")),
|
||||
discard!(ascii_ws),
|
||||
eoi))
|
||||
);
|
||||
|
||||
make_fn!(path<StrIter, &str>,
|
||||
make_fn!(path<StrIter, &str>,
|
||||
until!(either!(discard!(ascii_ws), eoi))
|
||||
);
|
||||
|
||||
make_fn!(pub url<StrIter, (Option<&str>, Option<&str>, &str)>,
|
||||
make_fn!(pub url<StrIter, (Option<&str>, Option<&str>, &str)>,
|
||||
do_each!(
|
||||
protocol => optional!(proto),
|
||||
domain => optional!(domain),
|
||||
|
18
src/iter.rs
18
src/iter.rs
@ -55,10 +55,10 @@ impl<'a, T: Debug + 'a> InputIter for SliceIter<'a, T> {}
|
||||
impl<'a, T: Debug + 'a> Span<&'a [T]> for SliceIter<'a, T> {
|
||||
fn span(&self, idx: SpanRange) -> &'a [T] {
|
||||
match idx {
|
||||
SpanRange::Range(r) => self.source.index(r),
|
||||
SpanRange::RangeTo(r) => self.source.index(r),
|
||||
SpanRange::RangeFrom(r) => self.source.index(r),
|
||||
SpanRange::RangeFull(r) => self.source.index(r),
|
||||
SpanRange::Range(r) => self.source.index(r),
|
||||
SpanRange::RangeTo(r) => self.source.index(r),
|
||||
SpanRange::RangeFrom(r) => self.source.index(r),
|
||||
SpanRange::RangeFull(r) => self.source.index(r),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -75,7 +75,7 @@ impl<'a, T: Debug> From<&'a [T]> for SliceIter<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl <'a, T: Debug> From<&'a Vec<T>> for SliceIter<'a, T> {
|
||||
impl<'a, T: Debug> From<&'a Vec<T>> for SliceIter<'a, T> {
|
||||
fn from(source: &'a Vec<T>) -> Self {
|
||||
SliceIter::new(source.as_slice())
|
||||
}
|
||||
@ -140,10 +140,10 @@ use std::ops::Index;
|
||||
impl<'a> Span<&'a str> for StrIter<'a> {
|
||||
fn span(&self, idx: SpanRange) -> &'a str {
|
||||
match idx {
|
||||
SpanRange::Range(r) => self.source.index(r),
|
||||
SpanRange::RangeTo(r) => self.source.index(r),
|
||||
SpanRange::RangeFrom(r) => self.source.index(r),
|
||||
SpanRange::RangeFull(r) => self.source.index(r),
|
||||
SpanRange::Range(r) => self.source.index(r),
|
||||
SpanRange::RangeTo(r) => self.source.index(r),
|
||||
SpanRange::RangeFrom(r) => self.source.index(r),
|
||||
SpanRange::RangeFull(r) => self.source.index(r),
|
||||
}
|
||||
}
|
||||
}
|
13
src/lib.rs
13
src/lib.rs
@ -91,7 +91,8 @@ impl Error {
|
||||
pub fn new<S, M>(msg: M, offset: &S) -> Self
|
||||
where
|
||||
S: Offsetable,
|
||||
M: Into<String> {
|
||||
M: Into<String>,
|
||||
{
|
||||
Error {
|
||||
msg: msg.into(),
|
||||
offset: offset.get_offset(),
|
||||
@ -103,8 +104,8 @@ impl Error {
|
||||
pub fn caused_by<S, M>(msg: M, offset: &S, cause: Self) -> Self
|
||||
where
|
||||
S: Offsetable,
|
||||
M: Into<String> {
|
||||
|
||||
M: Into<String>,
|
||||
{
|
||||
Error {
|
||||
msg: msg.into(),
|
||||
offset: offset.get_offset(),
|
||||
@ -189,14 +190,14 @@ impl<I: InputIter, O> Result<I, O> {
|
||||
}
|
||||
}
|
||||
|
||||
pub use iter::SliceIter;
|
||||
pub use combinators::*;
|
||||
pub use iter::SliceIter;
|
||||
|
||||
#[macro_use]
|
||||
pub mod combinators;
|
||||
pub mod iter;
|
||||
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
#[cfg(test)]
|
||||
mod integration_tests;
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
@ -1,8 +1,8 @@
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
use super::{InputIter, Offsetable, Result};
|
||||
use iter::{StrIter, SliceIter};
|
||||
use combinators::*;
|
||||
use iter::{SliceIter, StrIter};
|
||||
|
||||
#[test]
|
||||
fn test_slice_iter() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user