a
    ø£BeÛ  ã                   @   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ee
e  dœdd„Zddd„Zdeedœ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 n t jy0   Y dS 0 dS )z'wrap generate_tkens to catch EOF errorsN)ÚtokenizeÚgenerate_tokensÚ
TokenError)Úreadliner	   © r   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/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 n’ tjyä ‰  zxt‡ fdd„|D ƒƒrÎ|rª|d jd |d jd f}|}|d j	}nd }}d}t 
tjd|||¡V  n‚ W Y d ‰ Š n
d ‰ Š 0 0 d S )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>,   ó    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   sR   d}|   d¡}|D ]2}|t|ƒ }| d¡s4|d7 }||kr@ qJ|}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r”|j d¡}t||d ƒD ](\}}||vrj||d  t	|ƒ ||< qj|| }|	dkr¬|d n|}||	 |krÂ �qº|j
tjk�r0t|jƒ�s0|�r"|�r"|d j
tjk�r"|d jdk�r"d|d |jf |d< n| |j¡ nh|j
tjk�r˜|jdk�rZ|�rZ| d¡ |jdk�r||�r|| |d ¡ n|jd	k�r˜|�r˜| d¡ | |¡ ||
 | |kr" �qºq"|�rÈ|d S |�rÖ|d S 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   N)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\   sH    

.


r@   )N)r   )r   )Ú__doc__Úcollectionsr   Úior   Úkeywordr   r   r   Útypingr   r   r   r   Ústrr%   r/   Úintr@   r   r   r   r   Ú<module>   s    ÿ
ÿ
"