
    BVh3                        d 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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  ej                   d      ZdZdZej(                  dk\  reZ	ej(                  d	k\  reZej,                  ZneZej.                  Zej1                  d
      Z ej4                  d      Zd Zd Zd Zd Zd Z d Z!d Z"ddZ#d Z$ddZ%y)z/Converting code to AST.

Adapted from Tangent.
    N)errors)inspect_utils)
tf_inspect
 )   	   )r   
__future__z\s*c                 &    | j                  dd      S )z7Removes any backslash line continuations from the code.z\
r   )replace)code_strings    W/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/autograph/pyct/parser.py_unfold_continuationsr   :   s    			VR	((    c                    t        |       } t        j                  t        j                  |       j
                        }d}g }	 |D ]  }|j                  |        	 |D ]p  }|\  }}}}}|t        j                  k(  r|}t        |      } nF|t        j                  t        j                  t        j                  t        j                  fvsnd} n |s| S t        |      }d|v }	t        |      D ]\  \  }
}|\  }}}}}|t        j                  k(  s"d|v r|	sd|v r|	st        j                   d      t        |      |k\  r||d }||f||
<   ^ t        j"                  |      }g }t%        | j'                  d      |j'                  d            D ]  \  }}t)        j*                  t,        |      j/                         }t)        j*                  t,        |      j/                         }t        |      t        |      kD  r|t        |      t        |      z
  d }n|}|j                  |        dj1                  |      }|S # t        j                  $ r Y w xY w)z9Dedents a code so that its first line starts at row zero.Nr   	 z:code mixing tabs and spaces for indentation is not allowedr   )r   tokenizegenerate_tokensioStringIOreadlineappend
TokenErrorINDENTlenNLNEWLINESTRINGCOMMENT	enumerater   UnsupportedLanguageElementError
untokenizezipsplitrematch_LEADING_WHITESPACEgroupjoin)r   	token_genblock_indentationtokenstoktok_type
tok_string_block_levelfirst_indent_uses_tabsinew_codededented_codelinenew_lineoriginal_indent
new_indentdedented_lines                     r   dedent_blockr<   ?   sf    &k2+&&r{{;'?'H'HI)&	 mmC  	c$'!Hj!Q8??"$)*k	X%%x8H8HJ 
J	 
%&+#44&! )fa$'!Hj!Q8??"*!7j )? 44HJ 	J	ZK	'-
Z(fQi)   (( -K--d3X^^D5IJ (ndHhh2D9??AO-x8>>@J
?c*o-3/#j/ABCmm'( YY}%(	/k 
		 	 			s   H? ?IIc                 Z   t        j                  |       rt        |       S 	 t        j                  |       }t        |      }t        d |D              }dj                  ||fz         }t        |t        |            |fS # t        $ r }t        j                  d|  d|       d}~ww xY w)a  Returns the AST and source code of given entity.

  Args:
    entity: Any, Python function/method/class
    future_features: Iterable[Text], future features to use (e.g.
      'print_statement'). See
      https://docs.python.org/2/reference/simple_stmts.html#future

  Returns:
    gast.AST, Text: the parsed AST node; the source code that was parsed to
    generate the AST (including any prefixes that this function may have added).
  z$Unable to locate the source code of a=  . Note that functions defined in certain environments, like the interactive Python shell, do not expose their source code. If that is the case, you should define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.experimental.do_not_convert. Original error: Nc              3   >   K   | ]  }d j                  |        yw)zfrom __future__ import {}N)format).0names     r   	<genexpr>zparse_entity.<locals>.<genexpr>   s#      L37!((.Ls   r   )preamble_len)r   islambda_parse_lambdagetimmediatesourceOSErrorr   InaccessibleSourceCodeErrorr<   tupler*   parser   )entityfuture_featuresoriginal_sourceesourcefuture_statementss         r   parse_entityrQ      s     F#  	L#66v>O (& L;JL L99&&23&	vC$8	96	AA 
 L

