a
    ÷£Be³I  ã                   @   sL  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZmZmZ d dlZd dlmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZ er¼ddlmZmZ G d	d
„ d
e  e j!e"¡ƒZ#G dd„ de#e"ƒZ$G dd„ de#e"ƒZ%dZ&ej'dd… dk�r>e&d7 Z&ej'dd… dk�r>e&d7 Z&e&d7 Z&ddd„Z(dS )é    N)ÚModule)ÚIterableÚIteratorÚListÚOptionalÚTupleÚAnyÚcastÚTYPE_CHECKING)Úxrangeé   )ÚLineNumbers)	ÚTokenÚmatch_tokenÚis_non_coding_tokenÚpatched_generate_tokensÚ	last_stmtÚannotate_fstring_nodesÚgenerate_tokensÚ	is_moduleÚis_stmt)ÚAstNodeÚ	TokenInfoc                   @   s6   e Zd Zdd„ Zejdd„ ƒZddd„Zddd	„Zd
S )ÚASTTextBasec                 C   s$   || _ t |¡}|| _t|ƒ| _d S ©N)Ú	_filenameÚsixÚensure_textÚ_textr   Ú_line_numbers)ÚselfÚsource_textÚfilename© r#   úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/asttokens/asttokens.pyÚ__init__%   s    
zASTTextBase.__init__c                 C   s   t ‚dS )áÎ  
    Returns two ``(lineno, col_offset)`` tuples for the start and end of the given node.
    If the positions can't be determined, or the nodes don't correspond to any particular text,
    returns ``(1, 0)`` for both.

    ``padded`` corresponds to the ``padded`` argument to ``ast.get_source_segment()``.
    This means that if ``padded`` is True, the start position will be adjusted to include
    leading whitespace if ``node`` is a multiline statement.
    N)ÚNotImplementedError©r    ÚnodeÚpaddedr#   r#   r$   Úget_text_positions4   s    zASTTextBase.get_text_positionsTc                 C   s(   |   ||¡\}}| jj|Ž | jj|Ž fS )a  
    Returns the (startpos, endpos) positions in source text corresponding to the given node.
    Returns (0, 0) for nodes (like `Load`) that don't correspond to any particular text.

    See ``get_text_positions()`` for details on the ``padded`` argument.
    )r+   r   Úline_to_offset©r    r)   r*   ÚstartÚendr#   r#   r$   Úget_text_rangeB   s    

þzASTTextBase.get_text_rangec                 C   s   |   ||¡\}}| j||… S )zÚ
    Returns the text corresponding to the given node.
    Returns '' for nodes (like `Load`) that don't correspond to any particular text.

    See ``get_text_positions()`` for details on the ``padded`` argument.
    )r0   r   r-   r#   r#   r$   Úget_textP   s    zASTTextBase.get_textN)T)T)	Ú__name__Ú
__module__Ú__qualname__r%   ÚabcÚabstractmethodr+   r0   r1   r#   r#   r#   r$   r   $   s
   

r   c                       s´   e Zd ZdZd%‡ fdd„	Zdd„ Zd	d
„ Zedd„ ƒZedd„ ƒZ	edd„ ƒZ
edd„ ƒZdd„ Zdd„ Zdd„ Zd&dd„Zd'dd„Zd(dd„Zd)dd „Zd*d!d"„Zd#d$„ Z‡  ZS )+Ú	ASTTokensaš  
  ASTTokens maintains the text of Python code in several forms: as a string, as line numbers, and
  as tokens, and is used to mark and access token and position information.

  ``source_text`` must be a unicode or UTF8-encoded string. If you pass in UTF8 bytes, remember
  that all offsets you'll get are to the unicode text, which is available as the ``.text``
  property.

  If ``parse`` is set, the ``source_text`` will be parsed with ``ast.parse()``, and the resulting
  tree marked with token info and made available as the ``.tree`` property.

  If ``tree`` is given, it will be marked and made available as the ``.tree`` property. In
  addition to the trees produced by the ``ast`` module, ASTTokens will also mark trees produced
  using ``astroid`` library <https://www.astroid.org>.

  If only ``source_text`` is given, you may use ``.mark_tokens(tree)`` to mark the nodes of an AST
  tree created separately.
  FNú	<unknown>c                    sr   t t| ƒ ||¡ |r"t ||¡n|| _|d u r:t| jƒ}t|  	|¡ƒ| _
