o âÄdJã@spdZddlZddlmZddlmZddlmZddlmZddlmZGdd „d e ƒZ eGd d „d e ƒƒZ dS) 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@sBeZdZidd“dd“dd“dd“dd“dd“dd“d d“d d“d d“d d“d d“dd“dd“dd“dd“dd“dddddddddddddœ ¥Zd ZiZd!Zd‰d"d#„Zd$d%„Zd&d'„Z d(d)„Z dŠd*d+„Z d,d-„Z d.d/„Z d0d1„Zd2d3„Zd4d5„Zd6d7„Zd8d9„Zd:d;„Zdd?„Zd@dA„ZdBdC„ZdDdE„ZdFdG„ZdHdI„ZdJdK„ZdLdM„ZdNdO„ZdPdQ„ZdRdS„ZdTdU„Z dVdW„Z!dXdY„Z"dZd[„Z#d\d]„Z$d^d_„Z%d`da„Z&dbdc„Z'ddde„Z(dfdg„Z)dhdi„Z*djdk„Z+dldm„Z,dndo„Z-dpdq„Z.drds„Z/d‹dudv„Z0dwdx„Z1dydz„Z2d{d|„Z3d}d~„Z4dd€„Z5dd‚„Z6dƒd„„Z7d…d†„Z8e9d‡dˆ„ƒZ:dtS)ŒÚ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é é€cCs"d|_dg||_||_d|_dS©Nr)Ú tokenizerÚ_tokensZ _buffer_sizeÚ_index)ÚselfÚ lookahead©r9új/private/var/folders/v1/_jykv66s6qd26_69j1njbrl80000gr/T/pip-target-p1gutpg6/lib/python/jmespath/parser.pyÚ__init__Ns  zParser.__init__cCsH|j |¡}|dur |S| |¡}||j|<t|jƒ|jkr"| ¡|S©N)Ú_CACHEÚgetÚ _do_parseÚlenÚ _MAX_SIZEÚ_free_cache_entries)r7Ú expressionÚcachedÚ parsed_resultr9r9r:ÚparseTs   z Parser.parsec Cstz| |¡WStjy}z||_‚d}~wtjy)}z| |¡‚d}~wtjy9}z||_‚d}~wwr<)Ú_parserZ LexerErrorrCÚIncompleteExpressionErrorZset_expressionÚ ParseError)r7rCÚer9r9r:r?^s € €€þzParser._do_parsecCsrt ¡ |¡|_t|jƒ|_d|_|jdd}| ¡dks4|  d¡}t   |d|d|dd|d¡‚t ||ƒS)Nr)Ú binding_powerrÚstartÚvalueÚtypezUnexpected token: %s) rÚLexerÚtokenizer4Úlistr5r6Ú _expressionÚ_current_tokenÚ_lookahead_tokenrrIÚ ParsedResult)r7rCÚparsedÚtr9r9r:rGks     ÿ z Parser._parsecCsž| d¡}| ¡t|d|d|jƒ}||ƒ}| ¡}||j|krMt|d|dƒ}|dur:| d¡}| |¡n | ¡||ƒ}| ¡}||j|ks#|S)Nrz _token_nud_%srNz _token_led_%s)rTÚ_advanceÚgetattrÚ_error_nud_tokenrSÚ BINDING_POWERÚ_error_led_token)r7rKZ left_tokenZ nud_functionÚleftÚ current_tokenZledZ error_tokenr9r9r:rRvs$  þ  ø zParser._expressioncCót |d¡S©NrM)rr ©r7Útokenr9r9r:Ú_token_nud_literal‰ózParser._token_nud_literalcCr_r`)rÚfieldrar9r9r:Ú_token_nud_unquoted_identifierŒrdz%Parser._token_nud_unquoted_identifiercCs@t |d¡}| ¡dkr| d¡}t d|d|dd¡‚|S)NrMr0rrNz1Quoted identifier not allowed for function names.)rrerSrTrrI)r7rbrerWr9r9r:Ú_token_nud_quoted_identifiers  þz#Parser._token_nud_quoted_identifiercCs:t ¡}| ¡dkrt ¡}n| |jd¡}t ||¡S)Nr r*)rÚidentityrSÚ_parse_projection_rhsr[Úvalue_projection©r7rbr]Úrightr9r9r:Ú_token_nud_staršs    zParser._token_nud_starcCs| t ¡¡Sr<)Ú_token_led_filterrrhrar9r9r:Ú_token_nud_filter¢rdzParser._token_nud_filtercCs| ¡Sr<)Ú_parse_multi_select_hashrar9r9r:Ú_token_nud_lbrace¥ózParser._token_nud_lbracecCs| ¡}| d¡|S)Nr )rRÚ_match©r7rbrCr9r9r:Ú_token_nud_lparen¨s zParser._token_nud_lparencCs*t t ¡¡}| |jd¡}t ||¡S©Nr))rr)rhrir[Ú projectionrkr9r9r:Ú_token_nud_flatten­s ÿ zParser._token_nud_flattencCó| |jd¡}t |¡S)Nr-)rRr[rZnot_expression)r7rbÚexprr9r9r:Ú_token_nud_not³ó zParser._token_nud_notcCsv| ¡dvr| ¡}| t ¡|¡S| ¡dkr7| d¡dkr7| ¡| ¡| |jd¡}t  t ¡|¡S|  ¡S)N©rrr*rr ) rSÚ_parse_index_expressionÚ_project_if_slicerrhÚ _lookaheadrXrir[rwÚ_parse_multi_select_list)r7rbrlr9r9r:Ú_token_nud_lbracket·s   ÿzParser._token_nud_lbracketcCsN| d¡dks| d¡dkr| ¡St | d¡d¡}| ¡| d¡|S)NrrrrMr )r€Ú_parse_slice_expressionrÚindexrTrXrs)r7Únoder9r9r:r~Ès ÿ zParser._parse_index_expressioncCsºgd¢}d}| ¡}|dksS|dkrS|dkr,|d7}|dkr'| | d¡d¡| ¡n|dkr>| d¡d ||<| ¡n | | d¡d¡| ¡}|dksS|dks| d¡tj|ŽS) N)NNNrr rrrú syntax errorrrM)rSÚ_raise_parse_error_for_tokenrTrXrsrÚslice)r7Úpartsr„r^r9r9r:rƒ×s* ÿ   ÿó  zParser._parse_slice_expressioncCst ¡Sr<)rZ current_noderar9r9r:Ú_token_nud_currentïrrzParser._token_nud_currentcCry)Nr)rRr[rrrtr9r9r:Ú_token_nud_exprefòr|zParser._token_nud_exprefcCsl| ¡dks$| |jd¡}|ddkr|d |¡|St ||g¡S| ¡| |jd¡}t ||¡S)Nr*r,rNÚ subexpressionÚchildren) rSÚ_parse_dot_rhsr[ÚappendrrŒrXrirj©r7r]rlr9r9r:Ú_token_led_dotös  ÿ zParser._token_led_dotcCó| |jd¡}t ||¡S)Nr)rRr[rrrr9r9r:Ú_token_led_pipeó zParser._token_led_pipecCr’)Nr)rRr[rZ or_expressionrr9r9r:Ú _token_led_or r”zParser._token_led_orcCr’)Nr)rRr[rZand_expressionrr9r9r:Ú_token_led_and r”zParser._token_led_andcCs |ddkr| d¡}t |d|d|dd|d¡‚|d}g}| ¡dksC| ¡}| ¡dkr8| d¡| |¡| ¡dkr)| d¡t ||¡}|S) NrNreéþÿÿÿrLrMzInvalid function name '%s'r r) rTrrIrSrRrsrrZfunction_expression)r7r]Zprev_tÚnameÚargsrCZ function_noder9r9r:Ú_token_led_lparens"   þ     ü  zParser._token_led_lparencCsH| d¡}| d¡| ¡dkrt ¡}n| |jd¡}t |||¡S)Nrr r)r+)rRrsrSrrhrir[Zfilter_projection)r7r]Ú conditionrlr9r9r:rn%s    zParser._token_led_filtercCó | |d¡S)Nr©Ú_parse_comparator©r7r]r9r9r:Ú _token_led_eq/ó zParser._token_led_eqcCrœ)Nr(rrŸr9r9r:Ú _token_led_ne2r¡zParser._token_led_necCrœ)NrrrŸr9r9r:Ú _token_led_gt5r¡zParser._token_led_gtcCrœ)Nr&rrŸr9r9r:Ú_token_led_gte8r¡zParser._token_led_gtecCrœ)Nr%rrŸr9r9r:Ú _token_led_lt;r¡zParser._token_led_ltcCrœ)Nr'rrŸr9r9r:Ú_token_led_lte>r¡zParser._token_led_ltecCs&t |¡}| |jd¡}t ||¡Srv)rr)rir[rwrr9r9r:Ú_token_led_flattenAs ÿ zParser._token_led_flattencCsx| d¡}|ddvr$| ¡}|ddkr|d |¡|S| ||¡S| d¡| d¡| |jd¡}t ||¡S)NrrNr}Úindex_expressionrr*r ) rTr~rrrsrir[rrw)r7r]rbrlr9r9r:Ú_token_led_lbracketGs       zParser._token_led_lbracketcCs6t ||g¡}|ddkrt || |jd¡¡S|S)NrNrˆr*)rr¨rwrir[)r7r]rlZ index_exprr9r9r:rZs þzParser._project_if_slicecCs| |j|¡}t |||¡Sr<)rRr[rÚ comparator)r7r]rªrlr9r9r:ržcszParser._parse_comparatorcCsFg} | ¡}| |¡| ¡dkrn| d¡q| d¡t |¡S)NTr r)rRrrSrsrZmulti_select_list)r7Z expressionsrCr9r9r:rgs   ú  zParser._parse_multi_select_listcCs’g} | d¡}|jddgd|d}| d¡| d¡}tj||d}| |¡| ¡d kr6| d ¡n | ¡d krB| d ¡nqtj|d S) NTrr r )Ú token_typesrMr)Úkey_namer…rr)Únodes) rTÚ_match_multiple_tokensrsrRrZ key_val_pairrrSZmulti_select_dict)r7ÚpairsZ key_tokenr¬rMr…r9r9r:rpss$ ÿ       ñ zParser._parse_multi_select_hashcCsŽ|j| ¡|jkrt ¡}|S| ¡dkr| |¡}|S| ¡dkr*| |¡}|S| ¡dkr<| d¡| |¡}|S| |  d¡d¡|S)Nr/r+r,rr†) r[rSÚ_PROJECTION_STOPrrhrRrsrŽr‡rT)r7rKrlr9r9r:ri‡s" ö  ø  ú   þÿzParser._parse_projection_rhscCs€| ¡}|dvr | |¡S|dkr| d¡| ¡S|dkr'| d¡| ¡S| d¡}gd¢}d||df}| ||¡dS)N)r r r*r/r.r)r r r/r.úExpecting: %s, got: %srN)rSrRrsrrprTr‡)r7rKr8rWÚallowedÚmsgr9r9r:rŽ˜s     ÿzParser._parse_dot_rhscCs6|ddkrt |d|d|d¡‚| |d¡dS)NrNrrLrMú invalid token)rrHr‡rar9r9r:rZ´s ÿzParser._error_nud_tokencCs| |d¡dS)Nr´)r‡rar9r9r:r\ºózParser._error_led_tokenNcCs.| ¡|kr | ¡dS| || d¡¡dSr3)rSrXÚ_raise_parse_error_maybe_eofrT)r7Ú token_typer9r9r:rs½s   ÿz Parser._matchcCs*| ¡|vr| || d¡¡| ¡dSr3)rSr¶rTrX)r7r«r9r9r:r®Æs  ÿ zParser._match_multiple_tokenscCs|jd7_dS)Nr)r6©r7r9r9r:rXÌszParser._advancecCs|j|jdS©NrN©r5r6r¸r9r9r:rSÏrµzParser._current_tokencCs|j|j|dSr¹rº©r7rr9r9r:r€ÒszParser._lookaheadcCs|j|j|Sr<rºr»r9r9r:rTÕrµzParser._lookahead_tokencCs(|d}|d}|d}t ||||¡‚)NrLrMrN)rrI)r7rbÚreasonÚ lex_positionÚ actual_valueÚ actual_typer9r9r:r‡Øs ÿz#Parser._raise_parse_error_for_tokencCsJ|d}|d}|d}|dkrt |||¡‚d||f}t ||||¡‚)NrLrMrNrr±)rrHrI)r7Ú expected_typerbr½r¾r¿Úmessager9r9r:r¶ßsÿÿÿz#Parser._raise_parse_error_maybe_eofcCs8t t|j ¡ƒt|jdƒ¡D] }|j |d¡qdS)Nr)ÚrandomÚsamplerQr=ÚkeysÚintrAÚpop)r7Úkeyr9r9r:rBës$ÿzParser._free_cache_entriescCs|j ¡dS)z'Clear the expression compilation cache.N)r=Úclear)Úclsr9r9r:Úpurgeïsz Parser.purge)r)rr<);Ú__name__Ú __module__Ú __qualname__r[r°r=rAr;rFr?rGrRrcrfrgrmrorqrurxr{r‚r~rƒrŠr‹r‘r“r•r–ršrnr r¢r£r¤r¥r¦r§r©rržrrprirŽrZr\rsr®rXrSr€rTr‡r¶rBÚ classmethodrÊr9r9r9r:r%sÐÿþýüûúùø ÷ ö õ ô óòñðïâ"          rc@s.eZdZdd„Zd dd„Zdd„Zdd „ZdS) rUcCs||_||_dSr<)rCrV)r7rCrVr9r9r:r;÷s zParsedResult.__init__NcCst |¡}| |j|¡}|Sr<)rZTreeInterpreterÚvisitrV)r7rMÚoptionsÚ interpreterÚresultr9r9r:Úsearchûs zParsedResult.searchcCst ¡}| |j¡}|S)afRender 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. )rZGraphvizVisitorrÏrV)r7ÚrendererÚcontentsr9r9r:Ú_render_dot_files zParsedResult._render_dot_filecCs t|jƒSr<)ÚreprrVr¸r9r9r:Ú__repr__s zParsedResult.__repr__r<)rËrÌrÍr;rÓrÖrØr9r9r9r:rUõs   rU) Ú__doc__rÂZjmespathrZjmespath.compatrrrrÚobjectrrUr9r9r9r:Ús     S