Skip to content

Commit 5da3a60

Browse files
committed
add part to parse
1 parent 6432936 commit 5da3a60

File tree

5 files changed

+222
-59
lines changed

5 files changed

+222
-59
lines changed

src/parser/grammar/json_path_9535.pest

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ step = {":" ~ S ~ int?}
1616
start = {int}
1717
end = {int}
1818
slice_selector = { start? ~ S ~ ":" ~ S ~ end? ~ S ~ step? }
19-
filter_selector = {"?"~ S ~ logical_expr_or}
20-
logical_expr_or = {logical_expr_and ~ S ~ ("||" ~ S ~ logical_expr_and)*}
19+
filter_selector = {"?"~ S ~ logical_expr}
20+
logical_expr = {logical_expr_and ~ S ~ ("||" ~ S ~ logical_expr_and)*}
2121
logical_expr_and = {atom_expr ~ S ~ ("&&" ~ S ~ atom_expr)*}
2222
atom_expr = {paren_expr | comp_expr| test_expr}
23-
paren_expr = {not_op? ~ S ~ "(" ~ S ~ logical_expr_or ~ S ~ ")"}
23+
paren_expr = {not_op? ~ S ~ "(" ~ S ~ logical_expr ~ S ~ ")"}
2424
comp_expr = { comparable ~ S ~ comp_op ~ S ~ comparable }
2525
test_expr = {not_op? ~ S ~ test}
2626
test = {rel_query | jp_query | function_expr}
@@ -29,7 +29,7 @@ function_expr = { function_name ~ "(" ~ S ~ (function_argument ~ (S ~ "," ~ S ~
2929
function_name = { function_name_first ~ function_name_char* }
3030
function_name_first = { LCALPHA }
3131
function_name_char = { function_name_first | "_" | DIGIT }
32-
function_argument = { literal | test | logical_expr_or }
32+
function_argument = { literal | test | logical_expr }
3333
comparable = { literal | singular_query | function_expr }
3434
literal = { number | string | bool | null }
3535
bool = {"true" | "false"}

src/parser/macros2.rs

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
use crate::parser::model2::{Literal, SingularQuery};
1+
use crate::parser::model2::{Filter, FilterAtom, FnArg, Literal, SingularQuery, Test};
22

33
#[macro_export]
44
macro_rules! lit {
5-
() => { Literal::Null };
6-
(b$b:expr ) => { Literal::Bool($b) };
7-
(s$s:expr) => { Literal::String($s.to_string()) };
8-
(i$n:expr) => { Literal::Int($n) };
9-
(f$n:expr) => { Literal::Float($n) };
5+
() => {
6+
Literal::Null
7+
};
8+
(b$b:expr ) => {
9+
Literal::Bool($b)
10+
};
11+
(s$s:expr) => {
12+
Literal::String($s.to_string())
13+
};
14+
(i$n:expr) => {
15+
Literal::Int($n)
16+
};
17+
(f$n:expr) => {
18+
Literal::Float($n)
19+
};
1020
}
1121

1222
#[macro_export]
@@ -24,9 +34,15 @@ macro_rules! q_segments {
2434

2535
#[macro_export]
2636
macro_rules! q_segment {
27-
($name:ident) => { SingularQuerySegment::Name(stringify!($name).to_string()) };
28-
([$name:ident]) => { SingularQuerySegment::Name(format!("\"{}\"", stringify!($name))) };
29-
([$index:expr]) => { SingularQuerySegment::Index($index) };
37+
($name:ident) => {
38+
SingularQuerySegment::Name(stringify!($name).to_string())
39+
};
40+
([$name:ident]) => {
41+
SingularQuerySegment::Name(format!("\"{}\"", stringify!($name)))
42+
};
43+
([$index:expr]) => {
44+
SingularQuerySegment::Index($index)
45+
};
3046
}
3147
#[macro_export]
3248
macro_rules! singular_query {
@@ -63,5 +79,76 @@ macro_rules! slice {
6379
};
6480
($start:expr, $end:expr, $step:expr) => {
6581
(Some($start), Some($end), Some($step))
82+
};
83+
}
84+
85+
#[macro_export]
86+
macro_rules! test_fn {
87+
($name:ident $arg:expr) => {
88+
TestFunction::try_new(stringify!($name), vec![$arg]).unwrap()
89+
};
90+
($name:ident $arg1:expr, $arg2:expr ) => {
91+
TestFunction::try_new(stringify!($name), vec![$arg1, $arg2]).unwrap()
92+
};
93+
}
94+
95+
#[macro_export]
96+
macro_rules! arg {
97+
($arg:expr) => {
98+
FnArg::Literal($arg)
99+
};
100+
(t $arg:expr) => {
101+
FnArg::Test(Box::new($arg))
102+
};
103+
(f $arg:expr) => {
104+
FnArg::Filter($arg)
66105
}
106+
}
107+
108+
#[macro_export]
109+
macro_rules! test {
110+
(@ $($segments:expr)*) => { Test::RelQuery(vec![$($segments),*]) };
111+
(S $jq:expr) => { Test::AbsQuery($jq) };
112+
($tf:expr) => { Test::Function(Box::new($tf)) };
113+
114+
}
115+
116+
#[macro_export]
117+
macro_rules! or {
118+
($($items:expr)*) => {
119+
Filter::Or(vec![ $($items),* ])
120+
};
121+
}
122+
123+
#[macro_export]
124+
macro_rules! and {
125+
($($items:expr)*) => {
126+
Filter::And(vec![ $($items),* ])
127+
};
128+
}
129+
130+
#[macro_export]
131+
macro_rules! atom {
132+
(! $filter:expr) => {
133+
FilterAtom::filter($filter, true)
134+
};
135+
($filter:expr) => {
136+
FilterAtom::filter($filter, false)
137+
};
138+
(t! $filter:expr) => {
139+
FilterAtom::filter($filter, true)
140+
};
141+
(t $filter:expr) => {
142+
FilterAtom::filter($filter, false)
143+
};
144+
($lhs:expr, $s:ident, $rhs:expr) => {
145+
FilterAtom::Comparison(Box::new(cmp!($lhs, $s, $rhs)))
146+
};
147+
}
148+
149+
#[macro_export]
150+
macro_rules! cmp {
151+
($lhs:expr, $s:ident, $rhs:expr) => {
152+
Comparison::try_new($lhs, stringify!($s), $rhs).unwrap()
153+
}
67154
}

src/parser/model2.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::parser::errors2::JsonPathParserError;
33
use crate::parser::parser2::Parsed;
44

55
/// Represents a JSONPath query with a list of segments.
6-
#[derive(Debug, Clone)]
6+
#[derive(Debug, Clone, PartialEq)]
77
pub struct JpQuery {
88
segments: Vec<Segment>
99
}
@@ -22,7 +22,7 @@ impl Display for JpQuery {
2222
}
2323
}
2424
/// Enum representing different types of segments in a JSONPath query.
25-
#[derive(Debug, Clone)]
25+
#[derive(Debug, Clone, PartialEq)]
2626
pub enum Segment {
2727
/// Represents a descendant segment.
2828
Descendant,
@@ -32,6 +32,12 @@ pub enum Segment {
3232
Selectors(Vec<Selector>),
3333
}
3434

35+
impl Segment {
36+
pub fn name(name:&str) -> Self{
37+
Segment::Selector(Selector::Name(name.to_string()))
38+
}
39+
}
40+
3541
impl Display for Segment {
3642
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
3743
match self {
@@ -42,16 +48,16 @@ impl Display for Segment {
4248
}
4349
}
4450
/// Enum representing different types of selectors in a JSONPath query.
45-
#[derive(Debug, Clone)]
46-
enum Selector {
51+
#[derive(Debug, Clone, PartialEq)]
52+
pub enum Selector {
4753
/// Represents a name selector.
4854
Name(String),
4955
/// Represents a wildcard selector.
5056
Wildcard,
5157
/// Represents an index selector.
5258
Index(i64),
5359
/// Represents a slice selector.
54-
Slice(i64, i64, i64),
60+
Slice(Option<i64>, Option<i64>, Option<i64>),
5561
/// Represents a filter selector.
5662
Filter(Filter),
5763
}
@@ -62,37 +68,41 @@ impl Display for Selector {
6268
Selector::Name(name) => write!(f, "{}", name),
6369
Selector::Wildcard => write!(f, "*"),
6470
Selector::Index(index) => write!(f, "{}", index),
65-
Selector::Slice(start, end, step) => write!(f, "{}:{}:{}", start, end, step),
71+
Selector::Slice(start, end, step) => write!(f, "{}:{}:{}",
72+
start.unwrap_or(0),
73+
end.unwrap_or(0),
74+
step.unwrap_or(1)),
6675
Selector::Filter(filter) => write!(f, "[?{}]", filter),
6776
}
6877
}
6978
}
7079
/// Enum representing different types of filters in a JSONPath query.
71-
#[derive(Debug, Clone)]
80+
#[derive(Debug, Clone, PartialEq)]
7281
pub enum Filter {
7382
/// Represents a logical OR filter.
74-
Or(Box<Filter>, Box<Filter>),
83+
Or(Vec<Filter>),
7584
/// Represents a logical AND filter.
76-
And(Box<Filter>, Box<Filter>),
77-
/// Represents a logical NOT filter.
78-
Not(Box<Filter>),
85+
And(Vec<Filter>),
7986
/// Represents an atomic filter.
8087
Atom(FilterAtom),
8188
}
8289

8390
impl Display for Filter {
8491
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
92+
93+
let items_to_str = |items: &Vec<Filter>, sep:&str|
94+
items.iter().map(|f| f.to_string()).collect::<Vec<_>>().join(sep);
95+
8596
match self {
86-
Filter::Or(left, right) => write!(f, "{} || {}", left, right),
87-
Filter::And(left, right) => write!(f, "{} && {}", left, right),
88-
Filter::Not(expr) => write!(f, "!{}", expr),
97+
Filter::Or(filters) => write!(f, "{}", items_to_str(filters, " || ")),
98+
Filter::And(filters) => write!(f, "{}", items_to_str(filters, " && ")),
8999
Filter::Atom(atom) => write!(f, "{}", atom),
90100
}
91101
}
92102
}
93103

94104
/// Enum representing different types of atomic filters in a JSONPath query.
95-
#[derive(Debug, Clone)]
105+
#[derive(Debug, Clone, PartialEq)]
96106
pub enum FilterAtom {
97107
/// Represents a nested filter with an optional NOT flag.
98108
Filter {
@@ -144,7 +154,7 @@ impl Display for FilterAtom {
144154
}
145155
}
146156
/// Enum representing different types of comparisons in a JSONPath query.
147-
#[derive(Debug, Clone)]
157+
#[derive(Debug, Clone, PartialEq)]
148158
pub enum Comparison {
149159
/// Represents an equality comparison.
150160
Eq(Comparable, Comparable),
@@ -188,7 +198,7 @@ impl Display for Comparison {
188198
}
189199

190200
/// Enum representing different types of comparable values in a JSONPath query.
191-
#[derive(Debug, Clone)]
201+
#[derive(Debug, Clone, PartialEq)]
192202
pub enum Comparable {
193203
/// Represents a literal value.
194204
Literal(Literal),
@@ -245,7 +255,7 @@ impl Display for SingularQuerySegment {
245255
}
246256

247257
/// Enum representing different types of tests in a JSONPath query.
248-
#[derive(Debug, Clone)]
258+
#[derive(Debug, Clone, PartialEq)]
249259
pub enum Test {
250260
/// Represents a relative query.
251261
RelQuery(Vec<Segment>),
@@ -266,7 +276,7 @@ impl Display for Test {
266276
}
267277

268278
/// Enum representing different types of test functions in a JSONPath query.
269-
#[derive(Debug, Clone)]
279+
#[derive(Debug, Clone, PartialEq)]
270280
pub enum TestFunction {
271281
/// Represents a custom function.
272282
Custom(String, Vec<FnArg>),
@@ -311,7 +321,7 @@ impl Display for TestFunction {
311321
}
312322

313323
/// Enum representing different types of function arguments in a JSONPath query.
314-
#[derive(Debug, Clone)]
324+
#[derive(Debug, Clone, PartialEq)]
315325
pub enum FnArg {
316326
/// Represents a literal argument.
317327
Literal(Literal),

0 commit comments

Comments
 (0)