
    ^j"D                        U 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Zd dlmZ d dl m	Z	m
Z
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mZmZmZ erddlmZ neZej:                  Z ej<                  d      Zd	e d
ee!   de!fdZ" G d d ejF                  dd            Z$ G d de      Z%ee%ef   Z&d>de$d	e dee!   de'fdZ(d>de$d	e dee!   ddfdZ)de de'fdZ*de!dee   fdZ+dedefdZ,d?dede'deee-f   fdZ.e j^                  ja                         D  ch c]Q  \  } } e1|e2      s e3|e jh                  e jj                  e jl                  e jn                  e jp                  f      r|S c}} Z9d?dede'deeee
f      fdZ:e j^                  ja                         D  ch c]#  \  } } e1|e2      r e3|e jv                        r| % c}} Z<e j^                  ja                         D  ch c]#  \  } } e1|e2      r e3|e j                        r| % c}} h dz  Z=de&de'fd Z>de&de'fd!Z?de&de'fd"Z@de&de'fd#ZAde&de'fd$ZBe j                  fZDeed%f   eEd&<   	 d d'lFmGZG eDeGfz  ZDde&de'fd(ZIde&de'fd)ZJde&de'fd*ZKde&de'fd+ZLde&de'fd,ZM eN       ZOde	d-ee&ee$   geee$   ee$   f   f   d.eee&ee$   ee$   gdf      ddfd/ZPd?dede'deee	e&f      fd0ZQde!d1eee e e!f      de!fd2ZR G d3 d4      ZSd5ee   dee   fd6ZTd7eej:                     deej:                     fd8ZUde&de&fd9ZV ed:      de'fd;       ZWd<e j                  ddfd=ZXyc c}} w c c}} w c c}} w # eH$ r Y w xY w)@    N)ABCMeta)ModuleexprAST)	lru_cache)CallableDictIterableIteratorListOptionalTupleUnioncastAnyTYPE_CHECKINGType   )NodeNGz\r(?!\n)tok_typestringreturnc                 b    t         j                  |     dt        |      j                  d       S )zRReturns a human-friendly representation of a token with the given type and string.:u)tokentok_namereprlstrip)r   r   s     G/opt/ringagent/.cad-venv/lib/python3.12/site-packages/asttokens/util.py
token_reprr!   2   s/     NN8$%QtF|':':3'?&@	AA    c                       e Zd ZdZdefdZy)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.
  r   c                 B    t        | j                  | j                        S N)r!   typer   selfs    r    __str__zToken.__str__F   s    dii--r"   N)__name__
__module____qualname____doc__strr*    r"   r    r$   r$   8   s    .s .r"   r$   z0type string start end line index startpos endposc                   T    e Zd ZU dZeed<   dZeed<   dZeed<   dZ	eed<   dZ
eed<   y)EnhancedASTNfirst_token
last_tokenr   lineno
end_linenoend_col_offset)r+   r,   r-   r3   r$   __annotations__r4   r5   intr6   r7   r0   r"   r    r2   r2   L   s3    +u*e&#/*c.#r"   r2   r   tok_strc                 N    | j                   |k(  xr |du xs | 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   r:   s      r    match_tokenr=   X   s(    	x		PW_%O8OPr"   c                     t        | ||      sGt        dt        ||       dt        |        d| j                  d    d| j                  d   dz          y)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!   r/   startr<   s      r    expect_tokenrA   ]   sh    
 
UHg	.

8W56 7ZL	%++a.!1u{{1~7I6JL  
/r"   
token_typec                 d    | t         j                  t         j                  t         j                  fv S )zS
  These are considered non-coding tokens, as they don't affect the syntax tree.
  )r   NLCOMMENTENCODING)rB   s    r    is_non_coding_tokenrG   i   s"     
%--@	@@r"   textc                     t         j                  d|       } t        j                  t	        t
        g t        f   t        j                  |       j                              S )z;
  Generates standard library tokens for the given code.
  
)
_lone_cr_resubtokenizegenerate_tokensr   r   r/   ioStringIOreadline)rH   s    r    rN   rN   p   sF     
t	$$ 
	!	!$xC'8"++d:K:T:T"U	VVr"   nodec                 2    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rR   s    r    iter_children_funcrY      s     #*$"?	VEVVr"   include_joined_strc                 @    |st        |       rg S | j                         S r&   )is_joined_strrT   )rR   rZ   s     r    rV   rV      s     	d 3I				r"   c              #   $  K   |st        |       ry t        | t        j                        r3t	        | j
                  | j                        D ]  \  }}|| |  y t        j                  |       D ]  }|j                  t        vs|  y wr&   )
