a
    ÷£BeáE  ã                   @   sº  d dl Z 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
 d dlmZmZmZmZmZmZmZmZmZmZmZ d dlmZ erôddlmZ G dd	„ d	e
ƒZeeef Zejd  d
krîeeeeeef eeef ef Z nej Z dd„ Z!G dd„ de "dd¡ƒZ#ejdk�r(e j$Z%nG dd„ dƒZ%dOdd„Z&dPdd„Z'ejdk�r`dd„ Z(ndd„ Z(dd„ Z)dd„ Z*dQd d!„Z+d"d#„ ee j,ƒD ƒZ-dRd$d%„Z.d&d#„ ee j,ƒD ƒZ/d'd#„ ee j,ƒD ƒh d(£B Z0d)d*„ Z1d+d,„ Z2d-d.„ Z3d/d0„ Z4d1d2„ Z5d3d4„ Z6d5d6„ Z7e8ƒ Z9d7d8„ Z:dSd9d:„Z;d;d<„ Z<G d=d>„ d>e8ƒZ=ejd  d
k�rRd?d@„ Z>ndAd@„ Z>dBdC„ Z?dDdE„ Z@ejdd
… dFk�r¦d dGlAmBZB eBddH�dIdJ„ ƒZCdKdL„ ZDndMdJ„ ZCdNdL„ ZDdS )Té    N)ÚABCMeta)ÚModuleÚexprÚAST)ÚCallableÚDictÚIterableÚIteratorÚListÚOptionalÚTupleÚUnionÚcastÚAnyÚTYPE_CHECKING)Ú	iteritemsé   )ÚNodeNGc                   @   s   e Zd ZdZdZdZdS )ÚEnhancedASTNr   )Ú__name__Ú
__module__Ú__qualname__Zfirst_tokenZ
last_tokenÚlineno© r   r   úQ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/asttokens/util.pyr   !   s   r   é   c                 C   s   dt j|  t|ƒ d¡f S )zRReturns a human-friendly representation of a token with the given type and string.z%s:%sÚu)ÚtokenÚtok_nameÚreprÚlstrip)Útok_typeÚstringr   r   r   Ú
token_repr/   s    r#   c                   @   s   e Zd ZdZdd„ ZdS )ÚTokenaƒ  
  TokenInfo is an 8-tuple containing the same 5 fields as the tokens produced by the tokenize
  module, and 3 additional ones useful for this module:

  - [0] .type     Token type (see token.py)
  - [1] .string   Token (a string)
  - [2] .start    Starting (row, column) indices of the token (a 2-tuple of ints)
  - [3] .end      Ending (row, column) indices of the token (a 2-tuple of ints)
  - [4] .line     Original line (string)
  - [5] .index    Index of the token in the list of tokens that it belongs to.
  - [6] .startpos Starting character offset into the input text.
  - [7] .endpos   Ending character offset into the input text.
  c                 C   s   t | j| jƒS ©N)r#   Útyper"   ©Úselfr   r   r   Ú__str__D   s    zToken.__str__N)r   r   r   Ú__doc__r)   r   r   r   r   r$   6   s   r$   z0type string start end line index startpos endpos)é   é   c                   @   s   e Zd Zeƒ ZdS )ÚAstConstantN)r   r   r   ÚobjectÚvaluer   r   r   r   r-   L   s   r-   c                 C   s   | j |ko|du p| j|kS )zVReturns true if token is of the given type and, if a string is given, has that string.N)r&   r"   ©r   r!   Ztok_strr   r   r   Úmatch_tokenP   s    r1   c                 C   s>   t | ||ƒs:tdt||ƒt| ƒ| jd | jd d f ƒ‚dS )z³
  Verifies that the given token is of the expected type. If tok_str is given, the token string
  is verified too. If the token doesn't match, raises an informative ValueError.
  z+Expected token %s, got %s on line %s col %sr   r   N)r1   Ú
ValueErrorr#   ÚstrÚstartr0   r   r   r   Úexpect_tokenV   s
    þr5   )r+   é   c                 C   s   | t jt jt jfv S ©zW
    These are considered non-coding tokens, as they don't affect the syntax tree.
    )r   ÚNLÚCOMMENTÚENCODING©Ú
