B
    18™\`  ã            
   @   s¦   d dl Z d dlZdd„ ZG dd„ dƒZedkr¢xrejdd… D ]`ZeedƒLZeed	d
Z	e
ƒ  e
eƒ x*ee ¡  d¡dƒD ]\ZZe	 e¡ q~W W dQ R X q>W dS )é    Nc             C   s   |   d¡r| dd… S d|  S )zV
    Returns a CPP conditional that is the opposite of the conditional passed in.
    ú!é   N)Ú
startswith)Ú	condition© r   úP/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Tools/clinic/cpp.pyÚnegate   s    
r   c               @   sd   e Zd ZdZe d¡jZdddœdd„Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú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.
    z#^defined\s*\(\s*[A-Za-z0-9_]+\s*\)$NF)Úverbosec            C   s(   g | _ d| _d | _d| _|| _|| _d S )NFr   )ÚstackÚ
in_commentÚcontinuationÚline_numberÚfilenamer
   )Úselfr   r
   r   r   r   Ú__init__   s    zMonitor.__init__c          
   C   s.   d  dtt| ƒƒdt| jƒdt|  ¡ ƒdf¡S )NÚ z	<Monitor z line=z condition=ú>)ÚjoinÚstrÚidr   Úreprr   )r   r   r   r   Ú__repr__#   s    

zMonitor.__repr__c             C   s   t | jƒ d¡d |  ¡  S )Né   z: )r   r   Úrjustr   )r   r   r   r   Ústatus+   s    zMonitor.statusc             C   s   d  dd„ | jD ƒ¡S )zT
        Returns the current preprocessor state, as a single #if condition.
        z && c             s   s   | ]\}}|V  qd S )Nr   )Ú.0Útokenr   r   r   r   ú	<genexpr>2   s    z$Monitor.condition.<locals>.<genexpr>)r   r   )r   r   r   r   r   .   s    zMonitor.conditionc             G   sR   | j rd| j  }nd}td| d| jdƒ tdd dd„ |D ƒ¡ƒ t d	¡ d S )
Nú r   zError atÚlineú:z   c             s   s   | ]}t |ƒV  qd S )N)r   )r   Úxr   r   r   r   :   s    zMonitor.fail.<locals>.<genexpr>éÿÿÿÿ)r   Úprintr   r   ÚsysÚexit)r   Úar   r   r   r   Úfail4   s    zMonitor.failc             C   s   | j r|  d¡ d S )Nz;Ended file while still in a preprocessor conditional block!)r   r(   )r   r   r   r   Úclose=   s    zMonitor.closec             C   s"   x|  d¡D ]}|  |¡ qW d S )NÚ
)ÚsplitÚ	writeline)r   Úsr    r   r   r   ÚwriteA   s    zMonitor.writec                s¦  ˆ  j d7  _ | ¡ }‡ ‡fdd„}ˆ jr:ˆ j| }d ˆ _|sBd S | d¡rf|d d…  ¡ d ˆ _d S ˆ jrŠd|krŠ| d¡\}}}dˆ _xfd	|krìˆ jr¤ˆ  d
¡ | d	¡\}}}| d¡\}}}|rÞ| ¡ d | ¡  }qŒdˆ _| ¡ }P qŒW | d¡\}}	}|	r| ¡ }| 	d¡s d S |dd …  ¡ }|s:t
‚| ¡ }
|
d  ¡ ‰d |
dd … ¡ ¡ }dddh}|dddhB }ˆ|krŠd S ˆdkrž|ƒ  d‰ˆ|krZ|sÀˆ  dˆ d ¡ ˆdkräˆ  |¡s2d| d }nN| ¡ }
t|
ƒdkrˆ  dˆ d ¡ |
d }d| d }ˆdkr2d| }ˆ j d|f¡ ˆ jrVtˆ  ¡ ƒ d S |ƒ \}}ˆdkr„ˆ j dt|ƒf¡ n
ˆdkrŽˆ jr¢tˆ  ¡ ƒ d S )Nr   c                  s"   ˆ j sˆ  dˆ d ¡ ˆ j  ¡ S )Nú#z) without matching #if / #ifdef / #ifndef!)r   r(   Úpopr   )r   r   r   r   Ú	pop_stackI   s    z$Monitor.writeline.<locals>.pop_stackú\r#   r   z*/Fz/*zNested block comment!Tz//r/   r   ÚifZifdefZifndefÚelifÚelseZendifzInvalid format for #z line: no argument!ú(ú)z& line: should be exactly one argument!zdefined(r   )r   Ústripr   ÚendswithÚrstripr   Ú	partitionr(   Úlstripr   ÚAssertionErrorr+   Úlowerr   Úis_a_simple_definedÚlenr   Úappendr
   r$   r   r   )r   r    r1   Ú_ÚbeforeÚ	remainderÚcommentZcomment_endsÚafterZline_commentÚfieldsr   Z	if_tokensZ
all_tokensÚsymbolZprevious_tokenZprevious_conditionr   )r   r   r   r,   E   s†    












zMonitor.writeline)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚreÚcompileÚmatchr?   r   r   r   r   r(   r)   r.   r,   r   r   r   r   r	      s   	r	   Ú__main__r   ÚrtT)r
   r*   )rM   r%   r   r	   rI   Úargvr   ÚopenÚfÚcppr$   Ú	enumerateÚreadr+   r   r    r,   r   r   r   r   Ú<module>   s    -