diff --git a/src/parser/boxed_parser.rs b/src/parser/boxed_parser.rs new file mode 100644 index 0000000..8883910 --- /dev/null +++ b/src/parser/boxed_parser.rs @@ -0,0 +1,33 @@ +use crate::bnf::Bnf; +use crate::parser::{ParseResult, Parser, ParserInput}; + +pub struct BoxedParser<'a, I, O, E> +where + I: ParserInput, +{ + inner: Box + 'a>, +} + +impl<'a, I, O, E> BoxedParser<'a, I, O, E> +where + I: ParserInput, +{ + pub(crate) fn new

(inner: P) -> Self + where + P: Parser + 'a, + { + BoxedParser { + inner: Box::new(inner), + } + } +} + +impl<'a, I: ParserInput, O, E> Parser for BoxedParser<'a, I, O, E> { + fn parse(&self, input: I) -> ParseResult { + self.inner.parse(input) + } + + fn bnf(&self) -> Option { + self.inner.bnf() + } +} diff --git a/src/parser.rs b/src/parser/mod.rs similarity index 86% rename from src/parser.rs rename to src/parser/mod.rs index 7b2019b..244aed8 100644 --- a/src/parser.rs +++ b/src/parser/mod.rs @@ -1,6 +1,10 @@ +mod boxed_parser; + use crate::bnf::Bnf; use std::rc::Rc; +pub use boxed_parser::BoxedParser; + pub type ParseResult = Result<(O, I), E>; pub trait ParserInput: std::fmt::Debug {} @@ -122,37 +126,6 @@ where } } -pub struct BoxedParser<'a, I, O, E> -where - I: ParserInput, -{ - inner: Box + 'a>, -} - -impl<'a, I, O, E> BoxedParser<'a, I, O, E> -where - I: ParserInput, -{ - pub(crate) fn new

(inner: P) -> Self - where - P: Parser + 'a, - { - BoxedParser { - inner: Box::new(inner), - } - } -} - -impl<'a, I: ParserInput, O, E> Parser for BoxedParser<'a, I, O, E> { - fn parse(&self, input: I) -> ParseResult { - self.inner.parse(input) - } - - fn bnf(&self) -> Option { - self.inner.bnf() - } -} - impl Parser for F where F: Fn(I) -> ParseResult,