Skip to main content

Parser

Trait Parser 

pub trait Parser<I, O, E> {
Show 25 methods // Required method fn parse_next(&mut self, input: &mut I) -> Result<O, E>; // Provided methods fn parse( &mut self, input: I, ) -> Result<O, ParseError<I, <E as ParserError<I>>::Inner>> where Self: Sized, I: Stream + StreamIsPartial, E: ParserError<I>, <E as ParserError<I>>::Inner: ParserError<I> { ... } fn parse_iter(&mut self, input: I) -> ParseIter<'_, Self, I, O, E> ⓘ where Self: Sized, I: Stream + StreamIsPartial, E: ParserError<I>, <E as ParserError<I>>::Inner: ParserError<I> { ... } fn parse_peek(&mut self, input: I) -> Result<(I, O), E> { ... } fn by_ref(&mut self) -> ByRef<'_, Self, I, O, E> where Self: Sized { ... } fn value<O2>(self, val: O2) -> Value<Self, I, O, O2, E> where Self: Sized, O2: Clone { ... } fn default_value<O2>(self) -> DefaultValue<Self, I, O, O2, E> where Self: Sized, O2: Default { ... } fn void(self) -> Void<Self, I, O, E> where Self: Sized { ... } fn output_into<O2>(self) -> OutputInto<Self, I, O, O2, E> where Self: Sized, O: Into<O2> { ... } fn take(self) -> Take<Self, I, O, E> where Self: Sized, I: Stream { ... } fn with_taken(self) -> WithTaken<Self, I, O, E> where Self: Sized, I: Stream { ... } fn span(self) -> Span<Self, I, O, E> where Self: Sized, I: Stream + Location { ... } fn with_span(self) -> WithSpan<Self, I, O, E> where Self: Sized, I: Stream + Location { ... } fn map<G, O2>(self, map: G) -> Map<Self, G, I, O, O2, E> where G: FnMut(O) -> O2, Self: Sized { ... } fn try_map<G, O2, E2>(self, map: G) -> TryMap<Self, G, I, O, O2, E, E2> where Self: Sized, G: FnMut(O) -> Result<O2, E2>, I: Stream, E: FromExternalError<I, E2> + ParserError<I> { ... } fn verify_map<G, O2>(self, map: G) -> VerifyMap<Self, G, I, O, O2, E> where Self: Sized, G: FnMut(O) -> Option<O2>, I: Stream, E: ParserError<I> { ... } fn flat_map<G, H, O2>(self, map: G) -> FlatMap<Self, G, H, I, O, O2, E> where Self: Sized, G: FnMut(O) -> H, H: Parser<I, O2, E> { ... } fn and_then<G, O2>(self, inner: G) -> AndThen<Self, G, I, O, O2, E> where Self: Sized, G: Parser<O, O2, E>, O: StreamIsPartial, I: Stream { ... } fn parse_to<O2>(self) -> ParseTo<Self, I, O, O2, E> where Self: Sized, I: Stream, O: ParseSlice<O2>, E: ParserError<I> { ... } fn verify<G, O2>(self, filter: G) -> Verify<Self, G, I, O, O2, E> where Self: Sized, G: FnMut(&O2) -> bool, I: Stream, O: Borrow<O2>, E: ParserError<I>, O2: ?Sized { ... } fn context<C>(self, context: C) -> Context<Self, I, O, E, C> where Self: Sized, I: Stream, E: AddContext<I, C> + ParserError<I>, C: Clone + Debug { ... } fn context_with<F, C, FI>( self, context: F, ) -> ContextWith<Self, I, O, E, F, C, FI> where Self: Sized, I: Stream, E: AddContext<I, C> + ParserError<I>, F: Fn() -> FI + Clone, C: Debug, FI: Iterator<Item = C> { ... } fn map_err<G, E2>(self, map: G) -> MapErr<Self, G, I, O, E, E2> where G: FnMut(E) -> E2, Self: Sized { ... } fn complete_err(self) -> CompleteErr<Self, I, O, E> where Self: Sized { ... } fn err_into<E2>(self) -> ErrInto<Self, I, O, E, E2> where Self: Sized, E: Into<E2> { ... }
}
Expand description

Core trait for parsing

The simplest way to implement a Parser is with a function

use winnow::prelude::*;

fn empty(input: &mut &str) -> ModalResult<()> {
    let output = ();
    Ok(output)
}

let (input, output) = empty.parse_peek("Hello").unwrap();
assert_eq!(input, "Hello");  // We didn't consume any input

which can be made stateful by returning a function

use winnow::prelude::*;

fn empty<O: Clone>(output: O) -> impl FnMut(&mut &str) -> ModalResult<O> {
    move |input: &mut &str| {
        let output = output.clone();
        Ok(output)
    }
}

let (input, output) = empty("World").parse_peek("Hello").unwrap();
assert_eq!(input, "Hello");  // We didn't consume any input
assert_eq!(output, "World");

Additionally, some basic types implement Parser as well, including

  • u8 and char, see [winnow::token::one_of][crate::token::one_of]
  • &[u8] and &str, see [winnow::token::literal][crate::token::literal]

Required Methods§

fn parse_next(&mut self, input: &mut I) -> Result<O, E>

Take tokens from the [Stream], turning it into the output

This includes advancing the input [Stream] to the next location.

On error, input will be left pointing at the error location.

This is intended for a Parser to drive another Parser forward or for [incremental parsing][StreamIsPartial]

Provided Methods§

fn parse( &mut self, input: I, ) -> Result<O, ParseError<I, <E as ParserError<I>>::Inner>>
where Self: Sized, I: Stream + StreamIsPartial, E: ParserError<I>, <E as ParserError<I>>::Inner: ParserError<I>,

