Skip to content

Commit 4434f7f

Browse files
committed
last code examples
remove expr_opt and expr_res
1 parent ab5020c commit 4434f7f

File tree

3 files changed

+53
-77
lines changed

3 files changed

+53
-77
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
- `take_until_and_consume`, `take_until_and_consume1`: they can be replaced with `take_until` combined with `take`
3333
- `sized_buffer` and `length_bytes!`: they can be replaced with the `length_data` function
3434
- `non_empty`, `begin` and `rest_s` function
35-
- `cond_reduce!`, `cond_with_error!`, `closure!`, `apply`, `map_res_err!`
35+
- `cond_reduce!`, `cond_with_error!`, `closure!`, `apply`, `map_res_err!`, `expr_opt!`, `expr_res!`
3636
- `alt_complete`, `separated_list_complete`, `separated_nonempty_list_complete`
3737

3838
## 4.2.3 - 2019-03-23

src/combinator/macros.rs

Lines changed: 52 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,9 @@ macro_rules! complete (
447447
/// # use nom::IResult;
448448
///
449449
/// 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));
454453
/// }
455454
/// # fn main() {
456455
/// let arr1 = [1, 2, 3, 4, 5];
@@ -460,7 +459,7 @@ macro_rules! complete (
460459
/// let arr2 = [0xFE, 2, 3, 4, 5];
461460
/// // size is overflowing
462461
/// 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))));
464463
/// # }
465464
/// ```
466465
#[macro_export(local_inner_macros)]
@@ -554,6 +553,25 @@ macro_rules! map_res (
554553

555554
/// `map_opt!(I -> IResult<I, O>, O -> Option<P>) => I -> IResult<I, P>`
556555
/// 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+
/// ```
557575
#[macro_export(local_inner_macros)]
558576
macro_rules! map_opt (
559577
// Internal parser, do not use directly
@@ -579,6 +597,23 @@ macro_rules! map_opt (
579597
/// input to the specified type
580598
///
581599
/// 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+
/// ```
582617
#[macro_export(local_inner_macros)]
583618
macro_rules! parse_to (
584619
($i:expr, $t:ty ) => (
@@ -670,70 +705,6 @@ macro_rules! value (
670705
);
671706
);
672707

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-
737708
/// `opt!(I -> IResult<I,O>) => I -> IResult<I, Option<O>>`
738709
/// make the underlying parser optional
739710
///
@@ -947,7 +918,18 @@ macro_rules! tap (
947918
/// When we're at the end of the data, this combinator
948919
/// will succeed
949920
///
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+
/// ```
951933
#[macro_export(local_inner_macros)]
952934
macro_rules! eof (
953935
($i:expr,) => (

src/error.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ pub enum ErrorKind {
169169
MultiSpace,
170170
LengthValueFn,
171171
Eof,
172-
ExprOpt,
173-
ExprRes,
174172
Switch,
175173
TagBits,
176174
OneOf,
@@ -226,8 +224,6 @@ pub fn error_to_u32(e: &ErrorKind) -> u32 {
226224
ErrorKind::MultiSpace => 21,
227225
ErrorKind::LengthValueFn => 22,
228226
ErrorKind::Eof => 23,
229-
ErrorKind::ExprOpt => 24,
230-
ErrorKind::ExprRes => 25,
231227
ErrorKind::Switch => 27,
232228
ErrorKind::TagBits => 28,
233229
ErrorKind::OneOf => 29,
@@ -290,8 +286,6 @@ impl ErrorKind {
290286
ErrorKind::MultiSpace => "Multiple spaces",
291287
ErrorKind::LengthValueFn => "LengthValueFn",
292288
ErrorKind::Eof => "End of file",
293-
ErrorKind::ExprOpt => "Evaluate Option",
294-
ErrorKind::ExprRes => "Evaluate Result",
295289
ErrorKind::Switch => "Switch",
296290
ErrorKind::TagBits => "Tag on bitstream",
297291
ErrorKind::OneOf => "OneOf",

0 commit comments

Comments
 (0)