Skip to content

Commit

Permalink
Merge pull request #257 from epage/from
Browse files Browse the repository at this point in the history
fix(stream)!: Prep for `Checkpoint`s
  • Loading branch information
epage authored Jun 25, 2023
2 parents 7a414ab + 314efb5 commit f864c0f
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 84 deletions.
2 changes: 1 addition & 1 deletion examples/ndjson/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn main() -> Result<(), lexopt::Error> {
println!("{:?}", value);
println!();
// Tell the buffer how much we read
let consumed = input.offset_to(&remainder);
let consumed = remainder.offset_from(&input);
buffer.consume(consumed);
}
Err(ErrMode::Backtrack(e)) | Err(ErrMode::Cut(e)) => {
Expand Down
28 changes: 14 additions & 14 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ where
c == '\r' || c == '\n'
}) {
None if PARTIAL && input.is_partial() => Err(ErrMode::Incomplete(Needed::Unknown)),
None => Ok(input.next_slice(input.eof_offset())),
None => Ok(input.finish()),
Some(offset) => {
let (new_input, res) = input.next_slice(offset);
let bytes = new_input.as_bstr();
Expand Down Expand Up @@ -944,7 +944,7 @@ where
if <I as StreamIsPartial>::is_partial_supported() && input.is_partial() {
Err(ErrMode::Incomplete(Needed::new(1)))
} else {
Ok((input.next_slice(input.eof_offset()).0, value))
Ok((input.finish().0, value))
}
})
.parse_next(input)
Expand Down Expand Up @@ -1109,7 +1109,7 @@ where
if <I as StreamIsPartial>::is_partial_supported() && input.is_partial() {
Err(ErrMode::Incomplete(Needed::new(1)))
} else {
Ok((input.next_slice(input.eof_offset()).0, value))
Ok((input.finish().0, value))
}
})
.parse_next(input)
Expand Down Expand Up @@ -1480,7 +1480,7 @@ where
if i2.eof_offset() == 0 {
return Err(ErrMode::Incomplete(Needed::Unknown));
} else if i2.eof_offset() == current_len {
let offset = input.offset_to(&i2);
let offset = i2.offset_from(&input);
return Ok(input.next_slice(offset));
} else {
i = i2;
Expand All @@ -1496,7 +1496,7 @@ where
i = i2;
}
} else {
let offset = input.offset_to(&i);
let offset = i.offset_from(&input);
return Ok(input.next_slice(offset));
}
}
Expand Down Expand Up @@ -1535,7 +1535,7 @@ where
if i2.eof_offset() == 0 {
return Ok(input.next_slice(input.eof_offset()));
} else if i2.eof_offset() == current_len {
let offset = input.offset_to(&i2);
let offset = i2.offset_from(&input);
return Ok(input.next_slice(offset));
} else {
i = i2;
Expand All @@ -1546,12 +1546,12 @@ where
let next = control_char.len_utf8();
let (i2, _) = escapable.parse_next(i.next_slice(next).0)?;
if i2.eof_offset() == 0 {
return Ok(input.next_slice(input.eof_offset()));
return Ok(input.finish());
} else {
i = i2;
}
} else {
let offset = input.offset_to(&i);
let offset = i.offset_from(&input);
return Ok(input.next_slice(offset));
}
}
Expand All @@ -1561,7 +1561,7 @@ where
}
}

Ok(input.next_slice(input.eof_offset()))
Ok(input.finish())
}

