a
    ,=ics                     @   s:  d Z ddlmZ ddlmZ ddlmZ 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ZddlmZ ddlmZ dd	lmZ dd
lmZ d+ddZejdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd  d ejZG d!d" d"e Z!G d#d$ d$eZ"d%d& Z#d'd( Z$d)d* Z%dS ),zBAnnotate python syntax trees with formatting from the source file.    )absolute_import)division)print_functionN)zip)ast_constants)	ast_utils)
formatting)token_generatorTFc           	   	      s(   t  fdd}|S )Nc                    s   r| j |ddnt d r6| j|r.| jndd | |g|R i | rn| j| rfdndd W d    n1 s0    Y  d S )NFtrailing_comma default
)	max_lines	semicoloncommentr   )scope_noop_contextprefix_indentsuffix)selfnodeargskwargsr   fmax_suffix_linesr   r   r   	statementr    d/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/pasta/base/annotate.pywrapped*   s    
z_gen_wrapper.<locals>.wrapped
contextlibwraps)	r   r   r   r   r   r   r   r   r"   r    r   r!   _gen_wrapper(   s    r&   c                   c   s
   d V  d S Nr    r    r    r    r!   r   6   s    r   c                 C   s   t | ddS )z5Decorates a function where the node is an expression.r   )r   r&   r   r    r    r!   
expression;   s    r*   c                 C   s   t | ddS )zFDecorates a function where the node is a FormattedValue in an fstring.Fr   r(   r)   r    r    r!   fstring_expression@   s    r,   c                 C   s   t | ddS )zEDecorates a function where the node has whitespace prefix and suffix.Fr+   r(   r)   r    r    r!   space_aroundE   s    r-   c                 C   s   t | dddS )z:Decorates a function where the node has whitespace prefix.F)r   r   r(   r)   r    r    r!   
space_leftJ   s    r.   c                 C   s   t | ddddddS )z3Decorates a function where the node is a statement.F   T)r   r   r   r   r   r(   r)   r    r    r!   r   O   s    
r   c                 C   s   t | dddS )z&Special decorator for the module node.FT)r   r   r(   r)   r    r    r!   moduleU   s    r0   c                    s   t   fdd}|S )zADecorates a function where the node is a statement with children.c                    s   | j || jd  | |g|R i | t| drxt|}|r|jt|ddkrt|dp`d	 d }| 
|| n| j|dd	 d S )
Nr   block_suffixlinenor   r   r   Tr   )r   r   hasattrr   Zget_last_childr2   getattrfmtget
splitlinesr1   r   )r   r   r   r   Z
last_childindentr)   r    r!   r"   \   s    

z block_statement.<locals>.wrappedr#   )r   r"   r    r)   r!   block_statementZ   s    r;   c                       s  e Zd ZdZejZdd Z fddZdddZ	dddZ
dd Zdd ZejdddZdd ZdddZdddZdd Zdd ZdddZd d! Zed"d# Zed$d% Zejd&d' Zed(d) Zed*d+ Zd,d- Zed.d/ Z d0d1 Z!ejd2d3 Z"ejd4d5 Z#d6d7 Z$e%d8d9 Z&ed:d; Z'ed<d= Z(d>d? Z)ed@dA Z*edBdC Z+edDdE Z,edFdG Z-e.dHdI Z/dJdK Z0e.dLdM Z1e.dNdO Z2e.dPdQ Z3e.dRdS Z4e5dTdU Z6e.dVdW Z7e.dXdY Z8e.dZd[ Z9e.d\d] Z:e.d^d_ Z;e.d`da Z<e.dbdc Z=e.ddde Z>e5dfdg Z?e.dhdi Z@e.djdk ZAe.dldm ZBe.dndo ZCe5dpdq ZDe5drds ZEe5dtdu ZFe5dvdw ZGe5dxdy ZHe5dzd{ ZId|d} ZJd~d ZKdd ZLe5dd ZMe5dd ZNe5dd ZOe5dd ZPe5dd ZQe5dd ZRe5dd ZSe5dd ZTdddZUe5dd ZVe5dd ZWe5dd ZXe5dd ZYe5dd ZZe5dd Z[e5dd Z\e5dd Z]e%dd Z^dd Z_dd Z`dd Zadd Zbdd Zcdd Zddd Zedd Zfdd Zgdd Zhdd Zidd Zjdd Zkdd ZlddÄ Zmddń ZnddǄ ZoddɄ Zpdd˄ Zqdd̈́ Zrddτ Zsddф Ztddӄ ZuddՄ Zvddׄ Zwddل Zxddۄ Zye%dd݄ Zze%dd߄ Z{e%dd Z|e%dd Z}e~dd Ze~dd Ze~dd Zdd Zedd Z  ZS )BaseVisitora  Walks a syntax tree in the order it appears in code.

  This class has a dual-purpose. It is implemented (in this file) for annotating
  an AST with formatting information needed to reconstruct the source code, but
  it also is implemented in pasta.base.codegen to reconstruct the source code.

  Each visit method in this class specifies the order in which both child nodes
  and syntax tokens appear, plus where to account for whitespace, commas,
  parentheses, etc.
  c                 C   s   g | _ d| _d| _d| _d S )Nr   z  )_stackr   _indent_diff_default_indent_diffr   r    r    r!   __init__}   s    zBaseVisitor.__init__c                    s2   | j | tt| | || j  u s.J d S r'   )r=   appendsuperr<   visitpopr   r   	__class__r    r!   rD      s    zBaseVisitor.visitr   c                    s     j |d fddg|d dS )z>Account for some amount of whitespace as the prefix to a node.r   c                      s    j ddS )NTr4   wsr    r@   r    r!   <lambda>       z$BaseVisitor.prefix.<locals>.<lambda>r   Nattr)r   r   r   r    r@   r!   r      s    zBaseVisitor.prefixNFc                    s*    fdd}j |d|g|d dS )z>Account for some amount of whitespace as the suffix to a node.c                      s   j  dS )N)r   r   r   rI   r    r   r   r   r   r    r!   _ws   s    zBaseVisitor.suffix.<locals>._wsr   r   NrM   )r   r   r   r   r   r   rP   r    rO   r!   r      s    zBaseVisitor.suffixc                 c   sx   t ||}| j}| j}t|d d}|d u r4| j}|| _|| j | _|D ]
}|V  qJ| |d| g  || _|| _d S )Nr   indent_diffblock_suffix_%s)r6   r   r>   r7   r8   r?   rN   )r   r   children_attrchildrenprev_indentprev_indent_diffnew_diffchildr    r    r!   indented   s    
zBaseVisitor.indentedc                 C   s
   || _ d S r'   )r?   )r   r:   r    r    r!   set_default_indent_diff   s    z#BaseVisitor.set_default_indent_diffc                 c   sN   |r"| j ||d g |rdndd dV  |rJ| j ||d g |rBdndd dS )aB  Context manager to handle a parenthesized scope.

    Arguments:
      node: (ast.AST) Node to store the scope prefix and suffix on.
      attr: (string, optional) Attribute of the node contained in the scope, if
        any. For example, as `None`, the scope would wrap the entire node, but
        as 'bases', the scope might wrap only the bases of a class.
      trailing_comma: (boolean) If True, allow a trailing comma at the end.
      default_parens: (boolean) If True and no formatting information is
        present, the scope would be assumed to be parenthesized.
    _prefix(r   r   NZ_suffix)rM   r   r   rN   r   default_parensr    r    r!   r      s    

zBaseVisitor.scopec                 C   s   dS )zAccount for a specific token.Nr    )r   	token_valr    r    r!   token   s    zBaseVisitor.tokenc                 C   s   dS )z'Handles an attribute on the given node.Nr    )r   r   	attr_name	attr_valsdepsr   r    r    r!   rN      s    zBaseVisitor.attrTc                 C   s   dS )a9  Account for some amount of whitespace.

    Arguments:
      max_lines: (int) Maximum number of newlines to consider.
      semicolon: (boolean) If True, parse up to the next semicolon (if present).
      comment: (boolean) If True, look for a trailing comment even when not in
        a parenthesized scope.
    r   r    )r   r   r   r   r    r    r!   rJ      s    	zBaseVisitor.wsc                 C   s   d| S )zAccount for a number of dots..r    )r   num_dotsr    r    r!   dots   s    zBaseVisitor.dotsc                 C   s   | j ddS )z)Account for up to one line of whitespace.r/   r   rI   r@   r    r    r!   
ws_oneline   s    zBaseVisitor.ws_onelinec                 C   s   dS )z/Account for a suffix that may or may not occur.Nr    )r   r   rb   r`   r   r    r    r!   optional_token   s    zBaseVisitor.optional_tokenc                 G   s   |d S )%Account for one of the given symbols.r   r    r   symbolsr    r    r!   one_of_symbols   s    zBaseVisitor.one_of_symbolsc                 C   s   |  | d S r'   )generic_visitrF   r    r    r!   visit_Module   s    zBaseVisitor.visit_Modulec                 C   s,  t |drdnd}| j|d|| jg|d d | |j | j|d| jd| jgd	d | |d
D ]}| | qd|jr(t	|jdkrt
|jd tjr| |jd rt |jd dd | |jd  nV| |d| jg | d | j|d| jd| jgd	d | |dD ]}| | qd S )NZis_elifelififZopen_if r   
open_block::
bodyr/   r   TZ
elseprefixelse	open_elseorelse)r7   r8   rN   rJ   rD   testri   rY   rz   len
isinstanceastIfcheck_is_elifsetra   )r   r   tokstmtr    r    r!   visit_If   s*     
zBaseVisitor.visit_Ifc                 C   s   dS )a  Return True if the node continues a previous `if` statement as `elif`.

    In python 2.x, `elif` statments get parsed as If nodes. E.g, the following
    two syntax forms are indistinguishable in the ast in python 2.

    if a:
      do_something()
    elif b:
      do_something_else()

    if a:
      do_something()
    else:
      if b:
        do_something_else()

    This method should return True for the 'if b' node if it has the first form.
    Nr    rF   r    r    r!   r      s    zBaseVisitor.check_is_elifc                 C   s   | j |dd| jgdd | |j | j |d| jd| jgdd | |dD ]}| | qL|jr| j |d	| jd	| jd| jg| jd
 d | |dD ]}| | qd S )NZwhile_keywordwhilezwhile r   rt   ru   rv   rw   rx   else:
