
    /JfJ                         d Z ddl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dS )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.

    N)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dVd"Zd# Zd$ Z	d% Z
dWd&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/dXdLZ0dM Z1dN Z2dO Z3dP Z4dQ Z5dR Z6dS Z7dT Z8e9dU             Z:dKS )Y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
      c                 F    d | _         d g|z  | _        || _        d| _        d S Nr   )	tokenizer_tokens_buffer_size_index)self	lookaheads     R/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/jmespath/parser.py__init__zParser.__init__N   s*    v	)%    c                     | j                             |          }||S |                     |          }|| j         |<   t          | j                   | j        k    r|                                  |S N)_CACHEget	_do_parselen	_MAX_SIZE_free_cache_entries)r;   
expressioncachedparsed_results       r=   parsezParser.parseT   sl    ,,Mz22"/Jt{dn,,$$&&&r?   c                     	 |                      |          S # t          j        $ r}||_         d }~wt          j        $ r}|                    |            d }~wt          j        $ r}||_         d }~ww xY wrA   )_parser   
LexerErrorrH   IncompleteExpressionErrorset_expression
ParseError)r;   rH   es      r=   rD   zParser._do_parse^   s    
	;;z***$ 	 	 	%AL3 	 	 	Z((($ 	 	 	%AL	s*    A5.A5 AA5(A00A5c                    t          j                                        |          | _        t	          | j                  | _        d| _        |                     d          }|                                 dk    sG| 	                    d          }t          j        |d         |d         |d         d|d         z            t          ||          S )Nr   )binding_powerr
   startvaluetypezUnexpected token: %s)r   Lexertokenizer7   listr8   r:   _expression_current_token_lookahead_tokenr   rQ   ParsedResult)r;   rH   parsedts       r=   rM   zParser._parsek   s    //
