o
    <ZhC                     @   sf  U 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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mZmZmZ erdddlmZ G dd	 d	e
Zeeef ZejZd
d ZG dd de ddZ!dMddZ"dMddZ#dd Z$dd Z%dd Z&dNddZ'dd e j() D Z*dNddZ+d d e j() D Z,d!d e j() D h d"B Z-d#d$ Z.d%d& Z/d'd( Z0d)d* Z1d+d, Z2e j3fZ4eed-f e5d.< zd d/l6m7Z7 e4e7f7 Z4W n	 e8y   Y nw d0d1 Z9d2d3 Z:d4d5 Z;d6d7 Z<d8d9 Z=e> Z?d:d; Z@dNd<d=ZAd>d? ZBG d@dA dAZCdBdC ZDdDdE ZEdFdG ZFeddHdIdJ ZGdKdL ZHdS )O    N)ABCMeta)ModuleexprAST)	lru_cache)CallableDictIterableIteratorListOptionalTupleUnioncastAnyTYPE_CHECKINGType   )NodeNGc                   @   s   e Zd ZdZdZdZdS )EnhancedASTNr   )__name__
__module____qualname__Zfirst_tokenZ
last_tokenlineno r   r   E/var/www/html/lang_env/lib/python3.10/site-packages/asttokens/util.pyr   ,   s    r   c                 C   s   dt j|  t|df S )zRReturns a human-friendly representation of a token with the given type and string.z%s:%su)tokentok_namereprlstrip)tok_typestringr   r   r   
token_repr7   s   r#   c                   @   s   e Zd ZdZdd ZdS )Tokena  
  TokenInfo is an 8-tuple containing the same 5 fields as the tokens produced by the tokenize
  module, and 3 additional ones useful for this module:

  - [0] .type     Token type (see token.py)
  - [1] .string   Token (a string)
  - [2] .start    Starting (row, column) indices of the token (a 2-tuple of ints)
  - [3] .end      Ending (row, column) indices of the token (a 2-tuple of ints)
  - [4] .line     Original line (string)
  - [5] .index    Index of the token in the list of tokens that it belongs to.
  - [6] .startpos Starting character offset into the input text.
  - [7] .endpos   Ending character offset into the input text.
  c                 C   s   t | j| jS N)r#   typer"   selfr   r   r   __str__L   s   zToken.__str__N)r   r   r   __doc__r)   r   r   r   r   r$   >   s    r$   z0type string start end line index startpos endposc                 C   s   | j |ko|du p| j|kS )zVReturns true if token is of the given type and, if a string is given, has that string.N)r&   r"   r   r!   Ztok_strr   r   r   match_tokenQ   s   r,   c                 C   s>   t | ||stdt||t| | jd | jd d f dS )z
  Verifies that the given token is of the expected type. If tok_str is given, the token string
  is verified too. If the token doesn't match, raises an informative ValueError.
  z+Expected token %s, got %s on line %s col %sr   r   N)r,   
ValueErrorr#   strstartr+   r   r   r   expect_tokenW   s   r0   c                 C   s   | t jt jt jfv S )zS
  These are considered non-coding tokens, as they don't affect the syntax tree.
  )r   NLCOMMENTENCODING)
token_typer   r   r   is_non_coding_tokenc   s   r5   c                 C   s    t ttg tf t| jS )z;
  Generates standard library tokens for the given code.
  )tokenizegenerate_tokensr   r   r.   ioStringIOreadline)textr   r   r   r7   k   s    r7   c                 C   s   t | drtS tS )z
  Returns a function which yields all direct children of a AST node,
  skipping children that are singleton nodes.
  The function depends on whether ``node`` is from ``ast`` or from the ``astroid`` module.
  get_children)hasattriter_children_astroiditer_children_astnoder   r   r   iter_children_funcv   s   rB   Fc                 C   s   |st | rg S |  S r%   )is_joined_strr<   )rA   include_joined_strr   r   r   r>      s   r>   c              	   C   s<   h | ]\}}t |trt|tjtjtjtjtjfr|qS r   )	
isinstancer&   
issubclassastexpr_contextboolopoperatorunaryopcmpop.0ncr   r   r   	<setcomp>   s    
rQ   c                 c   sr    |s	t | r	d S t| tjr't| j| jD ]\}}|d ur!|V  |V  qd S t| D ]
}|jt	vr6|V  q,d S r%   )
rC   rE   rG   r   zipkeysvaluesiter_child_nodes	__class__
SINGLETONS)rA   rD   keyvaluechildr   r   r   r?      s   
r?   c                 C   *   h | ]\}}t |trt|tjr|qS r   )rE   r&   rF   rG   stmtrM   r   r   r   rQ      
    

