1 /// `tuple!(I->IResult<I,A>, I->IResult<I,B>, ... I->IResult<I,X>) => I -> IResult<I, (A, B, ..., X)>` 2 /// chains parsers and assemble the sub results in a tuple. 3 /// 4 /// The input type `I` must implement `nom::InputLength`. 5 /// 6 /// This combinator will count how much data is consumed by every child parser 7 /// and take it into account if there is not enough data. 8 /// 9 /// ``` 10 /// # #[macro_use] extern crate nom; 11 /// # use nom::error::ErrorKind; 12 /// # use nom::number::streaming::be_u16; 13 /// // the return type depends of the children parsers 14 /// named!(parser<&[u8], (u16, &[u8], &[u8]) >, 15 /// tuple!( 16 /// be_u16 , 17 /// take!(3), 18 /// tag!("fg") 19 /// ) 20 /// ); 21 /// 22 /// # fn main() { 23 /// assert_eq!( 24 /// parser(&b"abcdefgh"[..]), 25 /// Ok(( 26 /// &b"h"[..], 27 /// (0x6162u16, &b"cde"[..], &b"fg"[..]) 28 /// )) 29 /// ); 30 /// # } 31 /// ``` 32 #[macro_export(local_inner_macros)] 33 macro_rules! tuple ( 34 ($i:expr, $($rest:tt)*) => ( 35 { 36 tuple_parser!($i, (), $($rest)*) 37 } 38 ); 39 ); 40 41 /// Internal parser, do not use directly. 42 #[doc(hidden)] 43 #[macro_export(local_inner_macros)] 44 macro_rules! tuple_parser ( 45 ($i:expr, ($($parsed:tt),*), $e:path, $($rest:tt)*) => ( 46 tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*); 47 ); 48 ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( 49 { 50 let i_ = $i.clone(); 51 52 ( $submac!(i_, $($args)*) ).and_then(|(i,o)| { 53 let i_ = i.clone(); 54 tuple_parser!(i_, (o), $($rest)*) 55 }) 56 } 57 ); 58 ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( 59 { 60 let i_ = $i.clone(); 61 62 ( $submac!(i_, $($args)*) ).and_then(|(i,o)| { 63 let i_ = i.clone(); 64 tuple_parser!(i_, ($($parsed)* , o), $($rest)*) 65 }) 66 } 67 ); 68 ($i:expr, ($($parsed:tt),*), $e:path) => ( 69 tuple_parser!($i, ($($parsed),*), call!($e)); 70 ); 71 ($i:expr, (), $submac:ident!( $($args:tt)* )) => ( 72 { 73 let i_ = $i.clone(); 74 ( $submac!(i_, $($args)*) ).map(|(i,o)| (i, (o))) 75 } 76 ); 77 ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => ( 78 { 79 let i_ = $i.clone(); 80 ( $submac!(i_, $($args)*) ).map(|(i,o)| (i, ($($parsed),* , o))) 81 } 82 ); 83 ($i:expr, ($($parsed:expr),*)) => ( 84 { 85 $crate::lib::std::result::Result::Ok(($i, ($($parsed),*))) 86 } 87 ); 88 ); 89 90 /// `pair!(I -> IResult<I,O>, I -> IResult<I,P>) => I -> IResult<I, (O,P)>` 91 /// `pair` returns a tuple of the results of its two child parsers of both succeed. 92 /// 93 /// ``` 94 /// # #[macro_use] extern crate nom; 95 /// # use nom::Err; 96 /// # use nom::error::{Error, ErrorKind}; 97 /// # use nom::character::complete::{alpha1, digit1}; 98 /// named!(parser<&str, (&str, &str)>, pair!(alpha1, digit1)); 99 /// 100 /// # fn main() { 101 /// assert_eq!(parser("abc123"), Ok(("", ("abc", "123")))); 102 /// assert_eq!(parser("123abc"), Err(Err::Error(Error::new("123abc", ErrorKind::Alpha)))); 103 /// assert_eq!(parser("abc;123"), Err(Err::Error(Error::new(";123", ErrorKind::Digit)))); 104 /// # } 105 /// ``` 106 #[macro_export(local_inner_macros)] 107 macro_rules! pair( 108 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 109 pair!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) 110 ); 111 112 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 113 pair!($i, |i| $submac!(i, $($args)*), $g); 114 ); 115 116 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 117 pair!($i, $f, |i| $submac!(i, $($args)*)); 118 ); 119 120 ($i:expr, $f:expr, $g:expr) => ( 121 $crate::sequence::pairc($i, $f, $g) 122 ); 123 ); 124 125 /// `separated_pair!(I -> IResult<I,O>, I -> IResult<I, T>, I -> IResult<I,P>) => I -> IResult<I, (O,P)>` 126 /// `separated_pair(X,sep,Y)` returns a tuple of its first and third child parsers 127 /// if all 3 succeed. 128 /// 129 /// ``` 130 /// # #[macro_use] extern crate nom; 131 /// # use nom::Err; 132 /// # use nom::error::{Error, ErrorKind}; 133 /// # use nom::character::complete::{alpha1, digit1}; 134 /// named!(parser<&str, (&str, &str)>, separated_pair!(alpha1, char!(','), digit1)); 135 /// 136 /// # fn main() { 137 /// assert_eq!(parser("abc,123"), Ok(("", ("abc", "123")))); 138 /// assert_eq!(parser("123,abc"), Err(Err::Error(Error::new("123,abc", ErrorKind::Alpha)))); 139 /// assert_eq!(parser("abc;123"), Err(Err::Error(Error::new(";123", ErrorKind::Char)))); 140 /// # } 141 /// ``` 142 #[macro_export(local_inner_macros)] 143 macro_rules! separated_pair( 144 ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( 145 separated_pair!($i, |i| $submac!(i, $($args)*), $($rest)*) 146 ); 147 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( 148 separated_pair!($i, $f, |i| $submac!(i, $($args)*), $($rest)*) 149 ); 150 ($i:expr, $f:expr, $g:expr, $submac:ident!( $($args:tt)* )) => ( 151 separated_pair!($i, $f, $g, |i| $submac!(i, $($args)*)) 152 ); 153 ($i:expr, $f:expr, $g:expr, $h:expr) => ( 154 $crate::sequence::separated_pairc($i, $f, $g, $h) 155 ); 156 ); 157 158 /// `preceded!(I -> IResult<I,T>, I -> IResult<I,O>) => I -> IResult<I, O>` 159 /// `preceded` returns the result of its second parser if both succeed. 160 /// 161 /// ``` 162 /// # #[macro_use] extern crate nom; 163 /// # use nom::Err; 164 /// # use nom::error::{Error, ErrorKind}; 165 /// # use nom::character::complete::{alpha1}; 166 /// named!(parser<&str, &str>, preceded!(char!('-'), alpha1)); 167 /// 168 /// # fn main() { 169 /// assert_eq!(parser("-abc"), Ok(("", "abc"))); 170 /// assert_eq!(parser("abc"), Err(Err::Error(Error::new("abc", ErrorKind::Char)))); 171 /// assert_eq!(parser("-123"), Err(Err::Error(Error::new("123", ErrorKind::Alpha)))); 172 /// # } 173 /// ``` 174 #[macro_export(local_inner_macros)] 175 macro_rules! preceded( 176 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 177 preceded!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) 178 ); 179 180 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 181 preceded!($i, |i| $submac!(i, $($args)*), $g); 182 ); 183 184 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 185 preceded!($i, $f, |i| $submac!(i, $($args)*)); 186 ); 187 188 ($i:expr, $f:expr, $g:expr) => ( 189 $crate::sequence::precededc($i, $f, $g) 190 ); 191 ); 192 193 /// `terminated!(I -> IResult<I,O>, I -> IResult<I,T>) => I -> IResult<I, O>` 194 /// `terminated` returns the result of its first parser if both succeed. 195 /// 196 /// ``` 197 /// # #[macro_use] extern crate nom; 198 /// # use nom::Err; 199 /// # use nom::error::{Error, ErrorKind}; 200 /// # use nom::character::complete::{alpha1}; 201 /// named!(parser<&str, &str>, terminated!(alpha1, char!(';'))); 202 /// 203 /// # fn main() { 204 /// assert_eq!(parser("abc;"), Ok(("", "abc"))); 205 /// assert_eq!(parser("abc,"), Err(Err::Error(Error::new(",", ErrorKind::Char)))); 206 /// assert_eq!(parser("123;"), Err(Err::Error(Error::new("123;", ErrorKind::Alpha)))); 207 /// # } 208 /// ``` 209 #[macro_export(local_inner_macros)] 210 macro_rules! terminated( 211 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 212 terminated!($i, |i| $submac!(i, $($args)*), |i| $submac2!(i, $($args2)*)) 213 ); 214 215 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 216 terminated!($i, |i| $submac!(i, $($args)*), $g); 217 ); 218 219 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 220 terminated!($i, $f, |i| $submac!(i, $($args)*)); 221 ); 222 223 ($i:expr, $f:expr, $g:expr) => ( 224 $crate::sequence::terminatedc($i, $f, $g) 225 ); 226 ); 227 228 /// `delimited!(I -> IResult<I,T>, I -> IResult<I,O>, I -> IResult<I,U>) => I -> IResult<I, O>` 229 /// `delimited(opening, X, closing)` returns X. 230 /// 231 /// ``` 232 /// # #[macro_use] extern crate nom; 233 /// # use nom::character::complete::{alpha1}; 234 /// named!(parens, 235 /// delimited!( 236 /// tag!("("), 237 /// alpha1, 238 /// tag!(")") 239 /// ) 240 /// ); 241 /// 242 /// # fn main() { 243 /// let input = &b"(test)"[..]; 244 /// assert_eq!(parens(input), Ok((&b""[..], &b"test"[..]))); 245 /// # } 246 /// ``` 247 #[macro_export(local_inner_macros)] 248 macro_rules! delimited( 249 ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( 250 delimited!($i, |i| $submac!(i, $($args)*), $($rest)*) 251 ); 252 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ( 253 delimited!($i, $f, |i| $submac!(i, $($args)*), $($rest)*) 254 ); 255 ($i:expr, $f:expr, $g:expr, $submac:ident!( $($args:tt)* )) => ( 256 delimited!($i, $f, $g, |i| $submac!(i, $($args)*)) 257 ); 258 ($i:expr, $f:expr, $g:expr, $h:expr) => ( 259 $crate::sequence::delimitedc($i, $f, $g, $h) 260 ); 261 ); 262 263 /// `do_parse!(I->IResult<I,A> >> I->IResult<I,B> >> ... I->IResult<I,X> , ( O ) ) => I -> IResult<I, O>` 264 /// `do_parse` applies sub parsers in a sequence. 265 /// It can store intermediary results and make them available 266 /// for later parsers. 267 /// 268 /// The input type `I` must implement `nom::InputLength`. 269 /// 270 /// This combinator will count how much data is consumed by every child parser 271 /// and take it into account if there is not enough data. 272 /// 273 /// ``` 274 /// # #[macro_use] extern crate nom; 275 /// # use nom::{Err,Needed}; 276 /// use nom::number::streaming::be_u8; 277 /// 278 /// // this parser implements a common pattern in binary formats, 279 /// // the TAG-LENGTH-VALUE, where you first recognize a specific 280 /// // byte slice, then the next bytes indicate the length of 281 /// // the data, then you take that slice and return it 282 /// // 283 /// // here, we match the tag 42, take the length in the next byte 284 /// // and store it in `length`, then use `take!` with `length` 285 /// // to obtain the subslice that we store in `bytes`, then return 286 /// // `bytes` 287 /// // you can use other macro combinators inside do_parse (like the `tag` 288 /// // one here), or a function (like `be_u8` here), but you cannot use a 289 /// // module path (like `nom::be_u8`) there, because of limitations in macros 290 /// named!(tag_length_value, 291 /// do_parse!( 292 /// tag!( &[ 42u8 ][..] ) >> 293 /// length: be_u8 >> 294 /// bytes: take!(length) >> 295 /// (bytes) 296 /// ) 297 /// ); 298 /// 299 /// # fn main() { 300 /// let a: Vec<u8> = vec!(42, 2, 3, 4, 5); 301 /// let result_a: Vec<u8> = vec!(3, 4); 302 /// let rest_a: Vec<u8> = vec!(5); 303 /// assert_eq!(tag_length_value(&a[..]), Ok((&rest_a[..], &result_a[..]))); 304 /// 305 /// // here, the length is 5, but there are only 3 bytes afterwards (3, 4 and 5), 306 /// // so the parser will tell you that you need 7 bytes: one for the tag, 307 /// // one for the length, then 5 bytes 308 /// let b: Vec<u8> = vec!(42, 5, 3, 4, 5); 309 /// assert_eq!(tag_length_value(&b[..]), Err(Err::Incomplete(Needed::new(2)))); 310 /// # } 311 /// ``` 312 /// 313 /// the result is a tuple, so you can return multiple sub results, like 314 /// this: 315 /// `do_parse!(I->IResult<I,A> >> I->IResult<I,B> >> ... I->IResult<I,X> , ( O, P ) ) => I -> IResult<I, (O,P)>` 316 /// 317 /// ``` 318 /// # #[macro_use] extern crate nom; 319 /// use nom::number::streaming::be_u8; 320 /// named!(tag_length_value<(u8, &[u8])>, 321 /// do_parse!( 322 /// tag!( &[ 42u8 ][..] ) >> 323 /// length: be_u8 >> 324 /// bytes: take!(length) >> 325 /// (length, bytes) 326 /// ) 327 /// ); 328 /// 329 /// # fn main() { 330 /// # } 331 /// ``` 332 /// 333 #[macro_export(local_inner_macros)] 334 macro_rules! do_parse ( 335 (__impl $i:expr, ( $($rest:expr),* )) => ( 336 $crate::lib::std::result::Result::Ok(($i, ( $($rest),* ))) 337 ); 338 339 (__impl $i:expr, $field:ident : $submac:ident!( $($args:tt)* ) ) => ( 340 do_parse!(__impl $i, $submac!( $($args)* )) 341 ); 342 343 (__impl $i:expr, $submac:ident!( $($args:tt)* ) ) => ( 344 nom_compile_error!("do_parse is missing the return value. A do_parse call must end 345 with a return value between parenthesis, as follows: 346 347 do_parse!( 348 a: tag!(\"abcd\") >> 349 b: tag!(\"efgh\") >> 350 351 ( Value { a: a, b: b } ) 352 "); 353 ); 354 355 (__impl $i:expr, $field:ident : $submac:ident!( $($args:tt)* ) ~ $($rest:tt)* ) => ( 356 nom_compile_error!("do_parse uses >> as separator, not ~"); 357 ); 358 (__impl $i:expr, $submac:ident!( $($args:tt)* ) ~ $($rest:tt)* ) => ( 359 nom_compile_error!("do_parse uses >> as separator, not ~"); 360 ); 361 (__impl $i:expr, $field:ident : $e:ident ~ $($rest:tt)*) => ( 362 do_parse!(__impl $i, $field: call!($e) ~ $($rest)*); 363 ); 364 (__impl $i:expr, $e:ident ~ $($rest:tt)*) => ( 365 do_parse!(__impl $i, call!($e) ~ $($rest)*); 366 ); 367 368 (__impl $i:expr, $e:ident >> $($rest:tt)*) => ( 369 do_parse!(__impl $i, call!($e) >> $($rest)*); 370 ); 371 (__impl $i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( 372 { 373 use $crate::lib::std::result::Result::*; 374 375 let i_ = $i.clone(); 376 match $submac!(i_, $($args)*) { 377 Err(e) => Err(e), 378 Ok((i,_)) => { 379 let i_ = i.clone(); 380 do_parse!(__impl i_, $($rest)*) 381 }, 382 } 383 } 384 ); 385 386 (__impl $i:expr, $field:ident : $e:ident >> $($rest:tt)*) => ( 387 do_parse!(__impl $i, $field: call!($e) >> $($rest)*); 388 ); 389 390 (__impl $i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( 391 { 392 use $crate::lib::std::result::Result::*; 393 394 let i_ = $i.clone(); 395 match $submac!(i_, $($args)*) { 396 Err(e) => Err(e), 397 Ok((i,o)) => { 398 let $field = o; 399 let i_ = i.clone(); 400 do_parse!(__impl i_, $($rest)*) 401 }, 402 } 403 } 404 ); 405 406 // ending the chain 407 (__impl $i:expr, $e:ident >> ( $($rest:tt)* )) => ( 408 do_parse!(__impl $i, call!($e) >> ( $($rest)* )); 409 ); 410 411 (__impl $i:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ({ 412 use $crate::lib::std::result::Result::*; 413 414 match $submac!($i, $($args)*) { 415 Err(e) => Err(e), 416 Ok((i,_)) => { 417 do_parse!(__finalize i, $($rest)*) 418 }, 419 } 420 }); 421 422 (__impl $i:expr, $field:ident : $e:ident >> ( $($rest:tt)* )) => ( 423 do_parse!(__impl $i, $field: call!($e) >> ( $($rest)* ) ); 424 ); 425 426 (__impl $i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ({ 427 use $crate::lib::std::result::Result::*; 428 429 match $submac!($i, $($args)*) { 430 Err(e) => Err(e), 431 Ok((i,o)) => { 432 let $field = o; 433 do_parse!(__finalize i, $($rest)*) 434 }, 435 } 436 }); 437 438 (__finalize $i:expr, ( $o: expr )) => ({ 439 use $crate::lib::std::result::Result::Ok; 440 Ok(($i, $o)) 441 }); 442 443 (__finalize $i:expr, ( $($rest:tt)* )) => ({ 444 use $crate::lib::std::result::Result::Ok; 445 Ok(($i, ( $($rest)* ))) 446 }); 447 448 ($i:expr, $($rest:tt)*) => ( 449 { 450 do_parse!(__impl $i, $($rest)*) 451 } 452 ); 453 ($submac:ident!( $($args:tt)* ) >> $($rest:tt)* ) => ( 454 nom_compile_error!("if you are using do_parse outside of a named! macro, you must 455 pass the input data as first argument, like this: 456 457 let res = do_parse!(input, 458 a: tag!(\"abcd\") >> 459 b: tag!(\"efgh\") >> 460 ( Value { a: a, b: b } ) 461 );"); 462 ); 463 ($e:ident! >> $($rest:tt)* ) => ( 464 do_parse!( call!($e) >> $($rest)*); 465 ); 466 ); 467 468 #[doc(hidden)] 469 #[macro_export] 470 macro_rules! nom_compile_error ( 471 (( $($args:tt)* )) => ( compile_error!($($args)*) ); 472 ); 473 474 #[cfg(test)] 475 mod tests { 476 use crate::error::ErrorKind; 477 use crate::internal::{Err, IResult, Needed}; 478 use crate::number::streaming::be_u16; 479 480 // reproduce the tag and take macros, because of module import order 481 macro_rules! tag ( 482 ($i:expr, $inp: expr) => ( 483 { 484 #[inline(always)] 485 fn as_bytes<T: $crate::AsBytes>(b: &T) -> &[u8] { 486 b.as_bytes() 487 } 488 489 let expected = $inp; 490 let bytes = as_bytes(&expected); 491 492 tag_bytes!($i,bytes) 493 } 494 ); 495 ); 496 497 macro_rules! tag_bytes ( 498 ($i:expr, $bytes: expr) => ( 499 { 500 use $crate::lib::std::cmp::min; 501 502 let len = $i.len(); 503 let blen = $bytes.len(); 504 let m = min(len, blen); 505 let reduced = &$i[..m]; 506 let b = &$bytes[..m]; 507 508 let res: IResult<_,_,_> = if reduced != b { 509 Err($crate::Err::Error(error_position!($i, $crate::error::ErrorKind::Tag))) 510 } else if m < blen { 511 Err($crate::Err::Incomplete(Needed::new(blen))) 512 } else { 513 Ok((&$i[blen..], reduced)) 514 }; 515 res 516 } 517 ); 518 ); 519 520 macro_rules! take ( 521 ($i:expr, $count:expr) => ( 522 { 523 let cnt = $count as usize; 524 let res:IResult<&[u8],&[u8],_> = if $i.len() < cnt { 525 Err($crate::Err::Incomplete(Needed::new(cnt))) 526 } else { 527 Ok((&$i[cnt..],&$i[0..cnt])) 528 }; 529 res 530 } 531 ); 532 ); 533 534 #[derive(PartialEq, Eq, Debug)] 535 struct B { 536 a: u8, 537 b: u8, 538 } 539 540 #[derive(PartialEq, Eq, Debug)] 541 struct C { 542 a: u8, 543 b: Option<u8>, 544 } 545 546 /*FIXME: convert code examples to new error management 547 use util::{add_error_pattern, error_to_list, print_error}; 548 549 #[cfg(feature = "std")] 550 #[rustfmt::skip] 551 fn error_to_string<P: Clone + PartialEq>(e: &Context<P, u32>) -> &'static str { 552 let v: Vec<(P, ErrorKind<u32>)> = error_to_list(e); 553 // do it this way if you can use slice patterns 554 //match &v[..] { 555 // [ErrorKind::Custom(42), ErrorKind::Tag] => "missing `ijkl` tag", 556 // [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] => "missing `mnop` tag after `ijkl`", 557 // _ => "unrecognized error" 558 //} 559 560 let collected: Vec<ErrorKind<u32>> = v.iter().map(|&(_, ref e)| e.clone()).collect(); 561 if &collected[..] == [ErrorKind::Custom(42), ErrorKind::Tag] { 562 "missing `ijkl` tag" 563 } else if &collected[..] == [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] { 564 "missing `mnop` tag after `ijkl`" 565 } else { 566 "unrecognized error" 567 } 568 } 569 570 // do it this way if you can use box patterns 571 //use $crate::lib::std::str; 572 //fn error_to_string(e:Err) -> String 573 // match e { 574 // NodePosition(ErrorKind::Custom(42), i1, box Position(ErrorKind::Tag, i2)) => { 575 // format!("missing `ijkl` tag, found '{}' instead", str::from_utf8(i2).unwrap()) 576 // }, 577 // NodePosition(ErrorKind::Custom(42), i1, box NodePosition(ErrorKind::Custom(128), i2, box Position(ErrorKind::Tag, i3))) => { 578 // format!("missing `mnop` tag after `ijkl`, found '{}' instead", str::from_utf8(i3).unwrap()) 579 // }, 580 // _ => "unrecognized error".to_string() 581 // } 582 //} 583 */ 584 585 #[rustfmt::skip] 586 #[allow(unused_variables)] 587 #[test] 588 fn add_err() { 589 named!(err_test, 590 preceded!( 591 tag!("efgh"), 592 add_return_error!( 593 //ErrorKind::Custom(42u32), 594 ErrorKind::Char, 595 do_parse!( 596 tag!("ijkl") >> 597 //res: add_return_error!(ErrorKind::Custom(128u32), tag!("mnop")) >> 598 res: add_return_error!(ErrorKind::Eof, tag!("mnop")) >> 599 (res) 600 ) 601 ) 602 ) 603 ); 604 let a = &b"efghblah"[..]; 605 let b = &b"efghijklblah"[..]; 606 let c = &b"efghijklmnop"[..]; 607 608 let blah = &b"blah"[..]; 609 610 let res_a = err_test(a); 611 let res_b = err_test(b); 612 let res_c = err_test(c); 613 assert_eq!(res_a, 614 Err(Err::Error(error_node_position!(blah, 615 //ErrorKind::Custom(42u32), 616 ErrorKind::Eof, 617 error_position!(blah, ErrorKind::Tag))))); 618 //assert_eq!(res_b, Err(Err::Error(error_node_position!(&b"ijklblah"[..], ErrorKind::Custom(42u32), 619 // error_node_position!(blah, ErrorKind::Custom(128u32), error_position!(blah, ErrorKind::Tag)))))); 620 assert_eq!(res_b, Err(Err::Error(error_node_position!(&b"ijklblah"[..], ErrorKind::Eof, 621 error_node_position!(blah, ErrorKind::Eof, error_position!(blah, ErrorKind::Tag)))))); 622 assert_eq!(res_c, Ok((&b""[..], &b"mnop"[..]))); 623 } 624 625 #[rustfmt::skip] 626 #[test] 627 fn complete() { 628 named!(err_test, 629 do_parse!( 630 tag!("ijkl") >> 631 res: complete!(tag!("mnop")) >> 632 (res) 633 ) 634 ); 635 let a = &b"ijklmn"[..]; 636 637 let res_a = err_test(a); 638 assert_eq!(res_a, 639 Err(Err::Error(error_position!(&b"mn"[..], ErrorKind::Complete)))); 640 } 641 642 #[test] 643 fn pair() { 644 named!(tag_abc, tag!("abc")); 645 named!(tag_def, tag!("def")); 646 named!( pair_abc_def<&[u8],(&[u8], &[u8])>, pair!(tag_abc, tag_def) ); 647 648 assert_eq!( 649 pair_abc_def(&b"abcdefghijkl"[..]), 650 Ok((&b"ghijkl"[..], (&b"abc"[..], &b"def"[..]))) 651 ); 652 assert_eq!( 653 pair_abc_def(&b"ab"[..]), 654 Err(Err::Incomplete(Needed::new(3))) 655 ); 656 assert_eq!( 657 pair_abc_def(&b"abcd"[..]), 658 Err(Err::Incomplete(Needed::new(3))) 659 ); 660 assert_eq!( 661 pair_abc_def(&b"xxx"[..]), 662 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 663 ); 664 assert_eq!( 665 pair_abc_def(&b"xxxdef"[..]), 666 Err(Err::Error(error_position!(&b"xxxdef"[..], ErrorKind::Tag))) 667 ); 668 assert_eq!( 669 pair_abc_def(&b"abcxxx"[..]), 670 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 671 ); 672 } 673 674 #[test] 675 fn separated_pair() { 676 named!(tag_abc, tag!("abc")); 677 named!(tag_def, tag!("def")); 678 named!(tag_separator, tag!(",")); 679 named!( sep_pair_abc_def<&[u8],(&[u8], &[u8])>, separated_pair!(tag_abc, tag_separator, tag_def) ); 680 681 assert_eq!( 682 sep_pair_abc_def(&b"abc,defghijkl"[..]), 683 Ok((&b"ghijkl"[..], (&b"abc"[..], &b"def"[..]))) 684 ); 685 assert_eq!( 686 sep_pair_abc_def(&b"ab"[..]), 687 Err(Err::Incomplete(Needed::new(3))) 688 ); 689 assert_eq!( 690 sep_pair_abc_def(&b"abc,d"[..]), 691 Err(Err::Incomplete(Needed::new(3))) 692 ); 693 assert_eq!( 694 sep_pair_abc_def(&b"xxx"[..]), 695 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 696 ); 697 assert_eq!( 698 sep_pair_abc_def(&b"xxx,def"[..]), 699 Err(Err::Error(error_position!(&b"xxx,def"[..], ErrorKind::Tag))) 700 ); 701 assert_eq!( 702 sep_pair_abc_def(&b"abc,xxx"[..]), 703 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 704 ); 705 } 706 707 #[test] 708 fn preceded() { 709 named!(tag_abcd, tag!("abcd")); 710 named!(tag_efgh, tag!("efgh")); 711 named!( preceded_abcd_efgh<&[u8], &[u8]>, preceded!(tag_abcd, tag_efgh) ); 712 713 assert_eq!( 714 preceded_abcd_efgh(&b"abcdefghijkl"[..]), 715 Ok((&b"ijkl"[..], &b"efgh"[..])) 716 ); 717 assert_eq!( 718 preceded_abcd_efgh(&b"ab"[..]), 719 Err(Err::Incomplete(Needed::new(4))) 720 ); 721 assert_eq!( 722 preceded_abcd_efgh(&b"abcde"[..]), 723 Err(Err::Incomplete(Needed::new(4))) 724 ); 725 assert_eq!( 726 preceded_abcd_efgh(&b"xxx"[..]), 727 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 728 ); 729 assert_eq!( 730 preceded_abcd_efgh(&b"xxxxdef"[..]), 731 Err(Err::Error(error_position!(&b"xxxxdef"[..], ErrorKind::Tag))) 732 ); 733 assert_eq!( 734 preceded_abcd_efgh(&b"abcdxxx"[..]), 735 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 736 ); 737 } 738 739 #[test] 740 fn terminated() { 741 named!(tag_abcd, tag!("abcd")); 742 named!(tag_efgh, tag!("efgh")); 743 named!( terminated_abcd_efgh<&[u8], &[u8]>, terminated!(tag_abcd, tag_efgh) ); 744 745 assert_eq!( 746 terminated_abcd_efgh(&b"abcdefghijkl"[..]), 747 Ok((&b"ijkl"[..], &b"abcd"[..])) 748 ); 749 assert_eq!( 750 terminated_abcd_efgh(&b"ab"[..]), 751 Err(Err::Incomplete(Needed::new(4))) 752 ); 753 assert_eq!( 754 terminated_abcd_efgh(&b"abcde"[..]), 755 Err(Err::Incomplete(Needed::new(4))) 756 ); 757 assert_eq!( 758 terminated_abcd_efgh(&b"xxx"[..]), 759 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 760 ); 761 assert_eq!( 762 terminated_abcd_efgh(&b"xxxxdef"[..]), 763 Err(Err::Error(error_position!(&b"xxxxdef"[..], ErrorKind::Tag))) 764 ); 765 assert_eq!( 766 terminated_abcd_efgh(&b"abcdxxxx"[..]), 767 Err(Err::Error(error_position!(&b"xxxx"[..], ErrorKind::Tag))) 768 ); 769 } 770 771 #[test] 772 fn delimited() { 773 named!(tag_abc, tag!("abc")); 774 named!(tag_def, tag!("def")); 775 named!(tag_ghi, tag!("ghi")); 776 named!( delimited_abc_def_ghi<&[u8], &[u8]>, delimited!(tag_abc, tag_def, tag_ghi) ); 777 778 assert_eq!( 779 delimited_abc_def_ghi(&b"abcdefghijkl"[..]), 780 Ok((&b"jkl"[..], &b"def"[..])) 781 ); 782 assert_eq!( 783 delimited_abc_def_ghi(&b"ab"[..]), 784 Err(Err::Incomplete(Needed::new(3))) 785 ); 786 assert_eq!( 787 delimited_abc_def_ghi(&b"abcde"[..]), 788 Err(Err::Incomplete(Needed::new(3))) 789 ); 790 assert_eq!( 791 delimited_abc_def_ghi(&b"abcdefgh"[..]), 792 Err(Err::Incomplete(Needed::new(3))) 793 ); 794 assert_eq!( 795 delimited_abc_def_ghi(&b"xxx"[..]), 796 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 797 ); 798 assert_eq!( 799 delimited_abc_def_ghi(&b"xxxdefghi"[..]), 800 Err(Err::Error(error_position!( 801 &b"xxxdefghi"[..], 802 ErrorKind::Tag 803 ),)) 804 ); 805 assert_eq!( 806 delimited_abc_def_ghi(&b"abcxxxghi"[..]), 807 Err(Err::Error(error_position!(&b"xxxghi"[..], ErrorKind::Tag))) 808 ); 809 assert_eq!( 810 delimited_abc_def_ghi(&b"abcdefxxx"[..]), 811 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 812 ); 813 } 814 815 #[test] 816 fn tuple_test() { 817 named!(tuple_3<&[u8], (u16, &[u8], &[u8]) >, 818 tuple!( be_u16 , take!(3), tag!("fg") ) ); 819 820 assert_eq!( 821 tuple_3(&b"abcdefgh"[..]), 822 Ok((&b"h"[..], (0x6162u16, &b"cde"[..], &b"fg"[..]))) 823 ); 824 assert_eq!(tuple_3(&b"abcd"[..]), Err(Err::Incomplete(Needed::new(3)))); 825 assert_eq!(tuple_3(&b"abcde"[..]), Err(Err::Incomplete(Needed::new(2)))); 826 assert_eq!( 827 tuple_3(&b"abcdejk"[..]), 828 Err(Err::Error(error_position!(&b"jk"[..], ErrorKind::Tag))) 829 ); 830 } 831 832 #[test] 833 fn do_parse() { 834 fn ret_int1(i: &[u8]) -> IResult<&[u8], u8> { 835 Ok((i, 1)) 836 } 837 fn ret_int2(i: &[u8]) -> IResult<&[u8], u8> { 838 Ok((i, 2)) 839 } 840 841 //trace_macros!(true); 842 named!(do_parser<&[u8], (u8, u8)>, 843 do_parse!( 844 tag!("abcd") >> 845 opt!(tag!("abcd")) >> 846 aa: ret_int1 >> 847 tag!("efgh") >> 848 bb: ret_int2 >> 849 tag!("efgh") >> 850 (aa, bb) 851 ) 852 ); 853 //named!(do_parser<&[u8], (u8, u8)>, 854 // do_parse!( 855 // tag!("abcd") >> aa: ret_int1 >> tag!("efgh") >> bb: ret_int2 >> tag!("efgh") >> (aa, bb) 856 // ) 857 //); 858 859 //trace_macros!(false); 860 861 assert_eq!( 862 do_parser(&b"abcdabcdefghefghX"[..]), 863 Ok((&b"X"[..], (1, 2))) 864 ); 865 assert_eq!(do_parser(&b"abcdefghefghX"[..]), Ok((&b"X"[..], (1, 2)))); 866 assert_eq!( 867 do_parser(&b"abcdab"[..]), 868 Err(Err::Incomplete(Needed::new(4))) 869 ); 870 assert_eq!( 871 do_parser(&b"abcdefghef"[..]), 872 Err(Err::Incomplete(Needed::new(4))) 873 ); 874 } 875 876 #[rustfmt::skip] 877 #[test] 878 fn do_parse_dependency() { 879 use crate::number::streaming::be_u8; 880 881 named!(length_value, 882 do_parse!( 883 length: be_u8 >> 884 bytes: take!(length) >> 885 (bytes) 886 ) 887 ); 888 889 let a = [2u8, 3, 4, 5]; 890 let res_a = [3u8, 4]; 891 assert_eq!(length_value(&a[..]), Ok((&a[3..], &res_a[..]))); 892 let b = [5u8, 3, 4, 5]; 893 assert_eq!(length_value(&b[..]), Err(Err::Incomplete(Needed::new(5)))); 894 } 895 896 /* 897 named!(does_not_compile, 898 do_parse!( 899 length: be_u8 >> 900 bytes: take!(length) 901 ) 902 ); 903 named!(does_not_compile_either, 904 do_parse!( 905 length: be_u8 ~ 906 bytes: take!(length) ~ 907 ( () ) 908 ) 909 ); 910 fn still_does_not_compile() { 911 let data = b"abcd"; 912 913 let res = do_parse!( 914 tag!("abcd") >> 915 tag!("efgh") >> 916 ( () ) 917 ); 918 } 919 */ 920 } 921