B
    ­»ˆds×  ã               @   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          	      sv   ˆr| j |ddntƒ T ˆr6| j|ˆr.| jndd ˆ| |f|ž|Ž ˆrh| j|ˆˆˆ ˆr`dndd W d Q R X 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   © úE/var/www/html/venv/lib/python3.7/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   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.F)r   )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    r(   c                s   t  ˆ ¡‡ fdd„ƒ}|S )zADecorates a function where the node is a statement with children.c                s„   | j || jd ˆ | |f|ž|Ž t| dƒrrt |¡}|r€|jt|ddƒkr€t |d¡pZd 	¡ d }|  
||¡ n| j|dd	 d S )
N)r   Úblock_suffixÚlinenor   r   r   éÿÿÿÿT)r   )r   r   Úhasattrr   Zget_last_childr*   ÚgetattrÚfmtÚgetÚ
splitlinesr)   r   )r   r   r   r   Z
last_childÚindent)r   r   r   r   \   s    

z block_statement.<locals>.wrapped)r    r!   )r   r   r   )r   r   Úblock_statementZ   s    r2   c                   sÜ  e Zd ZdZejZdd„ Z‡ fdd„Zdîdd„Z	dïdd„Z
dd„ Zdd„ Zejdðdd„ƒZdd„ Zdñdd„Zdòdd„Zdd„ Zdd„ Zdód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„ ZKd€d„ ZLe5d‚dƒ„ ƒZMe5d„d…„ ƒZNe5d†d‡„ ƒZOe5dˆd‰„ ƒZPe5dŠd‹„ ƒZQe5dŒd„ ƒZRe5dŽd„ ƒZSe5dd‘„ ƒZTdôd’d“„ZUe5d”d•„ ƒZVe5d–d—„ ƒZWe5d˜d™„ ƒZXe5dšd›„ ƒZYe5dœd„ ƒZZe5dždŸ„ ƒZ[e5d d¡„ ƒZ\e5d¢d£„ ƒZ]e%d¤d¥„ ƒZ^d¦d§„ Z_d¨d©„ Z`dªd«„ Zad¬d­„ Zbd®d¯„ Zcd°d±„ Zdd²d³„ Zed´dµ„ Zfd¶d·„ Zgd¸d¹„ Zhdºd»„ Zid¼d½„ Zjd¾d¿„ ZkdÀdÁ„ ZldÂdÃ„ ZmdÄdÅ„ ZndÆdÇ„ ZodÈdÉ„ ZpdÊdË„ ZqdÌdÍ„ ZrdÎdÏ„ ZsdÐdÑ„ ZtdÒdÓ„ ZudÔdÕ„ ZvdÖd×„ ZwdØdÙ„ ZxdÚdÛ„ Zye%dÜdÝ„ ƒZze%dÞdß„ ƒZ{e%dàdá„ ƒZ|e%dâdã„ ƒZ}e~dädå„ ƒZe~dædç„ ƒZ€e~dèdé„ ƒZdêdë„ Z‚eƒdìdí„ ƒZ„‡  Z…S )õÚ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  ¡ ks.t‚d S )N)r4   ÚappendÚsuperr3   ÚvisitÚpopÚAssertionError)r   r   )Ú	__class__r   r   r:   ƒ   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 )NT)r   )Ú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   )r>   r   )r   r   r   r   r   r   Ú_ws   s    zBaseVisitor.suffix.<locals>._wsr   )r   N)rA   )r   r   r   r   r   r   rB   r   )r   r   r   r   r   r   Œ   s    zBaseVisitor.suffixc             c   s|   t ||ƒ}| j}| j}t |d d¡}|d kr4| j}|| _|| j | _x|D ]
}|V  qLW |  |d| g ¡ || _|| _d S )Nr   Úindent_diffzblock_suffix_%s)r-   r   r5   r.   r/   r6   rA   )r   r   Úchildren_attrÚchildrenÚprev_indentÚprev_indent_diffÚnew_diffÚchildr   r   r   Úindented“   s    


zBaseVisitor.indentedc             C   s
   || _ d S )N)r6   )r   r1   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   NÚ_suffixú))rA   )r   r   rA   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   rA   ½   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   r>   À   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   )r>   )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   rS   rQ   r   r   r   r   Úoptional_tokenÓ   s    zBaseVisitor.optional_tokenc             G   s   |d S )z%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 )N)Úgeneric_visit)r   r   r   r   r   Úvisit_Moduleß   s    zBaseVisitor.visit_Modulec             C   s4  t  |d¡rdnd}| j|d|| jg|d d |  |j¡ | j|d| jd| jgd	d x|  |d
¡D ]}|  |¡ qfW |jr0t	|jƒdkrÖt
|jd tjƒrÖ|  |jd ¡rÖt  |jd dd¡ |  |jd ¡ nZ|  |d| jg¡ |  d¡ | j|d| jd| jgd	d x |  |d¡D ]}|  |¡ qW d S )NZis_elifÚelifÚifZopen_ifú )r   Ú
open_blockú:z:
Úbodyr'   r   TZ
elseprefixÚelseÚ	open_elseÚorelse)r.   r/   rA   r>   r:   ÚtestrY   rJ   rg   ÚlenÚ
isinstanceÚastÚIfÚcheck_is_elifÚsetrR   )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   )r   r   r   r   r   rm   û   s    zBaseVisitor.check_is_elifc             C   s²   | j |dd| jgdd |  |j¡ | j |d| jd| jgdd x|  |d¡D ]}|  |¡ qNW |jr®| j |d	| jd	| jd| jg| jd
 d x|  |d¡D ]}|  |¡ qœW d S )NZwhile_keywordÚwhilezwhile )r   rb   rc   z:
rd   re   zelse:
rg   )rA   r>   r:   rh   rY   rJ   rg   r   )r   r   rp   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 x|  
|d¡D ]}|  |¡ q¬W |jr| j|d| jd| jd| j	g| jd d x|  
|d¡D ]}|  |¡ qüW d S )NÚAsyncForZfor_keywordÚasyncÚforz
async for )r   zfor Zfor_inÚinz in rb   rc   z:
rd   re   zelse:
rg   )r,   rk   rj   rt   rA   r>   r:   ÚtargetÚiterrY   rJ   rg   r   )r   r   rp   r   r   r   Ú	visit_For  s     
zBaseVisitor.visit_Forc             C   s
   |   |¡S )N)rz   )r   r   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 ¡rºd|j	d _| j|d| jd| jgdd n| j|d| jd| jgdd x|  |d¡D ]}|  |¡ qäW d S )NÚitemsÚis_continuedFÚwithzwith )r   Zwith_asÚasz as r'   r   TZ
with_commaú,z, rb   rc   z:
rd   )r,   Úvisit_With_3r-   rA   r>   r:   Úcontext_exprÚoptional_varsri   rd   Úcheck_is_continued_withr}   rY   rJ   )r   r   rp   r   r   r   Ú
visit_With8  s    

zBaseVisitor.visit_Withc             C   s
   |   |¡S )N)r…   )r   r   r   r   r   Úvisit_AsyncWithL  s    zBaseVisitor.visit_AsyncWithc             C   s   d S )Nr   )r   r   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   )r   r   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 x<t|jƒD ].\}}|  |¡ |t	|jƒd krZ|  
d¡ qZW | j|d	d
| jgdd x|  |d¡D ]}|  |¡ q²W d S )NÚ	AsyncWithr~   ru   zasync with )r   zwith r'   r€   Zwith_body_openrc   z:
rd   )r,   rk   rj   rˆ   rA   r>   Ú	enumerater|   r:   ri   rR   rY   rJ   )r   r   ÚiÚwithitemrp   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   z as )r   )r:   r‚   rƒ   rA   r>   )r   r   r   r   r   Úvisit_withitemv  s    zBaseVisitor.visit_withitemc          	   C   sÔ  x\t |jƒD ]N\}}| j|d| | jdgdd |  |¡ | j|d| | jgd| j d qW | j|dd| j|j| jgd|j d	d
 t|dg ƒt|dg ƒ }| j|dt	|ƒddØ x^t |j
ƒD ]P\}}|  |¡ |  |d| | jg¡ ||d krÂ| j|d| d| jgdd qÂW t|dƒrˆxdt |jƒD ]V\}}|  |¡ |  |d| | jg¡ ||jd kr.| j|d| d| jgdd q.W W d Q R X | j|d| jd| jgdd x |  |d¡D ]}|  |¡ q¼W d S )Nzdecorator_prefix_%dú@)r   zdecorator_suffix_%dr   Z	class_defÚclasszclass %s)Úname)r   rU   ÚbasesÚkeywordsT)r
   rP   zbase_suffix_%dr+   zbase_sep_%dr€   z, zkeyword_suffix_%dzkeyword_sep_%drb   rc   z:
rd   )r‰   Údecorator_listrA   r>   r:   r   r   r-   r   Úboolr   r,   r‘   rY   rJ   )r   r   rŠ   Ú	decoratorZ
class_argsÚbaseÚkeywordrp   r   r   r   Úvisit_ClassDef}  s2    


 
,zBaseVisitor.visit_ClassDefc          
   C   sÄ  x`t |jƒD ]R\}}| j|d| | jd| jgdd |  |¡ | j|d| | jgd| j d qW ttdƒr¬t	|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rdnd|jjrdndfƒ}| j|d|dkdd |  |j¡ W d Q R X t|dd ƒr‚| j|d| jd| jgddd |  |j¡ | j|d| jd| jgdd x |  |d¡D ]}|  |¡ q¬W d S )Nzdecorator_symbol_%dr   )r   zdecorator_suffix_%dr   ÚAsyncFunctionDefZfunction_defru   Údef)r   zasync def %s)rU   r   zdef %sÚ
kwonlyargsr'   r   r   T)r
   rP   ÚreturnsZreturns_prefixz->)r›   z -> rb   rc   z:
rd   )r‰   r’   rA   r>   r:   rY   r   r,   rk   rj   r˜   r   r-   r   Úsumri   ÚvarargÚkwargr   r›   rJ   )r   r   rŠ   r”   rš   Z
args_countrp   r   r   r   Úvisit_FunctionDef™  s:    



zBaseVisitor.visit_FunctionDefc             C   s
   |   |¡S )N)rŸ   )r   r   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 x|  |d	¡D ]}|  |¡ qhW | j |d
| jd| jd| jgdd x|  |d¡D ]}|  |¡ qªW d S )NÚopen_tryÚtryrc   ztry:
)r   r'   r   Trd   Úopen_finallyÚfinallyz	finally:
Ú	finalbody)	rA   r>   rY   ri   rd   r‡   r}   r:   rJ   )r   r   rp   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 x|  |d¡D ]}|  |¡ q8W x|jD ]}|  |¡ qRW |jr¬| j|d	| jd
| jd| jgdd x|  |d¡D ]}|  |¡ qšW d S )Nr}   Fr¡   r¢   rc   ztry:
)r   rd   rf   re   zelse:
rg   )r-   rA   r>   rY   rJ   r:   Úhandlersrg   )r   r   rp   Úhandlerr   r   r   Úvisit_TryExceptÓ  s    zBaseVisitor.visit_TryExceptc             C   sð   | j |d| jd| jd| jgdd x|  |d¡D ]}|  |¡ q0W x|jD ]}|  |¡ qJW |jr¤| j |d| jd| jd| jgd	d x|  |d
¡D ]}|  |¡ q’W |jrì| j |d| jd| jd| jgdd x|  |d¡D ]}|  |¡ qÚW d S )Nr¡   r¢   rc   ztry:
)r   rd   rf   re   zelse:
rg   r£   r¤   z	finally:
r¥   )rA   r>   rY   rJ   r:   r§   rg   r¥   )r   r   rp   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 x|  |d	¡D ]}|  |¡ q¤W d S )
NÚexceptr   r€   z as )r   rb   rc   z:
rd   )rR   Útyper:   r   rA   r>   r\   rj   rk   ÚASTrY   rJ   )r   r   rp   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_prefixra   )r   Zinst_prefixr€   z, Ztback_prefix)	r,   Úvisit_Raise_3rR   r¬   rA   r>   r:   ÚinstZtback)r   r   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 )ÚexcrA   r>   r:   r¯   rR   )r   r   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   Z
msg_prefixr€   z, )rA   r>   r:   rh   Úmsg)r   r   r   r   r   Úvisit_Assert*  s
    zBaseVisitor.visit_Assertc             C   sR   x@t |jƒD ]2\}}|  |¡ | j|d| | jd| jgdd qW |  |j¡ d S )Nzequal_%dú=z = )r   )r‰   Útargetsr:   rA   r>   Úvalue)r   r   rŠ   rx   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   Úoperatorz %s )r   )	r:   rx   r   ÚNODE_TYPE_TO_TOKENSr¬   ÚoprA   r>   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Úcolonrc   z: )r   Úequalr¹   z = )r:   rx   rA   r>   Ú
annotationr»   )r   r   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   )rA   r>   r:   r»   )r   r   r   r   r   Úvisit_AwaitK  s    zBaseVisitor.visit_Awaitc             C   s   |   d¡ d S )NÚbreak)rR   )r   r   r   r   r   Úvisit_BreakP  s    zBaseVisitor.visit_Breakc             C   s   |   d¡ d S )NÚcontinue)rR   )r   r   r   r   r   Úvisit_ContinueT  s    zBaseVisitor.visit_Continuec             C   sl   | j |dd| jgdd xNt|jƒD ]@\}}|  |¡ ||jd k	r$| j |d| | jd| jgdd q$W d S )NÚdelzdel )r   r+   zcomma_%dr€   z, )rA   r>   r‰   rº   r:   )r   r   rŠ   rx   r   r   r   Úvisit_DeleteX  s
    
zBaseVisitor.visit_Deletec          	   C   sª   | j |dd| jgdd | j|ddddv |  |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 Q R X d S )NÚexec)r   rd   FT)r
   rP   Z
in_globalsrw   r€   z, Z	in_locals)rA   r>   r   r:   rd   Úglobalsr\   Úlocals)r   r   r   r   r   Ú
visit_Exec`  s    zBaseVisitor.visit_Execc             C   s   |   |j¡ d S )N)r:   r»   )r   r   r   r   r   Ú
visit_Expro  s    zBaseVisitor.visit_Exprc             C   s^   |   d¡ g }x<|jD ]2}||jd kr8| | jdg¡ | | j|g¡ qW |  |d|¡ d S )NÚglobalr   r€   Únames)rR   rÓ   Úextendr>   rA   )r   r   ÚidentifiersÚidentr   r   r   Úvisit_Globals  s    
zBaseVisitor.visit_Globalc             C   st   |   d¡ xdt|jƒD ]V\}}| j|d| | jgdd |  |¡ ||jd kr| j|d| | jdgdd qW d S )NÚimportzalias_prefix_%dra   )r   r+   zalias_sep_%dr€   )rR   r‰   rÓ   rA   r>   r:   )r   r   rŠ   Úaliasr   r   r   Úvisit_Import}  s    

zBaseVisitor.visit_Importc          	   C   s^  |   d¡ | j|d| jgdd g }|jdkrF| |  |j¡| jg¡ |jr”|j d¡}x(|d d… D ]}|| j|| jdg7 }qfW || j|d g7 }| j|d|d	d|j |jp°d
 d | j|d| jgdd |   d¡ | j|dddl xdt	|j
