o
    <Zh`E                     @   s  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rTddlmZmZ G dd	 d	e jd
ZG dd deZG dd deZ dZ!ej"dd dkre!d7 Z!e!d7 Z!dddZ#dS )    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                   @   sD   e Zd ZdededdfddZejdd Zdd
dZdddZ	dS )ASTTextBasesource_textfilenamereturnNc                 C   s"   || _ t|}|| _t|| _d S N)	_filenamestr_textr   _line_numbers)selfr   r    r"   J/var/www/html/lang_env/lib/python3.10/site-packages/asttokens/asttokens.py__init__"   s   zASTTextBase.__init__c                 C   s   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paddedr"   r"   r#   get_text_positions-   s   zASTTextBase.get_text_positionsTc                 C   s(   |  ||\}}| 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endr"   r"   r#   get_text_range;   s   

zASTTextBase.get_text_rangec                 C   s   |  ||\}}| 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.
    )r/   r   r,   r"   r"   r#   get_textI   s   zASTTextBase.get_text)T)
__name__
__module____qualname__r   r$   abcabstractmethodr*   r/   r0   r"   r"   r"   r#   r   !   s    

r   )	metaclassc                       s   e Zd ZdZd% fdd	Zdd Zd	d
 Zedd Zedd Z	edd Z
edd Zdd Zdd Zdd Zd&ddZd&ddZd'ddZ	d&dd Zd&d!d"Zd#d$ Z  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                    sv   t t| || |rt||n|| _|d u rt| j}t| 	|| _
dd | j
D | _| jr9| | j d S d S )Nc                 S   s   g | ]}|j qS r"   )startpos).0tokr"   r"   r#   
<listcomp>u   s    z&ASTTokens.__init__.<locals>.<listcomp>)superr7   r$   astparse_treer   r   list_translate_tokens_tokens_token_offsetsmark_tokens)r!   r   r?   treer   tokens	__class__r"   r#   r$   i   s   
zASTTokens.__init__c                 C   s   ddl m} || | dS )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)rE   rJ   Z
visit_tree)r!   Z	root_noderJ   r"   r"   r#   rE   z   s   	zASTTokens.mark_tokensc           	      c   sf    t t|D ])\}}|\}}}}}t||||||| j|d |d | j|d |d V  qdS )zS
    Translates the given standard library tokens into our own representation.
    r   r   N)	enumerater   r   r    r+   )	r!   Zoriginal_tokensindexr;   tok_typetok_strr-   r.   liner"   r"   r#   rB      s   zASTTokens._translate_tokensc                 C      | j S )z,The source code passed into the constructor.)r   r!   r"   r"   r#   text      zASTTokens.textc                 C   rP   )zIThe list of tokens corresponding to the source code from the constructor.)rC   rQ   r"   r"   r#   rG      rS   zASTTokens.tokensc                 C   rP   )zTThe root of the AST tree passed into the constructor or parsed from the source code.)r@   rQ   r"   r"   r#   rF      rS   zASTTokens.treec                 C   rP   )zThe filename that was parsed)r   rQ   r"   r"   r#   r      rS   zASTTokens.filenamec                 C   s   | j t| j|d  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   )rC   bisectrD   )r!   offsetr"   r"   r#   get_token_from_offset   s   zASTTokens.get_token_from_offsetc                 C   s   |  | j||S )z
    Returns the token containing the given (lineno, col_offset) position, or the preceeding token
    if the position is between tokens.
    )rV   r    r+   r!   lineno
col_offsetr"   r"   r#   	get_token   s   	zASTTokens.get_tokenc                 C   s   |  || j||S )zd
    Same as get_token(), but interprets col_offset as a UTF8 offset, which is what `ast` uses.
    )rZ   r    from_utf8_colrW   r"   r"   r#   get_token_from_utf8   s   zASTTokens.get_token_from_utf8c                 C   s@   |j d }|st| j| jr|d7 }t| j| js| 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   rL   r   rC   typer!   r;   include_extrair"   r"   r#   
next_token      

zASTTokens.next_tokenc                 C   s@   |j d }|st| j| jr|d8 }t| j| js| 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   r]   r_   r"   r"   r#   
prev_token   rc   zASTTokens.prev_tokenc                 C   sT   |}|r| j n| j}t|||s(t|js(||dd}t|||s(t|jr|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r`   )rd   rb   r   tokenISEOFr^   )r!   Zstart_tokenrM   rN   reversetadvancer"   r"   r#   
find_token   s   zASTTokens.find_tokenc                 c   s>    t |j|jd D ]}|st| j| js| j| V  q
d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)rangerL   r   rC   r^   )r!   first_token
last_tokenr`   ra   r"   r"   r#   token_range   s   
zASTTokens.token_rangec                 C   s   | j |j|j|d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.
    re   )ro   rm   rn   )r!   r(   r`   r"   r"   r#   
