|
1 | 1 | use crate::input::{InputError, MapWithInputExt}; |
2 | | -use crate::parser::combinator::{Map, MapResult, Optional, Or, Repeat, WithPrefix, WithSuffix}; |
| 2 | +use crate::parser::combinator::{ |
| 3 | + Map, MapResult, Optional, Or, RepeatN, RepeatVec, WithPrefix, WithSuffix, |
| 4 | +}; |
3 | 5 | use crate::parser::error::WithErrorMsg; |
4 | 6 | use crate::parser::simple::Eol; |
5 | 7 | use crate::parser::then::Then2; |
@@ -152,24 +154,72 @@ pub trait Parser: Sized { |
152 | 154 | Optional { parser: self } |
153 | 155 | } |
154 | 156 |
|
155 | | - /// Repeat this parser `N` times, returning an array. |
| 157 | + /// Repeat this parser `N` times, returning an [`array`]. |
| 158 | + /// |
| 159 | + /// See also [`repeat`](Self::repeat) which returns a [`Vec`] instead, for unknown or varying |
| 160 | + /// number of repeats. |
156 | 161 | /// |
157 | 162 | /// # Examples |
158 | 163 | /// ``` |
159 | 164 | /// # use utils::parser::{self, Parser}; |
160 | 165 | /// assert_eq!( |
161 | 166 | /// parser::u32() |
162 | 167 | /// .with_suffix(",".optional()) |
163 | | - /// .repeat() // N = 3 is inferred |
| 168 | + /// .repeat_n() // N = 3 is inferred |
164 | 169 | /// .parse(b"12,34,56"), |
165 | 170 | /// Ok(([12, 34, 56], &b""[..])) |
166 | 171 | /// ); |
167 | 172 | /// ``` |
168 | | - fn repeat<const N: usize>(self) -> Repeat<N, Self> |
| 173 | + fn repeat_n<const N: usize>(self) -> RepeatN<N, Self> |
169 | 174 | where |
170 | 175 | for<'i> Self::Output<'i>: Copy + Default, |
171 | 176 | { |
172 | | - Repeat { parser: self } |
| 177 | + RepeatN { parser: self } |
| 178 | + } |
| 179 | + |
| 180 | + /// Repeat this parser while it matches, returning a [`Vec`]. |
| 181 | + /// |
| 182 | + /// If the number of items is constant and known in advance, prefer [`repeat_n`](Self::repeat_n) |
| 183 | + /// as it avoids allocating. |
| 184 | + /// |
| 185 | + /// See also [`repeat_min`](Self::repeat_min), which ensures at least N items are parsed. |
| 186 | + /// |
| 187 | + /// # Examples |
| 188 | + /// ``` |
| 189 | + /// # use utils::parser::{self, Parser}; |
| 190 | + /// assert_eq!( |
| 191 | + /// parser::u32() |
| 192 | + /// .with_suffix(",".optional()) |
| 193 | + /// .repeat() |
| 194 | + /// .parse(b"12,34,56,78"), |
| 195 | + /// Ok((vec![12, 34, 56, 78], &b""[..])) |
| 196 | + /// ); |
| 197 | + /// ``` |
| 198 | + fn repeat(self) -> RepeatVec<Self> { |
| 199 | + RepeatVec { |
| 200 | + parser: self, |
| 201 | + min_elements: 0, |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + /// Repeat this parser at least N times, returning a [`Vec`]. |
| 206 | + /// |
| 207 | + /// See also [`repeat`](Self::repeat). |
| 208 | + /// |
| 209 | + /// # Examples |
| 210 | + /// ``` |
| 211 | + /// # use utils::parser::{self, Parser}; |
| 212 | + /// let parser = parser::u32() |
| 213 | + /// .with_suffix(",".optional()) |
| 214 | + /// .repeat_min(3); |
| 215 | + /// assert_eq!(parser.parse(b"12,34,56,78"), Ok((vec![12, 34, 56, 78], &b""[..]))); |
| 216 | + /// assert!(parser.parse(b"12,34").is_err()); |
| 217 | + /// ``` |
| 218 | + fn repeat_min(self, min_elements: usize) -> RepeatVec<Self> { |
| 219 | + RepeatVec { |
| 220 | + parser: self, |
| 221 | + min_elements, |
| 222 | + } |
173 | 223 | } |
174 | 224 |
|
175 | 225 | /// Parse a prefix (normally a string literal) before this parser. |
|
0 commit comments