;;DN++!!!22""$$--%%a((A''
AgJ&	(>7(KM M MJ///r?   c                     |                      d          }|                                  t          | d|d         z  | j                  } ||          }|                                 }|| j        |         k     rt          | d|z  d           }|+|                      d          }|                     |           n3|                                   ||          }|                                 }|| j        |         k     |S )Nr   z_token_nud_%srW   z_token_led_%s)r]   _advancegetattr_error_nud_tokenr\   BINDING_POWER_error_led_token)r;   rT   
left_tokennud_functionleftcurrent_tokenlederror_tokens           r=   r[   zParser._expressionv   s   **1--
/Jv$66!# # |J''++--d0???$- ?FFC{"33A66%%k2222s4yy $ 3 3 5 5 d0??? r?   c                 6    t          j        |d                   S NrV   )r   r   r;   tokens     r=   _token_nud_literalzParser._token_nud_literal   s    {5>***r?   c                 6    t          j        |d                   S rn   )r   fieldro   s     r=   _token_nud_unquoted_identifierz%Parser._token_nud_unquoted_identifier   s    yw(((r?   c                     t          j        |d                   }|                                 dk    r8|                     d          }t	          j        d|d         |d         d          |S )NrV   r2   r   rW   z1Quoted identifier not allowed for function names.)r   rs   r\   r]   r   rQ   )r;   rp   rs   r`   s       r=   _token_nud_quoted_identifierz#Parser._token_nud_quoted_identifier   sr    	%.))   H,,%%a((A'1W:qyCE E E r?   c                     t          j                    }|                                 dk    rt          j                    }n |                     | j        d                   }t          j        ||          S )Nr   r,   )r   identityr\   _parse_projection_rhsre   value_projectionr;   rp   ri   rights       r=   _token_nud_starzParser._token_nud_star   s_    |~~  J..LNNEE..t/A&/IJJE#D%000r?   c                 N    |                      t          j                              S rA   )_token_led_filterr   rx   ro   s     r=   _token_nud_filterzParser._token_nud_filter   s    %%clnn555r?   c                 *    |                                  S rA   )_parse_multi_select_hashro   s     r=   _token_nud_lbracezParser._token_nud_lbrace   s    ,,...r?   c                 X    |                                  }|                     d           |S )Nr   )r[   _matchr;   rp   rH   s      r=   _token_nud_lparenzParser._token_nud_lparen   s+    %%''
Hr?   c                     t          j        t          j                              }|                     | j        d                   }t          j        ||          S Nr+   )r   r+   rx   ry   re   
projectionr{   s       r=   _token_nud_flattenzParser._token_nud_flatten   sI    {3<>>****y)+ +~dE***r?   c                 j    |                      | j        d                   }t          j        |          S )Nr/   )r[   re   r   not_expression)r;   rp   exprs      r=   _token_nud_notzParser._token_nud_not   s.     25 9::!$'''r?   c                 
   |                                  dv r;|                                 }|                     t          j                    |          S |                                  dk    r|                     d          dk    rn|                                  |                                  |                     | j        d                   }t          j	        t          j                    |          S | 
                                S )Nr   r   r,   r   r   )r\   _parse_index_expression_project_if_slicer   rx   
_lookaheadrb   ry   re   r   _parse_multi_select_list)r;   rp   r|   s      r=   _token_nud_lbracketzParser._token_nud_lbracket   s      $7770022E
 ))#,..%@@@  ""f,,""j00MMOOOMMOOO..t/A&/IJJE>#,..%88800222r?   c                 >   |                      d          dk    s|                      d          dk    r|                                 S t          j        |                     d          d                   }|                                  |                     d           |S )Nr   r   r   rV   r   )r   _parse_slice_expressionr   indexr]   rb   r   )r;   nodes     r=   r   zParser._parse_index_expression   s    
 OOA'))""g--//111 9T22155g>??DMMOOOKK
###Kr?   c                 6   g d}d}|                                  }|dk    s|dk     r|dk    rI|dz  }|dk    r)|                     |                     d          d           |                                  nb|dk    r3|                     d          d	         ||<   |                                  n)|                     |                     d          d           |                                  }|dk    s|dk     |                     d           t          j        | S )
N)NNNr   r   r   r   r   syntax errorr   rV   )r\   _raise_parse_error_for_tokenr]   rb   r   r   slice)r;   partsr   rj   s       r=   r   zParser._parse_slice_expression   s:    #""++--:--%!))''
A::55--a00.B B B(**#44Q77@e11))!,,n> > > //11M  :--%!)) 	Jy%  r?   c                 (    t          j                    S rA   )r   current_nodero   s     r=   _token_nud_currentzParser._token_nud_current   s    !!!r?   c                 j    |                      | j        d                   }t          j        |          S )Nr   )r[   re   r   r   r   s      r=   _token_nud_exprefzParser._token_nud_expref   s-    %%d&8&BCC
z*%%%r?   c                    |                                  dk    s_|                     | j        d                   }|d         dk    r|d                             |           |S t	          j        ||g          S |                                  |                     | j        d                   }t	          j        ||          S )Nr,   r.   rW   subexpressionchildren)	r\   _parse_dot_rhsre   appendr   r   rb   ry   rz   r;   ri   r|   s      r=   _token_led_dotzParser._token_led_dot   s    ""$$..''(:5(ABBEF|..Z ''...($777 MMOOO.."5)+ +E'e444r?   c                 l    |                      | j        d                   }t          j        ||          S )Nr   )r[   re   r   r   r   s      r=   _token_led_pipezParser._token_led_pipe  s/      !3F!;<<xe$$$r?   c                 l    |                      | j        d                   }t          j        ||          S )Nr   )r[   re   r   or_expressionr   s      r=   _token_led_orzParser._token_led_or	  s0      !3D!9:: u---r?   c                 l    |                      | j        d                   }t          j        ||          S )Nr   )r[   re   r   and_expressionr   s      r=   _token_led_andzParser._token_led_and  s0      !3E!:;;!$...r?   c                     |d         dk    rG|                      d          }t          j        |d         |d         |d         d|d         z            |d         }g }|                                 dk    sn|                                 }|                                 dk    r|                     d           |                    |           |                                 dk    n|                     d           t          j        ||          }|S )	NrW   rs   rU   rV   zInvalid function name '%s'r   r   )	r]   r   rQ   r\   r[   r   r   r   function_expression)r;   ri   prev_tnameargsrH   function_nodes          r=   _token_led_lparenzParser._token_led_lparen  s   <7"" **2..F'w&.,vg>@ @ @ G}%%''833))++J""$$//G$$$KK
###	 %%''833
 	H/d;;r?   c                    |                      d          }|                     d           |                                 dk    rt          j                    }n |                     | j        d                   }t          j        |||          S )Nr   r   r+   r-   )r[   r   r\   r   rx   ry   re   filter_projection)r;   ri   	conditionr|   s       r=   r   zParser._token_led_filter%  s{    $$Q''	J  I--LNNEE..t/A(/KLLE$T5)<<<r?   c                 .    |                      |d          S )Nr   _parse_comparatorr;   ri   s     r=   _token_led_eqzParser._token_led_eq/      %%dD111r?   c                 .    |                      |d          S )Nr*   r   r   s     r=   _token_led_nezParser._token_led_ne2  r   r?   c                 .    |                      |d          S )Nr   r   r   s     r=   _token_led_gtzParser._token_led_gt5  r   r?   c                 .    |                      |d          S )Nr(   r   r   s     r=   _token_led_gtezParser._token_led_gte8      %%dE222r?   c                 .    |                      |d          S )Nr'   r   r   s     r=   _token_led_ltzParser._token_led_lt;  r   r?   c                 .    |                      |d          S )Nr)   r   r   s     r=   _token_led_ltezParser._token_led_lte>  r   r?   c                     t          j        |          }|                     | j        d                   }t          j        ||          S r   )r   r+   ry   re   r   r   s      r=   _token_led_flattenzParser._token_led_flattenA  sC    {4  **y)+ +~dE***r?   c                    |                      d          }|d         dv rS|                                 }|d         dk    r|d                             |           |S |                     ||          S |                     d           |                     d           |                     | j        d                   }t          j        ||          S )Nr   rW   r   index_expressionr   r,   r   )	r]   r   r   r   r   ry   re   r   r   )r;   ri   rp   r|   s       r=   _token_led_lbracketzParser._token_led_lbracketG  s    %%a((=///0022EF|111 Z ''...--dE::: KKKK
###..t/A&/IJJE>$...r?   c                     t          j        ||g          }|d         dk    r3t          j        ||                     | j        d                             S |S )NrW   r   r,   )r   r   r   ry   re   )r;   ri   r|   
index_exprs       r=   r   zParser._project_if_sliceZ  s`    )4-88
=G##>**4+=f+EFFH H H r?   c                 n    |                      | j        |                   }t          j        |||          S rA   )r[   re   r   
comparator)r;   ri   r   r|   s       r=   r   zParser._parse_comparatorc  s1      !3J!?@@~j$666r?   c                 
   g }	 |                                  }|                    |           |                                 dk    rn|                     d           X|                     d           t	          j        |          S )NTr   r   )r[   r   r\   r   r   multi_select_list)r;   expressionsrH   s      r=   r   zParser._parse_multi_select_listg  s    	%))++Jz***""$$
22G$$$	% 	J$[111r?   c                     g }	 |                      d          }|                     ddg           |d         }|                     d           |                     d          }t	          j        ||          }|                    |           |                                 d	k    r|                     d	           n.|                                 d
k    r|                     d
           nt	          j        |          S )NTr   r   r   )token_typesrV   r   )key_namer   r   r   )nodes)	r]   _match_multiple_tokensr   r[   r   key_val_pairr   r\   multi_select_dict)r;   pairs	key_tokenr   rV   r   s         r=   r   zParser._parse_multi_select_hashs  s   	--a00I ''02GH ( J J J )HKK   $$Q''E#XEBBBDLL""$$//G$$$$$$&&(22H%%%	  $51111r?   c                    | j         |                                          | j        k     rt          j                    }n|                                 dk    r|                     |          }n|                                 dk    r|                     |          }nl|                                 dk    r+|                     d           |                     |          }n)|                     | 	                    d          d           |S )Nr1   r-   r.   r   r   )
re   r\   _PROJECTION_STOPr   rx   r[   r   r   r   r]   )r;   rT   r|   s      r=   ry   zParser._parse_projection_rhs  s    d11334t7LLLLNNEE  ""j00$$]33EE  ""h..$$]33EE  ""e++KK''66EE--d.C.CA.F.F.<> > >r?   c                    |                                  }|dv r|                     |          S |dk    r)|                     d           |                                 S |dk    r)|                     d           |                                 S |                     d          }g d}d|d|d         }|                     ||           d S )	N)r   r   r,   r1   r0   r   )r   r   r1   r0   Expecting: , got: rW   )r\   r[   r   r   r   r]   r   )r;   rT   r<   r`   allowedmsgs         r=   r   zParser._parse_dot_rhs  s     ''))	LLL##M222*$$KK
###00222(""KK!!!00222%%a((A- - -GG -4GGQvYY?  --a55555r?   c                     |d         dk    r(t          j        |d         |d         |d                   |                     |d           d S )NrW   r
   rU   rV   invalid token)r   rO   r   ro   s     r=   rd   zParser._error_nud_token  sU    =E!!6ggf? ? ?))%AAAAAr?   c                 2    |                      |d           d S )Nr   )r   ro   s     r=   rf   zParser._error_led_token  s    ))%AAAAAr?   Nc                     |                                  |k    r|                                  d S |                     ||                     d                     d S r6   )r\   rb   _raise_parse_error_maybe_eofr]   )r;   
token_types     r=   r   zParser._match  s_      J..MMOOOOO--D11!446 6 6 6 6r?   c                     |                                  |vr)|                     ||                     d                     |                                  d S r6   )r\   r   r]   rb   )r;   r   s     r=   r   zParser._match_multiple_tokens  sU      33--T221557 7 7r?   c                 &    | xj         dz  c_         d S )Nr   )r:   r;   s    r=   rb   zParser._advance  s    qr?   c                 2    | j         | j                 d         S NrW   r8   r:   r   s    r=   r\   zParser._current_token  s    |DK(00r?   c                 8    | j         | j        |z            d         S r   r   r;   r   s     r=   r   zParser._lookahead  s    |DK&01&99r?   c                 ,    | j         | j        |z            S rA   r   r   s     r=   r]   zParser._lookahead_token  s    |DK&011r?   c                 `    |d         }|d         }|d         }t          j        ||||          )NrU   rV   rW   )r   rQ   )r;   rp   reasonlex_positionactual_valueactual_types         r=   r   z#Parser._raise_parse_error_for_token  s;    W~W~Fm#L,$/9 9 	9r?   c                     |d         }|d         }|d         }|dk    rt          j        |||          d|d|}t          j        ||||          )NrU   rV   rW   r
   r   r   )r   rO   rQ   )r;   expected_typerp   r   r   r   messages          r=   r   z#Parser._raise_parse_error_maybe_eof  s|    W~W~Fm%6lK9 9 9   /<mm.9k;#,W> > 	>r?   c                     t          j        t          | j                                                  t          | j        dz                      D ]}| j                            |d            d S )Nr   )randomsamplerZ   rB   keysintrF   pop)r;   keys     r=   rG   zParser._free_cache_entries  sf    =dk&6&6&8&8!9!93t~PQ?Q;R;RSS 	' 	'CKOOC&&&&	' 	'r?   c                 8    | j                                          dS )z'Clear the expression compilation cache.N)rB   clear)clss    r=   purgezParser.purge  s     	
r?   )r   )r   rA   );__name__
__module____qualname__re   r   rB   rF   r>   rK   rD   rM   r[   rq   rt   rv   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   ry   r   rd   rf   r   r   rb   r\   r   r]   r   r   rG   classmethodr   r?   r=   r	   r	   %   sK       qq 	Q 	1	
 	A 	! 	 	! 	! 	1 	! 	 	 	a 	q  	a!" 	a#$ =  MD  FI       	0 	0 	0   &+ + +) ) )	 	 	1 1 16 6 6/ / /  
+ + +( ( (3 3 3"  ! ! !0" " "& & &5 5 5% % %. . ./ / /  (= = =2 2 22 2 22 2 23 3 32 2 23 3 3+ + +/ / /&  7 7 7
2 
2 
22 2 2(  "6 6 68B B BB B B6 6 6 6    1 1 1: : :2 2 29 9 9
> 
> 
>' ' '   [  r?   r	   c                   (    e Zd Zd ZddZd Zd ZdS )r^   c                 "    || _         || _        d S rA   )rH   r_   )r;   rH   r_   s      r=   r>   zParsedResult.__init__  s    $r?   Nc                 d    t          j        |          }|                    | j        |          }|S rA   )r   TreeInterpretervisitr_   )r;   rV   optionsinterpreterresults        r=   searchzParsedResult.search  s.    -g66""4;66r?   c                 `    t          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 rA   )reprr_   r   s    r=   __repr__zParsedResult.__repr__  s    DK   r?   rA   )r  r  r  r>   r  r   r#  r  r?   r=   r^   r^     sU             
  ! ! ! ! !r?   r^   )__doc__r  jmespathr   jmespath.compatr   r   r   r   objectr	   r^   r  r?   r=   <module>r(     s    6        , , , , , ,                  M M M M MV M M M` ! ! ! ! !6 ! ! ! ! !r?   