Module winnow::combinator

source ·
Expand description

§List of parsers and combinators

Note: this list is meant to provide a nicer way to find a parser than reading through the documentation on docs.rs. Function combinators are organized in module so they are a bit easier to find.

§Basic elements

Those are used to take a series of tokens for the lowest level elements of your grammar, like, “here is a dot”, or “here is an big endian integer”.

combinatorusageinputnew inputoutputcomment
one_ofone_of(['a', 'b', 'c'])"abc""bc"Ok('a')Matches one of the provided set of tokens (works with non ASCII characters too)
none_ofnone_of(['a', 'b', 'c'])"xyab""yab"Ok('x')Matches anything but one of the provided set of tokens
literal"hello""hello world"" world"Ok("hello")Recognizes a specific suite of characters or bytes (see also Caseless)
taketake(4)"hello""o"Ok("hell")Takes a specific number of bytes or characters
take_whiletake_while(0.., is_alphabetic)"abc123""123"Ok("abc")Returns the longest slice of bytes or characters for which the provided set of tokens matches.
take_tilltake_till(0.., is_alphabetic)"123abc""abc"Ok("123")Returns a slice of bytes or characters until the provided set of tokens matches. This is the reverse behaviour from take_while: take_till(f) is equivalent to take_while(0.., \|c\| !f(c))
take_untiltake_until(0.., "world")"Hello world""world"Ok("Hello ")Returns a slice of bytes or characters until the provided literal is found.

§Choice combinators

combinatorusageinputnew inputoutputcomment
altalt(("ab", "cd"))"cdef""ef"Ok("cd")Try a list of parsers and return the result of the first successful one
dispatch----match for parsers
permutationpermutation(("ab", "cd", "12"))"cd12abc""c"Ok(("ab", "cd", "12"))Succeeds when all its child parser have succeeded, whatever the order

§Sequence combinators

combinatorusageinputnew inputoutputcomment
(...) (tuples)("ab", "XY", take(1))"abXYZ!""!"Ok(("ab", "XY", "Z"))Chains parsers and assemble the sub results in a tuple. You can use as many child parsers as you can put elements in a tuple
seq!seq!(_: '(', take(2), _: ')')"(ab)cd""cd"Ok("ab")
delimiteddelimited('(', take(2), ')')"(ab)cd""cd"Ok("ab")
precededpreceded("ab", "XY")"abXYZ""Z"Ok("XY")
terminatedterminated("ab", "XY")"abXYZ""Z"Ok("ab")
separated_pairseparated_pair("hello", ',', "world")"hello,world!""!"Ok(("hello", "world"))

§Applying a parser multiple times

combinatorusageinputnew inputoutputcomment
repeatrepeat(1..=3, "ab")"ababc""c"Ok(vec!["ab", "ab"])Applies the parser between m and n times (n included) and returns the list of results in a Vec
repeat_tillrepeat_till(0.., "ab", "ef")"ababefg""g"Ok((vec!["ab", "ab"], "ef"))Applies the first parser until the second applies. Returns a tuple containing the list of results from the first in a Vec and the result of the second
separatedseparated(1..=3, "ab", ",")"ab,ab,ab.""."Ok(vec!["ab", "ab", "ab"])Applies the parser and separator between m and n times (n included) and returns the list of results in a Vec
Repeat::fold`repeat(1..=2, be_u8).fold(0,acc, itemacc + item)`
  • eof: Returns its input if it is at the end of input data
  • Parser::complete_err: Replaces an Incomplete returned by the child parser with an Backtrack

