diff --git a/Cargo.lock b/Cargo.lock index ecb55b7..7fc4ab8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1648,7 +1648,7 @@ dependencies = [ "thiserror", "unicode-segmentation", "unicode-xid", - "winnow 0.4.9", + "winnow 0.5.0", ] [[package]] diff --git a/crates/typos/Cargo.toml b/crates/typos/Cargo.toml index 27eb0d7..53ea8fd 100644 --- a/crates/typos/Cargo.toml +++ b/crates/typos/Cargo.toml @@ -14,7 +14,7 @@ include.workspace = true [dependencies] anyhow = "1.0" thiserror = "1.0" -winnow = "0.4.9" +winnow = "0.5.0" unicode-xid = "0.2.4" once_cell = "1.17.2" serde = { version = "1.0", features = ["derive"] } diff --git a/crates/typos/src/tokens.rs b/crates/typos/src/tokens.rs index f2997cc..92b2d56 100644 --- a/crates/typos/src/tokens.rs +++ b/crates/typos/src/tokens.rs @@ -129,6 +129,7 @@ impl<'s> Iterator for Utf8Chunks<'s> { mod parser { use winnow::combinator::*; + use winnow::error::ParserError; use winnow::prelude::*; use winnow::stream::AsBStr; use winnow::stream::AsChar; @@ -138,7 +139,7 @@ mod parser { use winnow::token::*; use winnow::trace::trace; - pub(crate) fn next_identifier(input: T) -> IResult::Slice> + pub(crate) fn next_identifier(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -147,7 +148,7 @@ mod parser { preceded(ignore, identifier).parse_next(input) } - fn identifier(input: T) -> IResult::Slice> + fn identifier(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -160,7 +161,7 @@ mod parser { trace("identifier", take_while(1.., is_xid_continue)).parse_next(input) } - fn ignore(input: T) -> IResult::Slice> + fn ignore(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -189,7 +190,7 @@ mod parser { .parse_next(input) } - fn sep1(input: T) -> IResult::Slice> + fn sep1(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -202,7 +203,7 @@ mod parser { .parse_next(input) } - fn other(input: T) -> IResult::Slice> + fn other(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -219,7 +220,7 @@ mod parser { .parse_next(input) } - fn ordinal_literal(input: T) -> IResult::Slice> + fn ordinal_literal(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -244,7 +245,7 @@ mod parser { .parse_next(input) } - fn dec_literal(input: T) -> IResult::Slice> + fn dec_literal(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -253,7 +254,7 @@ mod parser { trace("dec_literal", take_while(1.., is_dec_digit_with_sep)).parse_next(input) } - fn hex_literal(input: T) -> IResult::Slice> + fn hex_literal(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -266,7 +267,7 @@ mod parser { .parse_next(input) } - fn css_color(input: T) -> IResult::Slice> + fn css_color(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -285,7 +286,7 @@ mod parser { .parse_next(input) } - fn uuid_literal(input: T) -> IResult::Slice> + fn uuid_literal(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -322,7 +323,7 @@ mod parser { .parse_next(input) } - fn hash_literal(input: T) -> IResult::Slice> + fn hash_literal(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -348,17 +349,18 @@ mod parser { .parse_next(input) } - fn base64_literal(input: T) -> IResult::Slice> + fn base64_literal(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, ::Token: AsChar + Copy, { - trace("base64", move |input: T| { - let (padding, captured) = take_while(1.., is_base64_digit).parse_next(input.clone())?; + trace("base64", move |input: &mut T| { + let start = input.checkpoint(); + let captured = take_while(1.., is_base64_digit).parse_next(input)?; const CHUNK: usize = 4; - let padding_offset = input.offset_to(&padding); + let padding_offset = input.offset_from(&start); let mut padding_len = CHUNK - padding_offset % CHUNK; if padding_len == CHUNK { padding_len = 0; @@ -371,21 +373,22 @@ mod parser { .iter() .all(|c| !['/', '+'].contains(&c.as_char())) { - return Err(winnow::error::ErrMode::Backtrack( - winnow::error::Error::new(input, winnow::error::ErrorKind::Slice), + return Err(winnow::error::ErrMode::from_error_kind( + input, + winnow::error::ErrorKind::Slice, )); } - let (after, _) = - take_while(padding_len..=padding_len, is_base64_padding).parse_next(padding)?; + take_while(padding_len..=padding_len, is_base64_padding).parse_next(input)?; - let after_offset = input.offset_to(&after); + let after_offset = input.offset_from(&start); + input.reset(start); Ok(input.next_slice(after_offset)) }) .parse_next(input) } - fn email_literal(input: T) -> IResult::Slice> + fn email_literal(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -403,7 +406,7 @@ mod parser { .parse_next(input) } - fn url_literal(input: T) -> IResult::Slice> + fn url_literal(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -432,7 +435,7 @@ mod parser { .parse_next(input) } - fn url_userinfo(input: T) -> IResult::Slice> + fn url_userinfo(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -449,7 +452,7 @@ mod parser { .parse_next(input) } - fn c_escape(input: T) -> IResult::Slice> + fn c_escape(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -466,7 +469,7 @@ mod parser { .parse_next(input) } - fn printf(input: T) -> IResult::Slice> + fn printf(input: &mut T) -> PResult<::Slice, ()> where T: Stream + StreamIsPartial + PartialEq, ::Slice: AsBStr + SliceLen + Default, @@ -475,13 +478,13 @@ mod parser { trace("printf", preceded('%', take_while(1.., is_xid_continue))).parse_next(input) } - fn take_many0(mut f: F) -> impl FnMut(I) -> IResult::Slice, E> + fn take_many0(mut f: F) -> impl Parser::Slice, E> where I: Stream, - F: winnow::Parser::Slice, E>, - E: winnow::error::ParseError, + F: Parser::Slice, E>, + E: ParserError, { - move |i: I| { + move |i: &mut I| { repeat(0.., f.by_ref()) .map(|()| ()) .recognize() @@ -619,9 +622,8 @@ mod unicode_parser { use super::parser::next_identifier; pub(crate) fn iter_identifiers(mut input: &str) -> impl Iterator { - std::iter::from_fn(move || match next_identifier(input) { - Ok((i, o)) => { - input = i; + std::iter::from_fn(move || match next_identifier(&mut input) { + Ok(o) => { debug_assert_ne!(o, ""); Some(o) } @@ -636,9 +638,8 @@ mod ascii_parser { use winnow::BStr; pub(crate) fn iter_identifiers(mut input: &BStr) -> impl Iterator { - std::iter::from_fn(move || match next_identifier(input) { - Ok((i, o)) => { - input = i; + std::iter::from_fn(move || match next_identifier(&mut input) { + Ok(o) => { debug_assert_ne!(o, b""); // This is safe because we've checked that the strings are a subset of ASCII // characters.