
    daf                        d dl Z d dlZd dlmZ d dlmZ eeef   Ze	e   Z
dedefdZ G d d      Zed	k(  rej                  d
d D ]l  Z ee      5 Z eed      Z e         ee        eej+                         j-                  d      d
      D ]  \  ZZej3                  e        	 ddd       n yy# 1 sw Y   zxY w)    N)Callable)NoReturn	conditionreturnc                 8    | j                  d      r| dd S d| z   S )zV
    Returns a CPP conditional that is the opposite of the conditional passed in.
    !   N)
startswith)r   s    '/root/Python-3.12.4/Tools/clinic/cpp.pynegater   
   s(     C }?    c                       e Zd ZU dZeegej                  e   dz  f   ed<    ej                  d      j                  Zddddedz  ded	dfd
Zd	efdZd	efdZd	efdZded	efdZddZded	dfdZded	dfdZy)Monitora  
    A simple C preprocessor that scans C source and computes, line by line,
    what the current C preprocessor #if state is.

    Doesn't handle everything--for example, if you have /* inside a C string,
    without a matching */ (also inside a C string), or with a */ inside a C
    string but on another line and with preprocessor macros in between...
    the parser will get lost.

    Anyway this implementation seems to work well enough for the CPython sources.
    Nis_a_simple_definedz#^defined\s*\(\s*[A-Za-z0-9_]+\s*\)$Fverbosefilenamer   r   c                X    g | _         d| _        d | _        d| _        || _        || _        y )NFr   )stack
in_commentcontinuationline_numberr   r   )selfr   r   s      r   __init__zMonitor.__init__"   s.    !#
(, r   c                     dj                  dt        t        |             dt        | j                        dt	        | j                               df      S )N z	<Monitor z line=z condition=>)joinstridr   reprr   r   s    r   __repr__zMonitor.__repr__*   sJ    ww4Mc$**+4 01  	r   c                 r    t        | j                        j                  d      dz   | j                         z   S )N   z: )r   r   rjustr   r"   s    r   statuszMonitor.status2   s/    4##$**1-4t~~7GGGr   c                 F    dj                  d | j                  D              S )zT
        Returns the current preprocessor state, as a single #if condition.
        z && c              3   &   K   | ]	  \  }}|  y wN ).0tokenr   s      r   	<genexpr>z$Monitor.condition.<locals>.<genexpr>9   s     H)9	9Hs   )r   r   r"   s    r   r   zMonitor.condition5   s     {{HTZZHHHr   ac                     | j                   rd| j                   z   }nd}t        d|z   d| j                  d       t        ddj                  d |D                     t	        j
                  d       y )	N r   zError atline:z   c              3   2   K   | ]  }t        |        y wr*   )r   )r,   xs     r   r.   zMonitor.fail.<locals>.<genexpr>A   s     0c!f0s   )r   printr   r   sysexit)r   r/   r   s      r   failzMonitor.fail;   sY    ==T]]*HHj8#VT-=-=sCeSXX0a001r   c                 @    | j                   r| j                  d       y y )Nz;Ended file while still in a preprocessor conditional block!)r   r:   r"   s    r   closezMonitor.closeD   s    ::IIST r   sc                 R    |j                  d      D ]  }| j                  |        y )N
)split	writeline)r   r=   r2   s      r   writezMonitor.writeH   s%    GGDM 	!DNN4 	!r   r2   c                 ^     xj                   dz  c_         |j                         }dt        f fd} j                  r j                  |z   }d  _        |sy |j	                  d      r|d d j                         dz    _        y  j                  r d|v r|j                  d      \  }}}d _        	 d
|v r j                  r j                  d       |j                  d
      \  }}}|j                  d      \  }}}|r%|j                         dz   |j                         z   }sd	 _        |j                         }	 |j                  d      \  }}	}|	r|j                         }|j                  d      sy |dd  j                         }|sJ |j                         }
|
d   j                         dj                  |
dd        j                         }dv r|s j                  dz   dz          dv rO j                  |      sd|z   dz   }dk(  r |       \  }} j                  j!                  |t#        |      f       nN|j                         }
t%        |
      dk7  r j                  dz   dz          |
d   }d|z   dz   }dk(  rd|z   }d j                  j!                  |f       nYdk(  r1 |       \  }} j                  j!                  |t#        |      f       n#dk(  r |       d   dk7  r	  |       d   dk7  rny  j&                  rt)         j+                                y y )Nr	   r   c                  ~     j                   s j                  dz   dz           j                   j                         S )N#z) without matching #if / #ifdef / #ifndef!)r   r:   pop)r   r-   s   r   	pop_stackz$Monitor.writeline.<locals>.pop_stackP   s1    ::		#+(SST::>>##r   \r6   r1   z*/FTz/*zNested block comment!z//rE   r   >   ifelififdefifndefzInvalid format for #z line: no argument!>   rI   rJ   ()rJ   z& line: should be exactly one argument!zdefined(rL   r   rI   elseendif)r   stripTokenAndConditionr   endswithrstripr   	partitionr:   lstripr
   r@   lowerr   r   r   appendr   lenr   r7   r'   )r   r2   rG   _before	remaindercommentcomment_endsafterline_commentfieldsr   previous_tokenprevious_conditionsymbolr-   s   `              @r   rA   zMonitor.writelineL   s   Azz|	$, 	$
 $$t+D $D== $Sb	 0 0 2S 8D ??t| "^^D1
1d"'t|??II56'+~~d';$9/8/B/B4/H,u!==?S05<<>AD"&}} )-t(<%g==?Ds#ABx tq	!HHVABZ(..0	55		058;PPQ&//	: #i# 5IF?9B6N$6JJ%%~v>P7Q&RS"*v;!#II4u<?ggh&/#5	H$ #iIJJui01f_1:.N.JJ~v6H/IJKg+a.D( +a.D( <<$++-  r   r*   )r   N)__name__
__module____qualname____doc__r   r   reMatch__annotations__compilematchr   boolr   r#   r'   r   objectr   r:   r<   rB   rA   r+   r   r   r   r      s    
 "3%#)="=>>$"**%KLRRu t  QU # H HI3 Iv ( U!s !t !l!c l!d l!r   r   __main__r	   Tr   r?   )ri   r8   collections.abcr   typingr   tupler   rR   list
TokenStackr   r   re   argvr   openfcppr7   	enumeratereadr@   r   r2   rA   r+   r   r   <module>r|      s    	 
 $  #s(O #$
c c f! f!P zHHQRL $(^ 	$q(D1CG(O%.qvvx~~d/CQ%G $!Td#$		$ 	$$ 	$ 	$s   AB88C	