HEX
Server: Apache
System: Linux srv1.prosuiteplus.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: prosuiteplus (1001)
PHP: 8.3.20
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/jmespath/__pycache__/parser.cpython-38.pyc
U

Q�Y�J�@spdZddlZddlmZddlmZddlmZddlmZddlmZGdd	�d	e	�Z
eGd
d�de	��ZdS)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�eZdZddddddddddddddddddddddddd	d
ddd
d�ZdZiZdZdxdd�Zdd�Zdd�Z	dd�Z
dydd�Zdd�Zdd�Z
dd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9d:�Zd;d<�Zd=d>�Zd?d@�ZdAdB�ZdCdD�Z dEdF�Z!dGdH�Z"dIdJ�Z#dKdL�Z$dMdN�Z%dOdP�Z&dQdR�Z'dSdT�Z(dUdV�Z)dWdX�Z*dYdZ�Z+d[d\�Z,d]d^�Z-d_d`�Z.dadb�Z/dzddde�Z0dfdg�Z1dhdi�Z2djdk�Z3dldm�Z4dndo�Z5dpdq�Z6drds�Z7dtdu�Z8e9dvdw��Z:dcS){�Parserr�����	���(�-�2�7�<)�eof�unquoted_identifier�quoted_identifier�literal�rbracket�rparen�comma�rbrace�numberZcurrent�expref�colon�pipe�or�and�eq�gt�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�r8�1/usr/lib/python3/dist-packages/jmespath/parser.py�__init__NszParser.__init__cCsH|j�|�}|dk	r|S|�|�}||j|<t|j�|jkrD|��|S�N)�_CACHE�get�	_do_parse�len�	_MAX_SIZE�_free_cache_entries)r6�
expression�cachedZ
parsed_resultr8r8r9�parseTs

zParser.parsec
Cs�z|�|�WStjk
r8}z||_�W5d}~XYnZtjk
rf}z|�|��W5d}~XYn,tjk
r�}z||_�W5d}~XYnXdSr;)�_parserZ
LexerErrorrB�IncompleteExpressionErrorZset_expression�
ParseError)r6rB�er8r8r9r>^s
zParser._do_parsecCsrt���|�|_t|j�|_d|_|jdd�}|��dksh|�	d�}t
�|d|d|dd|d��t||�S)Nr)�
binding_powerr�start�value�typezUnexpected token: %s)
rZLexer�tokenizer3�listr4r5�_expression�_current_token�_lookahead_tokenrrG�ParsedResult)r6rB�parsed�tr8r8r9rEks

�z
Parser._parsecCs�|�d�}|��t|d|d|j�}||�}|��}||j|kr�t|d|d�}|dkrt|�d�}|�|�q8|��||�}|��}q8|S)Nrz
_token_nud_%srLz
_token_led_%s)rQ�_advance�getattr�_error_nud_tokenrP�
BINDING_POWER�_error_led_token)r6rIZ
left_tokenZnud_function�left�
current_tokenZledZerror_tokenr8r8r9rOvs$

�

zParser._expressioncCst�|d�S�NrK)rr�r6�tokenr8r8r9�_token_nud_literal�szParser._token_nud_literalcCst�|d�Sr\)r�fieldr]r8r8r9�_token_nud_unquoted_identifier�sz%Parser._token_nud_unquoted_identifiercCs@t�|d�}|��dkr<|�d�}t�d|d|dd��|S)NrKr/rrLz1Quoted identifier not allowed for function names.)rr`rPrQrrG)r6r^r`rTr8r8r9�_token_nud_quoted_identifier�s
�z#Parser._token_nud_quoted_identifiercCs:t��}|��dkrt��}n|�|jd�}t�||�S)Nrr))r�identityrP�_parse_projection_rhsrX�value_projection�r6r^rZ�rightr8r8r9�_token_nud_star�s

