Skip to content

Commit

Permalink
Merge pull request #555 from epage/winnow
Browse files Browse the repository at this point in the history
refactor(parser): Resolve deprecations
  • Loading branch information
epage committed May 18, 2023
2 parents 0951271 + a52a722 commit faac518
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 81 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
Expand Up @@ -40,7 +40,7 @@ unbounded = []

[dependencies]
indexmap = { version = "1.9.1", features = ["std"] }
winnow = "0.4.3"
winnow = "0.4.6"
serde = { version = "1.0.145", optional = true }
kstring = { version = "2.0.0", features = ["max_inline"], optional = true }
toml_datetime = { version = "0.6.1", path = "../toml_datetime" }
Expand Down
4 changes: 2 additions & 2 deletions crates/toml_edit/src/parser/document.rs
Expand Up @@ -4,7 +4,7 @@ use winnow::combinator::cut_err;
use winnow::combinator::eof;
use winnow::combinator::opt;
use winnow::combinator::peek;
use winnow::combinator::repeat0;
use winnow::combinator::repeat;
use winnow::error::FromExternalError;
use winnow::token::any;
use winnow::token::one_of;
Expand Down Expand Up @@ -38,7 +38,7 @@ pub(crate) fn document(input: Input<'_>) -> IResult<Input<'_>, Document, ParserE
// Remove BOM if present
opt(b"\xEF\xBB\xBF"),
parse_ws(state_ref),
repeat0((
repeat(0.., (
dispatch! {peek(any);
crate::parser::trivia::COMMENT_START_SYMBOL => cut_err(parse_comment(state_ref)),
crate::parser::table::STD_TABLE_OPEN => cut_err(table(state_ref)),
Expand Down
4 changes: 2 additions & 2 deletions crates/toml_edit/src/parser/key.rs
Expand Up @@ -3,7 +3,7 @@ use std::ops::RangeInclusive;
use winnow::combinator::peek;
use winnow::combinator::separated1;
use winnow::token::any;
use winnow::token::take_while1;
use winnow::token::take_while;

use crate::key::Key;
use crate::parser::errors::CustomError;
Expand Down Expand Up @@ -58,7 +58,7 @@ pub(crate) fn simple_key(

// unquoted-key = 1*( ALPHA / DIGIT / %x2D / %x5F ) ; A-Z / a-z / 0-9 / - / _
fn unquoted_key(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<'_>> {
take_while1(UNQUOTED_CHAR)
take_while(1.., UNQUOTED_CHAR)
.map(|b| unsafe { from_utf8_unchecked(b, "`is_unquoted_char` filters out on-ASCII") })
.parse_next(input)
}
Expand Down
104 changes: 60 additions & 44 deletions crates/toml_edit/src/parser/numbers.rs
Expand Up @@ -5,7 +5,7 @@ use winnow::combinator::cut_err;
use winnow::combinator::opt;
use winnow::combinator::peek;
use winnow::combinator::preceded;
use winnow::combinator::repeat0;
use winnow::combinator::repeat;
use winnow::combinator::rest;
use winnow::token::one_of;
use winnow::token::tag;
Expand Down Expand Up @@ -56,15 +56,18 @@ pub(crate) fn dec_int(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<
alt((
(
one_of(DIGIT1_9),
repeat0(alt((
digit.value(()),
(
one_of(b'_'),
cut_err(digit)
.context(Context::Expected(ParserValue::Description("digit"))),
)
.value(()),
)))
repeat(
0..,
alt((
digit.value(()),
(
one_of(b'_'),
cut_err(digit)
.context(Context::Expected(ParserValue::Description("digit"))),
)
.value(()),
)),
)
.map(|()| ()),
)
.value(()),
Expand All @@ -85,14 +88,18 @@ pub(crate) fn hex_int(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<
HEX_PREFIX,
cut_err((
hexdig,
repeat0(alt((
hexdig.value(()),
(
one_of(b'_'),
cut_err(hexdig).context(Context::Expected(ParserValue::Description("digit"))),
)
.value(()),
)))
repeat(
0..,
alt((
hexdig.value(()),
(
one_of(b'_'),
cut_err(hexdig)
.context(Context::Expected(ParserValue::Description("digit"))),
)
.value(()),
)),
)
.map(|()| ()),
))
.recognize(),
Expand All @@ -110,15 +117,18 @@ pub(crate) fn oct_int(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<
OCT_PREFIX,
cut_err((
one_of(DIGIT0_7),
repeat0(alt((
one_of(DIGIT0_7).value(()),
(
one_of(b'_'),
cut_err(one_of(DIGIT0_7))
.context(Context::Expected(ParserValue::Description("digit"))),
)
.value(()),
)))
repeat(
0..,
alt((
one_of(DIGIT0_7).value(()),
(
one_of(b'_'),
cut_err(one_of(DIGIT0_7))
.context(Context::Expected(ParserValue::Description("digit"))),
)
.value(()),
)),
)
.map(|()| ()),
))
.recognize(),
Expand All @@ -137,15 +147,18 @@ pub(crate) fn bin_int(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<
BIN_PREFIX,
cut_err((
one_of(DIGIT0_1),
repeat0(alt((
one_of(DIGIT0_1).value(()),
(
one_of(b'_'),
cut_err(one_of(DIGIT0_1))
.context(Context::Expected(ParserValue::Description("digit"))),
)
.value(()),
)))
repeat(
0..,
alt((
one_of(DIGIT0_1).value(()),
(
one_of(b'_'),
cut_err(one_of(DIGIT0_1))
.context(Context::Expected(ParserValue::Description("digit"))),
)
.value(()),
)),
)
.map(|()| ()),
))
.recognize(),
Expand Down Expand Up @@ -207,14 +220,17 @@ pub(crate) fn frac(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<'_>
pub(crate) fn zero_prefixable_int(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<'_>> {
(
digit,
repeat0(alt((
digit.value(()),
(
one_of(b'_'),
cut_err(digit).context(Context::Expected(ParserValue::Description("digit"))),
)
.value(()),
)))
repeat(
0..,
alt((
digit.value(()),
(
one_of(b'_'),
cut_err(digit).context(Context::Expected(ParserValue::Description("digit"))),
)
.value(()),
)),
)
.map(|()| ()),
)
.recognize()
Expand Down
26 changes: 13 additions & 13 deletions crates/toml_edit/src/parser/strings.rs
Expand Up @@ -9,8 +9,7 @@ use winnow::combinator::fail;
use winnow::combinator::opt;
use winnow::combinator::peek;
use winnow::combinator::preceded;
use winnow::combinator::repeat0;
use winnow::combinator::repeat1;
use winnow::combinator::repeat;
use winnow::combinator::success;
use winnow::combinator::terminated;
use winnow::prelude::*;
Expand All @@ -19,8 +18,6 @@ use winnow::token::none_of;
use winnow::token::one_of;
use winnow::token::tag;
use winnow::token::take_while;
use winnow::token::take_while0;
use winnow::token::take_while1;

use crate::parser::errors::CustomError;
use crate::parser::numbers::HEXDIG;
Expand Down Expand Up @@ -71,7 +68,7 @@ fn basic_chars(input: Input<'_>) -> IResult<Input<'_>, Cow<'_, str>, ParserError
alt((
// Deviate from the official grammar by batching the unescaped chars so we build a string a
// chunk at a time, rather than a `char` at a time.
take_while1(BASIC_UNESCAPED)
take_while(1.., BASIC_UNESCAPED)
.try_map(std::str::from_utf8)
.map(Cow::Borrowed),
escaped.map(|c| Cow::Owned(String::from(c))),
Expand Down Expand Up @@ -203,7 +200,7 @@ fn mlb_content(input: Input<'_>) -> IResult<Input<'_>, Cow<'_, str>, ParserError
alt((
// Deviate from the official grammar by batching the unescaped chars so we build a string a
// chunk at a time, rather than a `char` at a time.
take_while1(MLB_UNESCAPED)
take_while(1.., MLB_UNESCAPED)
.try_map(std::str::from_utf8)
.map(Cow::Borrowed),
// Order changed fromg grammar so `escaped` can more easily `cut_err` on bad escape sequences
Expand Down Expand Up @@ -247,7 +244,7 @@ pub(crate) const MLB_UNESCAPED: (
// (including newlines) up to the next non-whitespace
// character or closing delimiter.
fn mlb_escaped_nl(input: Input<'_>) -> IResult<Input<'_>, (), ParserError<'_>> {
repeat1((ESCAPE, ws, ws_newlines))
repeat(1.., (ESCAPE, ws, ws_newlines))
.map(|()| ())
.value(())
.parse_next(input)
Expand All @@ -259,7 +256,7 @@ fn mlb_escaped_nl(input: Input<'_>) -> IResult<Input<'_>, (), ParserError<'_>> {
pub(crate) fn literal_string(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<'_>> {
delimited(
APOSTROPHE,
cut_err(take_while0(LITERAL_CHAR)),
cut_err(take_while(0.., LITERAL_CHAR)),
cut_err(APOSTROPHE),
)
.try_map(std::str::from_utf8)
Expand Down Expand Up @@ -304,11 +301,14 @@ pub(crate) const ML_LITERAL_STRING_DELIM: &[u8] = b"'''";
// ml-literal-body = *mll-content *( mll-quotes 1*mll-content ) [ mll-quotes ]
fn ml_literal_body(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<'_>> {
(
repeat0(mll_content).map(|()| ()),
repeat0((
mll_quotes(none_of(APOSTROPHE).value(())),
repeat1(mll_content).map(|()| ()),
))
repeat(0.., mll_content).map(|()| ()),
repeat(
0..,
(
mll_quotes(none_of(APOSTROPHE).value(())),
repeat(1.., mll_content).map(|()| ()),
),
)
.map(|()| ()),
opt(mll_quotes(tag(ML_LITERAL_STRING_DELIM).value(()))),
)
Expand Down
40 changes: 23 additions & 17 deletions crates/toml_edit/src/parser/trivia.rs
Expand Up @@ -3,13 +3,11 @@ use std::ops::RangeInclusive;
use winnow::combinator::alt;
use winnow::combinator::eof;
use winnow::combinator::opt;
use winnow::combinator::repeat0;
use winnow::combinator::repeat1;
use winnow::combinator::repeat;
use winnow::combinator::terminated;
use winnow::prelude::*;
use winnow::token::one_of;
use winnow::token::take_while0;
use winnow::token::take_while1;
use winnow::token::take_while;

use crate::parser::prelude::*;

Expand All @@ -31,7 +29,7 @@ pub(crate) const WSCHAR: (u8, u8) = (b' ', b'\t');

// ws = *wschar
pub(crate) fn ws(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<'_>> {
take_while0(WSCHAR)
take_while(0.., WSCHAR)
.map(|b| unsafe { from_utf8_unchecked(b, "`is_wschar` filters out on-ASCII") })
.parse_next(input)
}
Expand All @@ -51,7 +49,7 @@ pub(crate) const COMMENT_START_SYMBOL: u8 = b'#';

// comment = comment-start-symbol *non-eol
pub(crate) fn comment(input: Input<'_>) -> IResult<Input<'_>, &[u8], ParserError<'_>> {
(COMMENT_START_SYMBOL, take_while0(NON_EOL))
(COMMENT_START_SYMBOL, take_while(0.., NON_EOL))
.recognize()
.parse_next(input)
}
Expand All @@ -70,13 +68,14 @@ pub(crate) const CR: u8 = b'\r';

// ws-newline = *( wschar / newline )
pub(crate) fn ws_newline(input: Input<'_>) -> IResult<Input<'_>, &str, ParserError<'_>> {
repeat0(alt((newline.value(&b"\n"[..]), take_while1(WSCHAR))))
.map(|()| ())
.recognize()
.map(|b| unsafe {
from_utf8_unchecked(b, "`is_wschar` and `newline` filters out on-ASCII")
})
.parse_next(input)
repeat(
0..,
alt((newline.value(&b"\n"[..]), take_while(1.., WSCHAR))),
)
.map(|()| ())
.recognize()
.map(|b| unsafe { from_utf8_unchecked(b, "`is_wschar` and `newline` filters out on-ASCII") })
.parse_next(input)
}

// ws-newlines = newline *( wschar / newline )
Expand All @@ -92,10 +91,17 @@ pub(crate) fn ws_newlines(input: Input<'_>) -> IResult<Input<'_>, &str, ParserEr
// note: this rule is not present in the original grammar
// ws-comment-newline = *( ws-newline-nonempty / comment )
pub(crate) fn ws_comment_newline(input: Input<'_>) -> IResult<Input<'_>, &[u8], ParserError<'_>> {
repeat0(alt((
repeat1(alt((take_while1(WSCHAR), newline.value(&b"\n"[..])))).map(|()| ()),
comment.value(()),
)))
repeat(
0..,
alt((
repeat(
1..,
alt((take_while(1.., WSCHAR), newline.value(&b"\n"[..]))),
)
.map(|()| ()),
comment.value(()),
)),
)
.map(|()| ())
.recognize()
.parse_next(input)
Expand Down

0 comments on commit faac518

Please sign in to comment.