
    ^jC                     X   U 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	m
Z
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  G d de j8                  	      Z G d
 de      Z G d de      ZdZ ee!df   e"d<   ejF                  dd dk(  r
e dz  Z e dz  Z ddede$fdZ%y)    N)Module)IterableIteratorListOptionalTupleAnycast   )LineNumbers)AstNodeToken	TokenInfomatch_tokenis_non_coding_tokenpatched_generate_tokens	last_stmtannotate_fstring_nodesgenerate_tokens	is_moduleis_stmtc                       e Zd ZdededdfdZej                  dedede	e	e
e
f   e	e
e
f   f   fd       Zddedede	e
e
f   fd	Zddededefd
Zy)ASTTextBasesource_textfilenamereturnNc                 V    || _         t        |      }|| _        t        |      | _        y N)	_filenamestr_textr   _line_numbers)selfr   r   s      L/opt/ringagent/.cad-venv/lib/python3.12/site-packages/asttokens/asttokens.py__init__zASTTextBase.__init__   s)    DN
 k"KDJ$[1D    nodepaddedc                     t         )  
    Returns two ``(lineno, col_offset)`` tuples for the start and end of the given node.
    If the positions can't be determined, or the nodes don't correspond to any particular text,
    returns ``(1, 0)`` for both.

    ``padded`` corresponds to the ``padded`` argument to ``ast.get_source_segment()``.
    This means that if ``padded`` is True, the start position will be adjusted to include
    leading whitespace if ``node`` is a multiline statement.
    )NotImplementedErrorr#   r'   r(   s      r$   get_text_positionszASTTextBase.get_text_positions*   s
     r&   c                     | j                  ||      \  }} | j                  j                  |  | j                  j                  | fS )a  
    Returns the (startpos, endpos) positions in source text corresponding to the given node.
    Returns (0, 0) for nodes (like `Load`) that don't correspond to any particular text.

    See ``get_text_positions()`` for details on the ``padded`` argument.
    )r-   r"   line_to_offsetr#   r'   r(   startends        r$   get_text_rangezASTTextBase.get_text_range9   sP     ((v6JE3'd''/'d''- r&   c                 J    | j                  ||      \  }}| j                  || S )z
    Returns the text corresponding to the given node.
    Returns '' for nodes (like `Load`) that don't correspond to any particular text.

    See ``get_text_positions()`` for details on the ``padded`` argument.
    )r3   r!   r0   s        r$   get_textzASTTextBase.get_textF   s+     $$T62JE3::eS!!r&   )T)__name__
__module____qualname__r    r%   abcabstractmethodr   boolr   intr-   r3   r5    r&   r$   r   r      s    	2# 	2 	2 	2 !%U38_eCHo-.  $ %S/ "7 "D "C "r&   r   )	metaclassc                       e Zd ZdZ	 	 	 	 d(dededee   dedee	e
      ddf fd	Zd
eddfdZde	e
   dee   fdZedefd       Zedee   fd       Zedee   fd       Zedefd       ZdedefdZdededefdZdededefdZd)dededefdZd)dededefdZ	 d*dededee   dedef
d Z	 d)d!ed"ededee   fd#Zd)d$ededee   fd%Zd$ed&ede e eef   e eef   f   fd'Z! xZ"S )+	ASTTokensa  
  ASTTokens maintains the text of Python code in several forms: as a string, as line numbers, and
  as tokens, and is used to mark and access token and position information.

  ``source_text`` must be a unicode or UTF8-encoded string. If you pass in UTF8 bytes, remember
  that all offsets you'll get are to the unicode text, which is available as the ``.text``
  property.

  If ``parse`` is set, the ``source_text`` will be parsed with ``ast.parse()``, and the resulting
  tree marked with token info and made available as the ``.tree`` property.

  If ``tree`` is given, it will be marked and made available as the ``.tree`` property. In
  addition to the trees produced by the ``ast`` module, ASTTokens will also mark trees produced
  using ``astroid`` library <https://www.astroid.org>.

  If only ``source_text`` is given, you may use ``.mark_tokens(tree)`` to mark the nodes of an AST
  tree created separately.
  Nr   parsetreer   tokensr   c                 |   t         |   ||       |rt        j                  ||      n|| _        |t        | j                        }t        | j                  |            | _	        | j                  D cg c]  }|j                   c}| _        | j                  r| j                  | j                         y y c c}w r   )superr%   astrA   _treer   r!   list_translate_tokens_tokensstartpos_token_offsetsmark_tokens)r#   r   rA   rB   r   rC   tok	__class__s          r$   r%   zASTTokens.__init__e   s     
