]> git.proxmox.com Git - rustc.git/blob - src/vendor/pest/benches/json.rs
New upstream version 1.26.2+dfsg1
[rustc.git] / src / vendor / pest / benches / json.rs
1 // pest. Elegant, efficient grammars
2 // Copyright (C) 2016 DragoČ™ Tiselice
3 //
4 // This Source Code Form is subject to the terms of the Mozilla Public
5 // License, v. 2.0. If a copy of the MPL was not distributed with this
6 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8 #![recursion_limit = "80"]
9 #![feature(test)]
10
11 extern crate test;
12
13 #[macro_use]
14 extern crate pest;
15
16 use std::fs::File;
17 use std::io::Read;
18
19 use test::Bencher;
20
21 use pest::prelude::*;
22
23 impl_rdp! {
24 grammar! {
25 json = { value ~ eoi }
26
27 object = { ["{"] ~ pair ~ ([","] ~ pair)* ~ ["}"] | ["{"] ~ ["}"] }
28 pair = { string ~ [":"] ~ value }
29
30 array = { ["["] ~ value ~ ([","] ~ value)* ~ ["]"] | ["["] ~ ["]"] }
31
32 value = { string | number | object | array | ["true"] | ["false"] | ["null"] }
33
34 string = @{ ["\""] ~ (escape | !(["\""] | ["\\"]) ~ any)* ~ ["\""] }
35 escape = { ["\\"] ~ (["\""] | ["\\"] | ["/"] | ["b"] | ["f"] | ["n"] | ["r"] | ["t"] | unicode) }
36 unicode = { ["u"] ~ hex ~ hex ~ hex ~ hex }
37 hex = { ['0'..'9'] | ['a'..'f'] | ['A'..'F'] }
38
39 number = @{ ["-"]? ~ int ~ (["."] ~ ['0'..'9']+ ~ exp? | exp)? }
40 int = { ["0"] | ['1'..'9'] ~ ['0'..'9']* }
41 exp = { (["E"] | ["e"]) ~ (["+"] | ["-"])? ~ int }
42
43 whitespace = _{ [" "] | ["\t"] | ["\r"] | ["\n"] }
44 }
45 }
46
47 #[bench]
48 fn data(b: &mut Bencher) {
49 let mut file = File::open("benches/data.json").unwrap();
50 let mut data = String::new();
51
52 file.read_to_string(&mut data).unwrap();
53
54 let mut parser = Rdp::new(StringInput::new(&data));
55
56 b.iter(|| {
57 parser.json();
58
59 parser.reset();
60 });
61 }