+
    bJ  c                   s    R t ^ RIt^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RIHt  ! R R]	4      t
] ! R	 R
]	4      4       tR# )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                   s  a  ] tR t^%t o / R^ bR^ bR^ bR^ bR^ bR^ bR^ bR^ bR	^ bR
^ bR^ bR^ bR^bR^bR^bR^bR^bR^R^R^R^R^	R^R^R^(R^-R^2R^7R^</Ct^
t/ t^tRSR ltR t	R  t
R! tRTR" ltR# tR$ tR% tR& tR' tR( tR) tR* tR+ tR, tR- tR. tR/ tR0 tR1 tR2 tR3 tR4 tR5 tR6 t R7 t!R8 t"R9 t#R: t$R; t%R< t&R= t'R> t(R? t)R@ t*RA t+RB t,RC t-RD t.RE t/RF t0RURH lt1RI t2RJ t3RK t4RL t5RM t6RN t7RO t8RP t9]:RQ 4       t;RRt<V t=RG# )VParsereofunquoted_identifierquoted_identifierliteralrbracketrparencommarbracenumberZcurrentexprefcolonpipeorandeqgtltgtelteneflattenstarfilterdotnotlbracelbracketlparenc                sL    R V n         R .V,          V n        Wn        ^ V n        R # N)	tokenizer_tokens_buffer_size_index)self	lookahead   &&4/usr/lib/python3.14/site-packages/jmespath/parser.py__init__ZParser.__init__N   s$    v	)%    c                s    V P                   P                  V4      pVe   V# V P                  V4      pW0P                   V&   \        V P                   4      V P                  8  d   V P                  4        V# r"   )_CACHEZget	_do_parseZlen	_MAX_SIZE_free_cache_entries)r'   
expressionZcachedZparsed_result   &&  r*   parseZParser.parseT   s`    ,Mz2"/Jt{{dnn,$$&r,   c                s     V P                  V4      #   \        P                   d   pYn        h R p?i\        P                   d   pTP                  T4       h R p?i\        P                   d   pYn        h R p?ii ; ir"   )_parser   Z
LexerErrorr1   IncompleteExpressionErrorZset_expression
ParseError)r'   r1   Ze   && r*   r.   ZParser._do_parse^   sk    
	;;z**$$ 	%L33 	Z($$ 	%L	s6    A=/A=A=AA=0A=1A88A=c                s   \         P                  ! 4       P                  V4      V n        \	        V P                  4      V n        ^ V n        V P                  ^ R7      pV P                  4       R8X  gN   V P                  ^ 4      p\        P                  ! VR,          VR,          VR,          RVR,          ,          4      h\        W4      # )    )binding_powerr   startvaluetypezUnexpected token: %s)r    ZLexerZtokenizer#   listr$   r&   _expression_current_token_lookahead_tokenr   r6   ParsedResult)r'   r1   parsedtr2   r*   r4   ZParser._parsek   s    //
;DNN+!!!2""$-%%a(A'''
AgJ&	(>7(KM MJ//r,   c                s   V P                  ^ 4      pV P                  4        \        V RVR,          ,          V P                  4      pV! V4      pV P	                  4       pWP
                  V,          8  dg   \        V RV,          R4      pVf%   V P                  ^ 4      pV P                  V4       KS  V P                  4        V! V4      pV P	                  4       pK}  V# )r8   z_token_nud_%sr<   z_token_led_%sN)r@   _advanceZgetattr_error_nud_tokenr?   BINDING_POWER_error_led_token)r'   r9   Z
left_tokenZnud_functionleftcurrent_tokenZledZerror_tokens   &&      r*   r>   ZParser._expressionv   s    **1-
/Jv$66!!# J'++-00??$- ?FC{"33A6%%k24y $ 3 3 5r,   c                <    \         P                  ! VR ,          4      # r;   )r   r	   r'   tokenr)   r*   _token_nud_literalZParser._token_nud_literal   s    {{5>**r,   c                rJ   rK   )r   fieldrL   r)   r*   _token_nud_unquoted_identifierZ%Parser._token_nud_unquoted_identifier   s    yyw((r,   c                s    \         P                  ! VR ,          4      pV P                  4       R8X  d9   V P                  ^ 4      p\        P
                  ! ^ VR ,          VR,          R4      hV# )r;   r!   r<   z1Quoted identifier not allowed for function names.)r   rO   r?   r@   r   r6   )r'   rM   rO   rC   r2   r*   _token_nud_quoted_identifierZ#Parser._token_nud_quoted_identifier   se    		%.)  H,%%a(A''1W:qyCE E r,   c                s    \         P                  ! 4       pV P                  4       R 8X  d   \         P                  ! 4       pM"V P                  V P                  R,          4      p\         P
                  ! W#4      # )r
   r   )r   identityr?   _parse_projection_rhsrF   value_projectionr'   rM   rH   rightr2   r*   _token_nud_starZParser._token_nud_star   sU    ||~ J.LLNE..t/A/A&/IJE##D00r,   c                sJ    V P                  \        P                  ! 4       4      # r"   )_token_led_filterr   rR   rL   r)   r*   _token_nud_filterZParser._token_nud_filter   s    %%clln55r,   c                s"    V P                  4       # r"   )_parse_multi_select_hashrL   r)   r*   _token_nud_lbraceZParser._token_nud_lbrace   s    ,,..r,   c                sH    V P                  4       pV P                  R 4       V# )r   )r>   _matchr'   rM   r1   r7   r*   _token_nud_lparenZParser._token_nud_lparen   s"    %%'
Hr,   c                s    \         P                  ! \         P                  ! 4       4      pV P                  V P                  R ,          4      p\         P
                  ! W#4      # r   )r   r   rR   rS   rF   
projectionrU   r2   r*   _token_nud_flattenZParser._token_nud_flatten   sC    {{3<<>***y)+~~d**r,   c                r    V P                  V P                  R ,          4      p\        P                  ! V4      # )r   )r>   rF   r   Znot_expression)r'   rM   Zexprr7   r*   _token_nud_notZParser._token_nud_not   s.     2 25 9:!!$''r,   c                s   V P                  4       R9   d6   V P                  4       pV P                  \        P                  ! 4       V4      # V P                  4       R8X  d   V P                  ^4      R8X  dm   V P                  4        V P                  4        V P                  V P                  R,          4      p\        P                  ! \        P                  ! 4       V4      # V P                  4       # )r   r   r
   r   r   )r?   _parse_index_expression_project_if_slicer   rR   
_lookaheadrD   rS   rF   r`   _parse_multi_select_list)r'   rM   rV   r7   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                s   V P                  ^ 4      R8X  g   V P                  ^4      R8X  d   V P                  4       # \        P                  ! V P	                  ^ 4      R,          4      pV P                  4        V P                  R4       V# )r8   r   r;   r
   )rg   _parse_slice_expressionr   indexr@   rD   r\   )r'   node   & r*   re   ZParser._parse_index_expression   so    
 OOA')"g-//11 99T2215g>?DMMOKK
