From 035afbf22fac62f49666eb3a3d62a4ead97f8882 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Fri, 21 Oct 2022 22:22:21 -0700 Subject: [PATCH] Refactor parser module --- src/parser/boxed_parser.rs | 33 ++++++++++++++++++++++++++++++ src/{parser.rs => parser/mod.rs} | 35 ++++---------------------------- 2 files changed, 37 insertions(+), 31 deletions(-) create mode 100644 src/parser/boxed_parser.rs rename src/{parser.rs => parser/mod.rs} (86%) 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,