o
    ˷eX                     @   s^   d Z ddlmZ ddlmZ ddlmZ ddlZedg dZdd	 Z	dd
dZ
dddZdS )zToken-related utilities    )
namedtuple)StringIO)	iskeywordNToken)tokentextstartendlinec                 c   s6    zt | D ]}|V  qW dS  t jy   Y dS w )z(wrap generate_tokens to catch EOF errorsN)tokenizegenerate_tokens
TokenError)readliner    r   N/var/www/ideatree/venv/lib/python3.10/site-packages/IPython/utils/tokenutil.pyr      s   r   c                 C   sX   d}|  d}|D ]}|t| }|ds|d7 }||kr# ||fS |}q	d}||fS )a  Return the line in a cell at a given cursor position

    Used for calling line-based APIs that don't support multi-line input, yet.

    Parameters
    ----------
    cell : str
        multiline block of text
    cursor_pos : integer
        the cursor position

    Returns
    -------
    (line, offset): (string, integer)
        The line with the current cursor, and the character offset of the start of the line.
    r   T
    )
splitlineslenendswith)cell
cursor_posoffsetlinesr
   next_offsetr   r   r   line_at_cursor   s   

r   c                 C   s  g }g }g }ddi}t t| jD ]}t| }|j\}}	|j\}
}|
d |vrI|jd}t||d D ]\}}||vrH||d  t	| ||< q4|| }|	dkrU|d n|}||	 |kr_ nn|j
tjkrt|js|r|r|d j
tjkr|d jdkrd|d |jf |d< n4||j n-|j
tjkr|jdkr|r|d |jdkr|r||d  n|jd	kr|r|d || ||
 | |kr nq|r|d S |r|d S d
S )az  Get the token at a given cursor

    Used for introspection.

    Function calls are prioritized, so the token for the callable will be returned
    if the cursor is anywhere inside the call.

    Parameters
    ----------
    cell : unicode
        A block of Python code
    cursor_pos : int
        The location of the cursor in the block where the token should be found
    r   r   T.z%s.%s=()r   )r   r   r   r   r   r	   r
   r   	enumerater   r   r   NAMEr   r   OPappendpop)r   r   namestokens
call_namesoffsetstuptok
start_line	start_colend_lineend_colr   linenor
   r   boundaryr   r   r   token_at_cursor9   sL   

&


r3   )r   )__doc__collectionsr   ior   keywordr   r   r   r   r   r3   r   r   r   r   <module>   s    
	!