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 Vec<X> 5 #[macro_export] 6 macro_rules! separated_list( 7 ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => ( 8 { 9 use $crate::InputLength; 10 11 //FIXME: use crate vec 12 let mut res = ::std::vec::Vec::new(); 13 let mut input = $i; 14 15 // get the first element 16 match $submac!(input, $($args2)*) { 17 $crate::IResult::Error(_) => $crate::IResult::Done(input, ::std::vec::Vec::new()), 18 $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), 19 $crate::IResult::Done(i,o) => { 20 if i.input_len() == input.input_len() { 21 $crate::IResult::Error(error_position!($crate::ErrorKind::SeparatedList,input)) 22 } else { 23 res.push(o); 24 input = i; 25 26 loop { 27 // get the separator first 28 if let $crate::IResult::Done(i2,_) = $sep!(input, $($args)*) { 29 if i2.input_len() == input.input_len() { 30 break; 31 } 32 33 // get the element next 34 if let $crate::IResult::Done(i3,o3) = $submac!(i2, $($args2)*) { 35 if i3.input_len() == i2.input_len() { 36 break; 37 } 38 res.push(o3); 39 input = i3; 40 } else { 41 break; 42 } 43 } else { 44 break; 45 } 46 } 47 $crate::IResult::Done(input, res) 48 } 49 }, 50 } 51 } 52 ); 53 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 54 separated_list!($i, $submac!($($args)*), call!($g)); 55 ); 56 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 57 separated_list!($i, call!($f), $submac!($($args)*)); 58 ); 59 ($i:expr, $f:expr, $g:expr) => ( 60 separated_list!($i, call!($f), call!($g)); 61 ); 62 ); 63 64 /// `separated_nonempty_list!(I -> IResult<I,T>, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` 65 /// separated_nonempty_list(sep, X) returns Vec<X> 66 #[macro_export] 67 macro_rules! separated_nonempty_list( 68 ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => ( 69 { 70 use $crate::InputLength; 71 72 let mut res = ::std::vec::Vec::new(); 73 let mut input = $i; 74 75 // get the first element 76 match $submac!(input, $($args2)*) { 77 $crate::IResult::Error(a) => $crate::IResult::Error(a), 78 $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), 79 $crate::IResult::Done(i,o) => { 80 if i.input_len() == input.len() { 81 $crate::IResult::Error(error_position!($crate::ErrorKind::SeparatedNonEmptyList,input)) 82 } else { 83 res.push(o); 84 input = i; 85 86 loop { 87 if let $crate::IResult::Done(i2,_) = $sep!(input, $($args)*) { 88 if i2.input_len() == input.input_len() { 89 break; 90 } 91 92 if let $crate::IResult::Done(i3,o3) = $submac!(i2, $($args2)*) { 93 if i3.input_len() == i2.input_len() { 94 break; 95 } 96 res.push(o3); 97 input = i3; 98 } else { 99 break; 100 } 101 } else { 102 break; 103 } 104 } 105 $crate::IResult::Done(input, res) 106 } 107 }, 108 } 109 } 110 ); 111 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 112 separated_nonempty_list!($i, $submac!($($args)*), call!($g)); 113 ); 114 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 115 separated_nonempty_list!($i, call!($f), $submac!($($args)*)); 116 ); 117 ($i:expr, $f:expr, $g:expr) => ( 118 separated_nonempty_list!($i, call!($f), call!($g)); 119 ); 120 ); 121 122 /// `many0!(I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` 123 /// Applies the parser 0 or more times and returns the list of results in a Vec 124 /// 125 /// the embedded parser may return Incomplete 126 /// 127 /// ``` 128 /// # #[macro_use] extern crate nom; 129 /// # use nom::IResult::Done; 130 /// # fn main() { 131 /// named!(multi<&[u8], Vec<&[u8]> >, many0!( tag!( "abcd" ) ) ); 132 /// 133 /// let a = b"abcdabcdefgh"; 134 /// let b = b"azerty"; 135 /// 136 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 137 /// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); 138 /// assert_eq!(multi(&b[..]), Done(&b"azerty"[..], Vec::new())); 139 /// # } 140 /// ``` 141 /// 0 or more 142 #[macro_export] 143 macro_rules! many0( 144 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 145 { 146 use $crate::InputLength; 147 148 let ret; 149 let mut res = ::std::vec::Vec::new(); 150 let mut input = $i; 151 152 loop { 153 if input.input_len() == 0 { 154 ret = $crate::IResult::Done(input, res); 155 break; 156 } 157 158 match $submac!(input, $($args)*) { 159 $crate::IResult::Error(_) => { 160 ret = $crate::IResult::Done(input, res); 161 break; 162 }, 163 $crate::IResult::Incomplete($crate::Needed::Unknown) => { 164 ret = $crate::IResult::Incomplete($crate::Needed::Unknown); 165 break; 166 }, 167 $crate::IResult::Incomplete($crate::Needed::Size(i)) => { 168 let (size,overflowed) = i.overflowing_add(($i).input_len() - input.input_len()); 169 ret = match overflowed { 170 true => $crate::IResult::Incomplete($crate::Needed::Unknown), 171 false => $crate::IResult::Incomplete($crate::Needed::Size(size)), 172 }; 173 break; 174 }, 175 $crate::IResult::Done(i, o) => { 176 // loop trip must always consume (otherwise infinite loops) 177 if i == input { 178 ret = $crate::IResult::Error(error_position!($crate::ErrorKind::Many0,input)); 179 break; 180 } 181 182 res.push(o); 183 input = i; 184 } 185 } 186 } 187 188 ret 189 } 190 ); 191 ($i:expr, $f:expr) => ( 192 many0!($i, call!($f)); 193 ); 194 ); 195 196 /// `many1!(I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` 197 /// Applies the parser 1 or more times and returns the list of results in a Vec 198 /// 199 /// the embedded parser may return Incomplete 200 /// 201 /// ``` 202 /// # #[macro_use] extern crate nom; 203 /// # use nom::IResult::{Done, Error}; 204 /// # #[cfg(feature = "verbose-errors")] 205 /// # use nom::Err::Position; 206 /// # use nom::ErrorKind; 207 /// # fn main() { 208 /// named!(multi<&[u8], Vec<&[u8]> >, many1!( tag!( "abcd" ) ) ); 209 /// 210 /// let a = b"abcdabcdefgh"; 211 /// let b = b"azerty"; 212 /// 213 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 214 /// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); 215 /// assert_eq!(multi(&b[..]), Error(error_position!(ErrorKind::Many1,&b[..]))); 216 /// # } 217 /// ``` 218 #[macro_export] 219 macro_rules! many1( 220 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 221 { 222 use $crate::InputLength; 223 match $submac!($i, $($args)*) { 224 $crate::IResult::Error(_) => $crate::IResult::Error( 225 error_position!($crate::ErrorKind::Many1,$i) 226 ), 227 $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), 228 $crate::IResult::Done(i1,o1) => { 229 if i1.input_len() == 0 { 230 $crate::IResult::Done(i1,vec![o1]) 231 } else { 232 233 let mut res = ::std::vec::Vec::with_capacity(4); 234 res.push(o1); 235 let mut input = i1; 236 let mut incomplete: ::std::option::Option<$crate::Needed> = 237 ::std::option::Option::None; 238 loop { 239 if input.input_len() == 0 { 240 break; 241 } 242 match $submac!(input, $($args)*) { 243 $crate::IResult::Error(_) => { 244 break; 245 }, 246 $crate::IResult::Incomplete($crate::Needed::Unknown) => { 247 incomplete = ::std::option::Option::Some($crate::Needed::Unknown); 248 break; 249 }, 250 $crate::IResult::Incomplete($crate::Needed::Size(i)) => { 251 let (size,overflowed) = i.overflowing_add(($i).input_len() - input.input_len()); 252 incomplete = ::std::option::Option::Some( 253 match overflowed { 254 true => $crate::Needed::Unknown, 255 false => $crate::Needed::Size(size), 256 } 257 ); 258 break; 259 }, 260 $crate::IResult::Done(i, o) => { 261 if i.input_len() == input.input_len() { 262 break; 263 } 264 res.push(o); 265 input = i; 266 } 267 } 268 } 269 270 match incomplete { 271 ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), 272 ::std::option::Option::None => $crate::IResult::Done(input, res) 273 } 274 } 275 } 276 } 277 } 278 ); 279 ($i:expr, $f:expr) => ( 280 many1!($i, call!($f)); 281 ); 282 ); 283 284 /// `many_till!(I -> IResult<I,O>, I -> IResult<I,P>) => I -> IResult<I, (Vec<O>, P)>` 285 /// Applies the first parser until the second applies. Returns a tuple containing the list 286 /// of results from the first in a Vec and the result of the second. 287 /// 288 /// The first embedded parser may return Incomplete 289 /// 290 /// ``` 291 /// # #[macro_use] extern crate nom; 292 /// # use nom::IResult::{Done, Error}; 293 /// # #[cfg(feature = "verbose-errors")] 294 /// # use nom::Err::Position; 295 /// # use nom::ErrorKind; 296 /// # fn main() { 297 /// named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( tag!( "abcd" ), tag!( "efgh" ) ) ); 298 /// 299 /// let a = b"abcdabcdefghabcd"; 300 /// let b = b"efghabcd"; 301 /// let c = b"azerty"; 302 /// 303 /// let res_a = (vec![&b"abcd"[..], &b"abcd"[..]], &b"efgh"[..]); 304 /// let res_b: (Vec<&[u8]>, &[u8]) = (Vec::new(), &b"efgh"[..]); 305 /// assert_eq!(multi(&a[..]), Done(&b"abcd"[..], res_a)); 306 /// assert_eq!(multi(&b[..]), Done(&b"abcd"[..], res_b)); 307 /// assert_eq!(multi(&c[..]), Error(error_position!(ErrorKind::ManyTill,&c[..]))); 308 /// # } 309 /// ``` 310 #[macro_export] 311 macro_rules! many_till( 312 ($i:expr, $submac1:ident!( $($args1:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 313 { 314 use $crate::InputLength; 315 316 let ret; 317 let mut res = ::std::vec::Vec::new(); 318 let mut input = $i; 319 320 loop { 321 match $submac2!(input, $($args2)*) { 322 $crate::IResult::Done(i, o) => { 323 ret = $crate::IResult::Done(i, (res, o)); 324 break; 325 }, 326 _ => { 327 match $submac1!(input, $($args1)*) { 328 $crate::IResult::Error(_) => { 329 ret = $crate::IResult::Error(error_position!($crate::ErrorKind::ManyTill,input)); 330 break; 331 }, 332 $crate::IResult::Incomplete($crate::Needed::Unknown) => { 333 ret = $crate::IResult::Incomplete($crate::Needed::Unknown); 334 break; 335 }, 336 $crate::IResult::Incomplete($crate::Needed::Size(i)) => { 337 let (size,overflowed) = i.overflowing_add(($i).input_len() - input.input_len()); 338 ret = match overflowed { 339 true => $crate::IResult::Incomplete($crate::Needed::Unknown), 340 false => $crate::IResult::Incomplete($crate::Needed::Size(size)), 341 }; 342 break; 343 }, 344 $crate::IResult::Done(i, o) => { 345 // loop trip must always consume (otherwise infinite loops) 346 if i == input { 347 ret = $crate::IResult::Error(error_position!($crate::ErrorKind::ManyTill,input)); 348 break; 349 } 350 351 res.push(o); 352 input = i; 353 }, 354 } 355 }, 356 } 357 } 358 359 ret 360 } 361 ); 362 ($i:expr, $f:expr, $g: expr) => ( 363 many_till!($i, call!($f), call!($g)); 364 ); 365 ); 366 367 /// `many_m_n!(usize, usize, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` 368 /// Applies the parser between m and n times (n included) and returns the list of 369 /// results in a Vec 370 /// 371 /// the embedded parser may return Incomplete 372 /// 373 /// ``` 374 /// # #[macro_use] extern crate nom; 375 /// # use nom::IResult::{Done, Error}; 376 /// # #[cfg(feature = "verbose-errors")] 377 /// # use nom::Err::Position; 378 /// # use nom::ErrorKind; 379 /// # fn main() { 380 /// named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, tag!( "abcd" ) ) ); 381 /// 382 /// let a = b"abcdefgh"; 383 /// let b = b"abcdabcdefgh"; 384 /// let c = b"abcdabcdabcdabcdabcdefgh"; 385 /// 386 /// assert_eq!(multi(&a[..]),Error(error_position!(ErrorKind::ManyMN,&a[..]))); 387 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 388 /// assert_eq!(multi(&b[..]), Done(&b"efgh"[..], res)); 389 /// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; 390 /// assert_eq!(multi(&c[..]), Done(&b"abcdefgh"[..], res2)); 391 /// # } 392 /// ``` 393 #[macro_export] 394 macro_rules! many_m_n( 395 ($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* )) => ( 396 { 397 use $crate::InputLength; 398 let mut res = ::std::vec::Vec::with_capacity($m); 399 let mut input = $i; 400 let mut count: usize = 0; 401 let mut err = false; 402 let mut incomplete: ::std::option::Option<$crate::Needed> = ::std::option::Option::None; 403 loop { 404 if count == $n { break } 405 match $submac!(input, $($args)*) { 406 $crate::IResult::Done(i, o) => { 407 // do not allow parsers that do not consume input (causes infinite loops) 408 if i.input_len() == input.input_len() { 409 break; 410 } 411 res.push(o); 412 input = i; 413 count += 1; 414 } 415 $crate::IResult::Error(_) => { 416 err = true; 417 break; 418 }, 419 $crate::IResult::Incomplete($crate::Needed::Unknown) => { 420 incomplete = ::std::option::Option::Some($crate::Needed::Unknown); 421 break; 422 }, 423 $crate::IResult::Incomplete($crate::Needed::Size(i)) => { 424 let (size,overflowed) = i.overflowing_add(($i).input_len() - input.input_len()); 425 incomplete = ::std::option::Option::Some( 426 match overflowed { 427 true => $crate::Needed::Unknown, 428 false => $crate::Needed::Size(size), 429 } 430 ); 431 break; 432 }, 433 } 434 if input.input_len() == 0 { 435 break; 436 } 437 } 438 439 if count < $m { 440 if err { 441 $crate::IResult::Error(error_position!($crate::ErrorKind::ManyMN,$i)) 442 } else { 443 match incomplete { 444 ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), 445 ::std::option::Option::None => $crate::IResult::Incomplete( 446 $crate::Needed::Unknown 447 ) 448 } 449 } 450 } else { 451 match incomplete { 452 ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), 453 ::std::option::Option::None => $crate::IResult::Done(input, res) 454 } 455 } 456 } 457 ); 458 ($i:expr, $m:expr, $n: expr, $f:expr) => ( 459 many_m_n!($i, $m, $n, call!($f)); 460 ); 461 ); 462 463 /// `count!(I -> IResult<I,O>, nb) => I -> IResult<I, Vec<O>>` 464 /// Applies the child parser a specified number of times 465 /// 466 /// ``` 467 /// # #[macro_use] extern crate nom; 468 /// # use nom::IResult::{Done,Error}; 469 /// # #[cfg(feature = "verbose-errors")] 470 /// # use nom::Err::Position; 471 /// # use nom::ErrorKind; 472 /// # fn main() { 473 /// named!(counter< Vec<&[u8]> >, count!( tag!( "abcd" ), 2 ) ); 474 /// 475 /// let a = b"abcdabcdabcdef"; 476 /// let b = b"abcdefgh"; 477 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 478 /// 479 /// assert_eq!(counter(&a[..]), Done(&b"abcdef"[..], res)); 480 /// assert_eq!(counter(&b[..]), Error(error_position!(ErrorKind::Count, &b[..]))); 481 /// # } 482 /// ``` 483 /// 484 #[macro_export] 485 macro_rules! count( 486 ($i:expr, $submac:ident!( $($args:tt)* ), $count: expr) => ( 487 { 488 let ret; 489 let mut input = $i; 490 let mut res = ::std::vec::Vec::with_capacity($count); 491 492 loop { 493 if res.len() == $count { 494 ret = $crate::IResult::Done(input, res); 495 break; 496 } 497 498 match $submac!(input, $($args)*) { 499 $crate::IResult::Done(i,o) => { 500 res.push(o); 501 input = i; 502 }, 503 $crate::IResult::Error(_) => { 504 ret = $crate::IResult::Error(error_position!($crate::ErrorKind::Count,$i)); 505 break; 506 }, 507 $crate::IResult::Incomplete($crate::Needed::Unknown) => { 508 ret = $crate::IResult::Incomplete($crate::Needed::Unknown); 509 break; 510 } 511 $crate::IResult::Incomplete($crate::Needed::Size(sz)) => { 512 let (size,overflowed) = sz.overflowing_add( 513 $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&input) 514 ); 515 ret = match overflowed { 516 true => $crate::IResult::Incomplete($crate::Needed::Unknown), 517 false => $crate::IResult::Incomplete($crate::Needed::Size(size)), 518 }; 519 break; 520 } 521 } 522 } 523 524 ret 525 } 526 ); 527 ($i:expr, $f:expr, $count: expr) => ( 528 count!($i, call!($f), $count); 529 ); 530 ); 531 532 /// `count_fixed!(O, I -> IResult<I,O>, nb) => I -> IResult<I, [O; nb]>` 533 /// Applies the child parser a fixed number of times and returns a fixed size array 534 /// The type must be specified and it must be `Copy` 535 /// 536 /// ``` 537 /// # #[macro_use] extern crate nom; 538 /// # use nom::IResult::{Done,Error}; 539 /// # #[cfg(feature = "verbose-errors")] 540 /// # use nom::Err::Position; 541 /// # use nom::ErrorKind; 542 /// # fn main() { 543 /// named!(counter< [&[u8]; 2] >, count_fixed!( &[u8], tag!( "abcd" ), 2 ) ); 544 /// // can omit the type specifier if returning slices 545 /// // named!(counter< [&[u8]; 2] >, count_fixed!( tag!( "abcd" ), 2 ) ); 546 /// 547 /// let a = b"abcdabcdabcdef"; 548 /// let b = b"abcdefgh"; 549 /// let res = [&b"abcd"[..], &b"abcd"[..]]; 550 /// 551 /// assert_eq!(counter(&a[..]), Done(&b"abcdef"[..], res)); 552 /// assert_eq!(counter(&b[..]), Error(error_position!(ErrorKind::Count, &b[..]))); 553 /// # } 554 /// ``` 555 /// 556 #[macro_export] 557 macro_rules! count_fixed ( 558 ($i:expr, $typ:ty, $submac:ident!( $($args:tt)* ), $count: expr) => ( 559 { 560 let ret; 561 let mut input = $i; 562 // `$typ` must be Copy, and thus having no destructor, this is panic safe 563 let mut res: [$typ; $count] = unsafe{[::std::mem::uninitialized(); $count as usize]}; 564 let mut cnt: usize = 0; 565 566 loop { 567 if cnt == $count { 568 ret = $crate::IResult::Done(input, res); break; 569 } 570 571 match $submac!(input, $($args)*) { 572 $crate::IResult::Done(i,o) => { 573 res[cnt] = o; 574 cnt += 1; 575 input = i; 576 }, 577 $crate::IResult::Error(_) => { 578 ret = $crate::IResult::Error(error_position!($crate::ErrorKind::Count,$i)); 579 break; 580 }, 581 $crate::IResult::Incomplete($crate::Needed::Unknown) => { 582 ret = $crate::IResult::Incomplete($crate::Needed::Unknown); 583 break; 584 } 585 $crate::IResult::Incomplete($crate::Needed::Size(sz)) => { 586 let (size,overflowed) = sz.overflowing_add( 587 $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&input) 588 ); 589 ret = match overflowed { 590 true => $crate::IResult::Incomplete($crate::Needed::Unknown), 591 false => $crate::IResult::Incomplete($crate::Needed::Size(size)), 592 }; 593 break; 594 } 595 } 596 } 597 598 ret 599 } 600 ); 601 ($i:expr, $typ: ty, $f:ident, $count: expr) => ( 602 count_fixed!($i, $typ, call!($f), $count); 603 ); 604 ); 605 606 /// `length_count!(I -> IResult<I, nb>, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` 607 /// gets a number from the first parser, then applies the second parser that many times 608 #[macro_export] 609 macro_rules! length_count( 610 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 611 { 612 match $submac!($i, $($args)*) { 613 $crate::IResult::Error(e) => $crate::IResult::Error(e), 614 $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), 615 $crate::IResult::Done(i, o) => { 616 match count!(i, $submac2!($($args2)*), o as usize) { 617 $crate::IResult::Error(e) => $crate::IResult::Error(e), 618 $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), 619 $crate::IResult::Incomplete($crate::Needed::Size(n)) => { 620 let (size,overflowed) = n.overflowing_add( 621 $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i) 622 ); 623 match overflowed { 624 true => $crate::IResult::Incomplete($crate::Needed::Unknown), 625 false => $crate::IResult::Incomplete($crate::Needed::Size(size)), 626 } 627 }, 628 $crate::IResult::Done(i2, o2) => $crate::IResult::Done(i2, o2) 629 } 630 } 631 } 632 } 633 ); 634 635 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 636 length_count!($i, $submac!($($args)*), call!($g)); 637 ); 638 639 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 640 length_count!($i, call!($f), $submac!($($args)*)); 641 ); 642 643 ($i:expr, $f:expr, $g:expr) => ( 644 length_count!($i, call!($f), call!($g)); 645 ); 646 ); 647 648 /// `length_data!(I -> IResult<I, nb>) => O` 649 /// 650 /// `length_data` gets a number from the first parser, than takes a subslice of the input 651 /// of that size, and returns that subslice 652 #[macro_export] 653 macro_rules! length_data( 654 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 655 match $submac!($i, $($args)*) { 656 $crate::IResult::Error(e) => $crate::IResult::Error(e), 657 $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), 658 $crate::IResult::Done(i, o) => { 659 match take!(i, o as usize) { 660 $crate::IResult::Error(e) => $crate::IResult::Error(e), 661 $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), 662 $crate::IResult::Incomplete($crate::Needed::Size(n)) => { 663 let (size,overflowed) = n.overflowing_add( 664 $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i) 665 ); 666 match overflowed { 667 true => $crate::IResult::Incomplete($crate::Needed::Unknown), 668 false => $crate::IResult::Incomplete($crate::Needed::Size(size)), 669 } 670 }, 671 $crate::IResult::Done(i2, o2) => $crate::IResult::Done(i2, o2) 672 } 673 } 674 } 675 ); 676 677 ($i:expr, $f:expr) => ( 678 length_data!($i, call!($f)); 679 ); 680 ); 681 682 /// `length_value!(I -> IResult<I, nb>, I -> IResult<I,O>) => I -> IResult<I, Vec<O>>` 683 /// gets a number from the first parser, takes a subslice of the input of that size, 684 /// then applies the second parser on that subslice. If the second parser returns 685 /// `Incomplete`, `length_value` will return an error 686 #[macro_export] 687 macro_rules! length_value( 688 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 689 { 690 match $submac!($i, $($args)*) { 691 $crate::IResult::Error(e) => $crate::IResult::Error(e), 692 $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), 693 $crate::IResult::Done(i, o) => { 694 match take!(i, o as usize) { 695 $crate::IResult::Error(e) => $crate::IResult::Error(e), 696 $crate::IResult::Incomplete($crate::Needed::Unknown) => $crate::IResult::Incomplete($crate::Needed::Unknown), 697 $crate::IResult::Incomplete($crate::Needed::Size(n)) => { 698 let (size,overflowed) = n.overflowing_add( 699 $crate::InputLength::input_len(&($i)) - $crate::InputLength::input_len(&i) 700 ); 701 match overflowed { 702 true => $crate::IResult::Incomplete($crate::Needed::Unknown), 703 false => $crate::IResult::Incomplete($crate::Needed::Size(size)), 704 } 705 }, 706 $crate::IResult::Done(i2, o2) => { 707 match complete!(o2, $submac2!($($args2)*)) { 708 $crate::IResult::Error(e) => $crate::IResult::Error(e), 709 $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), 710 $crate::IResult::Done(_, o3) => $crate::IResult::Done(i2, o3) 711 } 712 } 713 } 714 } 715 } 716 } 717 ); 718 719 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 720 length_value!($i, $submac!($($args)*), call!($g)); 721 ); 722 723 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 724 length_value!($i, call!($f), $submac!($($args)*)); 725 ); 726 727 ($i:expr, $f:expr, $g:expr) => ( 728 length_value!($i, call!($f), call!($g)); 729 ); 730 ); 731 732 /// `fold_many0!(I -> IResult<I,O>, R, Fn(R, O) -> R) => I -> IResult<I, R>` 733 /// Applies the parser 0 or more times and folds the list of return values 734 /// 735 /// the embedded parser may return Incomplete 736 /// 737 /// ``` 738 /// # #[macro_use] extern crate nom; 739 /// # use nom::IResult::Done; 740 /// # fn main() { 741 /// named!(multi<&[u8], Vec<&[u8]> >, 742 /// fold_many0!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { 743 /// acc.push(item); 744 /// acc 745 /// })); 746 /// 747 /// let a = b"abcdabcdefgh"; 748 /// let b = b"azerty"; 749 /// 750 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 751 /// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); 752 /// assert_eq!(multi(&b[..]), Done(&b"azerty"[..], Vec::new())); 753 /// # } 754 /// ``` 755 /// 0 or more 756 #[macro_export] 757 macro_rules! fold_many0( 758 ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $f:expr) => ( 759 { 760 use $crate::InputLength; 761 let ret; 762 let f = $f; 763 let mut res = $init; 764 let mut input = $i; 765 766 loop { 767 if input.input_len() == 0 { 768 ret = $crate::IResult::Done(input, res); 769 break; 770 } 771 772 match $submac!(input, $($args)*) { 773 $crate::IResult::Error(_) => { 774 ret = $crate::IResult::Done(input, res); 775 break; 776 }, 777 $crate::IResult::Incomplete($crate::Needed::Unknown) => { 778 ret = $crate::IResult::Incomplete($crate::Needed::Unknown); 779 break; 780 }, 781 $crate::IResult::Incomplete($crate::Needed::Size(i)) => { 782 let (size,overflowed) = i.overflowing_add( ($i).input_len() - input.input_len() ); 783 ret = match overflowed { 784 true => $crate::IResult::Incomplete($crate::Needed::Unknown), 785 false => $crate::IResult::Incomplete($crate::Needed::Size(size)), 786 }; 787 break; 788 }, 789 $crate::IResult::Done(i, o) => { 790 // loop trip must always consume (otherwise infinite loops) 791 if i == input { 792 ret = $crate::IResult::Error( 793 error_position!($crate::ErrorKind::Many0,input) 794 ); 795 break; 796 } 797 798 res = f(res, o); 799 input = i; 800 } 801 } 802 } 803 804 ret 805 } 806 ); 807 ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( 808 fold_many0!($i, call!($f), $init, $fold_f); 809 ); 810 ); 811 812 /// `fold_many1!(I -> IResult<I,O>, R, Fn(R, O) -> R) => I -> IResult<I, R>` 813 /// Applies the parser 1 or more times and folds the list of return values 814 /// 815 /// the embedded parser may return Incomplete 816 /// 817 /// ``` 818 /// # #[macro_use] extern crate nom; 819 /// # use nom::IResult::{Done, Error}; 820 /// # #[cfg(feature = "verbose-errors")] 821 /// # use nom::Err::Position; 822 /// # use nom::ErrorKind; 823 /// # fn main() { 824 /// named!(multi<&[u8], Vec<&[u8]> >, 825 /// fold_many1!( tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { 826 /// acc.push(item); 827 /// acc 828 /// })); 829 /// 830 /// let a = b"abcdabcdefgh"; 831 /// let b = b"azerty"; 832 /// 833 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 834 /// assert_eq!(multi(&a[..]), Done(&b"efgh"[..], res)); 835 /// assert_eq!(multi(&b[..]), Error(error_position!(ErrorKind::Many1,&b[..]))); 836 /// # } 837 /// ``` 838 #[macro_export] 839 macro_rules! fold_many1( 840 ($i:expr, $submac:ident!( $($args:tt)* ), $init:expr, $f:expr) => ( 841 { 842 use $crate::InputLength; 843 match $submac!($i, $($args)*) { 844 $crate::IResult::Error(_) => $crate::IResult::Error( 845 error_position!($crate::ErrorKind::Many1,$i) 846 ), 847 $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i), 848 $crate::IResult::Done(i1,o1) => { 849 let acc = $init; 850 let f = $f; 851 if i1.input_len() == 0 { 852 let acc = f(acc, o1); 853 $crate::IResult::Done(i1,acc) 854 } else { 855 let mut acc = f(acc, o1); 856 let mut input = i1; 857 let mut incomplete: ::std::option::Option<$crate::Needed> = 858 ::std::option::Option::None; 859 loop { 860 if input.input_len() == 0 { 861 break; 862 } 863 match $submac!(input, $($args)*) { 864 $crate::IResult::Error(_) => { 865 break; 866 }, 867 $crate::IResult::Incomplete($crate::Needed::Unknown) => { 868 incomplete = ::std::option::Option::Some($crate::Needed::Unknown); 869 break; 870 }, 871 $crate::IResult::Incomplete($crate::Needed::Size(i)) => { 872 let (size,overflowed) = i.overflowing_add( ($i).input_len() - input.input_len() ); 873 incomplete = ::std::option::Option::Some( 874 match overflowed { 875 true => $crate::Needed::Unknown, 876 false => $crate::Needed::Size(size), 877 } 878 ); 879 break; 880 }, 881 $crate::IResult::Done(i, o) => { 882 if i.input_len() == input.input_len() { 883 break; 884 } 885 acc = f(acc, o); 886 input = i; 887 } 888 } 889 } 890 891 match incomplete { 892 ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), 893 ::std::option::Option::None => $crate::IResult::Done(input, acc) 894 } 895 } 896 } 897 } 898 } 899 ); 900 ($i:expr, $f:expr, $init:expr, $fold_f:expr) => ( 901 fold_many1!($i, call!($f), $init, $fold_f); 902 ); 903 ); 904 905 /// `fold_many_m_n!(usize, usize, I -> IResult<I,O>, R, Fn(R, O) -> R) => I -> IResult<I, R>` 906 /// Applies the parser between m and n times (n included) and folds the list of return value 907 /// 908 /// the embedded parser may return Incomplete 909 /// 910 /// ``` 911 /// # #[macro_use] extern crate nom; 912 /// # use nom::IResult::{Done, Error}; 913 /// # #[cfg(feature = "verbose-errors")] 914 /// # use nom::Err::Position; 915 /// # use nom::ErrorKind; 916 /// # fn main() { 917 /// named!(multi<&[u8], Vec<&[u8]> >, 918 /// fold_many_m_n!(2, 4, tag!( "abcd" ), Vec::new(), |mut acc: Vec<_>, item| { 919 /// acc.push(item); 920 /// acc 921 /// })); 922 /// 923 /// let a = b"abcdefgh"; 924 /// let b = b"abcdabcdefgh"; 925 /// let c = b"abcdabcdabcdabcdabcdefgh"; 926 /// 927 /// assert_eq!(multi(&a[..]),Error(error_position!(ErrorKind::ManyMN,&a[..]))); 928 /// let res = vec![&b"abcd"[..], &b"abcd"[..]]; 929 /// assert_eq!(multi(&b[..]), Done(&b"efgh"[..], res)); 930 /// let res2 = vec![&b"abcd"[..], &b"abcd"[..], &b"abcd"[..], &b"abcd"[..]]; 931 /// assert_eq!(multi(&c[..]), Done(&b"abcdefgh"[..], res2)); 932 /// # } 933 /// ``` 934 #[macro_export] 935 macro_rules! fold_many_m_n( 936 ($i:expr, $m:expr, $n: expr, $submac:ident!( $($args:tt)* ), $init:expr, $f:expr) => ( 937 { 938 use $crate::InputLength; 939 let mut acc = $init; 940 let f = $f; 941 let mut input = $i; 942 let mut count: usize = 0; 943 let mut err = false; 944 let mut incomplete: ::std::option::Option<$crate::Needed> = ::std::option::Option::None; 945 loop { 946 if count == $n { break } 947 match $submac!(input, $($args)*) { 948 $crate::IResult::Done(i, o) => { 949 // do not allow parsers that do not consume input (causes infinite loops) 950 if i.input_len() == input.input_len() { 951 break; 952 } 953 acc = f(acc, o); 954 input = i; 955 count += 1; 956 } 957 $crate::IResult::Error(_) => { 958 err = true; 959 break; 960 }, 961 $crate::IResult::Incomplete($crate::Needed::Unknown) => { 962 incomplete = ::std::option::Option::Some($crate::Needed::Unknown); 963 break; 964 }, 965 $crate::IResult::Incomplete($crate::Needed::Size(i)) => { 966 let (size,overflowed) = i.overflowing_add( ($i).input_len() - input.input_len() ); 967 incomplete = ::std::option::Option::Some( 968 match overflowed { 969 true => $crate::Needed::Unknown, 970 false => $crate::Needed::Size(size), 971 } 972 ); 973 break; 974 }, 975 } 976 if input.input_len() == 0 { 977 break; 978 } 979 } 980 981 if count < $m { 982 if err { 983 $crate::IResult::Error(error_position!($crate::ErrorKind::ManyMN,$i)) 984 } else { 985 match incomplete { 986 ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), 987 ::std::option::Option::None => $crate::IResult::Incomplete($crate::Needed::Unknown) 988 } 989 } 990 } else { 991 match incomplete { 992 ::std::option::Option::Some(i) => $crate::IResult::Incomplete(i), 993 ::std::option::Option::None => $crate::IResult::Done(input, acc) 994 } 995 } 996 } 997 ); 998 ($i:expr, $m:expr, $n: expr, $f:expr, $init:expr, $fold_f:expr) => ( 999 fold_many_m_n!($i, $m, $n, call!($f), $init, $fold_f); 1000 ); 1001 ); 1002 1003 #[cfg(test)] 1004 mod tests { 1005 use internal::{Needed,IResult}; 1006 1007 use internal::IResult::*; 1008 use util::ErrorKind; 1009 use nom::{be_u8,be_u16,le_u16,digit}; 1010 use std::str::{self,FromStr}; 1011 1012 // reproduce the tag and take macros, because of module import order 1013 macro_rules! tag ( 1014 ($i:expr, $inp: expr) => ( 1015 { 1016 #[inline(always)] 1017 fn as_bytes<T: $crate::AsBytes>(b: &T) -> &[u8] { 1018 b.as_bytes() 1019 } 1020 1021 let expected = $inp; 1022 let bytes = as_bytes(&expected); 1023 1024 tag_bytes!($i,bytes) 1025 } 1026 ); 1027 ); 1028 1029 macro_rules! tag_bytes ( 1030 ($i:expr, $bytes: expr) => ( 1031 { 1032 use std::cmp::min; 1033 let len = $i.len(); 1034 let blen = $bytes.len(); 1035 let m = min(len, blen); 1036 let reduced = &$i[..m]; 1037 let b = &$bytes[..m]; 1038 1039 let res: $crate::IResult<_,_> = if reduced != b { 1040 $crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i)) 1041 } else if m < blen { 1042 $crate::IResult::Incomplete($crate::Needed::Size(blen)) 1043 } else { 1044 $crate::IResult::Done(&$i[blen..], reduced) 1045 }; 1046 res 1047 } 1048 ); 1049 ); 1050 1051 macro_rules! take( 1052 ($i:expr, $count:expr) => ( 1053 { 1054 let cnt = $count as usize; 1055 let res:$crate::IResult<&[u8],&[u8]> = if $i.len() < cnt { 1056 $crate::IResult::Incomplete($crate::Needed::Size(cnt)) 1057 } else { 1058 $crate::IResult::Done(&$i[cnt..],&$i[0..cnt]) 1059 }; 1060 res 1061 } 1062 ) 1063 ); 1064 1065 #[test] separated_list()1066 fn separated_list() { 1067 named!(multi<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!("abcd"))); 1068 named!(multi_empty<&[u8],Vec<&[u8]> >, separated_list!(tag!(","), tag!(""))); 1069 1070 let a = &b"abcdef"[..]; 1071 let b = &b"abcd,abcdef"[..]; 1072 let c = &b"azerty"[..]; 1073 let d = &b",,abc"[..]; 1074 let e = &b"abcd,abcd,ef"[..]; 1075 1076 let res1 = vec![&b"abcd"[..]]; 1077 assert_eq!(multi(a), Done(&b"ef"[..], res1)); 1078 let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; 1079 assert_eq!(multi(b), Done(&b"ef"[..], res2)); 1080 assert_eq!(multi(c), Done(&b"azerty"[..], Vec::new())); 1081 assert_eq!(multi_empty(d), Error(error_position!(ErrorKind::SeparatedList, d))); 1082 //let res3 = vec![&b""[..], &b""[..], &b""[..]]; 1083 //assert_eq!(multi_empty(d), Done(&b"abc"[..], res3)); 1084 let res4 = vec![&b"abcd"[..], &b"abcd"[..]]; 1085 assert_eq!(multi(e), Done(&b",ef"[..], res4)); 1086 } 1087 1088 #[test] separated_nonempty_list()1089 fn separated_nonempty_list() { 1090 named!(multi<&[u8],Vec<&[u8]> >, separated_nonempty_list!(tag!(","), tag!("abcd"))); 1091 1092 let a = &b"abcdef"[..]; 1093 let b = &b"abcd,abcdef"[..]; 1094 let c = &b"azerty"[..]; 1095 let d = &b"abcd,abcd,ef"[..]; 1096 1097 let res1 = vec![&b"abcd"[..]]; 1098 assert_eq!(multi(a), Done(&b"ef"[..], res1)); 1099 let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; 1100 assert_eq!(multi(b), Done(&b"ef"[..], res2)); 1101 assert_eq!(multi(c), Error(error_position!(ErrorKind::Tag,c))); 1102 let res3 = vec![&b"abcd"[..], &b"abcd"[..]]; 1103 assert_eq!(multi(d), Done(&b",ef"[..], res3)); 1104 } 1105 1106 #[test] many0()1107 fn many0() { 1108 named!( tag_abcd, tag!("abcd") ); 1109 named!( tag_empty, tag!("") ); 1110 named!( multi<&[u8],Vec<&[u8]> >, many0!(tag_abcd) ); 1111 named!( multi_empty<&[u8],Vec<&[u8]> >, many0!(tag_empty) ); 1112 1113 assert_eq!(multi(&b"abcdef"[..]), Done(&b"ef"[..], vec![&b"abcd"[..]])); 1114 assert_eq!(multi(&b"abcdabcdefgh"[..]), Done(&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]])); 1115 assert_eq!(multi(&b"azerty"[..]), Done(&b"azerty"[..], Vec::new())); 1116 assert_eq!(multi(&b"abcdab"[..]), Incomplete(Needed::Size(8))); 1117 assert_eq!(multi(&b"abcd"[..]), Done(&b""[..], vec![&b"abcd"[..]])); 1118 assert_eq!(multi(&b""[..]), Done(&b""[..], Vec::new())); 1119 assert_eq!(multi_empty(&b"abcdef"[..]), Error(error_position!(ErrorKind::Many0, &b"abcdef"[..]))); 1120 } 1121 1122 #[cfg(feature = "nightly")] 1123 use test::Bencher; 1124 1125 #[cfg(feature = "nightly")] 1126 #[bench] many0_bench(b: &mut Bencher)1127 fn many0_bench(b: &mut Bencher) { 1128 named!(multi<&[u8],Vec<&[u8]> >, many0!(tag!("abcd"))); 1129 b.iter(|| { 1130 multi(&b"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"[..]) 1131 }); 1132 } 1133 1134 #[test] many1()1135 fn many1() { 1136 named!(multi<&[u8],Vec<&[u8]> >, many1!(tag!("abcd"))); 1137 1138 let a = &b"abcdef"[..]; 1139 let b = &b"abcdabcdefgh"[..]; 1140 let c = &b"azerty"[..]; 1141 let d = &b"abcdab"[..]; 1142 1143 let res1 = vec![&b"abcd"[..]]; 1144 assert_eq!(multi(a), Done(&b"ef"[..], res1)); 1145 let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; 1146 assert_eq!(multi(b), Done(&b"efgh"[..], res2)); 1147 assert_eq!(multi(c), Error(error_position!(ErrorKind::Many1,c))); 1148 assert_eq!(multi(d), Incomplete(Needed::Size(8))); 1149 } 1150 1151 #[test] many_till()1152 fn many_till() { 1153 named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( tag!( "abcd" ), tag!( "efgh" ) ) ); 1154 1155 let a = b"abcdabcdefghabcd"; 1156 let b = b"efghabcd"; 1157 let c = b"azerty"; 1158 1159 let res_a = (vec![&b"abcd"[..], &b"abcd"[..]], &b"efgh"[..]); 1160 let res_b: (Vec<&[u8]>, &[u8]) = (Vec::new(), &b"efgh"[..]); 1161 assert_eq!(multi(&a[..]), Done(&b"abcd"[..], res_a)); 1162 assert_eq!(multi(&b[..]), Done(&b"abcd"[..], res_b)); 1163 assert_eq!(multi(&c[..]), Error(error_position!(ErrorKind::ManyTill,&c[..]))); 1164 } 1165 1166 #[test] infinite_many()1167 fn infinite_many() { 1168 fn tst(input: &[u8]) -> IResult<&[u8], &[u8]> { 1169 println!("input: {:?}", input); 1170 Error(error_position!(ErrorKind::Custom(0),input)) 1171 } 1172 1173 // should not go into an infinite loop 1174 named!(multi0<&[u8],Vec<&[u8]> >, many0!(tst)); 1175 let a = &b"abcdef"[..]; 1176 assert_eq!(multi0(a), Done(a, Vec::new())); 1177 1178 named!(multi1<&[u8],Vec<&[u8]> >, many1!(tst)); 1179 let a = &b"abcdef"[..]; 1180 assert_eq!(multi1(a), Error(error_position!(ErrorKind::Many1,a))); 1181 } 1182 1183 #[test] many_m_n()1184 fn many_m_n() { 1185 named!(multi<&[u8],Vec<&[u8]> >, many_m_n!(2, 4, tag!("Abcd"))); 1186 1187 let a = &b"Abcdef"[..]; 1188 let b = &b"AbcdAbcdefgh"[..]; 1189 let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; 1190 let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; 1191 let e = &b"AbcdAb"[..]; 1192 1193 assert_eq!(multi(a), Error(error_position!(ErrorKind::ManyMN,a))); 1194 let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; 1195 assert_eq!(multi(b), Done(&b"efgh"[..], res1)); 1196 let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; 1197 assert_eq!(multi(c), Done(&b"efgh"[..], res2)); 1198 let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; 1199 assert_eq!(multi(d), Done(&b"Abcdefgh"[..], res3)); 1200 assert_eq!(multi(e), Incomplete(Needed::Size(8))); 1201 } 1202 1203 #[test] count()1204 fn count() { 1205 const TIMES: usize = 2; 1206 named!( tag_abc, tag!("abc") ); 1207 named!( cnt_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); 1208 1209 assert_eq!(cnt_2(&b"abcabcabcdef"[..]), Done(&b"abcdef"[..], vec![&b"abc"[..], &b"abc"[..]])); 1210 assert_eq!(cnt_2(&b"ab"[..]), Incomplete(Needed::Size(3))); 1211 assert_eq!(cnt_2(&b"abcab"[..]), Incomplete(Needed::Size(6))); 1212 assert_eq!(cnt_2(&b"xxx"[..]), Error(error_position!(ErrorKind::Count, &b"xxx"[..]))); 1213 assert_eq!(cnt_2(&b"xxxabcabcdef"[..]), Error(error_position!(ErrorKind::Count, &b"xxxabcabcdef"[..]))); 1214 assert_eq!(cnt_2(&b"abcxxxabcdef"[..]), Error(error_position!(ErrorKind::Count, &b"abcxxxabcdef"[..]))); 1215 } 1216 1217 #[test] count_zero()1218 fn count_zero() { 1219 const TIMES: usize = 0; 1220 named!( tag_abc, tag!("abc") ); 1221 named!( counter_2<&[u8], Vec<&[u8]> >, count!(tag_abc, TIMES ) ); 1222 1223 let done = &b"abcabcabcdef"[..]; 1224 let parsed_done = Vec::new(); 1225 let rest = done; 1226 let incomplete_1 = &b"ab"[..]; 1227 let parsed_incompl_1 = Vec::new(); 1228 let incomplete_2 = &b"abcab"[..]; 1229 let parsed_incompl_2 = Vec::new(); 1230 let error = &b"xxx"[..]; 1231 let error_remain = &b"xxx"[..]; 1232 let parsed_err = Vec::new(); 1233 let error_1 = &b"xxxabcabcdef"[..]; 1234 let parsed_err_1 = Vec::new(); 1235 let error_1_remain = &b"xxxabcabcdef"[..]; 1236 let error_2 = &b"abcxxxabcdef"[..]; 1237 let parsed_err_2 = Vec::new(); 1238 let error_2_remain = &b"abcxxxabcdef"[..]; 1239 1240 assert_eq!(counter_2(done), Done(rest, parsed_done)); 1241 assert_eq!(counter_2(incomplete_1), Done(incomplete_1, parsed_incompl_1)); 1242 assert_eq!(counter_2(incomplete_2), Done(incomplete_2, parsed_incompl_2)); 1243 assert_eq!(counter_2(error), Done(error_remain, parsed_err)); 1244 assert_eq!(counter_2(error_1), Done(error_1_remain, parsed_err_1)); 1245 assert_eq!(counter_2(error_2), Done(error_2_remain, parsed_err_2)); 1246 } 1247 1248 #[test] count_fixed()1249 fn count_fixed() { 1250 const TIMES: usize = 2; 1251 named!( tag_abc, tag!("abc") ); 1252 named!( cnt_2<&[u8], [&[u8]; TIMES] >, count_fixed!(&[u8], tag_abc, TIMES ) ); 1253 1254 assert_eq!(cnt_2(&b"abcabcabcdef"[..]), Done(&b"abcdef"[..], [&b"abc"[..], &b"abc"[..]])); 1255 assert_eq!(cnt_2(&b"ab"[..]), Incomplete(Needed::Size(3))); 1256 assert_eq!(cnt_2(&b"abcab"[..]), Incomplete(Needed::Size(6))); 1257 assert_eq!(cnt_2(&b"xxx"[..]), Error(error_position!(ErrorKind::Count, &b"xxx"[..]))); 1258 assert_eq!(cnt_2(&b"xxxabcabcdef"[..]), Error(error_position!(ErrorKind::Count, &b"xxxabcabcdef"[..]))); 1259 assert_eq!(cnt_2(&b"abcxxxabcdef"[..]), Error(error_position!(ErrorKind::Count, &b"abcxxxabcdef"[..]))); 1260 } 1261 1262 #[allow(dead_code)] compile_count_fixed(input: &[u8]) -> IResult<&[u8], ()>1263 pub fn compile_count_fixed(input: &[u8]) -> IResult<&[u8], ()> { 1264 do_parse!(input, 1265 tag!("abcd") >> 1266 count_fixed!( u16, le_u16, 4 ) >> 1267 eof!() >> 1268 () 1269 ) 1270 } 1271 1272 #[allow(unused_variables)] 1273 #[test] count_fixed_no_type()1274 fn count_fixed_no_type() { 1275 const TIMES: usize = 2; 1276 named!( tag_abc, tag!("abc") ); 1277 named!( counter_2<&[u8], [&[u8]; TIMES], () >, count_fixed!(&[u8], tag_abc, TIMES ) ); 1278 1279 let done = &b"abcabcabcdef"[..]; 1280 let parsed_main = [&b"abc"[..], &b"abc"[..]]; 1281 let rest = &b"abcdef"[..]; 1282 let incomplete_1 = &b"ab"[..]; 1283 let incomplete_2 = &b"abcab"[..]; 1284 let error = &b"xxx"[..]; 1285 let error_1 = &b"xxxabcabcdef"[..]; 1286 let error_1_remain = &b"xxxabcabcdef"[..]; 1287 let error_2 = &b"abcxxxabcdef"[..]; 1288 let error_2_remain = &b"abcxxxabcdef"[..]; 1289 1290 assert_eq!(counter_2(done), Done(rest, parsed_main)); 1291 assert_eq!(counter_2(incomplete_1), Incomplete(Needed::Size(3))); 1292 assert_eq!(counter_2(incomplete_2), Incomplete(Needed::Size(6))); 1293 assert_eq!(counter_2(error), Error(error_position!(ErrorKind::Count, error))); 1294 assert_eq!(counter_2(error_1), Error(error_position!(ErrorKind::Count, error_1_remain))); 1295 assert_eq!(counter_2(error_2), Error(error_position!(ErrorKind::Count, error_2_remain))); 1296 } 1297 1298 named!(pub number<u32>, map_res!( 1299 map_res!( 1300 digit, 1301 str::from_utf8 1302 ), 1303 FromStr::from_str 1304 )); 1305 1306 #[test] length_count()1307 fn length_count() { 1308 named!(tag_abc, tag!(&b"abc"[..]) ); 1309 named!( cnt<&[u8], Vec<&[u8]> >, length_count!(number, tag_abc) ); 1310 1311 assert_eq!(cnt(&b"2abcabcabcdef"[..]), Done(&b"abcdef"[..], vec![&b"abc"[..], &b"abc"[..]])); 1312 assert_eq!(cnt(&b"2ab"[..]), Incomplete(Needed::Size(4))); 1313 assert_eq!(cnt(&b"3abcab"[..]), Incomplete(Needed::Size(7))); 1314 assert_eq!(cnt(&b"xxx"[..]), Error(error_position!(ErrorKind::Digit, &b"xxx"[..]))); 1315 assert_eq!(cnt(&b"2abcxxx"[..]), Error(error_position!(ErrorKind::Count, &b"xxx"[..]))); 1316 } 1317 1318 #[test] length_data()1319 fn length_data() { 1320 named!( take<&[u8], &[u8]>, length_data!(number) ); 1321 1322 assert_eq!(take(&b"6abcabcabcdef"[..]), Done(&b"abcdef"[..], &b"abcabc"[..])); 1323 assert_eq!(take(&b"3ab"[..]), Incomplete(Needed::Size(4))); 1324 assert_eq!(take(&b"xxx"[..]), Error(error_position!(ErrorKind::Digit, &b"xxx"[..]))); 1325 assert_eq!(take(&b"2abcxxx"[..]), Done(&b"cxxx"[..], &b"ab"[..])); 1326 } 1327 1328 #[test] length_value_test()1329 fn length_value_test() { 1330 named!(length_value_1<&[u8], u16 >, length_value!(be_u8, be_u16)); 1331 named!(length_value_2<&[u8], (u8, u8) >, length_value!(be_u8, tuple!(be_u8, be_u8))); 1332 1333 let i1 = vec![0, 5, 6]; 1334 assert_eq!(length_value_1(&i1), IResult::Error(error_position!(ErrorKind::Complete, &b""[..]))); 1335 assert_eq!(length_value_2(&i1), IResult::Error(error_position!(ErrorKind::Complete, &b""[..]))); 1336 1337 let i2 = vec![1, 5, 6, 3]; 1338 assert_eq!(length_value_1(&i2), IResult::Error(error_position!(ErrorKind::Complete, &i2[1..]))); 1339 assert_eq!(length_value_2(&i2), IResult::Error(error_position!(ErrorKind::Complete, &i2[1..]))); 1340 1341 let i3 = vec![2, 5, 6, 3, 4, 5, 7]; 1342 assert_eq!(length_value_1(&i3), IResult::Done(&i3[3..], 1286)); 1343 assert_eq!(length_value_2(&i3), IResult::Done(&i3[3..], (5, 6))); 1344 1345 let i4 = vec![3, 5, 6, 3, 4, 5]; 1346 assert_eq!(length_value_1(&i4), IResult::Done(&i4[4..], 1286)); 1347 assert_eq!(length_value_2(&i4), IResult::Done(&i4[4..], (5, 6))); 1348 } 1349 1350 #[test] fold_many0()1351 fn fold_many0() { 1352 fn fold_into_vec<T>(mut acc: Vec<T>, item: T) -> Vec<T> { 1353 acc.push(item); 1354 acc 1355 }; 1356 named!( tag_abcd, tag!("abcd") ); 1357 named!( tag_empty, tag!("") ); 1358 named!( multi<&[u8],Vec<&[u8]> >, fold_many0!(tag_abcd, Vec::new(), fold_into_vec) ); 1359 named!( multi_empty<&[u8],Vec<&[u8]> >, fold_many0!(tag_empty, Vec::new(), fold_into_vec) ); 1360 1361 assert_eq!(multi(&b"abcdef"[..]), Done(&b"ef"[..], vec![&b"abcd"[..]])); 1362 assert_eq!(multi(&b"abcdabcdefgh"[..]), Done(&b"efgh"[..], vec![&b"abcd"[..], &b"abcd"[..]])); 1363 assert_eq!(multi(&b"azerty"[..]), Done(&b"azerty"[..], Vec::new())); 1364 assert_eq!(multi(&b"abcdab"[..]), Incomplete(Needed::Size(8))); 1365 assert_eq!(multi(&b"abcd"[..]), Done(&b""[..], vec![&b"abcd"[..]])); 1366 assert_eq!(multi(&b""[..]), Done(&b""[..], Vec::new())); 1367 assert_eq!(multi_empty(&b"abcdef"[..]), Error(error_position!(ErrorKind::Many0, &b"abcdef"[..]))); 1368 } 1369 1370 #[test] fold_many1()1371 fn fold_many1() { 1372 fn fold_into_vec<T>(mut acc: Vec<T>, item: T) -> Vec<T> { 1373 acc.push(item); 1374 acc 1375 }; 1376 named!(multi<&[u8],Vec<&[u8]> >, fold_many1!(tag!("abcd"), Vec::new(), fold_into_vec)); 1377 1378 let a = &b"abcdef"[..]; 1379 let b = &b"abcdabcdefgh"[..]; 1380 let c = &b"azerty"[..]; 1381 let d = &b"abcdab"[..]; 1382 1383 let res1 = vec![&b"abcd"[..]]; 1384 assert_eq!(multi(a), Done(&b"ef"[..], res1)); 1385 let res2 = vec![&b"abcd"[..], &b"abcd"[..]]; 1386 assert_eq!(multi(b), Done(&b"efgh"[..], res2)); 1387 assert_eq!(multi(c), Error(error_position!(ErrorKind::Many1,c))); 1388 assert_eq!(multi(d), Incomplete(Needed::Size(8))); 1389 } 1390 1391 #[test] fold_many_m_n()1392 fn fold_many_m_n() { 1393 fn fold_into_vec<T>(mut acc: Vec<T>, item: T) -> Vec<T> { 1394 acc.push(item); 1395 acc 1396 }; 1397 named!(multi<&[u8],Vec<&[u8]> >, fold_many_m_n!(2, 4, tag!("Abcd"), Vec::new(), fold_into_vec)); 1398 1399 let a = &b"Abcdef"[..]; 1400 let b = &b"AbcdAbcdefgh"[..]; 1401 let c = &b"AbcdAbcdAbcdAbcdefgh"[..]; 1402 let d = &b"AbcdAbcdAbcdAbcdAbcdefgh"[..]; 1403 let e = &b"AbcdAb"[..]; 1404 1405 assert_eq!(multi(a), Error(error_position!(ErrorKind::ManyMN,a))); 1406 let res1 = vec![&b"Abcd"[..], &b"Abcd"[..]]; 1407 assert_eq!(multi(b), Done(&b"efgh"[..], res1)); 1408 let res2 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; 1409 assert_eq!(multi(c), Done(&b"efgh"[..], res2)); 1410 let res3 = vec![&b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..], &b"Abcd"[..]]; 1411 assert_eq!(multi(d), Done(&b"Abcdefgh"[..], res3)); 1412 assert_eq!(multi(e), Incomplete(Needed::Size(8))); 1413 } 1414 1415 } 1416