
    Pi                        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dS ) z6
:func:`~pandas.eval` source string parsing functions
    )annotations)Enum)StringIO)	iskeywordN)TYPE_CHECKING)HashableIteratord   namestrreturnc                   |                                  rt          |           s| S d | D             }d                    d |D                       } d t          j                                        D                                 ddddd	d
ddddd
           d                    fd| D                       } d|  } |                                  st          d|  d          | S )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     K   | ]9}|d                      d |                    dd          D                       fV  :dS ) c              3  4   K   | ]}t          |          V  d S )N)chr).0bs     s/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/pandas/core/computation/parsing.py	<genexpr>z;create_valid_python_identifier.<locals>.<genexpr>.<genexpr>+   s(      JJqCFFJJJJJJ    asciibackslashreplaceN)joinencode)r   cs     r   r   z1create_valid_python_identifier.<locals>.<genexpr>*   se         
BGGJJAHHW6H$I$IJJJJJK     r   r   c              3  X   K   | ]%\  }}|                     d ||k    rdnd          V  &dS )\	_UNICODE__BACKSLASH_N)replace)r   r   	c_escapeds      r   r   z1create_valid_python_identifier.<locals>.<genexpr>.   sV        Ay 	$qI~~=QQ     r   c                B    i | ]\  }}|d t           j        |          d S )_)tokentok_name)r   chartokvals      r   
<dictcomp>z2create_valid_python_identifier.<locals>.<dictcomp>6   sA     ' ' 'D& 	+%.(+++' ' 'r   r$   _QUESTIONMARK__EXCLAMATIONMARK__DOLLARSIGN_
_EUROSIGN__DEGREESIGN__SINGLEQUOTE__DOUBLEQUOTE__HASH_
_BACKTICK_)
 ?!$u   €   °'"#`c                <    g | ]}                     ||          S  )get)r   r'   special_characters_replacementss     r   
<listcomp>z2create_valid_python_identifier.<locals>.<listcomp>I   s*    UUU377dCCUUUr   BACKTICK_QUOTED_STRING_zCould not convert 'z' to a valid Python identifier.)isidentifierr   r   tokenizeEXACT_TOKEN_TYPESitemsupdateSyntaxError)r   genr?   s     @r   create_valid_python_identifierrI      sT     9T??    C 77      D' '%7==??' ' '# $**!$   	
 	
   77UUUUPTUUUVVD+T++D WUUUUVVVKr   toktuple[int, str]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_STRINGrC   NAMErI   )rJ   toknumr(   s      r   clean_backtick_quoted_toksrP   R   s9    ( NFF'''}<VDDDD6>r   r   c                    	 t          | t                    r|                     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.
    r;   z``   )
isinstancer   r!   tokenize_stringnextrI   rG   )r   	tokenizedr(   s      r   clean_column_namerW   l   s    &*4T3*?*?It||C&&&T#KKKK00	i#-f555   s   A#A& &A54A5c                      e Zd ZdZdZdZdZdS )
ParseStater   rR         N)__name__
__module____qualname__DEFAULTIN_BACKTICKIN_SINGLE_QUOTEIN_DOUBLE_QUOTEr=   r   r   rY   rY      s"        GKOOOOr   rY   slist[tuple[bool, str]]c                   g }g }d}t           j        }|t          |           k     r| |         }|xdk    r |t           j        k    rA|r*|                    dd                    |          f           |g}|dz  }t           j        }v|t           j        k    r|t          |           dz
  k    r| |dz            nd}|dk    r0|                    |           |                    |           |dz  }|                    |           |                    dd                    |          f           g }|dz  }t           j        }3nxd	k    rJ |t           j        k    rt           j        }ny|t           j        k    r| |dz
           d
k    rt           j        }nMdk    rH|t           j        k    rt           j        }n+|t           j        k    r| |dz
           d
k    rt           j        }|                    |           |dz  }|t          |           k     |r*|                    dd                    |          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   r;   Fr   rR   NrZ   Tr8   r   r9   )rY   r_   lenappendr   r`   ra   rb   )rc   
substringssubstriparse_stater'   	next_chars          r   _split_by_backtickrm      sO   $ JF	A$K
c!ff**t*"444 D"))5"''&//*BCCC"VFFA","8K J$:::-.#a&&1*__!a%4I C''d+++i000Q  d+++"))4*ABBB!#Q&0&8 # ;$ *"444","<KK!Z%???aAhRVFVFV","4K*"444","<KK!Z%???aAhRVFVFV","4Kd	Qa c!ff**d  45"''&//2333r   sourceIterator[tuple[int, str]]c              #     K   d                     d t          |           D                       } t          |           j        }t	          j        |          }|D ]\  }}}}}||fV  dS )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  R   K   | ]"\  }}|rt          |d d                   n|V  #dS )rR   N)rI   )r   is_backtick_quoted	substrings      r   r   z"tokenize_string.<locals>.<genexpr>   s[         *	 "*9QrT?;;;	     r   N)r   rm   r   readlinerC   generate_tokens)rn   line_readertoken_generatorrO   r(   r$   s         r   rT   rT      s        WW   .@-G-G    F 6""+K.{;;O#2  1afn r   )r   r   r   r   )rJ   rK   r   rK   )r   r   r   r   )rc   r   r   rd   )rn   r   r   ro   )__doc__
__future__r   enumr   ior   keywordr   r%   rC   typingr   collections.abcr   r	   rM   rI   rP   rW   rY   rm   rT   r=   r   r   <module>r      sS    # " " " " "                                          7 7 7 7t   4   <       K K K K\     r   