
    1Vh|                     p   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mZ ddl	m
Z  ej                  d      Zej                  ej                  ej                   ej"                  ej$                  ej&                  ej(                  ej*                  ej,                  ej.                  ej0                  ej2                  ej4                  ej6                  ej8                  ej:                  ej<                  ej>                  ej@                  ejB                  ejD                  ejF                  ejH                  ejJ                  ejL                  ejN                  ejP                  ejR                  fZ* G d	 d
ejV                        Z, e,       Z-d Z.d Z/d Z0 G d dejb                        Z2d Z3d Z4d Z5d Z6y)z%Helpers for working with python ASTs.    )absolute_import)division)print_functionN)errors)
formattingz+^[ 	]*#.*?coding[:=][ 	]*([-_.a-zA-Z0-9]+)c                   "     e Zd ZdZ fdZ xZS )_TreeNormalizerz,Replaces all op nodes with unique instances.c                 j    t        |t              r|j                         S t        t        |   |      S N)
isinstance_AST_OP_NODES	__class__superr	   visitselfnoder   s     D/home/dcms/DCMS/lib/python3.12/site-packages/pasta/base/ast_utils.pyr   z_TreeNormalizer.visit*   s,    $&^^$-d33    )__name__
__module____qualname____doc__r   __classcell__r   s   @r   r	   r	   '   s    44 4r   r	   c                 l    t        j                  t        |             }t        j	                  |       |S )zReplaces ast.parse; ensures additional properties on the parsed tree.

  This enforces the assumption that each node in the ast is unique.
  )astparsesanitize_source_tree_normalizerr   )srctrees     r   r   r   3   s*    
 
?3'	($	+r   c                     | j                  d      }t        |dd       D ]5  \  }}t        j                  |      st	        j
                  dd|      ||<   7 dj                  |      S )zStrip the 'coding' directive from python source code, if present.

  This is a workaround for https://bugs.python.org/issue18960. Also see PEP-0263.
  TN   z#.*$z# (removed coding) )
splitlines	enumerate_CODING_PATTERNmatchresubjoin)r!   	src_linesilines       r   r   r   =   sh    
 nnT")9Ra=) @gaT"VVF$8$?il@ 
	r   c                 Z    t        fd      }|j                  |        |j                  S )Nc                     t        |       S r   )r   )naccept_typess    r   <lambda>z$find_nodes_by_type.<locals>.<lambda>J   s    jL&A r   )FindNodeVisitorr   results)r   r3   visitors    ` r   find_nodes_by_typer8   I   s$    AB'	--	r   c                   $     e Zd Zd Z fdZ xZS )r5   c                      || _         g | _        y r   )
_conditionr6   )r   	conditions     r   __init__zFindNodeVisitor.__init__Q   s    DODLr   c                     | j                  |      r| j                  j                  |       t        t        |   |       y r   )r;   r6   appendr   r5   r   r   s     r   r   zFindNodeVisitor.visitU   s0    t
ll$	/4&t,r   )r   r   r   r=   r   r   r   s   @r   r5   r5   O   s    - -r   r5   c                 <   t        | t        j                        r	 | j                  d   S t        | t        j
                        rt        | j                        dk(  rbt        | j                  d   t        j
                        r;t        j                  | j                  d   d      rt        | j                  d         S | j                  r| j                  d   S t        | t        j                        r}t        | j                        dk(  rrt        | j                  d   t        j                        rJt        j                  | j                  d   d      r&t        | j                  d         S t        t        d      rPt        | t        j                        r6| j                  r| j                  d   S | j                  r| j                  d   S t        t        d      r5t        | t        j                        r| j                  rx| j                  d   S t        t        d	      rYt        | t        j                         r?| j                  r| j                  d   S | j"                  rt        | j"                  d         S | j                  d   S # t        $ r Y yw xY w)
aR  Get the last child node of a block statement.

  The input must be a block statement (e.g. ast.For, ast.With, etc).

  Examples:
    1. with first():
         second()
         last()

    2. try:
         first()
       except:
         second()
       finally:
         last()

  In both cases, the last child is the node for `last`.
  N   r   is_elifis_continuedTry
TryFinally	TryExcept)r   r   Modulebody
IndexErrorIflenorelsefmtgetget_last_childWithhasattrrE   	finalbodyrF   rG   handlersr   s    r   rP   rP   [   s   & cjj!YYr] cffDKKA*T[[^SVV"DA	*DKKN++{{[[_$!DII!
499Q< B		!n-DIIaL))sEz$8~~^^B{{[[_sL!js~~&F~~^^BsK Zcmm%D{{[[_}}DMM"-..	23  s   J 	JJc                     t        j                  |       D ]-  \  }}t        |t              s||v s|j	                  |        y  t        j                  d|d|       )Nz%Unable to find list containing child z on parent node )r   iter_fieldsr   listremover   InvalidAstError)parentchild_field_values       r   remove_childr_      sb    / na+t$+)= 	38& B 	C Cr   c                    t        |t        j                        rVt        j                  |dt        j                  |d             t        j                  |dt        j                  |d             | j
                  D ]J  }t        | |d      }||k(  rt        | ||        yt        |t              s5	 |||j                  |      <    y t        j                  d|d|       # t        $ r Y sw xY w)zReplace a node's child with another node while preserving formatting.

  Arguments:
    parent: (ast.AST) Parent node to replace a child of.
    node: (ast.AST) Child node to replace.
    replace_with: (ast.AST) New child node.
  prefixsuffixNzNode z is not a child of )rR   rN   
PASTA_DICTsetrO   _fieldsgetattrsetattrr   rX   index
ValueErrorr   rZ   )r[   r   replace_withfield	field_vals        r   replace_childrm      s     T3>>"GGL(CGGD($;<GGL(CGGD($;<~~ 
et,IDfe\*	It	$+7	)//$'(
 	vNOO  s   3C%%	C10C1c                     t        | d      xrh | j                  xrZ t        | j                  d   t        j                        xr1 t        | j                  d   j
                  t        j                        S )NrI   r   )rR   rI   r   r   ExprvalueStrrU   s    r   has_docstringrr      sX    
$
 2DII 2
TYYq\388
,2
TYYq\''
13r   )7r   
__future__r   r   r   r   r*   pasta.augmentr   
pasta.baser   rN   compiler(   AndOrEqNotEqIsIsNotInNotInLtLtEGtGtEAddSubMultDivModPowLShiftRShiftBitAndBitOrBitXorFloorDivInvertNotUAddUSubr   NodeTransformerr	   r    r   r   r8   NodeVisitorr5   rP   r_   rm   rr    r   r   <module>r      s:   + '  % 
 	   ( "**MN GGSVVSVVSYY		366399FFCGGSVVSWWcggsww#''GGSWWcjj#**cjj#))SZZLL#**cggsxx	4c)) 4 #$ 		-coo 	-/dCP43r   