1 //! Macro combinators 2 //! 3 //! Macros are used to make combination easier, 4 //! since they often do not depend on the type 5 //! of the data they manipulate or return. 6 //! 7 //! There is a trick to make them easier to assemble, 8 //! combinators are defined like this: 9 //! 10 //! ```ignore 11 //! macro_rules! tag ( 12 //! ($i:expr, $inp: expr) => ( 13 //! { 14 //! ... 15 //! } 16 //! ); 17 //! ); 18 //! ``` 19 //! 20 //! But when used in other combinators, are Used 21 //! like this: 22 //! 23 //! ```ignore 24 //! named!(my_function, tag!("abcd")); 25 //! ``` 26 //! 27 //! Internally, other combinators will rewrite 28 //! that call to pass the input as first argument: 29 //! 30 //! ```ignore 31 //! macro_rules! named ( 32 //! ($name:ident, $submac:ident!( $($args:tt)* )) => ( 33 //! fn $name<'a>( i: &'a [u8] ) -> IResult<'a,&[u8], &[u8]> { 34 //! $submac!(i, $($args)*) 35 //! } 36 //! ); 37 //! ); 38 //! ``` 39 //! 40 //! If you want to call a combinator directly, you can 41 //! do it like this: 42 //! 43 //! ```ignore 44 //! let res = { tag!(input, "abcd"); } 45 //! ``` 46 //! 47 //! Combinators must have a specific variant for 48 //! non-macro arguments. Example: passing a function 49 //! to take_while! instead of another combinator. 50 //! 51 //! ```ignore 52 //! macro_rules! take_while( 53 //! ($input:expr, $submac:ident!( $($args:tt)* )) => ( 54 //! { 55 //! ... 56 //! } 57 //! ); 58 //! 59 //! // wrap the function in a macro to pass it to the main implementation 60 //! ($input:expr, $f:expr) => ( 61 //! take_while!($input, call!($f)); 62 //! ); 63 //! ); 64 //! ``` 65 #[allow(unused_variables)] 66 67 /// Makes a function from a parser combination 68 /// 69 /// The type can be set up if the compiler needs 70 /// more information 71 /// 72 /// Function-like declaration: 73 /// ``` 74 /// # use nom::{named, tag}; 75 /// named!(my_function( &[u8] ) -> &[u8], tag!("abcd")); 76 /// ``` 77 /// Alternative declaration. First type parameter is input, second is output: 78 /// ``` 79 /// # use nom::{named, tag}; 80 /// named!(my_function<&[u8], &[u8]>, tag!("abcd")); 81 /// ``` 82 /// This one will have `&[u8]` as input type, `&[u8]` as output type: 83 /// ``` 84 /// # use nom::{named, tag}; 85 /// named!(my_function, tag!("abcd")); 86 /// ``` 87 /// Will use `&[u8]` as output type: 88 /// ``` 89 /// # use nom::{named, tag}; 90 /// named!(my_function<&[u8]>, tag!("abcd")); 91 /// ``` 92 /// Prefix them with 'pub' to make the functions public: 93 /// ``` 94 /// # use nom::{named, tag}; 95 /// named!(pub my_function, tag!("abcd")); 96 /// ``` 97 /// Prefix them with 'pub(crate)' to make the functions public within the crate: 98 /// ``` 99 /// # use nom::{named, tag}; 100 /// named!(pub(crate) my_function, tag!("abcd")); 101 /// ``` 102 #[macro_export(local_inner_macros)] 103 macro_rules! named ( 104 (#$($args:tt)*) => ( 105 named_attr!(#$($args)*); 106 ); 107 ($vis:vis $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( 108 $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, ($i, $crate::error::ErrorKind)> { 109 $submac!(i, $($args)*) 110 } 111 ); 112 ($vis:vis $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( 113 $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> { 114 $submac!(i, $($args)*) 115 } 116 ); 117 ($vis:vis $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( 118 $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, ($i, $crate::error::ErrorKind)> { 119 $submac!(i, $($args)*) 120 } 121 ); 122 ($vis:vis $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( 123 $vis fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, (&[u8], $crate::error::ErrorKind)> { 124 $submac!(i, $($args)*) 125 } 126 ); 127 ($vis:vis $name:ident, $submac:ident!( $($args:tt)* )) => ( 128 $vis fn $name( i: &[u8] ) -> $crate::IResult<&[u8], &[u8], (&[u8], $crate::error::ErrorKind)> { 129 $submac!(i, $($args)*) 130 } 131 ); 132 ); 133 134 /// Makes a function from a parser combination with arguments. 135 /// 136 /// ```ignore 137 /// //takes [`&[u8]`] as input 138 /// named_args!(tagged(open_tag: &[u8], close_tag: &[u8])<&str>, 139 /// delimited!(tag!(open_tag), map_res!(take!(4), str::from_utf8), tag!(close_tag)) 140 /// ); 141 142 /// //takes `&str` as input 143 /// named_args!(tagged(open_tag: &str, close_tag: &str)<&str, &str>, 144 /// delimited!(tag!(open_tag), take!(4), tag!(close_tag)) 145 /// ); 146 /// ``` 147 /// 148 /// Note: if using arguments that way gets hard to read, it is always 149 /// possible to write the equivalent parser definition manually, like 150 /// this: 151 /// 152 /// ```ignore 153 /// fn tagged(input: &[u8], open_tag: &[u8], close_tag: &[u8]) -> IResult<&[u8], &str> { 154 /// // the first combinator in the tree gets the input as argument. It is then 155 /// // passed from one combinator to the next through macro rewriting 156 /// delimited!(input, 157 /// tag!(open_tag), take!(4), tag!(close_tag) 158 /// ) 159 /// ); 160 /// ``` 161 /// 162 #[macro_export(local_inner_macros)] 163 macro_rules! named_args { 164 ($vis:vis $func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { 165 $vis fn $func_name(input: &[u8], $( $arg : $typ ),*) -> $crate::IResult<&[u8], $return_type> { 166 $submac!(input, $($args)*) 167 } 168 }; 169 170 ($vis:vis $func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { 171 $vis fn $func_name<'this_is_probably_unique_i_hope_please, 'a>( 172 input: &'this_is_probably_unique_i_hope_please [u8], $( $arg : $typ ),*) -> 173 $crate::IResult<&'this_is_probably_unique_i_hope_please [u8], $return_type> 174 { 175 $submac!(input, $($args)*) 176 } 177 }; 178 179 ($vis:vis $func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $input_type:ty, $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { 180 $vis fn $func_name(input: $input_type, $( $arg : $typ ),*) -> $crate::IResult<$input_type, $return_type> { 181 $submac!(input, $($args)*) 182 } 183 }; 184 185 ($vis:vis $func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $input_type:ty, $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => { 186 $vis fn $func_name<'a>( 187 input: $input_type, $( $arg : $typ ),*) 188 -> $crate::IResult<$input_type, $return_type> 189 { 190 $submac!(input, $($args)*) 191 } 192 }; 193 } 194 195 /// Makes a function from a parser combination, with attributes 196 /// 197 /// The usage of this macro is almost identical to `named!`, except that 198 /// you also pass attributes to be attached to the generated function. 199 /// This is ideal for adding documentation to your parser. 200 /// 201 /// Create my_function as if you wrote it with the doc comment /// My Func: 202 /// ``` 203 /// # use nom::{named_attr, tag}; 204 /// named_attr!(#[doc = "My Func"], my_function( &[u8] ) -> &[u8], tag!("abcd")); 205 /// ``` 206 /// Also works for pub functions, and multiple lines: 207 /// ``` 208 /// # use nom::{named_attr, tag}; 209 /// named_attr!(#[doc = "My Func\nRecognise abcd"], pub my_function, tag!("abcd")); 210 /// ``` 211 /// Multiple attributes can be passed if required: 212 /// ``` 213 /// # use nom::{named_attr, tag}; 214 /// named_attr!(#[doc = "My Func"] #[inline(always)], pub my_function, tag!("abcd")); 215 /// ``` 216 #[macro_export(local_inner_macros)] 217 macro_rules! named_attr ( 218 ($(#[$attr:meta])*, $vis:vis $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => ( 219 $(#[$attr])* 220 $vis fn $name( i: $i ) -> $crate::IResult<$i,$o, ($i, $crate::error::ErrorKind)> { 221 $submac!(i, $($args)*) 222 } 223 ); 224 ($(#[$attr:meta])*, $vis:vis $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => ( 225 $(#[$attr])* 226 $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> { 227 $submac!(i, $($args)*) 228 } 229 ); 230 ($(#[$attr:meta])*, $vis:vis $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => ( 231 $(#[$attr])* 232 $vis fn $name( i: $i ) -> $crate::IResult<$i, $o, ($i, $crate::error::ErrorKind)> { 233 $submac!(i, $($args)*) 234 } 235 ); 236 ($(#[$attr:meta])*, $vis:vis $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => ( 237 $(#[$attr])* 238 $vis fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, (&[u8], $crate::error::ErrorKind)> { 239 $submac!(i, $($args)*) 240 } 241 ); 242 ($(#[$attr:meta])*, $vis:vis $name:ident, $submac:ident!( $($args:tt)* )) => ( 243 $(#[$attr])* 244 $vis fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<&[u8], &[u8], (&[u8], $crate::error::ErrorKind)> { 245 $submac!(i, $($args)*) 246 } 247 ); 248 ); 249 250 /// Used to wrap common expressions and function as macros 251 /// 252 /// ``` 253 /// # #[macro_use] extern crate nom; 254 /// # use nom::IResult; 255 /// # fn main() { 256 /// fn take_wrapper(input: &[u8], i: u8) -> IResult<&[u8], &[u8]> { take!(input, i * 10) } 257 /// 258 /// // will make a parser taking 20 bytes 259 /// named!(parser, call!(take_wrapper, 2)); 260 /// # } 261 /// ``` 262 #[macro_export(local_inner_macros)] 263 macro_rules! call ( 264 ($i:expr, $fun:expr) => ( $fun( $i ) ); 265 ($i:expr, $fun:expr, $($args:expr),* ) => ( $fun( $i, $($args),* ) ); 266 ); 267 268 //FIXME: error rewrite 269 /// Prevents backtracking if the child parser fails 270 /// 271 /// This parser will do an early return instead of sending 272 /// its result to the parent parser. 273 /// 274 /// If another `return_error!` combinator is present in the parent 275 /// chain, the error will be wrapped and another early 276 /// return will be made. 277 /// 278 /// This makes it easy to build report on which parser failed, 279 /// where it failed in the input, and the chain of parsers 280 /// that led it there. 281 /// 282 /// Additionally, the error chain contains number identifiers 283 /// that can be matched to provide useful error messages. 284 /// 285 /// ``` 286 /// # #[macro_use] extern crate nom; 287 /// # use nom::Err; 288 /// # use nom::error::ErrorKind; 289 /// # fn main() { 290 /// named!(err_test<&[u8], &[u8]>, alt!( 291 /// tag!("abcd") | 292 /// preceded!(tag!("efgh"), return_error!(ErrorKind::Eof, 293 /// do_parse!( 294 /// tag!("ijkl") >> 295 /// res: return_error!(ErrorKind::Tag, tag!("mnop")) >> 296 /// (res) 297 /// ) 298 /// ) 299 /// ) 300 /// )); 301 /// let a = &b"efghblah"[..]; 302 /// let b = &b"efghijklblah"[..]; 303 /// let c = &b"efghijklmnop"[..]; 304 /// 305 /// let blah = &b"blah"[..]; 306 /// 307 /// let res_a = err_test(a); 308 /// let res_b = err_test(b); 309 /// let res_c = err_test(c); 310 /// assert_eq!(res_a, Err(Err::Failure(error_node_position!(blah, ErrorKind::Eof, error_position!(blah, ErrorKind::Tag))))); 311 /// assert_eq!(res_b, Err(Err::Failure(error_node_position!(&b"ijklblah"[..], ErrorKind::Eof, 312 /// error_node_position!(blah, ErrorKind::Tag, error_position!(blah, ErrorKind::Tag)))) 313 /// )); 314 /// # } 315 /// ``` 316 /// 317 #[macro_export(local_inner_macros)] 318 macro_rules! return_error ( 319 ($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( 320 { 321 use $crate::lib::std::result::Result::*; 322 use $crate::Err; 323 324 let i_ = $i.clone(); 325 let cl = || { 326 $submac!(i_, $($args)*) 327 }; 328 329 match cl() { 330 Err(Err::Incomplete(x)) => Err(Err::Incomplete(x)), 331 Ok((i, o)) => Ok((i, o)), 332 Err(Err::Error(e)) | Err(Err::Failure(e)) => { 333 return Err(Err::Failure($crate::error::append_error($i, $code, e))) 334 } 335 } 336 } 337 ); 338 ($i:expr, $code:expr, $f:expr) => ( 339 return_error!($i, $code, call!($f)); 340 ); 341 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 342 { 343 use $crate::lib::std::result::Result::*; 344 use $crate::Err; 345 346 let i_ = $i.clone(); 347 let cl = || { 348 $submac!(i_, $($args)*) 349 }; 350 351 match cl() { 352 Err(Err::Incomplete(x)) => Err(Err::Incomplete(x)), 353 Ok((i, o)) => Ok((i, o)), 354 Err(Err::Error(e)) | Err(Err::Failure(e)) => { 355 return Err(Err::Failure(e)) 356 } 357 } 358 } 359 ); 360 ($i:expr, $f:expr) => ( 361 return_error!($i, call!($f)); 362 ); 363 ); 364 365 //FIXME: error rewrite 366 /// Add an error if the child parser fails 367 /// 368 /// While `return_error!` does an early return and avoids backtracking, 369 /// add_return_error! backtracks normally. It just provides more context 370 /// for an error 371 /// 372 /// ``` 373 /// # #[macro_use] extern crate nom; 374 /// # use std::collections; 375 /// # use nom::Err; 376 /// # use nom::error::ErrorKind; 377 /// # fn main() { 378 /// named!(err_test, add_return_error!(ErrorKind::Tag, tag!("abcd"))); 379 /// 380 /// let a = &b"efghblah"[..]; 381 /// let res_a = err_test(a); 382 /// assert_eq!(res_a, Err(Err::Error(error_node_position!(a, ErrorKind::Tag, error_position!(a, ErrorKind::Tag))))); 383 /// # } 384 /// ``` 385 /// 386 #[macro_export(local_inner_macros)] 387 macro_rules! add_return_error ( 388 ($i:expr, $code:expr, $submac:ident!( $($args:tt)* )) => ( 389 { 390 use $crate::lib::std::result::Result::*; 391 use $crate::{Err,error::ErrorKind}; 392 393 match $submac!($i, $($args)*) { 394 Ok((i, o)) => Ok((i, o)), 395 Err(Err::Error(e)) => { 396 Err(Err::Error(error_node_position!($i, $code, e))) 397 }, 398 Err(Err::Failure(e)) => { 399 Err(Err::Failure(error_node_position!($i, $code, e))) 400 }, 401 Err(e) => Err(e), 402 } 403 } 404 ); 405 ($i:expr, $code:expr, $f:expr) => ( 406 add_return_error!($i, $code, call!($f)); 407 ); 408 ); 409 410 /// replaces a `Incomplete` returned by the child parser 411 /// with an `Error` 412 /// 413 /// ``` 414 /// # #[macro_use] extern crate nom; 415 /// # use std::collections; 416 /// # use nom::Err; 417 /// # use nom::error::ErrorKind; 418 /// # fn main() { 419 /// named!(take_5, complete!(take!(5))); 420 /// 421 /// let a = &b"abcd"[..]; 422 /// let res_a = take_5(a); 423 /// assert_eq!(res_a, Err(Err::Error(error_position!(a, ErrorKind::Complete)))); 424 /// # } 425 /// ``` 426 /// 427 #[macro_export(local_inner_macros)] 428 macro_rules! complete ( 429 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 430 $crate::combinator::completec($i, move |i| { $submac!(i, $($args)*) }) 431 ); 432 ($i:expr, $f:expr) => ( 433 complete!($i, call!($f)); 434 ); 435 ); 436 437 /// A bit like `std::try!`, this macro will return the remaining input and 438 /// parsed value if the child parser returned `Ok`, and will do an early 439 /// return for the `Err` side. 440 /// 441 /// this can provide more flexibility than `do_parse!` if needed 442 /// 443 /// ``` 444 /// # #[macro_use] extern crate nom; 445 /// # use nom::Err; 446 /// # use nom::error::ErrorKind; 447 /// # use nom::IResult; 448 /// 449 /// fn take_add(input:&[u8], size: u8) -> IResult<&[u8], &[u8]> { 450 /// let (i1, length) = try_parse!(input, map_opt!(nom::number::streaming::be_u8, |sz| size.checked_add(sz))); 451 /// let (i2, data) = try_parse!(i1, take!(length)); 452 /// return Ok((i2, data)); 453 /// } 454 /// # fn main() { 455 /// let arr1 = [1, 2, 3, 4, 5]; 456 /// let r1 = take_add(&arr1[..], 1); 457 /// assert_eq!(r1, Ok((&[4,5][..], &[2,3][..]))); 458 /// 459 /// let arr2 = [0xFE, 2, 3, 4, 5]; 460 /// // size is overflowing 461 /// let r1 = take_add(&arr2[..], 42); 462 /// assert_eq!(r1, Err(Err::Error(error_position!(&[254, 2,3,4,5][..], ErrorKind::MapOpt)))); 463 /// # } 464 /// ``` 465 #[macro_export(local_inner_macros)] 466 macro_rules! try_parse ( 467 ($i:expr, $submac:ident!( $($args:tt)* )) => ({ 468 use $crate::lib::std::result::Result::*; 469 470 match $submac!($i, $($args)*) { 471 Ok((i,o)) => (i,o), 472 Err(e) => return Err(e), 473 } 474 }); 475 ($i:expr, $f:expr) => ( 476 try_parse!($i, call!($f)) 477 ); 478 ); 479 480 /// `map!(I -> IResult<I, O>, O -> P) => I -> IResult<I, P>` 481 /// 482 /// maps a function on the result of a parser 483 /// 484 /// ```rust 485 /// # #[macro_use] extern crate nom; 486 /// # use nom::{Err,error::ErrorKind, IResult}; 487 /// use nom::character::complete::digit1; 488 /// # fn main() { 489 /// 490 /// named!(parse<&str, usize>, map!(digit1, |s| s.len())); 491 /// 492 /// // the parser will count how many characters were returned by digit1 493 /// assert_eq!(parse("123456"), Ok(("", 6))); 494 /// 495 /// // this will fail if digit1 fails 496 /// assert_eq!(parse("abc"), Err(Err::Error(error_position!("abc", ErrorKind::Digit)))); 497 /// # } 498 /// ``` 499 #[macro_export(local_inner_macros)] 500 macro_rules! map( 501 // Internal parser, do not use directly 502 (__impl $i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 503 $crate::combinator::mapc($i, move |i| {$submac!(i, $($args)*)}, $g) 504 ); 505 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 506 map!(__impl $i, $submac!($($args)*), $g); 507 ); 508 ($i:expr, $f:expr, $g:expr) => ( 509 map!(__impl $i, call!($f), $g); 510 ); 511 ); 512 513 /// `map_res!(I -> IResult<I, O>, O -> Result<P>) => I -> IResult<I, P>` 514 /// maps a function returning a Result on the output of a parser 515 /// 516 /// ```rust 517 /// # #[macro_use] extern crate nom; 518 /// # use nom::{Err,error::ErrorKind, IResult}; 519 /// use nom::character::complete::digit1; 520 /// # fn main() { 521 /// 522 /// named!(parse<&str, u8>, map_res!(digit1, |s: &str| s.parse::<u8>())); 523 /// 524 /// // the parser will convert the result of digit1 to a number 525 /// assert_eq!(parse("123"), Ok(("", 123))); 526 /// 527 /// // this will fail if digit1 fails 528 /// assert_eq!(parse("abc"), Err(Err::Error(error_position!("abc", ErrorKind::Digit)))); 529 /// 530 /// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) 531 /// assert_eq!(parse("123456"), Err(Err::Error(error_position!("123456", ErrorKind::MapRes)))); 532 /// # } 533 /// ``` 534 #[macro_export(local_inner_macros)] 535 macro_rules! map_res ( 536 // Internal parser, do not use directly 537 (__impl $i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 538 $crate::combinator::map_resc($i, move |i| {$submac!(i, $($args)*)}, move |i| {$submac2!(i, $($args2)*)}) 539 ); 540 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 541 map_res!(__impl $i, $submac!($($args)*), call!($g)); 542 ); 543 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 544 map_res!(__impl $i, $submac!($($args)*), $submac2!($($args2)*)); 545 ); 546 ($i:expr, $f:expr, $g:expr) => ( 547 map_res!(__impl $i, call!($f), call!($g)); 548 ); 549 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 550 map_res!(__impl $i, call!($f), $submac!($($args)*)); 551 ); 552 ); 553 554 /// `map_opt!(I -> IResult<I, O>, O -> Option<P>) => I -> IResult<I, P>` 555 /// maps a function returning an Option on the output of a parser 556 /// 557 /// ```rust 558 /// # #[macro_use] extern crate nom; 559 /// # use nom::{Err,error::ErrorKind, IResult}; 560 /// use nom::character::complete::digit1; 561 /// # fn main() { 562 /// 563 /// named!(parser<&str, u8>, map_opt!(digit1, |s: &str| s.parse::<u8>().ok())); 564 /// 565 /// // the parser will convert the result of digit1 to a number 566 /// assert_eq!(parser("123"), Ok(("", 123))); 567 /// 568 /// // this will fail if digit1 fails 569 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::Digit)))); 570 /// 571 /// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) 572 /// assert_eq!(parser("123456"), Err(Err::Error(("123456", ErrorKind::MapOpt)))); 573 /// # } 574 /// ``` 575 #[macro_export(local_inner_macros)] 576 macro_rules! map_opt ( 577 // Internal parser, do not use directly 578 (__impl $i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 579 $crate::combinator::map_optc($i, move |i| {$submac!(i, $($args)*)}, move |i| {$submac2!(i, $($args2)*)}) 580 ); 581 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 582 map_opt!(__impl $i, $submac!($($args)*), call!($g)); 583 ); 584 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 585 map_opt!(__impl $i, $submac!($($args)*), $submac2!($($args2)*)); 586 ); 587 ($i:expr, $f:expr, $g:expr) => ( 588 map_opt!(__impl $i, call!($f), call!($g)); 589 ); 590 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 591 map_opt!(__impl $i, call!($f), $submac!($($args)*)); 592 ); 593 ); 594 595 /// `parse_to!(O) => I -> IResult<I, O>` 596 /// uses the `parse` method from `std::str::FromStr` to convert the current 597 /// input to the specified type 598 /// 599 /// this will completely consume the input 600 /// 601 /// ```rust 602 /// # #[macro_use] extern crate nom; 603 /// # use nom::{Err,error::ErrorKind, IResult}; 604 /// use nom::character::complete::digit1; 605 /// # fn main() { 606 /// 607 /// named!(parser<&str, u8>, parse_to!(u8)); 608 /// 609 /// assert_eq!(parser("123"), Ok(("", 123))); 610 /// 611 /// assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::ParseTo)))); 612 /// 613 /// // this will fail if the mapped function fails (a `u8` is too small to hold `123456`) 614 /// assert_eq!(parser("123456"), Err(Err::Error(("123456", ErrorKind::ParseTo)))); 615 /// # } 616 /// ``` 617 #[macro_export(local_inner_macros)] 618 macro_rules! parse_to ( 619 ($i:expr, $t:ty ) => ( 620 { 621 use $crate::lib::std::result::Result::*; 622 use $crate::lib::std::option::Option; 623 use $crate::lib::std::option::Option::*; 624 use $crate::{Err,error::ErrorKind}; 625 626 use $crate::ParseTo; 627 use $crate::Slice; 628 use $crate::InputLength; 629 630 let res: Option<$t> = ($i).parse_to(); 631 match res { 632 Some(output) => Ok(($i.slice($i.input_len()..), output)), 633 None => Err(Err::Error($crate::error::make_error($i, ErrorKind::ParseTo))) 634 } 635 } 636 ); 637 ); 638 639 /// `verify!(I -> IResult<I, O>, O -> bool) => I -> IResult<I, O>` 640 /// returns the result of the child parser if it satisfies a verification function 641 /// 642 /// ``` 643 /// # #[macro_use] extern crate nom; 644 /// # fn main() { 645 /// named!(check<u32>, verify!(nom::number::streaming::be_u32, |val: &u32| *val < 3)); 646 /// # } 647 /// ``` 648 #[macro_export(local_inner_macros)] 649 macro_rules! verify ( 650 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( 651 $crate::combinator::verifyc($i, |i| $submac!(i, $($args)*), $g) 652 ); 653 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ( 654 $crate::combinator::verifyc($i, |i| $submac!(i, $($args)*), |&o| $submac2!(o, $($args2)*)) 655 ); 656 ($i:expr, $f:expr, $g:expr) => ( 657 $crate::combinator::verify($f, $g)($i) 658 ); 659 ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => ( 660 $crate::combinator::verify($f, |&o| $submac!(o, $($args)*))($i) 661 ); 662 ); 663 664 /// `value!(T, R -> IResult<R, S> ) => R -> IResult<R, T>` 665 /// 666 /// or `value!(T) => R -> IResult<R, T>` 667 /// 668 /// If the child parser was successful, return the value. 669 /// If no child parser is provided, always return the value 670 /// 671 /// ``` 672 /// # #[macro_use] extern crate nom; 673 /// # fn main() { 674 /// named!(x<u8>, value!(42, delimited!(tag!("<!--"), take!(5), tag!("-->")))); 675 /// named!(y<u8>, delimited!(tag!("<!--"), value!(42), tag!("-->"))); 676 /// let r = x(&b"<!-- abc --> aaa"[..]); 677 /// assert_eq!(r, Ok((&b" aaa"[..], 42))); 678 /// 679 /// let r2 = y(&b"<!----> aaa"[..]); 680 /// assert_eq!(r2, Ok((&b" aaa"[..], 42))); 681 /// # } 682 /// ``` 683 #[macro_export(local_inner_macros)] 684 macro_rules! value ( 685 ($i:expr, $res:expr, $submac:ident!( $($args:tt)* )) => ( 686 $crate::combinator::valuec($i, $res, |i| $submac!(i, $($args)*)) 687 ); 688 ($i:expr, $res:expr, $f:expr) => ( 689 $crate::combinator::valuec($i, $res, $f) 690 ); 691 ($i:expr, $res:expr) => ( 692 Ok(($i, $res)) 693 ); 694 ); 695 696 /// `opt!(I -> IResult<I,O>) => I -> IResult<I, Option<O>>` 697 /// make the underlying parser optional 698 /// 699 /// returns an Option of the returned type. This parser returns `Some(result)` if the child parser 700 /// succeeds,`None` if it fails, and `Incomplete` if it did not have enough data to decide 701 /// 702 /// *Warning*: if you are using `opt` for some kind of optional ending token (like an end of line), 703 /// you should combine it with `complete` to make sure it works. 704 /// 705 /// As an example, `opt!(tag!("\r\n"))` will return `Incomplete` if it receives an empty input, 706 /// because `tag` does not have enough input to decide. 707 /// On the contrary, `opt!(complete!(tag!("\r\n")))` would return `None` as produced value, 708 /// since `complete!` transforms an `Incomplete` in an `Error`. 709 /// 710 /// ``` 711 /// # #[macro_use] extern crate nom; 712 /// # fn main() { 713 /// named!( o<&[u8], Option<&[u8]> >, opt!( tag!( "abcd" ) ) ); 714 /// 715 /// let a = b"abcdef"; 716 /// let b = b"bcdefg"; 717 /// assert_eq!(o(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); 718 /// assert_eq!(o(&b[..]), Ok((&b"bcdefg"[..], None))); 719 /// # } 720 /// ``` 721 #[macro_export(local_inner_macros)] 722 macro_rules! opt( 723 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 724 { 725 $crate::combinator::optc($i, |i| $submac!(i, $($args)*)) 726 } 727 ); 728 ($i:expr, $f:expr) => ( 729 $crate::combinator::opt($f)($i) 730 ); 731 ); 732 733 /// `opt_res!(I -> IResult<I,O>) => I -> IResult<I, Result<nom::Err,O>>` 734 /// make the underlying parser optional 735 /// 736 /// returns a Result, with Err containing the parsing error 737 /// 738 /// ```ignore 739 /// # #[macro_use] extern crate nom; 740 /// # use nom::ErrorKind; 741 /// # fn main() { 742 /// named!( o<&[u8], Result<&[u8], nom::Err<&[u8]> > >, opt_res!( tag!( "abcd" ) ) ); 743 /// 744 /// let a = b"abcdef"; 745 /// let b = b"bcdefg"; 746 /// assert_eq!(o(&a[..]), Ok((&b"ef"[..], Ok(&b"abcd"[..]))); 747 /// assert_eq!(o(&b[..]), Ok((&b"bcdefg"[..], Err(error_position!(&b[..], ErrorKind::Tag)))); 748 /// # } 749 /// ``` 750 #[macro_export(local_inner_macros)] 751 macro_rules! opt_res ( 752 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 753 { 754 use $crate::lib::std::result::Result::*; 755 use $crate::Err; 756 757 let i_ = $i.clone(); 758 match $submac!(i_, $($args)*) { 759 Ok((i,o)) => Ok((i, Ok(o))), 760 Err(Err::Error(e)) => Ok(($i, Err(Err::Error(e)))), 761 // in case of failure, we return a real error 762 Err(e) => Err(e) 763 } 764 } 765 ); 766 ($i:expr, $f:expr) => ( 767 opt_res!($i, call!($f)); 768 ); 769 ); 770 771 /// `cond!(bool, I -> IResult<I,O>) => I -> IResult<I, Option<O>>` 772 /// Conditional combinator 773 /// 774 /// Wraps another parser and calls it if the 775 /// condition is met. This combinator returns 776 /// an Option of the return type of the child 777 /// parser. 778 /// 779 /// This is especially useful if a parser depends 780 /// on the value returned by a preceding parser in 781 /// a `do_parse!`. 782 /// 783 /// ``` 784 /// # #[macro_use] extern crate nom; 785 /// # use nom::IResult; 786 /// # fn main() { 787 /// fn f_true(i: &[u8]) -> IResult<&[u8], Option<&[u8]>> { 788 /// cond!(i, true, tag!("abcd")) 789 /// } 790 /// 791 /// fn f_false(i: &[u8]) -> IResult<&[u8], Option<&[u8]>> { 792 /// cond!(i, false, tag!("abcd")) 793 /// } 794 /// 795 /// let a = b"abcdef"; 796 /// assert_eq!(f_true(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); 797 /// 798 /// assert_eq!(f_false(&a[..]), Ok((&b"abcdef"[..], None))); 799 /// # } 800 /// ``` 801 /// 802 #[macro_export(local_inner_macros)] 803 macro_rules! cond( 804 ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => ( 805 $crate::combinator::condc($i, $cond, |i| $submac!(i, $($args)*) ) 806 ); 807 ($i:expr, $cond:expr, $f:expr) => ( 808 $crate::combinator::cond($cond, $f)($i) 809 ); 810 ); 811 812 /// `peek!(I -> IResult<I,O>) => I -> IResult<I, O>` 813 /// returns a result without consuming the input 814 /// 815 /// the embedded parser may return Err(Err::Incomplete 816 /// 817 /// ``` 818 /// # #[macro_use] extern crate nom; 819 /// # fn main() { 820 /// named!(ptag, peek!( tag!( "abcd" ) ) ); 821 /// 822 /// let r = ptag(&b"abcdefgh"[..]); 823 /// assert_eq!(r, Ok((&b"abcdefgh"[..], &b"abcd"[..]))); 824 /// # } 825 /// ``` 826 #[macro_export(local_inner_macros)] 827 macro_rules! peek( 828 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 829 $crate::combinator::peekc($i, |i| $submac!(i, $($args)*)) 830 ); 831 ($i:expr, $f:expr) => ( 832 $crate::combinator::peek($f)($i) 833 ); 834 ); 835 836 /// `not!(I -> IResult<I,O>) => I -> IResult<I, ()>` 837 /// returns a result only if the embedded parser returns Error or Err(Err::Incomplete) 838 /// does not consume the input 839 /// 840 /// ``` 841 /// # #[macro_use] extern crate nom; 842 /// # use nom::Err; 843 /// # use nom::error::ErrorKind; 844 /// # fn main() { 845 /// named!(not_e, do_parse!( 846 /// res: tag!("abc") >> 847 /// not!(char!('e')) >> 848 /// (res) 849 /// )); 850 /// 851 /// let r = not_e(&b"abcd"[..]); 852 /// assert_eq!(r, Ok((&b"d"[..], &b"abc"[..]))); 853 /// 854 /// let r2 = not_e(&b"abce"[..]); 855 /// assert_eq!(r2, Err(Err::Error(error_position!(&b"e"[..], ErrorKind::Not)))); 856 /// # } 857 /// ``` 858 #[macro_export(local_inner_macros)] 859 macro_rules! not( 860 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 861 $crate::combinator::notc($i, |i| $submac!(i, $($args)*)) 862 ); 863 ($i:expr, $f:expr) => ( 864 $crate::combinator::not($f)($i) 865 ); 866 ); 867 868 /// `tap!(name: I -> IResult<I,O> => { block }) => I -> IResult<I, O>` 869 /// allows access to the parser's result without affecting it 870 /// 871 /// ``` 872 /// # #[macro_use] extern crate nom; 873 /// # use std::str; 874 /// # fn main() { 875 /// named!(ptag, tap!(res: tag!( "abcd" ) => { println!("recognized {}", str::from_utf8(res).unwrap()) } ) ); 876 /// 877 /// let r = ptag(&b"abcdefgh"[..]); 878 /// assert_eq!(r, Ok((&b"efgh"[..], &b"abcd"[..]))); 879 /// # } 880 /// ``` 881 #[macro_export(local_inner_macros)] 882 macro_rules! tap ( 883 ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => ( 884 { 885 use $crate::lib::std::result::Result::*; 886 use $crate::{Err,Needed,IResult}; 887 888 match $submac!($i, $($args)*) { 889 Ok((i,o)) => { 890 let $name = o; 891 $e; 892 Ok((i, $name)) 893 }, 894 Err(e) => Err(Err::convert(e)), 895 } 896 } 897 ); 898 ($i:expr, $name: ident: $f:expr => $e:expr) => ( 899 tap!($i, $name: call!($f) => $e); 900 ); 901 ); 902 903 /// `eof!()` returns its input if it is at the end of input data 904 /// 905 /// When we're at the end of the data, this combinator 906 /// will succeed 907 /// 908 /// 909 /// ``` 910 /// # #[macro_use] extern crate nom; 911 /// # use std::str; 912 /// # use nom::{Err, error::ErrorKind}; 913 /// # fn main() { 914 /// named!(parser, eof!()); 915 /// 916 /// assert_eq!(parser(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof)))); 917 /// assert_eq!(parser(&b""[..]), Ok((&b""[..], &b""[..]))); 918 /// # } 919 /// ``` 920 #[macro_export(local_inner_macros)] 921 macro_rules! eof ( 922 ($i:expr,) => ( 923 { 924 use $crate::lib::std::result::Result::*; 925 use $crate::{Err,error::ErrorKind}; 926 927 use $crate::InputLength; 928 if ($i).input_len() == 0 { 929 Ok(($i, $i)) 930 } else { 931 Err(Err::Error(error_position!($i, ErrorKind::Eof))) 932 } 933 } 934 ); 935 ); 936 937 /// `exact!()` will fail if the child parser does not consume the whole data 938 /// 939 /// TODO: example 940 #[macro_export(local_inner_macros)] 941 macro_rules! exact ( 942 ($i:expr, $submac:ident!( $($args:tt)* )) => ({ 943 terminated!($i, $submac!( $($args)*), eof!()) 944 }); 945 ($i:expr, $f:expr) => ( 946 exact!($i, call!($f)); 947 ); 948 ); 949 950 /// `recognize!(I -> IResult<I, O> ) => I -> IResult<I, I>` 951 /// if the child parser was successful, return the consumed input as produced value 952 /// 953 /// ``` 954 /// # #[macro_use] extern crate nom; 955 /// # fn main() { 956 /// named!(x, recognize!(delimited!(tag!("<!--"), take!(5), tag!("-->")))); 957 /// let r = x(&b"<!-- abc --> aaa"[..]); 958 /// assert_eq!(r, Ok((&b" aaa"[..], &b"<!-- abc -->"[..]))); 959 /// # } 960 /// ``` 961 #[macro_export(local_inner_macros)] 962 macro_rules! recognize ( 963 ($i:expr, $submac:ident!( $($args:tt)* )) => ( 964 $crate::combinator::recognizec($i, |i| $submac!(i, $($args)*)) 965 ); 966 ($i:expr, $f:expr) => ( 967 $crate::combinator::recognize($f)($i) 968 ); 969 ); 970 971 #[cfg(test)] 972 mod tests { 973 use crate::internal::{Err, IResult, Needed}; 974 use crate::error::ParseError; 975 use crate::error::ErrorKind; 976 #[cfg(feature = "alloc")] 977 use crate::lib::std::boxed::Box; 978 979 // reproduce the tag and take macros, because of module import order 980 macro_rules! tag ( 981 ($i:expr, $tag: expr) => ({ 982 use $crate::lib::std::result::Result::*; 983 use $crate::{Err,Needed,IResult,error::ErrorKind}; 984 use $crate::{Compare,CompareResult,InputLength,Slice}; 985 986 let res: IResult<_,_> = match ($i).compare($tag) { 987 CompareResult::Ok => { 988 let blen = $tag.input_len(); 989 Ok(($i.slice(blen..), $i.slice(..blen))) 990 }, 991 CompareResult::Incomplete => { 992 Err(Err::Incomplete(Needed::Size($tag.input_len()))) 993 }, 994 CompareResult::Error => { 995 let e:ErrorKind = ErrorKind::Tag; 996 Err(Err::Error($crate::error::make_error($i, e))) 997 } 998 }; 999 res 1000 }); 1001 ); 1002 1003 macro_rules! take( 1004 ($i:expr, $count:expr) => ( 1005 { 1006 let cnt = $count as usize; 1007 let res:IResult<&[u8],&[u8]> = if $i.len() < cnt { 1008 Err($crate::Err::Incomplete($crate::Needed::Size(cnt))) 1009 } else { 1010 Ok((&$i[cnt..],&$i[0..cnt])) 1011 }; 1012 res 1013 } 1014 ); 1015 ); 1016 1017 mod pub_named_mod { 1018 named!(pub tst, tag!("abcd")); 1019 } 1020 1021 #[test] pub_named_test()1022 fn pub_named_test() { 1023 let a = &b"abcd"[..]; 1024 let res = pub_named_mod::tst(a); 1025 assert_eq!(res, Ok((&b""[..], a))); 1026 } 1027 1028 mod pub_crate_named_mod { 1029 named!(pub(crate) tst, tag!("abcd")); 1030 } 1031 1032 #[test] pub_crate_named_test()1033 fn pub_crate_named_test() { 1034 let a = &b"abcd"[..]; 1035 let res = pub_crate_named_mod::tst(a); 1036 assert_eq!(res, Ok((&b""[..], a))); 1037 } 1038 1039 #[test] apply_test()1040 fn apply_test() { 1041 fn sum2(a: u8, b: u8) -> u8 { 1042 a + b 1043 } 1044 fn sum3(a: u8, b: u8, c: u8) -> u8 { 1045 a + b + c 1046 } 1047 let a = call!(1, sum2, 2); 1048 let b = call!(1, sum3, 2, 3); 1049 1050 assert_eq!(a, 3); 1051 assert_eq!(b, 6); 1052 } 1053 1054 #[test] opt()1055 fn opt() { 1056 named!(opt_abcd<&[u8],Option<&[u8]> >, opt!(tag!("abcd"))); 1057 1058 let a = &b"abcdef"[..]; 1059 let b = &b"bcdefg"[..]; 1060 let c = &b"ab"[..]; 1061 assert_eq!(opt_abcd(a), Ok((&b"ef"[..], Some(&b"abcd"[..])))); 1062 assert_eq!(opt_abcd(b), Ok((&b"bcdefg"[..], None))); 1063 assert_eq!(opt_abcd(c), Err(Err::Incomplete(Needed::Size(4)))); 1064 } 1065 1066 #[test] opt_res()1067 fn opt_res() { 1068 named!(opt_res_abcd<&[u8], Result<&[u8], Err<(&[u8], ErrorKind)>> >, opt_res!(tag!("abcd"))); 1069 1070 let a = &b"abcdef"[..]; 1071 let b = &b"bcdefg"[..]; 1072 let c = &b"ab"[..]; 1073 assert_eq!(opt_res_abcd(a), Ok((&b"ef"[..], Ok(&b"abcd"[..])))); 1074 assert_eq!( 1075 opt_res_abcd(b), 1076 Ok(( 1077 &b"bcdefg"[..], 1078 Err(Err::Error(error_position!(b, ErrorKind::Tag))) 1079 )) 1080 ); 1081 assert_eq!(opt_res_abcd(c), Err(Err::Incomplete(Needed::Size(4)))); 1082 } 1083 1084 use crate::lib::std::convert::From; 1085 #[derive(Debug, PartialEq)] 1086 pub struct CustomError(&'static str); 1087 impl<I> From<(I, ErrorKind)> for CustomError { from(_: (I, ErrorKind)) -> Self1088 fn from(_: (I, ErrorKind)) -> Self { 1089 CustomError("test") 1090 } 1091 } 1092 1093 impl<I> ParseError<I> for CustomError { from_error_kind(_: I, _: ErrorKind) -> Self1094 fn from_error_kind(_: I, _: ErrorKind) -> Self { 1095 CustomError("from_error_kind") 1096 } 1097 append(_: I, _: ErrorKind, _: CustomError) -> Self1098 fn append(_: I, _: ErrorKind, _: CustomError) -> Self { 1099 CustomError("append") 1100 } 1101 } 1102 1103 1104 #[test] 1105 #[cfg(feature = "alloc")] cond()1106 fn cond() { 1107 fn f_true(i: &[u8]) -> IResult<&[u8], Option<&[u8]>, CustomError> { 1108 fix_error!(i, CustomError, cond!(true, tag!("abcd"))) 1109 } 1110 1111 fn f_false(i: &[u8]) -> IResult<&[u8], Option<&[u8]>, CustomError> { 1112 fix_error!(i, CustomError, cond!(false, tag!("abcd"))) 1113 } 1114 1115 assert_eq!(f_true(&b"abcdef"[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); 1116 assert_eq!(f_true(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); 1117 assert_eq!(f_true(&b"xxx"[..]), Err(Err::Error(CustomError("test")))); 1118 1119 assert_eq!(f_false(&b"abcdef"[..]), Ok((&b"abcdef"[..], None))); 1120 assert_eq!(f_false(&b"ab"[..]), Ok((&b"ab"[..], None))); 1121 assert_eq!(f_false(&b"xxx"[..]), Ok((&b"xxx"[..], None))); 1122 } 1123 1124 #[test] 1125 #[cfg(feature = "alloc")] cond_wrapping()1126 fn cond_wrapping() { 1127 // Test that cond!() will wrap a given identifier in the call!() macro. 1128 named!(tag_abcd, tag!("abcd")); 1129 fn f_true(i: &[u8]) -> IResult<&[u8], Option<&[u8]>, CustomError> { 1130 fix_error!(i, CustomError, cond!(true, tag_abcd)) 1131 } 1132 1133 fn f_false(i: &[u8]) -> IResult<&[u8], Option<&[u8]>, CustomError> { 1134 fix_error!(i, CustomError, cond!(false, tag_abcd)) 1135 } 1136 1137 assert_eq!(f_true(&b"abcdef"[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); 1138 assert_eq!(f_true(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); 1139 assert_eq!(f_true(&b"xxx"[..]), Err(Err::Error(CustomError("test")))); 1140 1141 assert_eq!(f_false(&b"abcdef"[..]), Ok((&b"abcdef"[..], None))); 1142 assert_eq!(f_false(&b"ab"[..]), Ok((&b"ab"[..], None))); 1143 assert_eq!(f_false(&b"xxx"[..]), Ok((&b"xxx"[..], None))); 1144 } 1145 1146 #[test] peek()1147 fn peek() { 1148 named!(peek_tag<&[u8],&[u8]>, peek!(tag!("abcd"))); 1149 1150 assert_eq!(peek_tag(&b"abcdef"[..]), Ok((&b"abcdef"[..], &b"abcd"[..]))); 1151 assert_eq!(peek_tag(&b"ab"[..]), Err(Err::Incomplete(Needed::Size(4)))); 1152 assert_eq!( 1153 peek_tag(&b"xxx"[..]), 1154 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag))) 1155 ); 1156 } 1157 1158 #[test] not()1159 fn not() { 1160 named!(not_aaa<()>, not!(tag!("aaa"))); 1161 assert_eq!( 1162 not_aaa(&b"aaa"[..]), 1163 Err(Err::Error(error_position!(&b"aaa"[..], ErrorKind::Not))) 1164 ); 1165 assert_eq!(not_aaa(&b"aa"[..]), Err(Err::Incomplete(Needed::Size(3)))); 1166 assert_eq!(not_aaa(&b"abcd"[..]), Ok((&b"abcd"[..], ()))); 1167 } 1168 1169 #[test] verify()1170 fn verify() { 1171 named!(test, verify!(take!(5), |slice: &[u8]| slice[0] == b'a')); 1172 assert_eq!(test(&b"bcd"[..]), Err(Err::Incomplete(Needed::Size(5)))); 1173 assert_eq!( 1174 test(&b"bcdefg"[..]), 1175 Err(Err::Error(error_position!( 1176 &b"bcdefg"[..], 1177 ErrorKind::Verify 1178 ))) 1179 ); 1180 assert_eq!(test(&b"abcdefg"[..]), Ok((&b"fg"[..], &b"abcde"[..]))); 1181 } 1182 1183 #[test] parse_to()1184 fn parse_to() { 1185 let res: IResult<_, _, (&str, ErrorKind)> = parse_to!("ab", usize); 1186 1187 assert_eq!( 1188 res, 1189 Err(Err::Error(error_position!( 1190 "ab", 1191 ErrorKind::ParseTo 1192 ))) 1193 ); 1194 1195 let res: IResult<_, _, (&str, ErrorKind)> = parse_to!("42", usize); 1196 1197 assert_eq!(res, Ok(("", 42))); 1198 //assert_eq!(ErrorKind::convert(ErrorKind::ParseTo), ErrorKind::ParseTo::<u64>); 1199 } 1200 1201 } 1202