#Kr,   c                s   . ROp^ pV P                  4       pVR8X  g   V^8  d   VR8X  dC   V^,          pV^8X  d"   V P                  V P                  ^ 4      R4       V P                  4        MSVR8X  d,   V P                  ^ 4      R,          W&   V P                  4        M!V P                  V P                  ^ 4      R4       V P                  4       pK  V P	                  R4       \
        P                  ! V!  # )Nr
   r   syntax errorr   r;   )NNN)r?   _raise_parse_error_for_tokenr@   rD   r\   r   slice)r'   Zpartsrk   rI   s   &   r*   rj   ZParser._parse_slice_expression   s     #++-:-%!)'
A:55--a0.B(*#44Q7@11))!,n> //1MJyy%  r,   c                s,    \         P                  ! 4       # r"   )r   Zcurrent_noderL   r)   r*   _token_nud_currentZParser._token_nud_current   s    !!r,   c                rb   )r   )r>   rF   r   r   r]   r7   r*   _token_nud_exprefZParser._token_nud_expref   s,    %%d&8&8&BC
zz*%%r,   c                s~   V P                  4       R 8X  gb   V P                  V P                  R,          4      pVR,          R8X  d   VR,          P                  V4       V# \        P
                  ! W.4      # V P                  4        V P                  V P                  R,          4      p\        P                  ! W4      # )r   r   r<   subexpressionchildren)	r?   _parse_dot_rhsrF   appendr   rs   rD   rS   rT   r'   rH   rV   r7   r*   _token_led_dotZParser._token_led_dot   s    ""$.''(:(:5(ABEF|.Z ''.(($77 MMO..""5)+E''44r,   c                r    V P                  V P                  R ,          4      p\        P                  ! W4      # )r   )r>   rF   r   r   rw   r7   r*   _token_led_pipeZParser._token_led_pipe  s,      !3!3F!;<xx$$r,   c                ry   )r   )r>   rF   r   Zor_expressionrw   r7   r*   _token_led_orZParser._token_led_or	  s.      !3!3D!9:  --r,   c                ry   )r   )r>   rF   r   Zand_expressionrw   r7   r*   _token_led_andZParser._token_led_and  s.      !3!3E!:;!!$..r,   c                s   VR ,          R8w  dN   V P                  R4      p\        P                  ! VR,          VR,          VR ,          RVR,          ,          4      hVR,          p. pV P                  4       R8X  gJ   V P	                  4       pV P                  4       R8X  d   V P                  R4       VP                  V4       K^  V P                  R4       \        P                  ! W44      pV# )r<   rO   r:   r;   zInvalid function name '%s'r   r   i)	r@   r   r6   r?   r>   r\   rv   r   Zfunction_expression)r'   rH   Zprev_tZnameZargsr1   Zfunction_nodes   &&     r*   _token_led_lparenZParser._token_led_lparen  s    <7" **2.F''w&.,vg>@ @ G}%%'83))+J""$/G$KK
#H//;r,   c                s   V P                  ^ 4      pV P                  R4       V P                  4       R8X  d   \        P                  ! 4       pM"V P                  V P                  R,          4      p\        P                  ! WV4      # )r8   r
   r   r   )r>   r\   r?   r   rR   rS   rF   Zfilter_projection)r'   rH   Z	conditionrV   r2   r*   rX   ZParser._token_led_filter%  sh    $$Q'	J I-LLNE..t/A/A(/KLE$$T)<<r,   c                &    V P                  VR 4      # )r   _parse_comparatorr'   rH   r)   r*   _token_led_eqZParser._token_led_eq/      %%dD11r,   c                r~   )r   r   r   r)   r*   _token_led_neZParser._token_led_ne2  r   r,   c                r~   )r   r   r   r)   r*   _token_led_gtZParser._token_led_gt5  r   r,   c                r~   )r   r   r   r)   r*   _token_led_gteZParser._token_led_gte8      %%dE22r,   c                r~   )r   r   r   r)   r*   _token_led_ltZParser._token_led_lt;  r   r,   c                r~   )r   r   r   r)   r*   _token_led_lteZParser._token_led_lte>  r   r,   c                s    \         P                  ! V4      pV P                  V P                  R ,          4      p\         P                  ! W4      # r_   )r   r   rS   rF   r`   rw   r7   r*   _token_led_flattenZParser._token_led_flattenA  s=    {{4 **y)+~~d**r,   c                s   V P                  ^ 4      pVR,          R9   dJ   V P                  4       pVR,          R8X  d   VR,          P                  V4       V# V P                  W4      # V P	                  R4       V P	                  R4       V P                  V P                  R,          4      p\        P                  ! W4      # )r8   r<   index_expressionrt   r   r
   rd   )	r@   re   rv   rf   r\   rS   rF   r   r`   )r'   rH   rM   rV   r2   r*   _token_led_lbracketZParser._token_led_lbracketG  s    %%a(=//002EF|11 Z ''.--d:: KKKK
#..t/A/A&/IJE>>$..r,   c                s    \         P                  ! W.4      pVR ,          R8X  d8   \         P                  ! VV P                  V P                  R,          4      4      # V# )r<   rp   r   )r   r   r`   rS   rF   )r'   rH   rV   Z
index_expr   &&& r*   rf   ZParser._project_if_sliceZ  sW    ))4-8
=G#>>**4+=+=f+EFH H r,   c                st    V P                  V P                  V,          4      p\        P                  ! W!V4      # r"   )r>   rF   r   
comparator)r'   rH   r   rV   r   r*   r   ZParser._parse_comparatorc  s.      !3!3J!?@~~j66r,   c                s    . p V P                  4       pVP                  V4       V P                  4       R8X  d   MV P                  R4       KK  V P                  R4       \        P
                  ! V4      # )Tr
   r   )r>   rv   r?   r\   r   Zmulti_select_list)r'   Zexpressionsr1      &  r*   rh   ZParser._parse_multi_select_listg  sc    ))+Jz*""$
