B
    ›³ëbU  ã               @   s¶  d Z ddlZddlZdddddddœZi ax4e ¡ D ](\ZZe e¡Z	xeD ]Z
e	te
< qLW q4W [	[
[d	d
iZdd„ t ¡ D ƒZdd e¡ d Z	e e	ej¡addddgZe dd e¡ d ¡Zdd„ ZdddddddœZdd„ Zeee ¡ ƒdd„ d d!Zd eejeƒ¡Ze e¡Zd"d#gd$d#gd%d&gd'd&ggZx eD ]Ze ed ¡ed< q>W d(d)„ Z[[[G d*d+„ d+ƒZe d,¡Ze d-¡Ze d.¡Z G d/d0„ d0ƒZ!G d1d2„ d2e!ƒZ"dS )3zSCons C Pre-Processor moduleé    Nz\s*(.+)z\s+(.+)Ú z/\s+([_A-Za-z][_A-Za-z0-9_]*)(\([^)]*\))?\s*(.*)z\s+([_A-Za-z][A-Za-z0-9_]*)))ÚifÚelif)ÚifdefÚifndef)ÚimportÚincludeÚinclude_next)ÚelseÚendif)Údefine)Úundefr   zif(?!n?def)c             C   s   g | ]}t  ||¡‘qS © )ÚoverrideÚget)Ú.0Úxr   r   ú(lib/python3.7/site-packages/SCons/cpp.pyú
<listcomp>U   s    r   z	^\s*#\s*(ú|z)(.*)$z/\*.*\*/z/\*.*z//.*z\rz\s*(ú)c             C   s   dd„ | D ƒS )Nc             S   s$   g | ]}|d  t  d|d ¡f‘qS )r   r   é   )ÚCPP_Expression_Cleaner_REÚsub)r   Útr   r   r   r   n   s    z+Cleanup_CPP_Expressions.<locals>.<listcomp>r   )Ztsr   r   r   ÚCleanup_CPP_Expressionsm   s    r   z not z != z and z or )ú!z!=z&&z||ú?ú:c             C   s   t |  d¡ S )Nr   )ÚCPP_to_Python_Ops_DictÚgroup)Úmr   r   r   Ú<lambda>‚   ó    r"   c             C   s   t | ƒS )N)Úlen)Úar   r   r   r"   Š   r#   T)ÚkeyÚreversezdefined\s+(\w+)z"\1" in __dict__zdefined\s*\((\w+)\)z(0x[0-9A-Fa-f]+)(?:L|UL)?z\1z(\d+)(?:L|UL)?c             C   s0   t  t| ¡} xtD ]\}}t ||| ¡} qW | S )zo
    Converts a C pre-processor expression into an equivalent
    Python expression that can be evaluated.
    )ÚCPP_to_Python_Ops_Expressionr   ÚCPP_to_Python_Ops_SubÚCPP_to_Python_Eval_ListÚre)ÚsÚexprÚreplr   r   r   ÚCPP_to_Python¢   s    r/   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚFunctionEvaluatorz6Handles delayed evaluation of a #define function call.c             C   s@   || _ t |¡| _y| d¡}W n tk
