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.
|
//! 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.
|
/// 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
|
where
|
||||||
I: InputIter,
|
I: InputIter,
|
||||||
{
|
{
|
||||||
match result {
|
match result {
|
||||||
Result::Complete(i, _) => Result::Fail(Error::new("Matched on input when we shouldn't have.".to_string(), &i)),
|
Result::Complete(i, _) => Result::Fail(Error::new(
|
||||||
Result::Abort(e) => Result::Abort(e),
|
"Matched on input when we shouldn't have.".to_string(),
|
||||||
Result::Incomplete(offset) => Result::Incomplete(offset),
|
&i,
|
||||||
Result::Fail(_) => Result::Complete(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.
|
/// 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.
|
/// The `must!` macro provided syntactice sugar for using this combinator.
|
||||||
pub fn must<I, O>(result: Result<I, O>) -> Result<I, O>
|
pub fn must<I, O>(result: Result<I, O>) -> Result<I, O>
|
||||||
where
|
where
|
||||||
I: InputIter
|
I: InputIter,
|
||||||
{
|
{
|
||||||
match result {
|
match result {
|
||||||
Result::Complete(i, o) => Result::Complete(i, o),
|
Result::Complete(i, o) => Result::Complete(i, o),
|
||||||
@ -117,7 +120,6 @@ where
|
|||||||
Result::Fail(e) => Result::Abort(e),
|
Result::Fail(e) => Result::Abort(e),
|
||||||
Result::Abort(e) => Result::Abort(e),
|
Result::Abort(e) => Result::Abort(e),
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Turns `Result::Fail` into `Result::Abort`.
|
/// Turns `Result::Fail` into `Result::Abort`.
|
||||||
@ -181,14 +183,14 @@ macro_rules! wrap_err {
|
|||||||
/// The `trap!` macro provides syntactic sugar for using this combinator.
|
/// The `trap!` macro provides syntactic sugar for using this combinator.
|
||||||
pub fn trap<I, O>(result: Result<I, O>) -> Result<I, O>
|
pub fn trap<I, O>(result: Result<I, O>) -> Result<I, O>
|
||||||
where
|
where
|
||||||
I: InputIter
|
I: InputIter,
|
||||||
{
|
{
|
||||||
match result {
|
match result {
|
||||||
Result::Complete(i, o) => Result::Complete(i, o),
|
Result::Complete(i, o) => Result::Complete(i, o),
|
||||||
Result::Incomplete(offset) => Result::Incomplete(offset),
|
Result::Incomplete(offset) => Result::Incomplete(offset),
|
||||||
Result::Fail(e) => Result::Fail(e),
|
Result::Fail(e) => Result::Fail(e),
|
||||||
Result::Abort(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`
|
/// Turns `Result::Abort` into `Result::Fail` allowing you to trap and then convert any `Result::Abort`
|
||||||
@ -227,11 +229,11 @@ where
|
|||||||
M: Into<String>,
|
M: Into<String>,
|
||||||
{
|
{
|
||||||
match result {
|
match result {
|
||||||
Result::Complete(i, o) => Result::Complete(i, o),
|
Result::Complete(i, o) => Result::Complete(i, o),
|
||||||
Result::Incomplete(ref offset) => Result::Abort(Error::new(msg, offset)),
|
Result::Incomplete(ref offset) => Result::Abort(Error::new(msg, offset)),
|
||||||
Result::Fail(e) => Result::Abort(e),
|
Result::Fail(e) => Result::Abort(e),
|
||||||
Result::Abort(e) => Result::Abort(e),
|
Result::Abort(e) => Result::Abort(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Turns `Result::Fail` and `Result::Incomplete` into `Result::Abort`.
|
/// Turns `Result::Fail` and `Result::Incomplete` into `Result::Abort`.
|
||||||
@ -474,20 +476,14 @@ where
|
|||||||
I: InputIter,
|
I: InputIter,
|
||||||
{
|
{
|
||||||
match result {
|
match result {
|
||||||
Result::Complete(i, o) => {
|
Result::Complete(i, o) => Result::Complete(i, Some(o)),
|
||||||
Result::Complete(i, Some(o))
|
// Incomplete could still work possibly parse.
|
||||||
}
|
Result::Incomplete(i) => Result::Incomplete(i),
|
||||||
// Incomplete could still work possibly parse.
|
// Fail just means it didn't match.
|
||||||
Result::Incomplete(i) => {
|
Result::Fail(_) => Result::Complete(iter, None),
|
||||||
Result::Incomplete(i)
|
// Aborts are hard failures that the parser can't recover from.
|
||||||
}
|
Result::Abort(e) => Result::Abort(e),
|
||||||
// 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
|
/// 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.
|
/// 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() {
|
match i.next() {
|
||||||
Some(b) => {
|
Some(b) => match b {
|
||||||
match b {
|
b'\r' => Result::Complete(i, *b),
|
||||||
b'\r' => Result::Complete(i, *b),
|
b'\n' => Result::Complete(i, *b),
|
||||||
b'\n' => Result::Complete(i, *b),
|
b'\t' => Result::Complete(i, *b),
|
||||||
b'\t' => Result::Complete(i, *b),
|
b' ' => Result::Complete(i, *b),
|
||||||
b' ' => Result::Complete(i, *b),
|
_ => Result::Fail(Error::new("Not whitespace", &i)),
|
||||||
_ => Result::Fail(Error::new("Not whitespace", &i)),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
None => {
|
None => Result::Fail(Error::new("Unexpected End Of Input", &i)),
|
||||||
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!(
|
do_each!(
|
||||||
proto => until!(text_token!("://")),
|
proto => until!(text_token!("://")),
|
||||||
_ => must!(text_token!("://")),
|
_ => must!(text_token!("://")),
|
||||||
@ -9,18 +10,18 @@
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
make_fn!(domain<StrIter, &str>,
|
make_fn!(domain<StrIter, &str>,
|
||||||
until!(either!(
|
until!(either!(
|
||||||
discard!(text_token!("/")),
|
discard!(text_token!("/")),
|
||||||
discard!(ascii_ws),
|
discard!(ascii_ws),
|
||||||
eoi))
|
eoi))
|
||||||
);
|
);
|
||||||
|
|
||||||
make_fn!(path<StrIter, &str>,
|
make_fn!(path<StrIter, &str>,
|
||||||
until!(either!(discard!(ascii_ws), eoi))
|
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!(
|
do_each!(
|
||||||
protocol => optional!(proto),
|
protocol => optional!(proto),
|
||||||
domain => optional!(domain),
|
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> {
|
impl<'a, T: Debug + 'a> Span<&'a [T]> for SliceIter<'a, T> {
|
||||||
fn span(&self, idx: SpanRange) -> &'a [T] {
|
fn span(&self, idx: SpanRange) -> &'a [T] {
|
||||||
match idx {
|
match idx {
|
||||||
SpanRange::Range(r) => self.source.index(r),
|
SpanRange::Range(r) => self.source.index(r),
|
||||||
SpanRange::RangeTo(r) => self.source.index(r),
|
SpanRange::RangeTo(r) => self.source.index(r),
|
||||||
SpanRange::RangeFrom(r) => self.source.index(r),
|
SpanRange::RangeFrom(r) => self.source.index(r),
|
||||||
SpanRange::RangeFull(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 {
|
fn from(source: &'a Vec<T>) -> Self {
|
||||||
SliceIter::new(source.as_slice())
|
SliceIter::new(source.as_slice())
|
||||||
}
|
}
|
||||||
@ -140,10 +140,10 @@ use std::ops::Index;
|
|||||||
impl<'a> Span<&'a str> for StrIter<'a> {
|
impl<'a> Span<&'a str> for StrIter<'a> {
|
||||||
fn span(&self, idx: SpanRange) -> &'a str {
|
fn span(&self, idx: SpanRange) -> &'a str {
|
||||||
match idx {
|
match idx {
|
||||||
SpanRange::Range(r) => self.source.index(r),
|
SpanRange::Range(r) => self.source.index(r),
|
||||||
SpanRange::RangeTo(r) => self.source.index(r),
|
SpanRange::RangeTo(r) => self.source.index(r),
|
||||||
SpanRange::RangeFrom(r) => self.source.index(r),
|
SpanRange::RangeFrom(r) => self.source.index(r),
|
||||||
SpanRange::RangeFull(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
|
pub fn new<S, M>(msg: M, offset: &S) -> Self
|
||||||
where
|
where
|
||||||
S: Offsetable,
|
S: Offsetable,
|
||||||
M: Into<String> {
|
M: Into<String>,
|
||||||
|
{
|
||||||
Error {
|
Error {
|
||||||
msg: msg.into(),
|
msg: msg.into(),
|
||||||
offset: offset.get_offset(),
|
offset: offset.get_offset(),
|
||||||
@ -103,8 +104,8 @@ impl Error {
|
|||||||
pub fn caused_by<S, M>(msg: M, offset: &S, cause: Self) -> Self
|
pub fn caused_by<S, M>(msg: M, offset: &S, cause: Self) -> Self
|
||||||
where
|
where
|
||||||
S: Offsetable,
|
S: Offsetable,
|
||||||
M: Into<String> {
|
M: Into<String>,
|
||||||
|
{
|
||||||
Error {
|
Error {
|
||||||
msg: msg.into(),
|
msg: msg.into(),
|
||||||
offset: offset.get_offset(),
|
offset: offset.get_offset(),
|
||||||
@ -189,14 +190,14 @@ impl<I: InputIter, O> Result<I, O> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub use iter::SliceIter;
|
|
||||||
pub use combinators::*;
|
pub use combinators::*;
|
||||||
|
pub use iter::SliceIter;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod combinators;
|
pub mod combinators;
|
||||||
pub mod iter;
|
pub mod iter;
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test;
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod integration_tests;
|
mod integration_tests;
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test;
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use std::fmt::{Debug, Display};
|
use std::fmt::{Debug, Display};
|
||||||
|
|
||||||
use super::{InputIter, Offsetable, Result};
|
use super::{InputIter, Offsetable, Result};
|
||||||
use iter::{StrIter, SliceIter};
|
|
||||||
use combinators::*;
|
use combinators::*;
|
||||||
|
use iter::{SliceIter, StrIter};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_slice_iter() {
|
fn test_slice_iter() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user