
    &e                         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
 Z	 ddee
e                  fdZddZddedefdZdS )zToken-related utilities    )
namedtuple)StringIO)	iskeywordN)	TokenInfo)ListOptionalToken)tokentextstartendlinec              #   n   K   	 t          j        |           D ]}|V  dS # t           j        $ r Y dS w xY w)z'wrap generate_tkens to catch EOF errorsN)tokenizegenerate_tokens
TokenError)readliner
   s     7lib/python3.11/site-packages/IPython/utils/tokenutil.pyr   r      s_      -h77 	 	EKKKK	 	   s   ! 44extra_errors_to_catchc              #     	K   g d}|t          |t                    sJ ||pg z   }g }	 t          j        |           D ]}|                    |           |V  d S # t          j        $ r	t          	fd|D                       rc|r6|d         j        d         |d         j        d         f}|}|d         j	        }ndx}}d}t          j
        t          j        d|||          V  n Y d 		d S d 		ww xY w)N)zunterminated string literalzinvalid non-printable characterz!after line continuation characterc              3   6   K   | ]}|j         d          v V  dS )r   N)args).0errorexcs     r   	<genexpr>z/generate_tokens_catch_errors.<locals>.<genexpr>,   s.      AAu#AAAAAA    r   )   r    )
isinstancelistr   r   appendr   anyr   r   r   r   
ERRORTOKEN)
r   r   default_errors_to_catcherrors_to_catchtokensr
   r   r   r   r   s
            @r   generate_tokens_catch_errorsr)      sd       
 !(J7Ld,S,S(((-1F1L"MO F-h77 	 	EMM%   KKKK	 	    AAAAAAAAA 	 r
(+VBZ^A->>bz$$$X%8"eS$OOOOOO  POOOOOs   0A C6,A?C11C6c                     d}|                      d          }|D ]8}|t          |          z   }|                    d          s|dz  }||k    r n|}9d}||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_offsets         r   line_at_cursorr4   :   s    " FOOD!!E  s4yy(}}T"" 	 1K##E&>r   r/   r0   c                 &   g }g }g }ddi}t          t          |           j                  D ]}t          | }|j        \  }}	|j        \  }
}|
dz   |vrU|j                            d          }t          ||dz             D ]'\  }}||vr||dz
           t          |          z   ||<   (||         }|	dk    r|dz   n|}||	z   |k    r n4|j
        t          j        k    rut          |j                  sa|rD|rB|d         j
        t          j        k    r'|d         j        dk    r|d         d|j        |d<   n|                    |j                   n|j
        t          j        k    rm|j        dk    r|r|                    d           |j        dk    r|r|                    |d                    n"|j        dk    r|r|                    d           |                    |           ||
         |z   |k    r n|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   .=()r    )r   r   r   r	   r   r   r   r,   	enumerater-   r
   r   NAMEr   r   OPr#   pop)r/   r0   namesr(   
call_namesoffsetstuptok
start_line	start_colend_lineend_colr2   linenor   r1   boundarys                    r   token_at_cursorrI   \   s]    EFJ!fGx~~677 ) )Sk !$	
IG'a<w&&H''--E )%a @ @ D D((&-fQh&7#d))&CGFO$%.!^^:>>I)) E9%%i.A.A% ' 'F2J$4$C$Cr
[^H^H^',Ryyy#((;b		SX&&&&Y(+%%x35		"x35!!%),,,,SZr"""c8w&33E 4  "~	 Ryrr   )N)r   )__doc__collectionsr   ior   keywordr   r   r   typingr   r   r	   r   strr)   r4   intrI    r   r   <module>rR      s    
 # " " " " "                    ! ! ! ! ! ! ! ! 	
7EEEFF   <@ %-d3i%8   >   DD D# D3 D D D D D Dr   