r4   Y nX || _dS )z¶
        Squirrels away the arguments and expansion value of a #define
        macro function for later evaluation when we must actually expand
        a value that uses it.
        z##N)ÚnameÚfunction_arg_separatorÚsplitÚargsÚAttributeErrorÚ	expansion)Úselfr1   r4   r6   r   r   r   Ú__init__·   s    zFunctionEvaluator.__init__c                sf   t | jƒt |ƒkr td| j ƒ‚| j‰ dd„ tˆ |ƒD ƒ}‡ fdd„| jD ƒ}d |¡}t|tƒ |ƒS )zo
        Evaluates the expansion of a #define macro function called
        with the specified values.
        z%Incorrect number of arguments to `%s'c             S   s   i | ]\}}||“qS r   r   )r   ÚkÚvr   r   r   ú
<dictcomp>Ñ   s    z.FunctionEvaluator.__call__.<locals>.<dictcomp>c                s    g | ]}|ˆ kr|nt |ƒ‘qS r   )Úrepr)r   r,   )r4   r   r   r   Ò   s    z.FunctionEvaluator.__call__.<locals>.<listcomp>z + )	r$   r4   Ú
ValueErrorr1   Úzipr6   ÚjoinÚevalÚglobals)r7   ÚvaluesZ	localvarsÚpartsZ	statementr   )r4   r   Ú__call__Å   s    
zFunctionEvaluator.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r8   rD   r   r   r   r   r0   ´   s   r0   z\\?
z(\S+)\(([^)]*)\)z,\s*c               @   s(  e Zd ZdZejdi ddfdd„Zdd„ Zd	d
„ Zdd„ Z	dFd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„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ ZdGd(d)„ZdHd*d+„Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Z d<d=„ Z!d>d?„ Z"d@dA„ Z#e#Z$dBdC„ Z%dDdE„ Z&dS )IÚPreProcessorz7The main workhorse class for handling C pre-processing.r   r   éÿÿÿÿc             C   s€   t |ƒ}|f| ||f dœ| _| ¡ | _| j| jd< |rB| j| _|| _d| ji}x"t 	¡ D ]}t
| d| ƒ||< q\W || _d S )N)ú"ú<Ú__dict__Úscons_current_fileZdo_)ÚtupleÚ
searchpathÚcopyÚcpp_namespaceÚall_includeÚ
do_includeÚdepthrN   ÚTableÚkeysÚgetattrÚdefault_table)r7   ZcurrentZcpppathÚdictÚallrU   ÚdÚopr   r   r   r8   é   s    
	
zPreProcessor.__init__c             C   s   || _ |  |¡S )zU
        Pre-processes a file.

        This is the main public entry point.
        )Úcurrent_fileÚprocess_file)r7   Úfiler   r   r   rD     s    zPreProcessor.__call__c             C   s   |   |  |  |¡¡|¡S )zW
        Pre-processes a file.

        This is the main internal entry point.
        )Ú_process_tuplesÚtupleizeÚ	read_file)r7   r`   r   r   r   r_     s    zPreProcessor.process_filec             C   s   |   |  |¡¡S )zJ
        Pre-processes a file contents.

        Is used by tests
        )ra   rb   )r7   Úcontentsr   r   r   Úprocess_contents"  s    zPreProcessor.process_contentsNc             C   s\   g | _ | j ¡ | _|| _|| _|  |¡ x(| jrP| j d¡}| j|d  |ƒ q*W |  |¡S )Nr   )	ÚstackrY   rQ   Údispatch_tabler^   ÚtuplesÚinitialize_resultÚpopÚfinalize_result)r7   rh   r`   r   r   r   r   ra   *  s    
zPreProcessor._process_tuplesc             C   s   |   |  |¡¡S )a‰  
        Turns the contents of a file into a list of easily-processed
        tuples describing the CPP lines in the file.

        The first element of each tuple is the line's preprocessor
        directive (#if, #include, #define, etc., minus the initial '#').
        The remaining elements are specific to the type of directive, as
        pulled apart by the regular expression.
        )Ú_match_tuplesÚ_parse_tuples)r7   rd   r   r   r   rb   9  s    
zPreProcessor.tupleizec             C   s   t  d|¡}t |¡}t|ƒS )Nr   )Úline_continuationsr   ÚCPP_ExpressionÚfindallr   )r7   rd   rh   r   r   r   rm   E  s    
zPreProcessor._parse_tuplesc             C   sH   g }x>|D ]6}t |d   |d ¡}|r
| |d f| ¡  ¡ q
W |S )Nr   r   )rV   ÚmatchÚappendÚgroups)r7   rh   Úresultr   r!   r   r   r   rl   K  s    
zPreProcessor._match_tuplesc             C   s   | j  | j¡ | j ¡ | _dS )z†
        Pushes the current dispatch table on the stack and re-initializes
        the current dispatch table to the default.
        N)rf   rr   rg   rY   rQ   )r7   r   r   r   ÚsaveV  s    zPreProcessor.savec             C   s*   y| j  ¡ | _W n tk
r$   Y nX dS )zf
        Pops the previous dispatch table off the stack and makes it the
        current one.
        N)rf   rj   rg   Ú
IndexError)r7   r   r   r   Úrestore^  s      zPreProcessor.restorec             C   s   dS )z
        Null method for when we explicitly want the action for a
        specific preprocessor directive to do nothing.
        Nr   )r7   r   r   r   r   Ú