§Modifiers

  • cond: Conditional combinator. Wraps another parser and calls it if the condition is met
  • Parser::flat_map: method to map a new parser from the output of the first parser, then apply that parser over the rest of the input
  • Parser::value: method to replace the result of a parser
  • Parser::default_value: method to replace the result of a parser
  • Parser::void: method to discard the result of a parser
  • Parser::map: method to map a function on the result of a parser
  • Parser::and_then: Applies a second parser over the output of the first one
  • Parser::verify_map: Maps a function returning an Option on the output of a parser
  • Parser::try_map: Maps a function returning a Result on the output of a parser
  • Parser::parse_to: Apply std::str::FromStr to the output of the parser
  • not: Returns a result only if the embedded parser returns Backtrack or Incomplete. Does not consume the input
  • opt: Make the underlying parser optional
  • peek: Returns a result without consuming the input
  • Parser::take: If the child parser was successful, return the consumed input as the produced value
  • Parser::with_taken: If the child parser was successful, return a tuple of the consumed input and the produced output.
  • Parser::span: If the child parser was successful, return the location of the consumed input as the produced value
  • Parser::with_span: If the child parser was successful, return a tuple of the location of the consumed input and the produced output.
  • Parser::verify: Returns the result of the child parser if it satisfies a verification function

§Error management and debugging

  • cut_err: Commit the parse result, disallowing alternative parsers from being attempted
  • backtrack_err: Attempts a parse, allowing alternative parsers to be attempted despite use of cut_err
  • Parser::context: Add context to the error if the parser fails
  • trace: Print the parse state with the debug feature flag
  • todo(): Placeholder parser

§Remaining combinators

  • empty: Returns a value without consuming any input, always succeeds
  • fail: Inversion of empty. Always fails.
  • Parser::by_ref: Allow moving &mut impl Parser into other parsers

§Text parsing

  • any: Matches one token

  • tab: Matches a tab character \t

  • crlf: Recognizes the string \r\n

  • line_ending: Recognizes an end of line (both \n and \r\n)

  • newline: Matches a newline character \n

  • till_line_ending: Recognizes a string of any char except \r or \n

  • rest: Return the remaining input

  • alpha0: Recognizes zero or more lowercase and uppercase alphabetic characters: [a-zA-Z]. alpha1 does the same but returns at least one character

  • alphanumeric0: Recognizes zero or more numerical and alphabetic characters: [0-9a-zA-Z]. alphanumeric1 does the same but returns at least one character

  • space0: Recognizes zero or more spaces and tabs. space1 does the same but returns at least one character

  • multispace0: Recognizes zero or more spaces, tabs, carriage returns and line feeds. multispace1 does the same but returns at least one character

  • digit0: Recognizes zero or more numerical characters: [0-9]. digit1 does the same but returns at least one character

  • hex_digit0: Recognizes zero or more hexadecimal numerical characters: [0-9A-Fa-f]. hex_digit1 does the same but returns at least one character

  • oct_digit0: Recognizes zero or more octal characters: [0-7]. oct_digit1 does the same but returns at least one character

  • float: Parse a floating point number in a byte string

  • dec_int: Decode a variable-width, decimal signed integer

  • dec_uint: Decode a variable-width, decimal unsigned integer

  • hex_uint: Decode a variable-width, hexadecimal integer

  • take_escaped: Recognize the input slice with escaped characters

  • escaped_transform: Parse escaped characters, unescaping them

§Character test functions

Use these functions with a combinator like take_while:

§Binary format parsing

  • length_repeat Gets a number from the first parser, then applies the second parser that many times
  • length_take: Gets a number from the first parser, then takes a subslice of the input of that size, and returns that subslice
  • length_and_then: Gets a number from the first parser, takes a subslice of the input of that size, then applies the second parser on that subslice. If the second parser returns Incomplete, length_value will return an error

§Integers

Parsing integers from binary formats can be done in two ways: With parser functions, or combinators with configurable endianness.

§Bit stream parsing

  • bits: Transforms the current input type (byte slice &[u8]) to a bit stream on which bit specific parsers and more general combinators can be applied
  • bytes: Transforms its bits stream input back into a byte slice for the underlying parser
  • take: Take a set number of bits
  • pattern: Check if a set number of bits matches a pattern
  • bool: Match any one bit

Macros§

  • match for parsers
  • Initialize a struct or tuple out of a sequences of parsers

Structs§

Traits§

Functions§

Type Aliases§