Skip to content

Commit

Permalink
feat: Allow ParseError to be used without the StreamOnce constraint
Browse files Browse the repository at this point in the history
This will make mappings over parse errors be possible as desired in #86

BREAKING CHANGE

Changes the type parameters of `ParseError`
  • Loading branch information
Marwes committed Aug 7, 2017
1 parent e79764d commit 520da8e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 119 deletions.
17 changes: 9 additions & 8 deletions src/byte.rs
Expand Up @@ -4,8 +4,8 @@ use std::marker::PhantomData;

use self::ascii::AsciiChar;

use combinator::{satisfy, skip_many, token, tokens, Expected, Satisfy, SkipMany, Token, With};
use primitives::{ConsumedResult, Info, ParseError, Parser, RangeStream, Stream};
use combinator::{Expected, satisfy, Satisfy, skip_many, SkipMany, token, Token, tokens, With};
use primitives::{ConsumedResult, Info, Parser, StreamError, Stream};
use range::take;

/// Parses a byteacter and succeeds if the byteacter is equal to `c`.
Expand Down Expand Up @@ -281,8 +281,8 @@ where
.parse_lazy(input)
.map(|bytes| bytes.as_slice())
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
tokens(|&l, r| l == r, Info::Range(self.0), self.0.iter()).add_error(errors)
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
tokens::<_, _, I>(|&l, r| l == r, Info::Range(self.0), self.0.iter()).add_error(errors)
}
}

Expand Down Expand Up @@ -330,9 +330,9 @@ where
let cmp = &mut self.1;
tokens(|&l, r| cmp(l, r), Info::Range(self.0), self.0).parse_lazy(input)
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
let cmp = &mut self.1;
tokens(|&l, r| cmp(l, r), Info::Range(self.0), self.0.iter()).add_error(errors)
tokens::<_, _, I>(|&l, r| cmp(l, r), Info::Range(self.0), self.0.iter()).add_error(errors)
}
}

Expand Down Expand Up @@ -368,6 +368,7 @@ where
/// Parsers for decoding numbers in big-endian or little-endian order.
pub mod num {
use super::*;
use primitives::RangeStream;

use byteorder::{ByteOrder, BE, LE};

Expand All @@ -392,8 +393,8 @@ pub mod num {
fn parse_lazy(&mut self, input: Self::Input) -> ConsumedResult<Self::Output, Self::Input> {
take(::std::mem::size_of::<Self::Output>()).map(B::$read_name).parse_lazy(input)
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
take(::std::mem::size_of::<Self::Output>()).add_error(errors)
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
take::<I>(::std::mem::size_of::<Self::Output>()).add_error(errors)
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/char.rs
@@ -1,5 +1,5 @@
use primitives::{ConsumedResult, ParseError, Parser, Stream};
use combinator::{satisfy, skip_many, token, tokens, Expected, Satisfy, SkipMany, Token, With};
use primitives::{Parser, StreamError, ConsumedResult, Stream};
use combinator::{Expected, satisfy, Satisfy, skip_many, SkipMany, token, Token, tokens, With};
use std::marker::PhantomData;

/// Parses a character and succeeds if the character is equal to `c`.
Expand Down Expand Up @@ -308,8 +308,8 @@ where
.parse_lazy(input)
.map(|_| self.0)
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
tokens(eq, self.0.into(), self.0.chars()).add_error(errors)
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
tokens::<_, _, I>(eq, self.0.into(), self.0.chars()).add_error(errors)
}
}

Expand Down Expand Up @@ -351,8 +351,8 @@ where
.parse_lazy(input)
.map(|_| self.0)
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
tokens(&mut self.1, self.0.into(), self.0.chars()).add_error(errors)
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
tokens::<_, _, I>(&mut self.1, self.0.into(), self.0.chars()).add_error(errors)
}
}

