11#[ cfg( feature = "compiled-path" ) ]
22pub ( crate ) mod parse_impl {
33 use crate :: ast:: parse:: { JSPathParser , Rule } ;
4+ use crate :: ast:: { kw, CompOp , IndexSelector , Main , NameSelector } ;
45 use crate :: ast:: {
56 AbsSingularQuery , AtomExpr , Bool , BracketName , BracketedSelection , ChildSegment , CompExpr ,
6- Comparable , DescendantSegment , EOI , FilterSelector , FunctionArgument , FunctionExpr ,
7- FunctionName , IndexSegment , JPQuery , JSInt , JSString , Literal , LogicalExpr , LogicalExprAnd ,
8- MemberNameShorthand , NameSegment , NotOp , Null , Number , ParenExpr , PestIgnoredPunctuated ,
9- PestLiteralWithoutRule , RelQuery , RelSingularQuery , Root , Segment , Segments , Selector ,
10- SingularQuery , SingularQuerySegment , SingularQuerySegments , SliceEnd , SliceSelector ,
11- SliceStart , SliceStep , Test , TestExpr , WildcardSelector ,
12- WildcardSelectorOrMemberNameShorthand ,
7+ Comparable , DescendantSegment , FilterSelector , FunctionArgument , FunctionExpr , FunctionName ,
8+ IndexSegment , JPQuery , JSInt , JSString , Literal , LogicalExpr , LogicalExprAnd , MemberNameShorthand ,
9+ NameSegment , NotOp , Null , Number , ParenExpr , PestIgnoredPunctuated , PestLiteralWithoutRule ,
10+ RelQuery , RelSingularQuery , Root , Segment , Segments , Selector , SingularQuery ,
11+ SingularQuerySegment , SingularQuerySegments , SliceEnd , SliceSelector , SliceStart ,
12+ SliceStep , Test , TestExpr , WildcardSelector , WildcardSelectorOrMemberNameShorthand ,
13+ EOI ,
1314 } ;
14- use crate :: ast:: { CompOp , IndexSelector , Main , NameSelector , kw} ;
1515 use pest:: Parser ;
1616 use proc_macro2:: { Ident , TokenStream } ;
17- use quote:: { ToTokens , quote } ;
17+ use quote:: { quote , ToTokens } ;
1818 use syn:: parse:: { Parse , ParseStream } ;
1919 use syn:: punctuated:: Punctuated ;
20+ use syn:: spanned:: Spanned ;
2021 use syn:: token:: Token ;
21- use syn:: { LitBool , LitInt , LitStr , Token , token } ;
22+ use syn:: { token , LitBool , LitInt , LitStr , Token } ;
2223
2324 pub trait ParseUtilsExt : Parse {
2425 fn peek ( input : ParseStream ) -> bool ;
@@ -629,11 +630,27 @@ pub(crate) mod parse_impl {
629630
630631 impl ToTokens for FunctionName {
631632 fn to_tokens ( & self , tokens : & mut TokenStream ) {
632- tokens. extend ( quote ! {
633- :: jsonpath_ast:: ast:: FunctionName :: new(
634- :: proc_macro2:: Ident :: new( "function_name" , :: proc_macro2:: Span :: call_site( ) )
635- )
636- } ) ;
633+ // tokens.extend(quote! {
634+ // ::jsonpath_ast::ast::FunctionName::new(
635+ // ::proc_macro2::Ident::new("function_name", ::proc_macro2::Span::call_site())
636+ // )
637+ // });
638+ let variant = match self {
639+ // Literal::Number(inner) => {
640+ // quote!(new_number(#inner))
641+ // }
642+ FunctionName :: Length ( _) => { quote ! ( new_length( Default :: default ( ) ) ) }
643+ FunctionName :: Value ( _) => { quote ! ( new_value( Default :: default ( ) ) ) }
644+ FunctionName :: Count ( _) => { quote ! ( new_count( Default :: default ( ) ) ) }
645+ FunctionName :: Search ( _) => { quote ! ( new_search( Default :: default ( ) ) ) }
646+ FunctionName :: Match ( _) => { quote ! ( new_match( Default :: default ( ) ) ) }
647+ FunctionName :: In ( _) => { quote ! ( new_in( Default :: default ( ) ) ) }
648+ FunctionName :: Nin ( _) => { quote ! ( new_nin( Default :: default ( ) ) ) }
649+ FunctionName :: NoneOf ( _) => { quote ! ( new_none_of( Default :: default ( ) ) ) }
650+ FunctionName :: AnyOf ( _) => { quote ! ( new_any_of( Default :: default ( ) ) ) }
651+ FunctionName :: SubsetOf ( _) => { quote ! ( new_subset_of( Default :: default ( ) ) ) }
652+ } ;
653+ tokens. extend ( quote ! ( :: jsonpath_ast:: ast:: FunctionName :: #variant) )
637654 }
638655 }
639656
@@ -840,9 +857,13 @@ pub(crate) mod parse_impl {
840857 impl ToTokens for TestExpr {
841858 fn to_tokens ( & self , tokens : & mut TokenStream ) {
842859 let Self { not_op, test } = self ;
860+ let repr_not = match not_op {
861+ Some ( not_op) => quote ! { Some ( #not_op) } ,
862+ None => quote ! { None } ,
863+ } ;
843864 tokens. extend ( quote ! {
844865 :: jsonpath_ast:: ast:: TestExpr :: new(
845- #not_op ,
866+ #repr_not ,
846867 #test
847868 )
848869 } ) ;
@@ -992,7 +1013,11 @@ pub(crate) mod parse_impl {
9921013
9931014 impl ParseUtilsExt for CompExpr {
9941015 fn peek ( input : ParseStream ) -> bool {
995- Comparable :: peek ( input)
1016+ let fork = input. fork ( ) ;
1017+ // This is very suboptimal but the only option because at this point in the stream a comp_expr and a test_expr
1018+ // look identical if they're both functions, IE: $[?match(@, $.regex)] is a test_exp while $[?match(@, $.regex) == true]
1019+ // is a comp_exp
1020+ fork. parse :: < Comparable > ( ) . is_ok ( ) && fork. parse :: < CompOp > ( ) . is_ok ( )
9961021 }
9971022 }
9981023 impl ParseUtilsExt for TestExpr {
@@ -1084,6 +1109,27 @@ pub(crate) mod parse_impl {
10841109 Ok ( num)
10851110 }
10861111
1112+ fn function_name_expected_args ( name : & FunctionName ) -> ( String , usize ) {
1113+ ( format ! ( "{:?}" , name) , match name {
1114+ FunctionName :: Length ( _) | FunctionName :: Value ( _) | FunctionName :: Count ( _) => { 1 } ,
1115+ FunctionName :: Search ( _) | FunctionName :: Match ( _)
1116+ | FunctionName :: In ( _) | FunctionName :: Nin ( _)
1117+ | FunctionName :: NoneOf ( _) | FunctionName :: AnyOf ( _) | FunctionName :: SubsetOf ( _) => { 2 } ,
1118+ } )
1119+ }
1120+ impl Parse for FunctionExpr {
1121+ fn parse ( __input : ParseStream ) -> :: syn:: Result < Self > {
1122+ let paren;
1123+ let ret = Self { name : __input. parse ( ) ?, paren : syn:: parenthesized!( paren in __input ) , args : PestIgnoredPunctuated :: parse_separated_nonempty ( & paren) ? } ;
1124+ let ( func_name, expected_num_args) = function_name_expected_args ( & ret. name ) ;
1125+ if expected_num_args == ret. args . 0 . len ( ) {
1126+ Ok ( ret)
1127+ } else {
1128+ Err ( syn:: Error :: new ( ret. args . span ( ) , format ! ( "Invalid number of arguments for function {}, expected {}" , func_name, expected_num_args) ) )
1129+ }
1130+ }
1131+ }
1132+
10871133 impl ParseUtilsExt for FunctionExpr {
10881134 fn peek ( input : ParseStream ) -> bool {
10891135 FunctionName :: peek ( input)
@@ -1105,53 +1151,6 @@ pub(crate) mod parse_impl {
11051151 }
11061152 }
11071153
1108- pub fn validate_function_name ( input : ParseStream ) -> Result < Ident , syn:: Error > {
1109- if input. peek ( kw:: length) {
1110- input. parse :: < kw:: length > ( ) ?;
1111- return Ok ( Ident :: new ( "length" , input. span ( ) ) ) ;
1112- }
1113- if input. peek ( kw:: value) {
1114- input. parse :: < kw:: value > ( ) ?;
1115- return Ok ( Ident :: new ( "value" , input. span ( ) ) ) ;
1116- }
1117- if input. peek ( kw:: count) {
1118- input. parse :: < kw:: count > ( ) ?;
1119- return Ok ( Ident :: new ( "count" , input. span ( ) ) ) ;
1120- }
1121- if input. peek ( kw:: search) {
1122- input. parse :: < kw:: search > ( ) ?;
1123- return Ok ( Ident :: new ( "search" , input. span ( ) ) ) ;
1124- }
1125- if input. peek ( Token ! [ match ] ) {
1126- input. parse :: < Token ! [ match ] > ( ) ?;
1127- return Ok ( Ident :: new ( "match" , input. span ( ) ) ) ;
1128- }
1129- if input. peek ( Token ! [ in] ) {
1130- input. parse :: < Token ! [ in] > ( ) ?;
1131- return Ok ( Ident :: new ( "in" , input. span ( ) ) ) ;
1132- }
1133- if input. peek ( kw:: nin) {
1134- input. parse :: < kw:: nin > ( ) ?;
1135- return Ok ( Ident :: new ( "nin" , input. span ( ) ) ) ;
1136- }
1137- if input. peek ( kw:: none_of) {
1138- input. parse :: < kw:: none_of > ( ) ?;
1139- return Ok ( Ident :: new ( "none_of" , input. span ( ) ) ) ;
1140- }
1141- if input. peek ( kw:: any_of) {
1142- input. parse :: < kw:: any_of > ( ) ?;
1143- return Ok ( Ident :: new ( "any_of" , input. span ( ) ) ) ;
1144- }
1145- if input. peek ( kw:: subset_of) {
1146- input. parse :: < kw:: subset_of > ( ) ?;
1147- return Ok ( Ident :: new ( "subset_of" , input. span ( ) ) ) ;
1148- }
1149- Err ( syn:: Error :: new (
1150- input. span ( ) ,
1151- "invalid function name, expected one of: length, value, count, search, match, in, nin, none_of, any_of, subset_of" ,
1152- ) )
1153- }
1154-
11551154 impl ParseUtilsExt for RelQuery {
11561155 fn peek ( input : ParseStream ) -> bool {
11571156 input. peek ( Token ! [ @] )
0 commit comments