Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
J-F-Liu committed Dec 7, 2018
1 parent c1c6441 commit 23fb639
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/char_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub fn alphanum(term: u8) -> bool {
/// Recognises a hexadecimal digit, `0-9a-fA-F`.
#[inline]
pub fn hex_digit(term: u8) -> bool {
(term >= 0x30 && term <= 0x39) ||
(term >= 0x41 && term <= 0x46) ||
(term >= 0x61 && term <= 0x66)
(term >= 0x30 && term <= 0x39)
|| (term >= 0x41 && term <= 0x46)
|| (term >= 0x61 && term <= 0x66)
}

/// Recognises an octal digit, `0-7`.
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod result;
pub mod range;
mod result;
pub mod set;

/// Constains predefined parsers and combinators.
Expand All @@ -8,7 +8,7 @@ pub mod parser;
/// Utility functions to recognize char class of byte value.
pub mod char_class;

pub use crate::result::{Result, Error};
pub use crate::result::{Error, Result};

/// Parser type, `Parser<I, O>` is alias of `parser::Parser<'static, I, O>`.
pub type Parser<I, O> = parser::Parser<'static, I, O>;
6 changes: 2 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'a, I, O> Parser<'a, I, O> {
results
.borrow_mut()
.entry(key)
.or_insert_with(||(self.method)(input, start))
.or_insert_with(|| (self.method)(input, start))
.clone()
})
}
Expand Down Expand Up @@ -508,9 +508,7 @@ impl<'a, I: Copy + 'a, O: 'a, U: 'a> Mul<Parser<'a, I, U>> for Parser<'a, I, O>
}

/// Chain two passers where the second parser depends on the first's result.
impl<'a, I: Copy, O: 'a, U: 'a, F: Fn(O) -> Parser<'a, I, U> + 'a> Shr<F>
for Parser<'a, I, O>
{
impl<'a, I: Copy, O: 'a, U: 'a, F: Fn(O) -> Parser<'a, I, U> + 'a> Shr<F> for Parser<'a, I, O> {
type Output = Parser<'a, I, U>;

fn shr(self, other: F) -> Self::Output {
Expand Down
43 changes: 31 additions & 12 deletions src/range.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

use std::ops::{Range, RangeFrom, RangeTo, RangeFull};
use std::ops::{Range, RangeFrom, RangeFull, RangeTo};

pub enum Bound<'a, T: 'a> {
Excluded(&'a T),
Expand All @@ -14,26 +13,46 @@ pub trait RangeArgument<T> {
}

impl<T> RangeArgument<T> for Range<T> {
fn start(&self) -> Bound<T> { Included(&self.start) }
fn end(&self) -> Bound<T> { Excluded(&self.end) }
fn start(&self) -> Bound<T> {
Included(&self.start)
}
fn end(&self) -> Bound<T> {
Excluded(&self.end)
}
}

impl<T> RangeArgument<T> for RangeFrom<T> {
fn start(&self) -> Bound<T> { Included(&self.start) }
fn end(&self) -> Bound<T> { Unbounded }
fn start(&self) -> Bound<T> {
Included(&self.start)
}
fn end(&self) -> Bound<T> {
Unbounded
}
}

impl<T> RangeArgument<T> for RangeTo<T> {
fn start(&self) -> Bound<T> { Unbounded }
fn end(&self) -> Bound<T> { Excluded(&self.end) }
fn start(&self) -> Bound<T> {
Unbounded
}
fn end(&self) -> Bound<T> {
Excluded(&self.end)
}
}

impl<T> RangeArgument<T> for RangeFull {
fn start(&self) -> Bound<T> { Unbounded }
fn end(&self) -> Bound<T> { Unbounded }
fn start(&self) -> Bound<T> {
Unbounded
}
fn end(&self) -> Bound<T> {
Unbounded
}
}

impl RangeArgument<usize> for usize {
fn start(&self) -> Bound<usize> { Included(self) }
fn end(&self) -> Bound<usize> { Included(self) }
fn start(&self) -> Bound<usize> {
Included(self)
}
fn end(&self) -> Bound<usize> {
Included(self)
}
}
60 changes: 43 additions & 17 deletions src/result.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
use std::fmt::{self, Display};
use std::error;
use std::fmt::{self, Display};

/// Parser error.
#[derive(Debug, PartialEq, Clone)]
pub enum Error {
Incomplete,
Mismatch { message: String, position: usize },
Conversion { message: String, position: usize },
Expect { message: String, position: usize, inner: Box<Error> },
Custom { message: String, position: usize, inner: Option<Box<Error>> },
Mismatch {
message: String,
position: usize,
},
Conversion {
message: String,
position: usize,
},
Expect {
message: String,
position: usize,
inner: Box<Error>,
},
Custom {
message: String,
position: usize,
inner: Option<Box<Error>>,
},
}

impl error::Error for Error {
Expand All @@ -20,18 +34,30 @@ impl error::Error for Error {
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Incomplete =>
write!(f, "Incomplete"),
Error::Mismatch { ref message, ref position } =>
write!(f, "Mismatch at {}: {}", position, message),
Error::Conversion { ref message, ref position } =>
write!(f, "Conversion failed at {}: {}", position, message),
Error::Expect { ref message, ref position, ref inner } =>
write!(f, "{} at {}: {}", message, position, inner),
Error::Custom { ref message, ref position, inner: Some(ref inner) } =>
write!(f, "{} at {}, (inner: {})", message, position, inner),
Error::Custom { ref message, ref position, inner: None } =>
write!(f, "{} at {}", message, position),
Error::Incomplete => write!(f, "Incomplete"),
Error::Mismatch {
ref message,
ref position,
} => write!(f, "Mismatch at {}: {}", position, message),
Error::Conversion {
ref message,
ref position,
} => write!(f, "Conversion failed at {}: {}", position, message),
Error::Expect {
ref message,
ref position,
ref inner,
} => write!(f, "{} at {}: {}", message, position, inner),
Error::Custom {
ref message,
ref position,
inner: Some(ref inner),
} => write!(f, "{} at {}, (inner: {})", message, position, inner),
Error::Custom {
ref message,
ref position,
inner: None,
} => write!(f, "{} at {}", message, position),
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cmp::{PartialEq, PartialOrd};
use std::ops::{Range, RangeFrom, RangeTo, RangeFull};
use std::ops::{Range, RangeFrom, RangeFull, RangeTo};
use std::str;

/// Set relationship.
Expand Down Expand Up @@ -57,8 +57,7 @@ impl<T> Set<T> for RangeFull {
}
}

macro_rules! impl_set_for_array
{
macro_rules! impl_set_for_array {
($n:expr) => {
impl Set<u8> for [u8; $n] {
fn contains(&self, elem: &u8) -> bool {
Expand Down
7 changes: 5 additions & 2 deletions tests/list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate pom;

use pom::Parser;
use pom::parser::*;
use pom::Parser;

fn spaces() -> Parser<u8, ()> {
one_of(b" ").repeat(1..).discard()
Expand All @@ -21,5 +21,8 @@ fn test_list() {
assert_eq!(works().parse(one), Ok(vec![b'a', b'b', b'c']));

let two = b"a and b and c and ";
assert_eq!(dangle().parse(two), Ok((vec![b'a', b'b', b'c'], &b" and"[..])));
assert_eq!(
dangle().parse(two),
Ok((vec![b'a', b'b', b'c'], &b" and"[..]))
);
}

0 comments on commit 23fb639

Please sign in to comment.