
    `2fJ                         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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.

    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:yK)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                 D    d | _         d g|z  | _        || _        d| _        y Nr   )	tokenizer_tokens_buffer_size_index)self	lookaheads     O/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/jmespath/parser.py__init__zParser.__init__N   s&    v	)%    c                     | j                   j                  |      }||S | j                  |      }|| j                   |<   t        | j                         | j                  kD  r| j                          |S N)_CACHEget	_do_parselen	_MAX_SIZE_free_cache_entries)r;   
expressioncachedparsed_results       r=   parsezParser.parseT   sb    ,Mz2"/Jt{{dnn,$$&r?   c                     	 | j                  |      S # t        j                  $ r}||_         d }~wt        j                  $ r}|j                  |        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^   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tokenizer7   listr8   r:   _expression_current_token_lookahead_tokenr   rQ   ParsedResult)r;   rH   parsedts       r=   rM   zParser._parsek   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_%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0??$- ?FC{"33A6%%k24y $ 3 3 5 d00?? r?   c                 2    t        j                  |d         S NrV   )r   r   r;   tokens     r=   _token_nud_literalzParser._token_nud_literal   s    {{5>**r?   c                 2    t        j                  |d         S rn   )r   fieldro   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 )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   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_rhsre   value_projectionr;   rp   ri   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 rA   )_token_led_filterr   rx   ro   s     r=   _token_nud_filterzParser._token_nud_filter   s    %%clln55r?   c                 "    | j                         S rA   )_parse_multi_select_hashro   s     r=   _token_nud_lbracezParser._token_nud_lbrace   s    ,,..r?   c                 H    | j                         }| j                  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                  | j                  d         }t        j
                  ||      S Nr+   )r   r+   rx   ry   re   
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[   re   r   not_expression)r;   rp   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   rx   
_lookaheadrb   ry   re   r   _parse_multi_select_list)r;   rp   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   rV   r   )r   _parse_slice_expressionr   indexr]   rb   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   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.B(*#44Q7@e11))!,n> //1M  :-%!) 	Jyy%  r?   c                 *    t        j                         S rA   )r   current_nodero   s     r=   _token_nud_currentzParser._token_nud_current   s    !!r?   c                 h    | j                  | j                  d         }t        j                  |      S )Nr   )r[   re   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.   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BEF|.Z ''.(($77 MMO..""5)+E''e44r?   c                 j    | j                  | j                  d         }t        j                  ||      S )Nr   )r[   re   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[   re   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[   re   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 )	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))+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   rx   ry   re   filter_projection)r;   ri   	conditionr|   s       r=   r   zParser._token_led_filter%  sj    $$Q'	J I-LLNE..t/A/A(/KLE$$T5)<<r?   c                 &    | j                  |d      S )Nr   _parse_comparatorr;   ri   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_ne2  r   r?   c                 &    | j                  |d      S )Nr   r   r   s     r=   _token_led_gtzParser._token_led_gt5  r   r?   c                 &    | j                  |d      S )Nr(   r   r   s     r=   _token_led_gtezParser._token_led_gte8      %%dE22r?   c                 &    | j                  |d      S )Nr'   r   r   s     r=   _token_led_ltzParser._token_led_lt;  r   r?   c                 &    | j                  |d      S )Nr)   r   r   s     r=   _token_led_ltezParser._token_led_lte>  r   r?   c                     t        j                  |      }| j                  | j                  d         }t        j                  ||      S r   )r   r+   ry   re   r   r   s      r=   _token_led_flattenzParser._token_led_flattenA  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   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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 )NrW   r   r,   )r   r   r   ry   re   )r;   ri   r|   
index_exprs       r=   r   zParser._project_if_sliceZ  sY    ))4-8
=G#>>**4+=+=f+EFH H r?   c                 l    | j                  | j                  |         }t        j                  |||      S rA   )r[   re   r   
comparator)r;   ri   r   r|   s       r=   r   zParser._parse_comparatorc  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;   expressionsrH   s      r=   r   zParser._parse_multi_select_listg  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_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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   )
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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: rW   )r\   r[   r   r   r   r]   r   )r;   rT   r<   r`   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 )NrW   r
   rU   rV   invalid token)r   rO   r   ro   s     r=   rd   zParser._error_nud_token  sD    =E!66ggf? ?))%Ar?   c                 (    | j                  |d       y )Nr   )r   ro   s     r=   rf   zParser._error_led_token  s    ))%Ar?   Nc                     | j                         |k(  r| j                          y | j                  || j                  d             y r6   )r\   rb   _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 r6   )r\   r   r]   rb   )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   )r:   r;   s    r=   rb   zParser._advance  s    qr?   c                 :    | j                   | j                     d   S NrW   r8   r:   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 rA   r   r   s     r=   r]   zParser._lookahead_token  s    ||DKK&011r?   c                 P    |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  s9    W~W~Fm##L,$/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  so    W~W~Fm%66lK9 9   /<.9;##,W> 	>r?   c                     t        j                  t        | j                  j	                               t        | j                  dz              D ]  }| j                  j                  |d          y )Nr   )randomsamplerZ   rB   keysintrF   pop)r;   keys     r=   rG   zParser._free_cache_entries  sO    ==dkk&6&6&8!93t~~PQ?Q;RS 	'CKKOOC&	'r?   c                 8    | j                   j                          y)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	   %   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 rA   )rH   r_   )r;   rH   r_   s      r=   r>   zParsedResult.__init__  s    $r?   Nc                 h    t        j                  |      }|j                  | j                  |      }|S rA   )r   TreeInterpretervisitr_   )r;   rV   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 rA   )reprr_   r   s    r=   __repr__zParsedResult.__repr__  s    DKK  r?   rA   )r  r  r  r>   r  r   r#  r  r?   r=   r^   r^     s    
!r?   r^   )__doc__r  jmespathr   jmespath.compatr   r   r   r   objectr	   r^   r  r?   r=   <module>r(     sH   6   ,   MV M` !6 ! !r?   