Current File : //lib/python2.7/site-packages/jmespath/parser.pyo
�
Q�Yc@s�dZddlZddlmZddlmZddlmZddlmZddlmZde	fd	��YZ
ed
e	fd��Y�ZdS(s�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.

i����N(tlexer(twith_repr_method(tast(t
exceptions(tvisitortParsercBs�eZidd6dd6dd6dd6dd6dd6dd6dd6dd	6dd
6dd6dd6d
d6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6d d!6d"d#6d$d%6d&d'6d(d)6Zd*ZiZd+Zdd,�Zd-�Zd.�Zd/�Z	dd0�Z
d1�Zd2�Zd3�Z
d4�Zd5�Zd6�Zd7�Zd8�Zd9�Zd:�Zd;�Zd<�Zd=�Zd>�Zd?�Zd@�ZdA�ZdB�ZdC�ZdD�ZdE�ZdF�Z dG�Z!dH�Z"dI�Z#dJ�Z$dK�Z%dL�Z&dM�Z'dN�Z(dO�Z)dP�Z*dQ�Z+dR�Z,dS�Z-dT�Z.d_dU�Z0dV�Z1dW�Z2dX�Z3dY�Z4dZ�Z5d[�Z6d\�Z7d]�Z8e9d^��Z:RS(`iteoftunquoted_identifiertquoted_identifiertliteraltrbrackettrparentcommatrbracetnumbertcurrenttexpreftcolonitpipeitoritanditeqtgttlttgtetltetnei	tflattenitstaritfilteri(tdoti-tnoti2tlbracei7tlbracketi<tlpareni
i�cCs/d|_dg||_||_d|_dS(Ni(tNonet	tokenizert_tokenst_buffer_sizet_index(tselft	lookahead((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt__init__Ns		cCsg|jj|�}|dk	r"|S|j|�}||j|<t|j�|jkrc|j�n|S(N(t_CACHEtgetR#t	_do_parsetlent	_MAX_SIZEt_free_cache_entries(R(t
expressiontcachedt
parsed_result((s3/usr/lib/python2.7/site-packages/jmespath/parser.pytparseTs

cCs�y|j|�SWnhtjk
r5}||_�nGtjk
rZ}|j|��n"tjk
r{}||_�nXdS(N(t_parseRt
LexerErrorR1tIncompleteExpressionErrortset_expressiont
ParseError(R(R1te((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyR-^s	
	cCs�tj�j|�|_t|j�|_d|_|jdd�}|j�dks�|j	d�}t
j|d|d|dd|d��nt||�S(Nit
binding_powerRtstarttvaluettypesUnexpected token: %s(
RtLexerttokenizeR$tlistR%R't_expressiont_current_tokent_lookahead_tokenRR9tParsedResult(R(R1tparsedtt((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyR5ks	cCs�|jd�}|j�t|d|d|j�}||�}|j�}xz||j|kr�t|d|d�}|dkr�|jd�}|j|�qQ|j�||�}|j�}qQW|S(Nis
_token_nud_%sR>s
_token_led_%s(RDt_advancetgetattrt_error_nud_tokenRCt
BINDING_POWERR#t_error_led_token(R(R;t
left_tokentnud_functiontleftt
current_tokentledterror_token((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRBvs 

cCstj|d�S(NR=(RR	(R(ttoken((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_nud_literal�scCstj|d�S(NR=(Rtfield(R(RS((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_nud_unquoted_identifier�scCs[tj|d�}|j�dkrW|jd�}tjd|d|dd��n|S(NR=R"iR>s1Quoted identifier not allowed for function names.(RRURCRDRR9(R(RSRURG((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_nud_quoted_identifier�scCsStj�}|j�dkr-tj�}n|j|jd�}tj||�S(NR
R(RtidentityRCt_parse_projection_rhsRKtvalue_projection(R(RSROtright((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_nud_star�s
cCs|jtj��S(N(t_token_led_filterRRX(R(RS((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_nud_filter�scCs
|j�S(N(t_parse_multi_select_hash(R(RS((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_nud_lbrace�scCs|j�}|jd�|S(NR(RBt_match(R(RSR1((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_nud_lparen�s
cCs;tjtj��}|j|jd�}tj||�S(NR(RRRXRYRKt
projection(R(RSROR[((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_nud_flatten�scCs#|j|jd�}tj|�S(NR(RBRKRtnot_expression(R(RStexpr((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_nud_not�scCs�|j�dkr4|j�}|jtj�|�S|j�dkr�|jd�dkr�|j�|j�|j|jd�}tj	tj�|�S|j
�SdS(NRRRiR
(snumberR(RCt_parse_index_expressiont_project_if_sliceRRXt
_lookaheadRHRYRKRct_parse_multi_select_list(R(RSR[((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_nud_lbracket�s

cCso|jd�dks*|jd�dkr4|j�Stj|jd�d�}|j�|jd�|SdS(NiRiR=R
(Rjt_parse_slice_expressionRtindexRDRHRa(R(tnode((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRh�s


cCs�dddg}d}|j�}x�|dkr�|dkr�|dkr�|d7}|dkr{|j|jd�d�n|j�nI|dkr�|jd�d||<|j�n|j|jd�d�|j�}q$W|jd�tj|�S(	NiR
iRissyntax errorRR=(R#RCt_raise_parse_error_for_tokenRDRHRaRtslice(R(tpartsRnRP((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRm�s$



cCs
tj�S(N(Rtcurrent_node(R(RS((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_nud_current�scCs#|j|jd�}tj|�S(NR(RBRKRR(R(RSR1((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_nud_expref�scCs�|j�dksc|j|jd�}|ddkrM|dj|�|Stj||g�Sn0|j�|j|jd�}tj||�SdS(NRRR>t
subexpressiontchildren(	RCt_parse_dot_rhsRKtappendRRvRHRYRZ(R(ROR[((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_led_dot�s
cCs&|j|jd�}tj||�S(NR(RBRKRR(R(ROR[((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_led_pipescCs&|j|jd�}tj||�S(NR(RBRKRt
or_expression(R(ROR[((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt
_token_led_or	scCs&|j|jd�}tj||�S(NR(RBRKRtand_expression(R(ROR[((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_led_and
scCs�|ddkrN|jd�}tj|d|d|dd|d��n|d}g}xQ|j�dks�|j�}|j�dkr�|jd�n|j|�qaW|jd�tj||�}|S(	NR>RUi����R<R=sInvalid function name '%s'RR(	RDRR9RCRBRaRyRtfunction_expression(R(ROtprev_ttnametargsR1t
function_node((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_led_lparens

cCsf|jd�}|jd�|j�dkr=tj�}n|j|jd�}tj|||�S(NiR
RR(RBRaRCRRXRYRKtfilter_projection(R(ROt	conditionR[((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyR]%s
cCs|j|d�S(NR(t_parse_comparator(R(RO((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt
_token_led_eq/scCs|j|d�S(NR(R�(R(RO((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt
_token_led_ne2scCs|j|d�S(NR(R�(R(RO((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt
_token_led_gt5scCs|j|d�S(NR(R�(R(RO((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_led_gte8scCs|j|d�S(NR(R�(R(RO((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt
_token_led_lt;scCs|j|d�S(NR(R�(R(RO((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_led_lte>scCs5tj|�}|j|jd�}tj||�S(NR(RRRYRKRc(R(ROR[((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_led_flattenAscCs�|jd�}|dd	krc|j�}|ddkrP|dj|�|S|j||�Sn@|jd�|jd�|j|jd�}tj||�SdS(
NiR>RRtindex_expressionRwRR
(snumberscolon(	RDRhRyRiRaRYRKRRc(R(RORSR[((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_token_led_lbracketGs

cCsMtj||g�}|ddkrEtj||j|jd��S|SdS(NR>RqR(RR�RcRYRK(R(ROR[t
index_expr((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRiZscCs)|j|j|�}tj|||�S(N(RBRKRt
comparator(R(ROR�R[((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyR�cscCsig}xFtrN|j�}|j|�|j�dkr>Pq	|jd�q	W|jd�tj|�S(NR
R(tTrueRBRyRCRaRtmulti_select_list(R(texpressionsR1((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRkgs	

cCs�g}x�tr�|jd�}|jdddg�|d}|jd�|jd�}tjd|d|�}|j|�|j�d	kr�|jd	�q	|j�d
kr	|jd
�Pq	q	Wtj	d|�S(Nittoken_typesRRR=Rtkey_nameRoRR
tnodes(
R�RDt_match_multiple_tokensRaRBRtkey_val_pairRyRCtmulti_select_dict(R(tpairst	key_tokenR�R=Ro((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyR_ss 		




cCs�|j|j�|jkr+tj�}n�|j�dkrO|j|�}nn|j�dkrs|j|�}nJ|j�dkr�|jd�|j|�}n|j|j	d�d�|S(NR!RRissyntax error(
RKRCt_PROJECTION_STOPRRXRBRaRxRpRD(R(R;R[((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRY�s
cCs�|j�}|d	kr%|j|�S|dkrH|jd�|j�S|dkrk|jd�|j�S|jd�}ddddg}d||df}|j||�dS(
NRRRR!R isExpecting: %s, got: %sR>(squoted_identifiersunquoted_identifiersstar(RCRBRaRkR_RDRp(R(R;R)RGtallowedtmsg((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRx�s	




cCsH|ddkr4tj|d|d|d��n|j|d�dS(NR>RR<R=s
invalid token(RR7Rp(R(RS((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRJ�scCs|j|d�dS(Ns
invalid token(Rp(R(RS((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRL�scCs<|j�|kr|j�n|j||jd��dS(Ni(RCRHt_raise_parse_error_maybe_eofRD(R(t
token_type((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRa�s
cCs<|j�|kr.|j||jd��n|j�dS(Ni(RCR�RDRH(R(R�((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyR��scCs|jd7_dS(Ni(R'(R(((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRH�scCs|j|jdS(NR>(R%R'(R(((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRC�scCs|j|j|dS(NR>(R%R'(R(R((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRj�scCs|j|j|S(N(R%R'(R(R((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRD�scCs:|d}|d}|d}tj||||��dS(NR<R=R>(RR9(R(RStreasontlex_positiontactual_valuetactual_type((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRp�s



cCsn|d}|d}|d}|dkrBtj|||��nd||f}tj||||��dS(NR<R=R>RsExpecting: %s, got: %s(RR7R9(R(t
expected_typeRSR�R�R�tmessage((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyR��s



cCsAx:tj|jj�t|jd��D]}|j|=q)WdS(Ni(trandomtsampleR+tkeystintR/(R(tkey((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyR0�s/cCs|jj�dS(s'Clear the expression compilation cache.N(R+tclear(tcls((s3/usr/lib/python2.7/site-packages/jmespath/parser.pytpurge�sN(;t__name__t
__module__RKR�R+R/R*R4R-R5RBRTRVRWR\R^R`RbRdRgRlRhRmRtRuRzR{R}RR�R]R�R�R�R�R�R�R�R�RiR�RkR_RYRxRJRLR#RaR�RHRCRjRDRpR�R0tclassmethodR�(((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyR%s�
	
	
																					
																										REcBs/eZd�Zdd�Zd�Zd�ZRS(cCs||_||_dS(N(R1RF(R(R1RF((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyR*�s	cCs(tj|�}|j|j|�}|S(N(RtTreeInterpretertvisitRF(R(R=toptionstinterpretertresult((s3/usr/lib/python2.7/site-packages/jmespath/parser.pytsearch�scCs"tj�}|j|j�}|S(sfRender 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.

        (RtGraphvizVisitorR�RF(R(trenderertcontents((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt_render_dot_files
cCs
t|j�S(N(treprRF(R(((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt__repr__sN(R�R�R*R#R�R�R�(((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyRE�s		(t__doc__R�tjmespathRtjmespath.compatRRRRtobjectRRE(((s3/usr/lib/python2.7/site-packages/jmespath/parser.pyt<module>s��