
    BVh&%                         d 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 ddlmZ  G d dej                        Z
 G d	 d
ej                        Zd Zd Zd Zy)z1AST conversion templates.

Adapted from Tangent.
    N)anno)ast_util)parser)
qual_namesc                   d     e Zd ZdZd Z fdZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Z xZS )ContextAdjusterzAdjusts the ctx field of nodes to ensure consistency.

  This transformer can change the ctx fields of a variable, tuple and other
  AST elements that allow one, based on whether the element is being read or
  written.
  c                     || _         y N)_ctx_override)selfoverride_values     Z/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/autograph/pyct/templates.py__init__zContextAdjuster.__init__'   s
    'D    c                     | j                   }t        t        |   |      }t	        |d      r"|j
                  J dj                  |             || _         |S )Nctxznode {} has ctx unset)r   superr   visithasattrr   format)r   nodeoriginal_override	__class__s      r   r   zContextAdjuster.visit*   sX    **$-d3DtUXX!G#:#A#A$#GG!*DKr   c                 H    | j                   | j                         |_        y y r
   )r   r   r   r   s     r   _apply_overridezContextAdjuster._apply_override2   s#    %##%dh &r   c                 t    | j                  |       t        j                  | _        | j	                  |      }|S r
   )r   gastLoadr   generic_visitr   s     r   visit_AttributezContextAdjuster.visit_Attribute6   s1    Dd#DKr   c                 F    | j                  |       | j                  |      S r
   r   r    r   s     r   visit_TuplezContextAdjuster.visit_Tuple<   !    d##r   c                 F    | j                  |       | j                  |      S r
   r#   r   s     r   
visit_ListzContextAdjuster.visit_List@   r%   r   c                 F    | j                  |       | j                  |      S r
   r#   r   s     r   
visit_NamezContextAdjuster.visit_NameD   r%   r   c                 T    | j                  |       d | _        | j                  |      S r
   )r   r   r    r   s     r   
visit_CallzContextAdjuster.visit_CallH   s+     Dd##r   c                 2    d | _         | j                  |      S r
   r   r    r   s     r   
visit_DictzContextAdjuster.visit_DictO        Dd##r   c                     | j                  |       t        j                  | _        | j	                  |j
                        |_        | j                  |      S r
   )r   r   r   r   r   valuer    r   s     r   visit_SubscriptzContextAdjuster.visit_SubscriptU   sA    DDJJ'DJd##r   c                 2    d | _         | j                  |      S r
   r-   r   s     r   visit_comprehensionz#ContextAdjuster.visit_comprehension[   r/   r   c                 2    d | _         | j                  |      S r
   r-   r   s     r   visit_LambdazContextAdjuster.visit_Lambdaa   r/   r   )__name__
__module____qualname____doc__r   r   r   r!   r$   r'   r)   r+   r.   r2   r4   r6   __classcell__)r   s   @r   r   r      sD    (&$$$$$$$$r   r   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
ReplaceTransformerzReplace AST nodes.c                 *   || _         d| _        t        j                  j                  t        j                  j
                  t        j                  j                  t        j                  j                  t        j                  j                  dh| _
        y)zCreate a new ReplaceTransformer.

    Args:
      replacements: A mapping from placeholder names to (lists of) AST nodes
          that these placeholders will be replaced by.
    Ffunction_context_nameN)replacementsin_replacementsr   Basic
DIRECTIVESEXTRA_LOOP_TESTORIGINSKIP_PROCESSINGStaticORIG_DEFINITIONSpreserved_annos)r   r@   s     r   r   zReplaceTransformer.__init__k   sb     %D D



""



""$$Dr   c                     | j                   |   }t        j                  || j                        }t	        |t
        j                        r|g}|S )zPrepares a replacement AST that's safe to swap in for a node.

    Args:
      replaced: ast.AST, the node being replaced
      key: Hashable, the key of the replacement AST
    Returns:
      ast.AST, the replacement AST
    )preserve_annos)r@   r   
copy_cleanrI   
isinstancer   AST)r   replacedkeyrepl	new_nodess        r   _prepare_replacementz'ReplaceTransformer._prepare_replacement}   sG     S!D##D9M9MNI)TXX&+ir   c                 \    | j                  |j                        }||j                  u r|S |S r
   )r   r1   )r   r   	new_values      r   
visit_ExprzReplaceTransformer.visit_Expr   s-     

4::&IDJJkr   c                 f   |j                   | j                  vr| j                  |      S | j                  ||j                         }t	        |t
        j                        r|S |r*t	        |t        t        f      rt        d |D              r|S t        dj                  ||j                               )Nc              3   P   K   | ]  }t        |t        j                           y wr
   )rM   r   keyword).0rs     r   	<genexpr>z3ReplaceTransformer.visit_keyword.<locals>.<genexpr>   s     8ajDLL)8s   $&zta keyword argument may only be replaced by another keyword or a non-empty list of keywords. Found: {} for keyword {})argr@   r    rS   rM   r   rY   listtupleall
