| 1 | //! # List of parsers and combinators | 
| 2 | //! | 
|---|
| 3 | //! **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. | 
|---|
| 4 | //! | 
|---|
| 5 | //! ## Basic elements | 
|---|
| 6 | //! | 
|---|
| 7 | //! Those are used to recognize the lowest level elements of your grammar, like, "here is a dot", or "here is an big endian integer". | 
|---|
| 8 | //! | 
|---|
| 9 | //! | combinator | usage | input | new input | output | comment | | 
|---|
| 10 | //! |---|---|---|---|---|---| | 
|---|
| 11 | //! | [`one_of`][crate::token::one_of] | `one_of(['a', 'b', 'c'])` |  `"abc"` |  `"bc"` | `Ok('a')` |Matches one of the provided characters (works with non ASCII characters too)| | 
|---|
| 12 | //! | [`none_of`][crate::token::none_of] | `none_of(['a', 'b', 'c'])` |  `"xyab"` |  `"yab"` | `Ok('x')` |Matches anything but the provided characters| | 
|---|
| 13 | //! | [`tag`][crate::token::tag] | `"hello"` |  `"hello world"` |  `" world"` | `Ok("hello")` |Recognizes a specific suite of characters or bytes (see also [`Caseless`][crate::ascii::Caseless])| | 
|---|
| 14 | //! | [`take`][crate::token::take] | `take(4)` |  `"hello"` |  `"o"` | `Ok("hell")` |Takes a specific number of bytes or characters| | 
|---|
| 15 | //! | [`take_while`][crate::token::take_while] | `take_while(0.., is_alphabetic)` |  `"abc123"` |  `"123"` | `Ok("abc")` |Returns the longest list of bytes for which the provided pattern matches.| | 
|---|
| 16 | //! | [`take_till0`][crate::token::take_till0] | `take_till0(is_alphabetic)` |  `"123abc"` |  `"abc"` | `Ok("123")` |Returns the longest list of bytes or characters until the provided pattern matches. `take_till1` does the same, but must return at least one character. This is the reverse behaviour from `take_while`: `take_till(f)` is equivalent to `take_while(0.., \|c\| !f(c))`| | 
|---|
| 17 | //! | [`take_until`][crate::token::take_until] | `take_until(0.., "world")` |  `"Hello world"` |  `"world"` | `Ok("Hello ")` |Returns the longest list of bytes or characters until the provided tag is found.| | 
|---|
| 18 | //! | 
|---|
| 19 | //! ## Choice combinators | 
|---|
| 20 | //! | 
|---|
| 21 | //! | combinator | usage | input | new input | output | comment | | 
|---|
| 22 | //! |---|---|---|---|---|---| | 
|---|
| 23 | //! | [`alt`] | `alt(("ab", "cd"))` |  `"cdef"` |  `"ef"` | `Ok("cd")` |Try a list of parsers and return the result of the first successful one| | 
|---|
| 24 | //! | [`dispatch`] | \- | \- | \- | \- | `match` for parsers | | 
|---|
| 25 | //! | [`permutation`] | `permutation(("ab", "cd", "12"))` | `"cd12abc"` | `"c"` | `Ok(("ab", "cd", "12"))` |Succeeds when all its child parser have succeeded, whatever the order| | 
|---|
| 26 | //! | 
|---|
| 27 | //! ## Sequence combinators | 
|---|
| 28 | //! | 
|---|
| 29 | //! | combinator | usage | input | new input | output | comment | | 
|---|
| 30 | //! |---|---|---|---|---|---| | 
|---|
| 31 | //! | [`(...)` (tuples)][crate::Parser] | `("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| | 
|---|
| 32 | //! | [`seq!`] | `seq!(_: char('('), take(2), _: char(')'))` | `"(ab)cd"` | `"cd"` | `Ok("ab")` || | 
|---|
| 33 | //! | [`delimited`] | `delimited(char('('), take(2), char(')'))` | `"(ab)cd"` | `"cd"` | `Ok("ab")` || | 
|---|
| 34 | //! | [`preceded`] | `preceded("ab", "XY")` | `"abXYZ"` | `"Z"` | `Ok("XY")` || | 
|---|
| 35 | //! | [`terminated`] | `terminated("ab", "XY")` | `"abXYZ"` | `"Z"` | `Ok("ab")` || | 
|---|
| 36 | //! | [`separated_pair`] | `separated_pair("hello", char(','), "world")` | `"hello,world!"` | `"!"` | `Ok(("hello", "world"))` || | 
|---|
| 37 | //! | 
|---|
| 38 | //! ## Applying a parser multiple times | 
|---|
| 39 | //! | 
|---|
| 40 | //! | combinator | usage | input | new input | output | comment | | 
|---|
| 41 | //! |---|---|---|---|---|---| | 
|---|
| 42 | //! | [`repeat`] | `repeat(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| | 
|---|
| 43 | //! | [`repeat_till`] | `repeat_till(0.., tag( "ab" ), tag( "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| | 
|---|
| 44 | //! | [`separated`] | `separated(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| | 
|---|
| 45 | //! | [`fold_repeat`] | `fold_repeat(1..=2, be_u8, \|\| 0, \|acc, item\| acc + item)` | `[1, 2, 3]` | `[3]` | `Ok(3)` |Applies the parser between m and n times (n included) and folds the list of return value| | 
|---|
| 46 | //! | 
|---|
| 47 | //! ## Partial related | 
|---|
| 48 | //! | 
|---|
| 49 | //! - [`eof`]: Returns its input if it is at the end of input data | 
|---|
| 50 | //! - [`Parser::complete_err`]: Replaces an `Incomplete` returned by the child parser with an `Backtrack` | 
|---|
| 51 | //! | 
|---|
| 52 | //! ## Modifiers | 
|---|
| 53 | //! | 
|---|
| 54 | //! - [`cond`]: Conditional combinator. Wraps another parser and calls it if the condition is met | 
|---|
| 55 | //! - [`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 | 
|---|
| 56 | //! - [`Parser::value`]: method to replace the result of a parser | 
|---|
| 57 | //! - [`Parser::default_value`]: method to replace the result of a parser | 
|---|
| 58 | //! - [`Parser::void`]: method to discard the result of a parser | 
|---|
| 59 | //! - [`Parser::map`]: method to map a function on the result of a parser | 
|---|
| 60 | //! - [`Parser::and_then`]: Applies a second parser over the output of the first one | 
|---|
| 61 | //! - [`Parser::verify_map`]: Maps a function returning an `Option` on the output of a parser | 
|---|
| 62 | //! - [`Parser::try_map`]: Maps a function returning a `Result` on the output of a parser | 
|---|
| 63 | //! - [`Parser::parse_to`]: Apply [`std::str::FromStr`] to the output of the parser | 
|---|
| 64 | //! - [`not`]: Returns a result only if the embedded parser returns `Backtrack` or `Incomplete`. Does not consume the input | 
|---|
| 65 | //! - [`opt`]: Make the underlying parser optional | 
|---|
| 66 | //! - [`peek`]: Returns a result without consuming the input | 
|---|
| 67 | //! - [`Parser::recognize`]: If the child parser was successful, return the consumed input as the produced value | 
|---|
| 68 | //! - [`Parser::with_recognized`]: If the child parser was successful, return a tuple of the consumed input and the produced output. | 
|---|
| 69 | //! - [`Parser::span`]: If the child parser was successful, return the location of the consumed input as the produced value | 
|---|
| 70 | //! - [`Parser::with_span`]: If the child parser was successful, return a tuple of the location of the consumed input and the produced output. | 
|---|
| 71 | //! - [`Parser::verify`]: Returns the result of the child parser if it satisfies a verification function | 
|---|
| 72 | //! | 
|---|
| 73 | //! ## Error management and debugging | 
|---|
| 74 | //! | 
|---|
| 75 | //! - [`cut_err`]: Commit the parse result, disallowing alternative parsers from being attempted | 
|---|
| 76 | //! - [`backtrack_err`]: Attempts a parse, allowing alternative parsers to be attempted despite | 
|---|
| 77 | //!   use of `cut_err` | 
|---|
| 78 | //! - [`Parser::context`]: Add context to the error if the parser fails | 
|---|
| 79 | //! - [`trace`]: Print the parse state with the `debug` feature flag | 
|---|
| 80 | //! - [`todo()`]: Placeholder parser | 
|---|
| 81 | //! | 
|---|
| 82 | //! ## Remaining combinators | 
|---|
| 83 | //! | 
|---|
| 84 | //! - [`success`]: Returns a value without consuming any input, always succeeds | 
|---|
| 85 | //! - [`fail`]: Inversion of `success`. Always fails. | 
|---|
| 86 | //! - [`Parser::by_ref`]: Allow moving `&mut impl Parser` into other parsers | 
|---|
| 87 | //! | 
|---|
| 88 | //! ## Text parsing | 
|---|
| 89 | //! | 
|---|
| 90 | //! - [`any`][crate::token::any]: Matches one token | 
|---|
| 91 | //! - [`tab`][crate::ascii::tab]: Matches a tab character `\t` | 
|---|
| 92 | //! - [`crlf`][crate::ascii::crlf]: Recognizes the string `\r\n` | 
|---|
| 93 | //! - [`line_ending`][crate::ascii::line_ending]: Recognizes an end of line (both `\n` and `\r\n`) | 
|---|
| 94 | //! - [`newline`][crate::ascii::newline]: Matches a newline character `\n` | 
|---|
| 95 | //! - [`till_line_ending`][crate::ascii::till_line_ending]: Recognizes a string of any char except `\r` or `\n` | 
|---|
| 96 | //! - [`rest`]: Return the remaining input | 
|---|
| 97 | //! | 
|---|
| 98 | //! - [`alpha0`][crate::ascii::alpha0]: Recognizes zero or more lowercase and uppercase alphabetic characters: `[a-zA-Z]`. [`alpha1`][crate::ascii::alpha1] does the same but returns at least one character | 
|---|
| 99 | //! - [`alphanumeric0`][crate::ascii::alphanumeric0]: Recognizes zero or more numerical and alphabetic characters: `[0-9a-zA-Z]`. [`alphanumeric1`][crate::ascii::alphanumeric1] does the same but returns at least one character | 
|---|
| 100 | //! - [`space0`][crate::ascii::space0]: Recognizes zero or more spaces and tabs. [`space1`][crate::ascii::space1] does the same but returns at least one character | 
|---|
| 101 | //! - [`multispace0`][crate::ascii::multispace0]: Recognizes zero or more spaces, tabs, carriage returns and line feeds. [`multispace1`][crate::ascii::multispace1] does the same but returns at least one character | 
|---|
| 102 | //! - [`digit0`][crate::ascii::digit0]: Recognizes zero or more numerical characters: `[0-9]`. [`digit1`][crate::ascii::digit1] does the same but returns at least one character | 
|---|
| 103 | //! - [`hex_digit0`][crate::ascii::hex_digit0]: Recognizes zero or more hexadecimal numerical characters: `[0-9A-Fa-f]`. [`hex_digit1`][crate::ascii::hex_digit1] does the same but returns at least one character | 
|---|
| 104 | //! - [`oct_digit0`][crate::ascii::oct_digit0]: Recognizes zero or more octal characters: `[0-7]`. [`oct_digit1`][crate::ascii::oct_digit1] does the same but returns at least one character | 
|---|
| 105 | //! | 
|---|
| 106 | //! - [`float`][crate::ascii::float]: Parse a floating point number in a byte string | 
|---|
| 107 | //! - [`dec_int`][crate::ascii::dec_int]: Decode a variable-width, decimal signed integer | 
|---|
| 108 | //! - [`dec_uint`][crate::ascii::dec_uint]: Decode a variable-width, decimal unsigned integer | 
|---|
| 109 | //! - [`hex_uint`][crate::ascii::hex_uint]: Decode a variable-width, hexadecimal integer | 
|---|
| 110 | //! | 
|---|
| 111 | //! - [`escaped`][crate::ascii::escaped]: Matches a byte string with escaped characters | 
|---|
| 112 | //! - [`escaped_transform`][crate::ascii::escaped_transform]: Matches a byte string with escaped characters, and returns a new string with the escaped characters replaced | 
|---|
| 113 | //! | 
|---|
| 114 | //! ### Character test functions | 
|---|
| 115 | //! | 
|---|
| 116 | //! Use these functions with a combinator like `take_while`: | 
|---|
| 117 | //! | 
|---|
| 118 | //! - [`AsChar::is_alpha`][crate::stream::AsChar::is_alpha]: Tests if byte is ASCII alphabetic: `[A-Za-z]` | 
|---|
| 119 | //! - [`AsChar::is_alphanum`][crate::stream::AsChar::is_alphanum]: Tests if byte is ASCII alphanumeric: `[A-Za-z0-9]` | 
|---|
| 120 | //! - [`AsChar::is_dec_digit`][crate::stream::AsChar::is_dec_digit]: Tests if byte is ASCII digit: `[0-9]` | 
|---|
| 121 | //! - [`AsChar::is_hex_digit`][crate::stream::AsChar::is_hex_digit]: Tests if byte is ASCII hex digit: `[0-9A-Fa-f]` | 
|---|
| 122 | //! - [`AsChar::is_oct_digit`][crate::stream::AsChar::is_oct_digit]: Tests if byte is ASCII octal digit: `[0-7]` | 
|---|
| 123 | //! - [`AsChar::is_space`][crate::stream::AsChar::is_space]: Tests if byte is ASCII space or tab: `[ \t]` | 
|---|
| 124 | //! - [`AsChar::is_newline`][crate::stream::AsChar::is_newline]: Tests if byte is ASCII newline: `[\n]` | 
|---|
| 125 | //! | 
|---|
| 126 | //! ## Binary format parsing | 
|---|
| 127 | //! | 
|---|
| 128 | //! - [`length_count`][crate::binary::length_count] Gets a number from the first parser, then applies the second parser that many times | 
|---|
| 129 | //! - [`length_data`][crate::binary::length_data]: Gets a number from the first parser, then takes a subslice of the input of that size, and returns that subslice | 
|---|
| 130 | //! - [`length_value`][crate::binary::length_value]: 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 | 
|---|
| 131 | //! | 
|---|
| 132 | //! ### Integers | 
|---|
| 133 | //! | 
|---|
| 134 | //! Parsing integers from binary formats can be done in two ways: With parser functions, or combinators with configurable endianness. | 
|---|
| 135 | //! | 
|---|
| 136 | //! - **configurable endianness:** [`i16`][crate::binary::i16], [`i32`][crate::binary::i32], | 
|---|
| 137 | //!   [`i64`][crate::binary::i64], [`u16`][crate::binary::u16], [`u32`][crate::binary::u32], | 
|---|
| 138 | //!   [`u64`][crate::binary::u64] are combinators that take as argument a | 
|---|
| 139 | //!   [`winnow::binary::Endianness`][crate::binary::Endianness], like this: `i16(endianness)`. If the | 
|---|
| 140 | //!   parameter is `winnow::binary::Endianness::Big`, parse a big endian `i16` integer, otherwise a | 
|---|
| 141 | //!   little endian `i16` integer. | 
|---|
| 142 | //! - **fixed endianness**: The functions are prefixed by `be_` for big endian numbers, and by `le_` for little endian numbers, and the suffix is the type they parse to. As an example, `be_u32` parses a big endian unsigned integer stored in 32 bits. | 
|---|
| 143 | //!   - [`be_f32`][crate::binary::be_f32], [`be_f64`][crate::binary::be_f64]: Big endian floating point numbers | 
|---|
| 144 | //!   - [`le_f32`][crate::binary::le_f32], [`le_f64`][crate::binary::le_f64]: Little endian floating point numbers | 
|---|
| 145 | //!   - [`be_i8`][crate::binary::be_i8], [`be_i16`][crate::binary::be_i16], [`be_i24`][crate::binary::be_i24], [`be_i32`][crate::binary::be_i32], [`be_i64`][crate::binary::be_i64], [`be_i128`][crate::binary::be_i128]: Big endian signed integers | 
|---|
| 146 | //!   - [`be_u8`][crate::binary::be_u8], [`be_u16`][crate::binary::be_u16], [`be_u24`][crate::binary::be_u24], [`be_u32`][crate::binary::be_u32], [`be_u64`][crate::binary::be_u64], [`be_u128`][crate::binary::be_u128]: Big endian unsigned integers | 
|---|
| 147 | //!   - [`le_i8`][crate::binary::le_i8], [`le_i16`][crate::binary::le_i16], [`le_i24`][crate::binary::le_i24], [`le_i32`][crate::binary::le_i32], [`le_i64`][crate::binary::le_i64], [`le_i128`][crate::binary::le_i128]: Little endian signed integers | 
|---|
| 148 | //!   - [`le_u8`][crate::binary::le_u8], [`le_u16`][crate::binary::le_u16], [`le_u24`][crate::binary::le_u24], [`le_u32`][crate::binary::le_u32], [`le_u64`][crate::binary::le_u64], [`le_u128`][crate::binary::le_u128]: Little endian unsigned integers | 
|---|
| 149 | //! | 
|---|
| 150 | //! ### Bit stream parsing | 
|---|
| 151 | //! | 
|---|
| 152 | //! - [`bits`][crate::binary::bits::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 | 
|---|
| 153 | //! - [`bytes`][crate::binary::bits::bytes]: Transforms its bits stream input back into a byte slice for the underlying parser | 
|---|
| 154 | //! - [`take`][crate::binary::bits::take]: Take a set number of bits | 
|---|
| 155 | //! - [`tag`][crate::binary::bits::tag]: Check if a set number of bits matches a pattern | 
|---|
| 156 | //! - [`bool`][crate::binary::bits::bool]: Match any one bit | 
|---|
| 157 |  | 
|---|
| 158 | mod branch; | 
|---|
| 159 | mod core; | 
|---|
| 160 | mod debug; | 
|---|
| 161 | mod multi; | 
|---|
| 162 | mod parser; | 
|---|
| 163 | mod sequence; | 
|---|
| 164 |  | 
|---|
| 165 | #[ cfg(test)] | 
|---|
| 166 | mod tests; | 
|---|
| 167 |  | 
|---|
| 168 | pub use self::branch::*; | 
|---|
| 169 | pub use self::core::*; | 
|---|
| 170 | pub use self::debug::*; | 
|---|
| 171 | pub use self::multi::*; | 
|---|
| 172 | pub use self::parser::*; | 
|---|
| 173 | pub use self::sequence::*; | 
|---|
| 174 |  | 
|---|
| 175 | #[ allow(unused_imports)] | 
|---|
| 176 | use crate::Parser; | 
|---|
| 177 |  | 
|---|