
    c*b`                          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	 ddl
mZmZmZ ddlmZ  G d de      Zy)	    N)xrange   )LineNumbers)Tokenmatch_tokenis_non_coding_token)
MarkTokensc                       e Zd ZdZd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d Zy)	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.
  Nc                    || _         |rt        j                  ||      n|| _        t	        |t
        j                        r|j                  d      }|| _        t        |      | _
        t        | j                  |            | _        | j                  D cg c]  }|j                   c}| _        | j                  r| j!                  | j                         y y c c}w )Nutf8)	_filenameastparse_tree
isinstancesixbinary_typedecode_textr   _line_numberslist_generate_tokens_tokensstartpos_token_offsetsmark_tokens)selfsource_textr   treefilenametoks         3lib/python3.12/site-packages/asttokens/asttokens.py__init__zASTTokens.__init__-   s    DN5:;1DJ
 +s/&&v.kDJ$[1D --k:;DL 48<<@<C3<<<@Dzz
tzz"  As   Cc                 8    t        |       j                  |       y)ap  
    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.
    N)r	   
visit_tree)r   	root_nodes     r#   r   zASTTokens.mark_tokensD   s     t	*    c              #   J  K   t        t        j                  t        j                  |      j
                              D ]a  \  }}|\  }}}}}t        ||||||| j                  j                  |d   |d         | j                  j                  |d   |d                c yw)z.
    Generates tokens for the given code.
    r   r   N)		enumeratetokenizegenerate_tokensioStringIOreadliner   r   line_to_offset)	r   textindexr"   tok_typetok_strstartendlines	            r#   r   zASTTokens._generate_tokensO   s        8 8T9J9S9S TU
s,/)hT(GUCu$$33E!HeAhG$$33CFCFCE E Vs   B!B#c                     | j                   S )z,The source code passed into the constructor.)r   r   s    r#   r1   zASTTokens.text[        ::r(   c                     | j                   S )zIThe list of tokens corresponding to the source code from the constructor.)r   r9   s    r#   tokenszASTTokens.tokens`   s     <<r(   c                     | j                   S )zTThe root of the AST tree passed into the constructor or parsed from the source code.)r   r9   s    r#   r    zASTTokens.treee   r:   r(   c                     | j                   S )zThe filename that was parsed)r   r9   s    r#   r!   zASTTokens.filenamej   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   )r   bisectr   )r   offsets     r#   get_token_from_offsetzASTTokens.get_token_from_offseto   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.
    )rB   r   r0   r   lineno
col_offsets      r#   	get_tokenzASTTokens.get_tokenv   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.
    )rG   r   from_utf8_colrD   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   r2   r   r   typer   r"   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   rL   rN   s       r#   
prev_tokenzASTTokens.prev_token   rR   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rO   )rT   rQ   r   tokenISEOFrM   )r   start_tokenr3   r4   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)r   r2   r   r   rM   )r   first_token
last_tokenrO   rP   s        r#   token_rangezASTTokens.token_range   sQ     
 K%%z'7'7!';<	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.
    rV   )ra   r_   r`   )r   noderO   s      r#   
get_tokenszASTTokens.get_tokens   s&    
 D,,doo][[r(   c                     t        |d      sy|j                  j                  }t        d | j	                  |      D              r | j
                  j                  dd|      dz   }||j                  j                  fS )z
    After mark_tokens() has been called, 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.
    r_   )r   r   c              3   P   K   | ]  }t        |t        j                           y w)N)r   rW   NEWLINE).0r[   s     r#   	<genexpr>z+ASTTokens.get_text_range.<locals>.<genexpr>   s     
H2GQ;q%--(2Gs   $&
r   r   )	hasattrr_   r   anyrd   r   rfindr`   endpos)r   rc   r5   s      r#   get_text_rangezASTTokens.get_text_range   sk     4'%%E

H$//$2G
HHjjtQ.2e4??))**r(   c                 H    | j                  |      \  }}| j                  || S )z
    After mark_tokens() has been called, returns the text corresponding to the given node. Returns
    '' for nodes (like `Load`) that don't correspond to any particular text.
    )ro   r   )r   rc   r5   r6   s       r#   get_textzASTTokens.get_text   s)    
 $$T*JE3::ec""r(   )FNz	<unknown>)F)NF)__name__
__module____qualname____doc__r$   r   r   propertyr1   r<   r    r!   rB   rG   rJ   rQ   rT   r]   ra   rd   ro   rq    r(   r#   r   r      s    $#.+
E        H]X		
\+ #r(   r   )r   r@   rW   r+   r-   r   	six.movesr   line_numbersr   utilr   r   r   r   r	   objectr   rw   r(   r#   <module>r|      s6        	 
  % 9 9 #t# t#r(   