rz   )rN   rJ   rD   r{   ri   rY   rz   r   r   r   r   r    r    r!   visit_While  s    zBaseVisitor.visit_Whilec                 C   s
  t tdr6t|tjr6| j|dd| jd| jgdd n| j|dd| jgdd | |j | j|d| jd	| jgd
d | |j | j|d| jd| j	gdd | 
|dD ]}| | q|jr| j|d| jd| jd| j	g| jd d | 
|dD ]}| | qd S )NAsyncForZfor_keywordasyncforz
async for r   zfor Zfor_inin in rt   ru   rv   rw   rx   r   rz   )r5   r~   r}   r   rN   rJ   rD   targetiterri   rY   rz   r   r   r    r    r!   	visit_For  s&    zBaseVisitor.visit_Forc                 C   s
   |  |S r'   )r   rF   r    r    r!   visit_AsyncFor5  s    zBaseVisitor.visit_AsyncForc                 C   s   t |dr| |S t|dds8| j|dd| jgdd | |j |jrr| j|d| jd| jgd	d | |j t|j	d
kr| 
|j	d rd|j	d _| j|d| jd| jgdd n| j|d| jd| jgdd | |dD ]}| | qd S )Nitemsis_continuedFwithwith r   Zwith_asas as r/   r   TZ
with_comma,, rt   ru   rv   rw   )r5   visit_With_3r6   rN   rJ   rD   context_exproptional_varsr|   rw   check_is_continued_withr   ri   rY   r   r    r    r!   
visit_With8  s     

zBaseVisitor.visit_Withc                 C   s
   |  |S r'   )r   rF   r    r    r!   visit_AsyncWithL  s    zBaseVisitor.visit_AsyncWithc                 C   s   d S r'   r    rF   r    r    r!   check_is_continued_tryO  s    z"BaseVisitor.check_is_continued_tryc                 C   s   dS )a  Return True if the node continues a previous `with` statement.

    In python 2.x, `with` statments with many context expressions get parsed as
    a tree of With nodes. E.g, the following two syntax forms are
    indistinguishable in the ast in python 2.

    with a, b, c:
      do_something()

    with a:
      with b:
        with c:
          do_something()

    This method should return True for the `with b` and `with c` nodes.
    Nr    rF   r    r    r!   r   S  s    z#BaseVisitor.check_is_continued_withc                 C   s   t tdr6t|tjr6| j|dd| jd| jgdd n| j|dd| jgdd t|jD ].\}}| | |t	|jd krX| 
