
    h*%                     z   S r SSKrSSKJr  SSKJrJr  SSKJr  SSK	J
r
  SSKJr  SS	KJrJr  SS
KJr  SSK	Jr  SSK	Jr  SSK	Jr   " S S\R*                  5      r   SS jr " S S\5      r " S S\5      r " S S\5      rS r\R8                  " S5      R:                  rS r " S S5      r  " S S\5      r!g)z.
Support for parsing strings into code trees.
    N)StringIO   )PyrexScannerStringSourceDescriptor)ModuleScope)
PyrexTypes)VisitorTransform)NodeStatListNode)NameNode)Parsing)Main)	UtilNodesc                   (    \ rS rSrSS jrSS jrSrg)StringParseContext   Nc                 b    Uc  / nUc  0 n[         R                  R                  XX4SS9  Xl        g )N3)cpplanguage_level)r   Context__init__module_name)selfnameinclude_directoriescompiler_directivesr   s        V/var/www/fran/franai/venv/lib/python3.13/site-packages/Cython/Compiler/TreeFragment.pyr   StringParseContext.__init__   s;    &"$&"$d9Lfij    c                 P    XR                   S4;  a  [        S5      e[        US U S9$ )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_moduleStringParseContext.find_module!   s.    //:: !eff;dDIIr    )r   )NNF)NNr   TF)__name__
__module____qualname____firstlineno__r   r+   __static_attributes__ r    r   r   r      s     Jr    r   c           	         Uc  [        U 5      n[        U[        5      (       d   S5       eSnU n	Uc  U SS4n[        X5      n
U(       a  SU
l        UR                  XSS9n[        U5      n[        XUXUS9n[        R                  " US	9nUc$  [        R                  " USXS
9nXl        SUl        O[        R                  " XUS9nXl        U$ )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.
z!unicode code snippets only pleasezUTF-8r   r   TF)r'   r(   )source_encodingscoper$   initial_pos)allow_struct_enum_decorator)ctx)levelr8   )r   
isinstancestrr   in_utility_coder+   r   r   r   Ctxp_moduler5   is_pxdp_code)r   codepxdsr9   r6   r$   r7   r<   encodingr   code_sourcer5   bufscannerr8   trees                   r   parse_from_stringsrH   '   s    & $T*
 dC  E"EE HKQl(4K&*#uME
4.C3x"[RG
++2M
NC}KA
~~g<JKr    c                       \ rS rSrS rSrg)
TreeCopier]   c                 R    Uc  U$ UR                  5       nU R                  U5        U$ N)
clone_nodevisitchildrenr   nodecs      r   
visit_NodeTreeCopier.visit_Node^   s+    <K!Aq!Hr    r2   N)r-   r.   r/   r0   rS   r1   r2   r    r   rJ   rJ   ]   s    r    rJ   c                   4   ^  \ rS rSrU 4S jrU 4S jrSrU =r$ )ApplyPositionAndCopyg   c                 .   > [         TU ]  5         Xl        g rM   superr   r'   r   r'   	__class__s     r   r   ApplyPositionAndCopy.__init__h       r    c                 H   > [         TU ]  U5      nU R                  Ul        U$ rM   )rZ   rS   r'   )r   rQ   copyr\   s      r   rS   ApplyPositionAndCopy.visit_Nodel   s"    w!$'88r    r'   r-   r.   r/   r0   r   rS   r1   __classcell__r\   s   @r   rV   rV   g   s     r    rV   c                   N   ^  \ rS rSrSrSrU 4S jrS rS rS r	S r
S	 rS
rU =r$ )TemplateTransformr   a4  
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 l         X@l        0 n/ nU HU  n[        =R                  S-  sl        [        R
                  " [        R                  5      nXU'   UR                  U5        MW     XPl	        [        T
U ]-  U5      n	U(       a%  [        R                  " U R                  U5      UU	S9n	U	$ )Nr   )tempsbody)substitutionsr'   rg   temp_name_counterr   
TempHandler   py_object_typeappendtempmaprZ   __call__TempsBlockNodeget_pos)r   rQ   rl   rj   r'   rq   temphandlestemphandleresultr\   s             r   rr   TemplateTransform.__call__   s    *D//14/))**C*CDF"DMv&	 
 !$'--dll4.@4?39;F r    c                 T    U R                   (       a  U R                   $ UR                   $ rM   rb   r   rQ   s     r   rt   TemplateTransform.get_pos   s    8888O88Or    c                     Uc  g UR                  5       nU R                  b  U R                  Ul        U R                  U5        U$ rM   )rN   r'   rO   rP   s      r   rS   TemplateTransform.visit_Node   s>    <!Axx#q!Hr    c                     U R                   R                  U5      nUb,  U R                  nUc  UR                  n[        U5      " U5      $ U R	                  U5      $ rM   )rl   getr'   rV   rS   )r   rQ   keysubr'   s        r   try_substitution"TemplateTransform.try_substitution   sR      $$S)?((C{$((C',S11??4((r    c                     U R                   R                  UR                  5      nU(       a   UR                  U R	                  U5      5      $ U R                  XR                  5      $ rM   )rq   r   r   refrt   r   )r   rQ   
temphandles      r   visit_NameNode TemplateTransform.visit_NameNode   sJ    \\%%dii0
>>$,,t"455((yy99r    c                     [        UR                  [        5      (       a%  U R                  XR                  R                  5      $ U R                  U5      $ rM   )r:   exprr   r   r   rS   r{   s     r   visit_ExprStatNode$TemplateTransform.visit_ExprStatNode   s=     dii**((yy~~>>??4((r    )r'   rl   rq   )r-   r.   r/   r0   __doc__rm   rr   rt   rS   r   r   r   r1   rd   re   s   @r   rg   rg   r   s3    6 $):) )r    rg   c                 "    [        5       " U 5      $ rM   )rJ   )rQ   s    r   copy_code_treer      s    <r    z^ *c           
      "   U  Vs/ s H  oR                  5       S:w  d  M  UPM     n nU (       aQ  [        U  Vs/ s H&  n[        [        U5      R	                  S5      5      PM(     sn5      nU  Vs/ s H  oUS PM	     n nU $ s  snf s  snf s  snf )zQStrips empty lines and common indentation from the list of strings given in lines r   N)stripminlen_match_indentgroup)linesx	minindents      r   strip_common_indentr      s{     11bQE1GA]1-33A67GH	(-.19:.L	 2G.s   BB-B0Bc                   .    \ rS rSrSS jrS rSS jrSrg)	TreeFragment   Nc                    Uc  0 nUc  / nUc  / nU(       d  Sn[        U[        5      (       a  S nU" U5      n	0 n
UR                  5        H  u  pU" U5      X'   M     [        X)XUS9=pUc  UR                  n[        U[
        5      (       d  [        UR                  U/S9nU H  nUc  M  U" U5      nM     Xl        O8[        U[        5      (       a  U(       a
  [        5       eXl        O[        S5      eX@l        g )Nz(tree fragment)c                 T    SR                  [        U R                  S5      5      5      $ )N
)joinr   split)r   s    r   fmt"TreeFragment.__init__.<locals>.fmt   s    uzz*=aggen*MNNr    )r9   r6   )r'   statsz3Unrecognized code format (accepts unicode and Node))r:   r;   itemsrH   rk   r   r'   rootr
   NotImplementedError
ValueErrorrj   )r   rA   r   rB   rj   pipeliner9   r6   r   fmt_codefmt_pxdsr   valuemodt	transforms                   r   r   TreeFragment.__init__   s    <D=EH$DdC  N4yHH"jjl
 #E
 +(\ghhC}FFa.. SWWQC8%	$aL & Id##)++IRSS
r    c                 ,    [        U R                  5      $ rM   )r   r   )r   s    r   r`   TreeFragment.copy  s    dii((r    c                 f    Uc  0 nUc  / n[        5       " U R                  UU R                  U-   US9$ )N)rl   rj   r'   )rg   r   rj   )r   nodesrj   r'   s       r   
substituteTreeFragment.substitute  s?    =E=E "49938+/::+=SJ 	Jr    )r   rj   )NNNNNN)NNN)r-   r.   r/   r0   r   r`   r   r1   r2   r    r   r   r      s    !F)Jr    r   c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )SetPosTransformi  c                 .   > [         TU ]  5         Xl        g rM   rY   r[   s     r   r   SetPosTransform.__init__  r^   r    c                 J    U R                   Ul         U R                  U5        U$ rM   )r'   rO   r{   s     r   rS   SetPosTransform.visit_Node  s     884 r    rb   rc   re   s   @r   r   r     s     r    r   )NNNNFF)"r   reior   Scanningr   r   Symtabr   r   r   Visitorr	   Nodesr
   r   	ExprNodesr   r   r   r   r   r   rH   rJ   rV   rg   r   compilematchr   r   r   r   r2   r    r   <module>r      s   

 
  :   % %    J J GKAF',3l! : W)( W)t 

5!''.J .Jb& r    