
    \jsL                     z    d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ  G d de      Z	e G d	 d
e             Z
y)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                      e Zd Zi ddddddddddddddd	dd
ddddddddddddddddddddddddddddddZd Zi Zd!ZdUd"Zd# Zd$ Z	d% Z
dVd&Zd' Zd( Zd) Zd* Zd+ Zd, Zd- Zd. Zd/ Zd0 Zd1 Zd2 Zd3 Zd4 Zd5 Zd6 Zd7 Zd8 Zd9 Zd: Zd; Z d< Z!d= Z"d> Z#d? Z$d@ Z%dA Z&dB Z'dC Z(dD Z)dE Z*dF Z+dG Z,dH Z-dI Z.dJ Z/dWdLZ0dM Z1dN Z2dO Z3dP Z4dQ Z5dR Z6dS Z7e8dT        Z9yK)X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                 D    d | _         d g|z  | _        || _        d| _        y Nr   )	tokenizer_tokens_buffer_size_index)self	lookaheads     9/root/env/lib/python3.12/site-packages/jmespath/parser.py__init__zParser.__init__L   s&    v	)%    c                 V   	 | j                   |   S # t        $ r Y nw xY w| j                  |      }t        | j                         | j                  k\  rG	 | j                   t        t        | j                               = n# t        t        t        f$ r |cY S w xY w|| j                   |<   |S N)	_CACHEKeyError	_do_parselen	_MAX_SIZEnextiterStopIterationRuntimeError)r:   
expressionparsed_results      r<   parsezParser.parseR   s    	;;z** 		z2t{{t~~-%KKT$++%6 78m\: 
% %$
% #0Js    	)A> >BBc                     	 | j                  |      S # t        j                  $ r}||_         d }~wt        j                  $ r}|j                  |        d }~wt        j                  $ r}||_         d }~ww xY wr@   )_parser   
LexerErrorrJ   IncompleteExpressionErrorset_expression
ParseError)r:   rJ   es      r<   rC   zParser._do_parsei   so    
	;;z**$$ 	%AL33 	Z($$ 	%AL	s*    A9.A9AA9,A44A9c                 f   t        j                         j                  |      | _        t	        | j                        | _        d| _        | j                  d      }| j                         dk(  s8| j                  d      }t        j                  |d   |d   |d   d|d   z        t        ||      S )Nr   )binding_powerr
   startvaluetypezUnexpected token: %s)r   Lexertokenizer6   listr7   r9   _expression_current_token_lookahead_tokenr   rR   ParsedResult)r:   rJ   parsedts       r<   rN   zParser._parsev   s    //
