Skip to content

Commit

Permalink
feat: Add the produce parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Dec 25, 2019
1 parent 2f533f0 commit ac15b87
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<a name=""></a>
## (2019-12-24)




<a name="4.0.0-beta.2"></a>
## 4.0.0-beta.2 (2019-12-19)

Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ pub use crate::parser::{
sep_end_by1, skip_count, skip_count_min_max, skip_many, skip_many1,
},
sequence::between,
token::{any, eof, none_of, one_of, position, satisfy, satisfy_map, token, tokens, value},
token::{
any, eof, none_of, one_of, position, produce, satisfy, satisfy_map, token, tokens, value,
},
};

#[doc(inline)]
Expand Down
38 changes: 38 additions & 0 deletions src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,44 @@ where
Value(v, PhantomData)
}

#[derive(Copy, Clone)]
pub struct Produce<Input, F>(F, PhantomData<fn(Input) -> Input>);
impl<Input, F, R> Parser<Input> for Produce<Input, F>
where
Input: Stream,
F: FnMut() -> R,
{
type Output = R;
type PartialState = ();
#[inline]
fn parse_lazy(&mut self, _: &mut Input) -> ParseResult<R, Input::Error> {
PeekOk((self.0)())
}
}

/// Always returns the value produced by calling `f`.
///
/// Can be used when `value` is unable to be used for lack of `Clone` implementation on the value.
///
/// ```
/// # use combine::*;
/// # fn main() {
/// #[derive(Debug, PartialEq)]
/// struct NoClone;
/// let result = produce(|| vec![NoClone])
/// .parse("hello world")
/// .map(|x| x.0);
/// assert_eq!(result, Ok(vec![NoClone]));
/// # }
/// ```
pub fn produce<Input, F, R>(f: F) -> Produce<Input, F>
where
Input: Stream,
F: FnMut() -> R,
{
Produce(f, PhantomData)
}

#[derive(Copy, Clone)]
pub struct Eof<Input>(PhantomData<Input>);
impl<Input> Parser<Input> for Eof<Input>
Expand Down

0 comments on commit ac15b87

Please sign in to comment.