mirror of
https://github.com/crate-ci/typos.git
synced 2024-11-28 12:01:06 -05:00
refactor: Switch to winnow
This commit is contained in:
parent
67b9cbaa8c
commit
15e748d0e5
5 changed files with 175 additions and 321 deletions
29
Cargo.lock
generated
29
Cargo.lock
generated
|
@ -945,12 +945,6 @@ dependencies = [
|
||||||
"autocfg",
|
"autocfg",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "minimal-lexical"
|
|
||||||
version = "0.2.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "miniz_oxide"
|
name = "miniz_oxide"
|
||||||
version = "0.5.4"
|
version = "0.5.4"
|
||||||
|
@ -972,16 +966,6 @@ dependencies = [
|
||||||
"unicase",
|
"unicase",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "nom"
|
|
||||||
version = "7.1.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36"
|
|
||||||
dependencies = [
|
|
||||||
"memchr",
|
|
||||||
"minimal-lexical",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nom8"
|
name = "nom8"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
@ -1622,13 +1606,13 @@ dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"bstr 1.1.0",
|
"bstr 1.1.0",
|
||||||
"itertools",
|
"itertools",
|
||||||
"nom",
|
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"serde",
|
"serde",
|
||||||
"simdutf8",
|
"simdutf8",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
|
"winnow",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1802,7 +1786,7 @@ name = "varcon-core"
|
||||||
version = "2.2.7"
|
version = "2.2.7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"enumflags2",
|
"enumflags2",
|
||||||
"nom",
|
"winnow",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -2000,6 +1984,15 @@ version = "0.42.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"
|
checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winnow"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "efdd927d1a3d5d98abcfc4cf8627371862ee6abfe52a988050621c50c66b4493"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "yansi"
|
name = "yansi"
|
||||||
version = "0.5.1"
|
version = "0.5.1"
|
||||||
|
|
|
@ -14,7 +14,7 @@ include.workspace = true
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
nom = "7.1"
|
winnow = "0.3.0"
|
||||||
unicode-xid = "0.2.4"
|
unicode-xid = "0.2.4"
|
||||||
once_cell = "1.17.0"
|
once_cell = "1.17.0"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
|
|
@ -125,36 +125,31 @@ impl<'s> Iterator for Utf8Chunks<'s> {
|
||||||
}
|
}
|
||||||
|
|
||||||
mod parser {
|
mod parser {
|
||||||
use nom::branch::*;
|
use winnow::branch::*;
|
||||||
use nom::bytes::complete::*;
|
use winnow::bytes::complete::*;
|
||||||
use nom::character::complete::*;
|
use winnow::character::complete::*;
|
||||||
use nom::combinator::*;
|
use winnow::combinator::*;
|
||||||
use nom::sequence::*;
|
use winnow::prelude::*;
|
||||||
use nom::{AsChar, IResult};
|
use winnow::sequence::*;
|
||||||
|
use winnow::stream::AsBStr;
|
||||||
|
use winnow::stream::AsChar;
|
||||||
|
use winnow::stream::SliceLen;
|
||||||
|
use winnow::stream::Stream;
|
||||||
|
|
||||||
pub(crate) fn next_identifier<T>(input: T) -> IResult<T, T>
|
pub(crate) fn next_identifier<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Offset
|
|
||||||
+ Clone
|
|
||||||
+ Default
|
|
||||||
+ PartialEq
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
preceded(ignore, identifier)(input)
|
preceded(ignore, identifier)(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn identifier<T>(input: T) -> IResult<T, T>
|
fn identifier<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition + std::fmt::Debug,
|
T: Stream + PartialEq,
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
{
|
{
|
||||||
// Generally a language would be `{XID_Start}{XID_Continue}*` but going with only
|
// Generally a language would be `{XID_Start}{XID_Continue}*` but going with only
|
||||||
// `{XID_Continue}+` because XID_Continue is a superset of XID_Start and rather catch odd
|
// `{XID_Continue}+` because XID_Continue is a superset of XID_Start and rather catch odd
|
||||||
|
@ -163,21 +158,11 @@ mod parser {
|
||||||
take_while1(is_xid_continue)(input)
|
take_while1(is_xid_continue)(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ignore<T>(input: T) -> IResult<T, T>
|
fn ignore<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Offset
|
|
||||||
+ Clone
|
|
||||||
+ Default
|
|
||||||
+ PartialEq
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
take_many0(alt((
|
take_many0(alt((
|
||||||
// CAUTION: If adding an ignorable literal, if it doesn't start with `is_xid_continue`,
|
// CAUTION: If adding an ignorable literal, if it doesn't start with `is_xid_continue`,
|
||||||
|
@ -198,42 +183,23 @@ mod parser {
|
||||||
)))(input)
|
)))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sep1<T>(input: T) -> IResult<T, T>
|
fn sep1<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Offset
|
|
||||||
+ Clone
|
|
||||||
+ Default
|
|
||||||
+ PartialEq
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
alt((
|
alt((
|
||||||
recognize(satisfy(|c| !is_xid_continue(c))),
|
recognize(satisfy(|c| !is_xid_continue(c))),
|
||||||
map(eof, |_| T::default()),
|
map(eof, |_| <T as Stream>::Slice::default()),
|
||||||
))(input)
|
))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn other<T>(input: T) -> IResult<T, T>
|
fn other<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Offset
|
|
||||||
+ Clone
|
|
||||||
+ PartialEq
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
recognize(tuple((
|
recognize(tuple((
|
||||||
satisfy(|c| !is_xid_continue(c)),
|
satisfy(|c| !is_xid_continue(c)),
|
||||||
|
@ -241,19 +207,11 @@ mod parser {
|
||||||
)))(input)
|
)))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ordinal_literal<T>(input: T) -> IResult<T, T>
|
fn ordinal_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Offset
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ Clone
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
fn is_sep(c: impl AsChar) -> bool {
|
fn is_sep(c: impl AsChar) -> bool {
|
||||||
let c = c.as_char();
|
let c = c.as_char();
|
||||||
|
@ -274,25 +232,20 @@ mod parser {
|
||||||
)))(input)
|
)))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dec_literal<T>(input: T) -> IResult<T, T>
|
fn dec_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition + std::fmt::Debug,
|
T: Stream + PartialEq,
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
{
|
{
|
||||||
take_while1(is_dec_digit_with_sep)(input)
|
take_while1(is_dec_digit_with_sep)(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hex_literal<T>(input: T) -> IResult<T, T>
|
fn hex_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ Clone
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
preceded(
|
preceded(
|
||||||
pair(char('0'), alt((char('x'), char('X')))),
|
pair(char('0'), alt((char('x'), char('X')))),
|
||||||
|
@ -300,21 +253,11 @@ mod parser {
|
||||||
)(input)
|
)(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn css_color<T>(input: T) -> IResult<T, T>
|
fn css_color<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Offset
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ Clone
|
|
||||||
+ Default
|
|
||||||
+ PartialEq
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
preceded(
|
preceded(
|
||||||
char('#'),
|
char('#'),
|
||||||
|
@ -325,19 +268,11 @@ mod parser {
|
||||||
)(input)
|
)(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn uuid_literal<T>(input: T) -> IResult<T, T>
|
fn uuid_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Offset
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ Clone
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
recognize(alt((
|
recognize(alt((
|
||||||
tuple((
|
tuple((
|
||||||
|
@ -365,19 +300,11 @@ mod parser {
|
||||||
)))(input)
|
)))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash_literal<T>(input: T) -> IResult<T, T>
|
fn hash_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Offset
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ Clone
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
// Size considerations:
|
// Size considerations:
|
||||||
// - 40 characters holds for a SHA-1 hash from older Git versions.
|
// - 40 characters holds for a SHA-1 hash from older Git versions.
|
||||||
|
@ -396,19 +323,11 @@ mod parser {
|
||||||
))(input)
|
))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn base64_literal<T>(input: T) -> IResult<T, T>
|
fn base64_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Offset
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ Clone
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
let (padding, captured) = take_while1(is_base64_digit)(input.clone())?;
|
let (padding, captured) = take_while1(is_base64_digit)(input.clone())?;
|
||||||
|
|
||||||
|
@ -419,37 +338,30 @@ mod parser {
|
||||||
padding_len = 0;
|
padding_len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if captured.input_len() < 90
|
if captured.slice_len() < 90
|
||||||
&& padding_len == 0
|
&& padding_len == 0
|
||||||
&& captured
|
&& captured
|
||||||
.iter_elements()
|
.as_bstr()
|
||||||
|
.iter()
|
||||||
.all(|c| !['/', '+'].contains(&c.as_char()))
|
.all(|c| !['/', '+'].contains(&c.as_char()))
|
||||||
{
|
{
|
||||||
return Err(nom::Err::Error(nom::error::Error::new(
|
return Err(winnow::Err::Backtrack(winnow::error::Error::new(
|
||||||
input,
|
input,
|
||||||
nom::error::ErrorKind::LengthValue,
|
winnow::error::ErrorKind::LengthValue,
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let (after, _) = take_while_m_n(padding_len, padding_len, is_base64_padding)(padding)?;
|
let (after, _) = take_while_m_n(padding_len, padding_len, is_base64_padding)(padding)?;
|
||||||
|
|
||||||
let after_offset = input.offset(&after);
|
let after_offset = input.offset(&after);
|
||||||
Ok(input.take_split(after_offset))
|
Ok(input.next_slice(after_offset))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn email_literal<T>(input: T) -> IResult<T, T>
|
fn email_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Offset
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ Clone
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
recognize(tuple((
|
recognize(tuple((
|
||||||
take_while1(is_localport_char),
|
take_while1(is_localport_char),
|
||||||
|
@ -458,19 +370,11 @@ mod parser {
|
||||||
)))(input)
|
)))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn url_literal<T>(input: T) -> IResult<T, T>
|
fn url_literal<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Offset
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ Clone
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
recognize(tuple((
|
recognize(tuple((
|
||||||
opt(terminated(
|
opt(terminated(
|
||||||
|
@ -490,19 +394,11 @@ mod parser {
|
||||||
)))(input)
|
)))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn url_userinfo<T>(input: T) -> IResult<T, T>
|
fn url_userinfo<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Offset
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ Clone
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
recognize(tuple((
|
recognize(tuple((
|
||||||
take_while1(is_localport_char),
|
take_while1(is_localport_char),
|
||||||
|
@ -510,19 +406,11 @@ mod parser {
|
||||||
)))(input)
|
)))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn c_escape<T>(input: T) -> IResult<T, T>
|
fn c_escape<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Offset
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ Clone
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
// We don't know whether the string we are parsing is a literal string (no escaping) or
|
// We don't know whether the string we are parsing is a literal string (no escaping) or
|
||||||
// regular string that does escaping. The escaped letter might be part of a word, or it
|
// regular string that does escaping. The escaped letter might be part of a word, or it
|
||||||
|
@ -531,53 +419,26 @@ mod parser {
|
||||||
preceded(take_while1(is_escape), take_while(is_xid_continue))(input)
|
preceded(take_while1(is_escape), take_while(is_xid_continue))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn printf<T>(input: T) -> IResult<T, T>
|
fn printf<T>(input: T) -> IResult<T, <T as Stream>::Slice>
|
||||||
where
|
where
|
||||||
T: nom::InputTakeAtPosition
|
T: Stream + PartialEq,
|
||||||
+ nom::InputTake
|
<T as Stream>::Slice: AsBStr + SliceLen + Default,
|
||||||
+ nom::InputIter
|
<T as Stream>::Token: AsChar + Copy,
|
||||||
+ nom::InputLength
|
|
||||||
+ nom::Offset
|
|
||||||
+ nom::Slice<std::ops::RangeTo<usize>>
|
|
||||||
+ nom::Slice<std::ops::RangeFrom<usize>>
|
|
||||||
+ Clone
|
|
||||||
+ std::fmt::Debug,
|
|
||||||
<T as nom::InputTakeAtPosition>::Item: AsChar + Copy,
|
|
||||||
<T as nom::InputIter>::Item: AsChar + Copy,
|
|
||||||
{
|
{
|
||||||
preceded(char('%'), take_while1(is_xid_continue))(input)
|
preceded(char('%'), take_while1(is_xid_continue))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn take_many0<I, E, F>(mut f: F) -> impl FnMut(I) -> IResult<I, I, E>
|
fn take_many0<I, E, F>(mut f: F) -> impl FnMut(I) -> IResult<I, <I as Stream>::Slice, E>
|
||||||
where
|
where
|
||||||
I: nom::Offset + nom::InputTake + Clone + PartialEq + std::fmt::Debug,
|
I: Stream,
|
||||||
F: nom::Parser<I, I, E>,
|
F: winnow::Parser<I, <I as Stream>::Slice, E>,
|
||||||
E: nom::error::ParseError<I>,
|
E: winnow::error::ParseError<I>,
|
||||||
{
|
{
|
||||||
move |i: I| {
|
move |i: I| {
|
||||||
let mut current = i.clone();
|
winnow::multi::many0(f.by_ref())
|
||||||
loop {
|
.map(|()| ())
|
||||||
match f.parse(current.clone()) {
|
.recognize()
|
||||||
Err(nom::Err::Error(_)) => {
|
.parse_next(i)
|
||||||
let offset = i.offset(¤t);
|
|
||||||
let (after, before) = i.take_split(offset);
|
|
||||||
return Ok((after, before));
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
return Err(e);
|
|
||||||
}
|
|
||||||
Ok((next, _)) => {
|
|
||||||
if next == current {
|
|
||||||
return Err(nom::Err::Error(E::from_error_kind(
|
|
||||||
i,
|
|
||||||
nom::error::ErrorKind::Many0,
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
|
|
||||||
current = next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,11 +12,11 @@ include.workspace = true
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
parser = ["nom"]
|
parser = ["winnow"]
|
||||||
flags = ["enumflags2"]
|
flags = ["enumflags2"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nom = { version = "7", optional = true }
|
winnow = { version = "0.3.0", optional = true }
|
||||||
enumflags2 = { version = "0.7", optional = true }
|
enumflags2 = { version = "0.7", optional = true }
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use nom::IResult;
|
use winnow::stream::Stream;
|
||||||
use nom::InputTakeAtPosition;
|
use winnow::IResult;
|
||||||
use nom::Parser;
|
use winnow::Parser;
|
||||||
|
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
|
@ -64,31 +64,31 @@ A Cv: acknowledgment's / Av B C: acknowledgement's
|
||||||
|
|
||||||
impl Cluster {
|
impl Cluster {
|
||||||
pub fn parse(input: &str) -> IResult<&str, Self> {
|
pub fn parse(input: &str) -> IResult<&str, Self> {
|
||||||
let header = nom::sequence::tuple((
|
let header = winnow::sequence::tuple((
|
||||||
nom::bytes::streaming::tag("#"),
|
winnow::bytes::streaming::tag("#"),
|
||||||
nom::character::streaming::space0,
|
winnow::character::streaming::space0,
|
||||||
nom::character::streaming::not_line_ending,
|
winnow::character::streaming::not_line_ending,
|
||||||
nom::character::streaming::line_ending,
|
winnow::character::streaming::line_ending,
|
||||||
));
|
));
|
||||||
let note = nom::sequence::preceded(
|
let note = winnow::sequence::preceded(
|
||||||
nom::sequence::pair(
|
winnow::sequence::pair(
|
||||||
nom::bytes::streaming::tag("##"),
|
winnow::bytes::streaming::tag("##"),
|
||||||
nom::character::streaming::space0,
|
winnow::character::streaming::space0,
|
||||||
),
|
),
|
||||||
nom::sequence::terminated(
|
winnow::sequence::terminated(
|
||||||
nom::character::streaming::not_line_ending,
|
winnow::character::streaming::not_line_ending,
|
||||||
nom::character::streaming::line_ending,
|
winnow::character::streaming::line_ending,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
let mut cluster = nom::sequence::tuple((
|
let mut cluster = winnow::sequence::tuple((
|
||||||
nom::combinator::opt(header),
|
winnow::combinator::opt(header),
|
||||||
nom::multi::many1(nom::sequence::terminated(
|
winnow::multi::many1(winnow::sequence::terminated(
|
||||||
Entry::parse,
|
Entry::parse,
|
||||||
nom::character::streaming::line_ending,
|
winnow::character::streaming::line_ending,
|
||||||
)),
|
)),
|
||||||
nom::multi::many0(note),
|
winnow::multi::many0(note),
|
||||||
));
|
));
|
||||||
let (input, (header, entries, notes)) = (cluster)(input)?;
|
let (input, (header, entries, notes)): (_, (_, _, Vec<_>)) = (cluster)(input)?;
|
||||||
|
|
||||||
let header = header.map(|s| s.2.to_owned());
|
let header = header.map(|s| s.2.to_owned());
|
||||||
let notes = notes.into_iter().map(|s| s.to_owned()).collect();
|
let notes = notes.into_iter().map(|s| s.to_owned()).collect();
|
||||||
|
@ -150,28 +150,30 @@ A B C: coloration's / B. Cv: colouration's
|
||||||
|
|
||||||
impl Entry {
|
impl Entry {
|
||||||
pub fn parse(input: &str) -> IResult<&str, Self> {
|
pub fn parse(input: &str) -> IResult<&str, Self> {
|
||||||
let var_sep = nom::sequence::tuple((
|
let var_sep = winnow::sequence::tuple((
|
||||||
nom::character::streaming::space0,
|
winnow::character::streaming::space0,
|
||||||
nom::bytes::streaming::tag("/"),
|
winnow::bytes::streaming::tag("/"),
|
||||||
nom::character::streaming::space0,
|
winnow::character::streaming::space0,
|
||||||
));
|
));
|
||||||
let (input, variants) = nom::multi::separated_list1(var_sep, Variant::parse)(input)?;
|
let (input, variants) = winnow::multi::separated_list1(var_sep, Variant::parse)(input)?;
|
||||||
|
|
||||||
let desc_sep = nom::sequence::tuple((
|
let desc_sep = winnow::sequence::tuple((
|
||||||
nom::character::streaming::space0,
|
winnow::character::streaming::space0,
|
||||||
nom::bytes::streaming::tag("|"),
|
winnow::bytes::streaming::tag("|"),
|
||||||
));
|
));
|
||||||
let (input, description) =
|
let (input, description) = winnow::combinator::opt(winnow::sequence::tuple((
|
||||||
nom::combinator::opt(nom::sequence::tuple((desc_sep, Self::parse_description)))(input)?;
|
desc_sep,
|
||||||
|
Self::parse_description,
|
||||||
|
)))(input)?;
|
||||||
|
|
||||||
let comment_sep = nom::sequence::tuple((
|
let comment_sep = winnow::sequence::tuple((
|
||||||
nom::character::streaming::space0,
|
winnow::character::streaming::space0,
|
||||||
nom::bytes::streaming::tag("#"),
|
winnow::bytes::streaming::tag("#"),
|
||||||
));
|
));
|
||||||
let (input, comment) = nom::combinator::opt(nom::sequence::tuple((
|
let (input, comment) = winnow::combinator::opt(winnow::sequence::tuple((
|
||||||
comment_sep,
|
comment_sep,
|
||||||
nom::character::streaming::space1,
|
winnow::character::streaming::space1,
|
||||||
nom::character::streaming::not_line_ending,
|
winnow::character::streaming::not_line_ending,
|
||||||
)))(input)?;
|
)))(input)?;
|
||||||
|
|
||||||
let mut e = match description {
|
let mut e = match description {
|
||||||
|
@ -191,22 +193,22 @@ impl Entry {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_description(input: &str) -> IResult<&str, Self> {
|
fn parse_description(input: &str) -> IResult<&str, Self> {
|
||||||
let (input, (pos, archaic, note, description)) = nom::sequence::tuple((
|
let (input, (pos, archaic, note, description)) = winnow::sequence::tuple((
|
||||||
nom::combinator::opt(nom::sequence::tuple((
|
winnow::combinator::opt(winnow::sequence::tuple((
|
||||||
nom::character::streaming::space1,
|
winnow::character::streaming::space1,
|
||||||
Pos::parse,
|
Pos::parse,
|
||||||
))),
|
))),
|
||||||
nom::combinator::opt(nom::sequence::tuple((
|
winnow::combinator::opt(winnow::sequence::tuple((
|
||||||
nom::character::streaming::space1,
|
winnow::character::streaming::space1,
|
||||||
nom::bytes::streaming::tag("(-)"),
|
winnow::bytes::streaming::tag("(-)"),
|
||||||
))),
|
))),
|
||||||
nom::combinator::opt(nom::sequence::tuple((
|
winnow::combinator::opt(winnow::sequence::tuple((
|
||||||
nom::character::streaming::space1,
|
winnow::character::streaming::space1,
|
||||||
nom::bytes::streaming::tag("--"),
|
winnow::bytes::streaming::tag("--"),
|
||||||
))),
|
))),
|
||||||
nom::combinator::opt(nom::sequence::tuple((
|
winnow::combinator::opt(winnow::sequence::tuple((
|
||||||
nom::character::streaming::space1,
|
winnow::character::streaming::space1,
|
||||||
nom::bytes::streaming::take_till(|c| c == '\n' || c == '\r' || c == '#'),
|
winnow::bytes::streaming::take_till(|c| c == '\n' || c == '\r' || c == '#'),
|
||||||
))),
|
))),
|
||||||
))(input)?;
|
))(input)?;
|
||||||
|
|
||||||
|
@ -319,24 +321,22 @@ mod test_entry {
|
||||||
|
|
||||||
impl Variant {
|
impl Variant {
|
||||||
pub fn parse(input: &str) -> IResult<&str, Self> {
|
pub fn parse(input: &str) -> IResult<&str, Self> {
|
||||||
let types = nom::multi::separated_list1(nom::character::streaming::space1, Type::parse);
|
let types =
|
||||||
let sep = nom::sequence::tuple((
|
winnow::multi::separated_list1(winnow::character::streaming::space1, Type::parse);
|
||||||
nom::bytes::streaming::tag(":"),
|
let sep = winnow::sequence::tuple((
|
||||||
nom::character::streaming::space0,
|
winnow::bytes::streaming::tag(":"),
|
||||||
|
winnow::character::streaming::space0,
|
||||||
));
|
));
|
||||||
let (input, (types, word)) = nom::sequence::separated_pair(types, sep, word)(input)?;
|
let (input, (types, word)) = winnow::sequence::separated_pair(types, sep, word)(input)?;
|
||||||
let v = Self { types, word };
|
let v = Self { types, word };
|
||||||
Ok((input, v))
|
Ok((input, v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn word(input: &str) -> IResult<&str, String> {
|
fn word(input: &str) -> IResult<&str, String> {
|
||||||
input
|
winnow::bytes::take_till1(|item: char| item.is_ascii_whitespace())
|
||||||
.split_at_position1(
|
.map(|s: &str| s.to_owned().replace('_', " "))
|
||||||
|item| item.is_ascii_whitespace(),
|
.parse_next(input)
|
||||||
nom::error::ErrorKind::Alpha,
|
|
||||||
)
|
|
||||||
.map(|(i, s)| (i, s.to_owned().replace('_', " ")))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -409,8 +409,8 @@ mod test_variant {
|
||||||
impl Type {
|
impl Type {
|
||||||
pub fn parse(input: &str) -> IResult<&str, Type> {
|
pub fn parse(input: &str) -> IResult<&str, Type> {
|
||||||
let (input, category) = Category::parse(input)?;
|
let (input, category) = Category::parse(input)?;
|
||||||
let (input, tag) = nom::combinator::opt(Tag::parse)(input)?;
|
let (input, tag) = winnow::combinator::opt(Tag::parse)(input)?;
|
||||||
let (input, num) = nom::combinator::opt(nom::character::streaming::digit1)(input)?;
|
let (input, num) = winnow::combinator::opt(winnow::character::streaming::digit1)(input)?;
|
||||||
let num = num.map(|s| s.parse().expect("parser ensured its a number"));
|
let num = num.map(|s| s.parse().expect("parser ensured its a number"));
|
||||||
let t = Type { category, tag, num };
|
let t = Type { category, tag, num };
|
||||||
Ok((input, t))
|
Ok((input, t))
|
||||||
|
@ -465,8 +465,8 @@ mod test_type {
|
||||||
|
|
||||||
impl Category {
|
impl Category {
|
||||||
pub fn parse(input: &str) -> IResult<&str, Category> {
|
pub fn parse(input: &str) -> IResult<&str, Category> {
|
||||||
let symbols = nom::character::streaming::one_of("ABZCD_");
|
let symbols = winnow::character::streaming::one_of("ABZCD_");
|
||||||
nom::combinator::map(symbols, |c| match c {
|
winnow::combinator::map(symbols, |c| match c {
|
||||||
'A' => Category::American,
|
'A' => Category::American,
|
||||||
'B' => Category::BritishIse,
|
'B' => Category::BritishIse,
|
||||||
'Z' => Category::BritishIze,
|
'Z' => Category::BritishIze,
|
||||||
|
@ -499,8 +499,8 @@ mod test_category {
|
||||||
|
|
||||||
impl Tag {
|
impl Tag {
|
||||||
pub fn parse(input: &str) -> IResult<&str, Tag> {
|
pub fn parse(input: &str) -> IResult<&str, Tag> {
|
||||||
let symbols = nom::character::streaming::one_of(".vV-x");
|
let symbols = winnow::character::streaming::one_of(".vV-x");
|
||||||
nom::combinator::map(symbols, |c| match c {
|
winnow::combinator::map(symbols, |c| match c {
|
||||||
'.' => Tag::Eq,
|
'.' => Tag::Eq,
|
||||||
'v' => Tag::Variant,
|
'v' => Tag::Variant,
|
||||||
'V' => Tag::Seldom,
|
'V' => Tag::Seldom,
|
||||||
|
@ -532,12 +532,12 @@ mod test_tag {
|
||||||
|
|
||||||
impl Pos {
|
impl Pos {
|
||||||
pub fn parse(input: &str) -> IResult<&str, Pos> {
|
pub fn parse(input: &str) -> IResult<&str, Pos> {
|
||||||
use nom::bytes::streaming::tag;
|
use winnow::bytes::streaming::tag;
|
||||||
let noun = tag("<N>");
|
let noun = tag("<N>");
|
||||||
let verb = tag("<V>");
|
let verb = tag("<V>");
|
||||||
let adjective = tag("<Adj>");
|
let adjective = tag("<Adj>");
|
||||||
let adverb = tag("<Adv>");
|
let adverb = tag("<Adv>");
|
||||||
nom::branch::alt((
|
winnow::branch::alt((
|
||||||
noun.map(|_| Pos::Noun),
|
noun.map(|_| Pos::Noun),
|
||||||
verb.map(|_| Pos::Verb),
|
verb.map(|_| Pos::Verb),
|
||||||
adjective.map(|_| Pos::Adjective),
|
adjective.map(|_| Pos::Adjective),
|
||||||
|
|
Loading…
Reference in a new issue