Expand Down
65 changes: 33 additions & 32 deletions src/combinator.rs
@@ -1,7 +1,7 @@
use std::iter::FromIterator;
use std::marker::PhantomData;
use primitives::{Consumed, ConsumedResult, Error, Info, ParseError, ParseResult, Parser, Stream,
StreamOnce};
use primitives::{Info, Parser, ParseResult, ConsumedResult, ParseError, StreamError, Stream,
StreamOnce, Error, Consumed};
use primitives::FastResult::*;

macro_rules! impl_parser {
Expand All @@ -25,7 +25,7 @@ macro_rules! impl_parser {
input: Self::Input) -> ConsumedResult<Self::Output, Self::Input> {
self.0.parse_lazy(input)
}
fn add_error(&mut self, error: &mut ParseError<Self::Input>) {
fn add_error(&mut self, error: &mut StreamError<Self::Input>) {
self.0.add_error(error)
}
}
Expand Down Expand Up @@ -212,7 +212,7 @@ where
fn parse_lazy(&mut self, input: I) -> ConsumedResult<I::Item, I> {
satisfy_impl(input, |c| if c == self.c { Some(c) } else { None })
}
fn add_error(&mut self, error: &mut ParseError<Self::Input>) {
fn add_error(&mut self, error: &mut StreamError<Self::Input>) {
error
.errors
.push(Error::Expected(Info::Token(self.c.clone())));
Expand Down Expand Up @@ -302,7 +302,7 @@ where
EmptyOk((self.tokens.clone(), input))
}
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
errors.add_error(Error::Expected(self.expected.clone()));
}
}
Expand Down Expand Up @@ -425,7 +425,7 @@ where
Some(err) => err,
})
}
fn add_error(&mut self, error: &mut ParseError<Self::Input>) {
fn add_error(&mut self, error: &mut StreamError<Self::Input>) {
for p in self.0.as_mut() {
p.add_error(error);
}
Expand Down Expand Up @@ -455,7 +455,7 @@ where
satisfy(|c| self.tokens.clone().into_iter().any(|t| t == c)).parse_lazy(input)
}

fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
for expected in self.tokens.clone() {
errors.add_error(Error::Expected(Info::Token(expected)));
}
Expand Down Expand Up @@ -567,7 +567,7 @@ where
iter.into_result_fast(value)
}

fn add_error(&mut self, error: &mut ParseError<Self::Input>) {
fn add_error(&mut self, error: &mut StreamError<Self::Input>) {
self.parser.add_error(error)
}
}
Expand Down Expand Up @@ -801,7 +801,7 @@ where
fn parse_lazy(&mut self, input: I) -> ConsumedResult<(), I> {
EmptyErr(ParseError::empty(input.position()))
}
fn add_error(&mut self, error: &mut ParseError<Self::Input>) {
fn add_error(&mut self, error: &mut StreamError<Self::Input>) {
error.errors.push(Error::Unexpected(self.0.clone()));
}
}
Expand Down Expand Up @@ -913,7 +913,7 @@ where
}
}

fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
errors.add_error(Error::Expected("end of input".into()))
}
}
Expand Down Expand Up @@ -954,7 +954,7 @@ pub struct Iter<P: Parser> {
enum State<I: StreamOnce> {
Ok,
EmptyErr,
ConsumedErr(ParseError<I>),
ConsumedErr(StreamError<I>),
}

impl<P: Parser> Iter<P> {
Expand Down Expand Up @@ -1078,7 +1078,7 @@ where
let result = Some(first).into_iter().chain(iter.by_ref()).collect();
iter.into_result_fast(result)
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors)
}
}
Expand Down Expand Up @@ -1183,7 +1183,7 @@ where
.parse_lazy(input)
}

fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.parser.add_error(errors)
}
}
Expand Down Expand Up @@ -1248,7 +1248,7 @@ where
})
}

fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.parser.add_error(errors)
}
}
Expand Down Expand Up @@ -1319,7 +1319,7 @@ where
.parse_lazy(input)
}

fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.parser.add_error(errors)
}
}
Expand Down Expand Up @@ -1391,7 +1391,7 @@ where
})
}

fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.parser.add_error(errors)
}
}
Expand Down Expand Up @@ -1610,7 +1610,7 @@ where
Ok((l, input)).into()
}

fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors)
}
}
Expand Down Expand Up @@ -1674,7 +1674,7 @@ where
}
Ok((l, input)).into()
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors)
}
}
Expand Down Expand Up @@ -1759,7 +1759,7 @@ where
EmptyOk((o, input))
}

fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors);
}
}
Expand Down Expand Up @@ -1805,7 +1805,7 @@ where
fn parse_lazy(&mut self, input: I) -> ConsumedResult<Self::Output, I> {
self.0.parse_lazy(input).map(|(_, b)| b)
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors)
}
}
Expand Down Expand Up @@ -1839,7 +1839,7 @@ where
fn parse_lazy(&mut self, input: I) -> ConsumedResult<Self::Output, I> {
self.0.parse_lazy(input).map(|(a, _)| a)
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors)
}
}
Expand Down Expand Up @@ -1885,7 +1885,7 @@ where
}
}

fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors);
errors.add_error(Error::Message(self.1.clone()));
}
Expand Down Expand Up @@ -1932,7 +1932,7 @@ where
},
}
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors);
self.1.add_error(errors);
}
Expand Down Expand Up @@ -1972,7 +1972,7 @@ where
EmptyErr(err) => EmptyErr(err),
}
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors);
}
}
Expand All @@ -1995,7 +1995,7 @@ impl<I, A, B, P, F> Parser for FlatMap<P, F>
where
I: Stream,
P: Parser<Input = I, Output = A>,
F: FnMut(A) -> Result<B, ParseError<I>>,
F: FnMut(A) -> Result<B, StreamError<I>>,
{
type Input = I;
type Output = B;
Expand All @@ -2014,7 +2014,7 @@ where
ConsumedErr(err) => ConsumedErr(err),
}
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors);
}
}
Expand All @@ -2026,7 +2026,7 @@ where
pub fn flat_map<P, F, B>(p: P, f: F) -> FlatMap<P, F>
where
P: Parser,
F: FnMut(P::Output) -> Result<B, ParseError<P::Input>>,
F: FnMut(P::Output) -> Result<B, StreamError<P::Input>>,
{
FlatMap(p, f)
}
Expand Down Expand Up @@ -2059,7 +2059,7 @@ where
ConsumedErr(err) => ConsumedErr(err),
}
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors);
}
}
Expand Down Expand Up @@ -2096,7 +2096,7 @@ where
self.0.parse_lazy(input)
}

fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
let start = errors.errors.len();
self.0.add_error(errors);
// Replace all expected errors that were added from the previous add_error
Expand Down Expand Up @@ -2155,7 +2155,7 @@ where
ConsumedErr(err) => ConsumedErr(err),
}
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors);
}
}
Expand Down Expand Up @@ -2232,7 +2232,7 @@ macro_rules! tuple_parser {
EmptyOk((($h, $($id),+), input))
}
}
fn add_error(&mut self, errors: &mut ParseError<Self::Input>) {
fn add_error(&mut self, errors: &mut StreamError<Self::Input>) {
self.0.add_error(errors);
}
}
Expand Down Expand Up @@ -2527,7 +2527,8 @@ mod tests {
#[test]
fn sep_by_consumed_error() {
let mut parser2 = sep_by((letter(), letter()), token(','));
let result_err: Result<(Vec<(char, char)>, &str), ParseError<&str>> = parser2.parse("a,bc");
let result_err: Result<(Vec<(char, char)>, &str), ParseError<usize, char, &str>> =
parser2.parse("a,bc");
assert!(result_err.is_err());
}

Expand Down

0 comments on commit 520da8e

Please sign in to comment.