summaryrefslogtreecommitdiffstats
path: root/vendor/winnow/src/_tutorial/chapter_5.rs
blob: b703bcf5c1e51996859aa796b778f33add4a5e76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! # Chapter 5: Repetition
//!
//! In [`chapter_3`], we covered how to sequence different parsers into a tuple but sometimes you need to run a
//! single parser many times into a [`Vec`].
//!
//! Let's take our `parse_digits` and collect a list of them with [`many0`]:
//! ```rust
//! # use winnow::IResult;
//! # use winnow::Parser;
//! # use winnow::bytes::take_while1;
//! # use winnow::branch::dispatch;
//! # use winnow::bytes::take;
//! # use winnow::combinator::fail;
//! use winnow::combinator::opt;
//! use winnow::multi::many0;
//! use winnow::sequence::terminated;
//!
//! fn parse_list(input: &str) -> IResult<&str, Vec<usize>> {
//!     many0(terminated(parse_digits, opt(','))).parse_next(input)
//! }
//!
//! // ...
//! # fn parse_digits(input: &str) -> IResult<&str, usize> {
//! #     dispatch!(take(2usize);
//! #          "0b" => parse_bin_digits.map_res(|s| usize::from_str_radix(s, 2)),
//! #          "0o" => parse_oct_digits.map_res(|s| usize::from_str_radix(s, 8)),
//! #          "0d" => parse_dec_digits.map_res(|s| usize::from_str_radix(s, 10)),
//! #          "0x" => parse_hex_digits.map_res(|s| usize::from_str_radix(s, 16)),
//! #          _ => fail,
//! #      ).parse_next(input)
//! # }
//! #
//! # fn parse_bin_digits(input: &str) -> IResult<&str, &str> {
//! #     take_while1((
//! #         ('0'..='7'),
//! #     )).parse_next(input)
//! # }
//! #
//! # fn parse_oct_digits(input: &str) -> IResult<&str, &str> {
//! #     take_while1((
//! #         ('0'..='7'),
//! #     )).parse_next(input)
//! # }
//! #
//! # fn parse_dec_digits(input: &str) -> IResult<&str, &str> {
//! #     take_while1((
//! #         ('0'..='9'),
//! #     )).parse_next(input)
//! # }
//! #
//! # fn parse_hex_digits(input: &str) -> IResult<&str, &str> {
//! #     take_while1((
//! #         ('0'..='9'),
//! #         ('A'..='F'),
//! #         ('a'..='f'),
//! #     )).parse_next(input)
//! # }
//!
//! fn main() {
//!     let input = "0x1a2b,0x3c4d,0x5e6f Hello";
//!
//!     let (remainder, digits) = parse_list.parse_next(input).unwrap();
//!
//!     assert_eq!(remainder, " Hello");
//!     assert_eq!(digits, vec![0x1a2b, 0x3c4d, 0x5e6f]);
//!
//!     assert!(parse_digits("ghiWorld").is_err());
//! }
//! ```
//!
//! You'll notice that the above allows trailing `,` when we intended to not support that.  We can
//! easily fix this by using [`separated0`]:
//! ```rust
//! # use winnow::IResult;
//! # use winnow::Parser;
//! # use winnow::bytes::take_while1;
//! # use winnow::branch::dispatch;
//! # use winnow::bytes::take;
//! # use winnow::combinator::fail;
//! use winnow::multi::separated0;
//!
//! fn parse_list(input: &str) -> IResult<&str, Vec<usize>> {
//!     separated0(parse_digits, ",").parse_next(input)
//! }
//!
//! // ...
//! # fn parse_digits(input: &str) -> IResult<&str, usize> {
//! #     dispatch!(take(2usize);
//! #          "0b" => parse_bin_digits.map_res(|s| usize::from_str_radix(s, 2)),
//! #          "0o" => parse_oct_digits.map_res(|s| usize::from_str_radix(s, 8)),
//! #          "0d" => parse_dec_digits.map_res(|s| usize::from_str_radix(s, 10)),
//! #          "0x" => parse_hex_digits.map_res(|s| usize::from_str_radix(s, 16)),
//! #          _ => fail,
//! #      ).parse_next(input)
//! # }
//! #
//! # fn parse_bin_digits(input: &str) -> IResult<&str, &str> {
//! #     take_while1((
//! #         ('0'..='7'),
//! #     )).parse_next(input)
//! # }
//! #
//! # fn parse_oct_digits(input: &str) -> IResult<&str, &str> {
//! #     take_while1((
//! #         ('0'..='7'),
//! #     )).parse_next(input)
//! # }
//! #
//! # fn parse_dec_digits(input: &str) -> IResult<&str, &str> {
//! #     take_while1((
//! #         ('0'..='9'),
//! #     )).parse_next(input)
//! # }
//! #
//! # fn parse_hex_digits(input: &str) -> IResult<&str, &str> {
//! #     take_while1((
//! #         ('0'..='9'),
//! #         ('A'..='F'),
//! #         ('a'..='f'),
//! #     )).parse_next(input)
//! # }
//!
//! fn main() {
//!     let input = "0x1a2b,0x3c4d,0x5e6f Hello";
//!
//!     let (remainder, digits) = parse_list.parse_next(input).unwrap();
//!
//!     assert_eq!(remainder, " Hello");
//!     assert_eq!(digits, vec![0x1a2b, 0x3c4d, 0x5e6f]);
//!
//!     assert!(parse_digits("ghiWorld").is_err());
//! }
//! ```
//!
//! If you look closely at [`many0`], it isn't collecting directly into a [`Vec`] but
//! [`Accumulate`] to gather the results.  This let's us make more complex parsers than we did in
//! [`chapter_2`] by accumulating the results into a `()` and [`recognize`][Parser::recognize]-ing the captured input:
//! ```rust
//! # use winnow::IResult;
//! # use winnow::Parser;
//! # use winnow::bytes::take_while1;
//! # use winnow::branch::dispatch;
//! # use winnow::bytes::take;
//! # use winnow::combinator::fail;
//! # use winnow::multi::separated0;
//! #
//! fn recognize_list(input: &str) -> IResult<&str, &str> {
//!     parse_list.recognize().parse_next(input)
//! }
//!
//! fn parse_list(input: &str) -> IResult<&str, ()> {
//!     separated0(parse_digits, ",").parse_next(input)
//! }
//!
//! // ...
//! # fn parse_digits(input: &str) -> IResult<&str, usize> {
//! #     dispatch!(take(2usize);
//! #          "0b" => parse_bin_digits.map_res(|s| usize::from_str_radix(s, 2)),
//! #          "0o" => parse_oct_digits.map_res(|s| usize::from_str_radix(s, 8)),
//! #          "0d" => parse_dec_digits.map_res(|s| usize::from_str_radix(s, 10)),
//! #          "0x" => parse_hex_digits.map_res(|s| usize::from_str_radix(s, 16)),
//! #          _ => fail,
//! #      ).parse_next(input)
//! # }
//! #
//! # fn parse_bin_digits(input: &str) -> IResult<&str, &str> {
//! #     take_while1((
//! #         ('0'..='7'),
//! #     )).parse_next(input)
//! # }
//! #
//! # fn parse_oct_digits(input: &str) -> IResult<&str, &str> {
//! #     take_while1((
//! #         ('0'..='7'),
//! #     )).parse_next(input)
//! # }
//! #
//! # fn parse_dec_digits(input: &str) -> IResult<&str, &str> {
//! #     take_while1((
//! #         ('0'..='9'),
//! #     )).parse_next(input)
//! # }
//! #
//! # fn parse_hex_digits(input: &str) -> IResult<&str, &str> {
//! #     take_while1((
//! #         ('0'..='9'),
//! #         ('A'..='F'),
//! #         ('a'..='f'),
//! #     )).parse_next(input)
//! # }
//!
//! fn main() {
//!     let input = "0x1a2b,0x3c4d,0x5e6f Hello";
//!
//!     let (remainder, digits) = recognize_list.parse_next(input).unwrap();
//!
//!     assert_eq!(remainder, " Hello");
//!     assert_eq!(digits, "0x1a2b,0x3c4d,0x5e6f");
//!
//!     assert!(parse_digits("ghiWorld").is_err());
//! }
//! ```

#![allow(unused_imports)]
use super::chapter_2;
use super::chapter_3;
use crate::multi::many0;
use crate::multi::separated0;
use crate::stream::Accumulate;
use crate::Parser;
use std::vec::Vec;

pub use super::chapter_4 as previous;
pub use super::chapter_6 as next;