Trait aoc_parse::Parser

source ·
pub trait Parser {
    type Output;
    type RawOutput: ParserOutput<UserType = Self::Output>;
    type Iter<'parse>: ParseIter<'parse, RawOutput = Self::RawOutput>
    where
        Self: 'parse
; fn parse_iter<'parse>(
        &'parse self,
        context: &mut ParseContext<'parse>,
        start: usize
    ) -> Result<Self::Iter<'parse>, Reported>; fn parse(&self, s: &str) -> Result<Self::Output, ParseError> { ... } fn parse_raw(&self, s: &str) -> Result<Self::RawOutput, ParseError> { ... } }
Expand description

Trait implemented by all parsers.

This is implemented by the built-in parsers, like i32, as well as user-defined parsers created with parser!.

To run a parser, pass some text to the parse method.

Required Associated Types§

The type of value this parser produces from text.

The type this parser produces internally before converting to the output type.

Some combinators use the RawOutput to determine how types should combine. For example, if A::RawOutput = (), then A produces no output; and if B::RawOutput = (i32,) then B produces an integer; SequenceParser<A, B>::RawOutput will then be (i32,), the result of concatenating the two raw tuples, rather than ((), i32).

However, RawOutput is very often a singleton tuple, and these are awkward for users, so we convert to the Output type before presenting a result to the user.

The type that implements matching, backtracking, and type conversion for this parser, an implementation detail.

Required Methods§

Produce a parse iterator. This is an internal implementation detail of the parser and shouldn’t normally be called directly from application code.

Provided Methods§

Fully parse the given source string s and return the resulting value.

This is the main way of using a Parser.

This succeeds only if this parser matches the entire input string. It’s an error if any unmatched characters are left over at the end of s.

Examples found in repository?
src/util.rs (line 36)
27
28
29
30
31
32
33
34
35
36
37
pub fn aoc_parse<P, E>(puzzle_input: &str, parser: P) -> Result<P::Output, E>
where
    P: Parser,
    E: From<ParseError>,
{
    let mut p = puzzle_input.to_string();
    if !p.ends_with('\n') {
        p.push('\n');
    }
    Ok(parser.parse(&p)?)
}

Like parse but produce the output in its raw form.

Examples found in repository?
src/traits.rs (line 42)
41
42
43
    fn parse(&self, s: &str) -> Result<Self::Output> {
        self.parse_raw(s).map(|v| v.into_user_type())
    }

Implementations on Foreign Types§

Implementors§