r\   
isinstanceastr	   zipkeysvaluesiter_child_nodes	__class__
SINGLETONS)rR   rZ   keyvaluechilds        r    rW   rW      s     	d 3
chh DIIt{{3 e		k ##D) e j(ks   BB	B>   ConstDelAttrDelName
AssignAttr
AssignNamec                 :    | j                   j                  t        v S )z+Returns whether node is an expression node.)rd   r+   expr_class_namesrX   s    r    is_exprrp          		 	 $4	44r"   c                 :    | j                   j                  t        v S )z)Returns whether node is a statement node.)rd   r+   stmt_class_namesrX   s    r    is_stmtrt      rq   r"   c                 4    | j                   j                  dk(  S )z&Returns whether node is a module node.r   rd   r+   rX   s    r    	is_modulerw      s    		 	 H	,,r"   c                 4    | j                   j                  dk(  S )zFReturns whether node is a JoinedStr node, used to represent f-strings.	JoinedStrrv   rX   s    r    r\   r\      s     
	 	 K	//r"   c                 4    | j                   j                  dk(  S )zSReturns whether node is an `Expr` node, which is a statement that is an expression.Exprrv   rX   s    r    is_expr_stmtr|      s    		 	 F	**r"   .CONSTANT_CLASSES)ri   c                 "    t        | t              S )z(Returns whether node is a Constant node.)r^   r}   rX   s    r    is_constantr      s    	D*	++r"   c                 @    t        |       xr | j                  t        u S )z)Returns whether node is an Ellipsis node.)r   rg   EllipsisrX   s    r    is_ellipsisr      s    	T		5tzzX55r"   c                 4    | j                   j                  dk(  S )z2Returns whether node is a starred expression node.Starredrv   rX   s    r    
is_starredr      s    		 	 I	--r"   c           	          | j                   j                  dv xsV | j                   j                  dk(  xr; 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   )	rd   r+   anymapis_slicer   r_   r   eltsrX   s    r    r   r      sX    
 nn!66 
..
!
!W
, =#hSYY 5 : :;<	r"   c                     | j                   j                  dk(  xrP t        | t        j                         xr3 | j
                  | j                  cxu xr | j                  cxu xr d u S c S )Nr   )rd   r+   r^   r_   r   loweruppersteprX   s    r    is_empty_astroid_slicer      s\    
nn( 8sww'
'8
**


7dii
74
7 8r"   previsit	postvisitc                    |sd }t        |       }t               }d}| dt        fg}|r|j                         \  }}}	|	t        u ri||vsJ |j	                  |        |||      \  }
}|j                  |||f       t        |      } ||      D ]  }|j                  |||
t        f        n |||t        t        t           |	            }|r|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                      y r&   r0   )rR   pvaluerg   s      r    <lambda>zvisit_tree.<locals>.<lambda>  s    r"   N)rY   set	_PREVISITpopaddappendleninsertr   r   r$   )rR   r   r   iter_childrendoneretstackcurrent	par_valuerg   r   
post_valueinsns                 r    
visit_treer      s    * 
0I$T*-	$#;?y:Q9R% %		GY	D   
hhw#GY7fjllGY
34 JcW% 2!S1fi012 gy$x*FGc 	 
*r"   c              #      K   t        |       }t               }| g}|rZ|j                         }||vsJ |j                  |       | t	        |      } |||      D ]  }|j                  ||        |rYyyw)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)rY   r   r   r   r   r   )rR   rZ   r   r   r   r   r   cs           r    walkr   &  s      %T*-	$&%iikG$HHW
M e*C7$67 ll3 	s   A3A86A8replacementsc                     d}g }t        |      D ]-  \  }}}|j                  | ||        |j                  |       |}/ |j                  | |d        dj                  |      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)rH   r   ppartsr@   endnew_texts          r    replacer   B  sn     !
% &| 4 uc8	LLa	LLA ,,tABx	r"   c                   6    e Zd ZdZddZdedeeef   de	fdZ
y)	NodeMethodsz[
  Helper to get `visit_{node_type}` methods given a node's class and cache the results.
  r   Nc                     i | _         y r&   )_cacher(   s    r    __init__zNodeMethods.__init__Y  s	    fhDKr"   objclsc                     | j                   j                  |      }|sCd|j                  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)r)   r   r   methodnames        r    r   zNodeMethods.get\  sW    
 [[__S!F**,,dsD#"3"34fdkk#Mr"   )r   N)r+   r,   r-   r.   r   r   r   r   r'   r   r   r0   r"   r    r   r   U  s0    i
S 
uWd]3 
 
r"   r   original_tokensc              #   P  K   g }| D ]  }|j                   t        j                  t        j                  t        j                  fv r0|r|d   j
                  |j                  k(  r|j                  |       nt        |      D ]  }|  g }|  t        |      D ]  }|  yw)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'   rM   NAME
