
    i                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	Z	ddl
Z
ddlmZ erddlmZmZ d	Zdd
ZddZddZ G d de      ZddZddZy)z6
:func:`~pandas.eval` source string parsing functions
    )annotations)Enum)StringIO)	iskeywordN)TYPE_CHECKING)HashableIteratord   c                   | j                         rt        |       s| S d | D        }dj                  d |D              } t        j                  j                         D ci c]  \  }}|dt        j                  |    d }}}|j                  dddddd	d
dddd
       dj                  | D cg c]  }|j                  ||       c}      } d|  } | j                         st        d|  d      | S c c}}w c c}w )aj  
    Create valid Python identifiers from any string.

    Check if name contains any special characters. If it contains any
    special characters, the special characters will be replaced by
    a special string and a prefix is added.

    Raises
    ------
    SyntaxError
        If the returned name is not a Python valid identifier, raise an exception.
    c           	   3  p   K   | ].  }|d j                  d |j                  dd      D              f 0 yw) c              3  2   K   | ]  }t        |        y w)N)chr).0bs     \/app/cer_product_mecsu/.venv/lib/python3.12/site-packages/pandas/core/computation/parsing.py	<genexpr>z;create_valid_python_identifier.<locals>.<genexpr>.<genexpr>+   s     JqCFJs   asciibackslashreplaceN)joinencode)r   cs     r   r   z1create_valid_python_identifier.<locals>.<genexpr>*   s8       
BGGJAHHW6H$IJJKs   46r   c              3  T   K   | ]   \  }}|j                  d ||k7  rdnd       " yw)\	_UNICODE__BACKSLASH_N)replace)r   r   	c_escapeds      r   r   z1create_valid_python_identifier.<locals>.<genexpr>.   s1      Ay 	$qI~=Qs   &(__QUESTIONMARK__EXCLAMATIONMARK__DOLLARSIGN_
_EUROSIGN__DEGREESIGN__SINGLEQUOTE__DOUBLEQUOTE__HASH_
_BACKTICK_)
 ?!$u   €   °'"#`BACKTICK_QUOTED_STRING_zCould not convert 'z' to a valid Python identifier.)isidentifierr   r   tokenizeEXACT_TOKEN_TYPESitemstokentok_nameupdategetSyntaxError)namegenchartokvalspecial_characters_replacementss        r   create_valid_python_identifierrA      s-    9T?C 77  D &77==?'D& 	%..()++'# ' $**!$   	
 77PTU377dCUVD$TF+D/v5TUVVK3'& Vs    "C60C<c                \    | \  }}|t         k(  rt        j                  t        |      fS ||fS )a[  
    Clean up a column name if surrounded by backticks.

    Backtick quoted string are indicated by a certain tokval value. If a string
    is a backtick quoted token it will processed by
    :func:`_create_valid_python_identifier` so that the parser can find this
    string when the query is executed.
    In this case the tok will get the NAME tokval.

    Parameters
    ----------
    tok : tuple of int, str
        ints correspond to the all caps constants in the tokenize module

    Returns
    -------
    tok : Tuple[int, str]
        Either the input or token or the replacement values
    )BACKTICK_QUOTED_STRINGr4   NAMErA   )toktoknumr?   s      r   clean_backtick_quoted_toksrG   R   s5    ( NFF''}}<VDDD6>    c                    	 t        | t              r| j                  dd      n| } t        d|  d      }t	        |      d   }t        |      S # t        $ r | cY S w xY w)a  
    Function to emulate the cleaning of a backtick quoted name.

    The purpose for this function is to see what happens to the name of
    identifier if it goes to the process of being parsed a Python code
    inside a backtick quoted string and than being cleaned
    (removed of any special characters).

    Parameters
    ----------
    name : hashable
        Name to be cleaned.

    Returns
    -------
    name : hashable
        Returns the name after tokenizing and cleaning.
    r1   z``   )
isinstancestrr   tokenize_stringnextrA   r;   )r<   	tokenizedr?   s      r   clean_column_namerP   l   sa    &*4T3*?t||C&T#avQK0	i#-f55 s   AA AAc                      e Zd ZdZdZdZdZy)
ParseStater   rJ         N)__name__
__module____qualname__DEFAULTIN_BACKTICKIN_SINGLE_QUOTEIN_DOUBLE_QUOTE rH   r   rR   rR      s    GKOOrH   rR   c                L   g }g }d}t         j                  }|t        |       k  r| |   }|xdk(  r |t         j                  k(  r=|r"|j                  ddj	                  |      f       |g}|dz  }t         j
                  }k|t         j
                  k(  rD|t        |       dz
  k7  r| |dz      nd}|dk(  r(|j                  |       |j                  |       |dz  }|j                  |       |j                  ddj	                  |      f       g }|dz  }t         j                  }xd	k(  rT |t         j                  k(  rt         j                  }n|t         j                  k(  rs| |dz
     d
k7  rht         j                  }nWdk(  rS|t         j                  k(  rt         j                  }n/|t         j                  k(  r| |dz
     d
k7  rt         j                  }	 |j                  |       |dz  }|t        |       k  r|r"|j                  ddj	                  |      f       |S )a  
    Splits a str into substrings along backtick characters (`).

    Disregards backticks inside quotes.

    Parameters
    ----------
    s : str
        The Python source code string.

    Returns
    -------
    substrings: list[tuple[bool, str]]
        List of tuples, where each tuple has two elements:
        The first is a boolean indicating if the substring is backtick-quoted.
        The second is the actual substring.
    r   r1   Fr   rJ   NrS   Tr.   r   r/   )rR   rX   lenappendr   rY   rZ   r[   )s
substringssubstriparse_stater>   	next_chars          r   _split_by_backtickrf      s   $ JF	A$$K
c!f*t*"4"44"))5"''&/*BC"VFFA","8"8K J$:$::-.#a&1*_!a%4I C'd+i0Q  d+"))4*AB!#Q&0&8&8 *"4"44","<"<K!Z%?%??aAhRVFV","4"4K*"4"44","<"<K!Z%?%??aAhRVFV","4"4Kd	Qa c!f*d 5"''&/23rH   c              #     K   dj                  d t        |       D              } t        |       j                  }t	        j
                  |      }|D ]  \  }}}}}||f  yw)a$  
    Tokenize a Python source code string.

    Parameters
    ----------
    source : str
        The Python source code string.

    Returns
    -------
    tok_generator : Iterator[Tuple[int, str]]
        An iterator yielding all tokens with only toknum and tokval (Tuple[ing, str]).
    r   c              3  F   K   | ]  \  }}|rt        |d d       n|  yw)rJ   N)rA   )r   is_backtick_quoted	substrings      r   r   z"tokenize_string.<locals>.<genexpr>   s7       *	 " +9Qr?;s   !N)r   rf   r   readliner4   generate_tokens)sourceline_readertoken_generatorrF   r?   r   s         r   rM   rM      sq       WW  .@-G F 6"++K..{;O#2 1afns   A A")r<   rL   returnrL   )rE   tuple[int, str]rq   rr   )r<   r   rq   r   )r`   rL   rq   zlist[tuple[bool, str]])rn   rL   rq   zIterator[tuple[int, str]])__doc__
__future__r   enumr   ior   keywordr   r7   r4   typingr   collections.abcr   r	   rC   rA   rG   rP   rR   rf   rM   r\   rH   r   <module>rz      sY    #         7t4< K\rH   