,
,
.vh 7G
 HIc	KL LLs   B 	B*
B%%B*c                    t        j                  |       D ]5  }t        |dd      }|
||z
  |_        t        |dd      }|,||z
  |_        7 ||dz
  | }t        | dd      }||d   d| |d<   t        | dd      }	|	,t        j                  d|d	         }
|
|
j                  d	      }	|	|d	   |	d |d	<   d
j                  |D cg c]  }|j                          c}      }| |fS c c}w )zCReturns a clean node and source code without indenting and context.linenoN
end_lineno   end_col_offset
col_offsetz(?<!\w)lambda(?!\w)r   r   )
gastwalkgetattrrS   rT   r&   searchstartr*   rstrip)nodelinesminlmaxlnrS   rT   
code_linesrV   rX   r'   c
code_blocks                r   _without_contextrg      s   99T? 'aQ$'F$ahL$/J$&al' TAXd#* 4!148.^O^4JrNt\40*II,jm<E;;q>jqM*+.JqMyyj9!((*9:*	z	 :s   C)c                 |    | y t        | t        j                        r| j                  S t        | t              sJ | S N)
isinstancerY   Nameidstr)r_   s    r   	_arg_namern      s6    	\dii 77N	D#			+r   c                    t        j                  |      }t        d | j                  j                  D              }|t        |j                        k7  ry|j                  t        | j                  j                        k7  ry|j                  t        | j                  j                        k7  ryt        d | j                  j                  D              }|t        |j                        k7  ryy)z.Returns True is node fits the argspec of func.c              3   2   K   | ]  }t        |        y wri   rn   r@   args     r   rB   z(_node_matches_argspec.<locals>.<genexpr>   s     =sIcN=   Fc              3   2   K   | ]  }t        |        y wri   rq   rr   s     r   rB   z(_node_matches_argspec.<locals>.<genexpr>   s     IS)C.Irt   T)
r   getfullargspecrI   argsvarargsrn   varargvarkwkwarg
kwonlyargs)r_   funcarg_spec	node_argsnode_kwonlyargss        r   _node_matches_argspecr      s     &&t,(=diinn==)%&&499#3#344^^y11IDII4H4HII/h1122	r   c           
         t        j                  |       }t        j                  |       }| j                  j                  }t        j                  ||j                        }dj                  |      }t        |dd      }g }|D ]%  }t        |d|      |k  r|j                  |       % n g }	|D ]-  }|	j                  d t        j                  |      D               / g }
|	D ]  }t        d}}t        j                  |      D ]C  }t!        |t        |d|            }t        |d|      }t        |dd      }||}t#        ||      }E ||cxk  r|k  srn u|
j                  |||f        t%        |
      d	k(  r|
\  \  }}}t'        ||||      S |
sCd
j                  |	D cg c]  }t)        |       c}      }t+        j,                  d|  d|       |
D cg c]  }t/        |d   |       s| }}t%        |      d	k(  r|\  \  }}}t'        ||||      S d
j                  d t1        |      D              }t+        j,                  d|  d|       c c}w c c}w )a&  Returns the AST and source code of given lambda function.

  Args:
    lam: types.LambdaType, Python function/method/class

  Returns:
    gast.AST, Text: the parsed AST node; the source code that was parsed to
    generate the AST (including any prefixes that this function may have added).
  r   r   FrC   single_noderS   c              3   V   K   | ]!  }t        |t        j                        s| # y wri   )rj   rY   Lambda)r@   rc   s     r   rB   z _parse_lambda.<locals>.<genexpr>  s$      BjDKK&@Bs   ))rT   NrU   r   z#could not parse the source code of z*: no matching AST found among candidates:
