Skip to main content

oni_comb_parser/combinator/
context.rs

1use crate::error::ContextError;
2use crate::fail::{Fail, PResult};
3use crate::input::Input;
4use crate::parser::Parser;
5
6pub struct Context<P> {
7  pub(crate) parser: P,
8  pub(crate) label: &'static str,
9}
10
11impl<I, P> Parser<I> for Context<P>
12where
13  I: Input,
14  P: Parser<I>,
15  P::Error: ContextError,
16{
17  type Error = P::Error;
18  type Output = P::Output;
19
20  #[inline]
21  fn parse_next(&mut self, input: &mut I) -> PResult<Self::Output, Self::Error> {
22    match self.parser.parse_next(input) {
23      Ok(v) => Ok(v),
24      Err(Fail::Backtrack(e)) => Err(Fail::Backtrack(e.add_context(self.label))),
25      Err(Fail::Cut(e)) => Err(Fail::Cut(e.add_context(self.label))),
26      Err(e) => Err(e),
27    }
28  }
29}