Skip to content

Commit f4da1a0

Browse files
author
meir
committed
some grammer optimizations
1 parent 594a1c3 commit f4da1a0

File tree

6 files changed

+326
-6
lines changed

6 files changed

+326
-6
lines changed

json_examples/data_array.json

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
[
2+
{
3+
"_id": "5741cfe6bf9f447a509a269e",
4+
"index": 0,
5+
"guid": "642f0c2a-3d87-43ac-8f82-25f004e0c96a",
6+
"isActive": false,
7+
"balance": "$3,666.68",
8+
"picture": "http://placehold.it/32x32",
9+
"age": 39,
10+
"eyeColor": "blue",
11+
"name": "Leonor Herman",
12+
"gender": "female",
13+
"company": "RODEOMAD",
14+
"email": "leonorherman@rodeomad.com",
15+
"phone": "+1 (848) 456-2962",
16+
"address": "450 Seeley Street, Iberia, North Dakota, 7859",
17+
"about": "Reprehenderit in anim laboris labore sint occaecat labore proident ipsum exercitation. Ut ea aliqua duis occaecat consectetur aliqua anim id. Dolor ea fugiat excepteur reprehenderit eiusmod enim non sit nisi. Mollit consequat anim mollit et excepteur qui laborum qui eiusmod. Qui ea amet incididunt cillum quis occaecat excepteur qui duis nisi. Dolore labore eu sunt consequat magna.\r\n",
18+
"registered": "2015-03-06T02:49:06 -02:00",
19+
"latitude": -29.402032,
20+
"longitude": 151.088135,
21+
"tags": [
22+
"Lorem",
23+
"voluptate",
24+
"aute",
25+
"ullamco",
26+
"elit",
27+
"esse",
28+
"culpa"
29+
],
30+
"friends": [
31+
{
32+
"id": 0,
33+
"name": "Millicent Norman"
34+
},
35+
{
36+
"id": 1,
37+
"name": "Vincent Cannon"
38+
},
39+
{
40+
"id": 2,
41+
"name": "Gray Berry"
42+
}
43+
],
44+
"greeting": "Hello, Leonor Herman! You have 4 unread messages.",
45+
"favoriteFruit": "apple"
46+
},
47+
{
48+
"_id": "5741cfe69424f42d4493caa2",
49+
"index": 1,
50+
"guid": "40ec6b43-e6e6-44e1-92a8-dc80cd5d7179",
51+
"isActive": true,
52+
"balance": "$2,923.78",
53+
"picture": "http://placehold.it/32x32",
54+
"age": 36,
55+
"eyeColor": "blue",
56+
"name": "Barton Barnes",
57+
"gender": "male",
58+
"company": "BRAINQUIL",
59+
"email": "bartonbarnes@brainquil.com",
60+
"phone": "+1 (907) 553-3739",
61+
"address": "644 Falmouth Street, Sedley, Michigan, 5602",
62+
"about": "Et nulla laboris consectetur laborum labore. Officia dolor sint do amet excepteur dolore eiusmod. Occaecat pariatur sunt velit sunt ullamco labore commodo mollit sint dolore occaecat.\r\n",
63+
"registered": "2014-08-28T01:07:22 -03:00",
64+
"latitude": 14.056553,
65+
"longitude": -61.911624,
66+
"tags": [
67+
"laboris",
68+
"sunt",
69+
"esse",
70+
"tempor",
71+
"pariatur",
72+
"occaecat",
73+
"et"
74+
],
75+
"friends": [
76+
{
77+
"id": 0,
78+
"name": "Tillman Mckay"
79+
},
80+
{
81+
"id": 1,
82+
"name": "Rivera Berg"
83+
},
84+
{
85+
"id": 2,
86+
"name": "Rosetta Erickson"
87+
}
88+
],
89+
"greeting": "Hello, Barton Barnes! You have 2 unread messages.",
90+
"favoriteFruit": "banana"
91+
}
92+
]
93+

