/ lib / asttokens / util.pyc
util.pyc
  1  o

  2  -��c�D�@s�ddlZddlZddlZddlZddlZddlZddlmZddlmZm	Z	m
  3  Z
  4  ddlmZm
Z
mZmZmZmZmZmZmZmZmZddlmZerzddlmZGdd	�d	e
  5  �ZeeefZejdd
  6  krweeeeeefeeefefZ nej Z dd�Z!Gd
d�de�"dd��Z#ejdkr�ej$Z%nGdd�d�Z%dNdd�Z&dNdd�Z'ejdkr�dd�Z(ndd�Z(dd�Z)dd�Z*dd �Z+d!d"�eej,�D�Z-d#d$�Z.d%d"�eej,�D�Z/d&d"�eej,�D�hd'�BZ0d(d)�Z1d*d+�Z2d,d-�Z3d.d/�Z4d0d1�Z5d2d3�Z6d4d5�Z7e8�Z9d6d7�Z:d8d9�Z;d:d;�Z<Gd<d=�d=e8�Z=ejdd
  7  k�r$d>d?�Z>nd@d?�Z>dAdB�Z?dCdD�Z@ejdd
  8  �dEk�rOddFlAmBZBeBddG�dHdI��ZCdJdK�ZDdSdLdI�ZCdMdK�ZDdS)O�N)�ABCMeta)�Module�expr�AST)�Callable�Dict�Iterable�Iterator�List�Optional�Tuple�Union�cast�Any�
TYPE_CHECKING)�	iteritems�)�NodeNGc@seZdZdZdZdZdS)�EnhancedASTNr)�__name__�
  9  __module__�__qualname__�first_token�
 10  last_token�lineno�rr��C:\Users\Jacks.GUTTSPC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\asttokens\util.pyr!sr�cCsdtj|t|��d�fS)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�stringrrr�
 11  token_repr/sr%c@seZdZdZdd�ZdS)�Tokena�
 12    TokenInfo is an 8-tuple containing the same 5 fields as the tokens produced by the tokenize
 13    module, and 3 additional ones useful for this module:
 14  
 15    - [0] .type     Token type (see token.py)
 16    - [1] .string   Token (a string)
 17    - [2] .start    Starting (row, column) indices of the token (a 2-tuple of ints)
 18    - [3] .end      Ending (row, column) indices of the token (a 2-tuple of ints)
 19    - [4] .line     Original line (string)
 20    - [5] .index    Index of the token in the list of tokens that it belongs to.
 21    - [6] .startpos Starting character offset into the input text.
 22    - [7] .endpos   Ending character offset into the input text.
 23    cCst|j|j�S�N)r%�typer$��selfrrr�__str__Dsz
Token.__str__N)rrr�__doc__r+rrrrr&6s
r&z0type string start end line index startpos endpos)��c@seZdZe�ZdS)�AstConstantN)rrr�object�valuerrrrr/Ls
 24  r/cCs|j|ko
|dup
|j|kS)zVReturns true if token is of the given type and, if a string is given, has that string.N)r(r$�rr#�tok_strrrr�match_tokenPsr4cCs>t|||�stdt||�t|�|jd|jddf��dS)z�
 25    Verifies that the given token is of the expected type. If tok_str is given, the token string
 26    is verified too. If the token doesn't match, raises an informative ValueError.
 27    z+Expected token %s, got %s on line %s col %srrN)r4�
 28  ValueErrorr%�str�startr2rrr�expect_tokenVs��r8)r-�cCs|tjtjtjfvS�zW
 29      These are considered non-coding tokens, as they don't affect the syntax tree.
 30      )r�NL�COMMENT�ENCODING��
 31  token_typerrr�is_non_coding_tokendsr@cCs
 32  |tjkSr:)r�N_TOKENSr>rrrr@ks
 33  cCs t�ttgtft�|�j��S)z;
 34    Generates standard library tokens for the given code.
 35    )�tokenize�generate_tokensrrr6�io�StringIO�readline)�textrrrrCss rCcCst|d�rtStS)z�
 36    Returns a function which yields all direct children of a AST node,
 37    skipping children that are singleton nodes.
 38    The function depends on whether ``node`` is from ``ast`` or from the ``astroid`` module.
 39    �get_children)�hasattr�iter_children_astroid�iter_children_ast��noderrr�iter_children_func~srNcCst|�rgS|��Sr')�
is_joined_strrHrLrrrrJ�srJc	Cs<h|]\}}t|t�rt|tjtjtjtjtjf�r|�qSr)	�
 40  isinstancer(�
 41  issubclass�ast�expr_context�boolop�operator�unaryop�cmpop��.0�n�crrr�	<setcomp>�s
 42  �r\ccsn�t|�rdSt|tj�r%t|j|j�D]\}}|dur|V|VqdSt�|�D]
 43  }|jt	vr4|Vq*dSr')
 44  rOrPrRr�zip�keys�values�iter_child_nodes�	__class__�
 45  SINGLETONS)rM�keyr1�childrrrrK�s�
 46  ��rKcC�*h|]\}}t|t�rt|tj�r|�qSr)rPr(rQrR�stmtrXrrrr\��
 47  �
 48  
 49  �cCrer)rPr(rQrRrrXrrrr\�rg>ZDelNameZ
 50  AssignAttrZDelAttr�
 51  AssignName�ConstcC�|jjtvS)z+Returns whether node is an expression node.)rar�expr_class_namesrLrrr�is_expr��rlcCrj)z)Returns whether node is a statement node.)rar�stmt_class_namesrLrrr�is_stmt�rmrocC�|jjdkS)z&Returns whether node is a module node.r�rarrLrrr�	is_module�rmrrcCrp)zFReturns whether node is a JoinedStr node, used to represent f-strings.�	JoinedStrrqrLrrrrO�srOcCrp)z2Returns whether node is a starred expression node.�StarredrqrLrrr�
 52  is_starred�rmrucCs0|jjdvp|jjdkotttttj|�j��S)z?Returns whether node represents a slice, e.g. `1:2` in `x[1:2]`)�Slice�ExtSlicer)	rar�any�map�is_slicerrRr�eltsrLrrrrz�s�rzcCsB|jjdko t|tj�o |j|juo|juoduSS)Nrv)rarrPrRr�lower�upper�steprLrrr�is_empty_astroid_slice�s� ��rcCs�|sdd�}t|�}t�}d}|dtfg}|r^|��\}}}	|	turQ||vs(J�|�|�|||�\}
 53  }|�|||f�t|�}||�D]}
|�||
|
 54  tf�qDn|||tt	t
 55  |	��}|s|S)a5
 56    Scans the tree under the node depth-first using an explicit stack. It avoids implicit recursion
 57    via the function call stack to avoid hitting 'maximum recursion depth exceeded' error.
 58  
 59    It calls ``previsit()`` and ``postvisit()`` as follows:
 60  
 61    * ``previsit(node, par_value)`` - should return ``(par_value, value)``
 62          ``par_value`` is as returned from ``previsit()`` of the parent.
 63  
 64    * ``postvisit(node, par_value, value)`` - should return ``value``
 65          ``par_value`` is as returned from ``previsit()`` of the parent, and ``value`` is as
 66          returned from ``previsit()`` of this node itself. The return ``value`` is ignored except
 67          the one for the root node, which is returned from the overall ``visit_tree()`` call.
 68  
 69    For the initial node, ``par_value`` is None. ``postvisit`` may be None.
 70    cS�dSr'r)rM�pvaluer1rrr�<lambda>�szvisit_tree.<locals>.<lambda>N)rN�set�	_PREVISIT�pop�add�append�len�insertrrr&)rMZprevisitZ	postvisit�
iter_children�done�ret�stack�currentZ	par_valuer1r�Z
 71  post_value�insrZrrr�
 72  visit_tree�s(
 73  ��r�ccsl�t|�}t�}|g}|r4|��}||vsJ�|�|�|Vt|�}||�D]}|�||�q'|s
dSdS)a�
 74    Recursively yield all descendant nodes in the tree starting at ``node`` (including ``node``
 75    itself), using depth-first pre-order traversal (yieling parents before their children).
 76  
 77    This is similar to ``ast.walk()``, but with a different order, and it works for both ``ast`` and
 78    ``astroid`` trees. Also, as ``iter_children()``, it skips singleton nodes generated by ``ast``.
 79    N)rNr�r�r�r�r�)rMr�r�r�r�r�r[rrr�walks�	
 80  �r�cCsXd}g}t|�D]\}}}|�|||��|�|�|}q|�||d��d�|�S)ah
 81    Replaces multiple slices of text with new values. This is a convenience method for making code
 82    modifications of ranges e.g. as identified by ``ASTTokens.get_text_range(node)``. Replacements is
 83    an iterable of ``(start, end, new_text)`` tuples.
 84  
 85    For example, ``replace("this is a test", [(0, 4, "X"), (8, 9, "THE")])`` produces
 86    ``"X is THE test"``.
 87    rN�)�sortedr��join)rG�replacements�p�partsr7�end�new_textrrr�replace0s
 88  
 89  
 90  r�c@s eZdZdZdd�Zdd�ZdS)�NodeMethodsz[
 91    Helper to get `visit_{node_type}` methods given a node's class and cache the results.
 92    cCs
 93  i|_dSr')�_cacher)rrr�__init__Hs
 94  zNodeMethods.__init__cCs:|j�|�}|sd|j��}t|||j�}||j|<|S)z�
 95      Using the lowercase name of the class as node_type, returns `obj.visit_{node_type}`,
 96      or `obj.visit_default` if the type-specific method is not found.
 97      �visit_)r��getrr|�getattr�
visit_default)r*�obj�cls�method�namerrrr�Ls
 98  zNodeMethods.getN)rrrr,r�r�rrrrr�Dsr�cCst|�Sr')�iter)�original_tokensrrr�patched_generate_tokens]sr�ccs|�g}|D],}|jtjtjtjfvr"|r|dj|jkr"|�|�qt|�D]}|Vq&g}|Vqt|�D]}|Vq6dS)a�
 99      Fixes tokens yielded by `tokenize.generate_tokens` to handle more non-ASCII characters in identifiers.
100      Workaround for https://github.com/python/cpython/issues/68382.
101      Should only be used when tokenizing a string that is known to be valid syntax,
102      because it assumes that error tokens are not actually errors.
103      Combines groups of consecutive NAME, NUMBER, and/or ERRORTOKEN tokens into a single NAME token.
104      �����N)	r(rB�NAME�
105  ERRORTOKEN�NUMBERr�r7r��combine_tokens)r��group�tokZcombined_tokenrrrr�as�	��cCsftdd�|D��rtdd�|D��dkr|Stjtjd�dd�|D��|dj|d	j|djd
106  �gS)Ncss�|]	}|jtjkVqdSr')r(rBr��rYr�rrr�	<genexpr>|s�z!combine_tokens.<locals>.<genexpr>cSsh|]}|j�qSr)�liner�rrrr\|sz!combine_tokens.<locals>.<setcomp>rr�css�|]}|jVqdSr')r$)rY�trrrr��s�rr�)r(r$r7r�r�)	rxr�rB�	TokenInfor�r�r7r�r�)r�rrrr�zs(��r�cCs(dd�t�|�D�}|rt|d�S|S)zq
107    If the given AST node contains multiple statements, return the last one.
108    Otherwise, just return the node.
109    c
110  Ss,g|]}t|tjtjttdd�f�r|�qS)�
111  match_caser)rPrRrf�
excepthandlerr�)rYrdrrr�
112  <listcomp>�s��zlast_stmt.<locals>.<listcomp>r�)rRr`�	last_stmt)rMZchild_stmtsrrrr��s�r�)r-�)�	lru_cache)�maxsizecsbd�t���}dd�t�|�D�}dd�|D�}tt|��t|�k}t�fdd�|D��}|o0|S)a�
113      The positions attached to nodes inside f-string FormattedValues have some bugs
114      that were fixed in Python 3.9.7 in https://github.com/python/cpython/pull/27729.
115      This checks for those bugs more concretely without relying on the Python version.
116      Specifically this checks:
117       - Values with a format spec or conversion
118       - Repeated (i.e. identical-looking) expressions
119       - Multiline f-strings implicitly concatenated.
120      z�(
121        f"a {b}{b} c {d!r} e {f:g} h {i:{j}} k {l:{m:n}}"
122        f"a {b}{b} c {d!r} e {f:g} h {i:{j}} k {l:{m:n}}"
123        f"{x + y + z} {x} {y} {z} {z} {z!a} {z:z}"
124      )cSsg|]
125  }t|tj�r|�qSr)rPrR�Name�rYrMrrrr��sz*fstring_positions_work.<locals>.<listcomp>cSsg|]}|j|jf�qSr)r�
126  col_offsetr�rrrr��sc3s"�|]}t��|�|jkVqdSr')rR�get_source_segment�idr���sourcerrr��s
127  ��
128  �z)fstring_positions_work.<locals>.<genexpr>)rR�parser�r�r��all)�treeZ
129  name_nodesZname_positionsZpositions_are_uniqueZcorrect_source_segmentsrr�r�fstring_positions_work�s
130  �r�cCs�t|�D];}t|tj�s
q|jD].}t|dd�t|tj�r>t�s/t|j�D]}t|dd�q&|j	r>t|j	dd�t
131  |j	�qqdS)z
132      Add a special attribute `_broken_positions` to nodes inside f-strings
133      if the lineno/col_offset cannot be trusted.
134      �_broken_positionsTN)r�rPrRrsr_�setattr�FormattedValuer�r1�format_spec�annotate_fstring_nodes)r�Z	joinedstr�partrdrrrr��s
135  
136  ���r�cCsdS)NFrrrrrr���cCr�r'r)�_treerrrr��r�r')ErR�collectionsrD�sysrrB�abcrrrr�typingrrrr	r
137  rrr
rrr�sixr�astroid_compatrr�AstNode�version_info�intr6r�r%�
138  namedtupler&�Constantr/r4r8r@rCrNrJ�__dict__rbrKrnrkrlrorrrOrurzrr0r�r�r�r�r�r�r�r��	functoolsr�r�r�rrrr�<module>sx4$
139  
140  
141  
142  
143  
144  	�
145  ,
146  
147