1
1
use crate :: parser:: errors:: JsonPathParserError :: ParserError ;
2
2
use crate :: parser:: errors:: { parser_err, JsonPathParserError } ;
3
- use crate :: parser:: model:: FilterExpression :: { And , Or } ;
3
+ use crate :: parser:: model:: FilterExpression :: { And , Not , Or } ;
4
4
use crate :: parser:: model:: {
5
5
FilterExpression , FilterSign , Function , JsonPath , JsonPathIndex , Operand ,
6
6
} ;
@@ -145,10 +145,10 @@ fn parse_chain_in_operand(rule: Pair<Rule>) -> Result<Operand, JsonPathParserErr
145
145
}
146
146
147
147
fn parse_filter_index ( pair : Pair < Rule > ) -> Result < JsonPathIndex , JsonPathParserError > {
148
- Ok ( JsonPathIndex :: Filter ( parse_logic ( pair. into_inner ( ) ) ?) )
148
+ Ok ( JsonPathIndex :: Filter ( parse_logic_or ( pair. into_inner ( ) ) ?) )
149
149
}
150
150
151
- fn parse_logic ( pairs : Pairs < Rule > ) -> Result < FilterExpression , JsonPathParserError > {
151
+ fn parse_logic_or ( pairs : Pairs < Rule > ) -> Result < FilterExpression , JsonPathParserError > {
152
152
let mut expr: Option < FilterExpression > = None ;
153
153
let error_message = format ! ( "Failed to parse logical expression: {:?}" , pairs) ;
154
154
for pair in pairs {
@@ -168,7 +168,7 @@ fn parse_logic_and(pairs: Pairs<Rule>) -> Result<FilterExpression, JsonPathParse
168
168
let mut expr: Option < FilterExpression > = None ;
169
169
let error_message = format ! ( "Failed to parse logical `and` expression: {:?}" , pairs, ) ;
170
170
for pair in pairs {
171
- let next_expr = parse_logic_atom ( pair. into_inner ( ) ) ?;
171
+ let next_expr = parse_logic_not ( pair. into_inner ( ) ) ?;
172
172
match expr {
173
173
None => expr = Some ( next_expr) ,
174
174
Some ( e) => expr = Some ( And ( Box :: new ( e) , Box :: new ( next_expr) ) ) ,
@@ -180,10 +180,26 @@ fn parse_logic_and(pairs: Pairs<Rule>) -> Result<FilterExpression, JsonPathParse
180
180
}
181
181
}
182
182
183
+ fn parse_logic_not ( mut pairs : Pairs < Rule > ) -> Result < FilterExpression , JsonPathParserError > {
184
+ if let Some ( rule) = pairs. peek ( ) . map ( |x| x. as_rule ( ) ) {
185
+ match rule {
186
+ Rule :: not => {
187
+ pairs. next ( ) . expect ( "unreachable in arithmetic: should have a value as pairs.peek() was Some(_)" ) ;
188
+ parse_logic_not ( pairs)
189
+ . map ( |expr|Not ( Box :: new ( expr) ) )
190
+ } ,
191
+ Rule :: logic_atom => parse_logic_atom ( pairs. next ( ) . expect ( "unreachable in arithmetic: should have a value as pairs.peek() was Some(_)" ) . into_inner ( ) ) ,
192
+ x => Err ( JsonPathParserError :: UnexpectedRuleLogicError ( x, pairs) ) ,
193
+ }
194
+ } else {
195
+ Err ( JsonPathParserError :: UnexpectedNoneLogicError ( pairs) )
196
+ }
197
+ }
198
+
183
199
fn parse_logic_atom ( mut pairs : Pairs < Rule > ) -> Result < FilterExpression , JsonPathParserError > {
184
200
if let Some ( rule) = pairs. peek ( ) . map ( |x| x. as_rule ( ) ) {
185
201
match rule {
186
- Rule :: logic => parse_logic ( pairs. next ( ) . expect ( "unreachable in arithmetic: should have a value as pairs.peek() was Some(_)" ) . into_inner ( ) ) ,
202
+ Rule :: logic_or => parse_logic_or ( pairs. next ( ) . expect ( "unreachable in arithmetic: should have a value as pairs.peek() was Some(_)" ) . into_inner ( ) ) ,
187
203
Rule :: atom => {
188
204
let left: Operand = parse_atom ( pairs. next ( ) . unwrap ( ) ) ?;
189
205
if pairs. peek ( ) . is_none ( ) {
0 commit comments