Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(error)!: Consistently pass Checkpoints around #470

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/custom_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ use winnow::error::ErrMode;
use winnow::error::ErrorKind;
use winnow::error::ParserError;
use winnow::prelude::*;
use winnow::stream::Stream;

#[derive(Debug, PartialEq, Eq)]
pub enum CustomError<I> {
MyError,
Nom(I, ErrorKind),
}

impl<I: Clone> ParserError<I> for CustomError<I> {
impl<I: Stream + Clone> ParserError<I> for CustomError<I> {
fn from_error_kind(input: &I, kind: ErrorKind) -> Self {
CustomError::Nom(input.clone(), kind)
}

fn append(self, _: &I, _: ErrorKind) -> Self {
fn append(self, _: &I, _: &<I as Stream>::Checkpoint, _: ErrorKind) -> Self {
self
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/_tutorial/chapter_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,17 @@
//! return Ok(output);
//! }
//!
//! input.reset(start);
//! input.reset(&start);
//! if let Ok(output) = ("0o", parse_oct_digits).parse_next(input) {
//! return Ok(output);
//! }
//!
//! input.reset(start);
//! input.reset(&start);
//! if let Ok(output) = ("0d", parse_dec_digits).parse_next(input) {
//! return Ok(output);
//! }
//!
//! input.reset(start);
//! input.reset(&start);
//! ("0x", parse_hex_digits).parse_next(input)
//! }
//!
Expand Down
10 changes: 5 additions & 5 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@
Some(_) => {
if input.eof_offset() == current_len {
let offset = input.offset_from(&start);
input.reset(start);
input.reset(&start);

Check warning on line 1501 in src/ascii/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/ascii/mod.rs#L1501

Added line #L1501 was not covered by tests
return Ok(input.next_slice(offset));
}
}
Expand All @@ -1507,7 +1507,7 @@
let _ = escapable.parse_next(input)?;
} else {
let offset = input.offset_from(&start);
input.reset(start);
input.reset(&start);

Check warning on line 1510 in src/ascii/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/ascii/mod.rs#L1510

Added line #L1510 was not covered by tests
return Ok(input.next_slice(offset));
}
}
Expand Down Expand Up @@ -1540,7 +1540,7 @@
Some(_) => {
if input.eof_offset() == current_len {
let offset = input.offset_from(&start);
input.reset(start);
input.reset(&start);

Check warning on line 1543 in src/ascii/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/ascii/mod.rs#L1543

Added line #L1543 was not covered by tests
return Ok(input.next_slice(offset));
}
}
Expand All @@ -1549,14 +1549,14 @@
let _ = escapable.parse_next(input)?;
} else {
let offset = input.offset_from(&start);
input.reset(start);
input.reset(&start);

Check warning on line 1552 in src/ascii/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/ascii/mod.rs#L1552

Added line #L1552 was not covered by tests
return Ok(input.next_slice(offset));
}
}
}
}

input.reset(start);
input.reset(&start);

Check warning on line 1559 in src/ascii/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/ascii/mod.rs#L1559

Added line #L1559 was not covered by tests
Ok(input.finish())
}