ƒD ]V\}}| j|d| | jgdd |  |¡ ||j
d k	rö| j|d| | jdgdd qöW W d Q R X d S )Nr´   Zmodule_prefixra   )r   r   rV   r+   r(   )Úlevelr(   r   )rU   r   Zmodule_suffixrØ   rÓ   T)r
   zalias_prefix_%dzalias_sep_%dr€   )rR   rA   r>   rÛ   rÔ   rX   r(   Úsplitr   r‰   rÓ   r:   )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   )r:   rx   rA   rŠ   r>   r»   )r   r   r   r   r   Úvisit_NamedExpr¡  s    
 zBaseVisitor.visit_NamedExprc             C   s^   |   d¡ g }x<|jD ]2}||jd kr8| | jdg¡ | | j|g¡ qW |  |d|¡ d S )NÚnonlocalr   r€   rÓ   )rR   rÓ   rÔ   r>   rA   )r   r   rÕ   rÖ   r   r   r   Úvisit_Nonlocal§  s    
zBaseVisitor.visit_Nonlocalc             C   s   |   d¡ d S )NÚpass)rR   )r   r   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 xnt|jƒD ]`\}}|  |¡ ||jd k	rÒ| j |d| | jd| jgd	d q|js| j |d
| jdgdd qW d S )NZ
print_openÚprintzprint )r   Zredirectionz>>Zvalues_prefixr€   z, r
   r+   zcomma_%d)rA   r>   Údestr:   Ú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_prefixra   )r   )rR   r»   rA   r>   r:   )r   r   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_prefixra   )r   )rR   r»   rA   r>   r:   )r   r   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   )rA   r>   r:   r»   )r   r   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ÚdotrV   )r   )r:   r»   rA   r>   rR   )r   r   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¿   z %s )r¿   )r   rU   )	r   r¾   r¬   r¿   r:   ÚleftrA   r>   Úright)r   r   Ú	op_symbolr   r   r   Úvisit_BinOpå  s
    zBaseVisitor.visit_BinOpc             C   sn   t jt|jƒ d }xTt|jƒD ]F\}}|  |¡ ||jd k	r | j|d| | j|| jgd| dd q W d S )Nr   r+   zop_%dz %s )r¿   )r   rU   )	r   r¾   r¬   r¿   r‰   rç   r:   rA   r>   )r   r   ró   rŠ   r»   r   r   r   Úvisit_BoolOpí  s    
zBaseVisitor.visit_BoolOpc          	   C   sf   |   |j¡ | j|ddd@ tjd d… dkr<|  |¡}n
|  |¡}|rX|  |dd¡ W d Q R X d S )NÚ	argumentsT)rP   é   )é   é   r
   r€   )r:   Úfuncr   ÚsysÚversion_infoÚvisit_Call_arguments35Úvisit_Call_argumentsrZ   )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¡ xvt|ƒD ]j\}\}}|d k	r¨| j|d
| | j|g|d |  	|¡ ||d d k	rx| j|d| | jd| jgdd qxW t
|ƒS )Nc             S   s2   | d }t |tjƒr|j}t|ddƒt|ddƒfS )Nr'   r*   r   Ú
col_offset)rj   rk   r–   r»   r-   )ÚtupÚargr   r   r   Úarg_location  s    z6BaseVisitor.visit_Call_arguments.<locals>.arg_locationc             S   s   g | ]}d |f‘qS )Nr   )Ú.0Úkwr   r   r   ú
<listcomp>  s    z4BaseVisitor.visit_Call_arguments.<locals>.<listcomp>Ú*)Úkeyc             S   s   g | ]}d |f‘qS )Nr   )r  r  r   r   r   r    s    c             S   s   g | ]}d |f‘qS )Nr   )r  Únr   r   r   r    s    z**z	%s_prefix)r   r+   r'   zcomma_%dr€   z, )ZstarargsÚsortedr‘   r   r   r8   r‰   rA   r>   r:   r“   )r   r   r  Zsorted_keywordsÚall_argsrŠ   r   r  r   r   r   rþ     s     

$z BaseVisitor.visit_Call_argumentsc             C   sv   dd„ }t |j|j t |¡d}xJt|ƒD ]>\}}|  |¡ ||d k	r,| j|d| | jd| jgdd q,W t	|ƒS )	Nc             S   s€   dd„ }|| ƒr t |tjƒr dS ||ƒr8t | tjƒr8dS dd„ }|| ƒ}||ƒ}d|ks`d|k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 )N)rj   rk   r–   ÚStarred)Úar   r   r   Úis_arg!  s    zGBaseVisitor.visit_Call_arguments35.<locals>.arg_compare.<locals>.is_argr+   r'   c             S   s*   t | tjƒr| j} t| dd ƒt| dd ƒfS )Nr*   r   )rj   rk   r–   r»   r-   )r  r   r   r   Úget_pos-  s    zHBaseVisitor.visit_Call_arguments35.<locals>.arg_compare.<locals>.get_posNr   )rj   rk   r–   )Za1Za2r  r  Zpos1Úpos2r   r   r   Úarg_compare  s    z7BaseVisitor.visit_Call_arguments35.<locals>.arg_compare)r  r+   zcomma_%dr€   z, )r   )
r
  r   r‘   Ú	functoolsÚ
