
    -Ph*%                        d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZm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	 	 	 ddZ G d de          Z G d de          Z G d de          Zd Z ej        d          j        Zd Z G d d          Z  G d de          Z!dS ) z.
Support for parsing strings into code trees.
    N)StringIO   )PyrexScannerStringSourceDescriptor)ModuleScope)
PyrexTypes)VisitorTransform)NodeStatListNode)NameNode)Parsing)Main)	UtilNodesc                       e Zd ZddZddZdS )	StringParseContextNFc                 l    |g }|i }t           j                            | |||d           || _        d S )N3)cpplanguage_level)r   Context__init__module_name)selfnameinclude_directoriescompiler_directivesr   s        \/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/Cython/Compiler/TreeFragment.pyr   zStringParseContext.__init__   sM    &"$&"$d$79LRUfijjj    r   Tc                 Z    || j         dfvrt          d          t          |d |           S )NcythonzBNot yet supporting any cimports/includes from string code snippets)parent_modulecontext)r   AssertionErrorr   )r   r   from_moduleposneed_pxdabsolute_fallbackrelative_imports          r   find_modulezStringParseContext.find_module!   s9    t/::: !efff;dDIIIIr   )NNF)NNr   TF)__name__
__module____qualname__r   r)    r   r   r   r      sB               J J J J J Jr   r   Fc                    |t          |           }t          |t                    s
J d            d}| }	|| ddf}t          | |          }
|rd|
_        |                    |	|d          }t          |          }t          ||
||||	          }t          j	        |
          }|'t          j
        |d|	|          }||_        d|_        nt          j        |||          }||_        |S )a  
    Utility method to parse a (unicode) string of code. This is mostly
    used for internal Cython compiler purposes (creating code snippets
    that transforms should emit, as well as unit testing).

    code - a unicode string containing Cython (module-level) code
    name - a descriptive name for the code source (to use in error messages etc.)
    in_utility_code - used to suppress some messages from utility code. False by default
                      because some generated code snippets like properties and dataclasses
                      probably want to see those messages.

    RETURNS

    The tree, i.e. a ModuleNode. The ModuleNode's scope attribute is
    set to the scope used when parsing.
    Nz!unicode code snippets only pleasezUTF-8r   r   TF)r%   r&   )source_encodingscoper"   initial_pos)allow_struct_enum_decorator)ctx)levelr3   )r   
isinstancestrr   in_utility_coder)   r   r   r   Ctxp_moduler0   is_pxdp_code)r   codepxdsr4   r1   r"   r2   r7   encodingr   code_sourcer0   bufscannerr3   trees                   r   parse_from_stringsrC   '   s   & $T**
 dC  EE"EEEEHKQl(t44K +&*#uMME
4..C3x"g[R R RG
+2M
N
N
NC}KSAAA
~gU<<<DJKr   c                       e Zd Zd ZdS )
TreeCopierc                 `    ||S |                                 }|                     |           |S N)
clone_nodevisitchildrenr   nodecs      r   
visit_NodezTreeCopier.visit_Node^   s4    <K!!Aq!!!Hr   N)r*   r+   r,   rM   r-   r   r   rE   rE   ]   s#            r   rE   c                   (     e Zd Z fdZ fdZ xZS )ApplyPositionAndCopyc                 V    t                                                       || _        d S rG   superr   r%   r   r%   	__class__s     r   r   zApplyPositionAndCopy.__init__h   $    r   c                 b    t                                          |          }| j        |_        |S rG   )rR   rM   r%   )r   rK   copyrT   s      r   rM   zApplyPositionAndCopy.visit_Nodel   s(    ww!!$''8r   r*   r+   r,   r   rM   __classcell__rT   s   @r   rO   rO   g   sQ                    r   rO   c                   D     e Zd ZdZdZ fdZd Zd Zd Zd Z	d Z
 xZS )	TemplateTransforma  
    Makes a copy of a template tree while doing substitutions.

    A dictionary "substitutions" should be passed in when calling
    the transform; mapping names to replacement nodes. Then replacement
    happens like this:
     - If an ExprStatNode contains a single NameNode, whose name is
       a key in the substitutions dictionary, the ExprStatNode is
       replaced with a copy of the tree given in the dictionary.
       It is the responsibility of the caller that the replacement
       node is a valid statement.
     - If a single NameNode is otherwise encountered, it is replaced
       if its name is listed in the substitutions dictionary in the
       same way. It is the responsibility of the caller to make sure
       that the replacement nodes is a valid expression.

    Also a list "temps" should be passed. Any names listed will
    be transformed into anonymous, temporary names.

    Currently supported for tempnames is:
    NameNode
    (various function and class definition nodes etc. should be added to this)

    Each replacement node gets the position of the substituted node
    recursively applied to every member node.
    r   c                 x   || _         || _        i }g }|D ]O}t          xj        dz  c_        t	          j        t          j                  }|||<   |                    |           P|| _	        t                                          |          }	|r*t	          j        |                     |          ||	          }	|	S )Nr   )tempsbody)substitutionsr%   r\   temp_name_counterr   
TempHandler   py_object_typeappendtempmaprR   __call__TempsBlockNodeget_pos)r   rK   r`   r^   r%   re   temphandlestemphandleresultrT   s             r   rf   zTemplateTransform.__call__   s    * 	' 	'D//14//)**CDDF"GDMv&&&&!!$'' 	;-dll4.@.@4?39; ; ;F r   c                 ,    | j         r| j         S |j         S rG   )r%   r   rK   s     r   rh   zTemplateTransform.get_pos   s    8 	8O8Or   c                     |d S |                                 }| j        | j        |_        |                     |           |S rG   )rH   r%   rI   rJ   s      r   rM   zTemplateTransform.visit_Node   sE    <4!!Ax#q!!!Hr   c                     | j                             |          }|(| j        }||j        } t          |          |          S |                     |          S rG   )r`   getr%   rO   rM   )r   rK   keysubr%   s        r   try_substitutionz"TemplateTransform.try_substitution   sZ     $$S))?(C{$(C,',,S111??4(((r   c                     | j                             |j                  }|r(|                    |                     |                    S |                     ||j                  S rG   )re   rq   r   refrh   rt   )r   rK   
temphandles      r   visit_NameNodez TemplateTransform.visit_NameNode   sW    \%%di00
 	:>>$,,t"4"4555((ty999r   c                     t          |j        t                    r |                     ||j        j                  S |                     |          S rG   )r5   exprr   rt   r   rM   rn   s     r   visit_ExprStatNodez$TemplateTransform.visit_ExprStatNode   sD     di** 	)((ty~>>>??4(((r   )r*   r+   r,   __doc__ra   rf   rh   rM   rt   rx   r{   rY   rZ   s   @r   r\   r\   r   s         6     $    ) ) ): : :) ) ) ) ) ) )r   r\   c                 0     t                      |           S rG   )rE   )rK   s    r   copy_code_treer~      s    :<<r   z^ *c                 r    d | D             } | r't          d | D                       fd| D             } | S )zQStrips empty lines and common indentation from the list of strings given in linesc                 B    g | ]}|                                 d k    |S ) )strip.0xs     r   