d qX| j|d	d
| jgdd | |dD ]}| | qd S )N	AsyncWithr   r   zasync with r   r   r/   r   Zwith_body_openru   rv   rw   )r5   r~   r}   r   rN   rJ   	enumerater   rD   r|   ra   ri   rY   )r   r   iwithitemr   r    r    r!   r   f  s    
zBaseVisitor.visit_With_3c                 C   s>   |  |j |jr:| j|d| jd| jgdd |  |j d S )Nr   r   r   )rD   r   r   rN   rJ   rF   r    r    r!   visit_withitemv  s    zBaseVisitor.visit_withitemc                 C   s  t |jD ]N\}}| j|d| | jdgdd | | | j|d| | jgd| j d q
| j|dd| j|j| jgd|j d	d
 t|dg t|dg  }| j|dt	|dd t |j
D ]P\}}| | | |d| | jg ||d kr| j|d| d| jgdd qt|dr|t |jD ]V\}}| | | |d| | jg ||jd kr$| j|d| d| jgdd q$W d    n1 s0    Y  | j|d| jd| jgdd | |dD ]}| | qd S )Nzdecorator_prefix_%d@r   decorator_suffix_%dr   Z	class_defclasszclass %snamer   rd   baseskeywordsTr   r_   zbase_suffix_%dr3   zbase_sep_%dr   r   zkeyword_suffix_%dzkeyword_sep_%drt   ru   rv   rw   )r   decorator_listrN   rJ   rD   r   r   r6   r   boolr   r5   r   ri   rY   )r   r   r   	decoratorZ
class_argsbasekeywordr   r    r    r!   visit_ClassDef}  s:    



@zBaseVisitor.visit_ClassDefc              
   C   s  t |jD ]R\}}| j|d| | jd| jgdd | | | j|d| | jgd| j d q
ttdrt	|tj
r| j|d| jd| jd	| j|j| jgd
d|j d n,| j|d| jd	| j|j| jgd
d|j d t|jdg }tt|jj| |jjrdnd|jjrdndf}| j|d|dkdd | |j W d    n1 sP0    Y  t|dd r| j|d| jd| jgddd | |j | j|d| jd| jgdd | |dD ]}| | qd S )Nzdecorator_symbol_%dr   r   r   r   AsyncFunctionDefZfunction_defr   defr   zasync def %srd   r   zdef %s
kwonlyargsr/   r   r   Tr   returnsZreturns_prefixz->)r   z -> rt   ru   rv   rw   )r   r   rN   rJ   rD   ri   r   r5   r~   r}   r   r   r6   r   sumr|   varargkwargr   r   rY   )r   r   r   r   r   Z
args_countr   r    r    r!   visit_FunctionDef  sL    




,zBaseVisitor.visit_FunctionDefc                 C   s
   |  |S r'   )r   rF   r    r    r!   visit_AsyncFunctionDef  s    z"BaseVisitor.visit_AsyncFunctionDefc                 C   s   | j |dd| jd| jgdd t|jdkrZ| |jd rZd|jd _| |jd  n| |d	D ]}| | qf| j |d
| jd| jd| jgdd | |dD ]}| | qd S )Nopen_trytryru   try:
r   r/   r   Trw   open_finallyfinally	finally:
	finalbody)	rN   rJ   ri   r|   rw   r   r   rD   rY   r   r    r    r!   visit_TryFinally  s    zBaseVisitor.visit_TryFinallyc                 C   s   t |dds*| j|dd| jd| jgdd | |dD ]}| | q6|jD ]}| | qL|jr| j|d	| jd
| jd| jgdd | |dD ]}| | qd S )Nr   Fr   r   ru   r   r   rw   ry   rx   r   rz   )r6   rN   rJ   ri   rY   rD   handlersrz   r   r   r   handlerr    r    r!   visit_TryExcept  s    
zBaseVisitor.visit_TryExceptc                 C   s   | j |d| jd| jd| jgdd | |dD ]}| | q.|jD ]}| | qD|jr| j |d| jd| jd| jgd	d | |d
D ]}| | q|jr| j |d| jd| jd| jgdd | |dD ]}| | qd S )Nr   r   ru   r   r   rw   ry   rx   r   rz   r   r   r   r   )rN   rJ   ri   rY   rD   r   rz   r   r   r    r    r!   	visit_Try  s*    
zBaseVisitor.visit_Tryc                 C   s   |  d |jr| |j |jrL|jrL| j|d| j| dd| jgdd |jrzt|jtj	rn| |j n|  |j | j|d| jd| j
gdd | |d	D ]}| | qd S )
Nexceptr   r   r   r   rt   ru   rv   rw   )ra   typerD   r   rN   rJ   rn   r}   r~   ASTri   rY   r   r    r    r!   visit_ExceptHandler  s     
zBaseVisitor.visit_ExceptHandlerc                 C   s   t |dr| |S | d |jrF| j|d| jgdd | |j |jrt| j|d| jd| jgdd | |j |jr| j|d	| jd| jgdd | |j d S )
NcauseraiseZtype_prefixrs   r   Zinst_prefixr   r   Ztback_prefix)	r5   visit_Raise_3ra   r   rN   rJ   rD   instZtbackrF   r    r    r!   visit_Raise  s    


zBaseVisitor.visit_Raisec                 C   sh   |j rZ| j|dd| jgdd | |j  |jrd| j|d| jd| jgdd | |j n
| d d S )NZ
open_raiser   zraise r   Zcause_prefixfromz from )excrN   rJ   rD   r   ra   rF   r    r    r!   r     s    zBaseVisitor.visit_Raise_3c                 C   sR   | j |dd| jgdd | |j |jrN| j |dd| jgdd | |j d S )NZassert_openassertzassert r   
msg_prefixr   r   )rN   rJ   rD   r{   msgrF   r    r    r!   visit_Assert*  s
    zBaseVisitor.visit_Assertc                 C   sN   t |jD ]2\}}| | | j|d| | jd| jgdd q
| |j d S )Nzequal_%d= = r   )r   targetsrD   rN   rJ   valuer   r   r   r   r    r    r!   visit_Assign2  s    
"zBaseVisitor.visit_Assignc                 C   sT   |  |j dtjt|j d  }| j|d| j|| jgd| d |  |j d S )Nz%s=r   operator %s r   )	rD   r   r   NODE_TYPE_TO_TOKENSr   oprN   rJ   r   )r   r   Zop_tokenr    r    r!   visit_AugAssign9  s    zBaseVisitor.visit_AugAssignc                 C   sf   |  |j | j|d| jd| jgdd |  |j |jrb| j|d| jd| jgdd |  |j d S )Ncolonru   : r   equalr   r   )rD   r   rN   rJ   
annotationr   rF   r    r    r!   visit_AnnAssignA  s    zBaseVisitor.visit_AnnAssignc                 C   s(   | j |dd| jgdd | |j d S )Nawaitzawait r   rN   rJ   rD   r   rF   r    r    r!   visit_AwaitK  s    zBaseVisitor.visit_Awaitc                 C   s   |  d d S )Nbreakra   rF   r    r    r!   visit_BreakP  s    zBaseVisitor.visit_Breakc                 C   s   |  d d S )Ncontinuer   rF   r    r    r!   visit_ContinueT  s    zBaseVisitor.visit_Continuec                 C   sh   | j |dd| jgdd t|jD ]@\}}| | ||jd ur"| j |d| | jd| jgdd q"d S )Ndelzdel r   r3   comma_%dr   r   )rN   rJ   r   r   rD   r   r    r    r!   visit_DeleteX  s
    
