Skip to content

Commit

Permalink
style: reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
j5ik2o committed Feb 28, 2025
1 parent ffaf004 commit 49edf2b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 50 deletions.
16 changes: 6 additions & 10 deletions parser/benches/bench_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

use criterion::*;

use crate::oni_comb_json::oni_comb_parse_json;
use crate::nom_json::nom_parse_json;
use crate::oni_comb_json::oni_comb_parse_json;
use crate::pom_json::pom_parse_json;
// use pprof::criterion::{Output, PProfProfiler};

mod oni_comb_json;
mod nom_json;
mod oni_comb_json;
mod pom_json;

/// 異なる複雑さのJSONデータを用意
Expand Down Expand Up @@ -71,16 +71,12 @@ fn criterion_benchmark(criterion: &mut Criterion) {
group.bench_with_input(BenchmarkId::new("oni-comb-rs", name), data, |b, i| {
b.iter(|| oni_comb_parse_json(i))
});

// nomパーサーのベンチマーク
group.bench_with_input(BenchmarkId::new("nom", name), data, |b, i| {
b.iter(|| nom_parse_json(i))
});

group.bench_with_input(BenchmarkId::new("nom", name), data, |b, i| b.iter(|| nom_parse_json(i)));

// pomパーサーのベンチマーク
group.bench_with_input(BenchmarkId::new("pom", name), data, |b, i| {
b.iter(|| pom_parse_json(i))
});
group.bench_with_input(BenchmarkId::new("pom", name), data, |b, i| b.iter(|| pom_parse_json(i)));
}

group.finish();
Expand Down
53 changes: 15 additions & 38 deletions parser/benches/nom_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,15 @@ fn null<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, (), E> {
/// parsing the string
/// - `context` lets you add a static string to provide more information in the
/// error chain (to indicate which parser had an error)
fn string<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
i: &'a str,
) -> IResult<&'a str, &'a str, E> {
context(
"string",
preceded(char('\"'), cut(terminated(parse_str, char('\"')))),
)
.parse(i)
fn string<'a, E: ParseError<&'a str> + ContextError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
context("string", preceded(char('\"'), cut(terminated(parse_str, char('\"'))))).parse(i)
}

/// some combinators, like `separated_list0` or `many0`, will call a parser repeatedly,
/// accumulating results in a `Vec`, until it encounters an error.
/// If you want more control on the parser application, check out the `iterator`
/// combinator (cf `examples/iterator.rs`)
fn array<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
i: &'a str,
) -> IResult<&'a str, Vec<JsonValue>, E> {
fn array<'a, E: ParseError<&'a str> + ContextError<&'a str>>(i: &'a str) -> IResult<&'a str, Vec<JsonValue>, E> {
context(
"array",
preceded(
Expand All @@ -116,18 +108,13 @@ fn array<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
)),
),
)
.parse(i)
.parse(i)
}

fn key_value<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
i: &'a str,
) -> IResult<&'a str, (&'a str, JsonValue), E> {
separated_pair(
preceded(sp, string),
cut(preceded(sp, char(':'))),
json_value,
)
.parse(i)
separated_pair(preceded(sp, string), cut(preceded(sp, char(':'))), json_value).parse(i)
}

fn hash<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
Expand All @@ -138,26 +125,18 @@ fn hash<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
preceded(
char('{'),
cut(terminated(
map(
separated_list0(preceded(sp, char(',')), key_value),
|tuple_vec| {
tuple_vec
.into_iter()
.map(|(k, v)| (String::from(k), v))
.collect()
},
),
map(separated_list0(preceded(sp, char(',')), key_value), |tuple_vec| {
tuple_vec.into_iter().map(|(k, v)| (String::from(k), v)).collect()
}),
preceded(sp, char('}')),
)),
),
)
.parse(i)
.parse(i)
}

/// here, we apply the space parser before trying to parse a value
fn json_value<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
i: &'a str,
) -> IResult<&'a str, JsonValue, E> {
fn json_value<'a, E: ParseError<&'a str> + ContextError<&'a str>>(i: &'a str) -> IResult<&'a str, JsonValue, E> {
preceded(
sp,
alt((
Expand All @@ -169,13 +148,11 @@ fn json_value<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
map(null, |_| JsonValue::Null),
)),
)
.parse(i)
.parse(i)
}

/// the root element of a JSON parser is either an object or an array
fn root<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
i: &'a str,
) -> IResult<&'a str, JsonValue, E> {
fn root<'a, E: ParseError<&'a str> + ContextError<&'a str>>(i: &'a str) -> IResult<&'a str, JsonValue, E> {
delimited(
sp,
alt((
Expand All @@ -185,16 +162,16 @@ fn root<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
)),
opt(sp),
)
.parse(i)
.parse(i)
}

pub fn nom_parse_json(s: &str) {
match json_value::<(&str, ErrorKind)>(s) {
Ok((_, _)) => {
// パース成功
},
}
Err(_) => {
// パース失敗(ベンチマークでは無視)
}
}
}
}
2 changes: 1 addition & 1 deletion parser/src/core/committed_status.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// A structure representing the commit status of the parser.<br/>
/// パーサのコミット状態を表す構造体。
#[derive(Debug, Clone, PartialOrd, PartialEq)]
#[derive(Debug, Clone, Copy, PartialOrd, PartialEq)]
pub enum CommittedStatus {
Committed,
Uncommitted,
Expand Down
2 changes: 1 addition & 1 deletion parser/src/core/parse_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<'a, I, A> ParseResult<'a, I, A> {
ParseResult::Failure {
committed_status: is_committed,
..
} => Some(is_committed.clone()),
} => Some(*is_committed),
_ => None,
}
}
Expand Down

0 comments on commit 49edf2b

Please sign in to comment.