prism_parser/core/
parser.rs

1use crate::core::context::ParserContext;
2use crate::core::pos::Pos;
3use crate::core::presult::PResult;
4use crate::core::state::ParserState;
5use crate::error::ParseError;
6
7pub trait Parser<'arn, 'grm: 'arn, O, E: ParseError> {
8    fn parse(
9        &self,
10        pos: Pos,
11        state: &mut ParserState<'arn, 'grm, E>,
12        context: ParserContext,
13    ) -> PResult<O, E>;
14}
15
16pub fn map_parser<'a, 'arn: 'a, 'grm: 'arn, O, P, E: ParseError>(
17    p: impl Parser<'arn, 'grm, O, E> + 'a,
18    f: &'a impl Fn(O) -> P,
19) -> impl Parser<'arn, 'grm, P, E> + 'a {
20    move |pos: Pos, state: &mut ParserState<'arn, 'grm, E>, context: ParserContext| {
21        p.parse(pos, state, context).map(f)
22    }
23}
24
25impl<
26        'arn,
27        'grm: 'arn,
28        O,
29        E: ParseError,
30        T: Fn(Pos, &mut ParserState<'arn, 'grm, E>, ParserContext) -> PResult<O, E>,
31    > Parser<'arn, 'grm, O, E> for T
32{
33    #[inline(always)]
34    fn parse(
35        &self,
36        pos: Pos,
37        state: &mut ParserState<'arn, 'grm, E>,
38        context: ParserContext,
39    ) -> PResult<O, E> {
40        self(pos, state, context)
41    }
42}