From 0d4ea42678b4ecfa90b61744067b461700af8489 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Wed, 19 Oct 2022 22:06:10 -0700 Subject: [PATCH] Just use repeated combinator --- src/combinators.rs | 34 ---------------------------------- src/lib.rs | 8 ++++---- src/sequence.rs | 10 +++++----- 3 files changed, 9 insertions(+), 43 deletions(-) diff --git a/src/combinators.rs b/src/combinators.rs index 38d0efb..764c678 100644 --- a/src/combinators.rs +++ b/src/combinators.rs @@ -95,40 +95,6 @@ where } } -pub fn zero_or_more(parser: P) -> impl Parser, I> -where - P: Parser, - I: Copy, -{ - move |mut input| { - let mut results = Vec::new(); - - while let Ok((item, rest)) = parser.parse(input) { - results.push(item); - input = rest; - } - - Ok((results, input)) - } -} - -pub fn one_or_more(parser: P) -> impl Parser, I> -where - P: Parser + 'static, - I: Copy + 'static, - O: 'static, -{ - let parser = std::rc::Rc::new(parser); - parser - .clone() - .then(zero_or_more(parser)) - .map(|(first, rest)| { - let mut output = vec![first]; - output.extend(rest.into_iter()); - output - }) -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/lib.rs b/src/lib.rs index 16489f1..3069a71 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ pub use parser::{ParseResult, Parser}; mod tests { use super::*; use crate::choice::choice; - use crate::combinators::{one_or_more, zero_or_more}; + use crate::combinators::repeated; use crate::primitives::{any_char, literal, literal_char, one_of, pred}; use crate::sequence::seq; use std::collections::HashMap; @@ -54,8 +54,8 @@ mod tests { let json_number_inner = choice(( seq(( - one_or_more(digit()), - literal(".").then(zero_or_more(digit())).optional(), + repeated(digit()).at_least(1), + literal(".").then(repeated(digit())).optional(), )) .map(|(mut digits, maybe_more)| { if let Some((point, more)) = maybe_more { @@ -64,7 +64,7 @@ mod tests { } digits.into_iter().collect::() }), - seq((literal("."), one_or_more(digit()))).map(|(_point, digits)| { + seq((literal("."), repeated(digit()).at_least(1))).map(|(_point, digits)| { let mut d = vec!["."]; d.extend(digits.into_iter()); d.into_iter().collect::() diff --git a/src/sequence.rs b/src/sequence.rs index cba5a77..5e7000c 100644 --- a/src/sequence.rs +++ b/src/sequence.rs @@ -110,7 +110,7 @@ where #[cfg(test)] mod test { use super::*; - use crate::combinators::zero_or_more; + use crate::combinators::repeated; use crate::primitives::{identifier, literal}; #[test] @@ -128,16 +128,16 @@ mod test { fn test_seq() { let p = seq(( literal("bong").to(10), - zero_or_more(literal(" ")).to(()), + repeated(literal(" ")).to(()), literal("hits").to(20), )); assert_eq!(p.parse("bong hits").unwrap(), ((10, (), 20), "")); let p = seq(( literal("alpha").to(10), - zero_or_more(literal(" ")).to(()), - zero_or_more(literal("-")).to(()), - zero_or_more(literal(" ")), + repeated(literal(" ")).to(()), + repeated(literal("-")).to(()), + repeated(literal(" ")), literal("beta"), )); assert_eq!(