json_examples/data_obj.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"_id": "5741cfe6bf9f447a509a269e",
3+
"index": 0,
4+
"guid": "642f0c2a-3d87-43ac-8f82-25f004e0c96a",
5+
"isActive": false,
6+
"balance": "$3,666.68",
7+
"picture": "http://placehold.it/32x32",
8+
"age": 39,
9+
"eyeColor": "blue",
10+
"name": "Leonor Herman",
11+
"gender": "female",
12+
"company": "RODEOMAD",
13+
"email": "leonorherman@rodeomad.com",
14+
"phone": "+1 (848) 456-2962",
15+
"address": "450 Seeley Street, Iberia, North Dakota, 7859",
16+
"about": "Reprehenderit in anim laboris labore sint occaecat labore proident ipsum exercitation. Ut ea aliqua duis occaecat consectetur aliqua anim id. Dolor ea fugiat excepteur reprehenderit eiusmod enim non sit nisi. Mollit consequat anim mollit et excepteur qui laborum qui eiusmod. Qui ea amet incididunt cillum quis occaecat excepteur qui duis nisi. Dolore labore eu sunt consequat magna.\r\n",
17+
"registered": "2015-03-06T02:49:06 -02:00",
18+
"latitude": -29.402032,
19+
"longitude": 151.088135,
20+
"tags": [
21+
"Lorem",
22+
"voluptate",
23+
"aute",
24+
"ullamco",
25+
"elit",
26+
"esse",
27+
"culpa"
28+
],
29+
"school": {
30+
"friends": [
31+
{
32+
"id": 0,
33+
"name": "Millicent Norman"
34+
},
35+
{
36+
"id": 1,
37+
"name": "Vincent Cannon"
38+
},
39+
{
40+
"id": 2,
41+
"name": "Gray Berry"
42+
}
43+
]
44+
},
45+
"friends": [
46+
{
47+
"id": 0
48+
},
49+
{
50+
"id": 1,
51+
"name": "Vincent Cannon"
52+
},
53+
{
54+
"id": 2,
55+
"name": "Gray Berry"
56+
}
57+
],
58+
"greeting": "Hello, Leonor Herman! You have 4 unread messages.",
59+
"favoriteFruit": "apple"
60+
}

json_examples/example.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"store": {
3+
"book": [
4+
{
5+
"category": "reference",
6+
"author": "Nigel Rees",
7+
"title": "Sayings of the Century",
8+
"price": 8.95
9+
},
10+
{
11+
"category": "fiction",
12+
"author": "Evelyn Waugh",
13+
"title": "Sword of Honour",
14+
"price": 12.99
15+
},
16+
{
17+
"category": "fiction",
18+
"author": "Herman Melville",
19+
"title": "Moby Dick",
20+
"isbn": "0-553-21311-3",
21+
"price": 8.99
22+
},
23+
{
24+
"category": "fiction",
25+
"author": "J. R. R. Tolkien",
26+
"title": "The Lord of the Rings",
27+
"isbn": "0-395-19395-8",
28+
"price": 22.99
29+
}
30+
],
31+
"bicycle": {
32+
"color": "red",
33+
"price": 19.95
34+
}
35+
},
36+
"expensive": 10
37+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
[
2+
{
3+
"book": [
4+
{
5+
"category": "reference",
6+
"author": "Nigel Rees",
7+
"title": "Sayings of the Century",
8+
"price": 8.95
9+
},
10+
{
11+
"category": "fiction",
12+
"author": "Evelyn Waugh",
13+
"title": "Sword of Honour",
14+
"price": 12.99
15+
},
16+
{
17+
"category": "fiction",
18+
"author": "Herman Melville",
19+
"title": "Moby Dick",
20+
"isbn": "0-553-21311-3",
21+
"price": 8.99
22+
},
23+
{
24+
"category": "fiction",
25+
"author": "J. R. R. Tolkien",
26+
"title": "The Lord of the Rings",
27+
"isbn": "0-395-19395-8",
28+
"price": 22.99
29+
}
30+
],
31+
"bicycle": {
32+
"color": "red",
33+
"price": 19.95
34+
}
35+
},
36+
10,
37+
[
38+
{
39+
"category": "reference",
40+
"author": "Nigel Rees",
41+
"title": "Sayings of the Century",
42+
"price": 8.95
43+
},
44+
{
45+
"category": "fiction",
46+
"author": "Evelyn Waugh",
47+
"title": "Sword of Honour",
48+
"price": 12.99
49+
},
50+
{
51+
"category": "fiction",
52+
"author": "Herman Melville",
53+
"title": "Moby Dick",
54+
"isbn": "0-553-21311-3",
55+
"price": 8.99
56+
},
57+
{
58+
"category": "fiction",
59+
"author": "J. R. R. Tolkien",
60+
"title": "The Lord of the Rings",
61+
"isbn": "0-395-19395-8",
62+
"price": 22.99
63+
}
64+
],
65+
{
66+
"color": "red",
67+
"price": 19.95
68+
},
69+
{
70+
"category": "reference",
71+
"author": "Nigel Rees",
72+
"title": "Sayings of the Century",
73+
"price": 8.95
74+
},
75+
{
76+
"category": "fiction",
77+
"author": "Evelyn Waugh",
78+
"title": "Sword of Honour",
79+
"price": 12.99
80+
},
81+
{
82+
"category": "fiction",
83+
"author": "Herman Melville",
84+
"title": "Moby Dick",
85+
"isbn": "0-553-21311-3",
86+
"price": 8.99
87+
},
88+
{
89+
"category": "fiction",
90+
"author": "J. R. R. Tolkien",
91+
"title": "The Lord of the Rings",
92+
"isbn": "0-395-19395-8",
93+
"price": 22.99
94+
},
95+
"reference",
96+
"Nigel Rees",
97+
"Sayings of the Century",
98+
8.95,
99+
"fiction",
100+
"Evelyn Waugh",
101+
"Sword of Honour",
102+
12.99,
103+
"fiction",
104+
"Herman Melville",
105+
"Moby Dick",
106+
"0-553-21311-3",
107+
8.99,
108+
"fiction",
109+
"J. R. R. Tolkien",
110+
"The Lord of the Rings",
111+
"0-395-19395-8",
112+
22.99,
113+
"red",
114+
19.95
115+
]
116+

