-
-
Notifications
You must be signed in to change notification settings - Fork 250
/
json.rs
74 lines (59 loc) · 2.06 KB
/
json.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
extern crate criterion;
extern crate pest;
extern crate pest_grammars;
use criterion::{criterion_group, criterion_main, Criterion};
use std::fs::File;
use std::io::Read;
use pest::Parser;
use pest_grammars::json::*;
fn criterion_benchmark(c: &mut Criterion) {
let mut file = File::open("benches/data.json").unwrap();
let mut data = String::new();
file.read_to_string(&mut data).unwrap();
c.bench_function("json parser", |b| {
b.iter(|| JsonParser::parse(Rule::json, &data).unwrap())
});
}
mod autocorrect {
use pest_derive::Parser;
#[derive(Parser)]
#[grammar_inline = r#"
newline = ${ "\n" | "\r" }
space = ${ " "+ }
other = ${ !(pair) ~ ANY }
comment = ${ single_line_comment | multiline_comment }
single_line_comment = _{ "//" ~ (!(newline) ~ ANY)* }
multiline_comment = _{ "/*" ~ (!("*/") ~ ANY)* ~ "*/"}
string_type = _{
("\"" ~ (!(newline | "\"") ~ ANY)* ~ "\"")
}
key = ${ string_type ~ (" ")* ~ ":" ~ (" ")* }
string = ${ string_type }
pair = _{ key ~ string }
line = _{ pair | comment | space | other | newline }
item = _{ SOI ~ line* ~ EOI }
"#]
pub struct JsonParser;
}
fn line_col_benchmark(c: &mut Criterion) {
let mut file = File::open("benches/main.i18n.json").unwrap();
let mut data = String::new();
file.read_to_string(&mut data).unwrap();
let pairs = autocorrect::JsonParser::parse(autocorrect::Rule::item, &data).unwrap();
let last_pair = pairs.last().unwrap();
c.bench_function("line col", |b| {
b.iter(|| {
let _ = last_pair.as_span().start_pos().line_col();
});
});
}
criterion_group!(benches, criterion_benchmark, line_col_benchmark,);
criterion_main!(benches);