
    !isL                     z    S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr   " S S\5      r	\ " S	 S
\5      5       r
g)a  Top down operator precedence parser.

This is an implementation of Vaughan R. Pratt's
"Top Down Operator Precedence" parser.
(http://dl.acm.org/citation.cfm?doid=512927.512931).

These are some additional resources that help explain the
general idea behind a Pratt parser:

* http://effbot.org/zone/simple-top-down-parsing.htm
* http://javascript.crockford.com/tdop/tdop.html

A few notes on the implementation.

* All the nud/led tokens are on the Parser class itself, and are dispatched
  using getattr().  This keeps all the parsing logic contained to a single
  class.
* We use two passes through the data.  One to create a list of token,
  then one pass through the tokens to create the AST.  While the lexer actually
  yields tokens, we convert it to a list so we can easily implement two tokens
  of lookahead.  A previous implementation used a fixed circular buffer, but it
  was significantly slower.  Also, the average jmespath expression typically
  does not have a large amount of token so this is not an issue.  And
  interestingly enough, creating a token list first is actually faster than
  consuming from the token iterator one token at a time.

    )lexer)with_repr_method)ast)
exceptions)visitorc                      \ rS rSr0 SS_SS_SS_SS_SS_SS_S	S_S
S_SS_SS_SS_SS_SS_SS_SS_SS_SS_SSSSSSSSSSSSS .ErS!r0 rS"rSWS# jrS$ r	S% r
S& rSXS' jrS( rS) rS* rS+ rS, rS- rS. rS/ rS0 rS1 rS2 rS3 rS4 rS5 rS6 rS7 rS8 rS9 rS: rS; r S< r!S= r"S> r#S? r$S@ r%SA r&SB r'SC r(SD r)SE r*SF r+SG r,SH r-SI r.SJ r/SK r0SYSM jr1SN r2SO r3SP r4SQ r5SR r6SS r7ST r8\9SU 5       r:SVr;gL)ZParser#   eofr   unquoted_identifierquoted_identifierliteralrbracketrparencommarbracenumbercurrentexprefcolonpipe   or   and   eq   gt	         (   -   2   7   <   )ltgtelteneflattenstarfilterdotnotlbracelbracketlparen
   i   c                 B    S U l         S /U-  U l        Xl        SU l        g Nr   )	tokenizer_tokens_buffer_size_index)self	lookaheads     N/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/jmespath/parser.py__init__Parser.__init__L   s$    v	)%    c                 Z    U R                   U   $ ! [         a     Of = fU R                  U5      n[        U R                   5      U R                  :  aH   U R                   [        [        U R                   5      5      	 O! [        [        [        4 a    Us $ f = fX R                   U'   U$ N)	_CACHEKeyError	_do_parselen	_MAX_SIZEnextiterStopIterationRuntimeError)r;   
expressionparsed_results      r=   parseParser.parseR   s    	;;z** 		z2t{{t~~-%KKT$++%6 78m\: 
% %$
% #0Js    
)B   BBc                      U R                  U5      $ ! [        R                   a  nXl        e S nAf[        R                   a  nUR                  U5        e S nAf[        R                   a  nXl        e S nAff = frB   )_parser   
LexerErrorrL   IncompleteExpressionErrorset_expression
ParseError)r;   rL   es      r=   rE   Parser._do_parsei   sk    
	;;z**$$ 	%L33 	Z($$ 	%L	s*    A:.A:AA:.A55A:c                 d   [         R                  " 5       R                  U5      U l        [	        U R                  5      U l        SU l        U R                  SS9nU R                  5       S:X  d9  U R                  S5      n[        R                  " US   US   US   SUS   -  5      e[        X5      $ )Nr   )binding_powerr   startvaluetypezUnexpected token: %s)r   Lexertokenizer7   listr8   r:   _expression_current_token_lookahead_tokenr   rU   ParsedResult)r;   rL   parsedts       r=   rQ   Parser._parsev   s    //
;DNN+!!!2""$-%%a(A'''
AgJ&	(>7(KM MJ//r@   c                    U R                  S5      nU R                  5         [        U SUS   -  U R                  5      nU" U5      nU R	                  5       nXR
                  U   :  ar  [        U SU-  S 5      nUc#  U R                  S5      nU R                  U5        O(U R                  5         U" U5      nU R	                  5       nXR
                  U   :  a  Mr  U$ )Nr   z_token_nud_%sr\   z_token_led_%s)rb   _advancegetattr_error_nud_tokenra   BINDING_POWER_error_led_token)r;   rY   
left_tokennud_functionleftcurrent_tokenlederror_tokens           r=   r`   Parser._expression   s    **1-
/Jv$66!!# J'++-00??$- ?FC{"33A6%%k24y $ 3 3 5 00?? r@   c                 4    [         R                  " US   5      $ Nr[   )r   r   r;   tokens     r=   _token_nud_literalParser._token_nud_literal   s    {{5>**r@   c                 4    [         R                  " US   5      $ ru   )r   fieldrv   s     r=   _token_nud_unquoted_identifier%Parser._token_nud_unquoted_identifier   s    yyw((r@   c                     [         R                  " US   5      nU R                  5       S:X  a0  U R                  S5      n[        R
                  " SUS   US   S5      eU$ )Nr[   r3   r   r\   z1Quoted identifier not allowed for function names.)r   r{   ra   rb   r   rU   )r;   rw   r{   re   s       r=   _token_nud_quoted_identifier#Parser._token_nud_quoted_identifier   se    		%.)  H,%%a(A''1W:qyCE E r@   c                     [         R                  " 5       nU R                  5       S:X  a  [         R                  " 5       nOU R                  U R                  S   5      n[         R
                  " X#5      $ )Nr   r-   )r   identityra   _parse_projection_rhsrk   value_projectionr;   rw   ro   rights       r=   _token_nud_starParser._token_nud_star   sU    ||~ J.LLNE..t/A/A&/IJE##D00r@   c                 J    U R                  [        R                  " 5       5      $ rB   )_token_led_filterr   r   rv   s     r=   _token_nud_filterParser._token_nud_filter   s    %%clln55r@   c                 "    U R                  5       $ rB   )_parse_multi_select_hashrv   s     r=   _token_nud_lbraceParser._token_nud_lbrace   s    ,,..r@   c                 H    U R                  5       nU R                  S5        U$ )Nr   )r`   _matchr;   rw   rL   s      r=   _token_nud_lparenParser._token_nud_lparen   s"    %%'
Hr@   c                     [         R                  " [         R                  " 5       5      nU R                  U R                  S   5      n[         R
                  " X#5      $ Nr,   )r   r,   r   r   rk   
projectionr   s       r=   _token_nud_flattenParser._token_nud_flatten   sC    {{3<<>***y)+~~d**r@   c                 j    U R                  U R                  S   5      n[        R                  " U5      $ )Nr0   )r`   rk   r   not_expression)r;   rw   exprs      r=   _token_nud_notParser._token_nud_not   s.     2 25 9:!!$''r@   c                    U R                  5       S;   a5  U R                  5       nU R                  [        R                  " 5       U5      $ U R                  5       S:X  a}  U R                  S5      S:X  ah  U R                  5         U R                  5         U R                  U R                  S   5      n[        R                  " [        R                  " 5       U5      $ U R                  5       $ )Nr   r   r-   r   r   )ra   _parse_index_expression_project_if_slicer   r   
_lookaheadrh   r   rk   r   _parse_multi_select_list)r;   rw   r   s      r=   _token_nud_lbracketParser._token_nud_lbracket   s     $77002E
 ))#,,.%@@  "f,"j0MMOMMO..t/A/A&/IJE>>#,,.%880022r@   c                    U R                  S5      S:X  d  U R                  S5      S:X  a  U R                  5       $ [        R                  " U R	                  S5      S   5      nU R                  5         U R                  S5        U$ )Nr   r   r   r[   r   )r   _parse_slice_expressionr   indexrb   rh   r   )r;   nodes     r=   r   Parser._parse_index_expression   so    
 OOA')"g-//11 99T2215g>?DMMOKK
#Kr@   c                    / SQnSnU R                  5       nUS:X  d  US:  a  US:X  a=  US-  nUS:X  a!  U R                  U R                  S5      S5        U R                  5         ONUS:X  a'  U R                  S5      S	   X'   U R                  5         O!U R                  U R                  S5      S5        U R                  5       nUS:X  d  US:  a  M  U R	                  S5        [
        R                  " U6 $ )
N)NNNr   r   r   r   r   syntax errorr   r[   )ra   _raise_parse_error_for_tokenrb   rh   r   r   slice)r;   partsr   rp   s       r=   r   Parser._parse_slice_expression   s     #++-:-%!)'
A:55--a0.B(*#44Q7@11))!,n> //1M  :-%!) 	Jyy%  r@   c                 ,    [         R                  " 5       $ rB   )r   current_noderv   s     r=   _token_nud_currentParser._token_nud_current   s    !!r@   c                 j    U R                  U R                  S   5      n[        R                  " U5      $ )Nr   )r`   rk   r   r   r   s      r=   _token_nud_exprefParser._token_nud_expref   s,    %%d&8&8&BC
zz*%%r@   c                 Z   U R                  5       S:X  dT  U R                  U R                  S   5      nUS   S:X  a  US   R                  U5        U$ [        R
                  " X/5      $ U R                  5         U R                  U R                  S   5      n[        R                  " X5      $ )Nr-   r/   r\   subexpressionchildren)	ra   _parse_dot_rhsrk   appendr   r   rh   r   r   r;   ro   r   s      r=   _token_led_dotParser._token_led_dot  s    ""$.''(:(:5(ABEF|.Z ''.(($77 MMO..""5)+E''44r@   c                 j    U R                  U R                  S   5      n[        R                  " X5      $ )Nr   )r`   rk   r   r   r   s      r=   _token_led_pipeParser._token_led_pipe  s,      !3!3F!;<xx$$r@   c                 j    U R                  U R                  S   5      n[        R                  " X5      $ )Nr   )r`   rk   r   or_expressionr   s      r=   _token_led_orParser._token_led_or  s.      !3!3D!9:  --r@   c                 j    U R                  U R                  S   5      n[        R                  " X5      $ )Nr   )r`   rk   r   and_expressionr   s      r=   _token_led_andParser._token_led_and  s.      !3!3E!:;!!$..r@   c                    US   S:w  a9  U R                  S5      n[        R                  " US   US   US   SUS   -  5      eUS   n/ nU R                  5       S:X  d\  U R	                  5       nU R                  5       S:X  a  U R                  S5        UR                  U5        U R                  5       S:X  d  M\  U R                  S5        [        R                  " X45      nU$ )	Nr\   r{   rZ   r[   zInvalid function name '%s'r   r   )	rb   r   rU   ra   r`   r   r   r   function_expression)r;   ro   prev_tnameargsrL   function_nodes          r=   _token_led_lparenParser._token_led_lparen  s    <7" **2.F''w&.,vg>@ @ G}%%'83))+J""$/G$KK
#	 %%'83
 	H//;r@   c                    U R                  S5      nU R                  S5        U R                  5       S:X  a  [        R                  " 5       nOU R                  U R                  S   5      n[        R                  " XU5      $ )Nr   r   r,   r.   )r`   r   ra   r   r   r   rk   filter_projection)r;   ro   	conditionr   s       r=   r   Parser._token_led_filter0  sh    $$Q'	J I-LLNE..t/A/A(/KLE$$T)<<r@   c                 &    U R                  US5      $ )Nr   _parse_comparatorr;   ro   s     r=   _token_led_eqParser._token_led_eq:      %%dD11r@   c                 &    U R                  US5      $ )Nr+   r   r   s     r=   _token_led_neParser._token_led_ne=  r   r@   c                 &    U R                  US5      $ )Nr   r   r   s     r=   _token_led_gtParser._token_led_gt@  r   r@   c                 &    U R                  US5      $ )Nr)   r   r   s     r=   _token_led_gteParser._token_led_gteC      %%dE22r@   c                 &    U R                  US5      $ )Nr(   r   r   s     r=   _token_led_ltParser._token_led_ltF  r   r@   c                 &    U R                  US5      $ )Nr*   r   r   s     r=   _token_led_lteParser._token_led_lteI  r   r@   c                     [         R                  " U5      nU R                  U R                  S   5      n[         R                  " X5      $ r   )r   r,   r   rk   r   r   s      r=   _token_led_flattenParser._token_led_flattenL  s=    {{4 **y)+~~d**r@   c                 b   U R                  S5      nUS   S;   a@  U R                  5       nUS   S:X  a  US   R                  U5        U$ U R                  X5      $ U R	                  S5        U R	                  S5        U R                  U R                  S   5      n[        R                  " X5      $ )Nr   r\   r   index_expressionr   r-   r   )	rb   r   r   r   r   r   rk   r   r   )r;   ro   rw   r   s       r=   _token_led_lbracketParser._token_led_lbracketR  s    %%a(=//002EF|11 Z ''.--d:: KKKK
#..t/A/A&/IJE>>$..r@   c                     [         R                  " X/5      nUS   S:X  a3  [         R                  " UU R                  U R                  S   5      5      $ U$ )Nr\   r   r-   )r   r   r   r   rk   )r;   ro   r   
index_exprs       r=   r   Parser._project_if_slicee  sW    ))4-8
=G#>>**4+=+=f+EFH H r@   c                 l    U R                  U R                  U   5      n[        R                  " X!U5      $ rB   )r`   rk   r   
comparator)r;   ro   r   r   s       r=   r   Parser._parse_comparatorn  s.      !3!3J!?@~~j66r@   c                     / n U R                  5       nUR                  U5        U R                  5       S:X  a  OU R                  S5        MI  U R                  S5        [        R
                  " U5      $ )Nr   r   )r`   r   ra   r   r   multi_select_list)r;   expressionsrL   s      r=   r   Parser._parse_multi_select_listr  sj    ))+Jz*""$
2G$  	J$$[11r@   c                    / n U R                  S5      nU R                  SS/S9  US   nU R                  S5        U R                  S5      n[        R
                  " X4S9nUR                  U5        U R                  5       S:X  a  U R                  S5        O&U R                  5       S	:X  a  U R                  S	5        OM  [        R                  " US
9$ )Nr   r   r   )token_typesr[   r   )key_namer   r   r   )nodes)	rb   _match_multiple_tokensr   r`   r   key_val_pairr   ra   multi_select_dict)r;   pairs	key_tokenr  r[   r   s         r=   r   Parser._parse_multi_select_hash~  s    --a0I ''02GH ( J )HKK $$Q'E##XBDLL""$/G$$$&(2H%   $$511r@   c                    U R                   U R                  5          U R                  :  a  [        R                  " 5       nU$ U R                  5       S:X  a  U R                  U5      nU$ U R                  5       S:X  a  U R                  U5      nU$ U R                  5       S:X  a$  U R                  S5        U R                  U5      nU$ U R                  U R                  S5      S5        W$ )Nr2   r.   r/   r   r   )
rk   ra   _PROJECTION_STOPr   r   r`   r   r   r   rb   )r;   rY   r   s      r=   r   Parser._parse_projection_rhs  s    d1134t7L7LLLLNE    "j0$$]3E    "h.$$]3E    "e+KK''6E  --d.C.CA.F.<>r@   c                 T   U R                  5       nUS;   a  U R                  U5      $ US:X  a!  U R                  S5        U R                  5       $ US:X  a!  U R                  S5        U R	                  5       $ U R                  S5      n/ SQnSU< SUS   < 3nU R                  X55        g )	N)r   r   r-   r2   r1   r   )r   r   r2   r1   Expecting: , got: r\   )ra   r`   r   r   r   rb   r   )r;   rY   r<   re   allowedmsgs         r=   r   Parser._parse_dot_rhs  s     '')	LL##M22*$KK
#0022("KK!0022%%a(A-G -4QvY?  --a5r@   c                 |    US   S:X  a!  [         R                  " US   US   US   5      eU R                  US5        g )Nr\   r   rZ   r[   invalid token)r   rS   r   rv   s     r=   rj   Parser._error_nud_token  sD    =E!66ggf? ?))%Ar@   c                 (    U R                  US5        g )Nr  )r   rv   s     r=   rl   Parser._error_led_token  s    ))%Ar@   Nc                     U R                  5       U:X  a  U R                  5         g U R                  XR                  S5      5        g r6   )ra   rh   _raise_parse_error_maybe_eofrb   )r;   
token_types     r=   r   Parser._match  s8     J.MMO--11!46r@   c                     U R                  5       U;  a   U R                  XR                  S5      5        U R                  5         g r6   )ra   r  rb   rh   )r;   r  s     r=   r  Parser._match_multiple_tokens  s8     3--22157r@   c                 .    U =R                   S-  sl         g )Nr   )r:   r;   s    r=   rh   Parser._advance  s    qr@   c                 :    U R                   U R                     S   $ Nr\   r8   r:   r!  s    r=   ra   Parser._current_token  s    ||DKK(00r@   c                 @    U R                   U R                  U-      S   $ r$  r%  r;   r   s     r=   r   Parser._lookahead  s    ||DKK&01&99r@   c                 :    U R                   U R                  U-      $ rB   r%  r(  s     r=   rb   Parser._lookahead_token  s    ||DKK&011r@   c                 N    US   nUS   nUS   n[         R                  " X4XR5      e)NrZ   r[   r\   )r   rU   )r;   rw   reasonlex_positionactual_valueactual_types         r=   r   #Parser._raise_parse_error_for_token  s5    W~W~Fm##L$/9 	9r@   c                     US   nUS   nUS   nUS:X  a  [         R                  " X4U5      eSU< SU< 3n[         R                  " X4XV5      e)NrZ   r[   r\   r   r  r  )r   rS   rU   )r;   expected_typerw   r.  r/  r0  messages          r=   r  #Parser._raise_parse_error_maybe_eof  si    W~W~Fm%66K9 9   /<.9;##> 	>r@   c                 8    U R                   R                  5         g)z'Clear the expression compilation cache.N)rC   clear)clss    r=   purgeParser.purge  s     	

r@   )r9   r:   r8   r7   )r   )r   rB   )<__name__
__module____qualname____firstlineno__rk   r  rC   rG   r>   rN   rE   rQ   r`   rx   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rj   rl   r   r  rh   ra   r   rb   r   r  classmethodr9  __static_attributes__ r@   r=   r	   r	   #   s   qq 	Q 	1	
 	A 	! 	 	! 	! 	1 	! 	 	 	a 	q  	a!" 	a#$ =MD  FI.	0&+)	16/
+(3"!0"&5%./(=222323+/&7
22("68BB61:29
>  r@   r	   c                   0    \ rS rSrS rSS jrS rS rSrg)	rc   i  c                     Xl         X l        g rB   rL   rd   )r;   rL   rd   s      r=   r>   ParsedResult.__init__  s    $r@   Nc                 j    [         R                  " U5      nUR                  U R                  U5      nU$ rB   )r   TreeInterpretervisitrd   )r;   r[   optionsinterpreterresults        r=   searchParsedResult.search  s-    --g6""4;;6r@   c                 f    [         R                  " 5       nUR                  U R                  5      nU$ )a6  Render the parsed AST as a dot file.

Note that this is marked as an internal method because
the AST is an implementation detail and is subject
to change.  This method can be used to help troubleshoot
or for development purposes, but is not considered part
of the public supported API.  Use at your own risk.

)r   GraphvizVisitorrH  rd   )r;   renderercontentss      r=   _render_dot_fileParsedResult._render_dot_file  s)     **,>>$++.r@   c                 ,    [        U R                  5      $ rB   )reprrd   r!  s    r=   __repr__ParsedResult.__repr__  s    DKK  r@   rD  rB   )	r;  r<  r=  r>  r>   rL  rR  rV  r@  rA  r@   r=   rc   rc     s    
!r@   rc   N)__doc__jmespathr   jmespath.compatr   r   r   r   objectr	   rc   rA  r@   r=   <module>r\     sE   6  ,   VV Vr !6 ! !r@   