Parse all of input, generating O from it

This is intended for integrating your parser into the rest of your application.

For one Parser to drive another Parser forward or for [incremental parsing][StreamIsPartial], see instead Parser::parse_next.

This assumes the Parser intends to read all of input and will return an [eof][crate::combinator::eof] error if it does not. To ignore trailing input, combine your parser with a [rest][crate::token::rest] (e.g. (parser, rest).parse(input)).

See also the [tutorial][crate::_tutorial::chapter_6].

fn parse_iter(&mut self, input: I) -> ParseIter<'_, Self, I, O, E> ⓘ
where Self: Sized, I: Stream + StreamIsPartial, E: ParserError<I>, <E as ParserError<I>>::Inner: ParserError<I>,

Repeat this parse until all of input is consumed, generating O from it

This is intended for integrating your parser into the rest of your application. To instead iterate inside of a parser, see [iterator][crate::combinator::iterator].

This assumes the Parser intends to read all of input and will return an [eof][crate::combinator::eof] error if it does not.

§Example
fn number(input: &mut &str) -> Result<u32, ()> {
  terminated(dec_uint, opt(newline)).parse_next(input)
}

let input = "10\n20\n30";
let numbers = number.parse_iter(input)
    .map(|r| r.unwrap()).collect::<Vec<_>>();
assert_eq!(numbers, vec![10, 20, 30]);

fn parse_peek(&mut self, input: I) -> Result<(I, O), E>

Take tokens from the [Stream], turning it into the output

This returns a copy of the [Stream] advanced 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].

fn by_ref(&mut self) -> ByRef<'_, Self, I, O, E>
where Self: Sized,

Treat &mut Self as a parser

This helps when needing to move a Parser when all you have is a &mut Parser.

§Example

Because parsers are FnMut, they can be called multiple times. This prevents moving f into [length_take][crate::binary::length_take] and g into Parser::complete_err:

ⓘ
pub fn length_value<'i, O, E: ParserError<&'i [u8]>>(
    mut f: impl Parser<&'i [u8], usize, E>,
    mut g: impl Parser<&'i [u8], O, E>
) -> impl Parser<&'i [u8], O, E> {
  move |i: &mut &'i [u8]| {
    let mut data = length_take(f).parse_next(i)?;
    let o = g.complete_err().parse_next(&mut data)?;
    Ok(o)
  }
}

By adding by_ref, we can make this work:

pub fn length_value<'i, O, E: ParserError<&'i [u8]>>(
    mut f: impl Parser<&'i [u8], usize, E>,
    mut g: impl Parser<&'i [u8], O, E>
) -> impl Parser<&'i [u8], O, E> {
  move |i: &mut &'i [u8]| {
    let mut data = length_take(f.by_ref()).parse_next(i)?;
    let o = g.by_ref().complete_err().parse_next(&mut data)?;
    Ok(o)
  }
}

fn value<O2>(self, val: O2) -> Value<Self, I, O, O2, E>
where Self: Sized, O2: Clone,

Produce the provided value

§Example
use winnow::ascii::alpha1;

fn parser<'i>(input: &mut &'i str) -> ModalResult<i32> {
    alpha1.value(1234).parse_next(input)
}

assert_eq!(parser.parse_peek("abcd"), Ok(("", 1234)));
assert!(parser.parse_peek("123abcd;").is_err());

fn default_value<O2>(self) -> DefaultValue<Self, I, O, O2, E>
where Self: Sized, O2: Default,

Produce a type’s default value

§Example
use winnow::ascii::alpha1;

fn parser<'i>(input: &mut &'i str) -> ModalResult<u32> {
    alpha1.default_value().parse_next(input)
}

assert_eq!(parser.parse_peek("abcd"), Ok(("", 0)));
assert!(parser.parse_peek("123abcd;").is_err());

fn void(self) -> Void<Self, I, O, E>
where Self: Sized,

Discards the output of the Parser

§Example
use winnow::ascii::alpha1;

fn parser<'i>(input: &mut &'i str) -> ModalResult<()> {
    alpha1.void().parse_next(input)
}

assert_eq!(parser.parse_peek("abcd"), Ok(("", ())));
assert!(parser.parse_peek("123abcd;").is_err());

fn output_into<O2>(self) -> OutputInto<Self, I, O, O2, E>
where Self: Sized, O: Into<O2>,

Convert the parser’s output to another type using std::convert::From

§Example
use winnow::ascii::alpha1;

fn parser1<'s>(i: &mut &'s str) -> ModalResult<&'s str> {
  alpha1(i)
}

let mut parser2 = parser1.output_into();

// the parser converts the &str output of the child parser into a Vec<u8>
let bytes: ModalResult<(_, Vec<u8>), _> = parser2.parse_peek("abcd");
assert_eq!(bytes, Ok(("", vec![97, 98, 99, 100])));

fn take(self) -> Take<Self, I, O, E>
where Self: Sized, I: Stream,

Produce the consumed input as produced value.

§Example
use winnow::ascii::{alpha1};
use winnow::combinator::separated_pair;

fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
    separated_pair(alpha1, ',', alpha1).take().parse_next(input)
}

assert_eq!(parser.parse_peek("abcd,efgh"), Ok(("", "abcd,efgh")));
assert!(parser.parse_peek("abcd;").is_err());

fn with_taken(self) -> WithTaken<Self, I, O, E>
where Self: Sized, I: Stream,

Produce the consumed input with the output

Functions similarly to 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 winnow::ascii::{alpha1};
use winnow::token::literal;
use winnow::combinator::separated_pair;

fn parser<'i>(input: &mut &'i str) -> ModalResult<(bool, &'i str)> {
    separated_pair(alpha1, ',', alpha1).value(true).with_taken().parse_next(input)
}

