Skip to content
This repository has been archived by the owner on Dec 18, 2019. It is now read-only.

Commit

Permalink
Implement InputTakeAtPosition for Span
Browse files Browse the repository at this point in the history
  • Loading branch information
subhojit777 committed Sep 21, 2018
1 parent 8e84f42 commit 821f857
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions source/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ use nom::{
InputIter,
InputLength,
InputTake,
InputTakeAtPosition,
IResult,
Err,
Needed,
Context,
ErrorKind,
Offset,
Slice
};
Expand Down Expand Up @@ -825,6 +831,51 @@ pub struct Span<'a> {
slice: Input<'a>
}

impl<'a> InputTakeAtPosition for Span<'a> {
type Item = InputElement;

fn split_at_position<P>(&self, predicate: P) -> IResult<Self, Self, u32>
where
P: Fn(Self::Item) -> bool,
{
match (0..self.slice.len()).find(|b| predicate(self.slice[*b])) {
Some(i) => Ok((Span {
offset: self.offset,
line: self.line,
column: self.column,
slice: &self.slice[i..],
}, Span {
offset: self.offset,
line: self.line,
column: self.column,
slice: &self.slice[..i],
})),
None => Err(Err::Incomplete(Needed::Size(1))),
}
}

fn split_at_position1<P>(&self, predicate: P, e: ErrorKind<u32>) -> IResult<Self, Self, u32>
where
P: Fn(Self::Item) -> bool,
{
match (0..self.slice.len()).find(|b| predicate(self.slice[*b])) {
Some(0) => Err(Err::Error(Context::Code(*self, e))),
Some(i) => Ok((Span {
offset: self.offset,
line: self.line,
column: self.column,
slice: &self.slice[i..],
}, Span {
offset: self.offset,
line: self.line,
column: self.column,
slice: &self.slice[..i],
})),
None => Err(Err::Incomplete(Needed::Size(1))),
}
}
}

impl<'a> InputTake for Span<'a> {
fn take(&self, count: usize) -> Self {
Span {
Expand Down

0 comments on commit 821f857

Please sign in to comment.