From 97d35df6871d11da7096dda441397d77e0c89bcf Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Fri, 26 Jan 2024 00:20:57 -0800 Subject: [PATCH] repeated combinator --- src/combinators.rs | 18 ++++++++++++++++++ src/lib.rs | 2 ++ 2 files changed, 20 insertions(+) create mode 100644 src/combinators.rs diff --git a/src/combinators.rs b/src/combinators.rs new file mode 100644 index 0000000..b48a512 --- /dev/null +++ b/src/combinators.rs @@ -0,0 +1,18 @@ +use crate::Parser; + +pub fn repeated(parser: P) -> impl Parser, E> +where + P: Parser, + I: Copy, +{ + move |input: I| { + let mut acc = input; + let mut results = vec![]; + + while let Ok((item, rest)) = parser.parse(acc) { + results.push(item); + acc = rest; + } + Ok((results, acc)) + } +} diff --git a/src/lib.rs b/src/lib.rs index 35a9b8c..c4c9db1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,12 @@ #![allow(dead_code)] //TODO eventually turn this off mod choice; +mod combinators; mod map; mod primitives; mod sequence; pub use choice::*; +pub use combinators::*; pub use map::*; pub use primitives::*; pub use sequence::*;