assert_eq!(parser.parse_peek("abcd,efgh1"), Ok(("1", (true, "abcd,efgh"))));
assert!(parser.parse_peek("abcd;").is_err());

fn span(self) -> Span<Self, I, O, E>
where Self: Sized, I: Stream + Location,

Produce the location of the consumed input as produced value.

§Example
use winnow::stream::LocatingSlice;
use winnow::ascii::alpha1;
use winnow::combinator::separated_pair;

fn parser<'i>(input: &mut LocatingSlice<&'i str>) -> ModalResult<(Range<usize>, Range<usize>)> {
    separated_pair(alpha1.span(), ',', alpha1.span()).parse_next(input)
}

assert_eq!(parser.parse(LocatingSlice::new("abcd,efgh")), Ok((0..4, 5..9)));
assert!(parser.parse_peek(LocatingSlice::new("abcd;")).is_err());

fn with_span(self) -> WithSpan<Self, I, O, E>
where Self: Sized, I: Stream + Location,

Produce the location of consumed input with the output

Functions similarly to Parser::span 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 winnow::stream::LocatingSlice;
use winnow::ascii::alpha1;
use winnow::token::literal;
use winnow::combinator::separated_pair;

fn parser<'i>(input: &mut LocatingSlice<&'i str>) -> ModalResult<((usize, Range<usize>), (usize, Range<usize>))> {
    separated_pair(alpha1.value(1).with_span(), ',', alpha1.value(2).with_span()).parse_next(input)
}

assert_eq!(parser.parse(LocatingSlice::new("abcd,efgh")), Ok(((1, 0..4), (2, 5..9))));
assert!(parser.parse_peek(LocatingSlice::new("abcd;")).is_err());

fn map<G, O2>(self, map: G) -> Map<Self, G, I, O, O2, E>
where G: FnMut(O) -> O2, Self: Sized,

Maps a function over the output of a parser

§Example

fn parser<'i>(input: &mut &'i str) -> ModalResult<usize> {
    digit1.map(|s: &str| s.len()).parse_next(input)
}

// the parser will count how many characters were returned by digit1
assert_eq!(parser.parse_peek("123456"), Ok(("", 6)));

// this will fail if digit1 fails
assert!(parser.parse_peek("abc").is_err());

fn try_map<G, O2, E2>(self, map: G) -> TryMap<Self, G, I, O, O2, E, E2>
where Self: Sized, G: FnMut(O) -> Result<O2, E2>, I: Stream, E: FromExternalError<I, E2> + ParserError<I>,

Applies a function returning a Result over the output of a parser.

§Example
use winnow::ascii::digit1;

fn parser<'i>(input: &mut &'i str) -> ModalResult<u8> {
    digit1.try_map(|s: &str| s.parse::<u8>()).parse_next(input)
}

// the parser will convert the result of digit1 to a number
assert_eq!(parser.parse_peek("123"), Ok(("", 123)));

// this will fail if digit1 fails
assert!(parser.parse_peek("abc").is_err());

// this will fail if the mapped function fails (a `u8` is too small to hold `123456`)
assert!(parser.parse_peek("123456").is_err());

fn verify_map<G, O2>(self, map: G) -> VerifyMap<Self, G, I, O, O2, E>
where Self: Sized, G: FnMut(O) -> Option<O2>, I: Stream, E: ParserError<I>,

Apply both Parser::verify and Parser::map.

§Example
use winnow::ascii::digit1;

fn parser<'i>(input: &mut &'i str) -> ModalResult<u8> {
    digit1.verify_map(|s: &str| s.parse::<u8>().ok()).parse_next(input)
}

// the parser will convert the result of digit1 to a number
assert_eq!(parser.parse_peek("123"), Ok(("", 123)));

// this will fail if digit1 fails
assert!(parser.parse_peek("abc").is_err());

// this will fail if the mapped function fails (a `u8` is too small to hold `123456`)
assert!(parser.parse_peek("123456").is_err());

fn flat_map<G, H, O2>(self, map: G) -> FlatMap<Self, G, H, I, O, O2, E>
where Self: Sized, G: FnMut(O) -> H, H: Parser<I, O2, E>,

Creates a parser from the output of this one

§Example
use winnow::token::take;
use winnow::binary::u8;

fn length_take<'s>(input: &mut &'s [u8]) -> ModalResult<&'s [u8]> {
    u8.flat_map(take).parse_next(input)
}

assert_eq!(length_take.parse_peek(&[2, 0, 1, 2][..]), Ok((&[2][..], &[0, 1][..])));
assert!(length_take.parse_peek(&[4, 0, 1, 2][..]).is_err());

which is the same as

use winnow::token::take;
use winnow::binary::u8;

fn length_take<'s>(input: &mut &'s [u8]) -> ModalResult<&'s [u8]> {
    let length = u8.parse_next(input)?;
    let data = take(length).parse_next(input)?;
    Ok(data)
}

assert_eq!(length_take.parse_peek(&[2, 0, 1, 2][..]), Ok((&[2][..], &[0, 1][..])));
assert!(length_take.parse_peek(&[4, 0, 1, 2][..]).is_err());

fn and_then<G, O2>(self, inner: G) -> AndThen<Self, G, I, O, O2, E>
where Self: Sized, G: Parser<O, O2, E>, O: StreamIsPartial, I: Stream,

Applies a second parser over the output of the first one

§Example
use winnow::ascii::digit1;
use winnow::token::take;

fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
    take(5u8).and_then(digit1).parse_next(input)
}

assert_eq!(parser.parse_peek("12345"), Ok(("", "12345")));
assert_eq!(parser.parse_peek("123ab"), Ok(("", "123")));
assert!(parser.parse_peek("123").is_err());

