Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit 2ad8241

Browse files
author
Marko Mikulicic
committed
Use new AST base parser by default
1 parent f5514c9 commit 2ad8241

File tree

8 files changed

+56
-433
lines changed

8 files changed

+56
-433
lines changed

src/ast.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,12 @@ fn abs_index(index: i64, node: &Value) -> usize {
104104
#[cfg(test)]
105105
mod test {
106106
use super::*;
107-
use crate::parser_ast::parse;
107+
use crate::parser::parse;
108108
use serde_json::json;
109109

110110
#[test]
111111
fn demo() {
112-
let a1 = Path::Sel(
113-
Box::new(Path::Root),
114-
Selector::DotName("foo".to_owned()),
115-
);
112+
let a1 = Path::Sel(Box::new(Path::Root), Selector::DotName("foo".to_owned()));
116113
let a2 = Path::Sel(Box::new(a1), Selector::DotName("bar".to_owned()));
117114
let a3 = Path::Sel(
118115
Box::new(a2),

src/jsonpath.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
66

7+
use crate::ast;
78
use crate::parser;
8-
use crate::path::Path;
9+
use serde_json::Value;
910

1011
#[derive(Debug)]
1112
pub struct SyntaxError {
@@ -18,6 +19,19 @@ impl std::fmt::Display for SyntaxError {
1819
}
1920
}
2021

21-
pub fn parse(selector: &str) -> Result<impl Path, SyntaxError> {
22-
parser::parse(selector).map_err(|m| SyntaxError { message: m })
22+
pub enum FindError {
23+
// no errors yet
24+
}
25+
26+
pub fn parse(selector: &str) -> Result<Path, SyntaxError> {
27+
let p = parser::parse(selector).map_err(|m| SyntaxError { message: m })?;
28+
Ok(Path(p))
29+
}
30+
31+
pub struct Path(ast::Path);
32+
33+
impl Path {
34+
pub fn find<'a>(&'a self, document: &'a Value) -> Result<Vec<&'a Value>, FindError> {
35+
Ok(self.0.find(document).collect())
36+
}
2337
}

src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ extern crate pest;
88
#[macro_use]
99
extern crate pest_derive;
1010

11+
pub mod ast;
1112
pub mod jsonpath;
12-
mod matchers;
1313
mod parser;
14-
pub mod path;
15-
16-
pub mod ast;
17-
pub mod parser_ast;

src/matchers.rs

Lines changed: 0 additions & 176 deletions
This file was deleted.

src/parser.rs

Lines changed: 35 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,119 +4,77 @@
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
66

7-
use crate::matchers;
8-
use crate::path;
7+
pub use crate::ast::*;
98
use crate::pest::Parser;
109

1110
#[derive(Parser)]
1211
#[grammar = "grammar.pest"]
1312
struct PathParser;
1413

15-
pub fn parse(selector: &str) -> Result<impl path::Path, String> {
14+
pub fn parse(selector: &str) -> Result<Path, String> {
1615
let selector_rule = PathParser::parse(Rule::selector, selector)
1716
.map_err(|e| format!("{}", e))?
1817
.next()
1918
.unwrap();
2019

21-
let mut ms: Vec<Box<dyn matchers::Matcher>> = Vec::new();
20+
let mut res = Path::Root;
2221
for r in selector_rule.into_inner() {
23-
match r.as_rule() {
24-
Rule::rootSelector => ms.push(Box::new(matchers::RootSelector {})),
25-
26-
Rule::matcher => {
27-
for m in parse_matcher(r) {
28-
ms.push(m)
29-
}
30-
}
31-
32-
_ => println!("r={:?}", r),
22+
res = match r.as_rule() {
23+
Rule::rootSelector => res, // TODO: fix grammar so that this is a silent rule since we don't need it
24+
Rule::matcher => Path::Sel(Box::new(res), parse_selector(r)),
25+
_ => panic!("invalid parse tree {:?}", r),
3326
}
3427
}
35-
36-
Ok(path::new(ms))
28+
Ok(res)
3729
}
3830

39-
fn parse_matcher(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Box<dyn matchers::Matcher>> {
40-
let mut ms: Vec<Box<dyn matchers::Matcher>> = Vec::new();
41-
for r in matcher_rule.into_inner() {
42-
match r.as_rule() {
43-
Rule::wildcardedDotChild => ms.push(Box::new(matchers::WildcardedChild {})),
44-
45-
Rule::namedDotChild => {
46-
for m in parse_dot_child_matcher(r) {
47-
ms.push(m)
48-
}
49-
}
50-
51-
Rule::union => {
52-
for m in parse_union(r) {
53-
ms.push(m)
54-
}
55-
}
31+
fn parse_selector(matcher_rule: pest::iterators::Pair<Rule>) -> Selector {
32+
let r = matcher_rule.into_inner().next().unwrap();
5633

57-
_ => (),
58-
}
34+
match r.as_rule() {
35+
Rule::wildcardedDotChild => Selector::DotWildcard,
36+
Rule::namedDotChild => Selector::DotName(parse_child_name(r)),
37+
Rule::union => Selector::Union(parse_union_indices(r)),
38+
_ => panic!("invalid parse tree {:?}", r),
5939
}
60-
ms
6140
}
6241

63-
fn parse_dot_child_matcher(
64-
matcher_rule: pest::iterators::Pair<Rule>,
65-
) -> Vec<Box<dyn matchers::Matcher>> {
66-
let mut ms: Vec<Box<dyn matchers::Matcher>> = Vec::new();
67-
for r in matcher_rule.into_inner() {
68-
if let Rule::childName = r.as_rule() {
69-
ms.push(Box::new(matchers::Child::new(r.as_str().to_owned())));
70-
}
42+
fn parse_child_name(matcher_rule: pest::iterators::Pair<Rule>) -> String {
43+
let r = matcher_rule.into_inner().next().unwrap();
44+
match r.as_rule() {
45+
Rule::childName => r.as_str().to_owned(),
46+
_ => panic!("invalid parse tree {:?}", r),
7147
}
72-
ms
7348
}
7449

75-
fn parse_union(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Box<dyn matchers::Matcher>> {
76-
let mut ms: Vec<Box<dyn matchers::Matcher>> = Vec::new();
50+
fn parse_union_indices(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Index> {
51+
let mut res = Vec::new();
52+
7753
for r in matcher_rule.into_inner() {
7854
match r.as_rule() {
79-
Rule::unionChild => {
80-
for m in parse_union_child(r) {
81-
ms.push(m)
82-
}
83-
}
84-
Rule::unionArrayIndex => {
85-
for m in parse_union_array_index(r) {
86-
ms.push(m)
87-
}
88-
}
89-
_ => {}
55+
Rule::unionChild => res.append(&mut parse_union_child(r)),
56+
Rule::unionArrayIndex => res.push(parse_union_array_index(r)),
57+
_ => panic!("invalid parse tree {:?}", r),
9058
}
9159
}
92-
vec![Box::new(matchers::Union::new(ms))]
60+
res
9361
}
9462

95-
fn parse_union_child(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Box<dyn matchers::Matcher>> {
96-
let mut ms: Vec<Box<dyn matchers::Matcher>> = Vec::new();
63+
fn parse_union_child(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<Index> {
64+
let mut res = Vec::new();
9765
for r in matcher_rule.into_inner() {
9866
match r.as_rule() {
99-
Rule::doubleInner => {
100-
ms.push(Box::new(matchers::Child::new(unescape(r.as_str()))));
101-
}
102-
103-
Rule::singleInner => {
104-
ms.push(Box::new(matchers::Child::new(unescape_single(r.as_str()))));
105-
}
106-
107-
_ => (),
67+
Rule::doubleInner => res.push(Index::Field(unescape(r.as_str()))),
68+
Rule::singleInner => res.push(Index::Field(unescape_single(r.as_str()))),
69+
_ => panic!("invalid parse tree {:?}", r),
10870
}
10971
}
110-
ms
72+
res
11173
}
11274

113-
fn parse_union_array_index(
114-
matcher_rule: pest::iterators::Pair<Rule>,
115-
) -> Vec<Box<dyn matchers::Matcher>> {
116-
let mut ms: Vec<Box<dyn matchers::Matcher>> = Vec::new();
75+
fn parse_union_array_index(matcher_rule: pest::iterators::Pair<Rule>) -> Index {
11776
let i = matcher_rule.as_str().parse().unwrap();
118-
ms.push(Box::new(matchers::ArrayIndex::new(i)));
119-
ms
77+
Index::Number(i)
12078
}
12179

12280
fn unescape(contents: &str) -> String {

0 commit comments

Comments
 (0)