G[(+5:;1DJ ~tzz*f..v67DL 48<<@C3<<@Dzz
tzz"  As   5B9	root_nodec                 >    ddl m}  ||       j                  |       y)aq  
    Given the root of the AST or Astroid tree produced from source_text, visits all nodes marking
    them with token and position information by adding ``.first_token`` and
    ``.last_token`` attributes. This is done automatically in the constructor when ``parse`` or
    ``tree`` arguments are set, but may be used manually with a separate AST or Astroid tree.
    r   )
MarkTokensN)rM   rR   
visit_tree)r#   rP   rR   s      r$   rM   zASTTokens.mark_tokens|   s     (t	*r&   original_tokensc              #      K   t        t        |            D ]a  \  }}|\  }}}}}t        ||||||| j                  j	                  |d   |d         | j                  j	                  |d   |d                c yw)zS
    Translates the given standard library tokens into our own representation.
    r   r   N)	enumerater   r   r"   r/   )	r#   rT   indexrN   tok_typetok_strr1   r2   lines	            r$   rI   zASTTokens._translate_tokens   s        7 HI E
s,/)hT(GUCu$$33E!HeAhG$$33CFCFCE EEs   A:A<c                     | j                   S )z,The source code passed into the constructor.)r!   r#   s    r$   textzASTTokens.text        ::r&   c                     | j                   S )zIThe list of tokens corresponding to the source code from the constructor.)rJ   r\   s    r$   rC   zASTTokens.tokens   s     <<r&   c                     | j                   S )zTThe root of the AST tree passed into the constructor or parsed from the source code.)rG   r\   s    r$   rB   zASTTokens.tree   r^   r&   c                     | j                   S )zThe filename that was parsed)r   r\   s    r$   r   zASTTokens.filename   s     >>r&   offsetc                 b    | j                   t        j                  | j                  |      dz
     S )z
    Returns the token containing the given character offset (0-based position in source text),
    or the preceeding token if the position is between tokens.
    r   )rJ   bisectrL   )r#   rb   s     r$   get_token_from_offsetzASTTokens.get_token_from_offset   s)    
 <<d&9&96BQFGGr&   lineno
col_offsetc                 X    | j                  | j                  j                  ||            S )z
    Returns the token containing the given (lineno, col_offset) position, or the preceeding token
    if the position is between tokens.
    )re   r"   r/   r#   rf   rg   s      r$   	get_tokenzASTTokens.get_token   s)     %%d&8&8&G&GPZ&[\\r&   c                 Z    | j                  || j                  j                  ||            S )zd
    Same as get_token(), but interprets col_offset as a UTF8 offset, which is what `ast` uses.
    )rj   r"   from_utf8_colri   s      r$   get_token_from_utf8zASTTokens.get_token_from_utf8   s(     >>&$"4"4"B"B6:"VWWr&   rN   include_extrac                     |j                   dz   }|sJt        | j                  |   j                        r(|dz  }t        | j                  |   j                        r(| j                  |   S )z
    Returns the next token after the given one. If include_extra is True, includes non-coding
    tokens from the tokenize module, such as NL and COMMENT.
    r   rW   r   rJ   typer#   rN   rn   is       r$   
next_tokenzASTTokens.next_token   Y    
 			AAQ 4 45	Q  Q 4 45<<?r&   c                     |j                   dz
  }|sJt        | j                  |   j                        r(|dz  }t        | j                  |   j                        r(| j                  |   S )z
    Returns the previous token before the given one. If include_extra is True, includes non-coding
    tokens from the tokenize module, such as NL and COMMENT.
    r   rp   rr   s       r$   