fn parse_to<O2>(self) -> ParseTo<Self, I, O, O2, E>
where Self: Sized, I: Stream, O: ParseSlice<O2>, E: ParserError<I>,

Apply std::str::FromStr to the output of the parser

§Example
use winnow::{error::ErrMode, Parser};
use winnow::ascii::digit1;

fn parser<'s>(input: &mut &'s str) -> ModalResult<u64> {
    digit1.parse_to().parse_next(input)
}

// the parser will count how many characters were returned by digit1
assert_eq!(parser.parse_peek("123456"), Ok(("", 123456)));

// this will fail if digit1 fails
assert!(parser.parse_peek("abc").is_err());

fn verify<G, O2>(self, filter: G) -> Verify<Self, G, I, O, O2, E>
where Self: Sized, G: FnMut(&O2) -> bool, I: Stream, O: Borrow<O2>, E: ParserError<I>, O2: ?Sized,

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

fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
    alpha1.verify(|s: &str| s.len() == 4).parse_next(input)
}

assert_eq!(parser.parse_peek("abcd"), Ok(("", "abcd")));
assert!(parser.parse_peek("abcde").is_err());
assert!(parser.parse_peek("123abcd;").is_err());

fn context<C>(self, context: C) -> Context<Self, I, O, E, C>
where Self: Sized, I: Stream, E: AddContext<I, C> + ParserError<I>, C: Clone + Debug,

If 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

fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
    digit1
      .context(StrContext::Expected(StrContextValue::Description("digit")))
      .parse_next(input)
}

assert_eq!(parser.parse_peek("123456"), Ok(("", "123456")));
assert!(parser.parse_peek("abc").is_err());

fn context_with<F, C, FI>( self, context: F, ) -> ContextWith<Self, I, O, E, F, C, FI>
where Self: Sized, I: Stream, E: AddContext<I, C> + ParserError<I>, F: Fn() -> FI + Clone, C: 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

fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
    digit1
      .context_with(|| {
        "0123456789".chars().map(|c| StrContext::Expected(c.into()))
      })
      .parse_next(input)
}

assert_eq!(parser.parse_peek("123456"), Ok(("", "123456")));
assert!(parser.parse_peek("abc").is_err());

fn map_err<G, E2>(self, map: G) -> MapErr<Self, G, I, O, E, E2>
where G: FnMut(E) -> E2, Self: Sized,

Maps a function over the error of a parser

§Example

fn parser<'i>(input: &mut &'i str) -> Result<&'i str> {
    digit1.map_err(|mut e: ContextError| {
        e.extend("0123456789".chars().map(|c| StrContext::Expected(c.into())));
        e
    }).parse_next(input)
}

assert_eq!(parser.parse_peek("123456"), Ok(("", "123456")));
assert!(parser.parse_peek("abc").is_err());

fn complete_err(self) -> CompleteErr<Self, I, O, E>
where Self: Sized,

Transforms [Incomplete][crate::error::ErrMode::Incomplete] into [Backtrack][crate::error::ErrMode::Backtrack]

§Example

fn parser<'i>(input: &mut Partial<&'i str>) -> ModalResult<&'i str, InputError<Partial<&'i str>>> {
    take(5u8).complete_err().parse_next(input)
}

assert_eq!(parser.parse_peek(Partial::new("abcdefg")), Ok((Partial::new("fg"), "abcde")));
assert_eq!(parser.parse_peek(Partial::new("abcd")), Err(ErrMode::Backtrack(InputError::at(Partial::new("abcd")))));

fn err_into<E2>(self) -> ErrInto<Self, I, O, E, E2>
where Self: Sized, E: Into<E2>,

Convert the parser’s error to another type using std::convert::From

Trait Implementations§

§

impl<I, O, E> Parser<I, O, E> for Box<dyn Parser<I, O, E> + '_>

Available on crate feature alloc only.
§

fn parse_next(&mut self, i: &mut I) -> Result<O, E>

Take tokens from the [Stream], turning it into the output Read more
§

fn parse( &mut self, input: I, ) -> Result<O, ParseError<I, <E as ParserError<I>>::Inner>>
where Self: Sized, I: Stream + StreamIsPartial, E: ParserError<I>, <E as ParserError<I>>::Inner: ParserError<I>,

Parse all of input, generating O from it Read more
§

fn parse_iter(&mut self, input: I) -> ParseIter<'_, Self, I, O, E> ⓘ
where Self: Sized, I: Stream + StreamIsPartial, E: ParserError<I>, <E as ParserError<I>>::Inner: ParserError<I>,

Repeat this parse until all of input is consumed, generating O from it Read more
§

fn parse_peek(&mut self, input: I) -> Result<(I, O), E>

Take tokens from the [Stream], turning it into the output Read more
§

fn by_ref(&mut self) -> ByRef<'_, Self, I, O, E>
where Self: Sized,

Treat &mut Self as a parser Read more
§

fn value<O2>(self, val: O2) -> Value<Self, I, O, O2, E>
where Self: Sized, O2: Clone,

Produce the provided value Read more
§

fn default_value<O2>(self) -> DefaultValue<Self, I, O, O2, E>
where Self: Sized, O2: Default,

Produce a type’s default value Read more
§

fn void(self) -> Void<Self, I, O, E>
where Self: Sized,

Discards the output of the Parser Read more
§

fn output_into<O2>(self) -> OutputInto<Self, I, O, O2, E>
where Self: Sized, O: Into<O2>,

Convert the parser’s output to another type using std::convert::From Read more
§

fn take(self) -> Take<Self, I, O, E>
where Self: Sized, I: Stream,

