B
    \}                 @   sd   d Z ddlmZ ddlmZ ddlmZ ddlZedddd	d
dgZdd Z	dddZ
dddZdS )zToken-related utilities    )
namedtuple)StringIO)	iskeywordNTokentokentextstartendlinec             c   s:   yxt | D ]
}|V  qW W n t jk
r4   dS X dS )z(wrap generate_tokens to catch EOF errorsN)tokenizegenerate_tokens
TokenError)readliner    r   6lib/python3.7/site-packages/IPython/utils/tokenutil.pyr      s
    r   c             C   sT   d}|  d}x<|D ]0}|t| }|ds6|d7 }||kr@P |}qW 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
    )
splitlineslenendswith)cell
cursor_posoffsetlinesr
   Znext_offsetr   r   r   line_at_cursor   s    


r   c             C   s  g }g }g }ddi}xt t| jD ]}t| }|j\}}	|j\}
}|
d |kr|jd}x:t||d D ](\}}||krp||d  t	| ||< qpW || }|	dkr|d n|}||	 |krP |j
tjkr4t|js4|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r^|r^|d |jdkr|r||d  n|jd	kr|r|d || ||
 | |kr&P q&W |r|d S |r|d S d
S dS )a  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 : unicode
        A block of Python code
    cursor_pos : int
        The location of the cursor in the block where the token should be found
    r   r   T.z%s.%s=()r   N)r   r   r   r   r   r	   r
   r   	enumerater   r   r   NAMEr   r   OPappendpop)r   r   namestokensZ
call_namesZoffsetstuptokZ
start_lineZ	start_colZend_lineZend_colr   linenor
   r   boundaryr   r   r   token_at_cursor;   sH    

.


r,   )r   )r   )__doc__collectionsr   ior   keywordr   r   r   r   r   r,   r   r   r   r   <module>   s   	
#