
    7 fI                        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Zd dl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         e j!        e"                    Z# G d de#e"          Z$ G d de#e"          Z%dZ&ej'        dd         dk    r"e&dz  Z&ej'        dd         dk    r
e&dz  Z&e&dz  Z&ddZ(dS )    N)Module)IterableIteratorListOptionalTupleAnycastTYPE_CHECKING)xrange   )LineNumbers)	Tokenmatch_tokenis_non_coding_tokenpatched_generate_tokens	last_stmtannotate_fstring_nodesgenerate_tokens	is_moduleis_stmt)AstNode	TokenInfoc                   D    e Zd Zd Zej        d             ZddZddZdS )ASTTextBasec                 r    || _         t          j        |          }|| _        t	          |          | _        d S N)	_filenamesixensure_text_textr   _line_numbers)selfsource_textfilenames      I/var/www/equiseq/venv/lib/python3.11/site-packages/asttokens/asttokens.py__init__zASTTextBase.__init__%   s9    
 DN
 /+..KDJ$[1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_positions4   s
     r(   Tc                 p    |                      ||          \  }} | 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_rangeB   sG     ((v66JE3'd'/'d'- r(   c                 R    |                      ||          \  }}| 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.
    )r5   r!   r2   s        r&   get_textzASTTextBase.get_textP   s.     $$T622JE3:eSj!!r(   N)T)	__name__
__module____qualname__r'   abcabstractmethodr/   r5   r7    r(   r&   r   r   $   sj        2 2 2      	" 	" 	" 	" 	" 	"r(   r   c                        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.
  FN	<unknown>c                    t          t          |                               ||           |rt          j        ||          n|| _        |t          | j                  }t          | 	                    |                    | _
        d | j
        D             | _        | j        r|                     | j                   d S d S )Nc                     g | ]	}|j         