zBaseVisitor.visit_Deletec              	   C   s   | j |dd| jgdd | j|dddd | |j |jr| j |d| j| dd	| jgd
d | |j |jr| j |d| jd	| jgd
d | |j W d    n1 s0    Y  d S )Nexecr   rw   FTr   Z
in_globalsr   r   r   Z	in_locals)rN   rJ   r   rD   rw   globalsrn   localsrF   r    r    r!   
visit_Exec`  s    zBaseVisitor.visit_Execc                 C   s   |  |j d S r'   rD   r   rF   r    r    r!   
visit_Expro  s    zBaseVisitor.visit_Exprc                 C   sZ   |  d g }|jD ]2}||jd kr6|| jdg || j|g q| |d| d S )Nglobalr   r   namesra   r   extendrJ   rN   r   r   Zidentifiersidentr    r    r!   visit_Globals  s    

zBaseVisitor.visit_Globalc                 C   sp   |  d t|jD ]V\}}| j|d| | jgdd | | ||jd kr| j|d| | jdgdd qd S )Nimportalias_prefix_%drs   r   r3   alias_sep_%dr   )ra   r   r   rN   rJ   rD   )r   r   r   aliasr    r    r!   visit_Import}  s    

zBaseVisitor.visit_Importc                 C   sl  |  d | j|d| jgdd g }|jdkrF|| |j| jg |jr|jd}|d d D ]}|| j|| jdg7 }qd|| j|d g7 }| j|d|d	d|j |jpd
 d | j|d| jgdd |  d | j|dddr t	|j
D ]V\}}| j|d| | jgdd | | ||j
d ur| j|d| | jdgdd qW d    n1 s^0    Y  d S )Nr   Zmodule_prefixrs   r   r   re   r3   r0   )levelr0   r   r   Zmodule_suffixr  r   Tr
   r  r  r   )ra   rN   rJ   r  r   rg   r0   splitr   r   r   rD   )r   r   Zmodule_patternpartspartr   r  r    r    r!   visit_ImportFrom  s,    




zBaseVisitor.visit_ImportFromc                 C   s:   |  t | j|dt | jd| jgdd |  |j d S )Nr   z:=z := r   )rD   r   rN   r   rJ   r   rF   r    r    r!   visit_NamedExpr  s    
 zBaseVisitor.visit_NamedExprc                 C   sZ   |  d g }|jD ]2}||jd kr6|| jdg || j|g q| |d| d S )Nnonlocalr   r   r   r   r   r    r    r!   visit_Nonlocal  s    

zBaseVisitor.visit_Nonlocalc                 C   s   |  d d S )Npassr   rF   r    r    r!   
visit_Pass  s    zBaseVisitor.visit_Passc                 C   s   | j |dd| jgdd |jr| j |dd| jgdd | |j |jrf| j |d| jd| jgd	d n|js| j |d
| jdgdd t|jD ]`\}}| | ||jd ur| j |d| | jd| jgd	d q|js| j |d
| jdgdd qd S )NZ
print_openprintzprint r   Zredirectionz>>Zvalues_prefixr   r   r   r3   r   )rN   rJ   destrD   valuesnlr   )r   r   r   r   r    r    r!   visit_Print  s    
"zBaseVisitor.visit_Printc                 C   s6   |  d |jr2| j|d| jgdd | |j d S )NreturnZreturn_value_prefixrs   r   ra   r   rN   rJ   rD   rF   r    r    r!   visit_Return  s    
zBaseVisitor.visit_Returnc                 C   s6   |  d |jr2| j|d| jgdd | |j d S )NyieldZyield_value_prefixrs   r   r  rF   r    r    r!   visit_Yield  s    
zBaseVisitor.visit_Yieldc                 C   s.   | j |dd| jd| jgdd | |j d S )NZ
yield_fromr  r   zyield from r   r   rF   r    r    r!   visit_YieldFrom  s    zBaseVisitor.visit_YieldFromc                 C   s8   |  |j | j|d| jd| jgdd | |j d S )Ndotre   r   )rD   r   rN   rJ   ra   rF   r    r    r!   visit_Attribute  s    zBaseVisitor.visit_Attributec                 C   sR   t jt|j d }| |j | j|d| j|| jgd| dd | |j d S )Nr   r   r   r   r   )	r   r   r   r   rD   leftrN   rJ   rightr   r   	op_symbolr    r    r!   visit_BinOp  s    zBaseVisitor.visit_BinOpc                 C   sj   t jt|j d }t|jD ]F\}}| | ||jd ur| j|d| | j|| jgd| dd qd S )Nr   r3   zop_%dr   r  r   )	r   r   r   r   r   r  rD   rN   rJ   )r   r   r#  r   r   r    r    r!   visit_BoolOp  s    
zBaseVisitor.visit_BoolOpc                 C   sz   |  |j | j|dddJ tjd d dkr<| |}n
| |}|rX| |dd W d    n1 sl0    Y  d S )N	argumentsTr_      )      r   r   )rD   funcr   sysversion_infovisit_Call_arguments35visit_Call_argumentsrj   )r   r   Zany_argsr    r    r!   
visit_Call  s    
zBaseVisitor.visit_Callc                 C   s   dd }|j r4tdd |jD d|j fg |d}ndd |jD }dd |jD | }|jrn|d	|jf t|D ]j\}\}}|d ur| j|d
| | j|g|d | 	| ||d d urv| j|d| | jd| jgdd qvt
|S )Nc                 S   s2   | d }t |tjr|j}t|ddt|ddfS )Nr/   r2   r   
col_offsetr}   r~   r   r   r6   )tupargr    r    r!   arg_location  s    z6BaseVisitor.visit_Call_arguments.<locals>.arg_locationc                 S   s   g | ]}d |fqS r'   r    .0kwr    r    r!   
<listcomp>  rL   z4BaseVisitor.visit_Call_arguments.<locals>.<listcomp>*keyc                 S   s   g | ]}d |fqS r'   r    r6  r    r    r!   r9    rL   c                 S   s   g | ]}d |fqS r'   r    )r7  nr    r    r!   r9    rL   **z	%s_prefixr   r3   r/   r   r   r   )Zstarargssortedr   r   r   rB   r   rN   rJ   rD   r   )r   r   r5  Zsorted_keywordsall_argsr   r   r4  r    r    r!   r/    s"    
"z BaseVisitor.visit_Call_argumentsc                 C   sr   dd }t |j|j t|d}t|D ]>\}}| | ||d ur*| j|d| | jd| jgdd q*t	|S )	Nc                 S   s   dd }|| r t |tjr dS ||r8t | tjr8dS dd }|| }||}d|v s`d|v rddS ||k rpdS ||kr|dS dS )	z&Old-style comparator for sorting args.c                 S   s   t | tjtjf S r'   )r}   r~   r   Starredar    r    r!   is_arg!  s    zGBaseVisitor.visit_Call_arguments35.<locals>.arg_compare.<locals>.is_argr3   r/   c                 S   s*   t | tjr| j} t| dd t| dd fS )Nr2   r1  r2  rB  r    r    r!   get_pos-  s    zHBaseVisitor.visit_Call_arguments35.<locals>.arg_compare.<locals>.get_posNr   )r}   r~   r   )Za1Za2rD  rE  Zpos1pos2r    r    r!   arg_compare  s    z7BaseVisitor.visit_Call_arguments35.<locals>.arg_comparer;  r3   r   r   r   r   )
r?  r   r   	functools
cmp_to_keyr   rD   rN   rJ   r   )r   r   rG  r@  r   r4  r    r    r!   r.    s    
"z"BaseVisitor.visit_Call_arguments35c                 C   s(   | j |dd| jgdd | |j d S )Nstarr:  r   r   rF   r    r    r!   visit_StarredG  s    zBaseVisitor.visit_Starredc                 C   sx   |  |j tt|j|jD ]T\}\}}| j|d| | jgdd |  | | j|d| | jgdd |  | qd S )Nzop_prefix_%drs   r   zop_suffix_%d)rD   r   r   r   opscomparatorsrN   rJ   )r   r   r   r   Z
comparatorr    r    r!   visit_CompareK  s    
zBaseVisitor.visit_Comparec                 C   s   |  d ttt|j|j|jD ]\}}}|d u rT| j|d| | jdgdd n*| | | j|d| | jd| jgdd | | ||jd ur$| j|d	| | jd
| jgdd q$| j	|dd
dd | j|d| jdgdd d S )N{zstarstar_%dr>  r   zkey_val_sep_%dru   r   r3   r   r   r   
extracommaTallow_whitespace_prefixZclose_prefix})
ra   r   ranger|   keysr  rN   rJ   rD   rj   )r   r   r   r<  r   r    r    r!   
visit_DictT  s    
$

