
    7 fE                        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rTddlmZ  G 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 Z! G d d ej"        dd                    Z#ej        dk    re j$        Z%n G d d          Z%d7dZ&d7dZ'ej        dk    rd Z(nd Z(d Z)d Z*d8dZ+d  ee j,                  D             Z-d8dZ.d  ee j,                  D             Z/d  ee j,                  D             h dz  Z0d  Z1d! Z2d" Z3d# Z4d$ Z5d% Z6d& Z7 e8            Z9d' Z:d8d(Z;d) Z< G d* d+e8          Z=ej        d          d
k    rd, Z>nd- Z>d. Z?d/ Z@ej        dd
         d0k    r d d1lAmBZB  eBd2          d3             ZCd4 ZDdS d5 ZCd6 ZDdS )9    N)ABCMeta)ModuleexprAST)CallableDictIterableIteratorListOptionalTupleUnioncastAnyTYPE_CHECKING)	iteritems   )NodeNGc                       e Zd ZdZdZdZdS )EnhancedASTNr   )__name__
__module____qualname__first_token
last_tokenlineno     D/var/www/equiseq/venv/lib/python3.11/site-packages/asttokens/util.pyr   r   !   s        KJFFFr   r      c                 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     r   
token_reprr*   /   s3     N8,,,d6ll.A.A#.F.F.F	GGr   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__D   s    di---r   N)r   r   r   __doc__r2   r   r   r   r,   r,   6   s-         . . . . .r   r,   z0type string start end line index startpos endpos)      c                   "    e Zd Z e            ZdS )AstConstantN)r   r   r   objectvaluer   r   r   r7   r7   L   s        FHHEEEr   r7   c                 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=   P   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 r   N)r=   
ValueErrorr*   strstartr;   s      r   expect_tokenrB   V   sw     
UHg	.	. +
*7####SZZZZk!nnnek!nq((* + + ++ +r   )r4      c                 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COMMENTENCODING
token_types    r   is_non_coding_tokenrK   d   s    
 %(EM5>BBBr   c                 "    | t           j        k    S rE   )r$   N_TOKENSrI   s    r   rK   rK   k   s    
 ''r   c                     t          j        t          t          g t          f         t          j        |           j                            S )z;
  Generates standard library tokens for the given code.
  )tokenizegenerate_tokensr   r   r@   ioStringIOreadline)texts    r   rP   rP   s   s5     
	!$xC'8"+d:K:K:T"U"U	V	VVr   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_funcr\   ~   s      #*$"?"?	V		EVVr   Fc                 P    |st          |           rg S |                                 S r.   )is_joined_strrV   )r[   include_joined_strs     r   rX   rX      s0    	 d 3 3 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>rm      sk     c c cDAqz!T7J7J cS-sz3<VYV_`aaca c c cr   c              #     K   |s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^   ra   rc   r   zipkeysvaluesiter_child_nodes	__class__
SINGLETONS)r[   r_   keyr9   childs        r   rY   rY      s      	 d 3 3 
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   )ra   r/   rb   rc   stmtri   s      r   rm   rm      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   )ra   r/   rb   rc   r   ri   s      r   rm   rm      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.)rs   r   expr_class_namesrZ   s    r   is_exprr           
	 $4	44r   c                 (    | j         j        t          v S )z)Returns whether node is a statement node.)rs   r   stmt_class_namesrZ   s    r   is_stmtr      r   r   c                 "    | j         j        dk    S )z&Returns whether node is a module node.r   rs   r   rZ   s    r   	is_moduler      s     
	 H	,,r   c                 "    | j         j        dk    S )zFReturns whether node is a JoinedStr node, used to represent f-strings.	JoinedStrr   rZ   s    r   r^   r^      s    
 
	 K	//r   c                 "    | j         j        dk    S )z2Returns whether node is a starred expression node.Starredr   rZ   s    r   
is_starredr      s     
	 I	--r   c           	          | j         j        dv pN| 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   )	rs   r   anymapis_slicer   rc   r   eltsrZ   s    r   r   r      sR     n!66 
.
!W
, =#hSY 5 5 :;;<<	r   c                     | j         j        dk    o:t          | t          j                   o| j        | j        cxu o| j        cxu od u nc S )Nr   )rs   r   ra   rc   r   loweruppersteprZ   s    r   is_empty_astroid_slicer      se     n( 8sw''
'8
*