token_typer   r   r   Úis_non_coding_tokend   s    r=   c                 C   s
   | t jkS r7   )r   ÚN_TOKENSr;   r   r   r   r=   k   s    c                 C   s    t  ttg tf t | ¡jƒ¡S )z;
  Generates standard library tokens for the given code.
  )ÚtokenizeÚgenerate_tokensr   r   r3   ÚioÚStringIOÚreadline)Útextr   r   r   r@   s   s    r@   c                 C   s   t | dƒrtS tS )zÑ
  Returns a function which yields all direct children of a AST node,
  skipping children that are singleton nodes.
  The function depends on whether ``node`` is from ``ast`` or from the ``astroid`` module.
  Úget_children)ÚhasattrÚiter_children_astroidÚiter_children_ast©Únoder   r   r   Úiter_children_func~   s    rK   Fc                 C   s   |st | ƒrg S |  ¡ S r%   )Úis_joined_strrE   )rJ   Úinclude_joined_strr   r   r   rG   ˆ   s    rG   c              	   C   s<   h | ]4\}}t |tƒrt|tjtjtjtjtjfƒr|’qS r   )	Ú
isinstancer&   Ú
issubclassÚastÚexpr_contextÚboolopÚoperatorÚunaryopÚcmpop©Ú.0ÚnÚcr   r   r   Ú	<setcomp>�   s   ÿrZ   c                 c   sp   |st | ƒrd S t| tjƒrLt| j| jƒD ]\}}|d ur@|V  |V  q*d S t | ¡D ]}|jt	vrV|V  qVd S r%   )
rL   rN   rP   r   ÚzipÚkeysÚvaluesÚiter_child_nodesÚ	__class__Ú
SINGLETONS)rJ   rM   Úkeyr/   Úchildr   r   r   rH   ”   s    
rH   c                 C   s*   h | ]"\}}t |tƒrt|tjƒr|’qS r   )rN   r&   rO   rP   ÚstmtrV   r   r   r   rZ   ª   s   ÿc                 C   s*   h | ]"\}}t |tƒrt|tjƒr|’qS r   )rN   r&   rO   rP   r   rV   r   r   r   rZ   ¬   s   ÿ>   ZDelNameZDelAttrZ
AssignNameZConstZ
AssignAttrc                 C   s   | j jtv S )z+Returns whether node is an expression node.)r_   r   Úexpr_class_namesrI   r   r   r   Úis_expr²   s    re   c                 C   s   | j jtv S )z)Returns whether node is a statement node.)r_   r   Ústmt_class_namesrI   r   r   r   Úis_stmt·   s    rg   c                 C   s   | j jdkS )z&Returns whether node is a module node.r   ©r_   r   rI   r   r   r   Ú	is_module¼   s    ri   c                 C   s   | j jdkS )zFReturns whether node is a JoinedStr node, used to represent f-strings.Ú	JoinedStrrh   rI   r   r   r   rL   Á   s    rL   c                 C   s   | j jdkS )z2Returns whether node is a starred expression node.ÚStarredrh   rI   r   r   r   Ú
is_starredÉ   s    rl   c                 C   s0   | j jdv p.| j jdko.tttttj| ƒjƒƒS )z?Returns whether node represents a slice, e.g. `1:2` in `x[1:2]`)ÚSliceÚExtSlicer   )	r_   r   ÚanyÚmapÚis_slicer   rP   r   ÚeltsrI   r   r   r   rq   Ï   s    ürq   c                 C   sB   | j jdko@t| tjƒ o@| j| j  u o<| j  u o<d u S   S )Nrm   )r_   r   rN   rP   r   ÚlowerÚupperÚsteprI   r   r   r   Úis_empty_astroid_sliceÝ   s
    ÿ&ýrv   c                 C   s¾   |sdd„ }t | ƒ}tƒ }d}| dtfg}|rº| ¡ \}}}	|	tu r¢||vsPJ ‚| |¡ |||ƒ\}
}| |||f¡ t|ƒ}||ƒD ]}| |||
tf¡ qˆq*|||tt	t
 |	ƒƒ}q*|S )a5  
  Scans the tree under the node depth-first using an explicit stack. It avoids implicit recursion
  via the function call stack to avoid hitting 'maximum recursion depth exceeded' error.

  It calls ``previsit()`` and ``postvisit()`` as follows:

  * ``previsit(node, par_value)`` - should return ``(par_value, value)``
        ``par_value`` is as returned from ``previsit()`` of the parent.

  * ``postvisit(node, par_value, value)`` - should return ``value``
        ``par_value`` is as returned from ``previsit()`` of the parent, and ``value`` is as
        returned from ``previsit()`` of this node itself. The return ``value`` is ignored except
        the one for the root node, which is returned from the overall ``visit_tree()`` call.

  For the initial node, ``par_value`` is None. ``postvisit`` may be None.
  c                 S   s   d S r%   r   )rJ   Úpvaluer/   r   r   r   Ú<lambda>ü   ó    zvisit_tree.<locals>.<lambda>N)rK   ÚsetÚ	_PREVISITÚpopÚaddÚappendÚlenÚinsertr   r   r$   )rJ   ZprevisitZ	postvisitÚiter_childrenÚdoneÚretÚstackÚcurrentZ	par_valuer/   rw   Z
