Refactor parser module

This commit is contained in:
Greg Shuflin 2022-10-21 22:22:21 -07:00
parent 73845224d5
commit 035afbf22f
2 changed files with 37 additions and 31 deletions

View File

@ -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<dyn Parser<I, O, E> + 'a>,
}
impl<'a, I, O, E> BoxedParser<'a, I, O, E>
where
I: ParserInput,
{
pub(crate) fn new<P>(inner: P) -> Self
where
P: Parser<I, O, E> + 'a,
{
BoxedParser {
inner: Box::new(inner),
}
}
}
impl<'a, I: ParserInput, O, E> Parser<I, O, E> for BoxedParser<'a, I, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.inner.parse(input)
}
fn bnf(&self) -> Option<Bnf> {
self.inner.bnf()
}
}

View File

@ -1,6 +1,10 @@
mod boxed_parser;
use crate::bnf::Bnf;
use std::rc::Rc;
pub use boxed_parser::BoxedParser;
pub type ParseResult<I, O, E> = 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<dyn Parser<I, O, E> + 'a>,
}
impl<'a, I, O, E> BoxedParser<'a, I, O, E>
where
I: ParserInput,
{
pub(crate) fn new<P>(inner: P) -> Self
where
P: Parser<I, O, E> + 'a,
{
BoxedParser {
inner: Box::new(inner),
}
}
}
impl<'a, I: ParserInput, O, E> Parser<I, O, E> for BoxedParser<'a, I, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.inner.parse(input)
}
fn bnf(&self) -> Option<Bnf> {
self.inner.bnf()
}
}
impl<I: ParserInput, O, E, F> Parser<I, O, E> for F
where
F: Fn(I) -> ParseResult<I, O, E>,