7
7
7
7di
7
7
7
74
7
7
7
7r   c                     |sd }t          |           }t                      }d}| dt          fg}|r|                                \  }}}	|	t          u r~||vsJ |                    |            |||          \  }
}|                    |||f           t          |          } ||          D ] }|                    |||
t          f           !n+ |||t          t          t                   |	                    }||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   )r[   pvaluer9   s      r   <lambda>zvisit_tree.<locals>.<lambda>   s    D r   N)r\   set	_PREVISITpopaddappendleninsertr   r   r,   )r[   previsit	postvisititer_childrendoneretstackcurrent	par_valuer9   r   
post_valueinsrk   s                 r   
visit_treer      s6   $ 
 100I$T**-	$#$	"
#% H %		GY	D    
hhw#8GY77fjllGY
3444 JJc}W%% 2 2!S1fi011112 Igy$x*F*FGGc 	 H 
*r   c              #   $  K   t          |           }t                      }| g}|rk|                                }||vsJ |                    |           |V  t	          |          } |||          D ]}|                    ||           |idS 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)r\   r   r   r   r   r   )r[   r_   r   r   r   r   r   rl   s           r   walkr     s       %T**-	$&% iikkG$HHW
MMM e**C]7$67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)rT   replacementsppartsrA   endnew_texts          r   replacer   1  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.   )_cacher0   s    r   __init__zNodeMethods.__init__I  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   r   getattrvisit_default)r1   objclsmethodnames        r   r   zNodeMethods.getM  s\     [__S!!F  **,,,dsD#"344fdk#Mr   N)r   r   r   r3   r   r   r   r   r   r   r   E  s<               r   r   c                      t          |           S r.   )iter)original_tokenss    r   patched_generate_tokensr   ^  s       r   c              #   ,  K   g }| D ]v}|j         t          j        t          j        t          j        fv r.|r|d         j        |j        k    r|                    |           Zt          |          D ]}|V  g }|V  wt          |          D ]}|V  d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/   rO   NAME
ERRORTOKENNUMBERr   rA   r   combine_tokens)r   grouptokcombined_tokens       r   r   r   b  s       E  
(x}h&98?K
K
K L!"IMSY66S,U33 	 	N



				(//   r   c                 6   t          d | D                       rt          d | D                       dk    r| S t          j        t          j        d                    d | D                       | d         j        | d         j        | d         j                  gS )	Nc              3   @   K   | ]}|j         t          j        k    V  d S r.   )r/   rO   r   rj   r   s     r   	<genexpr>z!combine_tokens.<locals>.<genexpr>}  s,      @@3sx8..@@@@@@r   c                     h | ]	}|j         
S r   )liner   s     r   rm   z!combine_tokens.<locals>.<setcomp>}  s    HcHcHcVYHcHcHcr   r   r   c              3   $   K   | ]}|j         V  d S r.   )r)   )rj   ts     r   r   z!combine_tokens.<locals>.<genexpr>  s$      //Aqx//////r   r   r   )r/   r)   rA   r   r   )	r   r   rO   	TokenInfor   r   rA   r   r   )r   s    r   r   r   {  s    @@%@@@@@ CHcHc]bHcHcHcDdDdhiDiDil]ww///////Ahn"IM1X]   r   c                 x    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                 \    g | ])}t          |          st          |          j        d v '|*S ))excepthandlerExceptHandler
match_case	MatchCase	TryExcept
TryFinally)r   r/   r   )rj   rv   s     r   
<listcomp>zlast_stmt.<locals>.<listcomp>  sQ     
 
 
u~~
e- 2   
  r   r   )r\   	last_stmt)r[   child_stmtss     r   r   r     sT    
 
/)$//55
 
 
+  &[_%%%	+r   )r4      )	lru_cache)maxsizec                     dt          j                  } d t          j        |           D             }d |D             }t          t	          |                    t          |          k    }t          f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                 F    g | ]}t          |t          j                  |S r   )ra   rc   Namerj   r[   s     r   r   z*fstring_positions_work.<locals>.<listcomp>  s)    PPP4Zch5O5OP$PPPr   c                 *    g | ]}|j         |j        fS r   )r   
col_offsetr   s     r   r   z*fstring_positions_work.<locals>.<listcomp>  s!    LLLt{DO4LLLr   c              3   T   K   | ]"}t          j        |          |j        k    V  #d S r.   )rc   get_source_segmentid)rj   r[   sources     r   r   z)fstring_positions_work.<locals>.<genexpr>  sK       " "
 
VT**dg5" " " " " "r   )rc   parser   r   r   all)tree
name_nodesname_positionspositions_are_uniquecorrect_source_segmentsr   s        @r   fstring_positions_workr     s    	F 9VDPP38D>>PPPJLLLLLNs>2233s>7J7JJ! " " " "" " "    ;$;;r   c                    t           j        dk    rdS t          | d          D ]}t          |t          j                  s|j        D ]}t          |dd           t          |t          j                  rSt                      s(t          |j
                  D ]}t          |dd           |j        rt          |j        dd           dS )z
    Add a special attribute `_broken_positions` to nodes inside f-strings
    if the lineno/col_offset cannot be trusted.
    )r4      NT)r_   _broken_positions)sysversion_infor   ra   rc   r   rq   setattrFormattedValuer   r9   format_spec)r   	joinedstrpartrv   s       r   annotate_fstring_nodesr
    s     7"" f$4888 A A		3=11 " A A$)4000dC.// 	A')) 8dj)) 8 8e0$7777 AD$&94@@@AA Ar   c                      dS )NFr   r   r   r   r   r     s    5r   c                     d S r.   r   )_trees    r   r
  r
    s    Dr   r.   )F)Erc   collectionsrQ   r  r$   rO   abcr   r   r   r   typingr   r   r	   r
   r   r   r   r   r   r   r   sixr   astroid_compatr   r   AstNoder  intr@   r   r*   
namedtupler,   Constantr7   r=   rB   rK   rP   r\   rX   __dict__rt   rY   r   r   r   r   r   r^   r   r   r   r8   r   r   r   r   r   r   r   r   	functoolsr   r   r
  r   r   r   <module>r     s   


     				 



         ! ! ! ! ! ! ! ! ! ! m m m m m m m m m m m m m m m m m m m m m m m m m m        #$$$$$$    C    +v%&'aAc3c3hsCx#EFII"IH H H. . . . ."K"7,^__ . . .& v++       Q Q Q Q	+ 	+ 	+ 	+ vC C C C( ( (W W W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   :  (    &   , A!! ! ! !  2    , BQB6!!!!!!!!9T< < <>A A A A A6  	 	 	 	 	r   