prev_tokenzASTTokens.prev_token   ru   r&   start_tokenrX   rY   reversec                    |}|r| j                   n| j                  }t        |||      sVt        j                  |j
                        s7 ||d      }t        |||      s t        j                  |j
                        s7|S )z
    Looks for the first token, starting at start_token, that matches tok_type and, if given, the
    token string. Searches backwards if reverse is True. Returns ENDMARKER token if not found (you
    can check it with `token.ISEOF(t.type)`).
    Trn   )rw   rt   r   tokenISEOFrq   )r#   rx   rX   rY   ry   tadvances          r$   
find_tokenzASTTokens.find_token   sc     	A!(doodooG!Xw/AFF8K
!4
(a !Xw/AFF8KHr&   first_token
last_tokenc              #      K   t        |j                  |j                  dz         D ]8  }|s#t        | j                  |   j                        r(| j                  |    : yw)z
    Yields all tokens in order from first_token through and including last_token. If
    include_extra is True, includes non-coding tokens such as tokenize.NL and .COMMENT.
    r   N)rangerW   r   rJ   rq   )r#   r   r   rn   rs   s        r$   token_rangezASTTokens.token_range   sV      ;$$j&6&6&:; 	1$,,q/2F2FGll1os   AA"A"r'   c                 R    | j                  |j                  |j                  |      S )z
    Yields all tokens making up the given node. If include_extra is True, includes non-coding
    tokens such as tokenize.NL and .COMMENT.
    r{   )r   r   r   )r#   r'   rn   s      r$   
get_tokenszASTTokens.get_tokens   s&    
 D,,doo][[r&   r(   c                     t        |d      sy|j                  j                  }|j                  j                  }|r(t        d | j                  |      D              r|d   df}||fS )r*   r   r   r   r   c              3   P   K   | ]  }t        |t        j                           y wr   )r   r|   NEWLINE).0r~   s     r$   	<genexpr>z/ASTTokens.get_text_positions.<locals>.<genexpr>  s     Sk!U]]3Ss   $&r   )hasattrr   r1   r   r2   anyr   r0   s        r$   r-   zASTTokens.get_text_positions   sa     4'""E
//

C#ST__T=RSSQxme#:r&   )FN	<unknown>N)F)NF)#r6   r7   r8   __doc__r	   r;   r   r   r    r   r   r%   rM   r   r   rI   propertyr]   r   rC   rB   r   r<   re   rj   rm   rt   rw   r   r   r   r   r   r-   __classcell__rO   s   @r$   r@   r@   Q   sZ   , !,0## # 6
	#
 # Xi()# #.	+6 	+d 	+Ex	/B ExPU E C   d5k   HV$     H# H% H]c ]s ]u ]X X X X	E 	$ 	5 		E 	$ 	5 	 ]b(+6>smUY$  	  	
 \W \T \huo \W d uU3PS8_V[\_ad\dVeEe?f r&   r@   c            
            e Zd ZdZddedee   deddf fdZe	defd       Z
e	defd	       Zd
ededeeeef   eeef   f   fdZd
ededeeeef   eeef   f   fdZ xZS )ASTTextaF  
  Supports the same ``get_text*`` methods as ``ASTTokens``,
  but uses the AST to determine the text positions instead of tokens.
  This is faster than ``ASTTokens`` as it requires less setup work.

  It also (sometimes) supports nodes inside f-strings, which ``ASTTokens`` doesn't.

  Some node types and/or Python versions are not supported.
  In these cases the ``get_text*`` methods will fall back to using ``ASTTokens``
  which incurs the usual setup cost the first time.
  If you want to avoid this, check ``supports_tokenless(node)`` before calling ``get_text*`` methods.
  Nr   rB   r   r   c                     t         |   ||       || _        | j                  t        | j                         d | _        y r   )rE   r%   rG   r   