"zBaseVisitor.visit_Dictc                 C   s~   | j |dd| jgdd | |j | j |d| jd| jgdd | |j |jD ]}| | qR| j |d| jdgdd d S )	NZ	open_dictrO  r   Zkey_val_sepru   r   Z
close_dictrS  )rN   rJ   rD   r<  r   
generators)r   r   compr    r    r!   visit_DictCompe  s    
zBaseVisitor.visit_DictCompc                 C   s   |  | d S r'   	_comp_exprF   r    r    r!   visit_GeneratorExpo  s    zBaseVisitor.visit_GeneratorExpc                 C   s`   |  |j | j|d| jd| jgdd |  |j | j|d| jd| jgdd |  |j d S )Nrr    if r   rx   z else )rD   rw   rN   rJ   r{   rz   rF   r    r    r!   visit_IfExps  s
    zBaseVisitor.visit_IfExpc                 C   sP   | j |dd| jgdd | |j | j |d| jd| jgdd | |j d S )NZ
lambda_deflambdazlambda r   Zopen_lambdaru   r   )rN   rJ   rD   r   rw   rF   r    r    r!   visit_Lambda{  s    zBaseVisitor.visit_Lambdac                 C   s   | j |dd| jgdd t|jD ]@\}}| | ||jd ur"| j |d| | jd| jgdd q"|jr|| j|ddd	d
 | j |d| jdgdd d S )NZ	list_open[r   r3   r   r   r   rP  TrQ  Z
list_close]rN   rJ   r   eltsrD   rj   r   r   r   eltr    r    r!   
visit_List  s    
"zBaseVisitor.visit_Listc                 C   s   | j |ddd d S )Nra  rb  
open_braceclose_bracerZ  rF   r    r    r!   visit_ListComp  s    zBaseVisitor.visit_ListCompc                 C   sf   |r| j |d|| jg|d | |j t|jD ]\}}| | q2|rb| j |d| j|g|d d S )NZcompexp_openr   Zcompexp_close)rN   rJ   rD   rf  r   rW  )r   r   ri  rj  r   rX  r    r    r!   r[    s    zBaseVisitor._comp_expc                 C   s   |  |j d S r'   )ra   idrF   r    r    r!   
visit_Name  s    zBaseVisitor.visit_Namec                 C   s   |  t|j d S r'   )ra   strr   rF   r    r    r!   visit_NameConstant  s    zBaseVisitor.visit_NameConstantc                 C   s@   | j |dd| jgdd | |j | j |d| jdgdd d S )NZ	repr_open`r   Z
repr_closer   rF   r    r    r!   
visit_Repr  s    zBaseVisitor.visit_Reprc                 C   s   | j |dd| jgdd t|jD ]T\}}| | ||jd urd| j |d| | jd| jgdd q"| j|ddd	d
 q"| j |d| jdgdd d S )NZset_openrO  r   r3   r   r   r   rP  TrQ  Z	set_closerS  rc  re  r    r    r!   	visit_Set  s    