c              3   d   K   | ](  \  }\  }}}d j                  |t        |d             * yw)zMatch {}:
{}
F)include_encoding_markerN)r?   unparse)r@   r4   r_   r1   s       r   rB   z _parse_lambda.<locals>.<genexpr>7  s6      1
!\dAq q'$"NO1s   .0z: found multiple definitions with identical signatures at the location. This error may be avoided by defining each lambda on a single line and with unique argument names. The matching definitions were:
)inspect	getmodulegetsourcefile__code__co_firstlineno	linecachegetlines__dict__r*   rJ   r[   r   extendrY   rZ   MAX_SIZEminmaxr   rg   r   r   r"   r   r!   )lammodfdef_liner`   rO   	all_nodessearch_nodesr_   lambda_nodes
candidateslnra   rb   rc   rS   rT   llambda_codesvmatchess                        r   rE   rE      s    	##C !\\((( 

Q
-%775>& F>), 	d
 tXx(H4$ 	 , Bd B99T?B BB
 * *b1$DYYr] wq(D12dq(D)f1lD1j		vd x4T4()* 	_$T4D%t4499,?Qgaj?@L

0
0
-cU 355AN	DE E
 #G1&;AaD#&FQG'G\Q!T4D%t44 II 1&w/1 1' 	..+C5 1A BI	K	L L @ Hs   $II1Ic                     t        j                  |       }|j                  }|r||d }|r-t        |      dk7  rt	        dj                  |            |d   S |S )a  Returns the AST of given piece of code.

  Args:
    src: Text
    preamble_len: Int, indicates leading nodes in the parsed AST which should be
      dropped.
    single_node: Bool, whether `src` is assumed to be represented by exactly one
      AST node.

  Returns:
    ast.AST
  NrU   z!expected exactly one node, got {}r   )rY   rJ   bodyr   
ValueErrorr?   )srcrC   r   module_nodenodess        r   rJ   rJ   B  s^     

3+


%,- E
5zQ:AA%HII8O	,r   c                     t         | j                         z   } t        | t        d      }	 t	        |t
        j                        st        dj                  |            |j                  S )zReturns the AST of given identifier.

  Args:
    src: A piece of code that represents a single Python expression
  Returns:
    A gast.AST object.
  Raises:
    ValueError: if src does not consist of a single Expression.
  Tr   z.expected exactly one node of type Expr, got {})
STANDARD_PREAMBLEstriprJ   STANDARD_PREAMBLE_LENrj   rY   Exprr   r?   value)r   r_   s     r   parse_expressionr   Z  s[     	CIIK'#	s!6D	I$dDII&
:
A
A$
GI I	r   c                    ~t        | t        t        f      s| f} g }|r|j                  d       | D ]  }t        |t        j
                        rt	        j                  |      }n|}t        t        u rt        j                  |       |j                  t        j                  |      j                                 dj                  |      S )a  Returns the source code of given AST.

  Args:
    node: The code to compile, as an AST object.
    indentation: Unused, deprecated. The returning code will always be indented
      at 4 spaces.
    include_encoding_marker: Bool, whether to include a comment on the first
      line to explicitly specify UTF-8 encoding.

  Returns:
    code: The source code generated from the AST object
    source_mapping: A mapping between the user and AutoGraph generated code.
  z# coding=utf-8r   )rj   listrI   r   rY   ASTgast_to_ast
astunparseastfix_missing_locationsr   r   r*   )r_   indentationr   codesrc   ast_ns         r   r   r   m  s     	D4-	(7D
%	LL!" 4a!TXXq!eeS	&	LL##E*00234 
5	r   )r   T)NT)&__doc__r   r   r   r   r&   systextwrapr   r   rY    tensorflow.python.autograph.pyctr   r   tensorflow.python.utilr   dedentPY2_PREAMBLEPY3_PREAMBLEr   version_infor   maxsizemaxintcountr   compiler(   r   r<   rQ   rg   rn   r   rE   rJ   r   r    r   r   <module>r      s   
   	  	 
     3 : - x   v*t"[[("ZZ()//=  !bjj( )
AH!BHD,VLt0&r   