Skip to content

Commit

Permalink
Reworked file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zesterer committed Mar 12, 2022
1 parent 44d0f5b commit 96248fe
Show file tree
Hide file tree
Showing 13 changed files with 1,920 additions and 1,657 deletions.
83 changes: 48 additions & 35 deletions benches/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ static JSON: &'static [u8] = include_bytes!("sample.json");

#[bench]
fn chumsky_zero_copy(b: &mut Bencher) {
use ::chumsky::input::*;
use ::chumsky::zero_copy::prelude::*;

let json = chumsky_zero_copy::json();
b.iter(|| black_box(json.parse(JSON).unwrap()));
}

#[bench]
fn chumsky_zero_copy_check(b: &mut Bencher) {
use ::chumsky::input::*;
use ::chumsky::zero_copy::prelude::*;

let json = chumsky_zero_copy::json();
b.iter(|| black_box(json.check(JSON).unwrap()));
Expand Down Expand Up @@ -69,7 +69,7 @@ fn nom(b: &mut Bencher) {
}

mod chumsky_zero_copy {
use chumsky::input::*;
use chumsky::zero_copy::prelude::*;

use super::JsonZero;
use std::str;
Expand All @@ -85,7 +85,8 @@ mod chumsky_zero_copy {

let frac = just(b'.').then(digits.clone());

let exp = just(b'e').or(just(b'E'))
let exp = just(b'e')
.or(just(b'E'))
.then(just(b'+').or(just(b'-')).or_not())
.then(digits.clone());

Expand All @@ -97,20 +98,23 @@ mod chumsky_zero_copy {
.map_slice(|bytes| str::from_utf8(bytes).unwrap().parse().unwrap())
.boxed();

let escape = just(b'\\').then(choice((
just(b'\\'),
just(b'/'),
just(b'"'),
just(b'b').to(b'\x08'),
just(b'f').to(b'\x0C'),
just(b'n').to(b'\n'),
just(b'r').to(b'\r'),
just(b't').to(b'\t'),
)))
let escape = just(b'\\')
.then(choice((
just(b'\\'),
just(b'/'),
just(b'"'),
just(b'b').to(b'\x08'),
just(b'f').to(b'\x0C'),
just(b'n').to(b'\n'),
just(b'r').to(b'\r'),
just(b't').to(b'\t'),
)))
.ignored()
.boxed();

let string = filter(|c| *c != b'\\' && *c != b'"').ignored().or(escape)
let string = filter(|c| *c != b'\\' && *c != b'"')
.ignored()
.or(escape)
.repeated()
.map_slice(|bytes| bytes)
.delimited_by(just(b'"'), just(b'"'))
Expand All @@ -119,32 +123,41 @@ mod chumsky_zero_copy {
let array = value
.clone()
// .separated_by(just(b',').padded())
.then(just(b',').padded())
.map(|(x, _)| x)
.repeated()
.collect::<Vec<_>>()
.then(value.clone().or_not())
.map(|(mut xs, x)| {
if let Some(x) = x { xs.push(x); }
xs
})
.then(just(b',').padded())
.map(|(x, _)| x)
.repeated()
.collect::<Vec<_>>()
.then(value.clone().or_not())
.map(|(mut xs, x)| {
if let Some(x) = x {
xs.push(x);
}
xs
})
.padded()
.delimited_by(just(b'['), just(b']'))
.map(JsonZero::Array)
.boxed();

let member = string.clone().then(just(b':').padded()).map(|(s, _)| s).then(value);
let object = member.clone()
let member = string
.clone()
.then(just(b':').padded())
.map(|(s, _)| s)
.then(value);
let object = member
.clone()
//.separated_by(just(b',').padded())
.then(just(b',').padded())
.map(|(x, _)| x)
.repeated()
.collect::<Vec<_>>()
.then(member.clone().or_not())
.map(|(mut xs, x)| {
if let Some(x) = x { xs.push(x); }
xs
})
.then(just(b',').padded())
.map(|(x, _)| x)
.repeated()
.collect::<Vec<_>>()
.then(member.clone().or_not())
.map(|(mut xs, x)| {
if let Some(x) = x {
xs.push(x);
}
xs
})
.padded()
.delimited_by(just(b'{'), just(b'}'))
// .collect::<Vec<(_, JsonZero)>>()
Expand Down
Loading

0 comments on commit 96248fe

Please sign in to comment.