
    Xh                        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y)zToken-related utilities    )annotationsN)StringIO)	iskeyword)	TokenInfo)	Generator
NamedTuplec                  @    e Zd ZU ded<   ded<   ded<   ded<   ded<   y)	TokeninttokenstrtextstartendlineN)__name__
__module____qualname____annotations__     K/var/www/html/myenv/lib/python3.12/site-packages/IPython/utils/tokenutil.pyr
   r
      s    J
IJ	H
Ir   r
   c              #  ~   K   	 t        j                  |       E d{    y7 # t         j                  $ r Y yw xY ww)z'wrap generate_tkens to catch EOF errorsN)tokenizegenerate_tokens
TokenError)readlines    r   r   r      s6     ++H555 s(   =$ "$ =$ :=:=c              #    	K   g d}|t        |t              sJ ||xs g z   }g }	 t        j                  |       D ]  }|j	                  |       |  y # 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|||       n Y d 		y d 		ww xY ww)N)zunterminated string literalzinvalid non-printable characterz!after line continuation characterc              3  @   K   | ]  }|j                   d    v   yw)r   N)args).0errorexcs     r   	<genexpr>z/generate_tokens_catch_errors.<locals>.<genexpr>1   s     Au#As   r   )   r    )
isinstancelistr   r   appendr   anyr   r   r   r   
ERRORTOKEN)
r   extra_errors_to_catchdefault_errors_to_catcherrors_to_catchtokensr   r   r   r   r#   s
            @r   generate_tokens_catch_errorsr1       s     
 !(J7Ld,SSS-1F1L"MO F--h7 	EMM% K	  AAAr
((+VBZ^^A->>bz$$H//UCFF  Gs/   $C+/A C+C(+A3C#C+#C((C+c                    d}| j                  d      }|D ]2  }|t        |      z   }|j                  d      s|dz  }||kD  r ||fS |}4 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_offsets         r   line_at_cursorr<   ?   s{    " FOOD!E s4y(}}T" 1K# <  <r   c                   g }g }d}d}ddi}d}d}t        t        |       j                        D 	cg c]
  }	t        |	  }
}	|
syt	        dg|
z   t        j                  |
dgz               D ]  \  }\  }}|j                  \  }}|j                  \  }}|dz   |vrN|j                  j                  d      }t        ||dz         D ]!  \  }}||vs||dz
     t        |      z   ||<   # d}||   }||z   |kD  r ng|j                  t        j                  k(  xr t!        |j"                         x}r|rF|rD|j                  t        j$                  k(  r'|j"                  dk(  r|d   d|j"                  |d<   n|j'                  |j"                         |?|j                  t        j$                  k(  r"|j"                  d	k(  r|j)                  d       d}|so|r|d   nd}ne|j                  t        j$                  k(  rH|j"                  d
k(  r|r|j'                  |d          n"|j"                  dk(  r|r|j)                  d      }||   |z   |kD  s||   |z   |k  } n |r|r|d   S |r|S |r|d   S |r|S |r|d   S yc c}	w )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   Fr'   T.r%   =())r   r   r   r
   zip	itertoolspairwiser   r   r   r4   	enumerater5   r   r   NAMEr   r   OPr*   pop)r7   r8   names
call_namesclosing_call_namemost_recent_outer_nameoffsetsintersects_with_cursorcur_token_is_nametupr0   prev_toktoknext_tok
start_line	start_colend_lineend_colr:   linenor   r9   s                         r   token_at_cursorrY   a   s    EJ$()-!fG".x~/F/FG"s"F " %(	++FdVO<& 5!/3 !$		
IGG'a<w&HH''-E )%a @ F(&-fqj&9CI&EGFOF !$I
*  #		X]] : V9SXXCV?VVVNNhkk1MMS(',Ry#((;b	SXX&$NNhkk1MMS( 		"$)!6;r&YY(++%xx35!!%),SZ$.NN2$6!8w&3 &-Z%89%D
%R"k5n 3Ry 
  	"~	 &%	Ry _"s   J)returnz Generator[TokenInfo, None, None])N)r-   zlist[str] | None)r   )r7   r   r8   r   rZ   ztuple[str, int])r7   r   r8   r   rZ   r   )__doc__
__future__r   rC   r   ior   keywordr   r   typingr   r   r
   r   r1   r<   rY   r   r   r   <module>r`      sN     #      (J  9=%5>Dfr   