try some boxed stuff

This commit is contained in:
Richard Feldman 2019-09-17 03:48:13 -04:00
parent d54cf81f7b
commit 3f81845de5

View file

@ -188,10 +188,23 @@ impl Keyword {
pub trait Parser<'a, Output> {
fn parse(&self, &'a Bump, State<'a>) -> ParseResult<'a, Output>;
fn arena(&self) -> &'a Bump;
fn map<F, NewOutput>(self, transform: F) -> BoxedParser<'a, NewOutput>
where
Self: Sized + 'a,
Output: 'a,
NewOutput: 'a,
F: Fn(Output) -> NewOutput + 'a,
{
BoxedParser::new(self.arena(), map(self, transform))
}
}
pub struct BoxedParser<'a, Output> {
parser: &'a (dyn Parser<'a, Output> + 'a),
arena: &'a Bump,
}
impl<'a, Output> BoxedParser<'a, Output> {
@ -201,16 +214,18 @@ impl<'a, Output> BoxedParser<'a, Output> {
{
BoxedParser {
parser: arena.alloc(parser),
arena,
}
}
}
impl<'a, F, Output> Parser<'a, Output> for F
where
F: Fn(&'a Bump, State<'a>) -> ParseResult<'a, Output>,
{
impl<'a, Output> Parser<'a, Output> for BoxedParser<'a, Output> {
fn parse(&self, arena: &'a Bump, state: State<'a>) -> ParseResult<'a, Output> {
self(arena, state)
self.parser.parse(arena, state)
}
fn arena(&self) -> &'a Bump {
self.arena
}
}
@ -230,7 +245,7 @@ where
move |arena, state| get_parser().parse(arena, state)
}
pub fn map<'a, P, F, Before, After>(parser: P, transform: F) -> impl Parser<'a, After>
fn map<'a, P, F, Before, After>(parser: P, transform: F) -> impl Parser<'a, After>
where
P: Parser<'a, Before>,
F: Fn(Before) -> After,
@ -242,7 +257,7 @@ where
}
}
pub fn map_with_arena<'a, P, F, Before, After>(parser: P, transform: F) -> impl Parser<'a, After>
fn map_with_arena<'a, P, F, Before, After>(parser: P, transform: F) -> impl Parser<'a, After>
where
P: Parser<'a, Before>,
F: Fn(&'a Bump, Before) -> After,