post_valueÚinsrX   r   r   r   Ú
visit_treeé   s$    
r‡   c                 c   sf   t | ƒ}tƒ }| g}|rb| ¡ }||vs,J ‚| |¡ |V  t|ƒ}|||ƒD ]}| ||¡ qNqdS )a1  
  Recursively yield all descendant nodes in the tree starting at ``node`` (including ``node``
  itself), using depth-first pre-order traversal (yieling parents before their children).

  This is similar to ``ast.walk()``, but with a different order, and it works for both ``ast`` and
  ``astroid`` trees. Also, as ``iter_children()``, it skips singleton nodes generated by ``ast``.

  By default, ``JoinedStr`` (f-string) nodes and their contents are skipped
  because they previously couldn't be handled. Set ``include_joined_str`` to True to include them.
  N)rK   rz   r|   r}   r   r€   )rJ   rM   r�   r‚   r„   r…   r†   rY   r   r   r   Úwalk  s    
rˆ   c                 C   sX   d}g }t |ƒD ]*\}}}| | ||… ¡ | |¡ |}q| | |d… ¡ d |¡S )ah  
  Replaces multiple slices of text with new values. This is a convenience method for making code
  modifications of ranges e.g. as identified by ``ASTTokens.get_text_range(node)``. Replacements is
  an iterable of ``(start, end, new_text)`` tuples.

  For example, ``replace("this is a test", [(0, 4, "X"), (8, 9, "THE")])`` produces
  ``"X is THE test"``.
  r   NÚ )Úsortedr~   Újoin)rD   ZreplacementsÚpÚpartsr4   ÚendÚnew_textr   r   r   Úreplace1  s    

r�   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚNodeMethodsz[
  Helper to get `visit_{node_type}` methods given a node's class and cache the results.
  c                 C   s
   i | _ d S r%   )Ú_cacher'   r   r   r   Ú__init__I  s    zNodeMethods.__init__c                 C   s:   | j  |¡}|s6d|j ¡  }t|||jƒ}|| j |< |S )z£
    Using the lowercase name of the class as node_type, returns `obj.visit_{node_type}`,
    or `obj.visit_default` if the type-specific method is not found.
    Úvisit_)r’   Úgetr   rs   ÚgetattrZvisit_default)r(   ÚobjÚclsÚmethodÚnamer   r   r   r•   M  s    
zNodeMethods.getN)r   r   r   r*   r“   r•   r   r   r   r   r‘   E  s   r‘   c                 C   s   t | ƒS r%   )Úiter)Úoriginal_tokensr   r   r   Úpatched_generate_tokens^  s    r�   c                 c   sz   g }| D ]X}|j tjtjtjfv rB|r6|d j|jkrB| |¡ qt|ƒD ]
}|V  qJg }|V  qt|ƒD ]
}|V  qjdS )a¬  
    Fixes tokens yielded by `tokenize.generate_tokens` to handle more non-ASCII characters in identifiers.
    Workaround for https://github.com/python/cpython/issues/68382.
    Should only be used when tokenizing a string that is known to be valid syntax,
    because it assumes that error tokens are not actually errors.
    Combines groups of consecutive NAME, NUMBER, and/or ERRORTOKEN tokens into a single NAME token.
    éÿÿÿÿN)	r&   r?   ÚNAMEÚ
ERRORTOKENÚNUMBERrŽ   r4   r~   Úcombine_tokens)rœ   ÚgroupÚtokZcombined_tokenr   r   r   r�   b  s    	ÿýýc                 C   sf   t dd„ | D ƒƒr(tdd„ | D ƒƒdkr,| S tjtjd dd„ | D ƒ¡| d j| d	 j| d jd
�gS )Nc                 s   s   | ]}|j tjkV  qd S r%   )r&   r?   r    ©rW   r¤   r   r   r   Ú	<genexpr>}  ry   z!combine_tokens.<locals>.<genexpr>c                 S   s   h | ]
}|j ’qS r   )Úliner¥   r   r   r   rZ   }  ry   z!combine_tokens.<locals>.<setcomp>r   r‰   c                 s   s   | ]}|j V  qd S r%   )r"   )rW   Útr   r   r   r¦   ‚  ry   r   rž   )r&   r"   r4   rŽ   r§   )	ro   r   r?   Ú	TokenInforŸ   r‹   r4   rŽ   r§   )r£   r   r   r   r¢   {  s    (ûÿr¢   c                 C   s*   dd„ t | ƒ| ƒD ƒ}|r&t|d ƒS | S )zq
  If the given AST node contains multiple statements, return the last one.
  Otherwise, just return the node.
  c                 S   s&   g | ]}t |ƒst|ƒjd v r|‘qS ))ÚexcepthandlerÚExceptHandlerZ
