a
    ,=ic                     @   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m	Z	 ddlm
Z ddlmZ G d	d
 d
eZG dd de	jZdd ZdS )z,Generate code from an annotated syntax tree.    )absolute_import)division)print_functionN)annotate)
formatting)fstring_utilsc                   @   s   e Zd ZdZdS )
PrintErrorz2An exception for when we failed to print the tree.N)__name__
__module____qualname____doc__ r   r   c/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/pasta/base/codegen.pyr      s   r   c                       s   e Zd ZdZ fddZ fddZdd Zdd	 Zd
d Zdd Z	dd Z
dddZdddZdd Zdd Zdd Z  ZS )Printera  Traverses an AST and generates formatted python source code.
  
  This uses the same base visitor as annotating the AST, but instead of eating a
  token it spits one out. For special formatting information which was stored on
  the node, this is output exactly as it was read in unless one or more of the
  dependency attributes used to generate it has changed, in which case its
  default formatting is used.
  c                    s   t t|   d| _d S N )superr   __init__code)self	__class__r   r   r   ,   s    zPrinter.__init__c                    sb   t dd |_ztt| | W n4 tttt	fyX } zt
|W Y d }~n
d }~0 0 |`d S )Nc                   S   s   dS )NFr   r   r   r   r   <lambda>1       zPrinter.visit.<locals>.<lambda>)collectionsdefaultdict_printer_infor   r   visit	TypeError
ValueError
IndexErrorKeyErrorr   )r   nodeer   r   r   r   0   s    zPrinter.visitc                 C   sD   |  | t|d}|  j|d ur(|nt|j7  _| | d S Ncontent)prefixfmtgetr   reprnsuffixr   r"   r%   r   r   r   	visit_Num8   s    
 zPrinter.visit_Numc                 C   sD   |  | t|d}|  j|d ur(|nt|j7  _| | d S r$   r&   r'   r(   r   r)   sr+   r,   r   r   r   	visit_Str>   s    
 zPrinter.visit_Strc                 C   s   |  | t|d}|d u rjg }|jD ]2}t|tjrF||j q(|t	
t| q(td|}dd t	|D }|  jt	||7  _| | d S )Nr%   r   c                 S   s   g | ]}t |qS r   )to_str).0vr   r   r   
<listcomp>Q   r   z+Printer.visit_JoinedStr.<locals>.<listcomp>)r&   r'   r(   values
isinstanceastStrappendr/   r   placeholderlenr)   joinZget_formatted_valuesr   Zperform_replacementsr+   )r   r"   r%   partsvalr5   r   r   r   visit_JoinedStrD   s    

zPrinter.visit_JoinedStrc                 C   sD   |  | t|d}|  j|d ur(|nt|j7  _| | d S r$   r.   r,   r   r   r   visit_BytesU   s    
 zPrinter.visit_Bytesc                 C   s   |  j |7  _ d S )N)r   )r   valuer   r   r   token[   s    zPrinter.tokenFc                 C   s4   ~t ||}|d u r|r|}|  j|p*d7  _d S r   )r'   r(   r   )r   r"   	attr_nameZ	token_valZallow_whitespace_prefixdefaultrA   r   r   r   optional_token^   s
    zPrinter.optional_tokenNc                    st   ~t  dr j| rdS d j|< t |}|du sR|rVt fdd|D rV|}|  j|durh|nd7  _dS )a  Add the formatted data stored for a given attribute on this node.

    If any of the dependent attributes of the node have changed since it was
    annotated, then the stored formatted data for this attr_name is no longer
    valid, and we must use the default instead.
    
    Arguments:
      node: (ast.AST) An AST node to retrieve formatting information from.
      attr_name: (string) Name to load the formatting information from.
      attr_vals: (list of functions/strings) Unused here.
      deps: (optional, set of strings) Attributes of the node which the stored
        formatting data depends on.
      default: (string) Default formatted data for this attribute.
    r   NTc                 3   s*   | ]"}t  |d t |d kV  qd S )NZ__src)getattrr'   r(   )r2   depr"   r   r   	<genexpr>{   s   zPrinter.attr.<locals>.<genexpr>r   )hasattrr   r'   r(   anyr   )r   r"   rC   Z	attr_valsdepsrD   r>   r   rH   r   attrf   s    
zPrinter.attrc                 C   s(   zt |dW S  ty"   Y dS 0 d S )NZis_elifF)r'   r(   AttributeErrorr   r"   r   r   r   check_is_elif   s    zPrinter.check_is_elifc                 C   s   t |ddS NZis_continuedFrF   rO   r   r   r   check_is_continued_try   s    zPrinter.check_is_continued_tryc                 C   s   t |ddS rQ   rR   rO   r   r   r   check_is_continued_with   s    zPrinter.check_is_continued_with)FF)NN)r	   r
   r   r   r   r   r-   r0   r?   r@   rB   rE   rM   rP   rS   rT   __classcell__r   r   r   r   r   "   s   	 

r   c                 C   s~   t  }tdd }t| D ]&}t|dd}|r||  d7  < q|rntt	|dd d\}}|
| ||  |jS )z8Convenient function to get the python source for an AST.c                   S   s   dS )Nr   r   r   r   r   r   r      r   zto_str.<locals>.<lambda>indent_diffr      c                 S   s   | d r| d S dS )Nr   rW   r   )tupr   r   r   r      r   )key)r   r   r   r7   walkr'   r(   maxsix	iteritemsZset_default_indent_diffr   r   )treepZseen_indent_diffsr"   rV   _r   r   r   r1      s    



r1   )r   
__future__r   r   r   r7   r   r]   Z
pasta.baser   r   r'   r   	Exceptionr   ZBaseVisitorr   r1   r   r   r   r   <module>   s   m