diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 184 |
1 files changed, 41 insertions, 143 deletions
@@ -3,7 +3,6 @@ use core::{ clone::Clone, cmp::PartialEq, - fmt, iter::{IntoIterator, Iterator}, marker::Sized, ops::Range, @@ -24,28 +23,6 @@ pub type ParserOk<I, O> = (InputIter<I>, O); pub type ParserResult<I, O, OM = Emit, EM = Emit> = Result<ParserOk<I, <OM as Mode>::Output<O>>, <EM as Mode>::Output<InputIter<I>>>; -pub trait Trace<I: Input> { - fn trace(parser: &str, input: InputIter<I>); -} - -pub struct DebugTracer; - -impl<I> Trace<I> for DebugTracer -where - I: Input + fmt::Debug, -{ - fn trace(parser: &str, input: InputIter<I>) { - eprintln!("{}:{:?}", parser, input) - } -} - -impl<I> Trace<I> for () -where - I: Input, -{ - fn trace(_: &str, _: InputIter<I>) {} -} - #[derive(Debug)] pub enum ParserFinishedError<I: Input> { Err(InputIter<I>), @@ -55,21 +32,14 @@ pub enum ParserFinishedError<I: Input> { pub trait Parser<I: Input>: Sized { type Output; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM>; + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM>; fn parse(&self, it: InputIter<I>) -> ParserResult<I, Self::Output> { - self.run::<Emit, Emit, ()>(it) + self.run::<Emit, Emit>(it) } fn check(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, Check, Emit> { - self.run::<Check, Emit, ()>(it) - } - - fn trace<T: Trace<I>>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output> { - self.run::<Emit, Emit, T>(it) + self.run::<Check, Emit>(it) } fn parse_finished(&self, it: InputIter<I>) -> Result<Self::Output, ParserFinishedError<I>> { @@ -258,7 +228,7 @@ where if it.is_finished() { None } else { - match self.parser.run::<OM, EM, ()>(it) { + match self.parser.run::<OM, EM>(it) { Ok((rest, output)) => Some(Ok((rest, output))), Err(rest) => Some(Err(rest)), } @@ -286,7 +256,7 @@ where if it.is_finished() { None } else { - let (rest, output) = match self.parser.run::<OM, EM, ()>(it) { + let (rest, output) = match self.parser.run::<OM, EM>(it) { Ok((rest, output)) => (rest, output), Err(rest) => return Some(Err(rest)), }; @@ -313,7 +283,7 @@ where { type Output = Vec<P::Item>; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( + fn run<OM: Mode, EM: Mode>( &self, mut it: InputIter<I>, ) -> ParserResult<I, Self::Output, OM, EM> { @@ -358,7 +328,7 @@ where { type Output = Vec<P::Item>; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( + fn run<OM: Mode, EM: Mode>( &self, mut it: InputIter<I>, ) -> ParserResult<I, Self::Output, OM, EM> { @@ -392,7 +362,7 @@ where { type Output = Vec<P::Item>; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( + fn run<OM: Mode, EM: Mode>( &self, mut it: InputIter<I>, ) -> ParserResult<I, Self::Output, OM, EM> { @@ -422,12 +392,7 @@ where { type Output = O; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { - Tracer::trace("fn", it.clone()); - + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { match self(it) { Ok((rest, output)) => Ok((rest, OM::bind(|| output))), Err(rest) => Err(EM::bind(|| rest)), @@ -448,11 +413,8 @@ where { type Output = O; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { - match self.parser.run::<OM, EM, Tracer>(it) { + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { + match self.parser.run::<OM, EM>(it) { Ok((rest, output)) => Ok((rest, OM::map(output, |o| (self.f)(o)))), Err(rest) => Err(rest), } @@ -472,18 +434,13 @@ where { type Output = (P1::Output, P2::Output); - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { - Tracer::trace("and", it.clone()); - - let (rest, o1) = match self.left.run::<OM, EM, Tracer>(it) { + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { + let (rest, o1) = match self.left.run::<OM, EM>(it) { Ok((rest, output)) => (rest, output), Err(rest) => return Err(rest), }; - let (rest, o2) = match self.right.run::<OM, EM, Tracer>(rest) { + let (rest, o2) = match self.right.run::<OM, EM>(rest) { Ok((rest, output)) => (rest, output), Err(rest) => return Err(rest), }; @@ -505,18 +462,13 @@ where { type Output = P1::Output; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { - Tracer::trace("andnot", it.clone()); - - match self.not.run::<Check, Check, Tracer>(it.clone()) { + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { + match self.not.run::<Check, Check>(it.clone()) { Ok(_) => return Err(EM::bind(|| it.clone())), _ => (), }; - self.parser.run::<OM, EM, Tracer>(it) + self.parser.run::<OM, EM>(it) } } @@ -533,17 +485,12 @@ where { type Output = O; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { - Tracer::trace("or", it.clone()); - - if let Ok((rest, output)) = self.left.run::<OM, EM, Tracer>(it.clone()) { + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { + if let Ok((rest, output)) = self.left.run::<OM, EM>(it.clone()) { return Ok((rest, output)); }; - if let Ok((rest, output)) = self.right.run::<OM, EM, Tracer>(it.clone()) { + if let Ok((rest, output)) = self.right.run::<OM, EM>(it.clone()) { return Ok((rest, output)); }; @@ -561,12 +508,10 @@ where { type Output = I; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( + fn run<OM: Mode, EM: Mode>( &self, mut it: InputIter<I>, ) -> ParserResult<I, Self::Output, OM, EM> { - Tracer::trace("take", it.clone()); - let start = it.clone(); for _ in 0..self.amt { @@ -602,12 +547,10 @@ where { type Output = I::Item; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( + fn run<OM: Mode, EM: Mode>( &self, mut it: InputIter<I>, ) -> ParserResult<I, Self::Output, OM, EM> { - Tracer::trace("oneof", it.clone()); - let start = it.clone(); match it.next() { @@ -639,12 +582,10 @@ where { type Output = I::Item; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( + fn run<OM: Mode, EM: Mode>( &self, mut it: InputIter<I>, ) -> ParserResult<I, Self::Output, OM, EM> { - Tracer::trace("if", it.clone()); - match it.next() { Some((_, output)) if (self.f)(&output) => Ok((it, OM::bind(|| output))), _ => Err(EM::bind(|| it)), @@ -673,18 +614,13 @@ where { type Output = P1::Output; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { - Tracer::trace("preceded by", it.clone()); - + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { let rest = match self.preceded.check(it.clone()) { Ok((rest, _)) => rest, Err(_) => return Err(EM::bind(|| it)), }; - self.parser.run::<OM, EM, Tracer>(rest) + self.parser.run::<OM, EM>(rest) } } @@ -701,13 +637,8 @@ where { type Output = P1::Output; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { - Tracer::trace("followed by", it.clone()); - - let (rest, output) = match self.parser.run::<OM, EM, Tracer>(it) { + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { + let (rest, output) = match self.parser.run::<OM, EM>(it) { Ok((rest, output)) => (rest, output), Err(rest) => return Err(rest), }; @@ -735,18 +666,13 @@ where { type Output = P1::Output; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { - Tracer::trace("delimited by", it.clone()); - + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { let rest = match self.left.check(it.clone()) { Ok((rest, _)) => rest, Err(_) => return Err(EM::bind(|| it)), }; - let (rest, output) = match self.parser.run::<OM, EM, Tracer>(rest.clone()) { + let (rest, output) = match self.parser.run::<OM, EM>(rest.clone()) { Ok((rest, output)) => (rest, output), Err(rest) => return Err(rest), }; @@ -770,12 +696,7 @@ where { type Output = I; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { - Tracer::trace("recognize", it.clone()); - + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { let start = it.clone(); let rest = match self.parser.check(it.clone()) { @@ -799,12 +720,7 @@ where { type Output = I; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { - Tracer::trace("tag", it.clone()); - + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { match take(self.0.len()).parse(it.clone()) { Ok((rest, output)) if output == self.0 => Ok((rest, OM::bind(|| output))), Ok(_) => return Err(EM::bind(|| it)), @@ -832,10 +748,7 @@ where { type Output = (); - fn run<OM: Mode, EM: Mode, T: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { match self.parser.check(it.clone()) { Ok(_) => Err(EM::bind(|| it)), Err(_) => Ok((it, OM::bind(|| ()))), @@ -854,11 +767,8 @@ where { type Output = Option<P::Output>; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { - match self.parser.run::<OM, EM, Tracer>(it.clone()) { + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { + match self.parser.run::<OM, EM>(it.clone()) { Ok((rest, output)) => Ok((rest, OM::map(output, |o| Some(o)))), Err(_) => Ok((it, OM::bind(|| None))), } @@ -878,11 +788,8 @@ where { type Output = P::Output; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { - match self.parser.run::<Emit, Emit, Tracer>(it.clone()) { + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { + match self.parser.run::<Emit, Emit>(it.clone()) { Ok((rest, output)) if self.checker.check(it.clone()).is_ok() => { Ok((rest, OM::bind(|| output))) } @@ -905,10 +812,7 @@ where { type Output = P::Output; - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { match self.parser.parse(it.clone()) { Ok((rest, output)) if (self.checker)(&output) => Ok((rest, OM::bind(|| output))), Ok(_) => Err(EM::bind(|| it)), @@ -928,10 +832,7 @@ where { type Output = (); - fn run<OM: Mode, EM: Mode, T: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { match self.parser.check(it) { Ok((rest, _)) => Ok((rest, OM::bind(|| ()))), Err(rest) => Err(EM::bind(|| rest)), @@ -947,10 +848,7 @@ where { type Output = (); - fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>( - &self, - it: InputIter<I>, - ) -> ParserResult<I, Self::Output, OM, EM> { + fn run<OM: Mode, EM: Mode>(&self, it: InputIter<I>) -> ParserResult<I, Self::Output, OM, EM> { if it.is_finished() { Ok((it, OM::bind(|| ()))) } else { @@ -975,7 +873,7 @@ where mut it: InputIter<I>, ) -> Option<ParserResult<I, Self::Item, OM, EM>> { while !it.is_finished() { - it = match self.parser.run::<OM, EM, ()>(it.clone()) { + it = match self.parser.run::<OM, EM>(it.clone()) { Ok((rest, output)) => return Some(Ok((rest, output))), Err(_) => match take(1).check(it.clone()) { Ok((rest, _)) => rest, @@ -1095,7 +993,7 @@ pub mod str { impl<'src, 'regex> Parser<&'src str> for Regex<'regex> { type Output = &'src str; - fn run<OM: Mode, EM: Mode, Tracer: Trace<&'src str>>( + fn run<OM: Mode, EM: Mode>( &self, it: InputIter<&'src str>, ) -> ParserResult<&'src str, Self::Output, OM, EM> { |
