Skip to content

Commit

Permalink
refactor(typos): Upgrade to winnow 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jul 14, 2023
1 parent 162a6d7 commit 016ead4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/typos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
71 changes: 36 additions & 35 deletions crates/typos/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -138,7 +139,7 @@ mod parser {
use winnow::token::*;
use winnow::trace::trace;

pub(crate) fn next_identifier<T>(input: T) -> IResult<T, <T as Stream>::Slice>
pub(crate) fn next_identifier<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -147,7 +148,7 @@ mod parser {
preceded(ignore, identifier).parse_next(input)
}

fn identifier<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn identifier<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -160,7 +161,7 @@ mod parser {
trace("identifier", take_while(1.., is_xid_continue)).parse_next(input)
}

fn ignore<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn ignore<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand Down Expand Up @@ -189,7 +190,7 @@ mod parser {
.parse_next(input)
}

fn sep1<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn sep1<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -202,7 +203,7 @@ mod parser {
.parse_next(input)
}

fn other<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn other<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -219,7 +220,7 @@ mod parser {
.parse_next(input)
}

fn ordinal_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn ordinal_literal<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -244,7 +245,7 @@ mod parser {
.parse_next(input)
}

fn dec_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn dec_literal<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -253,7 +254,7 @@ mod parser {
trace("dec_literal", take_while(1.., is_dec_digit_with_sep)).parse_next(input)
}

fn hex_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn hex_literal<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -266,7 +267,7 @@ mod parser {
.parse_next(input)
}

fn css_color<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn css_color<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -285,7 +286,7 @@ mod parser {
.parse_next(input)
}

fn uuid_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn uuid_literal<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand Down Expand Up @@ -322,7 +323,7 @@ mod parser {
.parse_next(input)
}

fn hash_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn hash_literal<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -348,17 +349,18 @@ mod parser {
.parse_next(input)
}

fn base64_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn base64_literal<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
<T as Stream>::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;
Expand All @@ -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<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn email_literal<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -403,7 +406,7 @@ mod parser {
.parse_next(input)
}

fn url_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn url_literal<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand Down Expand Up @@ -432,7 +435,7 @@ mod parser {
.parse_next(input)
}

fn url_userinfo<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn url_userinfo<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -449,7 +452,7 @@ mod parser {
.parse_next(input)
}

fn c_escape<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn c_escape<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -466,7 +469,7 @@ mod parser {
.parse_next(input)
}

fn printf<T>(input: T) -> IResult<T, <T as Stream>::Slice>
fn printf<T>(input: &mut T) -> PResult<<T as Stream>::Slice, ()>
where
T: Stream + StreamIsPartial + PartialEq,
<T as Stream>::Slice: AsBStr + SliceLen + Default,
Expand All @@ -475,13 +478,13 @@ mod parser {
trace("printf", preceded('%', take_while(1.., is_xid_continue))).parse_next(input)
}

fn take_many0<I, E, F>(mut f: F) -> impl FnMut(I) -> IResult<I, <I as Stream>::Slice, E>
fn take_many0<I, E, F>(mut f: F) -> impl Parser<I, <I as Stream>::Slice, E>
where
I: Stream,
F: winnow::Parser<I, <I as Stream>::Slice, E>,
E: winnow::error::ParseError<I>,
F: Parser<I, <I as Stream>::Slice, E>,
E: ParserError<I>,
{
move |i: I| {
move |i: &mut I| {
repeat(0.., f.by_ref())
.map(|()| ())
.recognize()
Expand Down Expand Up @@ -619,9 +622,8 @@ mod unicode_parser {
use super::parser::next_identifier;

pub(crate) fn iter_identifiers(mut input: &str) -> impl Iterator<Item = &str> {
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)
}
Expand All @@ -636,9 +638,8 @@ mod ascii_parser {
use winnow::BStr;

pub(crate) fn iter_identifiers(mut input: &BStr) -> impl Iterator<Item = &str> {
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.
Expand Down

0 comments on commit 016ead4

Please sign in to comment.