do_nothingh  s    zPreProcessor.do_nothingc             C   s   |d | _ d S )Nr   )r^   )r7   r   r   r   r   rN   o  s    zPreProcessor.scons_current_filec          
   C   sB   t d |dd… ¡ƒ}yt|| jƒS  tttfk
r<   dS X dS )z×
        Evaluates a C preprocessor expression.

        This is done by converting it to a Python equivalent and
        eval()ing it in the C preprocessor namespace we use to
        track #define values.
        ú r   Nr   )r/   r?   r@   rR   Ú	NameErrorÚ	TypeErrorÚSyntaxError)r7   r   r   r   r   Úeval_expressionr  s
    zPreProcessor.eval_expressionc             C   s   |g| _ d S )N)rt   )r7   Úfnamer   r   r   ri   €  s    zPreProcessor.initialize_resultc             C   s   | j dd … S )Nr   )rt   )r7   r~   r   r   r   rk   ƒ  s    zPreProcessor.finalize_resultc             C   sR   |d }xD| j |d  D ]2}|tjkr,|}ntj ||¡}tj |¡r|S qW dS )zI
        Finds the #include file for a given preprocessor tuple.
        é   r   N)rP   ÚosÚcurdirÚpathr?   Úisfile)r7   r   r~   r\   Úfr   r   r   Úfind_include_file†  s    