ERRORTOKENNUMBERr   r@   r   combine_tokens)r   grouptokcombined_tokens       r    patched_generate_tokensr   i  s      ')E 
((x}}h&9&98??K
KE"IMMSYY6S,U3 	N
		 )/ s   B$B&r   c                 L   t        d | D              r&t        | D ch c]  }|j                   c}      dk7  r| S t        j                  t        j
                  dj                  d | D              | d   j                  | d   j                  | d   j                        gS c c}w )Nc              3   V   K   | ]!  }|j                   t        j                  k(   # y wr&   )r'   rM   r   ).0r   s     r    	<genexpr>z!combine_tokens.<locals>.<genexpr>  s     @3sxx8...@s   ')r   r   c              3   4   K   | ]  }|j                     y wr&   )r   )r   ts     r    r   z!combine_tokens.<locals>.<genexpr>  s     /Aqxx/s   r   r   )r'   r   r@   r   line)	r   r   r   rM   	TokenInfor   r   r@   r   )r   r   s     r    r   r     s    @%@@C]bHcVYHcDdhiDil]]ww///Ahnn"IMM1X]]  Ids   B!c                      t        |       |       D cg c]&  }t        |      st        |      j                  dv r|( }}|rt	        |d         S | S c c}w )zq
  If the given AST node contains multiple statements, return the last one.
  Otherwise, just return the node.
  )excepthandlerExceptHandler
match_case	MatchCase	TryExcept
TryFinallyr   )rY   rt   r'   r+   	last_stmt)rR   rh   child_stmtss      r    r   r     si     0)$/5
u~e-- 2  

+ 
 [_%%	+
s   +A)maxsizec                  ~   dt        j                        } t        j                  |       D cg c]  }t        |t         j                        s|! }}|D cg c]  }|j
                  |j                  f }}t        t        |            t        |      k(  }t        fd|D              }|xr |S c c}w c c}w )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              3   d   K   | ]'  }t        j                  |      |j                  k(   ) y wr&   )r_   get_source_segmentid)r   rR   sources     r    r   z)fstring_positions_work.<locals>.<genexpr>  s/        64(DGG3 s   -0)
r_   parser   r^   Namer5   
col_offsetr   r   all)treerR   
name_nodesname_positionspositions_are_uniquecorrect_source_segmentsr   s         @r    fstring_positions_workr     s    & 
6	$!$$N:dCHH3MN*N?IJtT[[$//2J.JS01S5HH     
	9"99 OJs   B5B5B:r   c                    t         j                  dk\  ryt        | d      D ]  }t        |t        j
                        s|j                  D ]  }t        |dd       t        |t        j                        s+t               s't        |j                        D ]  }t        |dd        |j                  sit        |j                  dd         y)zy
  Add a special attribute `_broken_positions` to nodes inside f-strings
  if the lineno/col_offset cannot be trusted.
  )      NT)rZ   _broken_positions)sysversion_infor   r^   r_   ry   rb   setattrFormattedValuer   rg   format_spec)r   	joinedstrpartrh   s       r    annotate_fstring_nodesr     s    
 	  6 ?ii/   ?d'.	D#,,	-%'DJJ' 6eE.56 
$""$7
>??r"   r&   )F)Yr_   collectionsrO   rer   r   rM   abcr   r   r   r   	functoolsr   typingr   r	   r
   r   r   r   r   r   r   r   r   r   astroid_compatr   r   compilerK   r9   r/   r!   
namedtupler$   r2   AstNodeboolr=   rA   rG   rN   rY   listrV   __dict__itemsr^   r'   
issubclassexpr_contextboolopoperatorunaryopcmpopre   rW   stmtrs   ro   rp   rt   rw   r\   r|   Constantr}   r8   astroid.nodesri   ImportErrorr   r   r   r   r   objectr   r   r   r   r   r   r   r   r   r   )r   r   s   00r    <module>r     s     	 	 
    ! !     $& 	bjj%B Bhsm B B."K""7,^_ .(#  V#
$Qu Q Qhsm Qt Q
	 	 	x} 	PT 	AC AD AW# W(9"5 WWS WX W D US[]aSaMb   LL..0 cDAqJq$4GS--szz3<<VYV_V_`a  c
C T huUXZ^U^O_F` * #&,,"4"4"6 H$!Q!!T*z!SXX/F  H #&<<#5#5#7 H41a!!T*z!SXX/F  HOP 5' 5d 55' 5d 5-G - -0 0D 0+w +4 + '*ll_ %c	" 4!uh
,g ,$ ,
6g 6$ 6
.W . .

7 
t 
 T  H	++gx/xQV7W1XXY+ h%(5/JDPQR+ 
	+\s  %PWBX9Y 8# T%S#*>%? C & (Xi-@ XiEX 0$x112 tH<N<N7O G  , 4: : :<? ?T ?k	c2HHF  s+   M.AM&(M /(M& M, ,M54M5