cmp_to_keyr‰   r:   rA   r>   r“   )r   r   r  r  rŠ   r  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   )rA   r>   r:   r»   )r   r   r   r   r   Úvisit_StarredG  s    zBaseVisitor.visit_Starredc             C   s|   |   |j¡ xjtt|j|jƒƒD ]T\}\}}| j|d| | jgdd |   |¡ | j|d| | jgdd |   |¡ q W d S )Nzop_prefix_%dra   )r   zop_suffix_%d)r:   rñ   r‰   r   ZopsZcomparatorsrA   r>   )r   r   rŠ   r¿   Z
comparatorr   r   r   Úvisit_CompareK  s     
zBaseVisitor.visit_Comparec             C   sê   |   d¡ x°ttt|jƒƒ|j|jƒD ]’\}}}|d krV| j|d| | jdgdd n*|  |¡ | j|d| | jd| jgdd |  |¡ ||jd k	r&| j|d	| | jd
| jgdd q&W | j	|dd
dd | j|d| jdgdd d S )NÚ{zstarstar_%dz**)r   zkey_val_sep_%drc   z: r+   zcomma_%dr€   z, Ú
extracommaT)Úallow_whitespace_prefixZclose_prefixÚ})
rR   r   Úrangeri   Úkeysrç   rA   r>   r:   rZ   )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¡ x|jD ]}|  |¡ qTW | j |d| jdgdd d S )	NZ	open_dictr  )r   Zkey_val_seprc   z: Z
close_dictr  )rA   r>   r:   r  r»   Ú
generators)r   r   Úcompr   r   r   Úvisit_DictCompe  s    zBaseVisitor.visit_DictCompc             C   s   |   |¡ d S )N)Ú	_comp_exp)r   r   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 )Nr`   z if )r   re   z else )r:   rd   rA   r>   rh   rg   )r   r   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_lambdarc   z: )rA   r>   r:   r   rd   )r   r   r   r   r   Úvisit_Lambda{  s    zBaseVisitor.visit_Lambdac             C   sœ   | j |dd| jgdd xNt|jƒD ]@\}}|  |¡ ||jd k	r$| j |d| | jd| jgdd q$W |jr€| j|ddd	d
 | j |d| jdgdd d S )NZ	list_openú[)r   r+   zcomma_%dr€   z, r  T)r  Z
list_closeú])rA   r>   r‰   Úeltsr:   rZ   )r   r   rŠ   Úeltr   r   r   Ú
visit_List‚  s    
$zBaseVisitor.visit_Listc             C   s   | j |ddd d S )Nr&  r'  )Ú
open_braceÚclose_brace)r!  )r   r   r   r   r   Úvisit_ListComp  s    zBaseVisitor.visit_ListCompc             C   sj   |r| j |d|| jg|d |  |j¡ x t|jƒD ]\}}|  |¡ q4W |rf| j |d| j|g|d d S )NZcompexp_open)r   Zcompexp_close)rA   r>   r:   r)  r‰   r  )r   r   r+  r,  rŠ   r  r   r   r   r!  “  s    zBaseVisitor._comp_expc             C   s   |   |j¡ d S )N)rR   Úid)r   r   r   r   r   Ú
visit_Name  s    zBaseVisitor.visit_Namec             C   s   |   t|jƒ¡ d S )N)rR   Ústrr»   )r   r   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_close)rA   r>   r:   r»   )r   r   r   r   r   Ú
visit_Repr¥  s    zBaseVisitor.visit_Reprc             C   s˜   | j |dd| jgdd xbt|jƒD ]T\}}|  |¡ ||jd k	rf| j |d| | jd| jgdd q$| j|ddd	d
 q$W | j |d| jdgdd d S )NZset_openr  )r   r+   zcomma_%dr€   z, r  T)r  Z	set_closer  )rA   r>   r‰   r(  r:   rZ   )r   r   rŠ   r)  r   r   r   Ú	visit_Set«  s    
"
zBaseVisitor.visit_Setc             C   s   | j |ddd d S )Nr  r  )r+  r,  )r!  )r   r   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_openr&  )r   Zslice_closer'  )r:   r»   rA   r>   Úslice)r   r   r   r   r   Úvisit_Subscript½  s    zBaseVisitor.visit_Subscriptc          	   C   s   | j |dddv xnt|jƒD ]`\}}|  |¡ ||jd k	r`| j|d| | jd| jgdd q| j|d	ddt|jƒd
kd qW W d Q R X d S )Nr(  T)rP   r+   zcomma_%dr€   z, )r   r  r'   )r  r   )r   r‰   r(  r:   rA   r>   rZ   ri   )r   r   rŠ   r)  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   rU   )r   r¾   r¬   r¿   rA   r>   r:   Úoperand)r   r   ró   r   r   r   Úvisit_UnaryOpÑ  s    zBaseVisitor.visit_UnaryOpc             C   s   |   d¡ d S )Nz...)rR   )r   r   r   r   r   Úvisit_EllipsisÛ  s    zBaseVisitor.visit_Ellipsisc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Ú	visit_Addß  s    zBaseVisitor.visit_Addc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Ú	visit_Subâ  s    zBaseVisitor.visit_Subc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Ú
visit_Multå  s    zBaseVisitor.visit_Multc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Ú	visit_Divè  s    zBaseVisitor.visit_Divc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Ú	visit_Modë  s    zBaseVisitor.visit_Modc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Ú	visit_Powî  s    zBaseVisitor.visit_Powc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Úvisit_LShiftñ  s    zBaseVisitor.visit_LShiftc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Úvisit_RShiftô  s    zBaseVisitor.visit_RShiftc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Úvisit_BitAnd÷  s    zBaseVisitor.visit_BitAndc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Úvisit_BitOrú  s    zBaseVisitor.visit_BitOrc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Úvisit_BitXorý  s    zBaseVisitor.visit_BitXorc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Úvisit_FloorDiv   s    zBaseVisitor.visit_FloorDivc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Úvisit_Invert  s    zBaseVisitor.visit_Invertc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Ú	visit_Not  s    zBaseVisitor.visit_Notc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Ú
visit_UAdd	  s    zBaseVisitor.visit_UAddc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Ú
visit_USub  s    zBaseVisitor.visit_USubc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Úvisit_Eq  s    zBaseVisitor.visit_Eqc             C   s   |   |d|  dd¡g¡ d S )Nr½   z!=z<>)rA   r\   )r   r   r   r   r   Úvisit_NotEq  s    zBaseVisitor.visit_NotEqc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Úvisit_Lt  s    zBaseVisitor.visit_Ltc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Ú	visit_LtE  s    zBaseVisitor.visit_LtEc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Úvisit_Gt  s    zBaseVisitor.visit_Gtc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Ú	visit_GtE  s    zBaseVisitor.visit_GtEc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   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 not)r   )rA   r>   )r   r   r   r   r   Úvisit_IsNot$  s    zBaseVisitor.visit_IsNotc             C   s   |   tjt|ƒ d ¡ d S )Nr   )rR   r   r¾   r¬   )r   r   r   r   r   Úvisit_In'  s    zBaseVisitor.visit_Inc             C   s   | j |dd| jdgdd d S )NrS  rU  rw   znot in)r   )rA   r>   )r   r   r   r   r   Úvisit_NotIn*  s    zBaseVisitor.visit_NotInc             C   s˜   g }|j  d¡}x(|d d… D ]}|| j|| jdg7 }qW || j|d g7 }| j|d|d|j d |jd k	r”| j|d| jd| jgdd	 |  |j¡ d S )
NrV   r+   r   )r   )rU   r   Úasnamer   z as )r   )r   rÜ   r>   rA   rY  rR   )r   r   Zname_patternrÝ   rÞ   r   r   r   Úvisit_alias1  s    


zBaseVisitor.visit_aliasc             C   sB   |   |j¡ |jd k	r>| j|d| jd| jgdd |  |j¡ d S )NZannotation_prefixrc   z: )r   )rR   r  rÃ   rA   r>   r:   )r   r   r   r   r   Ú	visit_arg>  s
    
zBaseVisitor.visit_argc          	   C   s^  t |dg ƒ}t |dg ƒ}t|ƒt|ƒks,t‚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}x~|D ]v}	|  |	¡ |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	 qÂW xˆtt||jƒƒD ]t\}
\}	}|  |	¡ | j	|d|
 | j
d| j
gdd	 |  |¡ |d7 }||k rN| j	|d| | j
d| j
gdd	 qNW |jrD| j	|d| j
d| j
gdd	 t|jtjƒr|  |j¡ n|  |j¡ |  	|d| j
g¡ |d7 }||k rh|  d¡ n$|rh|  	|d| j
d| j
d| j
g¡ xtt||ƒƒD ]~\}
\}	}|  |	¡ |d k	rÂ| j	|d|
 | j
d| j
gdd	 |  |¡ |d7 }||k rx| j	|d| | j
d| j
gdd	 qxW |jrZ| j	|d| j
d| j
gdd	 t|jtjƒr<|  |j¡ n|  |j¡ |  	|d| j
g¡ d S )Nrš   Úkw_defaultsZposonlyargsr'   r   zcomma_%dr€   z, )r   Zposonly_sepú/z/, z
default_%dr¹   Zvararg_prefixr  Zvararg_suffixZ
kwonly_sepzkw_default_%dZkwarg_prefixz**Zkwarg_suffix)r-   ri   r<   rœ   r   r   rž   Údefaultsr:   rA   r>   r‰   r   rj   rk   r­   rR   )r   r   rš   r\  Z
total_argsZarg_iZpos_argsÚ
positionalr–   r  rŠ   r   r   r   r   Úvisit_argumentsF  sp    "










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¡ x@t|jƒD ]2\}}| j|d
| | jd| jgdd |  |¡ qŒW d S )NZis_asyncFrv   ru   z async for )r   z for rw   z in zif_%dr`   z if )r-   rA   r>   r:   rx   ry   r‰   Zifs)r   r   rŠ   Zif_exprr   r   r   Úvisit_comprehension  s    
 zBaseVisitor.visit_comprehensionc             C   sX   |j d kr$| j|dd| jgdd n$|  |j ¡ | j|d| jdgdd |  |j¡ d S )NÚstarsz**)r   Úeqr¹   )r  rA   r>   rR   r:   r»   )r   r   r   r   r   Úvisit_keyword›  s
    
