o
    2h3                     @   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   b/var/www/html/chatgem/venv/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	|} n|tj
tjtjtjfvrPd} nq*|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   	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_liner   r   r   dedent_block?   sd   



r9   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_lambdagetimmediatesourceOSErrorr   InaccessibleSourceCodeErrorr9   tupler'   parser   )entityfuture_featuresoriginal_sourceesourcefuture_statementsr   r   r   parse_entity   s$   
	rL   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)r;   cr   r   r   
<listcomp>       z$_without_context.<locals>.<listcomp>)	gastwalkgetattrrM   rN   r#   searchstartr'   )nodelinesminlmaxlnrM   rN   
code_linesrP   rR   r$   
code_blockr   r   r   _without_context   s*   


rc   c                 C   s0   | d u rd S t | tjr| jS t | tsJ | S N)
isinstancerW   Nameidstr)r\   r   r   r   	_arg_name   s   ri   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 rd   ri   r;   argr   r   r   r=          z(_node_matches_argspec.<locals>.<genexpr>Fc                 s   rj   rd   rk   rl   r   r   r   r=      rn   T)
r   getfullargspecrD   argsvarargsri   varargvarkwkwarg
kwonlyargs)r\   funcarg_spec	node_argsnode_kwonlyargsr   r   r   _node_matches_argspec   s   
rz   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r>   single_noderM   c                 s   s     | ]}t |tjr|V  qd S rd   )re   rW   Lambda)r;   r`   r   r   r   r=     s    
z _parse_lambda.<locals>.<genexpr>rN   NrO   r   c                 S   s   g | ]}t |qS r   )unparse)r;   lr   r   r   rU   +  rV   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   )rz   )r;   vlamr   r   rU   1  s    c                 s   s.    | ]\}\}}}d  |t|ddV  qdS )zMatch {}:
{}
F)include_encoding_markerN)r:   r~   )r;   r1   r\   r.   r   r   r   r=   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'   rE   rY   r   extendrW   rX   MAX_SIZEminmaxr   rc   r   r   r   )r   modfdef_liner]   rJ   	all_nodessearch_nodesr\   lambda_nodes
candidateslnr^   r_   r`   rM   rN   lambda_codesmatchesr   r   r   r@      sf   






r@   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
  NrO   z!expected exactly one node, got {}r   )rW   rE   bodyr   
ValueErrorr:   )srcr>   r|   module_nodenodesr   r   r   rE   B  s   
rE   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.
  Tr{   z.expected exactly one node of type Expr, got {})
STANDARD_PREAMBLEstriprE   STANDARD_PREAMBLE_LENre   rW   Exprr   r:   value)r   r\   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   )re   listrD   r   rW   ASTgast_to_ast
astunparseastfix_missing_locationsr~   r   r'   )r\   indentationr   codesr`   ast_nr   r   r   r~   m  s   


r~   )r   T)NT)&__doc__r   r   r   r   r#   systextwrapr   r   rW    tensorflow.python.autograph.pyctr   r   tensorflow.python.utilr   dedentPY2_PREAMBLEPY3_PREAMBLEr   version_infor   maxsizemaxintcountr   compiler%   r   r9   rL   rc   ri   rz   r@   rE   r   r~   r   r   r   r   <module>   sH   




D$"	
Z