o
    ?e3                     @   s$  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dZdZdZejdkrLeZ	ejd	krWeZejZneZejZed
ZedZdd Zdd Zdd Zdd Zdd Z dd Z!dd Z"d!ddZ#dd Z$d"dd Z%dS )#z/Converting code to AST.

Adapted from Tangent.
    N)errors)inspect_utils)
tf_inspect
 )   	   )r   
__future__z\s*c                 C   s   |  ddS )z7Removes any backslash line continuations from the code.z\
r   )replace)code_string r   h/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/autograph/pyct/parser.py_unfold_continuations:   s   r   c                 C   s  t | } tt| j}d}g }z|D ]}|| qW n
 tjy'   Y nw |D ]&}|\}}}}}|tjkr@|}t	|} qQ|tj
tjtjtjfvrPd} qQq*|sU| S t	|}d|v }	t|D ]3\}
}|\}}}}}|tjkrd|v rw|	s}d|v r|	stdt	||kr||d }||f||
< qat|}g }t| d|dD ]0\}}tt| }tt| }t	|t	|kr|t	|t	| d }n|}|| qd|}|S )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   Z	token_genZblock_indentationtokenstoktok_typeZ
tok_string_Zblock_levelZfirst_indent_uses_tabsiZnew_codeZdedented_codelinenew_lineZoriginal_indent
new_indentZdedented_liner   r   r   dedent_block?   sj   



r0   c              
   C   s   t | r	t| S zt | }W n ty' } ztd|  d| d}~ww t|}tdd |D }d	||f }t
|t|d|fS )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                 s   s    | ]}d  |V  qdS )zfrom __future__ import {}N)format).0namer   r   r   	<genexpr>   s    

zparse_entity.<locals>.<genexpr>r   )preamble_len)r   islambda_parse_lambdaZgetimmediatesourceOSErrorr   ZInaccessibleSourceCodeErrorr0   tupler'   parser   )entityZfuture_featuresZoriginal_sourceesourceZfuture_statementsr   r   r   parse_entity   s$   
	r>   c                 C   s   t | D ] }t|dd}|dur|| |_t|dd}|dur%|| |_q||d | }t| dd}|durB|d d| |d< t| dd}	|	du r]td|d	 }
|
dur]|
d	}	|	durk|d	 |	d |d	< d
dd |D }| |fS )zCReturns a clean node and source code without indenting and context.linenoN
end_lineno   end_col_offset
col_offsetz(?<!\w)lambda(?!\w)r   r   c                 S   s   g | ]}|  qS r   )rstrip)r2   cr   r   r   
<listcomp>       z$_without_context.<locals>.<listcomp>)	gastwalkgetattrr?   r@   r#   searchstartr'   )nodelinesminlmaxlnr?   r@   Z
code_linesrB   rD   r$   Z
code_blockr   r   r   _without_context   s*   


rS   c                 C   s0   | d u rd S t | tjr| jS t | tsJ | S N)
isinstancerI   Nameidstr)rN   r   r   r   	_arg_name   s   rY   c                 C   s   t |}tdd | jjD }|t|jkrdS |jt| jjkr$dS |jt| jjkr/dS tdd | jj	D }|t|j	krCdS dS )z.Returns True is node fits the argspec of func.c                 s       | ]}t |V  qd S rT   rY   r2   argr   r   r   r4          z(_node_matches_argspec.<locals>.<genexpr>Fc                 s   rZ   rT   r[   r\   r   r   r   r4      r^   T)
r   getfullargspecr9   argsvarargsrY   varargvarkwkwarg
kwonlyargs)rN   funcZarg_specZ	node_argsZnode_kwonlyargsr   r   r   _node_matches_argspec   s   
rg   c                    s  t  }t  } jj}t||j}d|}t	|ddd}g }|D ]}t
|d||kr5|| q% g }	|D ]}|	dd t|D  q:g }
|	D ]C}td}}t|D ]"}t|t
|d|}t
|d|}t
|dd	}|d	urw|}t||}qZ||  kr|krn qN|
|||f qNt|
d
kr|
\\}}}t||||S |
sddd |	D }td  d|  fdd|
D }t|d
kr|\\}}}t||||S ddd t|D }td  d| )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r5   single_noder?   c                 s   s     | ]}t |tjr|V  qd S rT   )rU   rI   Lambda)r2   rR   r   r   r   r4     s    
z _parse_lambda.<locals>.<genexpr>r@   NrA   r   c                 S   s   g | ]}t |qS r   )unparse)r2   lr   r   r   rG   +  rH   z!_parse_lambda.<locals>.<listcomp>z#could not parse the source code of z*: no matching AST found among candidates:
c                    s   g | ]}t |d   r|qS )r   )rg   )r2   vlamr   r   rG   1  s    c                 s   s.    | ]\}\}}}d  |t|ddV  qdS )zMatch {}:
{}
F)include_encoding_markerN)r1   rk   )r2   r,   rN   r+   r   r   r   r4   7  s
    
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'   r:   rK   r   extendrI   rJ   MAX_SIZEminmaxr   rS   r   r   r   )ro   modfZdef_linerO   r=   Z	all_nodesZsearch_nodesrN   Zlambda_nodes
candidateslnrP   rQ   rR   r?   r@   Zlambda_codesmatchesr   rn   r   r7      sf   






r7   Tc                 C   sJ   t | }|j}|r||d }|r#t|dkrtd||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
  NrA   z!expected exactly one node, got {}r   )rI   r:   bodyr   
ValueErrorr1   )srcr5   ri   Zmodule_nodenodesr   r   r   r:   B  s   
r:   c                 C   s<   t |   } t| tdd}	 t|tjstd||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.
  Trh   z.expected exactly one node of type Expr, got {})
STANDARD_PREAMBLEstripr:   STANDARD_PREAMBLE_LENrU   rI   Exprr   r1   value)r   rN   r   r   r   parse_expressionZ  s   
r   c                 C   s~   ~t | ttfs| f} g }|r|d | D ]#}t |tjr$t|}n|}ttu r/t	| |t
|  qd|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   )rU   listr9   r   rI   ASTZgast_to_ast
astunparseastfix_missing_locationsrk   r   r'   )rN   indentationrp   codesrR   Zast_nr   r   r   rk   m  s   


rk   )r   T)NT)&__doc__r   rq   r   rv   r#   systextwrapr   r   rI   Z tensorflow.python.autograph.pyctr   r   Ztensorflow.python.utilr   dedentZPY2_PREAMBLEZPY3_PREAMBLErz   version_infor   maxsizeZmaxintcountr   compiler%   r   r0   r>   rS   rY   rg   r7   r:   r   rk   r   r   r   r   <module>   sH   




D$"	
Z