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> { ... } fn map<T, F>(self, mapper: F) -> MapParser<Self, F>
    where
        Self: Sized,
        F: Fn(Self::Output) -> T
, { ... } }
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.

Like parse but produce the output in its raw form.

Produce a new parser that behaves like this parser but additionally applies the given closure when producing the value.

use aoc_parse::{parser, prelude::*};
let p = u32.map(|x| x * 1_000_001);
assert_eq!(p.parse("123").unwrap(), 123_000_123);

This is used to implement the => feature of parser!.

let p = parser!(x:u32 => x * 1_000_001);
assert_eq!(p.parse("123").unwrap(), 123_000_123);

The closure is called after the overall parse succeeds, as part of turning the parse into Output values. This means the function will not be called during a partly-successful parse that later fails.

let p = parser!(("A" => panic!()) "B" "C");
assert!(p.parse("ABX").is_err());

let p2 = parser!({
   (i32 => panic!()) " ft" => 1,
   i32 " km" => 2,
});
assert_eq!(p2.parse("37 km").unwrap(), 2);

Implementations on Foreign Types§

Implementors§