Skip to content

Commit

Permalink
chore: Update winnow
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jul 10, 2023
1 parent 68be6ab commit 4cdad45
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 327 deletions.
4 changes: 2 additions & 2 deletions 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/toml_edit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ unbounded = []

[dependencies]
indexmap = { version = "2.0.0", features = ["std"] }
winnow = "0.4.9"
winnow = "0.5.0"
serde = { version = "1.0.145", optional = true }
kstring = { version = "2.0.0", features = ["max_inline"], optional = true }
toml_datetime = { version = "0.6.3", path = "../toml_datetime" }
Expand Down
12 changes: 6 additions & 6 deletions crates/toml_edit/src/parser/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::parser::prelude::*;
// ;; Array

// array = array-open array-values array-close
pub(crate) fn array<'i>(check: RecursionCheck) -> impl Parser<Input<'i>, Array, ContextError<'i>> {
trace("array", move |input| {
pub(crate) fn array<'i>(check: RecursionCheck) -> impl Parser<Input<'i>, Array, ContextError> {
trace("array", move |input: &mut Input<'i>| {
delimited(
ARRAY_OPEN,
cut_err(array_values(check)),
Expand All @@ -40,8 +40,8 @@ const ARRAY_SEP: u8 = b',';
// array-value / ws-comment-newline ]
pub(crate) fn array_values<'i>(
check: RecursionCheck,
) -> impl Parser<Input<'i>, Array, ContextError<'i>> {
move |input| {
) -> impl Parser<Input<'i>, Array, ContextError> {
move |input: &mut Input<'i>| {
let check = check.recursing(input)?;
(
opt(
Expand All @@ -68,8 +68,8 @@ pub(crate) fn array_values<'i>(

pub(crate) fn array_value<'i>(
check: RecursionCheck,
) -> impl Parser<Input<'i>, Value, ContextError<'i>> {
move |input| {
) -> impl Parser<Input<'i>, Value, ContextError> {
move |input: &mut Input<'i>| {
(
ws_comment_newline.span(),
value(check),
Expand Down
30 changes: 15 additions & 15 deletions crates/toml_edit/src/parser/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use winnow::trace::trace;
// local-date = full-date
// local-time = partial-time
// full-time = partial-time time-offset
pub(crate) fn date_time(input: Input<'_>) -> IResult<Input<'_>, Datetime, ContextError<'_>> {
pub(crate) fn date_time(input: &mut Input<'_>) -> PResult<Datetime> {
trace(
"date-time",
alt((
Expand Down Expand Up @@ -52,7 +52,7 @@ pub(crate) fn date_time(input: Input<'_>) -> IResult<Input<'_>, Datetime, Contex
}

// full-date = date-fullyear "-" date-month "-" date-mday
pub(crate) fn full_date(input: Input<'_>) -> IResult<Input<'_>, Date, ContextError<'_>> {
pub(crate) fn full_date(input: &mut Input<'_>) -> PResult<Date> {
trace(
"full-date",
(date_fullyear, b'-', cut_err((date_month, b'-', date_mday)))
Expand All @@ -62,7 +62,7 @@ pub(crate) fn full_date(input: Input<'_>) -> IResult<Input<'_>, Date, ContextErr
}

// partial-time = time-hour ":" time-minute ":" time-second [time-secfrac]
pub(crate) fn partial_time(input: Input<'_>) -> IResult<Input<'_>, Time, ContextError<'_>> {
pub(crate) fn partial_time(input: &mut Input<'_>) -> PResult<Time> {
trace(
"partial-time",
(
Expand All @@ -82,7 +82,7 @@ pub(crate) fn partial_time(input: Input<'_>) -> IResult<Input<'_>, Time, Context

// time-offset = "Z" / time-numoffset
// time-numoffset = ( "+" / "-" ) time-hour ":" time-minute
pub(crate) fn time_offset(input: Input<'_>) -> IResult<Input<'_>, Offset, ContextError<'_>> {
pub(crate) fn time_offset(input: &mut Input<'_>) -> PResult<Offset> {
trace(
"time-offset",
alt((
Expand All @@ -108,14 +108,14 @@ pub(crate) fn time_offset(input: Input<'_>) -> IResult<Input<'_>, Offset, Contex
}

// date-fullyear = 4DIGIT
pub(crate) fn date_fullyear(input: Input<'_>) -> IResult<Input<'_>, u16, ContextError<'_>> {
pub(crate) fn date_fullyear(input: &mut Input<'_>) -> PResult<u16> {
unsigned_digits::<4, 4>
.map(|s: &str| s.parse::<u16>().expect("4DIGIT should match u8"))
.parse_next(input)
}

// date-month = 2DIGIT ; 01-12
pub(crate) fn date_month(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError<'_>> {
pub(crate) fn date_month(input: &mut Input<'_>) -> PResult<u8> {
unsigned_digits::<2, 2>
.try_map(|s: &str| {
let d = s.parse::<u8>().expect("2DIGIT should match u8");
Expand All @@ -129,7 +129,7 @@ pub(crate) fn date_month(input: Input<'_>) -> IResult<Input<'_>, u8, ContextErro
}

// date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on month/year
pub(crate) fn date_mday(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError<'_>> {
pub(crate) fn date_mday(input: &mut Input<'_>) -> PResult<u8> {
unsigned_digits::<2, 2>
.try_map(|s: &str| {
let d = s.parse::<u8>().expect("2DIGIT should match u8");
Expand All @@ -143,14 +143,14 @@ pub(crate) fn date_mday(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError
}

// time-delim = "T" / %x20 ; T, t, or space
pub(crate) fn time_delim(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError<'_>> {
pub(crate) fn time_delim(input: &mut Input<'_>) -> PResult<u8> {
one_of(TIME_DELIM).parse_next(input)
}

const TIME_DELIM: (u8, u8, u8) = (b'T', b't', b' ');

// time-hour = 2DIGIT ; 00-23
pub(crate) fn time_hour(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError<'_>> {
pub(crate) fn time_hour(input: &mut Input<'_>) -> PResult<u8> {
unsigned_digits::<2, 2>
.try_map(|s: &str| {
let d = s.parse::<u8>().expect("2DIGIT should match u8");
Expand All @@ -164,7 +164,7 @@ pub(crate) fn time_hour(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError
}

// time-minute = 2DIGIT ; 00-59
pub(crate) fn time_minute(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError<'_>> {
pub(crate) fn time_minute(input: &mut Input<'_>) -> PResult<u8> {
unsigned_digits::<2, 2>
.try_map(|s: &str| {
let d = s.parse::<u8>().expect("2DIGIT should match u8");
Expand All @@ -178,7 +178,7 @@ pub(crate) fn time_minute(input: Input<'_>) -> IResult<Input<'_>, u8, ContextErr
}

// time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second rules
pub(crate) fn time_second(input: Input<'_>) -> IResult<Input<'_>, u8, ContextError<'_>> {
pub(crate) fn time_second(input: &mut Input<'_>) -> PResult<u8> {
unsigned_digits::<2, 2>
.try_map(|s: &str| {
let d = s.parse::<u8>().expect("2DIGIT should match u8");
Expand All @@ -192,7 +192,7 @@ pub(crate) fn time_second(input: Input<'_>) -> IResult<Input<'_>, u8, ContextErr
}

// time-secfrac = "." 1*DIGIT
pub(crate) fn time_secfrac(input: Input<'_>) -> IResult<Input<'_>, u32, ContextError<'_>> {
pub(crate) fn time_secfrac(input: &mut Input<'_>) -> PResult<u32> {
static SCALE: [u32; 10] = [
0,
100_000_000,
Expand Down Expand Up @@ -227,9 +227,9 @@ pub(crate) fn time_secfrac(input: Input<'_>) -> IResult<Input<'_>, u32, ContextE
.parse_next(input)
}

pub(crate) fn unsigned_digits<const MIN: usize, const MAX: usize>(
input: Input<'_>,
) -> IResult<Input<'_>, &str, ContextError<'_>> {
pub(crate) fn unsigned_digits<'i, const MIN: usize, const MAX: usize>(
input: &mut Input<'i>,
) -> PResult<&'i str> {
take_while(MIN..=MAX, DIGIT)
.map(|b: &[u8]| unsafe { from_utf8_unchecked(b, "`is_ascii_digit` filters out on-ASCII") })
.parse_next(input)
Expand Down
34 changes: 14 additions & 20 deletions crates/toml_edit/src/parser/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ use crate::RawString;
// ( ws keyval ws [ comment ] ) /
// ( ws table ws [ comment ] ) /
// ws )
pub(crate) fn document(input: Input<'_>) -> IResult<Input<'_>, Document, ContextError<'_>> {
pub(crate) fn document(input: &mut Input<'_>) -> PResult<Document> {
let state = RefCell::new(ParseState::default());
let state_ref = &state;

let (i, _o) = (
let _o = (
// Remove BOM if present
opt(b"\xEF\xBB\xBF"),
parse_ws(state_ref),
Expand All @@ -52,19 +52,15 @@ pub(crate) fn document(input: Input<'_>) -> IResult<Input<'_>, Document, Context
eof,
)
.parse_next(input)?;
state
.into_inner()
.into_document()
.map(|document| (i, document))
.map_err(|err| {
winnow::error::ErrMode::from_external_error(i, winnow::error::ErrorKind::Verify, err)
})
state.into_inner().into_document().map_err(|err| {
winnow::error::ErrMode::from_external_error(input, winnow::error::ErrorKind::Verify, err)
})
}

pub(crate) fn parse_comment<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError<'i>> + 's {
move |i| {
) -> impl Parser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
(comment, line_ending)
.span()
.map(|span| {
Expand All @@ -76,8 +72,8 @@ pub(crate) fn parse_comment<'s, 'i>(

pub(crate) fn parse_ws<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError<'i>> + 's {
move |i| {
) -> impl Parser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
ws.span()
.map(|span| state.borrow_mut().on_ws(span))
.parse_next(i)
Expand All @@ -86,8 +82,8 @@ pub(crate) fn parse_ws<'s, 'i>(

pub(crate) fn parse_newline<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError<'i>> + 's {
move |i| {
) -> impl Parser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
newline
.span()
.map(|span| state.borrow_mut().on_ws(span))
Expand All @@ -97,18 +93,16 @@ pub(crate) fn parse_newline<'s, 'i>(

pub(crate) fn keyval<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError<'i>> + 's {
move |i| {
) -> impl Parser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
parse_keyval
.try_map(|(p, kv)| state.borrow_mut().on_keyval(p, kv))
.parse_next(i)
}
}

// keyval = key keyval-sep val
pub(crate) fn parse_keyval(
input: Input<'_>,
) -> IResult<Input<'_>, (Vec<Key>, TableKeyValue), ContextError<'_>> {
pub(crate) fn parse_keyval(input: &mut Input<'_>) -> PResult<(Vec<Key>, TableKeyValue)> {
trace(
"keyval",
(
Expand Down

0 comments on commit 4cdad45

Please sign in to comment.