dd„ | j
D ƒ| _| jrn|  | j¡ d S )Nc                 S   s   g | ]
}|j ‘qS r#   )Ústartpos)Ú.0Útokr#   r#   r$   Ú
<listcomp>   ó    z&ASTTokens.__init__.<locals>.<listcomp>)Úsuperr7   r%   ÚastÚparseÚ_treer   r   ÚlistÚ_translate_tokensÚ_tokensÚ_token_offsetsÚmark_tokens)r    r!   r@   Útreer"   Útokens©Ú	__class__r#   r$   r%   p   s    
zASTTokens.__init__c                 C   s   ddl m} || ƒ |¡ dS )ap  
    Given the root of the AST or Astroid tree produced from source_text, visits all nodes marking
    them with token and position information by adding ``.first_token`` and
    ``.last_token``attributes. This is done automatically in the constructor when ``parse`` or
    ``tree`` arguments are set, but may be used manually with a separate AST or Astroid tree.
    r   )Ú
MarkTokensN)rF   rK   Z
visit_tree)r    Z	root_noderK   r#   r#   r$   rF   „   s    	zASTTokens.mark_tokensc           	      c   sd   t t|ƒƒD ]R\}}|\}}}}}t||||||| j |d |d ¡| j |d |d ¡ƒV  qdS )zS
    Translates the given standard library tokens into our own representation.
    r   r   N)Ú	enumerater   r   r   r,   )	r    Zoriginal_tokensÚindexr;   Útok_typeÚtok_strr.   r/   Úliner#   r#   r$   rC   �   s    þzASTTokens._translate_tokensc                 C   s   | j S )z,The source code passed into the constructor.)r   ©r    r#   r#   r$   Útext›   s    zASTTokens.textc                 C   s   | j S )zIThe list of tokens corresponding to the source code from the constructor.)rD   rQ   r#   r#   r$   rH   ¡   s    zASTTokens.tokensc                 C   s   | j S )zTThe root of the AST tree passed into the constructor or parsed from the source code.)rA   rQ   r#   r#   r$   rG   §   s    zASTTokens.treec                 C   s   | j S )zThe filename that was parsed)r   rQ   r#   r#   r$   r"   ­   s    zASTTokens.filenamec                 C   s   | j t | j|¡d  S )z£
    Returns the token containing the given character offset (0-based position in source text),
    or the preceeding token if the position is between tokens.
    r   )rD   ÚbisectrE   )r    Úoffsetr#   r#   r$   Úget_token_from_offset³   s    zASTTokens.get_token_from_offsetc                 C   s   |   | j ||¡¡S )zŽ
    Returns the token containing the given (lineno, col_offset) position, or the preceeding token
    if the position is between tokens.
    )rU   r   r,   ©r    ÚlinenoÚ
col_offsetr#   r#   r$   Ú	get_token»   s    	zASTTokens.get_tokenc                 C   s   |   || j ||¡¡S )zd
    Same as get_token(), but interprets col_offset as a UTF8 offset, which is what `ast` uses.
    )rY   r   Úfrom_utf8_colrV   r#   r#   r$   Úget_token_from_utf8Æ   s    zASTTokens.get_token_from_utf8c                 C   s2   |j d }|s(t| j| jƒr(|d7 }q| j| S )z 
    Returns the next token after the given one. If include_extra is True, includes non-coding
    tokens from the tokenize module, such as NL and COMMENT.
    r   ©rM   r   rD   Útype©r    r;   Úinclude_extraÚir#   r#   r$   Ú
