
    c*b.'                        d dl Z d dlZd dlZd dlmZ d Z G d d ej        dd                    ZddZddZ	 e
ed	          rd
 Znd Zd Zd Zd  ee j                  D             Zd Zd  ee j                  D             Zd  ee j                  D             h dz  Zd Zd Zd Zd Zd Zd Z e            Zd Zd Zd Z G d de          ZdS )    N)	iteritemsc                 p    t           j        |          dt          |                              d          S )zRReturns a human-friendly representation of a token with the given type and string.:u)tokentok_namereprlstrip)tok_typestrings     .lib/python3.11/site-packages/asttokens/util.py
token_reprr      s3     N8,,,d6ll.A.A#.F.F.F	GG    c                       e Zd ZdZ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                 6    t          | j        | j                  S N)r   typer   selfs    r   __str__zToken.__str__)   s    di---r   N)__name__
__module____qualname____doc__r    r   r   r   r      s-         . . . . .r   r   z0type string start end line index startpos endposc                 6    | 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_strs      r   match_tokenr    -   s$    	x		PW_%O8OPr   c                     t          | ||          sOt          dt          ||          dt          |           d| j        d         d| j        d         dz             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 z, got z	 on line r   z col    N)r    
ValueErrorr   strstartr   s      r   expect_tokenr&   2   sw    
 
UHg	.	. +
*7####SZZZZk!nnnek!nq((* + + ++ +r   ENCODINGc                 L    | 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COMMENTr'   
token_types    r   is_non_coding_tokenr.   ?   s     %(EM5>BBBr   c                 "    | t           j        k    S r)   )r   N_TOKENSr,   s    r   r.   r.   E   s     ''r   c                 >    t          | d          rt          n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nodes    r   iter_children_funcr8   L   s      #*$"?"?	V		EVVr   c                 L    t          |           rg S |                                 S r   )is_joined_strr2   r6   s    r   r4   r4   U   s)    4 I					r   c           
          h | ]c\  }}t          |t                    t          |t          j        t          j        t          j        t          j        t          j        f          a|dS r   )	
isinstancer   
issubclassastexpr_contextboolopoperatorunaryopcmpop.0ncs      r   	<setcomp>rH   ]   sk     c c cDAqz!T7J7J cS-sz3<VYV_`aaca c c cr   c              #   
  K   t          |           rd S t          | t          j                  r,t	          | j        | j                  D ]\  }}||V  |V  d S t          j        |           D ]}|j        t          vr|V  d S r   )
r:   r<   r>   Dictzipkeysvaluesiter_child_nodes	__class__
SINGLETONS)r7   keyvaluechilds       r   r5   r5   `   s      4 
Fch  DIt{33  e				kkkk
F#D))  e j((kkk r   c                 v    h | ]6\  }}t          |t                    t          |t          j                  4|7S r   )r<   r   r=   r>   stmtrD   s      r   rH   rH   v   sV     H H H$!Q!!T**H/9!SX/F/FHA H H Hr   c                 v    h | ]6\  }}t          |t                    t          |t          j                  4|7S r   )r<   r   r=   r>   exprrD   s      r   rH   rH   x   sV     H H H41a!!T**H/9!SX/F/FHQ H H Hr   >   ConstDelAttrDelName
AssignAttr
AssignNamec                 (    | j         j        t          v S )z+Returns whether node is an expression node.)rO   r   expr_class_namesr6   s    r   is_exprr_   ~       		 $4	44r   c                 (    | j         j        t          v S )z)Returns whether node is a statement node.)rO   r   stmt_class_namesr6   s    r   is_stmtrc      r`   r   c                 "    | j         j        dk    S )z&Returns whether node is a module node.ModulerO   r   r6   s    r   	is_modulerg      s    		 H	,,r   c                 "    | j         j        dk    S )zFReturns whether node is a JoinedStr node, used to represent f-strings.	JoinedStrrf   r6   s    r   r:   r:      s     
	 K	//r   c                 "    | j         j        dk    S )z2Returns whether node is a starred expression node.Starredrf   r6   s    r   
is_starredrl      s    		 I	--r   c                     | j         j        dv p6| j         j        dk    o&t          t          t          | j                            S )z?Returns whether node represents a slice, e.g. `1:2` in `x[1:2]`)SliceExtSliceTuple)rO   r   anymapis_sliceeltsr6   s    r   rs   rs      sE    
 n!66 
.
!W
, ,#h	**++	r   c                    |sd }t          |           }t                      }d}| dt          fg}|r|                                \  }}}	|	t          u r~||vsJ |                    |            |||          \  }
}|                    |||f           t          |          } ||          D ] }|                    |||
t          f           !n ||||	          }||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                     d S r   r   )r7   pvaluerR   s      r   <lambda>zvisit_tree.<locals>.<lambda>   s    D r   N)r8   set	_PREVISITpopaddappendleninsert)r7   previsit	postvisititer_childrendoneretstackcurrent	par_valuerR   rw   
post_valueinsrF   s                 r   
visit_treer      s&   " 
 100I$T**-	$#$	"
#% 1 %		GY	D    
hhw#8GY77fjllGY
3444 JJc}W%% 2 2!S1fi011112 Igy%00c 	 1 
*r   c              #   "  K   t          |           }t                      }| g}|rj|                                }||vsJ |                    |           |V  t	          |          } ||          D ]}|                    ||           |hd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)r8   ry   r{   r|   r~   r   )r7   r   r   r   r   r   rG   s          r   walkr      s       %T**-	$&% iikkG$HHW
MMM e**C]7##  ll3 	     r   c                    d}g }t          |          D ]:\  }}}|                    | ||                    |                    |           |};|                    | |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partsr%   endnew_texts          r   replacer      s     !
% &| 4 4  uc8	LLag	LLAA,,tABBx	r   c                       e Zd ZdZd Zd ZdS )NodeMethodsz[
  Helper to get `visit_{node_type}` methods given a node's class and cache the results.
  c                     i | _         d S r   )_cacher   s    r   __init__zNodeMethods.__init__  s    DKKKr   c                     | j                             |          }|s<d|j                                        z   }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names        r   r   zNodeMethods.get  s\    
 [__S!!F  **,,,dsD#"344fdk#Mr   N)r   r   r   r   r   r   r   r   r   r   r      s<           
 
 
 
 
r   r   r   )r>   collectionsr   sixr   r   
namedtupler   r    r&   r3   r.   r8   r4   __dict__rP   r5   rb   r^   r_   rc   rg   r:   rl   rs   objectrz   r   r   r   r   r   r   r   <module>r      s   


           H H H. . . . ."K"7,^__ . . .$Q Q Q Q
+ + + + 75* (C C C C( ( (W W W  c cIIcl33 c c c
  ,H H))CL"9"9 H H H H H99S\#:#: H H HOOOP 5 5 55 5 5- - -0 0 0. . .

 
 
 FHH	' ' 'V  2  &    &     r   