match_caseZ	MatchCaseZ	TryExceptZ
TryFinally)rg   r&   r   )rW   rb   r   r   r   Ú
<listcomp>�  s   ÿzlast_stmt.<locals>.<listcomp>rž   )rK   Ú	last_stmt)rJ   Zchild_stmtsr   r   r   r­   Š  s    
ÿr­   )r+   é   )Ú	lru_cache)Úmaxsizec                     sb   d‰ t  ˆ ¡} dd„ t  | ¡D ƒ}dd„ |D ƒ}tt|ƒƒt|ƒk}t‡ fdd„|D ƒƒ}|o`|S )aî  
    The positions attached to nodes inside f-string FormattedValues have some bugs
    that were fixed in Python 3.9.7 in https://github.com/python/cpython/pull/27729.
    This checks for those bugs more concretely without relying on the Python version.
    Specifically this checks:
     - Values with a format spec or conversion
     - Repeated (i.e. identical-looking) expressions
     - f-strings implicitly concatenated over multiple lines.
     - Multiline, triple-quoted f-strings.
    zÙ(
      f"a {b}{b} c {d!r} e {f:g} h {i:{j}} k {l:{m:n}}"
      f"a {b}{b} c {d!r} e {f:g} h {i:{j}} k {l:{m:n}}"
      f"{x + y + z} {x} {y} {z} {z} {z!a} {z:z}"
      f'''
      {s} {t}
      {u} {v}
      '''
    )c                 S   s   g | ]}t |tjƒr|‘qS r   )rN   rP   ÚName©rW   rJ   r   r   r   r¬   º  ry   z*fstring_positions_work.<locals>.<listcomp>c                 S   s   g | ]}|j |jf‘qS r   )r   Ú
col_offsetr²   r   r   r   r¬   »  ry   c                 3   s    | ]}t  ˆ |¡|jkV  qd S r%   )rP   Úget_source_segmentÚidr²   ©Úsourcer   r   r¦   ½  s   ÿz)fstring_positions_work.<locals>.<genexpr>)rP   Úparserˆ   r   rz   Úall)ÚtreeZ
name_nodesZname_positionsZpositions_are_uniqueZcorrect_source_segmentsr   r¶   r   Úfstring_positions_work£  s    	
þr»   c                 C   sŒ   t jdkrdS t| dd�D ]l}t|tjƒs,q|jD ]R}t|ddƒ t|tjƒr2t	ƒ spt|j
ƒD ]}t|ddƒ q^|jr2t|jddƒ q2qdS )z
    Add a special attribute `_broken_positions` to nodes inside f-strings
    if the lineno/col_offset cannot be trusted.
    )r+   é   NT)rM   Z_broken_positions)ÚsysÚversion_inforˆ   rN   rP   rj   r]   ÚsetattrÚFormattedValuer»   r/   Úformat_spec)rº   Z	joinedstrÚpartrb   r   r   r   Úannotate_fstring_nodesÃ  s    

rÃ   c                   C   s   dS )NFr   r   r   r   r   r»   Þ  s    c                 C   s   d S r%   r   )Z_treer   r   r   rÃ   â  s    )N)N)F)F)F)ErP   ÚcollectionsrA   r½   r   r?   Úabcr   r   r   r   Útypingr   r   r   r	   r
   r   r   r   r   r   r   Úsixr   Zastroid_compatr   r   ZAstNoder¾   Úintr3   r©   r#   Ú
namedtupler$   ÚConstantr-   r1   r5   r=   r@   rK   rG   Ú__dict__r`   rH   rf   rd   re   rg   ri   rL   rl   rq   rv   r.   r{   r‡   rˆ   r�   r‘   r�   r¢   r­   Ú	functoolsr¯   r»   rÃ   r   r   r   r   Ú<module>   sv   4$





þ
+