c                 C   r[   r   )rE   r&   rF   rG   r   rM   r   r   r   rQ      r]   >   ZDelNameZ
AssignAttrConstZDelAttrZ
AssignNamec                 C      | j jtv S )z+Returns whether node is an expression node.)rV   r   expr_class_namesr@   r   r   r   is_expr      ra   c                 C   r_   )z)Returns whether node is a statement node.)rV   r   stmt_class_namesr@   r   r   r   is_stmt   rb   rd   c                 C      | j jdkS )z&Returns whether node is a module node.r   rV   r   r@   r   r   r   	is_module   rb   rg   c                 C   re   )zFReturns whether node is a JoinedStr node, used to represent f-strings.	JoinedStrrf   r@   r   r   r   rC      s   rC   c                 C   re   )zSReturns whether node is an `Expr` node, which is a statement that is an expression.Exprrf   r@   r   r   r   is_expr_stmt   rb   rj   .CONSTANT_CLASSES)r^   c                 C   s
   t | tS )z(Returns whether node is a Constant node.)rE   rk   r@   r   r   r   is_constant   s   
rl   c                 C   s   t | o| jtu S )z)Returns whether node is an Ellipsis node.)rl   rY   Ellipsisr@   r   r   r   is_ellipsis   s   rn   c                 C   re   )z2Returns whether node is a starred expression node.Starredrf   r@   r   r   r   
is_starred   rb   rp   c                 C   s0   | j jdv p| j jdkotttttj| jS )z?Returns whether node represents a slice, e.g. `1:2` in `x[1:2]`)SliceExtSlicer   )	rV   r   anymapis_slicer   rG   r   eltsr@   r   r   r   ru      s   ru   c                 C   sB   | j jdko t| tj o | j| j  u o| j  u od u S   S )Nrq   )rV   r   rE   rG   r   lowerupperstepr@   r   r   r   is_empty_astroid_slice   s    rz   c                 C   s   |sdd }t | }t }d}| dtfg}|r^| \}}}	|	tu rQ||vs(J || |||\}
}||||f t|}||D ]}||||
tf qDn|||tt	t
 |	}|s|S )a5  
  Scans the tree under the node depth-first using an explicit stack. It avoids implicit recursion
  via the function call stack to avoid hitting 'maximum recursion depth exceeded' error.

  It calls ``previsit()`` and ``postvisit()`` as follows:

  * ``previsit(node, par_value)`` - should return ``(par_value, value)``
        ``par_value`` is as returned from ``previsit()`` of the parent.

  * ``postvisit(node, par_value, value)`` - should return ``value``
        ``par_value`` is as returned from ``previsit()`` of the parent, and ``value`` is as
        returned from ``previsit()`` of this node itself. The return ``value`` is ignored except
        the one for the root node, which is returned from the overall ``visit_tree()`` call.

  For the initial node, ``par_value`` is None. ``postvisit`` may be None.
  c                 S   s   d S r%   r   )rA   pvaluerY   r   r   r   <lambda>  s    zvisit_tree.<locals>.<lambda>N)rB   set	_PREVISITpopaddappendleninsertr   r   r$   )rA   ZprevisitZ	postvisititer_childrendoneretstackcurrentZ	par_valuerY   r{   Z
post_valueinsrO   r   r   r   
visit_tree   s(   
r   c                 c   sn    t | }t }| g}|r5| }||vsJ || |V  t|}|||D ]}||| q(|sdS dS )a1  
  Recursively yield all descendant nodes in the tree starting at ``node`` (including ``node``
  itself), using depth-first pre-order traversal (yieling parents before their children).

  This is similar to ``ast.walk()``, but with a different order, and it works for both ``ast`` and
  ``astroid`` trees. Also, as ``iter_children()``, it skips singleton nodes generated by ``ast``.

  By default, ``JoinedStr`` (f-string) nodes and their contents are skipped
  because they previously couldn't be handled. Set ``include_joined_str`` to True to include them.
  N)rB   r}   r   r   r   r   )rA   rD   r   r   r   r   r   rP   r   r   r   walk'  s   
r   c                 C   sX   d}g }t |D ]\}}}|| ||  || |}q|| |d  d|S )ah  
  Replaces multiple slices of text with new values. This is a convenience method for making code
  modifications of ranges e.g. as identified by ``ASTTokens.get_text_range(node)``. Replacements is
  an iterable of ``(start, end, new_text)`` tuples.

  For example, ``replace("this is a test", [(0, 4, "X"), (8, 9, "THE")])`` produces
  ``"X is THE test"``.
  r   N )sortedr   join)r;   Zreplacementsppartsr/   endnew_textr   r   r   replaceD  s   


r   c                   @   s    e Zd ZdZdd Zdd ZdS )NodeMethodsz[
  Helper to get `visit_{node_type}` methods given a node's class and cache the results.
  c                 C   s
   i | _ d S r%   )_cacher'   r   r   r   __init__\  s   