Expand Down
3 changes: 2 additions & 1 deletion src/binary/bits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub fn bits<I, O, E1, E2, P>(mut parser: P) -> impl Parser<I, O, E2>
where
E1: ParserError<(I, usize)> + ErrorConvert<E2>,
E2: ParserError<I>,
(I, usize): Stream,
I: Stream + Clone,
P: Parser<(I, usize), O, E1>,
{
Expand Down Expand Up @@ -320,7 +321,7 @@ where
if pattern == o {
Ok(o)
} else {
input.reset(start);
input.reset(&start);
Err(ErrMode::Backtrack(E::from_error_kind(
input,
ErrorKind::Tag,
Expand Down
16 changes: 8 additions & 8 deletions src/combinator/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@

let start = input.checkpoint();
for branch in self {
input.reset(start.clone());
input.reset(&start);

Check warning on line 129 in src/combinator/branch.rs

View check run for this annotation

Codecov / codecov/patch

src/combinator/branch.rs#L129

Added line #L129 was not covered by tests
match branch.parse_next(input) {
Err(ErrMode::Backtrack(e)) => {
error = match error {
Expand All @@ -139,7 +139,7 @@
}

match error {
Some(e) => Err(ErrMode::Backtrack(e.append(input, ErrorKind::Alt))),
Some(e) => Err(ErrMode::Backtrack(e.append(input, &start, ErrorKind::Alt))),
None => Err(ErrMode::assert(input, "`alt` needs at least one parser")),
}
}
Expand Down Expand Up @@ -204,7 +204,7 @@

macro_rules! alt_trait_inner(
($it:tt, $self:expr, $input:expr, $start:ident, $err:expr, $head:ident $($id:ident)+) => ({
$input.reset($start.clone());
$input.reset(&$start);
match $self.$it.parse_next($input) {
Err(ErrMode::Backtrack(e)) => {
let err = $err.or(e);
Expand All @@ -214,14 +214,14 @@
}
});
($it:tt, $self:expr, $input:expr, $start:ident, $err:expr, $head:ident) => ({
Err(ErrMode::Backtrack($err.append($input, ErrorKind::Alt)))
Err(ErrMode::Backtrack($err.append($input, &$start, ErrorKind::Alt)))
});
);

alt_trait!(Alt2 Alt3 Alt4 Alt5 Alt6 Alt7 Alt8 Alt9 Alt10 Alt11 Alt12 Alt13 Alt14 Alt15 Alt16 Alt17 Alt18 Alt19 Alt20 Alt21 Alt22);

// Manually implement Alt for (A,), the 1-tuple type
impl<I, O, E: ParserError<I>, A: Parser<I, O, E>> Alt<I, O, E> for (A,) {
impl<I: Stream, O, E: ParserError<I>, A: Parser<I, O, E>> Alt<I, O, E> for (A,) {
fn choice(&mut self, input: &mut I) -> PResult<O, E> {
self.0.parse_next(input)
}
Expand Down Expand Up @@ -266,8 +266,8 @@
// or errored on the remaining input
if let Some(err) = err {
// There are remaining parsers, and all errored on the remaining input
input.reset(start.clone());
return Err(ErrMode::Backtrack(err.append(input, ErrorKind::Alt)));
input.reset(&start);
return Err(ErrMode::Backtrack(err.append(input, &start, ErrorKind::Alt)));
}

// All parsers were applied
Expand All @@ -284,7 +284,7 @@
macro_rules! permutation_trait_inner(
($it:tt, $self:expr, $input:ident, $start:ident, $res:expr, $err:expr, $head:ident $($id:ident)*) => (
if $res.$it.is_none() {
$input.reset($start.clone());
$input.reset(&$start);
match $self.$it.parse_next($input) {
Ok(o) => {
$res.$it = Some(o);
Expand Down
8 changes: 4 additions & 4 deletions src/combinator/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
match f.parse_next(input) {
Ok(o) => Ok(Some(o)),
Err(ErrMode::Backtrack(_)) => {
input.reset(start);
input.reset(&start);

Check warning on line 82 in src/combinator/core.rs

View check run for this annotation

Codecov / codecov/patch

src/combinator/core.rs#L82

Added line #L82 was not covered by tests
Ok(None)
}
Err(e) => Err(e),
Expand Down Expand Up @@ -148,7 +148,7 @@
trace("peek", move |input: &mut I| {
let start = input.checkpoint();
let res = f.parse_next(input);
input.reset(start);
input.reset(&start);

Check warning on line 151 in src/combinator/core.rs

View check run for this annotation

Codecov / codecov/patch

src/combinator/core.rs#L151

Added line #L151 was not covered by tests
res
})
}
Expand Down Expand Up @@ -211,7 +211,7 @@
trace("not", move |input: &mut I| {
let start = input.checkpoint();
let res = parser.parse_next(input);
input.reset(start);
input.reset(&start);

Check warning on line 214 in src/combinator/core.rs

View check run for this annotation

Codecov / codecov/patch

src/combinator/core.rs#L214

Added line #L214 was not covered by tests
match res {
Ok(_) => Err(ErrMode::from_error_kind(input, ErrorKind::Not)),
Err(ErrMode::Backtrack(_)) => Ok(()),
Expand Down Expand Up @@ -408,7 +408,7 @@
Some(o)
}
Err(ErrMode::Backtrack(_)) => {
self.input.reset(start);
self.input.reset(&start);

Check warning on line 411 in src/combinator/core.rs

View check run for this annotation

Codecov / codecov/patch

src/combinator/core.rs#L411

Added line #L411 was not covered by tests
self.state = Some(State::Done);
None
}
Expand Down
Loading
Loading