pub trait Parser<'parse, 'source> {
type Output;
type RawOutput: ParserOutput<UserType = Self::Output>;
type Iter: ParseIter<RawOutput = Self::RawOutput>;
fn parse_iter(&'parse self, source: &'source str, start: usize) -> Self::Iter;
fn parse(&'parse self, s: &'source str) -> Result<Self::Output, ParseError> { ... }
fn parse_raw(
&'parse self,
s: &'source 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§
sourcetype RawOutput: ParserOutput<UserType = Self::Output>
type RawOutput: ParserOutput<UserType = Self::Output>
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.
Required Methods§
sourcefn parse_iter(&'parse self, source: &'source str, start: usize) -> Self::Iter
fn parse_iter(&'parse self, source: &'source str, start: usize) -> Self::Iter
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§
sourcefn parse(&'parse self, s: &'source str) -> Result<Self::Output, ParseError>
fn parse(&'parse self, s: &'source str) -> Result<Self::Output, ParseError>
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.
sourcefn parse_raw(
&'parse self,
s: &'source str
) -> Result<Self::RawOutput, ParseError>
fn parse_raw(
&'parse self,
s: &'source str
) -> Result<Self::RawOutput, ParseError>
Like parse but produce the output in its raw form.
sourcefn map<T, F>(self, mapper: F) -> MapParser<Self, F>where
Self: Sized,
F: Fn(Self::Output) -> T,
fn map<T, F>(self, mapper: F) -> MapParser<Self, F>where
Self: Sized,
F: Fn(Self::Output) -> T,
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);