zParser._token_nud_starcCs|�t���Sr;)�_token_led_filterrrcr]r8r8r9�_token_nud_filter�szParser._token_nud_filtercCs|��Sr;)�_parse_multi_select_hashr]r8r8r9�_token_nud_lbrace�szParser._token_nud_lbracecCs|��}|�d�|S)Nr)rO�_match�r6r^rBr8r8r9�_token_nud_lparen�s
zParser._token_nud_lparencCs*t�t���}|�|jd�}t�||�S�Nr()rr(rcrdrX�
projectionrfr8r8r9�_token_nud_flatten�s
�zParser._token_nud_flattencCs|�|jd�}t�|�S)Nr,)rOrXrZnot_expression)r6r^�exprr8r8r9�_token_nud_not�szParser._token_nud_notcCsz|��dkr$|��}|�t��|�S|��dkrn|�d�dkrn|��|��|�|jd�}t�	t��|�S|�
�SdS)N�rrr)rr)rP�_parse_index_expression�_project_if_slicerrc�
_lookaheadrUrdrXrq�_parse_multi_select_list)r6r^rgr8r8r9�_token_nud_lbracket�s�zParser._token_nud_lbracketcCsR|�d�dks|�d�dkr$|��St�|�d�d�}|��|�d�|SdS)NrrrrKr)rx�_parse_slice_expressionr�indexrQrUrm)r6�noder8r8r9rv�s�
zParser._parse_index_expressioncCs�dddg}d}|��}|dks�|dkr�|dkrZ|d7}|dkrP|�|�d�d�|��n6|dkr~|�d�d||<|��n|�|�d�d�|��}q|�d�tj|�S)	Nrrr
rr�syntax errorrrK)rP�_raise_parse_error_for_tokenrQrUrmr�slice)r6�partsr|r[r8r8r9r{�s,
�

�

zParser._parse_slice_expressioncCst��Sr;)rZcurrent_noder]r8r8r9�_token_nud_current�szParser._token_nud_currentcCs|�|jd�}t�|�S)Nr)rOrXrrrnr8r8r9�_token_nud_expref�szParser._token_nud_exprefcCsr|��dksJ|�|jd�}|ddkr:|d�|�|St�||g�Sn$|��|�|jd�}t�||�SdS)Nr)r+rL�
subexpression�children)	rP�_parse_dot_rhsrX�appendrr�rUrdre�r6rZrgr8r8r9�_token_led_dot�s�zParser._token_led_dotcCs|�|jd�}t�||�S)Nr)rOrXrrr�r8r8r9�_token_led_pipeszParser._token_led_pipecCs|�|jd�}t�||�S)Nr )rOrXrZ
or_expressionr�r8r8r9�
_token_led_or	szParser._token_led_orcCs|�|jd�}t�||�S)Nr!)rOrXrZand_expressionr�r8r8r9�_token_led_and
szParser._token_led_andcCs�|ddkr:|�d�}t�|d|d|dd|d��|d}g}|��dks||��}|��dkrp|�d�|�|�qF|�d�t�||�}|S)	NrLr`���rJrKzInvalid function name '%s'rr)	rQrrGrPrOrmr�rZfunction_expression)r6rZZprev_t�name�argsrBZ
function_noder8r8r9�_token_led_lparens$

�

zParser._token_led_lparencCsH|�d�}|�d�|��dkr*t��}n|�|jd�}t�|||�S)Nrrr(r*)rOrmrPrrcrdrXZfilter_projection)r6rZZ	conditionrgr8r8r9ri%s


zParser._token_led_filtercCs|�|d�S)Nr"��_parse_comparator�r6rZr8r8r9�
_token_led_eq/szParser._token_led_eqcCs|�|d�S)Nr'r�r�r8r8r9�
_token_led_ne2szParser._token_led_necCs|�|d�S)Nr#r�r�r8r8r9�
_token_led_gt5szParser._token_led_gtcCs|�|d�S)Nr%r�r�r8r8r9�_token_led_gte8szParser._token_led_gtecCs|�|d�S)Nr$r�r�r8r8r9�
_token_led_lt;szParser._token_led_ltcCs|�|d�S)Nr&r�r�r8r8r9�_token_led_lte>szParser._token_led_ltecCs&t�|�}|�|jd�}t�||�Srp)rr(rdrXrqr�r8r8r9�_token_led_flattenAs

�zParser._token_led_flattencCs~|�d�}|ddkrJ|��}|ddkr<|d�|�|S|�||�Sn0|�d�|�d�|�|jd�}t�||�SdS)NrrLru�index_expressionr�r)r)	rQrvr�rwrmrdrXrrq)r6rZr^rgr8r8r9�_token_led_lbracketGs


zParser._token_led_lbracketcCs:t�||g�}|ddkr2t�||�|jd��S|SdS)NrLr�r))rr�rqrdrX)r6rZrgZ
index_exprr8r8r9rwZs�zParser._project_if_slicecCs|�|j|�}t�|||�Sr;)rOrXr�
comparator)r6rZr�rgr8r8r9r�cszParser._parse_comparatorcCsFg}|��}|�|�|��dkr&q2q|�d�q|�d�t�|�S)Nrr)rOr�rPrmrZmulti_select_list)r6ZexpressionsrBr8r8r9rygs

zParser._parse_multi_select_listcCs�g}|�d�}|jddgd�|d}|�d�|�d�}tj||d�}|�|�|��dkrj|�d�q|��d	kr|�d	�q�qtj|d
�S)Nrrr)�token_typesrKr)�key_namer}rr)Znodes)	rQ�_match_multiple_tokensrmrOrZkey_val_pairr�rPZmulti_select_dict)r6ZpairsZ	key_tokenr�rKr}r8r8r9rkss 
�



zParser._parse_multi_select_hashcCs�|j|��|jkrt��}nd|��dkr6|�|�}nL|��dkrN|�|�}n4|��dkrp|�d�|�|�}n|�|�	d�d�|S)Nr.r*r+rr~)
rXrP�_PROJECTION_STOPrrcrOrmr�rrQ)r6rIrgr8r8r9rd�s

�zParser._parse_projection_rhscCs�|��}|dkr|�|�S|dkr4|�d�|��S|dkrN|�d�|��S|�d�}ddddg}d||df}|�||�dS)	N)rrr)r.r-rrr�Expecting: %s, got: %srL)rPrOrmryrkrQr)r6rIr7rTZallowed�msgr8r8r9r��s"	



��zParser._parse_dot_rhscCs6|ddkr&t�|d|d|d��|�|d�dS)NrLrrJrK�
invalid token)rrFrr]r8r8r9rW�s�zParser._error_nud_tokencCs|�|d�dS)Nr�)rr]r8r8r9rY�szParser._error_led_tokenNcCs,|��|kr|��n|�||�d��dSr2)rPrU�_raise_parse_error_maybe_eofrQ)r6�
token_typer8r8r9rm�s
�z
Parser._matchcCs*|��|kr|�||�d��|��dSr2)rPr�rQrU)r6r�r8r8r9r��s�zParser._match_multiple_tokenscCs|jd7_dS)Nr)r5�r6r8r8r9rU�szParser._advancecCs|j|jdS�NrL�r4r5r�r8r8r9rP�szParser._current_tokencCs|j|j|dSr�r��r6rr8r8r9rx�szParser._lookaheadcCs|j|j|Sr;r�r�r8r8r9rQ�szParser._lookahead_tokencCs,|d}|d}|d}t�||||��dS)NrJrKrL)rrG)r6r^�reason�lex_position�actual_value�actual_typer8r8r9r�s�z#Parser._raise_parse_error_for_tokencCsN|d}|d}|d}|dkr.t�|||��d||f}t�||||��dS)NrJrKrLrr�)rrFrG)r6Z
expected_typer^r�r�r��messager8r8r9r��s$���z#Parser._raise_parse_error_maybe_eofcCs.t�|j��t|jd��D]}|j|=qdS)Nr	)�randomZsampler<�keys�intr@)r6�keyr8r8r9rA�s zParser._free_cache_entriescCs|j��dS)z'Clear the expression compilation cache.N)r<�clear)�clsr8r8r9�purge�szParser.purge)r	)r)N);�__name__�
__module__�__qualname__rXr�r<r@r:rDr>rErOr_rarbrhrjrlrorrrtrzrvr{r�r�r�r�r�r�r�rir�r�r�r�r�r�r�r�rwr�ryrkrdr�rWrYrmr�rUrPrxrQrr�rA�classmethodr�r8r8r8r9r%s��"




	
	rc@s.eZdZdd�Zd
dd�Zdd�Zdd	�ZdS)rRcCs||_||_dSr;)rBrS)r6rBrSr8r8r9r:�szParsedResult.__init__NcCst�|�}|�|j|�}|Sr;)rZTreeInterpreter�visitrS)r6rKZoptionsZinterpreter�resultr8r8r9�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�rS)r6Zrenderer�contentsr8r8r9�_render_dot_files
zParsedResult._render_dot_filecCs
t|j�Sr;)�reprrSr�r8r8r9�__repr__szParsedResult.__repr__)N)r�r�r�r:r�r�r�r8r8r8r9rR�s
rR)�__doc__r�ZjmespathrZjmespath.compatrrrr�objectrrRr8r8r8r9�<module>sS