zNodeMethods.__init__c                 C   s:   | j |}|sd|j  }t|||j}|| j |< |S )z
    Using the lowercase name of the class as node_type, returns `obj.visit_{node_type}`,
    or `obj.visit_default` if the type-specific method is not found.
    visit_)r   getr   rw   getattrZvisit_default)r(   objclsmethodnamer   r   r   r   `  s   
zNodeMethods.getN)r   r   r   r*   r   r   r   r   r   r   r   X  s    r   c                 c   s|    g }| D ],}|j tjtjtjfv r"|r|d j|jkr"|| qt|D ]}|V  q&g }|V  qt|D ]}|V  q6dS )a  
    Fixes tokens yielded by `tokenize.generate_tokens` to handle more non-ASCII characters in identifiers.
    Workaround for https://github.com/python/cpython/issues/68382.
    Should only be used when tokenizing a string that is known to be valid syntax,
    because it assumes that error tokens are not actually errors.
    Combines groups of consecutive NAME, NUMBER, and/or ERRORTOKEN tokens into a single NAME token.
    N)	r&   r6   NAME
ERRORTOKENNUMBERr   r/   r   combine_tokens)Zoriginal_tokensgrouptokZcombined_tokenr   r   r   patched_generate_tokensn  s   	r   c                 C   sf   t dd | D rtdd | D dkr| S tjtjddd | D | d j| d	 j| d jd
gS )Nc                 s   s    | ]	}|j tjkV  qd S r%   )r&   r6   r   rN   r   r   r   r   	<genexpr>  s    z!combine_tokens.<locals>.<genexpr>c                 S   s   h | ]}|j qS r   )liner   r   r   r   rQ     s    z!combine_tokens.<locals>.<setcomp>r   r   c                 s   s    | ]}|j V  qd S r%   )r"   )rN   tr   r   r   r     s    r   r   )r&   r"   r/   r   r   )	rs   r   r6   	TokenInfor   r   r/   r   r   )r   r   r   r   r     s   (r   c                 C   s*   dd t | | D }|rt|d S | S )zq
  If the given AST node contains multiple statements, return the last one.
  Otherwise, just return the node.
  c                 S   s&   g | ]}t |st|jd v r|qS ))excepthandlerExceptHandler
match_caseZ	MatchCaseZ	TryExceptZ
TryFinally)rd   r&   r   )rN   rZ   r   r   r   
<listcomp>  s    zlast_stmt.<locals>.<listcomp>r   )rB   	last_stmt)rA   Zchild_stmtsr   r   r   r     s   
r   )maxsizec                     sb   d t  } dd t | D }dd |D }tt|t|k}t fdd|D }|o0|S )a  
  The positions attached to nodes inside f-string FormattedValues have some bugs
  that were fixed in Python 3.9.7 in https://github.com/python/cpython/pull/27729.
  This checks for those bugs more concretely without relying on the Python version.
  Specifically this checks:
   - Values with a format spec or conversion
   - Repeated (i.e. identical-looking) expressions
   - f-strings implicitly concatenated over multiple lines.
   - Multiline, triple-quoted f-strings.
  z(
    f"a {b}{b} c {d!r} e {f:g} h {i:{j}} k {l:{m:n}}"
    f"a {b}{b} c {d!r} e {f:g} h {i:{j}} k {l:{m:n}}"
    f"{x + y + z} {x} {y} {z} {z} {z!a} {z:z}"
    f'''
    {s} {t}
    {u} {v}
    '''
  )c                 S   s   g | ]
}t |tjr|qS r   )rE   rG   NamerN   rA   r   r   r   r     s    z*fstring_positions_work.<locals>.<listcomp>c                 S   s   g | ]}|j |jfqS r   )r   
col_offsetr   r   r   r   r     s    c                 3   s"    | ]}t  ||jkV  qd S r%   )rG   get_source_segmentidr   sourcer   r   r     s
    
z)fstring_positions_work.<locals>.<genexpr>)rG   parser   r   r}   all)treeZ
name_nodesZname_positionsZpositions_are_uniqueZcorrect_source_segmentsr   r   r   fstring_positions_work  s   
	r   c                 C   s   t jdkrdS t| ddD ]6}t|tjsq|jD ])}t|dd t|tjrBt	 s8t|j
D ]}t|dd q/|jrBt|jdd qqdS )zy
  Add a special attribute `_broken_positions` to nodes inside f-strings
  if the lineno/col_offset cannot be trusted.
  )      NT)rD   Z_broken_positions)sysversion_infor   rE   rG   rh   rT   setattrFormattedValuer   rY   format_spec)r   Z	joinedstrpartrZ   r   r   r   annotate_fstring_nodes  s    

r   r%   )F)IrG   collectionsr8   r   r   r6   abcr   r   r   r   	functoolsr   typingr   r   r	   r
   r   r   r   r   r   r   r   r   Zastroid_compatr   r   ZAstNoder   r#   
namedtupler$   r,   r0   r5   r7   rB   r>   __dict__itemsrW   r?   rc   r`   ra   rd   rg   rC   rj   Constantrk   __annotations__Zastroidr^   ImportErrorrl   rn   rp   ru   rz   objectr~   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sr   
8






+
