o
    û›hÛ  ã                   @   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   úMc:\Users\shubh\Videos\agent\nova\Lib\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r�t|jƒs�|r‰|r‰|d j
tjkr‰|d jdkr‰d|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    ÿ

ÿ
"