zBaseVisitor.visit_keywordc             C   s   |   |j¡ d S )N)r:   r»   )r   r   r   r   r   Úvisit_Index¤  s    zBaseVisitor.visit_Indexc             C   sf   xNt |jƒD ]@\}}|  |¡ ||jd k	r| j|d| | jd| jgdd qW | j|dddd d S )Nr+   z
dim_sep_%dr€   z, )r   r
   F)r‰   Zdimsr:   rA   r>   rZ   )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
lowerspacerc   )r   Z
stepspace1Z
step_colonZ
stepspace2Zstep_colon_2T)	Úlowerr:   rA   r>   ÚupperrZ   ÚstepÚcheck_slice_includes_stepÚis_explicit_step)r   r   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 )zFHelper function for Slice node to determine whether to visit its step.Frl  TÚNone)rj  r-   rj   rk   ÚNamer.  )r   r   r   r   r   rk  À  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 )
Nr+   Ú
conversionú!)rp  z!%c)rU   r   Zformat_spec_prefixrc   )r   )r:   r»   rp  rA   r>   ÚchrÚformat_spec)r   r   r   r   r   Úvisit_FormattedValueË  s    
z BaseVisitor.visit_FormattedValue)r   )NFFr   )NFF)NN)NFT)F)NN)†Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚabcÚABCMetaZ__metaclass__r7   r:   r   r   rJ   rK   r    Úcontextmanagerr   rR   rA   r>   rX   rY   rZ   r\   r(   r^   r2   rq   Úabstractmethodrm   rs   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í   rî   rð   rô   rõ   rÿ   rþ   rý   r  r  r  r   r"  r#  r%  r*  r-  r!  r/  r1  r3  r4  r5  r7  r8  r:  r;  r<  r=  r>  r?  r@  rA  rB  rC  rD  rE  rF  rG  rH  rI  rJ  rK  rL  rM  rN  rO  rP  rQ  rR  rV  rW  rX  rZ  r[  r`  ra  rd  r&   re  rg  rm  rk  r$   rt  Ú__classcell__r   r   )r=   r   r3   o   sð   

 



	$

	

	)	



