From 0e26ef1ea69aae156446d276236696d0b34701b6 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Fri, 26 Jan 2024 22:11:55 -0800 Subject: [PATCH] Repeated structure --- src/combinators.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) 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;