next_tokenÍ   s
    

zASTTokens.next_tokenc                 C   s2   |j d }|s(t| j| jƒr(|d8 }q| j| S )z¥
    Returns the previous token before the given one. If include_extra is True, includes non-coding
    tokens from the tokenize module, such as NL and COMMENT.
    r   r\   r^   r#   r#   r$   Ú
prev_tokenÙ   s
    

zASTTokens.prev_tokenc                 C   s>   |}|r| j n| j}t|||ƒs:t |j¡s:||dd�}q|S )z÷
    Looks for the first token, starting at start_token, that matches tok_type and, if given, the
    token string. Searches backwards if reverse is True. Returns ENDMARKER token if not found (you
    can check it with `token.ISEOF(t.type)`).
    T©r_   )rb   ra   r   ÚtokenÚISEOFr]   )r    Zstart_tokenrN   rO   ÚreverseÚtÚadvancer#   r#   r$   Ú
find_tokenå   s
    zASTTokens.find_tokenc                 c   s<   t |j|jd ƒD ]$}|s*t| j| jƒs| j| V  qdS )z²
    Yields all tokens in order from first_token through and including last_token. If
    include_extra is True, includes non-coding tokens such as tokenize.NL and .COMMENT.
    r   N)r   rM   r   rD   r]   )r    Úfirst_tokenÚ