"
zBaseVisitor.visit_Setc                 C   s   | j |ddd d S )NrO  rS  rh  rZ  rF   r    r    r!   visit_SetComp  s    zBaseVisitor.visit_SetCompc                 C   sP   |  |j | j|d| jd| jgdd |  |j | j|d| jdgdd d S )NZ
slice_openra  r   Zslice_closerb  )rD   r   rN   rJ   slicerF   r    r    r!   visit_Subscript  s    zBaseVisitor.visit_Subscriptc              	   C   s   | j |ddd| t|jD ]`\}}| | ||jd ur^| j|d| | jd| jgdd q| j|d	ddt|jd
kd qW d    n1 s0    Y  d S )Nrd  Tr'  r3   r   r   r   r   rP  r/   )rR  r   )r   r   rd  rD   rN   rJ   rj   r|   re  r    r    r!   visit_Tuple  s    

zBaseVisitor.visit_Tuplec                 C   s>   t jt|j d }| j|d|| jg|dd | |j d S )Nr   r   r  r   )r   r   r   r   rN   rJ   rD   operandr"  r    r    r!   visit_UnaryOp  s    zBaseVisitor.visit_UnaryOpc                 C   s   |  d d S )N...r   rF   r    r    r!   visit_Ellipsis  s    zBaseVisitor.visit_Ellipsisc                 C   s   |  tjt| d  d S Nr   ra   r   r   r   rF   r    r    r!   	visit_Add  s    zBaseVisitor.visit_Addc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   	visit_Sub  s    zBaseVisitor.visit_Subc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   
visit_Mult  s    zBaseVisitor.visit_Multc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   	visit_Div  s    zBaseVisitor.visit_Divc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   	visit_Mod  s    zBaseVisitor.visit_Modc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   	visit_Pow  s    zBaseVisitor.visit_Powc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   visit_LShift  s    zBaseVisitor.visit_LShiftc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   visit_RShift  s    zBaseVisitor.visit_RShiftc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   visit_BitAnd  s    zBaseVisitor.visit_BitAndc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   visit_BitOr  s    zBaseVisitor.visit_BitOrc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   visit_BitXor  s    zBaseVisitor.visit_BitXorc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   visit_FloorDiv   s    zBaseVisitor.visit_FloorDivc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   visit_Invert  s    zBaseVisitor.visit_Invertc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   	visit_Not  s    zBaseVisitor.visit_Notc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   
visit_UAdd	  s    zBaseVisitor.visit_UAddc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   
visit_USub  s    zBaseVisitor.visit_USubc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   visit_Eq  s    zBaseVisitor.visit_Eqc                 C   s   |  |d| ddg d S )Nr   z!=z<>)rN   rn   rF   r    r    r!   visit_NotEq  s    zBaseVisitor.visit_NotEqc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   visit_Lt  s    zBaseVisitor.visit_Ltc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   	visit_LtE  s    zBaseVisitor.visit_LtEc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   visit_Gt  s    zBaseVisitor.visit_Gtc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   	visit_GtE  s    zBaseVisitor.visit_GtEc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   visit_Is!  s    zBaseVisitor.visit_Isc                 C   s   | j |dd| jdgdd d S )Ncontentisnotzis notr   rN   rJ   rF   r    r    r!   visit_IsNot$  s    zBaseVisitor.visit_IsNotc                 C   s   |  tjt| d  d S r{  r|  rF   r    r    r!   visit_In'  s    zBaseVisitor.visit_Inc                 C   s   | j |dd| jdgdd d S )Nr  r  r   znot inr   r  rF   r    r    r!   visit_NotIn*  s    zBaseVisitor.visit_NotInc                 C   s   g }|j d}|d d D ]}|| j|| jdg7 }q|| j|d g7 }| j|d|d|j d |jd ur| j|d| jd| jgdd	 | |j d S )
Nre   r3   r   r   r   asnamer   r   r   )r   r	  rJ   rN   r  ra   )r   r   Zname_patternr
  r  r    r    r!   visit_alias1  s    

zBaseVisitor.visit_aliasc                 C   sB   |  |j |jd ur>| j|d| jd| jgdd | |j d S )NZannotation_prefixru   r   r   )ra   r4  r   rN   rJ   rD   rF   r    r    r!   	visit_arg>  s    
zBaseVisitor.visit_argc              	   C   sP  t |dg }t |dg }t|t|ks,J tt|j| tt |dg |jrRdnd|jr^dndf}d}t |dg |j }|jr|d t|j  n|}|jr|jt|j d  n|j}|D ]t}	| |	 |d7 }||k r| j|d| | j	d| j	gdd	 |tt |dg kr| j|d
| j	d| j	d| j	gdd	 qt
t||jD ]t\}
\}	}| |	 | j|d|
 | j	d| j	gdd	 | | |d7 }||k rF| j|d| | j	d| j	gdd	 qF|jr:| j|d| j	d| j	gdd	 t|jtjr| |j n| |j | |d| j	g |d7 }||k r^| d n$|r^| |d| j	d| j	d| j	g t
t||D ]~\}
\}	}| |	 |d ur| j|d|
 | j	d| j	gdd	 | | |d7 }||k rl| j|d| | j	d| j	gdd	 ql|jrL| j|d| j	d| j	gdd	 t|jtjr.| |j n| |j | |d| j	g d S )Nr   kw_defaultsposonlyargsr/   r   r   r   r   r   Zposonly_sep/z/, z
default_%dr   Zvararg_prefixr:  Zvararg_suffixZ
kwonly_sepzkw_default_%dZkwarg_prefixr>  Zkwarg_suffix)r6   r|   r   r   r   r   defaultsrD   rN   rJ   r   r   r}   r~   r   ra   )r   r   r   r  Z
total_argsZarg_iZpos_args
positionalr   r4  r   r   r    r    r!   visit_argumentsF  s~    "










zBaseVisitor.visit_argumentsc                 C   s   t |ddr0| j|d| jd| jd| jgdd n| j|d| jd| jgdd | |j | j|d| jd| jgd	d | |j t|jD ]2\}}| j|d
| | jd| jgdd | | qd S )Nis_asyncFr   r   z async for r   z for r   r   zif_%drr   r]  )r6   rN   rJ   rD   r   r   r   ifs)r   r   r   Zif_exprr    r    r!   visit_comprehension  s     zBaseVisitor.visit_comprehensionc                 C   sX   |j d u r$| j|dd| jgdd n$| |j  | j|d| jdgdd | |j d S )Nstarsr>  r   eqr   )r4  rN   rJ   ra   rD   r   rF   r    r    r!   visit_keyword  s
    
zBaseVisitor.visit_keywordc                 C   s   |  |j d S r'   r   rF   r    r    r!   visit_Index  s    zBaseVisitor.visit_Indexc                 C   sb   t |jD ]@\}}| | ||jd ur
| j|d| | jd| jgdd q
| j|dddd d S )Nr3   z
dim_sep_%dr   r   r   r   F)r   dimsrD   rN   rJ   rj   )r   r   r   dimr    r    r!   visit_ExtSlice  s
    
"zBaseVisitor.visit_ExtSlicec                 C   s   |j r| |j  | j|d| jd| jgdd |jr@| |j | |d| jg | |dd | |d| jg |jr| |r| j|dddd d|j_| |j d S )	NZ
lowerspaceru   r   Z
stepspace1Z
step_colonZ
stepspace2Zstep_colon_2T)	lowerrD   rN   rJ   upperrj   stepcheck_slice_includes_stepis_explicit_steprF   r    r    r!   visit_Slice  s    zBaseVisitor.visit_Slicec                 C   s8   |j s
dS t|j ddrdS t|j tjo4|j jdk S )FHelper function for Slice node to determine whether to visit its step.Fr  TNone)r  r6   r}   r~   Namerl  rF   r    r    r!   r    s
    z%BaseVisitor.check_slice_includes_stepc                 C   sp   |  |j |jdkr>| j|d| jdt|jgdd|j d |jrl| j|d| jd| jgdd	 |  |j d S )
Nr3   
conversion!)r  z!%cr   Zformat_spec_prefixru   r   )rD   r   r  rN   rJ   chrformat_specrF   r    r    r!   visit_FormattedValue  s    
z BaseVisitor.visit_FormattedValue)r   )NFFr   )NFF)NN)NFT)F)NN)__name__
__module____qualname____doc__abcABCMeta__metaclass__rA   rD   r   r   rY   rZ   r$   contextmanagerr   ra   rN   rJ   rg   ri   rj   rn   r0   rp   r;   r   abstractmethodr   r   r   r   r   r   r   r   r   r-   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r*   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r  r  r$  r%  r0  r/  r.  rK  rN  rV  rY  r\  r^  r`  rg  rk  r[  rm  ro  rq  rr  rs  ru  rv  rx  rz  r}  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r.   r  r  r  r  r,   r  __classcell__r    r    rG   r!   r<   o   st  
  



	










