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::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(("123abc", ErrorKind::Alpha)))); 103 /// assert_eq!(parser("abc;123"), Err(Err::Error((";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::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(("123,abc", ErrorKind::Alpha)))); 139 /// assert_eq!(parser("abc;123"), Err(Err::Error((";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::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(("abc", ErrorKind::Char)))); 171 /// assert_eq!(parser("-123"), Err(Err::Error(("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::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((",", ErrorKind::Char)))); 206 /// assert_eq!(parser("123;"), Err(Err::Error(("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::Size(5)))); 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::internal::{Err, IResult, Needed}; 477 use crate::number::streaming::be_u16; 478 use crate::error::ErrorKind; 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::Size(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::Size(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 #[cfg_attr(rustfmt, 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 #[cfg_attr(rustfmt, rustfmt_skip)] 586 #[allow(unused_variables)] 587 #[test] add_err()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 #[cfg_attr(rustfmt, rustfmt_skip)] 626 #[test] complete()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] pair()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!(pair_abc_def(&b"abcdefghijkl"[..]), Ok((&b"ghijkl"[..], (&b"abc"[..], &b"def"[..])))); 649 assert_eq!(pair_abc_def(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); 650 assert_eq!(pair_abc_def(&b"abcd"[..]), Err(Err::Incomplete(Needed::Size(3)))); 651 assert_eq!( 652 pair_abc_def(&b"xxx"[..]), 653 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 654 ); 655 assert_eq!( 656 pair_abc_def(&b"xxxdef"[..]), 657 Err(Err::Error(error_position!(&b"xxxdef"[..], ErrorKind::Tag))) 658 ); 659 assert_eq!( 660 pair_abc_def(&b"abcxxx"[..]), 661 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 662 ); 663 } 664 665 #[test] separated_pair()666 fn separated_pair() { 667 named!(tag_abc, tag!("abc")); 668 named!(tag_def, tag!("def")); 669 named!(tag_separator, tag!(",")); 670 named!( sep_pair_abc_def<&[u8],(&[u8], &[u8])>, separated_pair!(tag_abc, tag_separator, tag_def) ); 671 672 assert_eq!( 673 sep_pair_abc_def(&b"abc,defghijkl"[..]), 674 Ok((&b"ghijkl"[..], (&b"abc"[..], &b"def"[..]))) 675 ); 676 assert_eq!(sep_pair_abc_def(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); 677 assert_eq!(sep_pair_abc_def(&b"abc,d"[..]), Err(Err::Incomplete(Needed::Size(3)))); 678 assert_eq!( 679 sep_pair_abc_def(&b"xxx"[..]), 680 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 681 ); 682 assert_eq!( 683 sep_pair_abc_def(&b"xxx,def"[..]), 684 Err(Err::Error(error_position!(&b"xxx,def"[..], ErrorKind::Tag))) 685 ); 686 assert_eq!( 687 sep_pair_abc_def(&b"abc,xxx"[..]), 688 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 689 ); 690 } 691 692 #[test] preceded()693 fn preceded() { 694 named!(tag_abcd, tag!("abcd")); 695 named!(tag_efgh, tag!("efgh")); 696 named!( preceded_abcd_efgh<&[u8], &[u8]>, preceded!(tag_abcd, tag_efgh) ); 697 698 assert_eq!(preceded_abcd_efgh(&b"abcdefghijkl"[..]), Ok((&b"ijkl"[..], &b"efgh"[..]))); 699 assert_eq!(preceded_abcd_efgh(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); 700 assert_eq!(preceded_abcd_efgh(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(4)))); 701 assert_eq!( 702 preceded_abcd_efgh(&b"xxx"[..]), 703 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 704 ); 705 assert_eq!( 706 preceded_abcd_efgh(&b"xxxxdef"[..]), 707 Err(Err::Error(error_position!(&b"xxxxdef"[..], ErrorKind::Tag))) 708 ); 709 assert_eq!( 710 preceded_abcd_efgh(&b"abcdxxx"[..]), 711 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 712 ); 713 } 714 715 #[test] terminated()716 fn terminated() { 717 named!(tag_abcd, tag!("abcd")); 718 named!(tag_efgh, tag!("efgh")); 719 named!( terminated_abcd_efgh<&[u8], &[u8]>, terminated!(tag_abcd, tag_efgh) ); 720 721 assert_eq!(terminated_abcd_efgh(&b"abcdefghijkl"[..]), Ok((&b"ijkl"[..], &b"abcd"[..]))); 722 assert_eq!(terminated_abcd_efgh(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); 723 assert_eq!(terminated_abcd_efgh(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(4)))); 724 assert_eq!( 725 terminated_abcd_efgh(&b"xxx"[..]), 726 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 727 ); 728 assert_eq!( 729 terminated_abcd_efgh(&b"xxxxdef"[..]), 730 Err(Err::Error(error_position!(&b"xxxxdef"[..], ErrorKind::Tag))) 731 ); 732 assert_eq!( 733 terminated_abcd_efgh(&b"abcdxxxx"[..]), 734 Err(Err::Error(error_position!(&b"xxxx"[..], ErrorKind::Tag))) 735 ); 736 } 737 738 #[test] delimited()739 fn delimited() { 740 named!(tag_abc, tag!("abc")); 741 named!(tag_def, tag!("def")); 742 named!(tag_ghi, tag!("ghi")); 743 named!( delimited_abc_def_ghi<&[u8], &[u8]>, delimited!(tag_abc, tag_def, tag_ghi) ); 744 745 assert_eq!(delimited_abc_def_ghi(&b"abcdefghijkl"[..]), Ok((&b"jkl"[..], &b"def"[..]))); 746 assert_eq!(delimited_abc_def_ghi(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(3)))); 747 assert_eq!(delimited_abc_def_ghi(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(3)))); 748 assert_eq!(delimited_abc_def_ghi(&b"abcdefgh"[..]), Err(Err::Incomplete(Needed::Size(3)))); 749 assert_eq!( 750 delimited_abc_def_ghi(&b"xxx"[..]), 751 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 752 ); 753 assert_eq!( 754 delimited_abc_def_ghi(&b"xxxdefghi"[..]), 755 Err(Err::Error(error_position!(&b"xxxdefghi"[..], ErrorKind::Tag),)) 756 ); 757 assert_eq!( 758 delimited_abc_def_ghi(&b"abcxxxghi"[..]), 759 Err(Err::Error(error_position!(&b"xxxghi"[..], ErrorKind::Tag))) 760 ); 761 assert_eq!( 762 delimited_abc_def_ghi(&b"abcdefxxx"[..]), 763 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 764 ); 765 } 766 767 #[test] tuple_test()768 fn tuple_test() { 769 named!(tuple_3<&[u8], (u16, &[u8], &[u8]) >, 770 tuple!( be_u16 , take!(3), tag!("fg") ) ); 771 772 assert_eq!(tuple_3(&b"abcdefgh"[..]), Ok((&b"h"[..], (0x6162u16, &b"cde"[..], &b"fg"[..])))); 773 assert_eq!(tuple_3(&b"abcd"[..]), Err(Err::Incomplete(Needed::Size(3)))); 774 assert_eq!(tuple_3(&b"abcde"[..]), Err(Err::Incomplete(Needed::Size(2)))); 775 assert_eq!( 776 tuple_3(&b"abcdejk"[..]), 777 Err(Err::Error(error_position!(&b"jk"[..], ErrorKind::Tag))) 778 ); 779 } 780 781 #[test] do_parse()782 fn do_parse() { 783 fn ret_int1(i: &[u8]) -> IResult<&[u8], u8> { 784 Ok((i, 1)) 785 }; 786 fn ret_int2(i: &[u8]) -> IResult<&[u8], u8> { 787 Ok((i, 2)) 788 }; 789 790 //trace_macros!(true); 791 named!(do_parser<&[u8], (u8, u8)>, 792 do_parse!( 793 tag!("abcd") >> 794 opt!(tag!("abcd")) >> 795 aa: ret_int1 >> 796 tag!("efgh") >> 797 bb: ret_int2 >> 798 tag!("efgh") >> 799 (aa, bb) 800 ) 801 ); 802 //named!(do_parser<&[u8], (u8, u8)>, 803 // do_parse!( 804 // tag!("abcd") >> aa: ret_int1 >> tag!("efgh") >> bb: ret_int2 >> tag!("efgh") >> (aa, bb) 805 // ) 806 //); 807 808 //trace_macros!(false); 809 810 assert_eq!(do_parser(&b"abcdabcdefghefghX"[..]), Ok((&b"X"[..], (1, 2)))); 811 assert_eq!(do_parser(&b"abcdefghefghX"[..]), Ok((&b"X"[..], (1, 2)))); 812 assert_eq!(do_parser(&b"abcdab"[..]), Err(Err::Incomplete(Needed::Size(4)))); 813 assert_eq!(do_parser(&b"abcdefghef"[..]), Err(Err::Incomplete(Needed::Size(4)))); 814 } 815 816 #[cfg_attr(rustfmt, rustfmt_skip)] 817 #[test] do_parse_dependency()818 fn do_parse_dependency() { 819 use crate::number::streaming::be_u8; 820 821 named!(length_value, 822 do_parse!( 823 length: be_u8 >> 824 bytes: take!(length) >> 825 (bytes) 826 ) 827 ); 828 829 let a = [2u8, 3, 4, 5]; 830 let res_a = [3u8, 4]; 831 assert_eq!(length_value(&a[..]), Ok((&a[3..], &res_a[..]))); 832 let b = [5u8, 3, 4, 5]; 833 assert_eq!(length_value(&b[..]), Err(Err::Incomplete(Needed::Size(5)))); 834 } 835 836 /* 837 named!(does_not_compile, 838 do_parse!( 839 length: be_u8 >> 840 bytes: take!(length) 841 ) 842 ); 843 named!(does_not_compile_either, 844 do_parse!( 845 length: be_u8 ~ 846 bytes: take!(length) ~ 847 ( () ) 848 ) 849 ); 850 fn still_does_not_compile() { 851 let data = b"abcd"; 852 853 let res = do_parse!( 854 tag!("abcd") >> 855 tag!("efgh") >> 856 ( () ) 857 ); 858 } 859 */ 860 } 861