+
    bJ                         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                     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numbercurrentexprefcolonpipeorandeqgtltgtelteneflattenstarfilterdotnotlbracelbracketlparenc                L    R V n         R .V,          V n        Wn        ^ V n        R # N)	tokenizer_tokens_buffer_size_index)self	lookaheads   &&1/usr/lib/python3/dist-packages/jmespath/parser.py__init__Parser.__init__N   s$    v	)%    c                    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'   )_CACHEget	_do_parselen	_MAX_SIZE_free_cache_entries)r,   
expressioncachedparsed_results   &&  r.   parseParser.parseT   s`    ,Mz2"/Jt{{dnn,$$&r1   c                     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   
LexerErrorr9   IncompleteExpressionErrorset_expression
ParseError)r,   r9   es   && r.   r5   Parser._do_parse^   sk    
	;;z**$$ 	%L33 	Z($$ 	%L	s6    A=/A=A=AA=0A=1A88A=c                   \         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   Lexertokenizer(   listr)   r+   _expression_current_token_lookahead_tokenr   rC   ParsedResult)r,   r9   parsedts   &&  r.   r?   Parser._parsek   s    //
;DNN+!!!2""$-%%a(A'''
AgJ&	(>7(KM MJ//r1   c                   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# )rG   z_token_nud_%srK   z_token_led_%sN)rQ   _advancegetattr_error_nud_tokenrP   BINDING_POWER_error_led_token)r,   rH   
left_tokennud_functionleftcurrent_tokenlederror_tokens   &&      r.   rO   Parser._expressionv   s    **1-
/Jv$66!!# J'++-00??$- ?FC{"33A6%%k24y $ 3 3 5r1   c                <    \         P                  ! VR ,          4      # rJ   )r   r   r,   tokens   &&r.   _token_nud_literalParser._token_nud_literal   s    {{5>**r1   c                <    \         P                  ! VR ,          4      # rd   )r   fieldre   s   &&r.   _token_nud_unquoted_identifier%Parser._token_nud_unquoted_identifier   s    yyw((r1   c                    \         P                  ! VR ,          4      pV P                  4       R8X  d9   V P                  ^ 4      p\        P
                  ! ^ VR ,          VR,          R4      hV# )rJ   r%   rK   z1Quoted identifier not allowed for function names.)r   rj   rP   rQ   r   rC   )r,   rf   rj   rT   s   &&  r.   _token_nud_quoted_identifier#Parser._token_nud_quoted_identifier   se    		%.)  H,%%a(A''1W:qyCE E r1   c                    \         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   identityrP   _parse_projection_rhsrZ   value_projectionr,   rf   r^   rights   &&  r.   _token_nud_starParser._token_nud_star   sU    ||~ J.LLNE..t/A/A&/IJE##D00r1   c                J    V P                  \        P                  ! 4       4      # r'   )_token_led_filterr   rq   re   s   &&r.   _token_nud_filterParser._token_nud_filter   s    %%clln55r1   c                "    V P                  4       # r'   )_parse_multi_select_hashre   s   &&r.   _token_nud_lbraceParser._token_nud_lbrace   s    ,,..r1   c                H    V P                  4       pV P                  R 4       V# )r   )rO   _matchr,   rf   r9   s   && r.   _token_nud_lparenParser._token_nud_lparen   s"    %%'
Hr1   c                    \         P                  ! \         P                  ! 4       4      pV P                  V P                  R ,          4      p\         P
                  ! W#4      # r   )r   r   rq   rr   rZ   
projectionrt   s   &&  r.   _token_nud_flattenParser._token_nud_flatten   sC    {{3<<>***y)+~~d**r1   c                r    V P                  V P                  R ,          4      p\        P                  ! V4      # )r"   )rO   rZ   r   not_expression)r,   rf   exprs   && r.   _token_nud_notParser._token_nud_not   s.     2 25 9:!!$''r1   c                   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   )rP   _parse_index_expression_project_if_slicer   rq   
_lookaheadrW   rr   rZ   r   _parse_multi_select_list)r,   rf   ru   s   && r.   _token_nud_lbracketParser._token_nud_lbracket   s     $77002E
 ))#,,.%@@  "f,"j0MMOMMO..t/A/A&/IJE>>#,,.%880022r1   c                   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# )rG   r   rJ   r   )r   _parse_slice_expressionr   indexrQ   rW   r   )r,   nodes   & r.   r   Parser._parse_index_expression   so    
 OOA')"g-//11 99T2215g>?DMMOKK
#Kr1   c                   . 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   rJ   )NNN)rP   _raise_parse_error_for_tokenrQ   rW   r   r   slice)r,   partsr   r_   s   &   r.   r   Parser._parse_slice_expression   s     #++-:-%!)'
A:55--a0.B(*#44Q7@11))!,n> //1MJyy%  r1   c                ,    \         P                  ! 4       # r'   )r   current_nodere   s   &&r.   _token_nud_currentParser._token_nud_current   s    !!r1   c                r    V P                  V P                  R ,          4      p\        P                  ! V4      # )r   )rO   rZ   r   r   r   s   && r.   _token_nud_exprefParser._token_nud_expref   s,    %%d&8&8&BC
zz*%%r1   c                ~   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!   rK   subexpressionchildren)	rP   _parse_dot_rhsrZ   appendr   r   rW   rr   rs   r,   r^   ru   s   && r.   _token_led_dotParser._token_led_dot   s    ""$.''(:(:5(ABEF|.Z ''.(($77 MMO..""5)+E''44r1   c                r    V P                  V P                  R ,          4      p\        P                  ! W4      # )r   )rO   rZ   r   r   r   s   && r.   _token_led_pipeParser._token_led_pipe  s,      !3!3F!;<xx$$r1   c                r    V P                  V P                  R ,          4      p\        P                  ! W4      # )r   )rO   rZ   r   or_expressionr   s   && r.   _token_led_orParser._token_led_or	  s.      !3!3D!9:  --r1   c                r    V P                  V P                  R ,          4      p\        P                  ! W4      # )r   )rO   rZ   r   and_expressionr   s   && r.   _token_led_andParser._token_led_and  s.      !3!3E!:;!!$..r1   c                   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# )rK   rj   rI   rJ   zInvalid function name '%s'r   r   )	rQ   r   rC   rP   rO   r   r   r   function_expression)r,   r^   prev_tnameargsr9   function_nodes   &&     r.   _token_led_lparenParser._token_led_lparen  s    <7" **2.F''w&.,vg>@ @ G}%%'83))+J""$/G$KK
#H//;r1   c                   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      # )rG   r   r   r    )rO   r   rP   r   rq   rr   rZ   filter_projection)r,   r^   	conditionru   s   &&  r.   ry   Parser._token_led_filter%  sh    $$Q'	J I-LLNE..t/A/A(/KLE$$T)<<r1   c                &    V P                  VR 4      # )r   _parse_comparatorr,   r^   s   &&r.   _token_led_eqParser._token_led_eq/      %%dD11r1   c                &    V P                  VR 4      # )r   r   r   s   &&r.   _token_led_neParser._token_led_ne2  r   r1   c                &    V P                  VR 4      # )r   r   r   s   &&r.   _token_led_gtParser._token_led_gt5  r   r1   c                &    V P                  VR 4      # )r   r   r   s   &&r.   _token_led_gteParser._token_led_gte8      %%dE22r1   c                &    V P                  VR 4      # )r   r   r   s   &&r.   _token_led_ltParser._token_led_lt;  r   r1   c                &    V P                  VR 4      # )r   r   r   s   &&r.   _token_led_lteParser._token_led_lte>  r   r1   c                    \         P                  ! V4      pV P                  V P                  R ,          4      p\         P                  ! W4      # r   )r   r   rr   rZ   r   r   s   && r.   _token_led_flattenParser._token_led_flattenA  s=    {{4 **y)+~~d**r1   c                   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      # )rG   rK   index_expressionr   r   r   r   )	rQ   r   r   r   r   rr   rZ   r   r   )r,   r^   rf   ru   s   &&  r.   _token_led_lbracketParser._token_led_lbracketG  s    %%a(=//002EF|11 Z ''.--d:: KKKK
#..t/A/A&/IJE>>$..r1   c                    \         P                  ! W.4      pVR ,          R8X  d8   \         P                  ! VV P                  V P                  R,          4      4      # V# )rK   r   r   )r   r   r   rr   rZ   )r,   r^   ru   
index_exprs   &&& r.   r   Parser._project_if_sliceZ  sW    ))4-8
=G#>>**4+=+=f+EFH H r1   c                t    V P                  V P                  V,          4      p\        P                  ! W!V4      # r'   )rO   rZ   r   
comparator)r,   r^   r   ru   s   &&& r.   r   Parser._parse_comparatorc  s.      !3!3J!?@~~j66r1   c                    . 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   )rO   r   rP   r   r   multi_select_list)r,   expressionsr9   s   &  r.   r   Parser._parse_multi_select_listg  sc    ))+Jz*""$
2G$J$$[11r1   c                   . 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_typesrJ   r   )key_namer   r   r   )nodes)	rQ   _match_multiple_tokensr   rO   r   key_val_pairr   rP   multi_select_dict)r,   pairs	key_tokenr   rJ   r   s   &     r.   r}   Parser._parse_multi_select_hashs  s    --a0I ''02GH ( J )HKK $$Q'E##XBDLL""$/G$$$&(2H%$$511r1   c                   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!   r   )
rZ   rP   _PROJECTION_STOPr   rq   rO   r   r   r   rQ   )r,   rH   ru   s   && r.   rr   Parser._parse_projection_rhs  s    d1134t7L7LLLLNE    "j0$$]3E    "h.$$]3E    "e+KK''6E  --d.C.CA.F.<>r1   c                d   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: rK   N)r   r
   r   )r   r
   r$   r#   )rP   rO   r   r   r}   rQ   r   )r,   rH   r-   rT   allowedmsgs   &&    r.   r   Parser._parse_dot_rhs  s     '')	LL##M22*$KK
#0022("KK!0022%%a(A-G -4QvY?  --a5r1   c                    VR ,          R8X  d.   \         P                  ! VR,          VR,          VR ,          4      hV P                  VR4       R# )rK   r	   rI   rJ   invalid tokenN)r   rA   r   re   s   &&r.   rY   Parser._error_nud_token  sD    =E!66ggf? ?))%Ar1   c                *    V P                  VR 4       R# )r  N)r   re   s   &&r.   r[   Parser._error_led_token  s    ))%Ar1   Nc                    V P                  4       V8X  d   V P                  4        R# V P                  WP                  ^ 4      4       R# rG   N)rP   rW   _raise_parse_error_maybe_eofrQ   )r,   
token_types   &&r.   r   Parser._match  s8     J.MMO--11!46r1   c                    V P                  4       V9  d!   V P                  WP                  ^ 4      4       V P                  4        R# r
  )rP   r  rQ   rW   )r,   r   s   &&r.   r   Parser._match_multiple_tokens  s8     3--22157r1   c                8    V ;P                   ^,          un         R# )   N)r+   r,   s   &r.   rW   Parser._advance  s    qr1   c                J    V P                   V P                  ,          R ,          # rK   r)   r+   r  s   &r.   rP   Parser._current_token  s    ||DKK(00r1   c                X    V P                   V P                  V,           ,          R ,          # r  r  r,   r   s   &&r.   r   Parser._lookahead  s    ||DKK&01&99r1   c                J    V P                   V P                  V,           ,          # r'   r  r  s   &&r.   rQ   Parser._lookahead_token  s    ||DKK&011r1   c                f    VR ,          pVR,          pVR,          p\         P                  ! W4WR4      h)rI   rJ   rK   )r   rC   )r,   rf   reasonlex_positionactual_valueactual_types   &&&   r.   r   #Parser._raise_parse_error_for_token  s5    W~W~Fm##L$/9 	9r1   c                    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)rI   rJ   rK   r	   r   r   )r   rA   rC   )r,   expected_typerf   r  r   r!  messages   &&&    r.   r  #Parser._raise_parse_error_maybe_eof  si    W~W~Fm%66K9 9   /<.9;##> 	>r1   c                    \         P                  ! \        V P                  P	                  4       4      \        V P                  ^,          4      4       F  pV P                  P                  VR4       K!  	  R# )   N)randomsamplerN   r3   keysintr7   pop)r,   keys   & r.   r8   Parser._free_cache_entries  sK    ==dkk&6&6&8!93t~~PQ?Q;RSCKKOOC& Tr1   c                :    V P                   P                  4        R# )z'Clear the expression compilation cache.N)r3   clear)clss   &r.   purgeParser.purge  s     	

r1   )r*   r+   r)   r(   )r(  )rG   r'   )>__name__
__module____qualname____firstlineno__rZ   r   r3   r7   r/   r<   r5   r?   rO   rg   rk   rn   rv   rz   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   ry   r   r   r   r   r   r   r   r   r   r   r   r}   rr   r   rY   r[   r   r   rW   rP   r   rQ   r   r  r8   classmethodr3  __static_attributes____classdictcell____classdict__s   @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
>'  r1   r   c                   <   a  ] tR tRt o R tRR ltR tR tRtV t	R# )	rR   i  c                    Wn         W n        R # r'   r9   rS   )r,   r9   rS   s   &&&r.   r/   ParsedResult.__init__  s    $r1   Nc                j    \         P                  ! V4      pVP                  V P                  V4      pV# r'   )r   TreeInterpretervisitrS   )r,   rJ   optionsinterpreterresults   &&&  r.   searchParsedResult.search  s-    --g6""4;;6r1   c                f    \         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   GraphvizVisitorrD  rS   )r,   renderercontentss   &  r.   _render_dot_fileParsedResult._render_dot_file   s)     **,>>$++.r1   c                ,    \        V P                  4      # r'   )reprrS   r  s   &r.   __repr__ParsedResult.__repr__  s    DKK  r1   r@  r'   )
r5  r6  r7  r8  r/   rH  rN  rR  r:  r;  r<  s   @r.   rR   rR     s     
! !r1   rR   )__doc__r)  jmespathr   jmespath.compatr   r   r   r   objectr   rR    r1   r.   <module>rY     sH   6   ,   MV M` !6 ! !r1   