ValueErrorr   r   r   rQ   s      r   visit_keywordz ReplaceTransformer.visit_keyword   s    xxt(((%%$$T4884D$%k
:dT5M2
848
8k 	??Ev$((@ r   c                     | j                  |      }|j                  | j                  vr|S | j                  |j                     }t        |t        j
                  t        j
                  f      st        d|z        |j                  |_        |S )Nz>a function name can only be replaced by a Name node. Found: %s)	r    namer@   rM   r   Nameastra   idrb   s      r   visit_FunctionDefz$ReplaceTransformer.visit_FunctionDef   s~    d#Dyy)))kTYY'DdTYY12
J
  DIKr   c                     | j                  |      }|j                  | j                  vr|S | j                  |j                     }t        |t        j
                        st        d|z        |j                  |_        |S )Nz;An attribute can only be replaced by a Name node. Found: %s)r    attrr@   rM   r   rf   ra   rh   rb   s      r   r!   z"ReplaceTransformer.visit_Attribute   sr    d#Dyy)))kTYY'DdDII&
G$
NP PDIKr   c                 $   |j                   | j                  vr|S | j                  ||j                         }|s|S t        t	        |j
                              }|D ]   }t        |d      s|j                  |       " t        |      dk(  r|\  }|S )Nr      )	rh   r@   rS   r   typer   r   r   len)r   r   rR   adjusterns        r   r)   zReplaceTransformer.visit_Name   s    wwd'''k))$8I tDHH~.H 	E	q 9~jir   N)r7   r8   r9   r:   r   rS   rV   rc   ri   r!   r)    r   r   r=   r=   h   s(    $"$
r   r=   c                 L   t        | t              rt        j                  | ddd      S t        | t        j
                        r| j                         S t        | t              r| D cg c]  }t        |       c}S t        | t              rt        d | D              S | S c c}w )z'Converts from a known data type to AST.N)rh   r   
annotationtype_commentc              3   2   K   | ]  }t        |        y wr
   )_convert_to_ast)rZ   es     r   r\   z"_convert_to_ast.<locals>.<genexpr>   s     /#/s   )
rM   strr   rf   r   QNrg   r^   rw   r_   )rq   rx   s     r   rw   rw      s    
 399t4HH:==!557N4()*1OA**5/Q///	
( +s   (B!c                 "   t        | t              st        dt        |       z        |D ]  }t	        ||         ||<    t
        j                  t        j                  |       z   }t        j                  |t
        j                  d      }g }|D ]U  }t        |      j                  |      }t        |t        t        f      r|j                  |       E|j!                  |       W |D cg c]  }t#        j$                  |       }}|S c c}w )aB  Replaces placeholders in a Python template.

  AST Name and Tuple nodes always receive the context that inferred from
  the template. However, when replacing more complex nodes (that can potentially
  contain Name children), then the caller is responsible for setting the
  appropriate context.

  Args:
    template: A string representing Python code. Any symbol name can be used
        that appears in the template code can be used as placeholder.
    **replacements: A mapping from placeholder names to (lists of) AST nodes
        that these placeholders will be replaced by. String values are also
        supported as a shorthand for AST Name nodes with the respective ID.

  Returns:
    An AST node or list of AST nodes with the replacements made. If the
    template was a function, a list will be returned. If the template was a
    node, the same node will be returned. If the template was a string, an
    AST node will be returned (a `Module` node in the case of a multi-line
    string, an `Expr` node otherwise).

  Raises:
    ValueError: if the arguments are incorrect.
  z Expected string template, got %sF)preamble_lensingle_node)rM   ry   ra   rn   rw   r   STANDARD_PREAMBLEtextwrapdedentparseSTANDARD_PREAMBLE_LENr=   r   r^   r_   extendappendr   resolve)templater@   ktemplate_strnodesresultsr   r[   s           r   replacer      s    2 
Hc	"
7$x.H
II 7a%l1o6LO7))HOOH,EE,
,,//% ' dl+11$7D$u&nnTnnT -44qZ"4'4	. 5s   ,Dc                     t        | fi |}t        |      dk7  rt        d      |\  }t        |t        j
                        r|j                  S t        |t        j                        r|S t        d|z        )zFVariant of replace that generates expressions, instead of code blocks.rm   zBsingle expression expected; for more general templates use replacezSthe template is expected to generate an expression or a name node; instead found %s)r   ro   ra   rM   r   Exprr1   rf   )r   r@   replacementr   s       r   replace_as_expressionr     s|    1L1+
LN N
%$dii ::$		"K !	" "r   )r:   rg   r   r    tensorflow.python.autograph.pyctr   r   r   r   NodeTransformerr   r=   rw   r   r   rr   r   r   <module>r      s[   
    1 5 3 7F$d** F$Rk-- k\ *Z"r   