aoc_parse/parsers/
empty.rs

1//! Parser that successfully matches the empty string.
2
3use crate::{ParseContext, ParseIter, Parser, Reported, Result};
4
5#[derive(Clone, Copy)]
6pub struct EmptyParser;
7
8impl Parser for EmptyParser {
9    type Output = ();
10    type RawOutput = ();
11    type Iter<'parse> = EmptyParseIter;
12
13    fn parse_iter<'parse>(
14        &self,
15        _context: &mut ParseContext<'parse>,
16        start: usize,
17    ) -> Result<EmptyParseIter, Reported> {
18        Ok(EmptyParseIter { location: start })
19    }
20}
21
22pub struct EmptyParseIter {
23    location: usize,
24}
25
26impl<'parse> ParseIter<'parse> for EmptyParseIter {
27    type RawOutput = ();
28    fn match_end(&self) -> usize {
29        self.location
30    }
31    fn backtrack(&mut self, _context: &mut ParseContext<'parse>) -> Result<(), Reported> {
32        Err(Reported)
33    }
34    fn convert(&self) -> Self::RawOutput {}
35}
36
37/// Used by the `parser!()` macro to implement the empty pattern, `()`.
38#[doc(hidden)]
39pub fn empty() -> EmptyParser {
40    EmptyParser
41}