Skip to content

Commit

Permalink
feat: Add the position parser
Browse files Browse the repository at this point in the history
The position parser just returns the current position in the stream without consuming anything
  • Loading branch information
Marwes committed Oct 19, 2016
1 parent 608cdde commit d6c65f6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
42 changes: 42 additions & 0 deletions src/combinator.rs
Expand Up @@ -325,6 +325,48 @@ pub fn tokens<C, T, I>(cmp: C, expected: Info<I::Item, I::Range>, tokens: T) ->
}
}


#[derive(Clone)]
pub struct Position<I>
where I: Stream
{
_marker: PhantomData<I>,
}

impl<I> Parser for Position<I>
where I: Stream
{
type Input = I;
type Output = I::Position;

#[inline]
fn parse_lazy(&mut self, input: I) -> ConsumedResult<I::Position, I> {
EmptyOk((input.position(), input))
}
}

/// Parser which just returns the current position in the stream
///
/// ```
/// # extern crate combine;
/// # use combine::*;
/// # use combine::primitives::SourcePosition;
/// # fn main() {
/// let result = (position(), token('!'), position())
/// .parse(State::new("!"))
/// .map(|x| x.0);
/// assert_eq!(result, Ok((SourcePosition { line: 1, column: 1 },
/// '!',
/// SourcePosition { line: 1, column: 2 })));
/// # }
/// ```
#[inline(always)]
pub fn position<I>() -> Position<I>
where I: Stream
{
Position { _marker: PhantomData }
}

#[derive(Clone)]
pub struct Choice<S, P>(S, PhantomData<P>);

Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Expand Up @@ -139,9 +139,9 @@ pub use primitives::{Parser, ParseError, ConsumedResult, ParseResult, State, fro
StreamOnce};
#[doc(inline)]
pub use combinator::{any, between, chainl1, chainr1, choice, eof, env_parser, many, many1,
optional, parser, satisfy, satisfy_map, sep_by, sep_by1, sep_end_by,
sep_end_by1, skip_many, skip_many1, token, tokens, try, look_ahead, value,
unexpected, not_followed_by};
optional, parser, position, satisfy, satisfy_map, sep_by, sep_by1,
sep_end_by, sep_end_by1, skip_many, skip_many1, token, tokens, try,
look_ahead, value, unexpected, not_followed_by};

macro_rules! static_fn {
(($($arg: pat, $arg_ty: ty),*) -> $ret: ty { $body: expr }) => { {
Expand Down

0 comments on commit d6c65f6

Please sign in to comment.