
    9;ikE                     H   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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 erddlmZmZ  G d d	e j:                  
      Z G d de      Z G d de      Z dZ!ejD                  dd dk(  r
e!dz  Z!e!dz  Z!ddZ#y)    N)Module)IterableIteratorListOptionalTupleAnycastTYPE_CHECKING   )LineNumbers)	Tokenmatch_tokenis_non_coding_tokenpatched_generate_tokens	last_stmtannotate_fstring_nodesgenerate_tokens	is_moduleis_stmt)AstNode	TokenInfoc                   T    e Zd ZdededdfdZej                  d        Zd	dZd	dZ	y)
ASTTextBasesource_textfilenamereturnNc                 V    || _         t        |      }|| _        t        |      | _        y N)	_filenamestr_textr   _line_numbers)selfr   r   s      P/home/rose/Desktop/poly/venv/lib/python3.12/site-packages/asttokens/asttokens.py__init__zASTTextBase.__init__"   s)    DN
 k"KDJ$[1D    c                     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$   nodepaddeds      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_range;   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.
    )r4   r"   r1   s        r%   get_textzASTTextBase.get_textI   s+     $$T62JE3::eS!!r'   )T)
__name__
__module____qualname__r!   r&   abcabstractmethodr.   r4   r6    r'   r%   r   r   !   s@    	2# 	2 	2 	2  	"r'   r   )	metaclassc                        e Zd ZdZd fd	Zd Zd Zed        Zed        Z	ed        Z
ed        Zd	 Zd
 Zd ZddZddZddZ	 ddZddZ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.
  c                    t         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?   r&   astparse_treer   r"   list_translate_tokens_tokensstartpos_token_offsetsmark_tokens)r$   r   rC   treer   tokenstok	__class__s          r%   r&   zASTTokens.__init__i   s    	)T#K:5:;1DJ ~tzz*f..v67DL 48<<@<C3<<<@Dzz
tzz"  As   9B=c                 >    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)rJ   rP   
visit_tree)r$   	root_noderP   s      r%   rJ   zASTTokens.mark_tokensz   s     (t	*r'   c              #      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#   r0   )	r$   original_tokensindexrM   tok_typetok_strr2   r3   lines	            r%   rF   zASTTokens._translate_tokens   s     
   7 HI
s,/)hT(GUCu$$33E!HeAhG$$33CFCFCE E J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.)rG   r[   s    r%   rL   zASTTokens.tokens   s     <<r'   c                     | j                   S )zTThe root of the AST tree passed into the constructor or parsed from the source code.)rD   r[   s    r%   rK   zASTTokens.tree   r]   r'   c                     | j                   S )zThe filename that was parsed)r    r[   s    r%   r   zASTTokens.filename   s     >>r'   c                 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   )rG   bisectrI   )r$   offsets     r%   get_token_from_offsetzASTTokens.get_token_from_offset   s)     <<d&9&96BQFGGr'   c                 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.
    )rd   r#   r0   r$   lineno
col_offsets      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.
    )ri   r#   from_utf8_colrf   s      r%   get_token_from_utf8zASTTokens.get_token_from_utf8   s(    
 >>&$"4"4"B"B6:"VWWr'   c                     |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   rV   r   rG   typer$   rM   include_extra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   rn   rp   s       r%   
prev_tokenzASTTokens.prev_token   rt   r'   c                    |}|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rq   )rv   rs   r   tokenISEOFro   )r$   start_tokenrW   rX   reversetadvances          r%   
find_tokenzASTTokens.find_token   sc     	A!(doodooG!Xw/AFF8K
!4
(a !Xw/AFF8KHr'   c              #      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)rangerV   r   rG   ro   )r$   first_token
last_tokenrq   rr   s        r%   token_rangezASTTokens.token_range   sQ      ;$$j&6&6&:;	1$,,q/2F2FGll1o <s   AA"A"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.
    rx   )r   r   r   )r$   r,   rq   s      r%   
get_tokenszASTTokens.get_tokens   s&     D,,doo][[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   ry   NEWLINE).0r}   s     r%   	<genexpr>z/ASTTokens.get_text_positions.<locals>.<genexpr>  s     S=Rk!U]]3=Rs   $&r   )hasattrr   r2   r   r3   anyr   r1   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)r7   r8   r9   __doc__r&   rJ   rF   propertyr\   rL   rK   r   rd   ri   rl   rs   rv   r   r   r   r.   __classcell__rN   s   @r%   r?   r?   U   s    &#"
+	E  
  
  
  
H	]X

  !&\r'   r?   c                   P     e Zd ZdZd fd	Zed        Zed        Zd Z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.
  c                     t         t        |   ||       || _        | j                  t	        | j                         d | _        y r   )rA   r   r&   rD   r   
_asttokens)r$   r   rK   r   rN   s       r%   r&   zASTText.__init__"  s8    	'4!+x8DJzzTZZ(DOr'   c                     | j                   Dt        j                  | j                  | j                        | _         t        | j                          | j                   S r   )rD   rB   rC   r"   r    r   r[   s    r%   rK   zASTText.tree,  s>     zz99TZZ8djTZZ(::r'   c                     | j                   1t        | j                  | j                  | j                        | _         | j                   S )N)rK   r   )r   r?   r"   rK   r    r[   s    r%   	asttokenszASTText.asttokens4  s;     !
**yy>>do
 ??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   rg   Nr   decorator_list
decoratorsnodesr   doc_node)r   r#   offset_to_linelenr"   getattrrg   r   
end_linenor   rk   rh   r
   intend_col_offset)r$   r,   r-   r   decorators_node
start_nodestart_linenoend_nodestart_col_offsetr2   r   r   r3   s                r%   _get_text_positions_tokenlessz%ASTText._get_text_positions_tokenless?  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_positions  sF     t($/$//f==>>,,T6::r'   )Nr   )r7   r8   r9   r   r&   r   rK   r   r   r.   r   r   s   @r%   r   r     sA        @D;r'   r   )	arguments	Argumentswithitem   )      )argStarred)SliceExtSliceIndexkeywordc                 >   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)
ro   r7   _unsupported_tokenless_types
isinstancerB   ASTparentsysversionlower)r,   s    r%   r   r     s    . 4j!== 
, tSWW%% 	
$d*: 	
J</ MT[[!**.LL
, ))+
+r'   r   )$r:   rB   rb   r   ry   r   typingr   r   r   r   r   r	   r
   r   line_numbersr   utilr   r   r   r   r   r   r   r   r   r   r   ABCMetar   r?   r   r   version_infor   r<   r'   r%   <module>r      s     
  
   V V V %  
 &1"CKK 1"h} }@~;k ~;F  F BQ6!"44"KK"r'   