From 73eec22d62291a8cf6c095e1bdb74e83a01c759d Mon Sep 17 00:00:00 2001 From: John Turner Date: Fri, 14 Nov 2025 18:31:52 +0000 Subject: remove Trace trait and type parameter --- src/lib.rs | 184 ++++++++++++++----------------------------------------------- 1 file changed, 41 insertions(+), 143 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a8d1416..acf2dcc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 = (InputIter, O); pub type ParserResult = Result::Output>, ::Output>>; -pub trait Trace { - fn trace(parser: &str, input: InputIter); -} - -pub struct DebugTracer; - -impl Trace for DebugTracer -where - I: Input + fmt::Debug, -{ - fn trace(parser: &str, input: InputIter) { - eprintln!("{}:{:?}", parser, input) - } -} - -impl Trace for () -where - I: Input, -{ - fn trace(_: &str, _: InputIter) {} -} - #[derive(Debug)] pub enum ParserFinishedError { Err(InputIter), @@ -55,21 +32,14 @@ pub enum ParserFinishedError { pub trait Parser: Sized { type Output; - fn run>( - &self, - it: InputIter, - ) -> ParserResult; + fn run(&self, it: InputIter) -> ParserResult; fn parse(&self, it: InputIter) -> ParserResult { - self.run::(it) + self.run::(it) } fn check(&self, it: InputIter) -> ParserResult { - self.run::(it) - } - - fn trace>(&self, it: InputIter) -> ParserResult { - self.run::(it) + self.run::(it) } fn parse_finished(&self, it: InputIter) -> Result> { @@ -258,7 +228,7 @@ where if it.is_finished() { None } else { - match self.parser.run::(it) { + match self.parser.run::(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::(it) { + let (rest, output) = match self.parser.run::(it) { Ok((rest, output)) => (rest, output), Err(rest) => return Some(Err(rest)), }; @@ -313,7 +283,7 @@ where { type Output = Vec; - fn run>( + fn run( &self, mut it: InputIter, ) -> ParserResult { @@ -358,7 +328,7 @@ where { type Output = Vec; - fn run>( + fn run( &self, mut it: InputIter, ) -> ParserResult { @@ -392,7 +362,7 @@ where { type Output = Vec; - fn run>( + fn run( &self, mut it: InputIter, ) -> ParserResult { @@ -422,12 +392,7 @@ where { type Output = O; - fn run>( - &self, - it: InputIter, - ) -> ParserResult { - Tracer::trace("fn", it.clone()); - + fn run(&self, it: InputIter) -> ParserResult { 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>( - &self, - it: InputIter, - ) -> ParserResult { - match self.parser.run::(it) { + fn run(&self, it: InputIter) -> ParserResult { + match self.parser.run::(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>( - &self, - it: InputIter, - ) -> ParserResult { - Tracer::trace("and", it.clone()); - - let (rest, o1) = match self.left.run::(it) { + fn run(&self, it: InputIter) -> ParserResult { + let (rest, o1) = match self.left.run::(it) { Ok((rest, output)) => (rest, output), Err(rest) => return Err(rest), }; - let (rest, o2) = match self.right.run::(rest) { + let (rest, o2) = match self.right.run::(rest) { Ok((rest, output)) => (rest, output), Err(rest) => return Err(rest), }; @@ -505,18 +462,13 @@ where { type Output = P1::Output; - fn run>( - &self, - it: InputIter, - ) -> ParserResult { - Tracer::trace("andnot", it.clone()); - - match self.not.run::(it.clone()) { + fn run(&self, it: InputIter) -> ParserResult { + match self.not.run::(it.clone()) { Ok(_) => return Err(EM::bind(|| it.clone())), _ => (), }; - self.parser.run::(it) + self.parser.run::(it) } } @@ -533,17 +485,12 @@ where { type Output = O; - fn run>( - &self, - it: InputIter, - ) -> ParserResult { - Tracer::trace("or", it.clone()); - - if let Ok((rest, output)) = self.left.run::(it.clone()) { + fn run(&self, it: InputIter) -> ParserResult { + if let Ok((rest, output)) = self.left.run::(it.clone()) { return Ok((rest, output)); }; - if let Ok((rest, output)) = self.right.run::(it.clone()) { + if let Ok((rest, output)) = self.right.run::(it.clone()) { return Ok((rest, output)); }; @@ -561,12 +508,10 @@ where { type Output = I; - fn run>( + fn run( &self, mut it: InputIter, ) -> ParserResult { - 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>( + fn run( &self, mut it: InputIter, ) -> ParserResult { - Tracer::trace("oneof", it.clone()); - let start = it.clone(); match it.next() { @@ -639,12 +582,10 @@ where { type Output = I::Item; - fn run>( + fn run( &self, mut it: InputIter, ) -> ParserResult { - 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>( - &self, - it: InputIter, - ) -> ParserResult { - Tracer::trace("preceded by", it.clone()); - + fn run(&self, it: InputIter) -> ParserResult { let rest = match self.preceded.check(it.clone()) { Ok((rest, _)) => rest, Err(_) => return Err(EM::bind(|| it)), }; - self.parser.run::(rest) + self.parser.run::(rest) } } @@ -701,13 +637,8 @@ where { type Output = P1::Output; - fn run>( - &self, - it: InputIter, - ) -> ParserResult { - Tracer::trace("followed by", it.clone()); - - let (rest, output) = match self.parser.run::(it) { + fn run(&self, it: InputIter) -> ParserResult { + let (rest, output) = match self.parser.run::(it) { Ok((rest, output)) => (rest, output), Err(rest) => return Err(rest), }; @@ -735,18 +666,13 @@ where { type Output = P1::Output; - fn run>( - &self, - it: InputIter, - ) -> ParserResult { - Tracer::trace("delimited by", it.clone()); - + fn run(&self, it: InputIter) -> ParserResult { let rest = match self.left.check(it.clone()) { Ok((rest, _)) => rest, Err(_) => return Err(EM::bind(|| it)), }; - let (rest, output) = match self.parser.run::(rest.clone()) { + let (rest, output) = match self.parser.run::(rest.clone()) { Ok((rest, output)) => (rest, output), Err(rest) => return Err(rest), }; @@ -770,12 +696,7 @@ where { type Output = I; - fn run>( - &self, - it: InputIter, - ) -> ParserResult { - Tracer::trace("recognize", it.clone()); - + fn run(&self, it: InputIter) -> ParserResult { let start = it.clone(); let rest = match self.parser.check(it.clone()) { @@ -799,12 +720,7 @@ where { type Output = I; - fn run>( - &self, - it: InputIter, - ) -> ParserResult { - Tracer::trace("tag", it.clone()); - + fn run(&self, it: InputIter) -> ParserResult { 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>( - &self, - it: InputIter, - ) -> ParserResult { + fn run(&self, it: InputIter) -> ParserResult { match self.parser.check(it.clone()) { Ok(_) => Err(EM::bind(|| it)), Err(_) => Ok((it, OM::bind(|| ()))), @@ -854,11 +767,8 @@ where { type Output = Option; - fn run>( - &self, - it: InputIter, - ) -> ParserResult { - match self.parser.run::(it.clone()) { + fn run(&self, it: InputIter) -> ParserResult { + match self.parser.run::(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>( - &self, - it: InputIter, - ) -> ParserResult { - match self.parser.run::(it.clone()) { + fn run(&self, it: InputIter) -> ParserResult { + match self.parser.run::(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>( - &self, - it: InputIter, - ) -> ParserResult { + fn run(&self, it: InputIter) -> ParserResult { 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>( - &self, - it: InputIter, - ) -> ParserResult { + fn run(&self, it: InputIter) -> ParserResult { 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>( - &self, - it: InputIter, - ) -> ParserResult { + fn run(&self, it: InputIter) -> ParserResult { if it.is_finished() { Ok((it, OM::bind(|| ()))) } else { @@ -975,7 +873,7 @@ where mut it: InputIter, ) -> Option> { while !it.is_finished() { - it = match self.parser.run::(it.clone()) { + it = match self.parser.run::(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>( + fn run( &self, it: InputIter<&'src str>, ) -> ParserResult<&'src str, Self::Output, OM, EM> { -- cgit v1.2.3