mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 23:31:12 +00:00
try some boxed stuff
This commit is contained in:
parent
d54cf81f7b
commit
3f81845de5
1 changed files with 22 additions and 7 deletions
|
@ -188,10 +188,23 @@ impl Keyword {
|
||||||
|
|
||||||
pub trait Parser<'a, Output> {
|
pub trait Parser<'a, Output> {
|
||||||
fn parse(&self, &'a Bump, State<'a>) -> ParseResult<'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> {
|
pub struct BoxedParser<'a, Output> {
|
||||||
parser: &'a (dyn Parser<'a, Output> + 'a),
|
parser: &'a (dyn Parser<'a, Output> + 'a),
|
||||||
|
arena: &'a Bump,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Output> BoxedParser<'a, Output> {
|
impl<'a, Output> BoxedParser<'a, Output> {
|
||||||
|
@ -201,16 +214,18 @@ impl<'a, Output> BoxedParser<'a, Output> {
|
||||||
{
|
{
|
||||||
BoxedParser {
|
BoxedParser {
|
||||||
parser: arena.alloc(parser),
|
parser: arena.alloc(parser),
|
||||||
|
arena,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, F, Output> Parser<'a, Output> for F
|
impl<'a, Output> Parser<'a, Output> for BoxedParser<'a, Output> {
|
||||||
where
|
|
||||||
F: Fn(&'a Bump, State<'a>) -> ParseResult<'a, Output>,
|
|
||||||
{
|
|
||||||
fn parse(&self, arena: &'a Bump, state: State<'a>) -> ParseResult<'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)
|
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
|
where
|
||||||
P: Parser<'a, Before>,
|
P: Parser<'a, Before>,
|
||||||
F: Fn(Before) -> After,
|
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
|
where
|
||||||
P: Parser<'a, Before>,
|
P: Parser<'a, Before>,
|
||||||
F: Fn(&'a Bump, Before) -> After,
|
F: Fn(&'a Bump, Before) -> After,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue