o
    Zh                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddl	m
Z
mZ edg d	Zd
d Z	ddee
e  fddZdddZddedefddZdS )zToken-related utilities    )
namedtuple)StringIO)	iskeywordN)	TokenInfo)ListOptional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_tkens to catch EOF errorsN)tokenizegenerate_tokens
TokenError)readliner	    r   N/var/www/html/lang_env/lib/python3.10/site-packages/IPython/utils/tokenutil.pyr      s   r   extra_errors_to_catchc           	   
   #   s    g d}|d u st |tsJ ||pg  }g }zt| D ]
}|| |V  qW d S  tjyu   z=t fdd|D ri|rW|d jd |d jd f}|}|d j	}nd }}d}t
tjd|||V  n W Y d   d S d   ww )N)zunterminated string literalzinvalid non-printable characterz!after line continuation characterc                 3   s    | ]
}| j d  v V  qdS )r   N)args).0errorexcr   r   	<genexpr>,   s    z/generate_tokens_catch_errors.<locals>.<genexpr>r   )   r    )
isinstancelistr   r   appendr   anyr   r   r   r   
ERRORTOKEN)	r   r   Zdefault_errors_to_catchZerrors_to_catchtokensr	   r   r   r   r   r   r   generate_tokens_catch_errors   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
r   r   )
splitlineslenendswith)cell
cursor_posoffsetlinesr   next_offsetr   r   r   line_at_cursor:   s   

r.   r)   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 )av  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 : str
        A block of Python code
    cursor_pos : int
        The location of the cursor in the block where the token should be found
    r   r   Tr   .z%s.%s=()r   )r   r   r   r   r   r   r   r&   	enumerater'   r	   r   NAMEr   r
   OPr    pop)r)   r*   namesr#   Z
call_namesoffsetstuptok
start_lineZ	start_colend_lineZend_colr,   linenor   r+   boundaryr   r   r   token_at_cursor\   sL   

&


r?   )N)r   )__doc__collectionsr   ior   keywordr   r   r   typingr   r   r   r   strr$   r.   intr?   r   r   r   r   <module>   s    


"