Produce the consumed input as produced value. Read more
§

fn with_taken(self) -> WithTaken<Self, I, O, E>
where Self: Sized, I: Stream,

Produce the consumed input with the output Read more
§

fn span(self) -> Span<Self, I, O, E>
where Self: Sized, I: Stream + Location,

Produce the location of the consumed input as produced value. Read more
§

fn with_span(self) -> WithSpan<Self, I, O, E>
where Self: Sized, I: Stream + Location,

Produce the location of consumed input with the output Read more
§

fn map<G, O2>(self, map: G) -> Map<Self, G, I, O, O2, E>
where G: FnMut(O) -> O2, Self: Sized,

Maps a function over the output of a parser Read more
§

fn try_map<G, O2, E2>(self, map: G) -> TryMap<Self, G, I, O, O2, E, E2>
where Self: Sized, G: FnMut(O) -> Result<O2, E2>, I: Stream, E: FromExternalError<I, E2> + ParserError<I>,

Applies a function returning a Result over the output of a parser. Read more
§

fn verify_map<G, O2>(self, map: G) -> VerifyMap<Self, G, I, O, O2, E>
where Self: Sized, G: FnMut(O) -> Option<O2>, I: Stream, E: ParserError<I>,

§

fn flat_map<G, H, O2>(self, map: G) -> FlatMap<Self, G, H, I, O, O2, E>
where Self: Sized, G: FnMut(O) -> H, H: Parser<I, O2, E>,

Creates a parser from the output of this one Read more
§

fn and_then<G, O2>(self, inner: G) -> AndThen<Self, G, I, O, O2, E>
where Self: Sized, G: Parser<O, O2, E>, O: StreamIsPartial, I: Stream,

Applies a second parser over the output of the first one Read more
§

fn parse_to<O2>(self) -> ParseTo<Self, I, O, O2, E>
where Self: Sized, I: Stream, O: ParseSlice<O2>, E: ParserError<I>,

Apply std::str::FromStr to the output of the parser Read more
§

fn verify<G, O2>(self, filter: G) -> Verify<Self, G, I, O, O2, E>
where Self: Sized, G: FnMut(&O2) -> bool, I: Stream, O: Borrow<O2>, E: ParserError<I>, O2: ?Sized,

Returns the output of the child parser if it satisfies a verification function. Read more
§

fn context<C>(self, context: C) -> Context<Self, I, O, E, C>
where Self: Sized, I: Stream, E: AddContext<I, C> + ParserError<I>, C: Clone + Debug,

If parsing fails, add context to the error Read more
§

fn context_with<F, C, FI>( self, context: F, ) -> ContextWith<Self, I, O, E, F, C, FI>
where Self: Sized, I: Stream, E: AddContext<I, C> + ParserError<I>, F: Fn() -> FI + Clone, C: Debug, FI: Iterator<Item = C>,

If parsing fails, dynamically add context to the error Read more
§

fn map_err<G, E2>(self, map: G) -> MapErr<Self, G, I, O, E, E2>
where G: FnMut(E) -> E2, Self: Sized,

Maps a function over the error of a parser Read more
§

fn complete_err(self) -> CompleteErr<Self, I, O, E>
where Self: Sized,

Transforms [Incomplete][crate::error::ErrMode::Incomplete] into [Backtrack][crate::error::ErrMode::Backtrack] Read more
§

fn err_into<E2>(self) -> ErrInto<Self, I, O, E, E2>
where Self: Sized, E: Into<E2>,

Convert the parser’s error to another type using std::convert::From

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

§

impl<'s, I, E, const N: usize> Parser<I, <I as Stream>::Slice, E> for &'s [u8; N]
where E: ParserError<I>, I: Compare<&'s [u8; N]> + StreamIsPartial + Stream,

This is a shortcut for [literal][crate::token::literal].

§Example


fn parser<'s>(s: &mut &'s [u8]) -> ModalResult<&'s [u8]> {
  alt((b"Hello", take(5usize))).parse_next(s)
}

assert_eq!(parser.parse_peek(&b"Hello, World!"[..]), Ok((&b", World!"[..], &b"Hello"[..])));
assert_eq!(parser.parse_peek(&b"Something"[..]), Ok((&b"hing"[..], &b"Somet"[..])));
assert!(parser.parse_peek(&b"Some"[..]).is_err());
assert!(parser.parse_peek(&b""[..]).is_err());
§

fn parse_next(&mut self, i: &mut I) -> Result<<I as Stream>::Slice, E>

§

impl<'s, I, E> Parser<I, <I as Stream>::Slice, E> for &'s [u8]
where E: ParserError<I>, I: Compare<&'s [u8]> + StreamIsPartial + Stream,

This is a shortcut for [literal][crate::token::literal].

§Example


fn parser<'s>(s: &mut &'s [u8]) -> ModalResult<&'s [u8]> {
  alt((&"Hello"[..], take(5usize))).parse_next(s)
}

assert_eq!(parser.parse_peek(&b"Hello, World!"[..]), Ok((&b", World!"[..], &b"Hello"[..])));
assert_eq!(parser.parse_peek(&b"Something"[..]), Ok((&b"hing"[..], &b"Somet"[..])));
assert!(parser.parse_peek(&b"Some"[..]).is_err());
assert!(parser.parse_peek(&b""[..]).is_err());
§

fn parse_next(&mut self, i: &mut I) -> Result<<I as Stream>::Slice, E>

§

impl<'s, I, E> Parser<I, <I as Stream>::Slice, E> for &'s str
where E: ParserError<I>, I: Compare<&'s str> + StreamIsPartial + Stream,

This is a shortcut for [literal][crate::token::literal].

§Example