G	r3   c               @   s   e Zd ZdZdS )ÚAnnotationErrorz5An exception for when we failed to annotate the tree.N)ru  rv  rw  rx  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 )N)r9   r  r7   r	   ZTokenGeneratorÚtokens)r   Úsource)r=   r   r   r7   Þ  s    zAstAnnotator.__init__c                sl   y4t  |d| j¡ t  |d| j¡ tt| ƒ |¡ W n2 ttt	t
fk
rf } zt|ƒ‚W d d }~X Y nX d S )Nr1   rC   )r.   rn   r   r5   r9   r  r:   Ú	TypeErrorÚ
ValueErrorÚ
IndexErrorÚKeyErrorr~  )r   r   Úe)r=   r   r   r:   â  s    zAstAnnotator.visitc             c   sî   t ||ƒ}| jj}| j ¡ j}|d |d krX| j}d| _x|D ]
}|V  q@W || _dS | j}| j}	| j dd„ ¡}
|
j}t	||ƒ}|s¢d}t
d|d  tjd || _|| _x|D ]
}|V  q´W t |d| | j | j¡¡ || _|	| _dS )	zCGenerator which annotates child nodes with their indentation level.r   Nc             S   s   | j tjjkS )N)r¬   r	   ÚTOKENSÚINDENT)Útr   r   r   r?   þ  r@   z'AstAnnotator.indented.<locals>.<lambda>z    zbIndent detection failed (line %d); inner indentation level is not more than the outer indentation.)Úfilezblock_suffix_%s)r-   r€  Z_locÚpeek_non_whitespaceÚstartr5   r   Zpeek_conditionalÚsrcÚ_get_indent_diffrå   rû   Ústderrr.   rn   Úblock_whitespace)r   r   rD   rE   Zcur_locZnext_locrC   rI   rF   rG   Zindent_tokenÚ
new_indentrH   r   r   r   rJ   ê  s8    






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 )N)r€  Znext_of_typer  r   )r   Útoken_number_typer   r   r?     r@   z(AstAnnotator.visit_Num.<locals>.<lambda>ú-r   rS  )r	  )rU   r   N)
r	   r‡  ÚNUMBERr€  Úpeekr  ÚinsertrA   r0  r	  )r   r   Zcontentargsr   )r   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.rS  )Ús)rU   r   N)rA   r€  r0  r˜  )r   r   r   r   r   Ú	visit_Str  s    zAstAnnotator.visit_Strc                s„   | j  ¡ ƒ }d‰ dd„ |jD ƒ}xBt|ƒ\}}ˆ |7 ‰ |dkr@P | j }|| _ |  t|ƒ¡ || _ q"W | 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 )N)rj   rk   ÚFormattedValue)r  Úvr   r   r   ú	<genexpr>&  s    z/AstAnnotator.visit_JoinedStr.<locals>.<genexpr>NrS  c                  s   ˆ S )Nr   r   )Úresr   r   r?   1  r@   z.AstAnnotator.visit_JoinedStr.<locals>.<lambda>)r   )r€  Zfstrrç   Únextr:   rA   )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.rS  )r˜  )rU   r   N)rA   r€  r0  r˜  )r   r   r   r   r   Úvisit_Bytes3  s    zAstAnnotator.visit_Bytesc             C   s<   | j  ¡ jdkr|  d¡ nxtdƒD ]}|  d¡ q&W d S )Nz...rø   rV   )r€  r•  r  rR   r  )r   r   rŠ   r   r   r   r;  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.r_   )r€  Z	next_namerj   rk   rl   r  )r   r   Znext_tokr   r   r   rm   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¢   )rj   rk   Z	TryExceptr€  r‹  r  )r   r   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€   )rj   rk   ÚWithr€  r•  r  )r   r   r   r   r   r„   L  s    z$AstAnnotator.check_is_continued_withc             C   s   | j  ¡ jdkS )zFHelper function for Slice node to determine whether to visit its step.z],)r€  r‹  r  )r   r   r   r   r   rk  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krb|| j jdd7 }|S | j j||dS )z;Parse some whitespace from the source tokens and return it.ú;r'   )r   )r   r   )
r€  r•  r  Ú
whitespacerR   r¬   r	   r‡  ÚNLÚNEWLINE)r   r   r   r   Ú
next_tokenÚresultr   r   r   r>   W  s    