S r=   )startpos).0toks     r&   
<listcomp>z&ASTTokens.__init__.<locals>.<listcomp>   s    @@@C3<@@@r(   )superr?   r'   astparse_treer   r!   list_translate_tokens_tokens_token_offsetsmark_tokens)r#   r$   rI   treer%   tokens	__class__s         r&   r'   zASTTokens.__init__p   s    
 
)T##K:::5:D;111DJ ~tz**f..v6677DL A@4<@@@Dz #
tz"""""# #r(   c                 N    ddl m}  ||                               |           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.
    r   )
MarkTokensN)rO   rT   
visit_tree)r#   	root_noderT   s      r&   rO   zASTTokens.mark_tokens   s:     ('''''Jt	*****r(   c              #   "  K   t          t          |                    D ]o\  }}|\  }}}}}t          ||||||| j                            |d         |d                   | j                            |d         |d                             V  pdS )zS
    Translates the given standard library tokens into our own representation.
    r   r   N)	enumerater   r   r"   r1   )	r#   original_tokensindexrE   tok_typetok_strr3   r4   lines	            r&   rL   zASTTokens._translate_tokens   s      
   7 H HII 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&   textzASTTokens.text        :r(   c                     | j         S )zIThe list of tokens corresponding to the source code from the constructor.)rM   r_   s    r&   rQ   zASTTokens.tokens   s     <r(   c                     | j         S )zTThe root of the AST tree passed into the constructor or parsed from the source code.)rJ   r_   s    r&   rP   zASTTokens.tree   ra   r(   c                     | j         S )zThe filename that was parsed)r   r_   s    r&   r%   zASTTokens.filename   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   )rM   bisectrN   )r#   offsets     r&   get_token_from_offsetzASTTokens.get_token_from_offset   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.
    )rh   r"   r1   r#   lineno
col_offsets      r&   	get_tokenzASTTokens.get_token   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.
    )rm   r"   from_utf8_colrj   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   rZ   r   rM   typer#   rE   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   rr   rt   s       r&   
prev_tokenzASTTokens.prev_token   rx   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ru   )rz   rw   r   tokenISEOFrs   )r#   start_tokenr[   r\   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   rZ   r   rM   rs   )r#   first_token
last_tokenru   rv   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|   )r   r   r   )r#   r-   ru   s      r&   
get_tokenszASTTokens.get_tokens   s$     D,do][[[r(   c                     t          |d          sdS |j        j        }|j        j        }|r6t          d |                     |          D                       r
|d         df}||fS )r*   r   r   r   r   c              3   J   K   | ]}t          |t          j                  V  d S r   )r   r}   NEWLINE)rD   r   s     r&   	<genexpr>z/ASTTokens.get_text_positions.<locals>.<genexpr>  s.      SSk!U]33SSSSSSr(   r   )hasattrr   r3   r   r4   anyr   r2   s        r&   r/   zASTTokens.get_text_positions  sy     4'' ^"E
/
C #SST__T=R=RSSSSS Qxme#:r(   )FNr@   N)F)NF)r8   r9   r:   __doc__r'   rO   rL   propertyr`   rQ   rP   r%   rh   rm   rp   rw   rz   r   r   r   r/   __classcell__rR   s   @r&   r?   r?   \   s        &# # # # # #(
+ 
+ 
+	E 	E 	E   8
   8
   8
   8
H H H	] 	] 	]X X X
 
 
 

 
 
 
     !&   \ \ \ \      r(   r?   c                   \     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.
  Nr@   c                     t          t          |                               ||           || _        | j        t	          | j                   d | _        d S r   )rG   r   r'   rJ   r   
_asttokens)r#   r$   rP   r%   rR   s       r&   r'   zASTText.__init__,  sO    
 
'4!!+x888DJzTZ(((DOOOr(   c                     | j         8t          j        | j        | j                  | _         t          | j                    | j         S r   )rJ   rH   rI   r!   r   r   r_   s    r&   rP   zASTText.tree9  s;     z9TZ88djTZ(((:r(   c                 j    | j         &t          | j        | j        | j                  | _         | j         S )N)rP   r%   )r   r?   r!   rP   r   r_   s    r&   	asttokenszASTText.asttokensA  s;     !
*y>  do
 ?r(   c                    t           j        dd         dk     rt          d          t          |          r.d| j                            t          | j                            fS t          |dd          dS |sJ t          |dg           }|s"t          |d	d          }t          |d
g           }|r	|d         }n|}|j	        }t          |          }|r9||j	        k    s+||j        k    r#t          |dd          rt          |          rd}n | j                            ||j                  }||f}	t          t           |j                  }
t          t           |j                  }| j                            |
|          }|
|f}|	|fS )zF
    Version of ``get_text_positions()`` that doesn't use tokens.
    N         zPThis method should only be called internally after checking supports_tokenless()r   rk   r   decorator_list
decoratorsnodesr   doc_node)sysversion_infoAssertionErrorr   r"   offset_to_linelenr!   getattrrk   r   
end_linenor   ro   rl   r
   intend_col_offset)r#   r-   r.   r   decorators_node
start_nodestart_linenoend_nodestart_col_offsetr3   r   r   r4   s                r&   _get_text_positions_tokenlessz%ASTText._get_text_positions_tokenlessL  s   
 f$$mnnn H
 T'66s4:GGGGtXt$$,^KK4/44J 9lD99o?GR88j  a=jjj$LH  _'' DO++j$// , ,
 +99,
H]^^+,E
 c8.//J#x677N'55j.QQN~
&C#:r(   c                     t          |dd          rdS t          |          r|                     ||          S | j                            ||          S )r*   _broken_positionsNr   )r   supports_tokenlessr   r   r/   r,   s      r&   r/   zASTText.get_text_positions  s\     t($// ^$ >//f===>,,T6:::r(   )Nr@   )r8   r9   r:   r   r'   r   rP   r   r   r/   r   r   s   @r&   r   r     s                 8   8D D DL; ; ; ; ; ; ;r(   r   r=   r   r   )	arguments	Argumentswithitem)argStarred)SliceExtSliceIndexkeywordc                 N   t          |           j        t          vot          | t          j                   o6| duo2t          |           j        dk    ot          | j                  j        dv  o7t          j        dd         dk    odt          j	        
                                vS )a  
  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:

    - Python 3.7 and earlier
    - 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   ExceptHandlerr   r   pypy)rs   r8   _unsupported_tokenless_types
isinstancerH   ASTparentr   r   versionlower)r-   s    r&   r   r     s    0 4jj!== , tSW%%% 	
$d*: 	
JJ</ MT[!!*.LL, 
2A2
&
(, ))++
+r(   r   ))r;   rH   rf   r   r}   r   typingr   r   r   r   r   r	   r
   r   r   	six.movesr   line_numbersr   utilr   r   r   r   r   r   r   r   r   r   r   with_metaclassABCMetaobjectr   r?   r   r   r   r   r=   r(   r&   <module>r      s^   


 



  



        V V V V V V V V V V V V V V V V V V V V 



       % % % % % %                     
  '&&&&&&&&5" 5" 5" 5" 5"$#$S[&99 5" 5" 5"p@ @ @ @ @V @ @ @FE; E; E; E; E;k6 E; E; E;R  " BQB6!!"HHbqbV## $66  $MM $ $ $ $ $ $r(   