Current File : //usr/lib/python3/dist-packages/jmespath/__pycache__/parser.cpython-312.pyc
�

���b�J���dZddlZddlmZddlmZddlmZddlmZddlmZGd�d	e	�Z
eGd
�de	��Zy)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���eZdZidd�dd�dd�dd�dd�dd�dd�d	d�d
d�dd�dd�d
d�dd�dd�dd�dd�dd�ddddddddddddd��Zd ZiZd!ZdVd"�Zd#�Zd$�Z	d%�Z
dWd&�Zd'�Zd(�Z
d)�Zd*�Zd+�Zd,�Zd-�Zd.�Zd/�Zd0�Zd1�Zd2�Zd3�Zd4�Zd5�Zd6�Zd7�Zd8�Zd9�Zd:�Zd;�Z d<�Z!d=�Z"d>�Z#d?�Z$d@�Z%dA�Z&dB�Z'dC�Z(dD�Z)dE�Z*dF�Z+dG�Z,dH�Z-dI�Z.dJ�Z/dXdL�Z0dM�Z1dN�Z2dO�Z3dP�Z4dQ�Z5dR�Z6dS�Z7dT�Z8e9dU��Z:yK)Y�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�
�c�D�d|_dg|z|_||_d|_y�Nr)�	tokenizer�_tokens�_buffer_size�_index)�self�	lookaheads  �1/usr/lib/python3/dist-packages/jmespath/parser.py�__init__zParser.__init__Ns&������v�	�)���%������c���|jj|�}|�|S|j|�}||j|<t|j�|jkDr|j�|S�N)�_CACHE�get�	_do_parse�len�	_MAX_SIZE�_free_cache_entries)r;�
expression�cached�
parsed_results    r=�parsezParser.parseTsb��������,�����M����z�2�
�"/����J���t�{�{��d�n�n�,��$�$�&��r?c��	|j|�S#tj$r
}||_�d}~wtj$r}|j|��d}~wtj$r
}||_�d}~wwxYwrA)�_parser�
LexerErrorrH�IncompleteExpressionError�set_expression�
ParseError)r;rH�es   r=rDzParser._do_parse^so��
	��;�;�z�*�*���$�$�	�%�A�L����3�3�	�
���Z�(����$�$�	�%�A�L���	�s*��A9�.�A9�A�A9�,A4�4A9c�f�tj�j|�|_t	|j�|_d|_|jd��}|j�dk(s8|jd�}tj|d|d|dd|dz��t||�S)Nr)�
binding_powerr
�start�value�typezUnexpected token: %s)
r�Lexer�tokenizer7�listr8r:�_expression�_current_token�_lookahead_tokenrrQ�ParsedResult)r;rH�parsed�ts    r=rMz
Parser._parseks�������/�/�
�;����D�N�N�+�������!�!��!�2���"�"�$��-��%�%�a�(�A��'�'��'�
�A�g�J��&�	�(>��7��(K�M�
M��J��/�/r?c��|jd�}|j�t|d|dz|j�}||�}|j	�}||j
|krpt|d|zd�}|�#|jd�}|j
|�n(|j�||�}|j	�}||j
|kr�p|S)Nrz
_token_nud_%srWz
_token_led_%s)r]�_advance�getattr�_error_nud_tokenr\�
BINDING_POWER�_error_led_token)r;rT�
left_token�nud_function�left�
current_token�led�error_tokens        r=r[zParser._expressionvs����*�*�1�-�
��
�
����/�J�v�$6�6��!�!�#���J�'���+�+�-�
��d�0�0��?�?��$��-� ?��F�C��{�"�3�3�A�6���%�%�k�2��
�
���4�y�� $� 3� 3� 5�
��d�0�0��?�?��r?c�2�tj|d�S�NrV)rr
�r;�tokens  r=�_token_nud_literalzParser._token_nud_literal�s���{�{�5��>�*�*r?c�2�tj|d�Srn)r�fieldros  r=�_token_nud_unquoted_identifierz%Parser._token_nud_unquoted_identifier�s���y�y��w��(�(r?c��tj|d�}|j�dk(r/|jd�}t	j
d|d|dd��|S)NrVr2rrWz1Quoted identifier not allowed for function names.)rrsr\r]rrQ)r;rprsr`s    r=�_token_nud_quoted_identifierz#Parser._token_nud_quoted_identifier�se���	�	�%��.�)����� �H�,��%�%�a�(�A��'�'��1�W�:�q��y�C�E�
E��r?c���tj�}|j�dk(rtj�}n|j|jd�}tj
||�S)Nrr,)r�identityr\�_parse_projection_rhsre�value_projection�r;rpri�rights    r=�_token_nud_starzParser._token_nud_star�sW���|�|�~����� �J�.��L�L�N�E��.�.�t�/A�/A�&�/I�J�E��#�#�D�%�0�0r?c�H�|jtj��SrA)�_token_led_filterrrxros  r=�_token_nud_filterzParser._token_nud_filter�s���%�%�c�l�l�n�5�5r?c�"�|j�SrA)�_parse_multi_select_hashros  r=�_token_nud_lbracezParser._token_nud_lbrace�s���,�,�.�.r?c�H�|j�}|jd�|S)Nr)r[�_match�r;rprHs   r=�_token_nud_lparenzParser._token_nud_lparen�s"���%�%�'�
����H���r?c��tjtj��}|j|jd�}tj
||�S�Nr+)rr+rxryre�
projectionr{s    r=�_token_nud_flattenzParser._token_nud_flatten�sE���{�{�3�<�<�>�*���*�*����y�)�+���~�~�d�E�*�*r?c�h�|j|jd�}tj|�S)Nr/)r[rer�not_expression)r;rp�exprs   r=�_token_nud_notzParser._token_nud_not�s.������ 2� 2�5� 9�:���!�!�$�'�'r?c���|j�dvr4|j�}|jtj�|�S|j�dk(rz|jd�dk(rf|j
�|j
�|j|jd�}tjtj�|�S|j�S)N�rrr,rr)r\�_parse_index_expression�_project_if_slicerrx�
_lookaheadrbryrer��_parse_multi_select_list)r;rpr|s   r=�_token_nud_lbracketzParser._token_nud_lbracket�s������ �$7�7��0�0�2�E�
�)�)�#�,�,�.�%�@�@�
�
 �
 �
"�f�
,�����"�j�0��M�M�O��M�M�O��.�.�t�/A�/A�&�/I�J�E��>�>�#�,�,�.�%�8�8��0�0�2�2r?c��|jd�dk(s|jd�dk(r|j�Stj|j	d�d�}|j�|j
d�|S)NrrrrVr)r��_parse_slice_expressionr�indexr]rbr�)r;�nodes  r=r�zParser._parse_index_expression�so��

�O�O�A��'�)�����"�g�-��/�/�1�1��9�9�T�2�2�1�5�g�>�?�D��M�M�O��K�K�
�#��Kr?c���gd�}d}|j�}|dk(s�|dkr�|dk(r<|dz
}|dk(r!|j|jd�d�|j�nN|dk(r(|jd�d	||<|j�n!|j|jd�d�|j�}|dk(s|dkr��|j	d�tj|�S)
N)NNNrrrrr�syntax errorrrV)r\�_raise_parse_error_for_tokenr]rbr�r�slice)r;�partsr�rjs    r=r�zParser._parse_slice_expression�s���#�����+�+�-�
��:�-�%�!�)���'���
���A�:��5�5��-�-�a�0�.�B��
�
���(�*�#�4�4�Q�7��@��e���
�
���1�1��)�)�!�,�n�>� �/�/�1�M� �:�-�%�!�)�	
���J���y�y�%� � r?c�*�tj�SrA)r�current_noderos  r=�_token_nud_currentzParser._token_nud_current�s�����!�!r?c�h�|j|jd�}tj|�S)Nr)r[rerrr�s   r=�_token_nud_exprefzParser._token_nud_expref�s,���%�%�d�&8�&8��&B�C�
��z�z�*�%�%r?c�V�|j�dk(sS|j|jd�}|ddk(r|dj|�|St	j
||g�S|j
�|j|jd�}t	j||�S)Nr,r.rW�
subexpression�children)	r\�_parse_dot_rhsre�appendrr�rbryrz�r;rir|s   r=�_token_led_dotzParser._token_led_dot�s����"�"�$��.��'�'��(:�(:�5�(A�B�E��F�|��.��Z� �'�'��.����(�(�$���7�7�
�M�M�O��.�.��"�"�5�)�+�E��'�'��e�4�4r?c�j�|j|jd�}tj||�S)Nr)r[rerrr�s   r=�_token_led_pipezParser._token_led_pipes.��� � ��!3�!3�F�!;�<���x�x��e�$�$r?c�j�|j|jd�}tj||�S)Nr)r[rer�
or_expressionr�s   r=�
_token_led_orzParser._token_led_or	s0��� � ��!3�!3�D�!9�:��� � ��u�-�-r?c�j�|j|jd�}tj||�S)Nr)r[rer�and_expressionr�s   r=�_token_led_andzParser._token_led_and
s0��� � ��!3�!3�E�!:�;���!�!�$��.�.r?c��|ddk7r8|jd�}tj|d|d|dd|dz��|d}g}|j�dk(sY|j	�}|j�dk(r|jd�|j
|�|j�dk(s�Y|jd�tj||�}|S)	NrWrs���rUrVzInvalid function name '%s'rr)	r]rrQr\r[r�r�r�function_expression)r;ri�prev_t�name�argsrH�
function_nodes       r=�_token_led_lparenzParser._token_led_lparens�����<�7�"��*�*�2�.�F��'�'��w�����&��.�,�v�g��>�@�
@��G�}�����%�%�'�8�3��)�)�+�J��"�"�$��/����G�$��K�K�
�#�	�%�%�'�8�3�
	
���H���/�/��d�;�
��r?c��|jd�}|jd�|j�dk(rtj�}n|j|jd�}tj|||�S)Nrrr+r-)r[r�r\rrxryre�filter_projection)r;ri�	conditionr|s    r=rzParser._token_led_filter%sj���$�$�Q�'�	����J����� �I�-��L�L�N�E��.�.�t�/A�/A�(�/K�L�E��$�$�T�5�)�<�<r?c�&�|j|d�S)Nr��_parse_comparator�r;ris  r=�
_token_led_eqzParser._token_led_eq/����%�%�d�D�1�1r?c�&�|j|d�S)Nr*r�r�s  r=�
_token_led_nezParser._token_led_ne2r�r?c�&�|j|d�S)Nrr�r�s  r=�
_token_led_gtzParser._token_led_gt5r�r?c�&�|j|d�S)Nr(r�r�s  r=�_token_led_gtezParser._token_led_gte8����%�%�d�E�2�2r?c�&�|j|d�S)Nr'r�r�s  r=�
_token_led_ltzParser._token_led_lt;r�r?c�&�|j|d�S)Nr)r�r�s  r=�_token_led_ltezParser._token_led_lte>r�r?c��tj|�}|j|jd�}tj||�Sr�)rr+ryrer�r�s   r=�_token_led_flattenzParser._token_led_flattenAs?���{�{�4� ���*�*����y�)�+���~�~�d�E�*�*r?c�^�|jd�}|ddvr@|j�}|ddk(r|dj|�|S|j||�S|j	d�|j	d�|j|jd�}tj||�S)NrrWr��index_expressionr�r,r)	r]r�r�r�r�ryrerr�)r;rirpr|s    r=�_token_led_lbracketzParser._token_led_lbracketGs����%�%�a�(����=�/�/��0�0�2�E��F�|�1�1��Z� �'�'��.����-�-�d�E�:�:�
�K�K����K�K�
�#��.�.�t�/A�/A�&�/I�J�E��>�>�$��.�.r?c��tj||g�}|ddk(r2tj||j|jd��S|S)NrWr�r,)rr�r�ryre)r;rir|�
index_exprs    r=r�zParser._project_if_sliceZsY���)�)�4��-�8�
���=�G�#��>�>���*�*�4�+=�+=�f�+E�F�H�
H��r?c�l�|j|j|�}tj|||�SrA)r[rer�
comparator)r;rir�r|s    r=r�zParser._parse_comparatorcs0��� � ��!3�!3�J�!?�@���~�~�j�$��6�6r?c���g}	|j�}|j|�|j�dk(rn|jd��G|jd�t	j
|�S)Nrr)r[r�r\r�r�multi_select_list)r;�expressionsrHs   r=r�zParser._parse_multi_select_listgsj������)�)�+�J����z�*��"�"�$�
�2�����G�$�
�	
���J���$�$�[�1�1r?c��g}	|jd�}|jddg��|d}|jd�|jd�}t	j
||��}|j
|�|j�dk(r|jd�n%|j�d	k(r|jd	�n��t	j|�
�S)Nrrr)�token_typesrVr)�key_namer�rr)�nodes)	r]�_match_multiple_tokensr�r[r�key_val_pairr�r\�multi_select_dict)r;�pairs�	key_tokenr�rVr�s      r=r�zParser._parse_multi_select_hashss�������-�-�a�0�I�
�'�'�0�2G�H�
(�
J� ��)�H��K�K�� ��$�$�Q�'�E��#�#�X�E�B�D��L�L����"�"�$��/����G�$��$�$�&�(�2����H�%��� �$�$�5�1�1r?c���|j|j�|jkrtj�}|S|j�dk(r|j|�}|S|j�dk(r|j|�}|S|j�dk(r$|j
d�|j|�}|S|j|jd�d�S)Nr1r-r.rr�)
rer\�_PROJECTION_STOPrrxr[r�r�r�r])r;rTr|s   r=ryzParser._parse_projection_rhs�s������d�1�1�3�4�t�7L�7L�L��L�L�N�E����
 �
 �
"�j�
0��$�$�]�3�E����
 �
 �
"�h�
.��$�$�]�3�E���
�
 �
 �
"�e�
+��K�K����'�'�
�6�E���
�-�-�d�.C�.C�A�.F�.<�
>��r?c�J�|j�}|dvr|j|�S|dk(r!|jd�|j�S|dk(r!|jd�|j	�S|jd�}gd�}d|�d|d��}|j
||�y)	N)rrr,r1r0r)rrr1r0�Expecting: �, got: rW)r\r[r�r�r�r]r�)r;rTr<r`�allowed�msgs      r=r�zParser._parse_dot_rhs�s����'�'�)�	��L�L��#�#�M�2�2�
�*�
$��K�K�
�#��0�0�2�2�
�(�
"��K�K��!��0�0�2�2��%�%�a�(�A�-�G�-4�Q�v�Y�?�
�
�-�-�a��5r?c�x�|ddk(r tj|d|d|d��|j|d�y)NrWr
rUrV�
invalid token)rrOr�ros  r=rdzParser._error_nud_token�sD����=�E�!��6�6��g���g���f�
�?�
?��)�)�%��Ar?c�(�|j|d�y)Nr�)r�ros  r=rfzParser._error_led_token�s���)�)�%��Ar?Nc��|j�|k(r|j�y|j||jd��yr6)r\rb�_raise_parse_error_maybe_eofr])r;�
token_types  r=r�z
Parser._match�s:����� �J�.��M�M�O��-�-��D�1�1�!�4�
6r?c��|j�|vr!|j||jd��|j�yr6)r\r�r]rb)r;r�s  r=r�zParser._match_multiple_tokens�s:����� ��3��-�-��T�2�2�1�5�
7��
�
�r?c�.�|xjdz
c_y)Nr)r:�r;s r=rbzParser._advance�s�����q��r?c�:�|j|jdS�NrW�r8r:r�s r=r\zParser._current_token�s���|�|�D�K�K�(��0�0r?c�@�|j|j|zdSr�r��r;rs  r=r�zParser._lookahead�s���|�|�D�K�K�&�0�1�&�9�9r?c�:�|j|j|zSrAr�r�s  r=r]zParser._lookahead_token�s���|�|�D�K�K�&�0�1�1r?c�P�|d}|d}|d}tj||||��)NrUrVrW)rrQ)r;rp�reason�lex_position�actual_value�actual_types      r=r�z#Parser._raise_parse_error_for_token�s9���W�~���W�~���F�m���#�#�L�,�$/��9�	9r?c��|d}|d}|d}|dk(rtj|||��d|�d|��}tj||||��)NrUrVrWr
r�r�)rrOrQ)r;�
expected_typerpr�r�r��messages       r=r�z#Parser._raise_parse_error_maybe_eof�so���W�~���W�~���F�m���%���6�6��l�K�9�
9� �/<�.9�;���#�#��,��W�>�	>r?c���tjt|jj	��t|jdz��D]}|jj|d�� y)Nr)�random�samplerZrB�keys�intrF�pop)r;�keys  r=rGzParser._free_cache_entries�sO���=�=��d�k�k�&6�&6�&8�!9�3�t�~�~�PQ�?Q�;R�S�	'�C��K�K�O�O�C��&�	'r?c�8�|jj�y)z'Clear the expression compilation cache.N)rB�clear)�clss r=�purgezParser.purge�s��	�
�
���r?)r)rrA);�__name__�
__module__�__qualname__rer�rBrFr>rKrDrMr[rqrtrvr}r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�r�r�r�r�r�r�r�r�r�r�r�ryr�rdrfr�r�rbr\r�r]r�r�rG�classmethodr
�r?r=r	r	%s!���
�q���q��	�Q��	�1�	�
	�A��	�!�
�	���	�!��	�!��	�1��	�!��	���	���	
�a��	�q�� 	
�a�!�"	
�a�#�$������������=�M�D���F��I����	0��&+�)�	�1�6�/��
+�(�3�"
�!�0"�&�
5�%�.�/��(=�2�2�2�3�2�3�+�/�&�7�
2�2�(�"6�8B�B�6���1�:�2�9�
>�'����r?r	c�&�eZdZd�Zdd�Zd�Zd�Zy)r^c� �||_||_yrA)rHr_)r;rHr_s   r=r>zParsedResult.__init__�s��$�����r?Nc�h�tj|�}|j|j|�}|SrA)r�TreeInterpreter�visitr_)r;rV�options�interpreter�results     r=�searchzParsedResult.search�s-���-�-�g�6���"�"�4�;�;��6���
r?c�d�tj�}|j|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.

        )r�GraphvizVisitorrr_)r;�renderer�contentss   r=�_render_dot_filezParsedResult._render_dot_files)���*�*�,���>�>�$�+�+�.���r?c�,�t|j�SrA)�reprr_r�s r=�__repr__zParsedResult.__repr__s���D�K�K� � r?rA)rrrr>rr r#rr?r=r^r^�s����
�!r?r^)�__doc__r�jmespathr�jmespath.compatrrrr�objectr	r^rr?r=�<module>r(sH���6��,����M�V�M�`�!�6�!��!r?