fn parser<'s>(s: &mut &'s str) -> ModalResult<&'s str> {
  alt(("Hello", take(5usize))).parse_next(s)
}

assert_eq!(parser.parse_peek("Hello, World!"), Ok((", World!", "Hello")));
assert_eq!(parser.parse_peek("Something"), Ok(("hing", "Somet")));
assert!(parser.parse_peek("Some").is_err());
assert!(parser.parse_peek("").is_err());
§

fn parse_next(&mut self, i: &mut I) -> Result<<I as Stream>::Slice, E>

§

impl<I, E> Parser<I, (), E> for ()
where I: Stream, E: ParserError<I>,

§

fn parse_next(&mut self, _i: &mut I) -> Result<(), E>

§

impl<I, E> Parser<I, char, E> for char
where I: StreamIsPartial + Stream + Compare<char>, E: ParserError<I>,

This is a shortcut for [one_of][crate::token::one_of].

§Example

fn parser<'s>(i: &mut &'s str) -> ModalResult<char> {
    'a'.parse_next(i)
}
assert_eq!(parser.parse_peek("abc"), Ok(("bc", 'a')));
assert!(parser.parse_peek(" abc").is_err());
assert!(parser.parse_peek("bc").is_err());
assert!(parser.parse_peek("").is_err());
§

fn parse_next(&mut self, i: &mut I) -> Result<char, E>

§

impl<I, E> Parser<I, u8, E> for u8
where I: StreamIsPartial + Stream + Compare<u8>, E: ParserError<I>,

This is a shortcut for [one_of][crate::token::one_of].

§Example

fn parser<'s>(i: &mut &'s [u8]) -> ModalResult<u8>  {
    b'a'.parse_next(i)
}
assert_eq!(parser.parse_peek(&b"abc"[..]), Ok((&b"bc"[..], b'a')));
assert!(parser.parse_peek(&b" abc"[..]).is_err());
assert!(parser.parse_peek(&b"bc"[..]).is_err());
assert!(parser.parse_peek(&b""[..]).is_err());
§

fn parse_next(&mut self, i: &mut I) -> Result<u8, E>

§

impl<I, O0, E, P0> Parser<I, (O0,), E> for (P0,)
where I: Stream, E: ParserError<I>, P0: Parser<I, O0, E>,

§

fn parse_next(&mut self, i: &mut I) -> Result<(O0,), E>

§

impl<I, O0, O1, E, P0, P1> Parser<I, (O0, O1), E> for (P0, P1)
where I: Stream, E: ParserError<I>, P0: Parser<I, O0, E>, P1: Parser<I, O1, E>,

§

fn parse_next(&mut self, i: &mut I) -> Result<(O0, O1), E>

§

impl<I, O0, O1, O2, E, P0, P1, P2> Parser<I, (O0, O1, O2), E> for (P0, P1, P2)
where I: Stream, E: ParserError<I>, P0: Parser<I, O0, E>, P1: Parser<I, O1, E>, P2: Parser<I, O2, E>,

§

fn parse_next(&mut self, i: &mut I) -> Result<(O0, O1, O2), E>

§

impl<I, O0, O1, O2, O3, E, P0, P1, P2, P3> Parser<I, (O0, O1, O2, O3), E> for (P0, P1, P2, P3)
where I: Stream, E: ParserError<I>, P0: Parser<I, O0, E>, P1: Parser<I, O1, E>, P2: Parser<I, O2, E>, P3: Parser<I, O3, E>,

§

fn parse_next(&mut self, i: &mut I) -> Result<(O0, O1, O2, O3), E>

§

impl<I, O0, O1, O2, O3, O4, E, P0, P1, P2, P3, P4> Parser<I, (O0, O1, O2, O3, O4), E> for (P0, P1, P2, P3, P4)
where I: Stream, E: ParserError<I>, P0: Parser<I, O0, E>, P1: Parser<I, O1, E>, P2: Parser<I, O2, E>, P3: Parser<I, O3, E>, P4: Parser<I, O4, E>,

§

impl<I, O0, O1, O2, O3, O4, O5, E, P0, P1, P2, P3, P4, P5> Parser<I, (O0, O1, O2, O3, O4, O5), E> for (P0, P1, P2, P3, P4, P5)
where I: Stream, E: ParserError<I>, P0: Parser<I, O0, E>, P1: Parser<I, O1, E>, P2: Parser<I, O2, E>, P3: Parser<I, O3, E>, P4: Parser<I, O4, E>, P5: Parser<I, O5, E>,

§

impl<I, O0, O1, O2, O3, O4, O5, O6, E, P0, P1, P2, P3, P4, P5, P6> Parser<I, (O0, O1, O2, O3, O4, O5, O6), E> for (P0, P1, P2, P3, P4, P5, P6)
where I: Stream, E: ParserError<I>, P0: Parser<I, O0, E>, P1: Parser<I, O1, E>, P2: Parser<I, O2, E>, P3: Parser<I, O3, E>, P4: Parser<I, O4, E>, P5: Parser<I, O5, E>, P6: Parser<I, O6, E>,

§

impl<I, O0, O1, O2, O3, O4, O5, O6, O7, E, P0, P1, P2, P3, P4, P5, P6, P7> Parser<I, (O0, O1, O2, O3, O4, O5, O6, O7), E> for (P0, P1, P2, P3, P4, P5, P6, P7)
where I: Stream, E: ParserError<I>, P0: Parser<I, O0, E>, P1: Parser<I, O1, E>, P2: Parser<I, O2, E>, P3: Parser<I, O3, E>, P4: Parser<I, O4, E>, P5: Parser<I, O5, E>, P6: Parser<I, O6, E>, P7: Parser<I, O7, E>,

§