get_tokens   s   zASTTokens.get_tokensc                 C   sN   t |dsdS |jj}|jj}|r#tdd | |D r#|d df}||fS )r%   rm   r   r   rr   c                 s   s    | ]	}t |tjV  qd S r   )r   rf   NEWLINE)r:   ri   r"   r"   r#   	<genexpr>  s    z/ASTTokens.get_text_positions.<locals>.<genexpr>r   )hasattrrm   r-   rn   r.   anyrp   r,   r"   r"   r#   r*      s   
zASTTokens.get_text_positions)FNr8   N)F)NF)r1   r2   r3   __doc__r$   rE   rB   propertyrR   rG   rF   r   rV   rZ   r\   rb   rd   rk   ro   rp   r*   __classcell__r"   r"   rH   r#   r7   U   s.    








r7   c                       sJ   e Zd ZdZd fdd	Zedd Zedd	 Zd
d Zdd Z	  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.
  Nr8   c                    s6   t t| || || _| jd urt| j d | _d S r   )r=   rz   r$   r@   r   
_asttokens)r!   r   rF   r   rH   r"   r#   r$   "  s
   


zASTText.__init__c                 C   s,   | j d u rt| j| j| _ t| j  | j S r   )r@   r>   r?   r   r   r   rQ   r"   r"   r#   rF   ,  s   

zASTText.treec                 C   s&   | j d u rt| j| j| jd| _ | j S )N)rF   r   )r{   r7   r   rF   r   rQ   r"   r"   r#   	asttokens4  s   
zASTText.asttokensc                 C   s  t |rd| jt| jfS t|dddu rdS |sJ t|dg }|s1t|dd}t|dg }|r8|d }n|}|j}t|}|rZ||jksW||jkrZt|d	drZt	|rZd}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.
    rr   rX   Nrq   decorator_list
decoratorsnodesr   Zdoc_node)r   r    Zoffset_to_linelenr   getattrrX   r   
end_linenor   r[   rY   r	   intend_col_offset)r!   r(   r)   r~   Zdecorators_nodeZ
start_nodeZstart_linenoZend_nodeZstart_col_offsetr-   r   r   r.   r"   r"   r#   _get_text_positions_tokenless?  s:   



z%ASTText._get_text_positions_tokenlessc                 C   s2   t |ddrdS t|r| ||S | j||S )r%   Z_broken_positionsNrq   )r   supports_tokenlessr   r|   r*   r'   r"   r"   r#   r*     s
   zASTText.get_text_positions)Nr8   )r1   r2   r3   rw   r$   rx   rF   r|   r   r*   ry   r"   r"   rH   r#   rz     s    



Brz   )	arguments	Argumentswithitem   )      )argStarred)SliceExtSliceIndexkeywordc                 C   sR   t | jtvo(t| tj o | duo t | jdko t | jjdv  o(dt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``
  NZ
AssignName)r   ExceptHandlerpypy)
r^   r1   _unsupported_tokenless_types
isinstancer>   ASTparentsysversionlower)r(   r"   r"   r#   r     s   
r   r   )$r4   r>   rT   r   rf   r   typingr   r   r   r   r   r   r	   r
   line_numbersr   utilr   r   r   r   r   r   r   r   r   r   r   ABCMetar   r7   rz   r   version_infor   r"   r"   r"   r#   <module>   s*   (,4 A 