1 //! Parsers for applying parsers multiple times 2 3 /// `separated_list!(I -> IResult<I,T>, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` 4 /// separated_list(sep, X) returns a Vec<X> 5 /// 6 /// ```rust 7 /// # #[macro_use] extern crate nom; 8 /// # use nom::{Err, error::ErrorKind, Needed, IResult}; 9 /// use nom::multi::separated_list; 10 /// use nom::bytes::complete::tag; 11 /// 12 /// # fn main() { 13 /// named!(parser<&str, Vec<&str>>, separated_list!(tag("|"), tag("abc"))); 14 /// 15 /// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); 16 /// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"]))); 17 /// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"]))); 18 /// assert_eq!(parser(""), Ok(("", vec![]))); 19 /// assert_eq!(parser("def|abc"), Ok(("def|abc", vec![]))); 20 /// # } 21 /// ``` 22 #[cfg(feature = "alloc")] 23 #[macro_export(local_inner_macros)] 24 macro_rules! separated_list( 25 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 26 separated_list!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) 27 ); 28 29 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 30 separated_list!($i, |i| $submac!(i, $($args)*), $g); 31 ); 32 33 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 34 separated_list!($i, $f, |i| $submac!(i, $($args)*)); 35 ); 36 37 ($i:expr, $f:expr, $g:expr) => ( 38 $crate::multi::separated_listc($i, $f, $g) 39 ); 40 ); 41 42 /// `separated_nonempty_list!(I -> IResult<I,T>, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` 43 /// separated_nonempty_list(sep, X) returns a Vec<X> 44 /// 45 /// it will return an error if there is no element in the list 46 /// ```rust 47 /// # #[macro_use] extern crate nom; 48 /// # use nom::{Err, error::ErrorKind, Needed, IResult}; 49 /// use nom::multi::separated_nonempty_list; 50 /// use nom::bytes::complete::tag; 51 /// 52 /// # fn main() { 53 /// named!(parser<&str, Vec<&str>>, separated_nonempty_list!(tag("|"), tag("abc"))); 54 /// 55 /// assert_eq!(parser("abc|abc|abc"), Ok(("", vec!["abc", "abc", "abc"]))); 56 /// assert_eq!(parser("abc123abc"), Ok(("123abc", vec!["abc"]))); 57 /// assert_eq!(parser("abc|def"), Ok(("|def", vec!["abc"]))); 58 /// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag)))); 59 /// assert_eq!(parser("def|abc"), Err(Err::Error(("def|abc", ErrorKind::Tag)))); 60 /// # } 61 /// ``` 62 #[cfg(feature = "alloc")] 63 #[macro_export(local_inner_macros)] 64 macro_rules! separated_nonempty_list( 65 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 66 separated_nonempty_list!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) 67 ); 68 69 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 70 separated_nonempty_list!($i, |i| $submac!(i, $($args)*), $g); 71 ); 72 73 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 74 separated_nonempty_list!($i, $f, |i| $submac!(i, $($args)*)); 75 ); 76 77 ($i:expr, $f:expr, $g:expr) => ( 78 $crate::multi::separated_nonempty_listc($i, $f, $g) 79 ); 80 ); 81 82 /// `many0!(I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` 83 /// Applies the parser 0 or more times and returns the list of results in a Vec. 84 /// 85 /// The embedded parser may return Incomplete. 86 /// 87 /// `many0` will only return `Error` if the embedded parser does not consume any input 88 /// (to avoid infinite loops). 89 /// 90 /// ``` 91 /// # #[macro_use] extern crate nom; 92 /// # fn main() { 93 /// named!(multi<&[u8], Vec<&[u8]> >, many0!( tag!( "abcd" ) ) ); 94 /// 95 /// let a = b"abcdabcdefgh"; 96 /// let b = b"azerty"; 97 /// 98 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 99 /// assert_eq!(multi(&a[..]),Ok((&b"efgh"[..], res))); 100 /// assert_eq!(multi(&b[..]),Ok((&b"azerty"[..], Vec::new()))); 101 /// # } 102 /// ``` 103 /// 104 #[cfg(feature = "alloc")] 105 #[macro_export(local_inner_macros)] 106 macro_rules! many0( 107 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 108 many0!($i, |i| $submac!(i, $($args)*)) 109 ); 110 ($i:expr, $f:expr) => ( 111 $crate::multi::many0c($i, $f) 112 ); 113 ); 114 115 /// `many1!(I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` 116 /// Applies the parser 1 or more times and returns the list of results in a Vec 117 /// 118 /// the embedded parser may return Incomplete 119 /// 120 /// ``` 121 /// # #[macro_use] extern crate nom; 122 /// # use nom::Err; 123 /// # use nom::error::ErrorKind; 124 /// # fn main() { 125 /// named!(multi<&[u8], Vec<&[u8]> >, many1!( tag!( "abcd" ) ) ); 126 /// 127 /// let a = b"abcdabcdefgh"; 128 /// let b = b"azerty"; 129 /// 130 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 131 /// assert_eq!(multi(&a[..]), Ok((&b"efgh"[..], res))); 132 /// assert_eq!(multi(&b[..]), Err(Err::Error(error_position!(&b[..], ErrorKind::Tag)))); 133 /// # } 134 /// ``` 135 #[cfg(feature = "alloc")] 136 #[macro_export(local_inner_macros)] 137 macro_rules! many1( 138 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 139 many1!($i, |i| $submac!(i, $($args)*)) 140 ); 141 ($i:expr, $f:expr) => ( 142 $crate::multi::many1c($i, $f) 143 ); 144 ); 145 146 /// `many_till!(I -> IResult<I,O>, I -> IResult<I,P>) => I -> IResult<I, (Vec<O>, P)>` 147 /// Applies the first parser until the second applies. Returns a tuple containing the list 148 /// of results from the first in a Vec and the result of the second. 149 /// 150 /// The first embedded parser may return Incomplete 151 /// 152 /// ``` 153 /// # #[macro_use] extern crate nom; 154 /// # use nom::Err; 155 /// # use nom::error::ErrorKind; 156 /// # fn main() { 157 /// named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( tag!( "abcd" ), tag!( "efgh" ) ) ); 158 /// 159 /// let a = b"abcdabcdefghabcd"; 160 /// let b = b"efghabcd"; 161 /// let c = b"azerty"; 162 /// 163 /// let res_a = (vec![&b"abcd"[..], &b"abcd"[..]], &b"efgh"[..]); 164 /// let res_b: (Vec<&[u8]>, &[u8]) = (Vec::new(), &b"efgh"[..]); 165 /// assert_eq!(multi(&a[..]),Ok((&b"abcd"[..], res_a))); 166 /// assert_eq!(multi(&b[..]),Ok((&b"abcd"[..], res_b))); 167 /// assert_eq!(multi(&c[..]), Err(Err::Error(error_node_position!(&c[..], ErrorKind::ManyTill, 168 /// error_position!(&c[..], ErrorKind::Tag))))); 169 /// # } 170 /// ``` 171 #[cfg(feature = "alloc")] 172 #[macro_export(local_inner_macros)] 173 macro_rules! many_till( 174 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 175 many_till!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) 176 ); 177 178 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 179 many_till!($i, |i| $submac!(i, $($args)*), $g); 180 ); 181 182 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 183 many_till!($i, $f, |i| $submac!(i, $($args)*)); 184 ); 185 186 ($i:expr, $f:expr, $g:expr) => ( 187 $crate::multi::many_tillc($i, $f, $g) 188 ); 189 ); 190 191 /// `many_m_n!(usize, usize, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` 192 /// Applies the parser between m and n times (n included) and returns the list of 193 /// results in a Vec 194 /// 195 /// the embedded parser may return Incomplete 196 /// 197 /// ``` 198 /// # #[macro_use] extern crate nom; 199 /// # use nom::Err; 200 /// # use nom::error::ErrorKind; 201 /// # fn main() { 202 /// named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, tag!( "abcd" ) ) ); 203 /// 204 /// let a = b"abcdefgh"; 205 /// let b = b"abcdabcdefgh"; 206 /// let c = b"abcdabcdabcdabcdabcdefgh"; 207 /// 208 /// assert_eq!(multi(&a[..]), Err(Err::Error(error_position!(&b"efgh"[..], ErrorKind::Tag)))); 209 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 210 /// assert_eq!(multi(&b[..]),Ok((&b"efgh"[..], res))); 211 /// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; 212 /// assert_eq!(multi(&c[..]),Ok((&b"abcdefgh"[..], res2))); 213 /// # } 214 /// ``` 215 #[cfg(feature = "alloc")] 216 #[macro_export(local_inner_macros)] 217 macro_rules! many_m_n( 218 ($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* )) => ( 219 many_m_n!($i, $m, $n, |i| $submac!(i, $($args)*)) 220 ); 221 ($i:expr, $m:expr, $n: expr, $f:expr) => ( 222 $crate::multi::many_m_nc($i, $m, $n, $f) 223 ); 224 ); 225 226 /// `many0_count!(I -> IResult<I,O>) => I -> IResult<I, usize>` 227 /// Applies the parser 0 or more times and returns the number of times the parser was applied. 228 /// 229 /// `many0_count` will only return `Error` if the embedded parser does not consume any input 230 /// (to avoid infinite loops). 231 /// 232 /// ``` 233 /// #[macro_use] extern crate nom; 234 /// use nom::character::streaming::digit1; 235 /// 236 /// named!(number<&[u8], usize>, many0_count!(pair!(digit1, tag!(",")))); 237 /// 238 /// fn main() { 239 /// assert_eq!(number(&b"123,45,abc"[..]), Ok((&b"abc"[..], 2))); 240 /// } 241 /// ``` 242 /// 243 #[macro_export] 244 macro_rules! many0_count { 245 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 246 $crate::multi::many0_countc($i, |i| $submac!(i, $($args)*)) 247 ); 248 249 ($i:expr, $f:expr) => ( 250 $crate::multi::many0_countc($i, $f) 251 ); 252 } 253 254 /// `many1_count!(I -> IResult<I,O>) => I -> IResult<I, usize>` 255 /// Applies the parser 1 or more times and returns the number of times the parser was applied. 256 /// 257 /// ``` 258 /// #[macro_use] extern crate nom; 259 /// use nom::character::streaming::digit1; 260 /// 261 /// named!(number<&[u8], usize>, many1_count!(pair!(digit1, tag!(",")))); 262 /// 263 /// fn main() { 264 /// assert_eq!(number(&b"123,45,abc"[..]), Ok((&b"abc"[..], 2))); 265 /// } 266 /// ``` 267 /// 268 #[macro_export] 269 macro_rules! many1_count { 270 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 271 $crate::multi::many1_countc($i, |i| $submac!(i, $($args)*)) 272 ); 273 274 ($i:expr, $f:expr) => ( 275 $crate::multi::many1_countc($i, $f) 276 ); 277 } 278 279 /// `count!(I -> IResult<I,O>, nb) => I -> IResult<I, Vec<O>>` 280 /// Applies the child parser a specified number of times 281 /// 282 /// ``` 283 /// # #[macro_use] extern crate nom; 284 /// # use nom::Err; 285 /// # use nom::error::ErrorKind; 286 /// # fn main() { 287 /// named!(counter< Vec<&[u8]> >, count!( tag!( "abcd" ), 2 ) ); 288 /// 289 /// let a = b"abcdabcdabcdef"; 290 /// let b = b"abcdefgh"; 291 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 292 /// 293 /// assert_eq!(counter(&a[..]),Ok((&b"abcdef"[..], res))); 294 /// assert_eq!(counter(&b[..]), Err(Err::Error(error_position!(&b"efgh"[..], ErrorKind::Tag)))); 295 /// # } 296 /// ``` 297 /// 298 #[cfg(feature = "alloc")] 299 #[macro_export(local_inner_macros)] 300 macro_rules! count( 301 ($i:expr, $submac:ident!( $($args:tt)* ), $count: expr) => ( 302 count!($i, |i| $submac!(i, $($args)*), $count) 303 ); 304 ($i:expr, $f:expr, $count: expr) => ( 305 $crate::multi::count($f, $count)($i) 306 ); 307 ); 308 309 /// `length_count!(I -> IResult<I, nb>, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` 310 /// gets a number from the first parser, then applies the second parser that many times 311 /// 312 /// ```rust 313 /// # #[macro_use] extern crate nom; 314 /// # use nom::{Err, Needed}; 315 /// # use nom::error::ErrorKind; 316 /// use nom::number::complete::be_u8; 317 /// # fn main() { 318 /// named!(parser<Vec<&[u8]>>, length_count!(be_u8, tag!("abc"))); 319 /// 320 /// assert_eq!(parser(&b"\x02abcabcabc"[..]), Ok(((&b"abc"[..], vec![&b"abc"[..], &b"abc"[..]])))); 321 /// assert_eq!(parser(&b"\x04abcabcabc"[..]), Err(Err::Incomplete(Needed::Size(3)))); 322 /// # } 323 /// ``` 324 #[macro_export(local_inner_macros)] 325 #[cfg(feature = "alloc")] 326 macro_rules! length_count( 327 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 328 { 329 use $crate::lib::std::result::Result::*; 330 use $crate::Err; 331 332 match $submac!($i, $($args)*) { 333 Err(e) => Err(Err::convert(e)), 334 Ok((i, o)) => { 335 match count!(i, $submac2!($($args2)*), o as usize) { 336 Err(e) => Err(Err::convert(e)), 337 Ok((i2, o2)) => Ok((i2, o2)) 338 } 339 } 340 } 341 } 342 ); 343 344 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 345 length_count!($i, $submac!($($args)*), call!($g)); 346 ); 347 348 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 349 length_count!($i, call!($f), $submac!($($args)*)); 350 ); 351 352 ($i:expr, $f:expr, $g:expr) => ( 353 length_count!($i, call!($f), call!($g)); 354 ); 355 ); 356 357 /// `length_data!(I -> IResult<I, nb>) => O` 358 /// 359 /// `length_data` gets a number from the first parser, then takes a subslice of the input 360 /// of that size and returns that subslice 361 /// 362 /// ```rust 363 /// # #[macro_use] extern crate nom; 364 /// # use nom::{Err, Needed}; 365 /// # use nom::error::ErrorKind; 366 /// use nom::number::complete::be_u8; 367 /// # fn main() { 368 /// named!(parser, length_data!(be_u8)); 369 /// 370 /// assert_eq!(parser(&b"\x06abcabcabc"[..]), Ok((&b"abc"[..], &b"abcabc"[..]))); 371 /// assert_eq!(parser(&b"\x06abc"[..]), Err(Err::Incomplete(Needed::Size(6)))); 372 /// # } 373 /// ``` 374 #[macro_export(local_inner_macros)] 375 macro_rules! length_data( 376 ($i:expr, $submac:ident!( $($args:tt)* )) => ({ 377 $crate::multi::length_data(|i| $submac!(i, $($args)*))($i) 378 }); 379 380 ($i:expr, $f:expr) => ( 381 $crate::multi::length_data($f)($i) 382 ); 383 ); 384 385 /// `length_value!(I -> IResult<I, nb>, I -> IResult<I,O>) => I -> IResult<I, O>` 386 /// 387 /// Gets a number from the first parser, takes a subslice of the input of that size, 388 /// then applies the second parser on that subslice. If the second parser returns 389 /// `Incomplete`, `length_value` will return an error 390 /// 391 /// ```rust 392 /// # #[macro_use] extern crate nom; 393 /// # use nom::{Err, Needed}; 394 /// # use nom::error::ErrorKind; 395 /// use nom::number::complete::be_u8; 396 /// use nom::character::complete::alpha0; 397 /// use nom::bytes::complete::tag; 398 /// # fn main() { 399 /// named!(parser, length_value!(be_u8, alpha0)); 400 /// 401 /// assert_eq!(parser(&b"\x06abcabcabc"[..]), Ok((&b"abc"[..], &b"abcabc"[..]))); 402 /// assert_eq!(parser(&b"\x06abc"[..]), Err(Err::Incomplete(Needed::Size(6)))); 403 /// # } 404 /// ``` 405 #[macro_export(local_inner_macros)] 406 macro_rules! length_value( 407 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 408 length_value!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) 409 ); 410 411 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 412 length_value!($i, |i| $submac!(i, $($args)*), $g); 413 ); 414 415 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 416 length_value!($i, $f, |i| $submac!(i, $($args)*)); 417 ); 418 419 ($i:expr, $f:expr, $g:expr) => ( 420 $crate::multi::length_valuec($i, $f, $g); 421 ); 422 ); 423 424 /// `fold_many0!(I -> IResult<I,O>, R, Fn(R, O) -> R) => I -> IResult<I, R>` 425 /// Applies the parser 0 or more times and folds the list of return values 426 /// 427 /// the embedded parser may return Incomplete 428 /// 429 /// ``` 430 /// # #[macro_use] extern crate nom; 431 /// # fn main() { 432 /// named!(multi<&[u8], Vec<&[u8]> >, 433 /// fold_many0!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { 434 /// acc.push(item); 435 /// acc 436 /// })); 437 /// 438 /// let a = b"abcdabcdefgh"; 439 /// let b = b"azerty"; 440 /// 441 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 442 /// assert_eq!(multi(&a[..]),Ok((&b"efgh"[..], res))); 443 /// assert_eq!(multi(&b[..]),Ok((&b"azerty"[..], Vec::new()))); 444 /// # } 445 /// ``` 446 /// 0 or more 447 #[macro_export(local_inner_macros)] 448 macro_rules! fold_many0( 449 ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $fold_f:expr) => ( 450 fold_many0!($i, |i| $submac!(i, $($args)*), $init, $fold_f) 451 ); 452 ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( 453 $crate::multi::fold_many0($f, $init, $fold_f)($i) 454 ); 455 ); 456 457 /// `fold_many1!(I -> IResult<I,O>, R, Fn(R, O) -> R) => I -> IResult<I, R>` 458 /// Applies the parser 1 or more times and folds the list of return values 459 /// 460 /// the embedded parser may return Incomplete 461 /// 462 /// ``` 463 /// # #[macro_use] extern crate nom; 464 /// # use nom::Err; 465 /// # use nom::error::ErrorKind; 466 /// # fn main() { 467 /// named!(multi<&[u8], Vec<&[u8]> >, 468 /// fold_many1!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { 469 /// acc.push(item); 470 /// acc 471 /// })); 472 /// 473 /// let a = b"abcdabcdefgh"; 474 /// let b = b"azerty"; 475 /// 476 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 477 /// assert_eq!(multi(&a[..]),Ok((&b"efgh"[..], res))); 478 /// assert_eq!(multi(&b[..]), Err(Err::Error(error_position!(&b[..], ErrorKind::Many1)))); 479 /// # } 480 /// ``` 481 #[macro_export(local_inner_macros)] 482 macro_rules! fold_many1( 483 ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $fold_f:expr) => ( 484 fold_many1!($i, |i| $submac!(i, $($args)*), $init, $fold_f) 485 ); 486 ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( 487 $crate::multi::fold_many1c($i, $f, $init, $fold_f) 488 ); 489 ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( 490 fold_many1!($i, call!($f), $init, $fold_f); 491 ); 492 ); 493 494 /// `fold_many_m_n!(usize, usize, I -> IResult<I,O>, R, Fn(R, O) -> R) => I -> IResult<I, R>` 495 /// Applies the parser between m and n times (n included) and folds the list of return value 496 /// 497 /// the embedded parser may return Incomplete 498 /// 499 /// ``` 500 /// # #[macro_use] extern crate nom; 501 /// # use nom::Err; 502 /// # use nom::error::ErrorKind; 503 /// # fn main() { 504 /// named!(multi<&[u8], Vec<&[u8]> >, 505 /// fold_many_m_n!(2, 4, tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { 506 /// acc.push(item); 507 /// acc 508 /// })); 509 /// 510 /// let a = b"abcdefgh"; 511 /// let b = b"abcdabcdefgh"; 512 /// let c = b"abcdabcdabcdabcdabcdefgh"; 513 /// 514 /// assert_eq!(multi(&a[..]), Err(Err::Error(error_position!(&a[..], ErrorKind::ManyMN)))); 515 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 516 /// assert_eq!(multi(&b[..]),Ok((&b"efgh"[..], res))); 517 /// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; 518 /// assert_eq!(multi(&c[..]),Ok((&b"abcdefgh"[..], res2))); 519 /// # } 520 /// ``` 521 #[macro_export(local_inner_macros)] 522 macro_rules! fold_many_m_n( 523 ($i:expr, $m:expr, $n:expr, $submac:ident!( $($args:tt)* ), $init:expr, $fold_f:expr) => ( 524 fold_many_m_n!($i, $m, $n, |i| $submac!(i, $($args)*), $init, $fold_f) 525 ); 526 ($i:expr, $m:expr, $n:expr, $f:expr, $init:expr, $fold_f:expr) => ( 527 $crate::multi::fold_many_m_nc($i, $m, $n, $f, $init, $fold_f) 528 ); 529 ); 530 531 #[cfg(test)] 532 mod tests { 533 use crate::internal::{Err, IResult, Needed}; 534 use crate::error::ParseError; 535 use crate::lib::std::str::{self, FromStr}; 536 #[cfg(feature = "alloc")] 537 use crate::lib::std::vec::Vec; 538 use crate::character::streaming::digit1 as digit; 539 use crate::number::streaming::{be_u16, be_u8}; 540 use crate::error::ErrorKind; 541 542 // reproduce the tag and take macros, because of module import order 543 macro_rules! tag ( 544 ($i:expr, $inp: expr) => ( 545 { 546 #[inline(always)] 547 fn as_bytes<T: $crate::AsBytes>(b: &T) -> &[u8] { 548 b.as_bytes() 549 } 550 551 let expected = $inp; 552 let bytes = as_bytes(&expected); 553 554 tag_bytes!($i,bytes) 555 } 556 ); 557 ); 558 559 macro_rules! tag_bytes ( 560 ($i:expr, $bytes: expr) => ( 561 { 562 use $crate::lib::std::cmp::min; 563 let len = $i.len(); 564 let blen = $bytes.len(); 565 let m = min(len, blen); 566 let reduced = &$i[..m]; 567 let b = &$bytes[..m]; 568 569 let res: IResult<_,_,_> = if reduced != b { 570 Err($crate::Err::Error($crate::error::make_error($i, $crate::error::ErrorKind::Tag))) 571 } else if m < blen { 572 Err($crate::Err::Incomplete(Needed::Size(blen))) 573 } else { 574 Ok((&$i[blen..], reduced)) 575 }; 576 res 577 } 578 ); 579 ); 580 581 #[test] 582 #[cfg(feature = "alloc")] separated_list()583 fn separated_list() { 584 named!(multi<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!("abcd"))); 585 named!(multi_empty<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!(""))); 586 named!(multi_longsep<&[u8],Vec<&[u8]> >, separated_list!(tag!(".."), tag!("abcd"))); 587 588 let a = &b"abcdef"[..]; 589 let b = &b"abcd,abcdef"[..]; 590 let c = &b"azerty"[..]; 591 let d = &b",,abc"[..]; 592 let e = &b"abcd,abcd,ef"[..]; 593 let f = &b"abc"[..]; 594 let g = &b"abcd."[..]; 595 let h = &b"abcd,abc"[..]; 596 597 let res1 = vec![&b"abcd"[..]]; 598 assert_eq!(multi(a), Ok((&b"ef"[..], res1))); 599 let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; 600 assert_eq!(multi(b), Ok((&b"ef"[..], res2))); 601 assert_eq!(multi(c), Ok((&b"azerty"[..], Vec::new()))); 602 assert_eq!(multi_empty(d), Err(Err::Error(error_position!(d, ErrorKind::SeparatedList)))); 603 //let res3 = vec![&b""[..], &b""[..], &b""[..]]; 604 //assert_eq!(multi_empty(d),Ok((&b"abc"[..], res3))); 605 let res4 = vec![&b"abcd"[..], &b"abcd"[..]]; 606 assert_eq!(multi(e), Ok((&b",ef"[..], res4))); 607 608 assert_eq!(multi(f), Err(Err::Incomplete(Needed::Size(4)))); 609 assert_eq!(multi_longsep(g), Err(Err::Incomplete(Needed::Size(2)))); 610 assert_eq!(multi(h), Err(Err::Incomplete(Needed::Size(4)))); 611 } 612 613 #[test] 614 #[cfg(feature = "alloc")] separated_nonempty_list()615 fn separated_nonempty_list() { 616 named!(multi<&[u8],Vec<&[u8]> >, separated_nonempty_list!(tag!(","), tag!("abcd"))); 617 named!(multi_longsep<&[u8],Vec<&[u8]> >, separated_nonempty_list!(tag!(".."), tag!("abcd"))); 618 619 let a = &b"abcdef"[..]; 620 let b = &b"abcd,abcdef"[..]; 621 let c = &b"azerty"[..]; 622 let d = &b"abcd,abcd,ef"[..]; 623 624 let f = &b"abc"[..]; 625 let g = &b"abcd."[..]; 626 let h = &b"abcd,abc"[..]; 627 628 let res1 = vec![&b"abcd"[..]]; 629 assert_eq!(multi(a), Ok((&b"ef"[..], res1))); 630 let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; 631 assert_eq!(multi(b), Ok((&b"ef"[..], res2))); 632 assert_eq!(multi(c), Err(Err::Error(error_position!(c, ErrorKind::Tag)))); 633 let res3 = vec![&b"abcd"[..], &b"abcd"[..]]; 634 assert_eq!(multi(d), Ok((&b",ef"[..], res3))); 635 636 assert_eq!(multi(f), Err(Err::Incomplete(Needed::Size(4)))); 637 assert_eq!(multi_longsep(g), Err(Err::Incomplete(Needed::Size(2)))); 638 assert_eq!(multi(h), Err(Err::Incomplete(Needed::Size(4)))); 639 } 640 641 #[test] 642 #[cfg(feature = "alloc")] many0()643 fn many0() { 644 named!(tag_abcd, tag!("abcd")); 645 named!(tag_empty, tag!("")); 646 named!( multi<&[u8],Vec<&[u8]> >, many0!(tag_abcd) ); 647 named!( multi_empty<&[u8],Vec<&[u8]> >, many0!(tag_empty) ); 648 649 assert_eq!(multi(&b"abcdef"[..]), Ok((&b"ef"[..], vec![&b"abcd"[..]]))); 650 assert_eq!(multi(&b"abcdabcdefgh"[..]), Ok((&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]]))); 651 assert_eq!(multi(&b"azerty"[..]), Ok((&b"azerty"[..], Vec::new()))); 652 assert_eq!(multi(&b"abcdab"[..]), Err(Err::Incomplete(Needed::Size(4)))); 653 assert_eq!(multi(&b"abcd"[..]), Err(Err::Incomplete(Needed::Size(4)))); 654 assert_eq!(multi(&b""[..]), Err(Err::Incomplete(Needed::Size(4)))); 655 assert_eq!( 656 multi_empty(&b"abcdef"[..]), 657 Err(Err::Error(error_position!(&b"abcdef"[..], ErrorKind::Many0))) 658 ); 659 } 660 661 #[cfg(nightly)] 662 use test::Bencher; 663 664 #[cfg(nightly)] 665 #[bench] many0_bench(b: &mut Bencher)666 fn many0_bench(b: &mut Bencher) { 667 named!(multi<&[u8],Vec<&[u8]> >, many0!(tag!("abcd"))); 668 b.iter(|| multi(&b"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"[..])); 669 } 670 671 #[test] 672 #[cfg(feature = "alloc")] many1()673 fn many1() { 674 named!(multi<&[u8],Vec<&[u8]> >, many1!(tag!("abcd"))); 675 676 let a = &b"abcdef"[..]; 677 let b = &b"abcdabcdefgh"[..]; 678 let c = &b"azerty"[..]; 679 let d = &b"abcdab"[..]; 680 681 let res1 = vec![&b"abcd"[..]]; 682 assert_eq!(multi(a), Ok((&b"ef"[..], res1))); 683 let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; 684 assert_eq!(multi(b), Ok((&b"efgh"[..], res2))); 685 assert_eq!(multi(c), Err(Err::Error(error_position!(c, ErrorKind::Tag)))); 686 assert_eq!(multi(d), Err(Err::Incomplete(Needed::Size(4)))); 687 } 688 689 #[test] 690 #[cfg(feature = "alloc")] many_till()691 fn many_till() { 692 named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( tag!( "abcd" ), tag!( "efgh" ) ) ); 693 694 let a = b"abcdabcdefghabcd"; 695 let b = b"efghabcd"; 696 let c = b"azerty"; 697 698 let res_a = (vec![&b"abcd"[..], &b"abcd"[..]], &b"efgh"[..]); 699 let res_b: (Vec<&[u8]>, &[u8]) = (Vec::new(), &b"efgh"[..]); 700 assert_eq!(multi(&a[..]), Ok((&b"abcd"[..], res_a))); 701 assert_eq!(multi(&b[..]), Ok((&b"abcd"[..], res_b))); 702 assert_eq!( 703 multi(&c[..]), 704 Err(Err::Error(error_node_position!( 705 &c[..], 706 ErrorKind::ManyTill, 707 error_position!(&c[..], ErrorKind::Tag) 708 ))) 709 ); 710 } 711 712 #[test] 713 #[cfg(feature = "std")] infinite_many()714 fn infinite_many() { 715 fn tst(input: &[u8]) -> IResult<&[u8], &[u8]> { 716 println!("input: {:?}", input); 717 Err(Err::Error(error_position!(input, ErrorKind::Tag))) 718 } 719 720 // should not go into an infinite loop 721 named!(multi0<&[u8],Vec<&[u8]> >, many0!(tst)); 722 let a = &b"abcdef"[..]; 723 assert_eq!(multi0(a), Ok((a, Vec::new()))); 724 725 named!(multi1<&[u8],Vec<&[u8]> >, many1!(tst)); 726 let a = &b"abcdef"[..]; 727 assert_eq!(multi1(a), Err(Err::Error(error_position!(a, ErrorKind::Tag)))); 728 } 729 730 #[test] 731 #[cfg(feature = "alloc")] many_m_n()732 fn many_m_n() { 733 named!(multi<&[u8],Vec<&[u8]> >, many_m_n!(2, 4, tag!("Abcd"))); 734 735 let a = &b"Abcdef"[..]; 736 let b = &b"AbcdAbcdefgh"[..]; 737 let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; 738 let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; 739 let e = &b"AbcdAb"[..]; 740 741 assert_eq!(multi(a), Err(Err::Error(error_position!(&b"ef"[..], ErrorKind::Tag)))); 742 let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; 743 assert_eq!(multi(b), Ok((&b"efgh"[..], res1))); 744 let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; 745 assert_eq!(multi(c), Ok((&b"efgh"[..], res2))); 746 let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; 747 assert_eq!(multi(d), Ok((&b"Abcdefgh"[..], res3))); 748 assert_eq!(multi(e), Err(Err::Incomplete(Needed::Size(4)))); 749 } 750 751 #[test] 752 #[cfg(feature = "alloc")] count()753 fn count() { 754 const TIMES: usize = 2; 755 named!(tag_abc, tag!("abc")); 756 named!( cnt_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); 757 758 assert_eq!(cnt_2(&b"abcabcabcdef"[..]), Ok((&b"abcdef"[..], vec![&b"abc"[..], &b"abc"[..]]))); 759 assert_eq!(cnt_2(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); 760 assert_eq!(cnt_2(&b"abcab"[..]), Err(Err::Incomplete(Needed::Size(3)))); 761 assert_eq!(cnt_2(&b"xxx"[..]), Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))); 762 assert_eq!( 763 cnt_2(&b"xxxabcabcdef"[..]), 764 Err(Err::Error(error_position!(&b"xxxabcabcdef"[..], ErrorKind::Tag))) 765 ); 766 assert_eq!( 767 cnt_2(&b"abcxxxabcdef"[..]), 768 Err(Err::Error(error_position!(&b"xxxabcdef"[..], ErrorKind::Tag))) 769 ); 770 } 771 772 #[test] 773 #[cfg(feature = "alloc")] count_zero()774 fn count_zero() { 775 const TIMES: usize = 0; 776 named!(tag_abc, tag!("abc")); 777 named!( counter_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); 778 779 let done = &b"abcabcabcdef"[..]; 780 let parsed_done = Vec::new(); 781 let rest = done; 782 let incomplete_1 = &b"ab"[..]; 783 let parsed_incompl_1 = Vec::new(); 784 let incomplete_2 = &b"abcab"[..]; 785 let parsed_incompl_2 = Vec::new(); 786 let error = &b"xxx"[..]; 787 let error_remain = &b"xxx"[..]; 788 let parsed_err = Vec::new(); 789 let error_1 = &b"xxxabcabcdef"[..]; 790 let parsed_err_1 = Vec::new(); 791 let error_1_remain = &b"xxxabcabcdef"[..]; 792 let error_2 = &b"abcxxxabcdef"[..]; 793 let parsed_err_2 = Vec::new(); 794 let error_2_remain = &b"abcxxxabcdef"[..]; 795 796 assert_eq!(counter_2(done), Ok((rest, parsed_done))); 797 assert_eq!(counter_2(incomplete_1), Ok((incomplete_1, parsed_incompl_1))); 798 assert_eq!(counter_2(incomplete_2), Ok((incomplete_2, parsed_incompl_2))); 799 assert_eq!(counter_2(error), Ok((error_remain, parsed_err))); 800 assert_eq!(counter_2(error_1), Ok((error_1_remain, parsed_err_1))); 801 assert_eq!(counter_2(error_2), Ok((error_2_remain, parsed_err_2))); 802 } 803 804 #[derive(Debug, Clone, PartialEq)] 805 pub struct NilError; 806 807 impl<I> From<(I,ErrorKind)> for NilError { from(_: (I, ErrorKind)) -> Self808 fn from(_: (I, ErrorKind)) -> Self { 809 NilError 810 } 811 } 812 813 impl<I> ParseError<I> for NilError { from_error_kind(_: I, _: ErrorKind) -> NilError814 fn from_error_kind(_: I, _: ErrorKind) -> NilError { 815 NilError 816 } append(_: I, _: ErrorKind, _: NilError) -> NilError817 fn append(_: I, _: ErrorKind, _: NilError) -> NilError { 818 NilError 819 } 820 } 821 822 named!(pub number<u32>, map_res!( 823 map_res!( 824 digit, 825 str::from_utf8 826 ), 827 FromStr::from_str 828 )); 829 830 #[test] 831 #[cfg(feature = "alloc")] length_count()832 fn length_count() { 833 named!(tag_abc, tag!(&b"abc"[..])); 834 named!( cnt<&[u8], Vec<&[u8]> >, length_count!(number, tag_abc) ); 835 836 assert_eq!(cnt(&b"2abcabcabcdef"[..]), Ok((&b"abcdef"[..], vec![&b"abc"[..], &b"abc"[..]]))); 837 assert_eq!(cnt(&b"2ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); 838 assert_eq!(cnt(&b"3abcab"[..]), Err(Err::Incomplete(Needed::Size(3)))); 839 assert_eq!(cnt(&b"xxx"[..]), Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Digit)))); 840 assert_eq!( 841 cnt(&b"2abcxxx"[..]), 842 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 843 ); 844 } 845 846 #[test] length_data()847 fn length_data() { 848 named!( take<&[u8], &[u8]>, length_data!(number) ); 849 850 assert_eq!(take(&b"6abcabcabcdef"[..]), Ok((&b"abcdef"[..], &b"abcabc"[..]))); 851 assert_eq!(take(&b"3ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); 852 assert_eq!(take(&b"xxx"[..]), Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Digit)))); 853 assert_eq!(take(&b"2abcxxx"[..]), Ok((&b"cxxx"[..], &b"ab"[..]))); 854 } 855 856 #[test] length_value_test()857 fn length_value_test() { 858 named!(length_value_1<&[u8], u16 >, length_value!(be_u8, be_u16)); 859 named!(length_value_2<&[u8], (u8, u8) >, length_value!(be_u8, tuple!(be_u8, be_u8))); 860 861 let i1 = [0, 5, 6]; 862 assert_eq!(length_value_1(&i1), Err(Err::Error(error_position!(&b""[..], ErrorKind::Complete)))); 863 assert_eq!(length_value_2(&i1), Err(Err::Error(error_position!(&b""[..], ErrorKind::Complete)))); 864 865 let i2 = [1, 5, 6, 3]; 866 assert_eq!( 867 length_value_1(&i2), 868 Err(Err::Error(error_position!(&i2[1..2], ErrorKind::Complete))) 869 ); 870 assert_eq!( 871 length_value_2(&i2), 872 Err(Err::Error(error_position!(&i2[1..2], ErrorKind::Complete))) 873 ); 874 875 let i3 = [2, 5, 6, 3, 4, 5, 7]; 876 assert_eq!(length_value_1(&i3), Ok((&i3[3..], 1286))); 877 assert_eq!(length_value_2(&i3), Ok((&i3[3..], (5, 6)))); 878 879 let i4 = [3, 5, 6, 3, 4, 5]; 880 assert_eq!(length_value_1(&i4), Ok((&i4[4..], 1286))); 881 assert_eq!(length_value_2(&i4), Ok((&i4[4..], (5, 6)))); 882 } 883 884 #[test] 885 #[cfg(feature = "alloc")] fold_many0()886 fn fold_many0() { 887 fn fold_into_vec<T>(mut acc: Vec<T>, item: T) -> Vec<T> { 888 acc.push(item); 889 acc 890 }; 891 named!(tag_abcd, tag!("abcd")); 892 named!(tag_empty, tag!("")); 893 named!( multi<&[u8],Vec<&[u8]> >, fold_many0!(tag_abcd, Vec::new(), fold_into_vec) ); 894 named!( multi_empty<&[u8],Vec<&[u8]> >, fold_many0!(tag_empty, Vec::new(), fold_into_vec) ); 895 896 assert_eq!(multi(&b"abcdef"[..]), Ok((&b"ef"[..], vec![&b"abcd"[..]]))); 897 assert_eq!(multi(&b"abcdabcdefgh"[..]), Ok((&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]]))); 898 assert_eq!(multi(&b"azerty"[..]), Ok((&b"azerty"[..], Vec::new()))); 899 assert_eq!(multi(&b"abcdab"[..]), Err(Err::Incomplete(Needed::Size(4)))); 900 assert_eq!(multi(&b"abcd"[..]), Err(Err::Incomplete(Needed::Size(4)))); 901 assert_eq!(multi(&b""[..]), Err(Err::Incomplete(Needed::Size(4)))); 902 assert_eq!( 903 multi_empty(&b"abcdef"[..]), 904 Err(Err::Error(error_position!(&b"abcdef"[..], ErrorKind::Many0))) 905 ); 906 } 907 908 #[test] 909 #[cfg(feature = "alloc")] fold_many1()910 fn fold_many1() { 911 fn fold_into_vec<T>(mut acc: Vec<T>, item: T) -> Vec<T> { 912 acc.push(item); 913 acc 914 }; 915 named!(multi<&[u8],Vec<&[u8]> >, fold_many1!(tag!("abcd"), Vec::new(), fold_into_vec)); 916 917 let a = &b"abcdef"[..]; 918 let b = &b"abcdabcdefgh"[..]; 919 let c = &b"azerty"[..]; 920 let d = &b"abcdab"[..]; 921 922 let res1 = vec![&b"abcd"[..]]; 923 assert_eq!(multi(a), Ok((&b"ef"[..], res1))); 924 let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; 925 assert_eq!(multi(b), Ok((&b"efgh"[..], res2))); 926 assert_eq!(multi(c), Err(Err::Error(error_position!(c, ErrorKind::Many1)))); 927 assert_eq!(multi(d), Err(Err::Incomplete(Needed::Size(4)))); 928 } 929 930 #[test] 931 #[cfg(feature = "alloc")] fold_many_m_n()932 fn fold_many_m_n() { 933 fn fold_into_vec<T>(mut acc: Vec<T>, item: T) -> Vec<T> { 934 acc.push(item); 935 acc 936 }; 937 named!(multi<&[u8],Vec<&[u8]> >, fold_many_m_n!(2, 4, tag!("Abcd"), Vec::new(), fold_into_vec)); 938 939 let a = &b"Abcdef"[..]; 940 let b = &b"AbcdAbcdefgh"[..]; 941 let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; 942 let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; 943 let e = &b"AbcdAb"[..]; 944 945 assert_eq!(multi(a), Err(Err::Error(error_position!(a, ErrorKind::ManyMN)))); 946 let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; 947 assert_eq!(multi(b), Ok((&b"efgh"[..], res1))); 948 let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; 949 assert_eq!(multi(c), Ok((&b"efgh"[..], res2))); 950 let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; 951 assert_eq!(multi(d), Ok((&b"Abcdefgh"[..], res3))); 952 assert_eq!(multi(e), Err(Err::Incomplete(Needed::Size(4)))); 953 } 954 955 #[test] many0_count()956 fn many0_count() { 957 named!( 958 count0_nums(&[u8]) -> usize, 959 many0_count!(pair!(digit, tag!(","))) 960 ); 961 962 assert_eq!(count0_nums(&b"123,junk"[..]), Ok((&b"junk"[..], 1))); 963 964 assert_eq!(count0_nums(&b"123,45,junk"[..]), Ok((&b"junk"[..], 2))); 965 966 assert_eq!(count0_nums(&b"1,2,3,4,5,6,7,8,9,0,junk"[..]), Ok((&b"junk"[..], 10))); 967 968 assert_eq!(count0_nums(&b"hello"[..]), Ok((&b"hello"[..], 0))); 969 } 970 971 #[test] many1_count()972 fn many1_count() { 973 named!( 974 count1_nums(&[u8]) -> usize, 975 many1_count!(pair!(digit, tag!(","))) 976 ); 977 978 assert_eq!(count1_nums(&b"123,45,junk"[..]), Ok((&b"junk"[..], 2))); 979 980 assert_eq!(count1_nums(&b"1,2,3,4,5,6,7,8,9,0,junk"[..]), Ok((&b"junk"[..], 10))); 981 982 assert_eq!( 983 count1_nums(&b"hello"[..]), 984 Err(Err::Error(error_position!(&b"hello"[..], ErrorKind::Many1Count))) 985 ); 986 } 987 988 } 989