Skip to content

Commit

Permalink
chore: Update to Winnow 0.7 (#723)
Browse files Browse the repository at this point in the history
* chore: Update to Winnow 0.7

* bench: Fix a bug in the winnow code

* bench: Remove superfluous cut_err from Winnow

* bench: Remove remaining cut_err

* bench: Remove use of ErrMode

This is to match our example
  • Loading branch information
epage authored Feb 1, 2025
1 parent 10a8755 commit e4373d0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
19 changes: 14 additions & 5 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ vergen-gix = { version = "1.0", optional = true, features = ["emit_and_set"] }
ariadne = "0.2"
pom = "3.2"
nom = "7.1"
winnow = "0.6.0"
winnow = "0.7.0"
serde_json = { version = "1.0", features = ["preserve_order"] }
ciborium = { version = "0.2" }
criterion = "0.4.0"
Expand Down
50 changes: 21 additions & 29 deletions benches/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,94 +357,86 @@ mod winnow {
ascii::{digit0, digit1, take_escaped},
combinator::separated,
combinator::{alt, dispatch},
combinator::{cut_err, fail, opt, peek},
combinator::{fail, opt, peek},
combinator::{preceded, separated_pair, terminated},
error::{InputError, ParserError},
error::{EmptyError, ParserError},
prelude::*,
token::{any, none_of, one_of, take_while},
Result,
};

use super::JsonZero;
use std::str;

fn space<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<&'a [u8], E> {
fn space<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> Result<&'a [u8], E> {
take_while(0.., [b' ', b'\t', b'\r', b'\n']).parse_next(i)
}

fn number<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<f64, E> {
fn number<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> Result<f64, E> {
(
opt('-'),
alt(((one_of(b'1'..=b'9'), digit0).void(), one_of('0').void())),
opt(('.', digit1)),
opt((
one_of([b'e', b'E']),
opt(one_of([b'+', b'-'])),
cut_err(digit1),
)),
opt((one_of([b'e', b'E']), opt(one_of([b'+', b'-'])), digit1)),
)
.take()
.map(|bytes| str::from_utf8(bytes).unwrap().parse::<f64>().unwrap())
.parse_next(i)
}

fn string<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<&'a [u8], E> {
fn string<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> Result<&'a [u8], E> {
preceded(
'"',
cut_err(terminated(
terminated(
take_escaped(
none_of([b'\\', b'"']),
'\\',
one_of([b'\\', b'/', b'"', b'b', b'f', b'n', b'r', b't']),
),
'"',
)),
),
)
.parse_next(i)
}

fn array<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<Vec<JsonZero<'a>>, E> {
fn array<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> Result<Vec<JsonZero<'a>>, E> {
preceded(
'[',
cut_err(terminated(
terminated(
separated(0.., value, preceded(space, ',')),
preceded(space, ']'),
)),
),
)
.parse_next(i)
}

fn member<'a, E: ParserError<&'a [u8]>>(
i: &mut &'a [u8],
) -> PResult<(&'a [u8], JsonZero<'a>), E> {
separated_pair(
preceded(space, string),
cut_err(preceded(space, ':')),
value,
)
.parse_next(i)
) -> Result<(&'a [u8], JsonZero<'a>), E> {
separated_pair(preceded(space, string), preceded(space, ':'), value).parse_next(i)
}

fn object<'a, E: ParserError<&'a [u8]>>(
i: &mut &'a [u8],
) -> PResult<Vec<(&'a [u8], JsonZero<'a>)>, E> {
) -> Result<Vec<(&'a [u8], JsonZero<'a>)>, E> {
preceded(
'{',
cut_err(terminated(
terminated(
separated(0.., member, preceded(space, ',')),
preceded(space, '}'),
)),
),
)
.parse_next(i)
}

fn value<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<JsonZero<'a>, E> {
fn value<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> Result<JsonZero<'a>, E> {
preceded(
space,
dispatch!(peek(any);
b'n' => "null".value(JsonZero::Null),
b't' => "true".value(JsonZero::Bool(true)),
b'f' => "false".value(JsonZero::Bool(false)),
b'-' | b'0'..=b'9' => number.map(JsonZero::Num),
b'+' | b'-' | b'0'..=b'9' => number.map(JsonZero::Num),
b'"' => string.map(JsonZero::Str),
b'[' => array.map(JsonZero::Array),
b'{' => object.map(JsonZero::Object),
Expand All @@ -454,11 +446,11 @@ mod winnow {
.parse_next(i)
}

fn root<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<JsonZero<'a>, E> {
fn root<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> Result<JsonZero<'a>, E> {
terminated(value, space).parse_next(i)
}

pub fn json<'a>(i: &mut &'a [u8]) -> PResult<JsonZero<'a>, InputError<&'a [u8]>> {
pub fn json<'a>(i: &mut &'a [u8]) -> Result<JsonZero<'a>, EmptyError> {
root.parse_next(i)
}
}
Expand Down

0 comments on commit e4373d0

Please sign in to comment.