o
    ˷e5                     @   s  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 d dlmZ erXd dlmZ G dd de	Zeeef Zd	d
 ZG dd deddZejdkrqe jZ nG dd dZ d>ddZ!d>ddZ"ejdkrdd Z#ndd Z#dd Z$dd Z%dd ee j&D Z'dd  Z(d!d ee j&D Z)d"d ee j&D h d#B Z*d$d% Z+d&d' Z,d(d) Z-d*d+ Z.d,d- Z/d.d/ Z0e1 Z2d0d1 Z3d2d3 Z4d4d5 Z5G d6d7 d7e1Z6ejd  d8krd9d: Z7dS d;d: Z7d<d= Z8dS )?    N)ABCMeta)ModuleexprAST)
CallableDict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__first_token
last_tokenlineno r   r   E/var/www/ideatree/venv/lib/python3.10/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__=   s   zToken.__str__N)r   r   r   __doc__r(   r   r   r   r   r#   /   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,   E   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    tok_strr   r   r   match_tokenI   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      N)r1   
ValueErrorr"   strstartr/   r   r   r   expect_tokenO   s   r6   )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_token]   s   r>   c                 C   s
   | t jkS r8   )r   N_TOKENSr<   r   r   r   r>   d   s   
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_funcl   s   rF   c                 C   s   t | rg S |  S r$   )is_joined_strr@   rD   r   r   r   rB   v   s   rB   c              	   C   s<   h | ]\}}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    
rT   c                 c   sn    t | rd S t| tjr%t| j| jD ]\}}|d ur|V  |V  qd S t| D ]
}|jt	vr4|V  q*d S r$   )
rG   rH   rJ   r   zipkeysvaluesiter_child_nodes	__class__
SINGLETONS)rE   keyr.   childr   r   r   rC      s   
rC   c                 C   *   h | ]\}}t |trt|tjr|qS r   )rH   r%   rI   rJ   stmtrP   r   r   r   rT      
    

c                 C   r]   r   )rH   r%   rI   rJ   r   rP   r   r   r   rT      r_   >   ConstDelAttrDelName
AssignAttr
AssignNamec                 C      | j jtv S )z+Returns whether node is an expression node.)rY   r   expr_class_namesrD   r   r   r   is_expr      rg   c                 C   re   )z)Returns whether node is a statement node.)rY   r   stmt_class_namesrD   r   r   r   is_stmt   rh   rj   c                 C      | j jdkS )z&Returns whether node is a module node.r   rY   r   rD   r   r   r   	is_module   rh   rm   c                 C   rk   )zFReturns whether node is a JoinedStr node, used to represent f-strings.	JoinedStrrl   rD   r   r   r   rG      s   rG   c                 C   rk   )z2Returns whether node is a starred expression node.Starredrl   rD   r   r   r   
is_starred   rh   rp   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   )	rY   r   anymapis_slicer   rJ   r   eltsrD   r   r   r   ru      s   ru   c                 C   s   |sdd }t | }t }d}| dtfg}|r^| \}}}	|	tu rQ||vs(J || |||\}
}||||f t|}||D ]}||||
tf qDn|||tt	t
 |	}|s|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   )rE   pvaluer.   r   r   r   <lambda>   s    zvisit_tree.<locals>.<lambda>N)rF   set	_PREVISITpopaddappendleninsertr   r
   r#   )rE   previsit	postvisititer_childrendoneretstackcurrent	par_valuer.   rw   
post_valueinsrR   r   r   r   
visit_tree   s(   
r   c                 c   sl    t | }t }| g}|r4| }||vsJ || |V  t|}||D ]}||| q'|sdS dS )a  
  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``.
  N)rF   ry   r{   r|   r~   r   )rE   r   r   r   r   r   rS   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)textreplacementsppartsr5   endnew_textr   r   r   replace  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__-  s   
zNodeMethods.__init__c                 C   s:   | j |}|sd|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   lowergetattrvisit_default)r'   objclsmethodnamer   r   r   r   1  s   
zNodeMethods.getN)r   r   r   r)   r   r   r   r   r   r   r   )  s    r      c                 C   s   | S r$   r   )original_tokensr   r   r   patched_generate_tokensB  s   r   c                 c   s|    g }| D ],}|j tjtjtjfv r"|r|d j|jkr"|| qt|D ]}|V  q&g }|V  qt|D ]}|V  q6d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%   tokenizeNAME
ERRORTOKENNUMBERr   r5   r}   combine_tokens)r   grouptokcombined_tokenr   r   r   r   F  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   rQ   r   r   r   r   	<genexpr>a  s    z!combine_tokens.<locals>.<genexpr>c                 S   s   h | ]}|j qS r   )liner   r   r   r   rT   a  s    z!combine_tokens.<locals>.<setcomp>r2   r   c                 s   s    | ]}|j V  qd S r$   )r!   )rQ   tr   r   r   r   f  s    r   r   )r%   r!   r5   r   r   )	rs   r~   r   	TokenInfor   r   r5   r   r   )r   r   r   r   r   _  s   (r   r$   )9rJ   collectionssysr   r   abcr   r   r   r   typingr   r   r   r	   r
   r   r   r   r   r   sixr   astroid.node_classesr   r   AstNoder"   
namedtupler#   version_infoConstantr,   r1   r6   r>   rF   rB   __dict__rZ   rC   ri   rf   rg   rj   rm   rG   rp   ru   r-   rz   r   r   r   r   r   r   r   r   r   r   <module>   s\   0





	,