2G$J$$[11r,   c                s   . p V P                  ^ 4      pV P                  RR.R7       VR,          pV P                  R4       V P                  ^ 4      p\        P
                  ! W4R7      pVP                  V4       V P                  4       R8X  d   V P                  R4       K  V P                  4       R8X  g   K  V P                  R4        \        P                  ! TR	7      # )
Tr   r   )token_typesr;   r   )key_namerl   r   r   )Znodes)	r@   _match_multiple_tokensr\   r>   r   Zkey_val_pairrv   r?   Zmulti_select_dict)r'   ZpairsZ	key_tokenr   r;   rl   s   &     r*   rZ   ZParser._parse_multi_select_hashs  s    --a0I ''02GH ( J )HKK $$Q'E##XBDLL""$/G$$$&(2H%$$511r,   c                s   V P                   V P                  4       ,          V P                  8  d   \        P                  ! 4       pV# V P                  4       R 8X  d   V P                  V4      pV# V P                  4       R8X  d   V P                  V4      pV# V P                  4       R8X  d%   V P                  R4       V P                  V4      pV# V P                  V P                  ^ 4      R4       X# )r    r   r   rn   )
rF   r?   _PROJECTION_STOPr   rR   r>   r\   ru   ro   r@   )r'   r9   rV   r7   r*   rS   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                sd   V P                  4       pVR9   d   V P                  V4      # VR8X  d"   V P                  R4       V P                  4       # VR8X  d"   V P                  R4       V P	                  4       # V P                  ^ 4      p. ROpRV: RVR,          : 2pV P                  W54       R# )	r   r    r   Expecting: , got: r<   N)r   r   r   )r   r   r    r   )r?   r>   r\   rh   rZ   r@   ro   )r'   r9   r(   rC   ZallowedZmsgs   &&    r*   ru   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                s    VR ,          R8X  d.   \         P                  ! VR,          VR,          VR ,          4      hV P                  VR4       R# )r<   r   r:   r;   invalid tokenN)r   r5   ro   rL   r)   r*   rE   ZParser._error_nud_token  sD    =E!66ggf? ?))%Ar,   c                s*    V P                  VR 4       R# )r   N)ro   rL   r)   r*   rG   ZParser._error_led_token  s    ))%Ar,   Nc                s    V P                  4       V8X  d   V P                  4        R# V P                  WP                  ^ 4      4       R# r8   N)r?   rD   _raise_parse_error_maybe_eofr@   )r'   Z
token_typer)   r*   r\   ZParser._match  s8     J.MMO--11!46r,   c                s    V P                  4       V9  d!   V P                  WP                  ^ 4      4       V P                  4        R# r   )r?   r   r@   rD   )r'   r   r)   r*   r   ZParser._match_multiple_tokens  s8     3--22157r,   c                s8    V ;P                   ^,          un         R# )i   N)r&   r'      &r*   rD   ZParser._advance  s    qr,   c                sJ    V P                   V P                  ,          R ,          # r<   r$   r&   r   r   r*   r?   ZParser._current_token  s    ||DKK(00r,   c                sX    V P                   V P                  V,           ,          R ,          # r   r   r'   r   r)   r*   rg   ZParser._lookahead  s    ||DKK&01&99r,   c                sJ    V P                   V P                  V,           ,          # r"   r   r   r)   r*   r@   ZParser._lookahead_token  s    ||DKK&011r,   c                sf    VR ,          pVR,          pVR,          p\         P                  ! W4WR4      h)r:   r;   r<   )r   r6   )r'   rM   Zreasonlex_positionactual_valueactual_types   &&&   r*   ro   Z#Parser._raise_parse_error_for_token  s5    W~W~Fm##L$/9 	9r,   c                s    VR ,          pVR,          pVR,          pVR8X  d   \         P                  ! W4V4      hRV: RV: 2p\         P                  ! W4WV4      h)r:   r;   r<   r   r   r   )r   r5   r6   )r'   Zexpected_typerM   r   r   r   Zmessages   &&&    r*   r   Z#Parser._raise_parse_error_maybe_eof  si    W~W~Fm%66K9 9   /<.9;##> 	>r,   c                s    \         P                  ! \        V P                  P	                  4       4      \        V P                  ^,          4      4       F  pV P                  P                  VR4       K!  	  R# )   N)randomZsampler=   r-   ZkeysZintr/   Zpop)r'   Zkeyrm   r*   r0   ZParser._free_cache_entries  sK    ==dkk&6&6&8!93t~~PQ?Q;RSCKKOOC& Tr,   c                s:    V P                   P                  4        R# )z'Clear the expression compilation cache.N)r-   Zclear)Zclsr   r*   purgeZParser.purge  s     	