zPreProcessor.find_include_filec          	   C   s   t |ƒ
}| ¡ S Q R X d S )N)ÚopenÚread)r7   r`   r„   r   r   r   rc   ”  s    
zPreProcessor.read_filec             C   s:   | j }| jr| jd n| j}xdD ]}|| ||< q"W dS )aX  
        Causes the PreProcessor object to start processing #import,
        #include and #include_next lines.

        This method will be called when a #if, #ifdef, #ifndef or #elif
        evaluates True, or when we reach the #else in a #if, #ifdef,
        #ifndef or #elif block where a condition already evaluated
        False.

        rJ   )r   r   r	   r   r   N)rg   rf   rY   )r7   r   r\   Úpr9   r   r   r   Ústart_handling_includesš  s    
z$PreProcessor.start_handling_includesc             C   s<   | j }| j|d< | j|d< | j|d< | j|d< | j|d< dS )aN  
        Causes the PreProcessor object to stop processing #import,
        #include and #include_next lines.

        This method will be called when a #if, #ifdef, #ifndef or #elif
        evaluates False, or when we reach the #else in a #if, #ifdef,
        #ifndef or #elif block where a condition already evaluated True.
        r   r   r	   r   r   N)rg   rx   )r7   r   r\   r   r   r   Ústop_handling_includes«  s    	



z#PreProcessor.stop_handling_includesc             C   sP   |   ¡  | j}|r0|  ¡  | j|d< | j|d< n|  ¡  | j|d< | j|d< dS )zf
        Common logic for evaluating the conditions on #if, #ifdef and
        #ifndef lines.
        r   r
   N)ru   rg   r‰   rŠ   Údo_elif)r7   Z	conditionr\   r   r   r   Ú_do_if_else_condition¿  s    

z"PreProcessor._do_if_else_conditionc             C   s   |   |d | jk¡ dS )z4
        Default handling of a #ifdef line.
        r   N)rŒ   rR   )r7   r   r   r   r   Údo_ifdefÏ  s    zPreProcessor.do_ifdefc             C   s   |   |d | jk¡ dS )z5
        Default handling of a #ifndef line.
        r   N)rŒ   rR   )r7   r   r   r   r   Ú	do_ifndefÕ  s    zPreProcessor.do_ifndefc             C   s   |   |  |¡¡ dS )z1
        Default handling of a #if line.
        N)rŒ   r}   )r7   r   r   r   r   Údo_ifÛ  s    zPreProcessor.do_ifc             C   s0   | j }|  |¡r,|  ¡  | j|d< | j|d< dS )z3
        Default handling of a #elif line.
        r   r
   N)rg   r}   r‰   rŠ   )r7   r   r\   r   r   r   r‹   á  s
    

zPreProcessor.do_elifc             C   s   dS )z3
        Default handling of a #else line.
        Nr   )r7   r   r   r   r   Údo_elseë  s    zPreProcessor.do_elsec             C   s   |   ¡  dS )z4
        Default handling of a #endif line.
        N)rw   )r7   r   r   r   r   Údo_endifñ  s    zPreProcessor.do_endifc          	   C   sˆ   |\}}}}yt |ƒ}W n< ttfk
rT   d|krP|  |dd… ¡| j|< dS Y nX |rzt||dd… |ƒ}|| j|< n
|| j|< dS )z5
        Default handling of a #define line.
        zdefined r   Nr   rJ   )Úintr{   r=   r}   rR   r0   )r7   r   Ú_r1   r4   r6   Z	evaluatorr   r   r   Ú	do_define÷  s    
zPreProcessor.do_definec             C   s*   y| j |d = W n tk
r$   Y nX dS )z4
        Default handling of a #undef line.
        r   N)rR   ÚKeyError)r7   r   r   r   r   Údo_undef
  s      zPreProcessor.do_undefc             C   s   dS )z5
        Default handling of a #import line.
        Nr   )r7   r   r   r   r   Ú	do_import  s    zPreProcessor.do_importc             C   s¸   |   |¡}|sdS |  |¡}|r*|| jkr.dS | j |¡ | jdkrzd}x"| jD ]}|d dkrP|d7 }qPW || jkrzdS d|fg|  |  |¡¡ d| jfg }|| j | jdd…< dS )z6
        Default handling of a #include line.
        NrJ   r   rN   r   )	Úresolve_includer…   rt   rr   rU   rh   rb   rc   r^   )r7   r   Zinclude_fileZcurrent_depthZ
new_tuplesr   r   r   rT     s"    



zPreProcessor.do_includec             C   sÆ   |d   ¡ }xž|d dkrªy.| j| }xdD ]}| d|› |¡}q,W W nV tk
rž   t |¡}|sjdS | j| d¡ }t|ƒršt 	| d¡¡}||Ž }Y nX |sdS qW |d |d |dd… fS )	zøResolve a tuple-ized #include line.

        This handles recursive expansion of values without "" or <>
        surrounding the name until an initial " or < is found, to handle
        #include FILE where FILE is a #define somewhere else.
        r   r   z<"z<">ú\Nr   rJ   )
ÚstriprR   Úreplacer•   Úfunction_nameÚsearchr    Úcallabler2   r3   )r7   r   r,   Úcr!   r4   r   r   r   r˜   G  s"    


zPreProcessor.resolve_includec             C   s   | j  |  |¡¡ dS )z	
        N)rt   rr   r˜   )r7   r   r   r   r   rS   i  s    zPreProcessor.all_include)N)N)N)'rE   rF   rG   rH   r€   r   r8   rD   r_   re   ra   rb   rm   rl   ru   rw   rx   rN   r}   ri   rk   r…   rc   r‰   rŠ   rŒ   r   rŽ   r   r‹   r   r‘   r”   r–   r—   rT   Zdo_include_nextr˜   rS   r   r   r   r   rI   æ   sD   (	




+"rI   c               @   s   e Zd ZdZdd„ ZdS )ÚDumbPreProcessora“  A preprocessor that ignores all #if/#elif/#else/#endif directives
    and just reports back *all* of the #include files (like the classic
    SCons scanner did).

    This is functionally equivalent to using a regular expression to
    find all of the #include lines, only slower.  It exists mainly as
    an example of how the main PreProcessor class can be sub-classed
    to tailor its behavior.
    c             O   s<   t j| f|ž|Ž | j}xdD ]}| j ||< ||< qW d S )N)r   r   r
   r   r   r   )rI   r8   rY   rx   )r7   r4   Úkwr\   Úfuncr   r   r   r8   y  s    
zDumbPreProcessor.__init__N)rE   rF   rG   rH   r8   r   r   r   r   r    o  s   	r    )#rH   r€   r+   Zcpp_lines_dictrV   ÚitemsZop_listr-   ÚcompileÚer]   r   rW   Úlr?   ÚMro   ZCPP_Expression_Cleaner_Listr   r   r   r)   ÚsortedÚlistÚmapÚescaper(   r*   r/   r0   rn   rœ   r2   rI   r    r   r   r   r   Ú<module>   sp   




%


   