#








	






	



	




	



)


	














	


F




r<   c                   @   s   e Zd ZdZdS )AnnotationErrorz5An exception for when we failed to annotate the tree.N)r  r  r  r  r    r    r    r!   r    s   r  c                       s   e Zd Z fddZ fddZdd Ze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 Zd.ddZdd Zd d! Zd"d# Zd/d$d%Zd&d' Zd0d(d)Zd1d*d+Zd,d- Z  ZS )2AstAnnotatorc                    s   t t|   t|| _d S r'   )rC   r  rA   r	   ZTokenGeneratortokens)r   sourcerG   r    r!   rA     s    zAstAnnotator.__init__c                    sn   z4t |d| j t |d| j tt| | W n4 ttt	t
fyh } zt|W Y d }~n
d }~0 0 d S )Nr:   rQ   )r7   r   r   r>   rC   r  rD   	TypeError
ValueError
IndexErrorKeyErrorr  )r   r   erG   r    r!   rD     s    zAstAnnotator.visitc                 c   s   t ||}| jj}| j j}|d |d krT| j}d| _|D ]
}|V  q>|| _dS | j}| j}	| jdd }
|
j}t	||}|sd}t
d|d  tjd || _|| _|D ]
}|V  qt|d| | j| j || _|	| _dS )	zCGenerator which annotates child nodes with their indentation level.r   Nc                 S   s   | j tjjkS r'   )r   r	   TOKENSINDENT)tr    r    r!   rK     rL   z'AstAnnotator.indented.<locals>.<lambda>z    zbIndent detection failed (line %d); inner indentation level is not more than the outer indentation.)filerR   )r6   r  Z_locpeek_non_whitespacestartr>   r   Zpeek_conditionalsrc_get_indent_diffr  r,  stderrr7   r   block_whitespace)r   r   rS   rT   Zcur_locZnext_locrQ   rX   rU   rV   Zindent_token
new_indentrW   r    r    r!   rY     sB    

zAstAnnotator.indentedc                    sR   t jj fddg} j jdkr4|dd  j|d|dt|j	d dS )	z1Annotate a Num node with the exact number format.c                      s    j jS r'   )r  Znext_of_typer  r    r   Ztoken_number_typer    r!   rK     rL   z(AstAnnotator.visit_Num.<locals>.<lambda>-r   r  )r=  r   N)
r	   r  NUMBERr  peekr  insertrN   rn  r=  )r   r   Zcontentargsr    r  r!   	visit_Num  s
    zAstAnnotator.visit_Numc                 C   s    | j |d| jjgd|jd dS )z1Annotate a Str node with the exact string format.r  sr   NrN   r  rn  r  rF   r    r    r!   	visit_Str  s    zAstAnnotator.visit_Strc                    s   | j   }d dd |jD }t|\}} |7  |du r>q`| j }|| _ | t| || _ q | j|d fddg d dS )	z<Annotate a JoinedStr node with the fstr formatting metadata.r   c                 s   s   | ]}t |tjr|V  qd S r'   )r}   r~   FormattedValue)r7  vr    r    r!   	<genexpr>&  rL   z/AstAnnotator.visit_JoinedStr.<locals>.<genexpr>Nr  c                      s    S r'   r    r    resr    r!   rK   1  rL   z.AstAnnotator.visit_JoinedStr.<locals>.<lambda>r   )r  Zfstrr  nextrD   rN   )r   r   Z	fstr_iterr  Zres_parttgZprev_tokensr    r  r!   visit_JoinedStr!  s    zAstAnnotator.visit_JoinedStrc                 C   s    | j |d| jjgd|jd dS )z3Annotate a Bytes node with the exact string format.r  r  r   Nr  rF   r    r    r!   visit_Bytes3  s    zAstAnnotator.visit_Bytesc                 C   s8   | j  jdkr| d ntdD ]}| d q$d S )Nry  r)  re   )r  r  r  ra   rT  )r   r   r   r    r    r!   rz  8  s    zAstAnnotator.visit_Ellipsisc                 C   s    | j  }t|tjo|jdkS )z7Return True iff the If node is an `elif` in the source.rq   )r  Z	next_namer}   r~   r   r  )r   r   Znext_tokr    r    r!   r   B  s    
zAstAnnotator.check_is_elifc                 C   s   t |tjo| j jdkS )zFReturn True iff the TryExcept node is a continued `try` in the source.r   )r}   r~   Z	TryExceptr  r  r  rF   r    r    r!   r   G  s    z#AstAnnotator.check_is_continued_tryc                 C   s   t |tjo| j jdkS )zBReturn True iff the With node is a continued `with` in the source.r   )r}   r~   Withr  r  r  rF   r    r    r!   r   L  s    z$AstAnnotator.check_is_continued_withc                 C   s   | j  jdvS )r  z],)r  r  r  rF   r    r    r!   r  P  s    z&AstAnnotator.check_slice_includes_stepNFTc                 C   sv   | j  }|rf|rf|jdkrf| j  | d }| j  }|jtjjtjj	fv rb|| j jdd7 }|S | j j||dS )z;Parse some whitespace from the source tokens and return it.;r/   rh   )r   r   )
r  r  r  
whitespacera   r   r	   r  NLNEWLINE)r   r   r   r   
next_tokenresultr    r    r!   rJ   W  s    


zAstAnnotator.wsc                    s    fdd}|S )zParse a number of dots.c                      s   j  S r'   )r  rg   r    rf   r   r    r!   _parse_dotse  s    z&AstAnnotator.dots.<locals>._parse_dotsr    )r   rf   r  r    r  r!   rg   c  s    zAstAnnotator.dotsc                 C   s   t |d| j| d S )Nr   )r7   r   r  r  )r   r   indent_levelr    r    r!   r1   i  s    zAstAnnotator.block_suffixc                 C   sb   | j  }|j|kr2td||j|jd |jf |jdv rH| j   n|jdv r\| j   |jS )z2Parse a single token with exactly the given value.z$Expected %r but found %r
line %d: %sr   z({[z)}])r  r  r  r  r  lineZ	hint_openZhint_closed)r   r`   ra   r    r    r!   ra   l  s    