<listcomp>z'strip_common_indent.<locals>.<listcomp>   s%    1111bQr   c                 l    g | ]1}t          t          |                              d                     2S )r   )len_match_indentgroupr   s     r   r   z'strip_common_indent.<locals>.<listcomp>   s6    GGGA]1--33A6677GGGr   c                 $    g | ]}|d          S rG   r-   )r   r   	minindents     r   r   z'strip_common_indent.<locals>.<listcomp>   s!    ...19::...r   )min)linesr   s    @r   strip_common_indentr      sZ     21111E /GGGGGHH	.......Lr   c                   $    e Zd ZddZd ZddZdS )TreeFragmentNc                    |i }|g }|g }|sd}t          |t                    rd } ||          }	i }
|                                D ]\  }} ||          |
|<   t          ||	|
||          x}}||j        }t          |t
                    st          |j        |g          }|D ]}| ||          }|| _        n<t          |t                    r|rt                      || _        nt          d          || _        d S )Nz(tree fragment)c                 l    d                     t          |                     d                              S )N
)joinr   split)r   s    r   fmtz"TreeFragment.__init__.<locals>.fmt   s&    uzz*=aggenn*M*MNNNr   )r4   r1   )r%   statsz3Unrecognized code format (accepts unicode and Node))r5   r6   itemsrC   r_   r   r%   rootr
   NotImplementedError
ValueErrorr^   )r   r<   r   r=   r^   pipeliner4   r1   r   fmt_codefmt_pxdsrr   valuemodt	transforms                   r   r   zTreeFragment.__init__   sV   <D=EH 	%$DdC   	TNNNs4yyHH"jjll + +
U #E

(x\ghhhhC!}Fa.. 9 SWQC888% ! !	$IaLLDIId## 	T ,)+++DIIRSSS


r   c                 *    t          | j                  S rG   )r~   r   )r   s    r   rW   zTreeFragment.copy  s    di(((r   c                 b    |i }|g } t                      | j        || j        |z   |          S )N)r`   r^   r%   )r\   r   r^   )r   nodesr^   r%   s       r   
substitutezTreeFragment.substitute  sM    =E=E" ""4938+/:+=SJ J J 	Jr   )NNNNNN)NNN)r*   r+   r,   r   rW   r   r-   r   r   r   r      sR        ! ! ! !F) ) )J J J J J Jr   r   c                   $     e Zd Z fdZd Z xZS )SetPosTransformc                 V    t                                                       || _        d S rG   rQ   rS   s     r   r   zSetPosTransform.__init__  rU   r   c                 H    | j         |_         |                     |           |S rG   )r%   rI   rn   s     r   rM   zSetPosTransform.visit_Node  s$    84   r   rX   rZ   s   @r   r   r     sG                  r   r   )NNNNFF)"r|   reior   Scanningr   r   Symtabr   r   r   Visitorr	   Nodesr
   r   	ExprNodesr   r   r   r   r   r   rC   rE   rO   r\   r~   compilematchr   r   r   r   r-   r   r   <module>r      sS  
 
 
			       : : : : : : : :             % % % % % % % % % % % % % %                        J J J J J J J J GKAF',3 3 3 3l    !       :   W) W) W) W) W)( W) W) W)t   
5!!'  .J .J .J .J .J .J .J .Jb    &     r   