diff --git a/src/combinators.rs b/src/combinators.rs index 3e0aee6..4ffe931 100644 --- a/src/combinators.rs +++ b/src/combinators.rs @@ -1,15 +1,44 @@ -use crate::Parser; +use std::marker::PhantomData; + +use crate::{ParseResult, Parser}; pub fn repeated(parser: P) -> impl Parser, E> where P: Parser, { - move |input: I| { + Repeated::new(parser) +} + +struct Repeated +where + P: Parser, +{ + inner_parser: P, + phantom: PhantomData<(I, O, E)>, +} + +impl Repeated +where + P: Parser, +{ + fn new(inner_parser: P) -> Self { + Self { + inner_parser, + phantom: PhantomData, + } + } +} + +impl Parser, E> for Repeated +where + P: Parser, +{ + fn parse(&self, input: I) -> ParseResult, E> { let mut acc = input; let mut results = vec![]; loop { - match parser.parse(acc) { + match self.inner_parser.parse(acc) { Ok((item, rest)) => { results.push(item); acc = rest;