_asttokens)r#   r   rB   r   rO   s       r$   r%   zASTText.__init__  s6    	G[(+DJzzTZZ(+/DOr&   c                     | j                   Dt        j                  | j                  | j                        | _         t        | j                          | j                   S r   )rG   rF   rA   r!   r   r   r\   s    r$   rB   zASTText.tree!  s<    zz99TZZ8djTZZ(::r&   c                     | j                   1t        | j                  | j                  | j                        | _         | j                   S )N)rB   r   )r   r@   r!   rB   r   r\   s    r$   	asttokenszASTText.asttokens(  s9    !
**yy>>do
 ??r&   r'   r(   c                    t        |      r0d| j                  j                  t        | j                              fS t        |dd      y|sJ t        |dg       }|st        |dd      }t        |dg       }|r|d   }n|}|j                  }t        |      }|r9||j                  k7  s'||j                  k7  rt        |d	d      rt        |      rd}n&| j                  j                  ||j                        }||f}	t        t        |j                        }
t        t        |j                        }| j                  j                  |
|      }|
|f}|	|fS )
zF
    Version of ``get_text_positions()`` that doesn't use tokens.
    r   rf   Nr   decorator_list
decoratorsnodesr   doc_node)r   r"   offset_to_linelenr!   getattrrf   r   
end_linenor   rl   rg   r
   r<   end_col_offset)r#   r'   r(   r   decorators_node
start_nodestart_linenoend_nodestart_col_offsetr1   r   r   r2   s                r$   _get_text_positions_tokenlessz%ASTText._get_text_positions_tokenless2  sU    
 T''66s4::GGGtXt$,K4/4JlD9o?GR8j a=jj$$LH ' DOO+j$/ ++99,
H]H]^+,E
 c8../J#x667N''55j.QN~
&C#:r&   c                     t        |dd      ryt        |      r| j                  ||      S | j                  j	                  ||      S )r*   _broken_positionsNr   )r   supports_tokenlessr   r   r-   r,   s      r$   r-   zASTText.get_text_positionsu  sF     t($/$//f==>>,,T6::r&   )Nr   )r6   r7   r8   r   r	   r   r   r    r%   r   rB   r@   r   r   r;   r   r<   r   r-   r   r   s   @r$   r   r     s    0# 0Xf-= 0PS 0fj 0 F     AA!%AU38_eCHo-.AF;W ;d ;uU3PS8_V[\_ad\dVeEe?f ;r&   r   )	arguments	Argumentswithitem._unsupported_tokenless_types   )      )argStarred)SliceExtSliceIndexkeywordr'   r   c                 >   t        |       j                  t        vxr t        | t        j
                         xrA | duxr; t        |       j                  dk(  xr! t        | j                        j                  dv  xr  dt        j                  j                         vS )av  
  Returns True if the Python version and the node (if given) are supported by
  the ``get_text*`` methods of ``ASTText`` without falling back to ``ASTTokens``.
  See ``ASTText`` for why this matters.

  The following cases are not supported:

    - PyPy
    - ``ast.arguments`` / ``astroid.Arguments``
    - ``ast.withitem``
    - ``astroid.Comprehension``
    - ``astroid.AssignName`` inside ``astroid.Arguments`` or ``astroid.ExceptHandler``
    - The following nodes in Python 3.8 only:
      - ``ast.arg``
      - ``ast.Starred``
      - ``ast.Slice``
      - ``ast.ExtSlice``
      - ``ast.Index``
      - ``ast.keyword``
  N
AssignName)r   ExceptHandlerpypy)
rq   r6   r   
isinstancerF   ASTparentsysversionlower)r'   s    r$   r   r     s    , 4j!== , tSWW%% 	
$d*: 	
J</ MT[[!**.LL	, ))+
+
r&   r   )&r9   rF   rd   r   r|   r   typingr   r   r   r   r   r	   r
   line_numbersr   utilr   r   r   r   r   r   r   r   r   r   r   ABCMetar   r@   r   r   r    __annotations__version_infor;   r   r=   r&   r$   <module>r      s     
  
   G G G %   0"CKK 0"fw wt{;k {;@ 1W eCHo VBQ6!"44"KKS D r&   