zAstAnnotator.wsc                s   ‡ ‡fdd„}|S )zParse a number of dots.c                  s   ˆj  ˆ ¡S )N)r€  rX   r   )rW   r   r   r   Ú_parse_dotse  s    z&AstAnnotator.dots.<locals>._parse_dotsr   )r   rW   r©  r   )rW   r   r   rX   c  s    zAstAnnotator.dotsc             C   s   t  |d| j |¡¡ d S )Nr   )r.   rn   r€  r  )r   r   Úindent_levelr   r   r   r)   i  s    zAstAnnotator.block_suffixc             C   sb   | j  ¡ }|j|kr2td||j|jd |jf ƒ‚|jdkrH| j  ¡  n|jdk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   rQ   rR   r   r   r   rR   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)r.   r8   r€  r‹  r•  r  r>   rž  )r   r   rS   rQ   r  r   rR   Úparsedr   r   r   rZ   {  s    zAstAnnotator.optional_tokenc                s   ‡ ‡fdd„}|S )z%Account for one of the given symbols.c                 s@   ˆj  ¡ ‰ t‡ fdd„ˆD ƒd ƒ} | d kr<tdˆˆ jf ƒ‚| S )Nc             3   s   | ]}|ˆ j kr|V  qd S )N)r  )r  r˜  )r§  r   r   rœ    s    zGAstAnnotator.one_of_symbols.<locals>._one_of_symbols.<locals>.<genexpr>z"Expected one of: %r, but found: %r)r€  rž  r~  r  )Úfound)r   r[   )r§  r   Ú_one_of_symbols‹  s    
z4AstAnnotator.one_of_symbols.<locals>._one_of_symbolsr   )r   r[   r®  r   )r   r[   r   r\   ‰  s    zAstAnnotator.one_of_symbolsc       	   	   C   s‚   ~|r.x&|D ]}t  ||d t||dƒ¡ qW g }x6|D ].}t|tjƒrZ| |  |¡¡ q8| |ƒ ¡ q8W t  ||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   )	r.   rn   r-   rj   ÚsixÚstring_typesr8   rR   Újoin)	r   r   rS   rT   rU   r   ÚdepZ
attr_partsZattr_valr   r   r   rA   ”  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.
    )rA   r
   )r€  r   )r   r   rA   r
   rP   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ž  r>   )r   Ú
token_typerQ   rR   r   r   r   Ú_optional_tokenÏ  s
    

zAstAnnotator._optional_token)NFT)FF)NN)NFF)ru  rv  rw  r7   r:   rJ   r#   r—  r™  r   r¡  r%   r;  rm   r‡   r„   rk  r>   rX   r)   rR   rZ   r\   rA   r   r´  r}  r   r   )r=   r   r  Ü  s*   )	



,
r  c             C   s@   d}x6| D ].}|dkr |d7 }q
|dkr
|d|d  7 }q
W |S )Nr   ra   r'   ú	é   r   )r1   ÚwidthÚcr   r   r   Ú_get_indent_widthØ  s    

r¹  c             C   s   d}x~t | ƒD ]r\}}||kr P |dkr2|d7 }q|dkr|d |d  |kr`|d|d  7 }qd|d |  | |d d …  S qW | |d … S )Nr   ra   r'   rµ  r¶  )r‰   )r1   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)&rx  Ú
__future__r   r   r   ry  rk   r    r  Ú	itertoolsr¯  Z	six.movesr   rû   Z
pasta.baser   r   r   r.   r	   r"   r{  r   r#   r$   r%   r&   r   r(   r2   ÚNodeVisitorr3   Ú	Exceptionr~  r  r¹  rº  rŽ  r   r   r   r   Ú<module>   sP    
        q }