;DNN+!!!2""$-%%a(A'''
AgJ&	(>7(KM MJ//r>   c                    | j                  d      }| j                          t        | d|d   z  | j                        } ||      }| j	                         }|| j
                  |   k  rpt        | d|z  d       }|#| j                  d      }| j                  |       n(| j                           ||      }| j	                         }|| j
                  |   k  rp|S )Nr   z_token_nud_%srX   z_token_led_%s)r^   _advancegetattr_error_nud_tokenr]   BINDING_POWER_error_led_token)r:   rU   
left_tokennud_functionleftcurrent_tokenlederror_tokens           r<   r\   zParser._expression   s    **1-
/Jv$66!!# J'++-d00??$- ?FC{"33A6%%k24y $ 3 3 5 d00?? r>   c                 2    t        j                  |d         S NrW   )r   r   r:   tokens     r<   _token_nud_literalzParser._token_nud_literal   s    {{5>**r>   c                 2    t        j                  |d         S ro   )r   fieldrp   s     r<   _token_nud_unquoted_identifierz%Parser._token_nud_unquoted_identifier   s    yyw((r>   c                     t        j                  |d         }| j                         dk(  r/| j                  d      }t	        j
                  d|d   |d   d      |S )NrW   r2   r   rX   z1Quoted identifier not allowed for function names.)r   rt   r]   r^   r   rR   )r:   rq   rt   ra   s       r<   _token_nud_quoted_identifierz#Parser._token_nud_quoted_identifier   se    		%.)  H,%%a(A''1W:qyCE E r>   c                     t        j                         }| j                         dk(  rt        j                         }n| j                  | j                  d         }t        j
                  ||      S )Nr   r,   )r   identityr]   _parse_projection_rhsrf   value_projectionr:   rq   rj   rights       r<   _token_nud_starzParser._token_nud_star   sW    ||~ J.LLNE..t/A/A&/IJE##D%00r>   c                 H    | j                  t        j                               S r@   )_token_led_filterr   ry   rp   s     r<   _token_nud_filterzParser._token_nud_filter   s    %%clln55r>   c                 "    | j                         S r@   )_parse_multi_select_hashrp   s     r<   _token_nud_lbracezParser._token_nud_lbrace   s    ,,..r>   c                 H    | j                         }| j                  d       |S )Nr   )r\   _matchr:   rq   rJ   s      r<   _token_nud_lparenzParser._token_nud_lparen   s"    %%'
Hr>   c                     t        j                  t        j                               }| j                  | j                  d         }t        j
                  ||      S Nr+   )r   r+   ry   rz   rf   
projectionr|   s       r<   _token_nud_flattenzParser._token_nud_flatten   sE    {{3<<>***y)+~~dE**r>   c                 h    | j                  | j                  d         }t        j                  |      S )Nr/   )r\   rf   r   not_expression)r:   rq   exprs      r<   _token_nud_notzParser._token_nud_not   s.     2 25 9:!!$''r>   c                    | j                         dv r4| j                         }| j                  t        j                         |      S | j                         dk(  rz| j                  d      dk(  rf| j                          | j                          | j                  | j                  d         }t        j                  t        j                         |      S | j                         S )Nr   r   r,   r   r   )r]   _parse_index_expression_project_if_slicer   ry   
_lookaheadrc   rz   rf   r   _parse_multi_select_list)r:   rq   r}   s      r<   _token_nud_lbracketz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                    | j                  d      dk(  s| j                  d      dk(  r| j                         S t        j                  | j	                  d      d         }| j                          | j                  d       |S )Nr   r   r   rW   r   )r   _parse_slice_expressionr   indexr^   rc   r   )r:   nodes     r<   r   zParser._parse_index_expression   so    
 OOA')"g-//11 99T2215g>?DMMOKK
#Kr>   c                    g d}d}| j                         }|dk(  s|dk  r|dk(  r<|dz  }|dk(  r!| j                  | j                  d      d       | j                          nN|dk(  r(| j                  d      d	   ||<   | j                          n!| j                  | j                  d      d       | j                         }|dk(  s|dk  r| j	                  d       t        j                  | S )
N)NNNr   r   r   r   r   syntax errorr   rW   )r]   _raise_parse_error_for_tokenr^   rc   r   r   slice)r:   partsr   rk   s       r<   r   zParser._parse_slice_expression   s     #++-:-%!)'
A:55--a0.B(*#44Q7@e11))!,n> //1M  :-%!) 	Jyy%  r>   c                 *    t        j                         S r@   )r   current_noderp   s     r<   _token_nud_currentzParser._token_nud_current   s    !!r>   c                 h    | j                  | j                  d         }t        j                  |      S )Nr   )r\   rf   r   r   r   s      r<   _token_nud_exprefzParser._token_nud_expref   s,    %%d&8&8&BC
zz*%%r>   c                 V   | j                         dk(  sS| j                  | j                  d         }|d   dk(  r|d   j                  |       |S t	        j
                  ||g      S | j                          | j                  | j                  d         }t	        j                  ||      S )Nr,   r.   rX   subexpressionchildren)	r]   _parse_dot_rhsrf   appendr   r   rc   rz   r{   r:   rj   r}   s      r<   _token_led_dotzParser._token_led_dot  s    ""$.''(:(:5(ABEF|.Z ''.(($77 MMO..""5)+E''e44r>   c                 j    | j                  | j                  d         }t        j                  ||      S )Nr   )r\   rf   r   r   r   s      r<   _token_led_pipezParser._token_led_pipe  s.      !3!3F!;<xxe$$r>   c                 j    | j                  | j                  d         }t        j                  ||      S )Nr   )r\   rf   r   or_expressionr   s      r<   _token_led_orzParser._token_led_or  s0      !3!3D!9:  u--r>   c                 j    | j                  | j                  d         }t        j                  ||      S )Nr   )r\   rf   r   and_expressionr   s      r<   _token_led_andzParser._token_led_and  s0      !3!3E!:;!!$..r>   c                    |d   dk7  r8| j                  d      }t        j                  |d   |d   |d   d|d   z        |d   }g }| j                         dk(  sY| j	                         }| j                         dk(  r| j                  d       |j                  |       | j                         dk(  sY| j                  d       t        j                  ||      }|S )	NrX   rt   rV   rW   zInvalid function name '%s'r   r   )	r^   r   rR   r]   r\   r   r   r   function_expression)r:   rj   prev_tnameargsrJ   function_nodes          r<   _token_led_lparenzParser._token_led_lparen  s    <7" **2.F''w&.,vg>@ @ G}%%'83))+J""$/G$KK
#	 %%'83
 	H//d;r>   c                     | j                  d      }| j                  d       | j                         dk(  rt        j                         }n| j                  | j                  d         }t        j                  |||      S )Nr   r   r+   r-   )r\   r   r]   r   ry   rz   rf   filter_projection)r:   rj   	conditionr}   s       r<   r   zParser._token_led_filter0  sj    $$Q'	J I-LLNE..t/A/A(/KLE$$T5)<<r>   c                 &    | j                  |d      S )Nr   _parse_comparatorr:   rj   s     r<   _token_led_eqzParser._token_led_eq:      %%dD11r>   c                 &    | j                  |d      S )Nr*   r   r   s     r<   _token_led_nezParser._token_led_ne=  r   r>   c                 &    | j                  |d      S )Nr   r   r   s     r<   _token_led_gtzParser._token_led_gt@  r   r>   c                 &    | j                  |d      S )Nr(   r   r   s     r<   _token_led_gtezParser._token_led_gteC      %%dE22r>   c                 &    | j                  |d      S )Nr'   r   r   s     r<   _token_led_ltzParser._token_led_ltF  r   r>   c                 &    | j                  |d      S )Nr)   r   r   s     r<   _token_led_ltezParser._token_led_lteI  r   r>   c                     t        j                  |      }| j                  | j                  d         }t        j                  ||      S r   )r   r+   rz   rf   r   r   s      r<   _token_led_flattenzParser._token_led_flattenL  s?    {{4 **y)+~~dE**r>   c                 ^   | j                  d      }|d   dv r@| j                         }|d   dk(  r|d   j                  |       |S | j                  ||      S | j	                  d       | j	                  d       | j                  | j                  d         }t        j                  ||      S )Nr   rX   r   index_expressionr   r,   r   )	r^   r   r   r   r   rz   rf   r   r   )r:   rj   rq   r}   s       r<   _token_led_lbracketzParser._token_led_lbracketR  s    %%a(=//002EF|11 Z ''.--dE:: KKKK
#..t/A/A&/IJE>>$..r>   c                     t        j                  ||g      }|d   dk(  r2t        j                  || j                  | j                  d               S |S )NrX   r   r,   )r   r   r   rz   rf   )r:   rj   r}   
index_exprs       r<   r   zParser._project_if_slicee  sY    ))4-8
=G#>>**4+=+=f+EFH H r>   c                 l    | j                  | j                  |         }t        j                  |||      S r@   )r\   rf   r   
comparator)r:   rj   r   r}   s       r<   r   zParser._parse_comparatorn  s0      !3!3J!?@~~j$66r>   c                     g }	 | j                         }|j                  |       | j                         dk(  rn| j                  d       G| j                  d       t	        j
                  |      S )Nr   r   )r\   r   r]   r   r   multi_select_list)r:   expressionsrJ   s      r<   r   zParser._parse_multi_select_listr  sj    ))+Jz*""$
2G$  	J$$[11r>   c                    g }	 | j                  d      }| j                  ddg       |d   }| j                  d       | j                  d      }t	        j
                  ||      }|j                  |       | j                         dk(  r| j                  d       n%| j                         d	k(  r| j                  d	       nt	        j                  |
      S )Nr   r   r   )token_typesrW   r   )key_namer   r   r   )nodes)	r^   _match_multiple_tokensr   r\   r   key_val_pairr   r]   multi_select_dict)r:   pairs	key_tokenr   rW   r   s         r<   r   zParser._parse_multi_select_hash~  s    --a0I ''02GH ( J )HKK $$Q'E##XEBDLL""$/G$$$&(2H%   $$511r>   c                    | j                   | j                            | j                  k  rt        j                         }|S | j                         dk(  r| j                  |      }|S | j                         dk(  r| j                  |      }|S | j                         dk(  r$| j                  d       | j                  |      }|S | j                  | j                  d      d       S )Nr1   r-   r.   r   r   )
rf   r]   _PROJECTION_STOPr   ry   r\   r   r   r   r^   )r:   rU   r}   s      r<   rz   z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                 J   | j                         }|dv r| j                  |      S |dk(  r!| j                  d       | j                         S |dk(  r!| j                  d       | j	                         S | j                  d      }g d}d|d|d   }| j                  ||       y )	N)r   r   r,   r1   r0   r   )r   r   r1   r0   Expecting: , got: rX   )r]   r\   r   r   r   r^   r   )r:   rU   r;   ra   allowedmsgs         r<   r   z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                 x    |d   dk(  r t        j                  |d   |d   |d         | j                  |d       y )NrX   r
   rV   rW   invalid token)r   rP   r   rp   s     r<   re   zParser._error_nud_token  sD    =E!66ggf? ?))%Ar>   c                 (    | j                  |d       y )Nr   )r   rp   s     r<   rg   zParser._error_led_token  s    ))%Ar>   Nc                     | j                         |k(  r| j                          y | j                  || j                  d             y r5   )r]   rc   _raise_parse_error_maybe_eofr^   )r:   
token_types     r<   r   zParser._match  s:     J.MMO--D11!46r>   c                     | j                         |vr!| j                  || j                  d             | j                          y r5   )r]   r   r^   rc   )r:   r   s     r<   r   zParser._match_multiple_tokens  s:     3--T22157r>   c                 .    | xj                   dz  c_         y )Nr   )r9   r:   s    r<   rc   zParser._advance  s    qr>   c                 :    | j                   | j                     d   S NrX   r7   r9   r   s    r<   r]   zParser._current_token  s    ||DKK(00r>   c                 @    | j                   | j                  |z      d   S r   r   r:   r   s     r<   r   zParser._lookahead  s    ||DKK&01&99r>   c                 :    | j                   | j                  |z      S r@   r   r   s     r<   r^   zParser._lookahead_token  s    ||DKK&011r>   c                 P    |d   }|d   }|d   }t        j                  ||||      )NrV   rW   rX   )r   rR   )r:   rq   reasonlex_positionactual_valueactual_types         r<   r   z#Parser._raise_parse_error_for_token  s9    W~W~Fm##L,$/9 	9r>   c                     |d   }|d   }|d   }|dk(  rt        j                  |||      d|d|}t        j                  ||||      )NrV   rW   rX   r
   r   r   )r   rP   rR   )r:   expected_typerq   r   r   r   messages          r<   r   z#Parser._raise_parse_error_maybe_eof  so    W~W~Fm%66lK9 9   /<.9;##,W> 	>r>   c                 8    | j                   j                          y)z'Clear the expression compilation cache.N)rA   clear)clss    r<   purgezParser.purge  s     	

r>   )r   )r   r@   ):__name__
__module____qualname__rf   r   rA   rE   r=   rL   rC   rN   r\   rr   ru   rw   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   rz   r   re   rg   r   r   rc   r]   r   r^   r   r   classmethodr   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                   &    e Zd Zd ZddZd Zd Zy)r_   c                      || _         || _        y r@   )rJ   r`   )r:   rJ   r`   s      r<   r=   zParsedResult.__init__  s    $r>   Nc                 h    t        j                  |      }|j                  | j                  |      }|S r@   )r   TreeInterpretervisitr`   )r:   rW   optionsinterpreterresults        r<   searchzParsedResult.search  s-    --g6""4;;6r>   c                 d    t        j                         }|j                  | j                        }|S )af  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   GraphvizVisitorr  r`   )r:   renderercontentss      r<   _render_dot_filezParsedResult._render_dot_file  s)     **,>>$++.r>   c                 ,    t        | j                        S r@   )reprr`   r   s    r<   __repr__zParsedResult.__repr__  s    DKK  r>   r@   )r  r	  r
  r=   r  r  r  r  r>   r<   r_   r_     s    
!r>   r_   N)__doc__jmespathr   jmespath.compatr   r   r   r   objectr	   r_   r  r>   r<   <module>r"     sE   6  ,   VV Vr !6 ! !r>   