impl<I, O0, O1, O2, O3, O4, O5, O6, O7, O8, E, P0, P1, P2, P3, P4, P5, P6, P7, P8> Parser<I, (O0, O1, O2, O3, O4, O5, O6, O7, O8), E> for (P0, P1, P2, P3, P4, P5, P6, P7, P8)
where I: Stream, E: ParserError<I>, P0: Parser<I, O0, E>, P1: Parser<I, O1, E>, P2: Parser<I, O2, E>, P3: Parser<I, O3, E>, P4: Parser<I, O4, E>, P5: Parser<I, O5, E>, P6: Parser<I, O6, E>, P7: Parser<I, O7, E>, P8: Parser<I, O8, E>,

§

impl<I, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, E, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9> Parser<I, (O0, O1, O2, O3, O4, O5, O6, O7, O8, O9), E> for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9)
where I: Stream, E: ParserError<I>, P0: Parser<I, O0, E>, P1: Parser<I, O1, E>, P2: Parser<I, O2, E>, P3: Parser<I, O3, E>, P4: Parser<I, O4, E>, P5: Parser<I, O5, E>, P6: Parser<I, O6, E>, P7: Parser<I, O7, E>, P8: Parser<I, O8, E>, P9: Parser<I, O9, E>,

§

impl<I, O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, E, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10> Parser<I, (O0, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10), E> for (P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10)
where I: Stream, E: ParserError<I>, P0: Parser<I, O0, E>, P1: Parser<I, O1, E>, P2: Parser<I, O2, E>, P3: Parser<I, O3, E>, P4: Parser<I, O4, E>, P5: Parser<I, O5, E>, P6: Parser<I, O6, E>, P7: Parser<I, O7, E>, P8: Parser<I, O8, E>, P9: Parser<I, O9, E>, P10: Parser<I, O10, E>,

§

impl<I, O, E> Parser<I, O, E> for Box<dyn Parser<I, O, E> + '_>

Available on crate feature alloc only.
§

fn parse_next(&mut self, i: &mut I) -> Result<O, E>

Implementors§

Source§

impl<'i, O, P> Parser<LocatingSlice<&'i str>, O, ErrMode<ParseStack<'i>>> for ContextParser<P>
where P: Parser<Input<'i>, O, ErrMode<ParseStack<'i>>>,

Source§

impl<'i, O, P> Parser<LocatingSlice<&'i str>, O, ErrMode<ParseStack<'i>>> for LayerParser<P>
where P: Parser<Input<'i>, O, ErrMode<ParseStack<'i>>>,

§

impl<'s, I, E, const N: usize> Parser<I, <I as Stream>::Slice, E> for Caseless<&'s [u8; N]>
where E: ParserError<I>, I: Compare<Caseless<&'s [u8; N]>> + StreamIsPartial + Stream,

This is a shortcut for [literal][crate::token::literal].

§Example

use winnow::ascii::Caseless;

fn parser<'s>(s: &mut &'s [u8]) -> ModalResult<&'s [u8]> {
  alt((Caseless(b"hello"), take(5usize))).parse_next(s)
}

assert_eq!(parser.parse_peek(&b"Hello, World!"[..]), Ok((&b", World!"[..], &b"Hello"[..])));
assert_eq!(parser.parse_peek(&b"hello, World!"[..]), Ok((&b", World!"[..], &b"hello"[..])));
assert_eq!(parser.parse_peek(&b"HeLlo, World!"[..]), Ok((&b", World!"[..], &b"HeLlo"[..])));
assert_eq!(parser.parse_peek(&b"Something"[..]), Ok((&b"hing"[..], &b"Somet"[..])));
assert!(parser.parse_peek(&b"Some"[..]).is_err());
assert!(parser.parse_peek(&b""[..]).is_err());
§

impl<'s, I, E> Parser<I, <I as Stream>::Slice, E> for Caseless<&'s [u8]>
where E: ParserError<I>, I: Compare<Caseless<&'s [u8]>> + StreamIsPartial + Stream,

This is a shortcut for [literal][crate::token::literal].

§Example

use winnow::ascii::Caseless;

fn parser<'s>(s: &mut &'s [u8]) -> ModalResult<&'s [u8]> {
  alt((Caseless(&"hello"[..]), take(5usize))).parse_next(s)
}

assert_eq!(parser.parse_peek(&b"Hello, World!"[..]), Ok((&b", World!"[..], &b"Hello"[..])));
assert_eq!(parser.parse_peek(&b"hello, World!"[..]), Ok((&b", World!"[..], &b"hello"[..])));
assert_eq!(parser.parse_peek(&b"HeLlo, World!"[..]), Ok((&b", World!"[..], &b"HeLlo"[..])));
assert_eq!(parser.parse_peek(&b"Something"[..]), Ok((&b"hing"[..], &b"Somet"[..])));
assert!(parser.parse_peek(&b"Some"[..]).is_err());
assert!(parser.parse_peek(&b""[..]).is_err());
§

impl<'s, I, E> Parser<I, <I as Stream>::Slice, E> for Caseless<&'s str>
where E: ParserError<I>, I: Compare<Caseless<&'s str>> + StreamIsPartial + Stream,

This is a shortcut for [literal][crate::token::literal].

§Example


fn parser<'s>(s: &mut &'s str) -> ModalResult<&'s str> {
  alt((Caseless("hello"), take(5usize))).parse_next(s)
}

assert_eq!(parser.parse_peek("Hello, World!"), Ok((", World!", "Hello")));
assert_eq!(parser.parse_peek("hello, World!"), Ok((", World!", "hello")));
assert_eq!(parser.parse_peek("HeLlo, World!"), Ok((", World!", "HeLlo")));
assert_eq!(parser.parse_peek("Something"), Ok(("hing", "Somet")));
assert!(parser.parse_peek("Some").is_err());
assert!(parser.parse_peek("").is_err());
§

