
    Mh                        d Z ddlmZ ddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZmZ  G d d	e          ZddZ	 dddZdddZdddZdS )zToken-related utilities    )annotationsN)StringIO)	iskeyword)	TokenInfo)	Generator
NamedTuplec                  B    e Zd ZU ded<   ded<   ded<   ded<   ded<   dS )	TokeninttokenstrtextstartendlineN)__name__
__module____qualname____annotations__     W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/IPython/utils/tokenutil.pyr
   r
      s=         JJJIIIJJJHHHIIIIIr   r
   return Generator[TokenInfo, None, None]c              #  l   K   	 t          j        |           E 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)readlines    r   r   r      sY      +H55555555555   s     33extra_errors_to_catchlist[str] | Nonec              #    	K   g d}|t          |t                    sJ ||pg z   }g }	 t          j        |           D ]}|                    |           |V  d S # t          j        $ r	t          	fd|D                       r^|r6|d         j        d         |d         j        d         f}|}|d         j	        }ndx}}d}t          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>1   s.      AAu#AAAAAAr   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_errorsr4       sa       
 !(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$$H/UCFFFFFF  GFFFFFs   0A C1,A:C,,C1cellr   
cursor_posr   tuple[str, int]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)r5   r6   offsetlinesr   next_offsets         r   line_at_cursorr@   ?   s    " FOOD!!E  s4yy(}}T"" 	 1K##E<r   c                   g }g }d}d}ddi}d}d}d t          t          |           j                  D             }	|	sdS t          dg|	z   t	          j        |	dgz                       D ]\  }
\  }}|j        \  }}|j        \  }}|dz   |vrU|j        	                    d          }t          ||dz             D ]'\  }}||vr||dz
           t          |          z   ||<   (d}||         }||z   |k    r nJ|j        t          j        k    ot          |j                   x}r|r8|
r6|
j        t          j        k    r!|
j        dk    r|d	         d|j        |d	<   n|                    |j                   |7|j        t          j        k    r"|j        d
k    r|                    d	           d}|s|r|d	         nd}n`|j        t          j        k    rK|j        dk    r|r|                    |d	                    n"|j        dk    r|r|                    d	          }||         |z   |k    r||         |z   |k    } n|r
|r|d	         S |r|S |r|d	         S |r|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
    Nr*   r   Fc                     g | ]}t          | S r   )r
   )r%   tups     r   
<listcomp>z#token_at_cursor.<locals>.<listcomp>x   s)     " " "s" " "r   r+   T.r)   =())r   r   r   zip	itertoolspairwiser   r   r   r:   	enumerater;   r   r   NAMEr   r   OPr.   pop)r5   r6   names
call_namesclosing_call_namemost_recent_outer_nameoffsetsintersects_with_cursorcur_token_is_namer3   prev_toktoknext_tok
start_line	start_colend_lineend_colr>   linenor   r=   s                        r   token_at_cursorr_   a   s0    EJ$()-!fG"" ".x~~/FGG" " "F  r%(	+FdVO<<& & 5 5!/3 !$	
IG'a<w&&H''--E )%a @ @ F F((&-fqj&9CII&EGFO $I
** E #	X] : V9SXCVCV?VV 	7'' Nhk11MS((',Ryyy#((;b		SX&&&$Nhk11MS(( 		"$)! F6;)Er&Y(+%%x35!!%),,,,SZ$.NN2$6$6!8w&33 &-Z%89%D
%R"E	 4  3 Ry 
   	 "~	 	 &%	 Ry rr   )r   r   )N)r    r!   )r   )r5   r   r6   r   r   r7   )r5   r   r6   r   r   r   )__doc__
__future__r   rJ   r   ior   keywordr   r   typingr   r   r
   r   r4   r@   r_   r   r   r   <module>re      s"     # " " " " "                        ( ( ( ( ( ( ( (    J       9=    >    Df f f f f f fr   