
    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dS )	    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d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.
  FN	<unknown>c                    || _         |rt          j        ||          n|| _        t	          |t
          j                  r|                    d          }|| _        t          |          | _
        t          |                     |                    | _        d | j        D             | _        | j        r|                     | j                   d S d S )Nutf8c                     g | ]	}|j         
S  )startpos).0toks     3lib/python3.11/site-packages/asttokens/asttokens.py
<listcomp>z&ASTTokens.__init__.<locals>.<listcomp>>   s    @@@C3<@@@    )	_filenameastparse_tree
isinstancesixbinary_typedecode_textr   _line_numberslist_generate_tokens_tokens_token_offsetsmark_tokens)selfsource_textr   treefilenames        r   __init__zASTTokens.__init__-   s    DN5:D;111DJ
 +s// /&&v..kDJ$[11D --k::;;DL A@4<@@@Dz #
tz"""""# #r   c                 J    t          |                               |           dS )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	*****r   c              #   Z  K   t          t          j        t          j        |          j                            D ]o\  }}|\  }}}}}t          ||||||| j                            |d         |d                   | j                            |d         |d                             V  pdS )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T9J9J9S T TUU E E
s,/)hT(GUCu$33E!HeAhGG$33CFCFCCE E E E E EE Er   c                     | j         S )z,The source code passed into the constructor.)r   r&   s    r   r6   zASTTokens.text[        :r   c                     | j         S )zIThe list of tokens corresponding to the source code from the constructor.)r#   r>   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   r>   s    r   r(   zASTTokens.treee   r?   r   c                     | j         S )zThe filename that was parsed)r   r>   s    r   r)   zASTTokens.filenamej   s     >r   c                 R    | 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&96BBQFGGr   c                 ^    |                      | j                            ||                    S )z
    Returns the token containing the given (lineno, col_offset) position, or the preceeding token
    if the position is between tokens.
    )rG   r    r5   r&   lineno
col_offsets      r   	get_tokenzASTTokens.get_tokenv   s,     %%d&8&G&GPZ&[&[\\\r   c                 `    |                      || j                            ||                    S )zd
    Same as get_token(), but interprets col_offset as a UTF8 offset, which is what `ast` uses.
    )rL   r    from_utf8_colrI   s      r   get_token_from_utf8zASTTokens.get_token_from_utf8   s+     >>&$"4"B"B6:"V"VWWWr   c                     |j         dz   }|sCt          | j        |         j                  r$|dz  }t          | j        |         j                  $| 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   r7   r   r#   typer&   r   include_extrais       r   
next_tokenzASTTokens.next_token   `    
 		AA Q 455 	Q  Q 455 <?r   c                     |j         dz
  }|sCt          | j        |         j                  r$|dz  }t          | j        |         j                  $| 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   rQ   rS   s       r   
prev_tokenzASTTokens.prev_token   rW   r   c                     |}|r| j         n| j        }t          |||          sPt          j        |j                  s7 ||d          }t          |||          st          j        |j                  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rT   )rY   rV   r   tokenISEOFrR   )r&   start_tokenr8   r9   reversetadvances          r   
find_tokenzASTTokens.find_token   s     	A!(=doodoG!Xw// )AF8K8K )
'!4
(
(
(a !Xw// )AF8K8K )Hr   c              #      K   t          |j        |j        dz             D ]2}|st          | j        |         j                  s| j        |         V  3dS )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   r7   r   r#   rR   )r&   first_token
last_tokenrT   rU   s        r   token_rangezASTTokens.token_range   sj      
 K%z'7!';<<  	 1$,q/2FGG l1o r   c                 F    |                      |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[   )rf   rd   re   )r&   noderT   s      r   
get_tokenszASTTokens.get_tokens   s$    
 D,do][[[r   c                     t          |d          sdS |j        j        }t          d |                     |          D                       r| 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.
    rd   )r   r   c              3   J   K   | ]}t          |t          j                  V  d S )N)r   r\   NEWLINE)r   r`   s     r   	<genexpr>z+ASTTokens.get_text_range.<locals>.<genexpr>   s.      
H
HQ;q%-((
H
H
H
H
H
Hr   
r   r   )	hasattrrd   r   anyri   r   rfindre   endpos)r&   rh   r:   s      r   get_text_rangezASTTokens.get_text_range   s     4'' V%E

H
H$//$2G2G
H
H
HHH 3jtQ..2e4?)**r   c                 P    |                      |          \  }}| 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.
    )rs   r   )r&   rh   r:   r;   s       r   get_textzASTTokens.get_text   s,    
 $$T**JE3:eck""r   )FNr   )F)NF)__name__
__module____qualname____doc__r*   r%   r"   propertyr6   rA   r(   r)   rG   rL   rO   rV   rY   rb   rf   ri   rs   ru   r   r   r   r   r      s{        $# # # #.+ + +
E 
E 
E   8   8   8   8H H H] ] ]X X X	 	 	 		 	 	 	
 
 
 
   \ \ \ \+ + + # # # # #r   r   )r   rE   r\   r0   r2   r   	six.movesr   line_numbersr   utilr   r   r   r%   r	   objectr   r   r   r   <module>r      s    


    				 



       % % % % % % 9 9 9 9 9 9 9 9 9 9 # # # # # #t# t# t# t# t# t# t# t# t# t#r   