impl<F, G, H, I, O, O2, E> Parser<I, O2, E> for FlatMap<F, G, H, I, O, O2, E>
where F: Parser<I, O, E>, G: FnMut(O) -> H, H: Parser<I, O2, E>,

§

impl<F, G, I, O, E, E2> Parser<I, O, E2> for MapErr<F, G, I, O, E, E2>
where F: Parser<I, O, E>, G: FnMut(E) -> E2,

§

impl<F, G, I, O, O2, E, E2> Parser<I, O2, E> for TryMap<F, G, I, O, O2, E, E2>
where F: Parser<I, O, E>, G: FnMut(O) -> Result<O2, E2>, I: Stream, E: FromExternalError<I, E2> + ParserError<I>,

§

impl<F, G, I, O, O2, E> Parser<I, O2, E> for AndThen<F, G, I, O, O2, E>
where F: Parser<I, O, E>, G: Parser<O, O2, E>, O: StreamIsPartial, I: Stream,

§

impl<F, G, I, O, O2, E> Parser<I, O2, E> for Map<F, G, I, O, O2, E>
where F: Parser<I, O, E>, G: FnMut(O) -> O2,

§

impl<F, G, I, O, O2, E> Parser<I, O2, E> for VerifyMap<F, G, I, O, O2, E>
where F: Parser<I, O, E>, G: FnMut(O) -> Option<O2>, I: Stream, E: ParserError<I>,

§

impl<F, G, I, O, O2, E> Parser<I, O, E> for Verify<F, G, I, O, O2, E>
where F: Parser<I, O, E>, G: FnMut(&O2) -> bool, I: Stream, O: Borrow<O2>, E: ParserError<I>, O2: ?Sized,

§

impl<F, I, O, E, C> Parser<I, O, E> for Context<F, I, O, E, C>
where F: Parser<I, O, E>, I: Stream, E: AddContext<I, C> + ParserError<I>, C: Clone + Debug,

§

impl<F, I, O, E, E2> Parser<I, O, E2> for ErrInto<F, I, O, E, E2>
where F: Parser<I, O, E>, E: Into<E2>,

§

impl<F, I, O, E> Parser<I, (), E> for Void<F, I, O, E>
where F: Parser<I, O, E>,

§

impl<F, I, O, E> Parser<I, (O, <I as Stream>::Slice), E> for WithTaken<F, I, O, E>
where F: Parser<I, O, E>, I: Stream,

§

impl<F, I, O, E> Parser<I, (O, Range<usize>), E> for WithSpan<F, I, O, E>
where F: Parser<I, O, E>, I: Stream + Location,

§

impl<F, I, O, O2, E> Parser<I, O2, E> for DefaultValue<F, I, O, O2, E>
where F: Parser<I, O, E>, O2: Default,

§

impl<F, I, O, O2, E> Parser<I, O2, E> for OutputInto<F, I, O, O2, E>
where F: Parser<I, O, E>, O: Into<O2>,

§

impl<F, I, O, O2, E> Parser<I, O2, E> for Value<F, I, O, O2, E>
where F: Parser<I, O, E>, O2: Clone,

§

impl<I, O, E, F> Parser<I, <I as Stream>::Slice, E> for Take<F, I, O, E>
where F: Parser<I, O, E>, I: Stream,

§

impl<I, O, E, F> Parser<I, O, E> for F
where F: FnMut(&mut I) -> Result<O, E>, I: Stream,

§

impl<I, O, E, F> Parser<I, Range<usize>, E> for Span<F, I, O, E>
where F: Parser<I, O, E>, I: Stream + Location,

§

impl<I, O, E, P> Parser<I, O, E> for ByRef<'_, P, I, O, E>
where P: Parser<I, O, E>,

§

impl<I, O, E> Parser<I, Infix<I, O, E>, E> for Infix<I, O, E>
where I: Stream, E: ParserError<I>,

§

impl<I, O, E> Parser<I, Postfix<I, O, E>, E> for Postfix<I, O, E>
where I: Stream, E: ParserError<I>,

§

impl<I, O, E> Parser<I, Prefix<I, O, E>, E> for Prefix<I, O, E>
where I: Stream, E: ParserError<I>,

§

impl<I, O, Pop, Pre, Post, Pix, E> Parser<I, O, E> for Expression<I, O, Pop, Pre, Post, Pix, E>
where I: Stream + StreamIsPartial, Pop: Parser<I, O, E>, Pix: Parser<I, Infix<I, O, E>, E>, Pre: Parser<I, Prefix<I, O, E>, E>, Post: Parser<I, Postfix<I, O, E>, E>, E: ParserError<I>,

§

impl<P, I, O, C, E> Parser<I, C, E> for Repeat<P, I, O, C, E>
where P: Parser<I, O, E>, I: Stream, C: Accumulate<O>, E: ParserError<I>,

§

impl<P, I, O, E, F, C, FI> Parser<I, O, E> for ContextWith<P, I, O, E, F, C, FI>
where P: Parser<I, O, E>, I: Stream, E: AddContext<I, C> + ParserError<I>, F: Fn() -> FI + Clone, C: Debug, FI: Iterator<Item = C>,

§

impl<P, I, O, E> Parser<I, O, E> for CompleteErr<P, I, O, E>
where P: Parser<I, O, E>, I: Stream, E: ParserError<I>,

§

impl<P, I, O, O2, E> Parser<I, O2, E> for ParseTo<P, I, O, O2, E>
where P: Parser<I, O, E>, I: Stream, O: ParseSlice<O2>, E: ParserError<I>,