last_tokenr_   r`   r#   r#   r$   Útoken_rangeò   s    
zASTTokens.token_rangec                 C   s   | j |j|j|d�S )z�
    Yields all tokens making up the given node. If include_extra is True, includes non-coding
    tokens such as tokenize.NL and .COMMENT.
    rc   )rl   rj   rk   )r    r)   r_   r#   r#   r$   Ú
get_tokens   s    zASTTokens.get_tokensc                 C   sN   t |dƒsdS |jj}|jj}|rFtdd„ |  |¡D ƒƒrF|d df}||fS )r&   rj   ©©r   r   ro   c                 s   s   | ]}t |tjƒV  qd S r   )r   rd   ÚNEWLINE)r:   rg   r#   r#   r$   Ú	<genexpr>  r=   z/ASTTokens.get_text_positions.<locals>.<genexpr>r   )Úhasattrrj   r.   rk   r/   Úanyrm   r-   r#   r#   r$   r+     s    
zASTTokens.get_text_positions)FNr8   N)F)F)NF)F)F)r2   r3   r4   Ú__doc__r%   rF   rC   ÚpropertyrR   rH   rG   r"   rU   rY   r[   ra   rb   ri   rl   rm   r+   Ú__classcell__r#   r#   rI   r$   r7   \   s,   






 ý

r7   c                       sJ   e Zd ZdZd‡ fdd„	Zedd„ ƒZedd	„ ƒZd
d„ Zdd„ Z	‡  Z
S )ÚASTTextaF  
  Supports the same ``get_text*`` methods as ``ASTTokens``,
  but uses the AST to determine the text positions instead of tokens.
  This is faster than ``ASTTokens`` as it requires less setup work.

  It also (sometimes) supports nodes inside f-strings, which ``ASTTokens`` doesn't.

  Some node types and/or Python versions are not supported.
  In these cases the ``get_text*`` methods will fall back to using ``ASTTokens``
  which incurs the usual setup cost the first time.
  If you want to avoid this, check ``supports_tokenless(node)`` before calling ``get_text*`` methods.
  Nr8   c                    s6   t t| ƒ ||¡ || _| jd ur,t| jƒ d | _d S r   )r>   rw   r%   rA   r   Ú
_asttokens)r    r!   rG   r"   rI   r#   r$   r%   ,  s
    

zASTText.__init__c                 C   s,   | j d u r&t | j| j¡| _ t| j ƒ | j S r   )rA   r?   r@   r   r   r   rQ   r#   r#   r$   rG   9  s    

zASTText.treec                 C   s&   | j d u r t| j| j| jd�| _ | j S )N)rG   r"   )rx   r7   r   rG   r   rQ   r#   r#   r$   Ú	asttokensA  s    
ýzASTText.asttokensc                 C   s  t jdd… dk rtdƒ‚t|ƒr8d| j t| jƒ¡fS t|ddƒdu rLdS |sTJ ‚t|dg ƒ}|s|t|d	dƒ}t|d
g ƒ}|rŠ|d }n|}|j	}t
|ƒ}|rÎ||j	ksÈ||jkrÎt|ddƒrÎt|ƒrÎd}n| j ||j¡}||f}	tt|jƒ}
tt|jƒ}| j |
|¡}|
|f}|	|fS )zF
    Version of ``get_text_positions()`` that doesn't use tokens.
    Né   ©é   é   zPThis method should only be called internally after checking supports_tokenless()ro   rW   rn   Údecorator_listÚ
decoratorsÚnodesr   Zdoc_node)ÚsysÚversion_infoÚAssertionErrorr   r   Zoffset_to_lineÚlenr   ÚgetattrrW   r   Ú
end_linenor   rZ   rX   r	   ÚintÚend_col_offset)r    r)   r*   r   Zdecorators_nodeZ
start_nodeZstart_linenoZend_nodeZstart_col_offsetr.   r†   rˆ   r/   r#   r#   r$   Ú_get_text_positions_tokenlessL  sB    
ÿù
ø	÷z%ASTText._get_text_positions_tokenlessc                 C   s2   t |ddƒrdS t|ƒr$|  ||¡S | j ||¡S )r&   Z_broken_positionsNrn   )r…   Úsupports_tokenlessr‰   ry   r+   r(   r#   r#   r$   r+   ’  s
    zASTText.get_text_positions)Nr8   )r2   r3   r4   rt   r%   ru   rG   ry   r‰   r+   rv   r#   r#   rI   r$   rw     s   


Frw   r#   rz   r{   )Ú	argumentsÚ	ArgumentsÚwithitem)ÚargÚStarred)ÚSliceÚExtSliceÚIndexÚkeywordc                 C   sd   t | ƒjtvobt| tjƒ o@| duo@t | ƒjdko@t | jƒjdv  obtjdd… dkobdtj	 
¡ vS )a“  
  Returns True if the Python version and the node (if given) are supported by
  the ``get_text*`` methods of ``ASTText`` without falling back to ``ASTTokens``.
  See ``ASTText`` for why this matters.

  The following cases are not supported:

    - Python 3.7 and earlier
    - PyPy
    - ``ast.arguments`` / ``astroid.Arguments``
    - ``ast.withitem``
    - ``astroid.Comprehension``
    - ``astroid.AssignName`` inside ``astroid.Arguments`` or ``astroid.ExceptHandler``
    - The following nodes in Python 3.8 only:
      - ``ast.arg``
      - ``ast.Starred``
      - ``ast.Slice``
      - ``ast.ExtSlice``
      - ``ast.Index``
      - ``ast.keyword``
  NZ
AssignName)rŒ   ÚExceptHandlerrz   r{   Úpypy)r]   r2   Ú_unsupported_tokenless_typesÚ
isinstancer?   ÚASTÚparentr�   r‚   ÚversionÚlower)r)   r#   r#   r$   rŠ   ³  s    ûÿ
öôrŠ   )N))r5   r?   rS   r�   rd   r   Útypingr   r   r   r   r   r   r	   r
   r   Z	six.movesr   Úline_numbersr   Úutilr   r   r   r   r   r   r   r   r   r   r   Úwith_metaclassÚABCMetaÚobjectr   r7   rw   r–   r‚   rŠ   r#   r#   r#   r$   Ú<module>   s0   (,8 D 
