Skip to content

Commit 6432936

Browse files
committed
add part grammar
1 parent 0eaa748 commit 6432936

File tree

4 files changed

+204
-57
lines changed

4 files changed

+204
-57
lines changed

src/parser/errors2.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ pub enum JsonPathParserError {
3030
InvalidJsonPath(String),
3131
}
3232

33+
impl JsonPathParserError {
34+
pub fn empty(v:&str) -> Self {
35+
JsonPathParserError::EmptyInner(v.to_string())
36+
}
37+
}
38+
39+
impl From<&str> for JsonPathParserError {
40+
fn from(val: &str) -> Self {
41+
JsonPathParserError::EmptyInner(val.to_string())
42+
}
43+
}
3344

3445
impl From<(ParseIntError, &str)> for JsonPathParserError {
3546
fn from((err, val): (ParseIntError, &str)) -> Self {

src/parser/grammar/json_path_9535.pest

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ logical_expr_and = {atom_expr ~ S ~ ("&&" ~ S ~ atom_expr)*}
2222
atom_expr = {paren_expr | comp_expr| test_expr}
2323
paren_expr = {not_op? ~ S ~ "(" ~ S ~ logical_expr_or ~ S ~ ")"}
2424
comp_expr = { comparable ~ S ~ comp_op ~ S ~ comparable }
25-
test_expr = {not_op? ~ S ~ (filter_query | function_expr)}
25+
test_expr = {not_op? ~ S ~ test}
26+
test = {rel_query | jp_query | function_expr}
27+
rel_query = {curr ~ S ~ segments}
2628
function_expr = { function_name ~ "(" ~ S ~ (function_argument ~ (S ~ "," ~ S ~ function_argument)*)? ~ S ~ ")" }
2729
function_name = { function_name_first ~ function_name_char* }
2830
function_name_first = { LCALPHA }
2931
function_name_char = { function_name_first | "_" | DIGIT }
30-
function_argument = { literal | filter_query | logical_expr_or | function_expr }
31-
filter_query = {rel_query | jp_query}
32-
rel_query = {curr ~ S ~ segments}
32+
function_argument = { literal | test | logical_expr_or }
3333
comparable = { literal | singular_query | function_expr }
3434
literal = { number | string | bool | null }
3535
bool = {"true" | "false"}

src/parser/model2.rs

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@ use crate::parser::parser2::Parsed;
44

55
/// Represents a JSONPath query with a list of segments.
66
#[derive(Debug, Clone)]
7-
struct JpQuery {
7+
pub struct JpQuery {
88
segments: Vec<Segment>
99
}
1010

11+
impl JpQuery {
12+
pub fn new(segments: Vec<Segment>) -> Self {
13+
JpQuery {
14+
segments
15+
}
16+
}
17+
}
18+
1119
impl Display for JpQuery {
1220
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1321
write!(f, "${}", self.segments.iter().map(|s| s.to_string()).collect::<String>())
1422
}
1523
}
1624
/// Enum representing different types of segments in a JSONPath query.
1725
#[derive(Debug, Clone)]
18-
enum Segment {
26+
pub enum Segment {
1927
/// Represents a descendant segment.
2028
Descendant,
2129
/// Represents a selector segment.
@@ -61,7 +69,7 @@ impl Display for Selector {
6169
}
6270
/// Enum representing different types of filters in a JSONPath query.
6371
#[derive(Debug, Clone)]
64-
enum Filter {
72+
pub enum Filter {
6573
/// Represents a logical OR filter.
6674
Or(Box<Filter>, Box<Filter>),
6775
/// Represents a logical AND filter.
@@ -85,7 +93,7 @@ impl Display for Filter {
8593

8694
/// Enum representing different types of atomic filters in a JSONPath query.
8795
#[derive(Debug, Clone)]
88-
enum FilterAtom {
96+
pub enum FilterAtom {
8997
/// Represents a nested filter with an optional NOT flag.
9098
Filter {
9199
expr: Box<Filter>,
@@ -100,6 +108,20 @@ enum FilterAtom {
100108
Comparison(Box<Comparison>),
101109
}
102110

111+
impl FilterAtom {
112+
pub fn filter(expr: Filter, not: bool) -> Self {
113+
FilterAtom::Filter { expr:Box::new(expr), not }
114+
}
115+
116+
pub fn test(expr: Test, not: bool) -> Self {
117+
FilterAtom::Test { expr:Box::new(expr), not }
118+
}
119+
120+
pub fn cmp(cmp: Box<Comparison>) -> Self {
121+
FilterAtom::Comparison(cmp)
122+
}
123+
}
124+
103125
impl Display for FilterAtom {
104126
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
105127
match self {
@@ -123,7 +145,7 @@ impl Display for FilterAtom {
123145
}
124146
/// Enum representing different types of comparisons in a JSONPath query.
125147
#[derive(Debug, Clone)]
126-
enum Comparison {
148+
pub enum Comparison {
127149
/// Represents an equality comparison.
128150
Eq(Comparable, Comparable),
129151
/// Represents a non-equality comparison.
@@ -138,6 +160,20 @@ enum Comparison {
138160
Lte(Comparable, Comparable),
139161
}
140162

163+
impl Comparison {
164+
pub fn try_new(op: &str, left: Comparable, right: Comparable) -> Parsed<Self> {
165+
match op {
166+
"==" => Ok(Comparison::Eq(left, right)),
167+
"!=" => Ok(Comparison::Ne(left, right)),
168+
">" => Ok(Comparison::Gt(left, right)),
169+
">=" => Ok(Comparison::Gte(left, right)),
170+
"<" => Ok(Comparison::Lt(left, right)),
171+
"<=" => Ok(Comparison::Lte(left, right)),
172+
_ => Err(JsonPathParserError::InvalidJsonPath(format!("Invalid comparison operator: {}", op))),
173+
}
174+
}
175+
}
176+
141177
impl Display for Comparison {
142178
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
143179
match self {
@@ -153,7 +189,7 @@ impl Display for Comparison {
153189

154190
/// Enum representing different types of comparable values in a JSONPath query.
155191
#[derive(Debug, Clone)]
156-
enum Comparable {
192+
pub enum Comparable {
157193
/// Represents a literal value.
158194
Literal(Literal),
159195
/// Represents a function.
@@ -210,7 +246,7 @@ impl Display for SingularQuerySegment {
210246

211247
/// Enum representing different types of tests in a JSONPath query.
212248
#[derive(Debug, Clone)]
213-
enum Test {
249+
pub enum Test {
214250
/// Represents a relative query.
215251
RelQuery(Vec<Segment>),
216252
/// Represents an absolute query.
@@ -231,7 +267,7 @@ impl Display for Test {
231267

232268
/// Enum representing different types of test functions in a JSONPath query.
233269
#[derive(Debug, Clone)]
234-
enum TestFunction {
270+
pub enum TestFunction {
235271
/// Represents a custom function.
236272
Custom(String, Vec<FnArg>),
237273
/// Represents a length function.
@@ -247,7 +283,7 @@ enum TestFunction {
247283
}
248284

249285
impl TestFunction {
250-
pub fn new(name: &str, args: Vec<FnArg>) -> Parsed<Self> {
286+
pub fn try_new(name: &str, args: Vec<FnArg>) -> Parsed<Self> {
251287
match (name,args.as_slice()) {
252288
("length",[a]) => Ok(TestFunction::Length(Box::new(a.clone()))),
253289
("value",[a]) => Ok(TestFunction::Value(a.clone())),
@@ -276,7 +312,7 @@ impl Display for TestFunction {
276312

277313
/// Enum representing different types of function arguments in a JSONPath query.
278314
#[derive(Debug, Clone)]
279-
enum FnArg {
315+
pub enum FnArg {
280316
/// Represents a literal argument.
281317
Literal(Literal),
282318
/// Represents a test argument.

0 commit comments

Comments
 (0)