src/grammer.pest

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
literal = @{(!("."|"*"|"\""|"'"|"["|"]"|"="|" "|"("|")"|"{"|"}"|"|"|"<"|">"|"!") ~ ANY)+}
1+
complex_literal = _{(!("."|"*"|"\""|"'"|"["|"]"|"="|" "|"("|")"|"{"|"}"|"|"|"<"|">"|"!") ~ ANY)+}
2+
literal = @{('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | "-" | "_")+ | complex_literal}
23

34
string = _{("'" ~ (string_value) ~ "'") | ("\"" ~ (string_value) ~ "\"") | ("\"" ~ (string_value_escape_1) ~ "\"") | ("'" ~ (string_value_escape_2) ~ "'")}
45
string_escape = @{"\\"}
@@ -57,7 +58,7 @@ full_scan = {".."}
5758

5859
bracket = _{numbers_range | numbers_list | string_list | all | "?" ~ filter}
5960

60-
element = _{full_scan ~ (literal | all | "[" ~ bracket ~ "]") | "." ~ literal | "." ~ all | ("."? ~ "[" ~ bracket ~ "]")}
61+
element = _{"." ~ literal | full_scan ~ (literal | all | "[" ~ bracket ~ "]") | "." ~ all | ("."? ~ "[" ~ bracket ~ "]")}
6162

6263
first_element = _{literal | all}
6364

src/json_path.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl<'i, 'j, S:SelectValue> TermEvaluationResult<'i, 'j, S> {
279279

280280
#[derive(Debug)]
281281
pub struct PathCalculator<'i, UPTG: UserPathTrackerGenerator>{
282-
query: &'i Query<'i>,
282+
query: Option<&'i Query<'i>>,
283283
tracker_generator: Option<UPTG>,
284284
}
285285

@@ -299,14 +299,14 @@ impl<'i, UPTG: UserPathTrackerGenerator> PathCalculator<'i, UPTG> {
299299

300300
pub fn create(query: &'i Query<'i>) -> PathCalculator<'i, UPTG> {
301301
PathCalculator {
302-
query: query,
302+
query: Some(query),
303303
tracker_generator: None,
304304
}
305305
}
306306

307307
pub fn create_with_generator(query: &'i Query<'i>, tracker_generator: UPTG) -> PathCalculator<'i, UPTG> {
308308
PathCalculator {
309-
query: query,
309+
query: Some(query),
310310
tracker_generator: Some(tracker_generator),
311311
}
312312
}
@@ -787,7 +787,7 @@ impl<'i, UPTG: UserPathTrackerGenerator> PathCalculator<'i, UPTG> {
787787

788788
pub fn calc_with_paths<'j:'i, S:SelectValue>(&self, json: &'j S) -> Vec<CalculationResult<'j, S, UPTG::PT>>
789789
{
790-
let root = self.query.query.clone().next().unwrap();
790+
let root = self.query.unwrap().query.clone().next().unwrap();
791791
let mut calc_data = PathCalculatorData{
792792
results: Vec::new(),
793793
root: json,
@@ -800,6 +800,19 @@ impl<'i, UPTG: UserPathTrackerGenerator> PathCalculator<'i, UPTG> {
800800
calc_data.results.drain(..).collect()
801801
}
802802

803+
pub fn calc_once<'j, 'p, S:SelectValue>(mut q: Query<'j>, json: &'p S) -> Vec<&'p S> {
804+
let root = q.query.next().unwrap();
805+
let mut calc_data = PathCalculatorData{
806+
results: Vec::new(),
807+
root: json,
808+
};
809+
PathCalculator::<'p, DummyTrackerGenerator> {
810+
query: None,
811+
tracker_generator: None,
812+
}.calc_internal(root.into_inner(), json, None, &mut calc_data, true);
813+
calc_data.results.into_iter().map(|e: CalculationResult<'p, S, DummyTracker>| e.res).collect()
814+
}
815+
803816
pub fn calc<'j:'i, S:SelectValue>(&self, json: &'j S) -> Vec<&'j S>
804817
{
805818
self.calc_with_paths(json).into_iter().map(|e| e.res).collect()

0 commit comments

Comments
 (0)