zAstAnnotator.tokenc                 C   sp   ~t ||d |r| j n| j }|rl|j|krld}|rJ||  7 }t |||| j j |    dS )z/Try to parse a token and attach it to the node.r   N)r7   rB   r  r  r  r  rJ   r  )r   r   rb   r`   rR  r   ra   parsedr    r    r!   rj   {  s    zAstAnnotator.optional_tokenc                    s    fdd}|S )rk   c                     s@   j   t fddD d } | d u r<td jf | S )Nc                 3   s   | ]}| j kr|V  qd S r'   )r  )r7  r  r  r    r!   r    rL   zGAstAnnotator.one_of_symbols.<locals>._one_of_symbols.<locals>.<genexpr>z"Expected one of: %r, but found: %r)r  r  r  r  )foundrl   r  r!   _one_of_symbols  s    
z4AstAnnotator.one_of_symbols.<locals>._one_of_symbolsr    )r   rm   r  r    rl   r!   rn     s    zAstAnnotator.one_of_symbolsc           	   	   C   sz   ~|r*|D ]}t ||d t||d q
g }|D ].}t|tjrT|| | q2||  q2t ||d| dS )a7  Parses some source and sets an attribute on the given node.

    Stores some arbitrary formatting information on the node. This takes a list
    attr_vals which tell what parts of the source to parse. The result of each
    function is concatenated onto the formatting data, and strings in this list
    are a shorthand to look for an exactly matching token.

    For example:
      self.attr(node, 'foo', ['(', self.ws, 'Hello, world!', self.ws, ')'],
                deps=('s',), default=node.s)

    is a rudimentary way to parse a parenthesized string. After running this,
    the matching source code for this node will be stored in its formatting
    dict under the key 'foo'. The result might be `(
  'Hello, world!'
)`.

    This also keeps track of the current value of each of the dependencies.
    In the above example, we would have looked for the string 'Hello, world!'
    because that's the value of node.s, however, when we print this back, we
    want to know if the value of node.s has changed since this time. If any of
    the dependent values has changed, the default would be used instead.

    Arguments:
      node: (ast.AST) An AST node to attach formatting information to.
      attr_name: (string) Name to store the formatting information under.
      attr_vals: (list of functions/strings) Each item is either a function
        that parses some source and return a string OR a string to match
        exactly (as a token).
      deps: (optional, set of strings) Attributes of the node which attr_vals
        depends on.
      default: (string) Unused here.
    Z__srcNr   )	r7   r   r6   r}   sixstring_typesrB   ra   join)	r   r   rb   rc   rd   r   depZ
attr_partsZattr_valr    r    r!   rN     s     zAstAnnotator.attrc                 C   s   ~| j j|||dS )aK  Return a context manager to handle a parenthesized scope.

    Arguments:
      node: (ast.AST) Node to store the scope prefix and suffix on.
      attr: (string, optional) Attribute of the node contained in the scope, if
        any. For example, as `None`, the scope would wrap the entire node, but
        as 'bases', the scope might wrap only the bases of a class.
      trailing_comma: (boolean) If True, allow a trailing comma at the end.
      default_parens: (boolean) If True and no formatting information is
        present, the scope would be assumed to be parenthesized.
    )rN   r   )r  r   r^   r    r    r!   r     s    zAstAnnotator.scopec                 C   sB   | j  }|r"|j|ks"|j|kr&dS | j   |j|   S d S )Nr   )r  r  r   r  r  rJ   )r   
token_typer`   ra   r    r    r!   _optional_token  s
    

zAstAnnotator._optional_token)NFT)FF)NN)NFF)r  r  r  rA   rD   rY   r*   r  r  r  r  r-   rz  r   r   r   r  rJ   rg   r1   ra   rj   rn   rN   r   r  r  r    r    rG   r!   r    s6   )




	
 

,
r  c                 C   s<   d}| D ].}|dkr|d7 }q|dkr|d|d  7 }q|S Nr   rs   r/   	   r    )r:   widthcr    r    r!   _get_indent_width  s    
r  c                 C   s   d}t | D ]x\}}||kr  q|dkr2|d7 }q|dkr|d |d  |kr`|d|d  7 }qd|d |  | |d d     S q| |d  S r  )r   )r:   Zremove_widthr  r   r  r    r    r!   _ltrim_indent  s    
&r  c                 C   s2   t | }t |}|| }|dkr$dS t||| S )aK  Computes the whitespace added to an indented block.

  Finds the portion of an indent prefix that is added onto the outer indent. In
  most cases, the inner indent starts with the outer indent, but this is not
  necessarily true. For example, the outer block could be indented to four
  spaces and its body indented with one tab (effectively 8 spaces).

  Arguments:
    outer: (string) Indentation of the outer block.
    inner: (string) Indentation of the inner block.
  Returns:
    The string whitespace which is added to the indentation level when moving
    from outer to inner.
  r   N)r  r  )outerinnerZouter_wZinner_wZdiff_wr    r    r!   r    s    r  )TTTNFFF)&r  
__future__r   r   r   r  r~   r$   rH  	itertoolsr  Z	six.movesr   r,  Z
pasta.baser   r   r   r7   r	   r&   r  r   r*   r,   r-   r.   r   r0   r;   NodeVisitorr<   	Exceptionr  r  r  r  r  r    r    r    r!   <module>   sT     

        q }
