@@ -447,10 +447,9 @@ macro_rules! complete (
447
447
/// # use nom::IResult;
448
448
///
449
449
/// fn take_add(input:&[u8], size: u8) -> IResult<&[u8], &[u8]> {
450
- /// let (i1, sz) = try_parse!(input, nom::number::streaming::be_u8);
451
- /// let (i2, length) = try_parse!(i1, expr_opt!(size.checked_add(sz)));
452
- /// let (i3, data) = try_parse!(i2, take!(length));
453
- /// return Ok((i3, data));
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));
454
453
/// }
455
454
/// # fn main() {
456
455
/// let arr1 = [1, 2, 3, 4, 5];
@@ -460,7 +459,7 @@ macro_rules! complete (
460
459
/// let arr2 = [0xFE, 2, 3, 4, 5];
461
460
/// // size is overflowing
462
461
/// let r1 = take_add(&arr2[..], 42);
463
- /// assert_eq!(r1, Err(Err::Error(error_position!(&[2,3,4,5][..], ErrorKind::ExprOpt ))));
462
+ /// assert_eq!(r1, Err(Err::Error(error_position!(&[254, 2,3,4,5][..], ErrorKind::MapOpt ))));
464
463
/// # }
465
464
/// ```
466
465
#[ macro_export( local_inner_macros) ]
@@ -554,6 +553,25 @@ macro_rules! map_res (
554
553
555
554
/// `map_opt!(I -> IResult<I, O>, O -> Option<P>) => I -> IResult<I, P>`
556
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
+ /// ```
557
575
#[ macro_export( local_inner_macros) ]
558
576
macro_rules! map_opt (
559
577
// Internal parser, do not use directly
@@ -579,6 +597,23 @@ macro_rules! map_opt (
579
597
/// input to the specified type
580
598
///
581
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
+ /// ```
582
617
#[ macro_export( local_inner_macros) ]
583
618
macro_rules! parse_to (
584
619
( $i: expr, $t: ty ) => (
@@ -670,70 +705,6 @@ macro_rules! value (
670
705
) ;
671
706
) ;
672
707
673
- /// `expr_res!(Result<E, O>) => I -> IResult<I, O>`
674
- /// evaluate an expression that returns a Result<T, E> and returns a Ok((I, T)) if Ok
675
- ///
676
- /// See expr_opt for an example
677
- #[ macro_export( local_inner_macros) ]
678
- macro_rules! expr_res (
679
- ( $i: expr, $e: expr) => (
680
- {
681
- use $crate:: lib:: std:: result:: Result :: * ;
682
- use $crate:: { Err , error:: ErrorKind } ;
683
-
684
- match $e {
685
- Ok ( output) => Ok ( ( $i, output) ) ,
686
- Err ( _) => Err ( Err :: Error ( error_position!( $i, ErrorKind :: ExprRes ) ) )
687
- }
688
- }
689
- ) ;
690
- ) ;
691
-
692
- /// `expr_opt!(Option<O>) => I -> IResult<I, O>`
693
- /// evaluate an expression that returns a Option<T> and returns a Ok((I,T)) if Some
694
- ///
695
- /// Useful when doing computations in a chain
696
- ///
697
- /// ```
698
- /// # #[macro_use] extern crate nom;
699
- /// # use nom::Err;
700
- /// # use nom::IResult;
701
- /// # use nom::{number::streaming::be_u8, error::ErrorKind};
702
- ///
703
- /// fn take_add(input:&[u8], size: u8) -> IResult<&[u8], &[u8]> {
704
- /// do_parse!(input,
705
- /// sz: be_u8 >>
706
- /// length: expr_opt!(size.checked_add(sz)) >> // checking for integer overflow (returns an Option)
707
- /// data: take!(length) >>
708
- /// (data)
709
- /// )
710
- /// }
711
- /// # fn main() {
712
- /// let arr1 = [1, 2, 3, 4, 5];
713
- /// let r1 = take_add(&arr1[..], 1);
714
- /// assert_eq!(r1, Ok((&[4,5][..], &[2,3][..])));
715
- ///
716
- /// let arr2 = [0xFE, 2, 3, 4, 5];
717
- /// // size is overflowing
718
- /// let r1 = take_add(&arr2[..], 42);
719
- /// assert_eq!(r1, Err(Err::Error(error_position!(&[2,3,4,5][..], ErrorKind::ExprOpt))));
720
- /// # }
721
- /// ```
722
- #[ macro_export( local_inner_macros) ]
723
- macro_rules! expr_opt (
724
- ( $i: expr, $e: expr) => (
725
- {
726
- use $crate:: lib:: std:: result:: Result :: * ;
727
- use $crate:: { Err , error:: ErrorKind } ;
728
-
729
- match $e {
730
- $crate:: lib:: std:: option:: Option :: Some ( output) => Ok ( ( $i, output) ) ,
731
- $crate:: lib:: std:: option:: Option :: None => Err ( Err :: Error ( error_position!( $i, ErrorKind :: ExprOpt ) ) )
732
- }
733
- }
734
- ) ;
735
- ) ;
736
-
737
708
/// `opt!(I -> IResult<I,O>) => I -> IResult<I, Option<O>>`
738
709
/// make the underlying parser optional
739
710
///
@@ -947,7 +918,18 @@ macro_rules! tap (
947
918
/// When we're at the end of the data, this combinator
948
919
/// will succeed
949
920
///
950
- /// TODO: example
921
+ ///
922
+ /// ```
923
+ /// # #[macro_use] extern crate nom;
924
+ /// # use std::str;
925
+ /// # use nom::{Err, error::ErrorKind};
926
+ /// # fn main() {
927
+ /// named!(parser, eof!());
928
+ ///
929
+ /// assert_eq!(parser(&b"abc"[..]), Err(Err::Error((&b"abc"[..], ErrorKind::Eof))));
930
+ /// assert_eq!(parser(&b""[..]), Ok((&b""[..], &b""[..])));
931
+ /// # }
932
+ /// ```
951
933
#[ macro_export( local_inner_macros) ]
952
934
macro_rules! eof (
953
935
( $i: expr, ) => (
0 commit comments