r,   )r%   r&   r$   r#   )r   )r8   r"   )>__name__
__module____qualname____firstlineno__rF   r   r-   r/   r+   r3   r.   r4   r>   rN   rP   rQ   rW   rY   r[   r^   ra   rc   ri   re   rj   rq   rr   rx   rz   r{   r|   r}   rX   r   r   r   r   r   r   r   r   rf   r   rh   rZ   rS   ru   rE   rG   r\   r   rD   r?   rg   r@   ro   r   r0   Zclassmethodr   __static_attributes____classdictcell__Z__classdict__   @r*   r   r   %   s<    qq 	Q 	1	
 	A 	! 	 	! 	! 	1 	! 	 	 	a 	q  	a!" 	a#$ 	aqqa1"rr"B"=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                   s<   a  ] tR tRt o R tRR ltR tR tRtV t	R# )	rA   i  c                s    Wn         W n        R # r"   r1   rB   )r'   r1   rB   s   &&&r*   r+   ZParsedResult.__init__  s    $r,   Nc                sj    \         P                  ! V4      pVP                  V P                  V4      pV# r"   )r   ZTreeInterpretervisitrB   )r'   r;   ZoptionsZinterpreterZresults   &&&  r*   searchZParsedResult.search  s-    --g6""4;;6r,   c                sf    \         P                  ! 4       pVP                  V P                  4      pV# )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   ZGraphvizVisitorr   rB   )r'   ZrendererZcontentsr   r*   _render_dot_fileZParsedResult._render_dot_file   s)     **,>>$++.r,   c                s,    \        V P                  4      # r"   )ZreprrB   r   r   r*   __repr__ZParsedResult.__repr__  s    DKK  r,   r   r"   )
r   r   r   r   r+   r   r   r   r   r   r   r   r*   rA   rA     s     
! !r,   rA   )Z__doc__r   Zjmespathr    Zjmespath.compatr   r   r   r   Zobjectr   rA   ) r,   r*   <module>r      sH   6   ,   MV M` !6 ! !r,   