Trait Parser
trait Parser<I, O, E>
Core trait for parsing
The simplest way to implement a Parser is with a function
use *;
let = empty.parse_peek.unwrap;
assert_eq!; // We didn't consume any input
which can be made stateful by returning a function
use *;
let = empty.parse_peek.unwrap;
assert_eq!; // We didn't consume any input
assert_eq!;
Additionally, some basic types implement Parser as well, including
u8andchar, see [winnow::token::one_of][crate::token::one_of]&[u8]and&str, see [winnow::token::literal][crate::token::literal]
Required Methods
Provided Methods
fn parse(self: &mut Self, input: I) -> Result<O, ParseError<I, <E as ParserError<I>>::Inner>> where Self: core::marker::Sized, I: Stream + StreamIsPartial, E: ParserError<I>, <E as ParserError<I>>::Inner: ParserError<I>Parse all of
input, generatingOfrom itfn parse_peek(self: &mut Self, input: I) -> Result<(I, O), E>Take tokens from the
Stream, turning it into the outputThis returns a copy of the
Streamadvanced to the next location.Generally, prefer
Parser::parse_next. This is primarily intended for:- Migrating from older versions /
nom - Testing
Parsers
For look-ahead parsing, see instead [
peek][crate::combinator::peek].- Migrating from older versions /
fn by_ref(self: &mut Self) -> impls::ByRef<'_, Self, I, O, E> where Self: core::marker::SizedTreat
&mut Selfas a parserThis helps when needing to move a
Parserwhen all you have is a&mut Parser.Example
Because parsers are
FnMut, they can be called multiple times. This prevents movingfinto [length_take][crate::binary::length_take] andginto [Parser::complete_err]:# use *; # use Parser; # use ParserError; # use length_take;By adding
by_ref, we can make this work:# use *; # use Parser; # use ParserError; # use length_take;fn value<O2>(self: Self, val: O2) -> impls::Value<Self, I, O, O2, E> where Self: core::marker::Sized, O2: CloneProduce the provided value
Example
# use ; # use *; use alpha1; #fn default_value<O2>(self: Self) -> impls::DefaultValue<Self, I, O, O2, E> where Self: core::marker::Sized, O2: core::default::DefaultProduce a type's default value
Example
# use ; # use *; use alpha1; #fn void(self: Self) -> impls::Void<Self, I, O, E> where Self: core::marker::SizedDiscards the output of the
ParserExample
# use ; # use *; use alpha1; #fn output_into<O2>(self: Self) -> impls::OutputInto<Self, I, O, O2, E> where Self: core::marker::Sized, O: Into<O2>Convert the parser's output to another type using
std::convert::FromExample
# use *; # use ContextError; use alpha1; #fn take(self: Self) -> impls::Take<Self, I, O, E> where Self: core::marker::Sized, I: StreamProduce the consumed input as produced value.
Example
# use ; # use *; use ; use separated_pair; #fn with_taken(self: Self) -> impls::WithTaken<Self, I, O, E> where Self: core::marker::Sized, I: StreamProduce the consumed input with the output
Functions similarly to [take][Parser::take] except it returns the parser output as well.
This can be useful especially in cases where the output is not the same type as the input, or the input is a user defined type.
Returned tuple is of the format
(produced output, consumed input).Example
# use *; # use ; use ; use literal; use separated_pair; assert_eq!; assert!;fn span(self: Self) -> impls::Span<Self, I, O, E> where Self: core::marker::Sized, I: Stream + LocationProduce the location of the consumed input as produced value.
Example
# use *; # use ; # use Range; use LocatingSlice; use alpha1; use separated_pair; assert_eq!; assert!;fn with_span(self: Self) -> impls::WithSpan<Self, I, O, E> where Self: core::marker::Sized, I: Stream + LocationProduce the location of consumed input with the output
Functions similarly to
Parser::spanexcept it returns the parser output as well.This can be useful especially in cases where the output is not the same type as the input, or the input is a user defined type.
Returned tuple is of the format
(produced output, consumed input).Example
# use *; # use ; # use Range; use LocatingSlice; use alpha1; use literal; use separated_pair; assert_eq!; assert!;fn map<G, O2>(self: Self, map: G) -> impls::Map<Self, G, I, O, O2, E> where G: FnMut(O) -> O2, Self: core::marker::SizedMaps a function over the output of a parser
Example
# use *; # use ; # use digit1; #fn try_map<G, O2, E2>(self: Self, map: G) -> impls::TryMap<Self, G, I, O, O2, E, E2> where Self: core::marker::Sized, G: FnMut(O) -> Result<O2, E2>, I: Stream, E: FromExternalError<I, E2> + ParserError<I>Applies a function returning a
Resultover the output of a parser.Example
# use ; # use *; use digit1; #fn verify_map<G, O2>(self: Self, map: G) -> impls::VerifyMap<Self, G, I, O, O2, E> where Self: core::marker::Sized, G: FnMut(O) -> Option<O2>, I: Stream, E: ParserError<I>Apply both
Parser::verifyandParser::map.Example
# use ; # use *; use digit1; #fn flat_map<G, H, O2>(self: Self, map: G) -> impls::FlatMap<Self, G, H, I, O, O2, E> where Self: core::marker::Sized, G: FnMut(O) -> H, H: Parser<I, O2, E>Creates a parser from the output of this one
Example
# use ; use take; use u8; assert_eq!; assert!;which is the same as
# use ; use take; use u8; assert_eq!; assert!;fn and_then<G, O2>(self: Self, inner: G) -> impls::AndThen<Self, G, I, O, O2, E> where Self: core::marker::Sized, G: Parser<O, O2, E>, O: StreamIsPartial, I: StreamApplies a second parser over the output of the first one
Example
# use ; # use *; use digit1; use take; #fn parse_to<O2>(self: Self) -> impls::ParseTo<Self, I, O, O2, E> where Self: core::marker::Sized, I: Stream, O: ParseSlice<O2>, E: ParserError<I>Apply
std::str::FromStrto the output of the parserExample
# use *; use ; use digit1; // the parser will count how many characters were returned by digit1 assert_eq!; // this will fail if digit1 fails assert!;fn verify<G, O2>(self: Self, filter: G) -> impls::Verify<Self, G, I, O, O2, E> where Self: core::marker::Sized, G: FnMut(&O2) -> bool, I: Stream, O: crate::lib::std::borrow::Borrow<O2>, O2: ?Sized, E: ParserError<I>Returns the output of the child parser if it satisfies a verification function.
The verification function takes as argument a reference to the output of the parser.
Example
# use ; # use alpha1; # use *; #fn context<C>(self: Self, context: C) -> impls::Context<Self, I, O, E, C> where Self: core::marker::Sized, I: Stream, E: AddContext<I, C> + ParserError<I>, C: Clone + crate::lib::std::fmt::DebugIf parsing fails, add context to the error
This is used mainly to add user friendly information to errors when backtracking through a parse tree.
See also [tutorial][crate::_tutorial::chapter_7].
Example
# use *; # use ; # use digit1; # use StrContext; # use StrContextValue; #fn context_with<F, C, FI>(self: Self, context: F) -> impls::ContextWith<Self, I, O, E, F, C, FI> where Self: core::marker::Sized, I: Stream, E: AddContext<I, C> + ParserError<I>, F: Fn() -> FI + Clone, C: crate::lib::std::fmt::Debug, FI: Iterator<Item = C>If parsing fails, dynamically add context to the error
This is used mainly to add user friendly information to errors when backtracking through a parse tree.
See also [tutorial][crate::_tutorial::chapter_7].
Example
# use *; # use ; # use digit1; # use StrContext; # use StrContextValue; #fn map_err<G, E2>(self: Self, map: G) -> impls::MapErr<Self, G, I, O, E, E2> where G: FnMut(E) -> E2, Self: core::marker::SizedMaps a function over the error of a parser
Example
# use *; # use Parser; # use Result; # use digit1; # use StrContext; # use AddContext; # use ContextError; #fn complete_err(self: Self) -> impls::CompleteErr<Self, I, O, E> where Self: core::marker::SizedTransforms [
Incomplete][crate::error::ErrMode::Incomplete] into [Backtrack][crate::error::ErrMode::Backtrack]Example
# use ; # use take; # use *; #fn err_into<E2>(self: Self) -> impls::ErrInto<Self, I, O, E, E2> where Self: core::marker::Sized, E: Into<E2>Convert the parser's error to another type using
std::convert::From
Implementors
impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12, O13, O14, O15, O16, O17, O18, O19, O20, O21, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21)impl<F, I, O, O2, E> Parser for Value<F, I, O, O2, E>impl<I, E> Parser for u8impl<I: Stream, O0, O1, O2, E: ParserError<I>, P0, P1, P2> Parser for (P0, P1, P2)impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12, O13, O14, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14)impl<F, G, I, O, O2, E, E2> Parser for TryMap<F, G, I, O, O2, E, E2>impl<'s, I, E: ParserError<I>> Parser for &'s [u8]impl<I, O, E, F> Parser for Span<F, I, O, E>impl<'s, I, E: ParserError<I>, N: usize> Parser for &'s [u8; N]impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7> Parser for (P0, P1, P2, P3, P4, P5, P6, P7)impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12, O13, O14, O15, O16, O17, O18, O19, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19)impl<P, I, O, E> Parser for CompleteErr<P, I, O, E>impl<P, I, O, E, F, C, FI> Parser for ContextWith<P, I, O, E, F, C, FI>impl<I: Stream, O0, E: ParserError<I>, P0> Parser for (P0)impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12)impl<I, O, E, P> Parser for ByRef<'_, P, I, O, E>impl<I, O, E, F> Parser for Take<F, I, O, E>impl<I: Stream, O0, O1, O2, O3, O4, O5, E: ParserError<I>, P0, P1, P2, P3, P4, P5> Parser for (P0, P1, P2, P3, P4, P5)impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12, O13, O14, O15, O16, O17, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17)impl<P, I, O, O2, E> Parser for ParseTo<P, I, O, O2, E>impl<F, I, O, E, E2> Parser for ErrInto<F, I, O, E, E2>impl<'s, I, E: ParserError<I>> Parser for crate::ascii::Caseless<&'s str>impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10)impl<P, I, O, C, E> Parser for Repeat<P, I, O, C, E>impl<F, I, O, O2, E> Parser for DefaultValue<F, I, O, O2, E>impl<I: Stream, O0, O1, O2, O3, E: ParserError<I>, P0, P1, P2, P3> Parser for (P0, P1, P2, P3)impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12, O13, O14, O15, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15)impl<F, G, I, O, O2, E> Parser for VerifyMap<F, G, I, O, O2, E>impl<'s, I, E: ParserError<I>> Parser for crate::ascii::Caseless<&'s [u8]>impl<F, I, O, E> Parser for WithSpan<F, I, O, E>impl<'s, I, E: ParserError<I>, N: usize> Parser for crate::ascii::Caseless<&'s [u8; N]>impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8)impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12, O13, O14, O15, O16, O17, O18, O19, O20, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20)impl<F, G, I, O, O2, E> Parser for Verify<F, G, I, O, O2, E>impl<F, G, I, O, E, E2> Parser for MapErr<F, G, I, O, E, E2>impl<I, O, E, F> Parser for Fimpl<I: Stream, O0, O1, E: ParserError<I>, P0, P1> Parser for (P0, P1)impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12, O13, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13)impl<F, G, I, O, O2, E> Parser for Map<F, G, I, O, O2, E>impl<I, E> Parser for charimpl<F, I, O, E> Parser for WithTaken<F, I, O, E>impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6> Parser for (P0, P1, P2, P3, P4, P5, P6)impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12, O13, O14, O15, O16, O17, O18, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18)impl<F, G, H, I, O, O2, E> Parser for FlatMap<F, G, H, I, O, O2, E>impl<F, I, O, E, C> Parser for Context<F, I, O, E, C>impl<I: Stream, E: ParserError<I>> Parser for ()impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11)impl<F, I, O, E> Parser for Void<F, I, O, E>impl<I: Stream, O0, O1, O2, O3, O4, E: ParserError<I>, P0, P1, P2, P3, P4> Parser for (P0, P1, P2, P3, P4)impl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12, O13, O14, O15, O16, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16)impl<F, G, I, O, O2, E> Parser for AndThen<F, G, I, O, O2, E>impl<F, I, O, O2, E> Parser for OutputInto<F, I, O, O2, E>impl<'s, I, E: ParserError<I>> Parser for &'s strimpl<I: Stream, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, E: ParserError<I>, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9> Parser for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9)