/// Matches a byte string with escaped characters.
Expand Down Expand Up @@ -1680,7 +1680,7 @@ where
} else if i2.eof_offset() == current_len {
return Ok((remainder, res));
} else {
offset = input.offset_to(&i2);
offset = i2.offset_from(&input);
}
}
Err(ErrMode::Backtrack(_)) => {
Expand All @@ -1691,7 +1691,7 @@ where
if i2.eof_offset() == 0 {
return Err(ErrMode::Incomplete(Needed::Unknown));
} else {
offset = input.offset_to(&i2);
offset = i2.offset_from(&input);
}
} else {
return Ok((remainder, res));
Expand Down Expand Up @@ -1735,7 +1735,7 @@ where
} else if i2.eof_offset() == current_len {
return Ok((remainder, res));
} else {
offset = input.offset_to(&i2);
offset = i2.offset_from(&input);
}
}
Err(ErrMode::Backtrack(_)) => {
Expand All @@ -1744,9 +1744,9 @@ where
let (i2, o) = transform.parse_next(i.next_slice(next).0)?;
res.accumulate(o);
if i2.eof_offset() == 0 {
return Ok((i.next_slice(i.eof_offset()).0, res));
return Ok((i.finish().0, res));
} else {
offset = input.offset_to(&i2);
offset = i2.offset_from(&input);
}
} else {
return Ok((remainder, res));
Expand Down
28 changes: 14 additions & 14 deletions src/ascii/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,43 +155,43 @@ mod complete {

match alpha1::<_, Error<_>>(a) {
Ok((i, _)) => {
assert_eq!(a.offset_to(i) + i.len(), a.len());
assert_eq!(i.offset_from(a) + i.len(), a.len());
}
_ => panic!("wrong return type in offset test for alpha"),
}
match digit1::<_, Error<_>>(b) {
Ok((i, _)) => {
assert_eq!(b.offset_to(i) + i.len(), b.len());
assert_eq!(i.offset_from(b) + i.len(), b.len());
}
_ => panic!("wrong return type in offset test for digit"),
}
match alphanumeric1::<_, Error<_>>(c) {
Ok((i, _)) => {
assert_eq!(c.offset_to(i) + i.len(), c.len());
assert_eq!(i.offset_from(c) + i.len(), c.len());
}
_ => panic!("wrong return type in offset test for alphanumeric"),
}
match space1::<_, Error<_>>(d) {
Ok((i, _)) => {
assert_eq!(d.offset_to(i) + i.len(), d.len());
assert_eq!(i.offset_from(d) + i.len(), d.len());
}
_ => panic!("wrong return type in offset test for space"),
}
match multispace1::<_, Error<_>>(e) {
Ok((i, _)) => {
assert_eq!(e.offset_to(i) + i.len(), e.len());
assert_eq!(i.offset_from(e) + i.len(), e.len());
}
_ => panic!("wrong return type in offset test for multispace"),
}
match hex_digit1::<_, Error<_>>(f) {
Ok((i, _)) => {
assert_eq!(f.offset_to(i) + i.len(), f.len());
assert_eq!(i.offset_from(f) + i.len(), f.len());
}
_ => panic!("wrong return type in offset test for hex_digit"),
}
match oct_digit1::<_, Error<_>>(f) {
Ok((i, _)) => {
assert_eq!(f.offset_to(i) + i.len(), f.len());
assert_eq!(i.offset_from(f) + i.len(), f.len());
}
_ => panic!("wrong return type in offset test for oct_digit"),
}
Expand Down Expand Up @@ -1099,49 +1099,49 @@ mod partial {
match alpha1::<_, Error<_>>(Partial::new(a)) {
Ok((i, _)) => {
let i = i.into_inner();
assert_eq!(a.offset_to(i) + i.len(), a.len());
assert_eq!(i.offset_from(a) + i.len(), a.len());
}
_ => panic!("wrong return type in offset test for alpha"),
}
match digit1::<_, Error<_>>(Partial::new(b)) {
Ok((i, _)) => {
let i = i.into_inner();
assert_eq!(b.offset_to(i) + i.len(), b.len());
assert_eq!(i.offset_from(b) + i.len(), b.len());
}
_ => panic!("wrong return type in offset test for digit"),
}
match alphanumeric1::<_, Error<_>>(Partial::new(c)) {
Ok((i, _)) => {
let i = i.into_inner();
assert_eq!(c.offset_to(i) + i.len(), c.len());
assert_eq!(i.offset_from(c) + i.len(), c.len());
}
_ => panic!("wrong return type in offset test for alphanumeric"),
}
match space1::<_, Error<_>>(Partial::new(d)) {
Ok((i, _)) => {
let i = i.into_inner();
assert_eq!(d.offset_to(i) + i.len(), d.len());
assert_eq!(i.offset_from(d) + i.len(), d.len());
}
_ => panic!("wrong return type in offset test for space"),
}
match multispace1::<_, Error<_>>(Partial::new(e)) {
Ok((i, _)) => {
let i = i.into_inner();
assert_eq!(e.offset_to(i) + i.len(), e.len());
assert_eq!(i.offset_from(e) + i.len(), e.len());
}
_ => panic!("wrong return type in offset test for multispace"),
}
match hex_digit1::<_, Error<_>>(Partial::new(f)) {
Ok((i, _)) => {
let i = i.into_inner();
assert_eq!(f.offset_to(i) + i.len(), f.len());
assert_eq!(i.offset_from(f) + i.len(), f.len());
}
_ => panic!("wrong return type in offset test for hex_digit"),
}
match oct_digit1::<_, Error<_>>(Partial::new(f)) {
Ok((i, _)) => {
let i = i.into_inner();
assert_eq!(f.offset_to(i) + i.len(), f.len());
assert_eq!(i.offset_from(f) + i.len(), f.len());
}
_ => panic!("wrong return type in offset test for oct_digit"),
}
Expand Down
5 changes: 1 addition & 4 deletions src/combinator/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ pub fn rest<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E
where
I: Stream,
{
trace("rest", move |input: I| {
Ok(input.next_slice(input.eof_offset()))
})
.parse_next(input)
trace("rest", move |input: I| Ok(input.finish())).parse_next(input)
}

/// Return the length of the remaining input.
Expand Down
4 changes: 2 additions & 2 deletions src/combinator/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ where
let i = input.clone();
match (self.parser).parse_next(i) {
Ok((i, _)) => {
let offset = input.offset_to(&i);
let offset = i.offset_from(&input);
Ok(input.next_slice(offset))
}
Err(e) => Err(e),
Expand Down Expand Up @@ -585,7 +585,7 @@ where
let i = input.clone();
match (self.parser).parse_next(i) {
Ok((remaining, result)) => {
let offset = input.offset_to(&remaining);
let offset = remaining.offset_from(&input);
let (remaining, recognized) = input.next_slice(offset);
Ok((remaining, (result, recognized)))
}
Expand Down
3 changes: 2 additions & 1 deletion src/combinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::error::Error;
use crate::error::ErrorKind;
use crate::error::Needed;
use crate::error::ParseError;
use crate::stream::Stream;
use crate::token::take;
use crate::IResult;
use crate::Parser;
Expand Down Expand Up @@ -560,7 +561,7 @@ fn alt_test() {
}

fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> {
Ok((&b""[..], input))
Ok(input.finish())
}

#[allow(unused_variables)]
Expand Down
5 changes: 3 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,10 @@ pub fn convert_error<I: core::ops::Deref<Target = str>>(
use crate::stream::Offset;

let mut result = crate::lib::std::string::String::new();
let input = &*input;

for (i, (substring, kind)) in e.errors.iter().enumerate() {
let offset = input.offset_to(substring);
let offset = substring.offset_from(input);

if input.is_empty() {
match kind {
Expand Down Expand Up @@ -541,7 +542,7 @@ pub fn convert_error<I: core::ops::Deref<Target = str>>(
.trim_end();

// The (1-indexed) column number is the offset of our substring into that line
let column_number = line.offset_to(substring) + 1;
let column_number = substring.offset_from(line) + 1;

match kind {
VerboseErrorKind::Context(s) => write!(
Expand Down
Loading

0 comments on commit f864c0f

Please sign in to comment.