o
    @a@F                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlm	Z	 ddl
mZ ddlmZmZmZmZmZmZmZ ddlmZ ddlmZ d	d
 Zdd Zdd ZG dd deZG dd deZG dd deZdd Zdd Z dd Z!dd Z"dS )z
This module provides a dummy parser for pythran annotations.
    * spec_parser reads the specs from a python module and returns them.
    pytype_to_pretty_type)defaultdict)productN)ListSetDictNDArrayTuplePointerFun)PythranSyntaxError)cfgc                 C   st  ddl m}m} ddl m}m} ddl m}m}m}m}	m	}
m
} ddl m}m}m}m}m}m} z
ddl m}m} W n tyG   |}|}Y nw t| trct| t|krWdS tdd	 t| |D S t|f}| |v rq||v rqd
S t|f}| |v r||v rd
S |	tf}| |v r||v rd
S t| t|urdS t| dsdS t| tu rt| j dd  |j dd  S t| j |j S )Nr   )	complex64
complex128)float32float64)int8int16int32int64intpintc)uint8uint16uint32uint64uintpuintc)
complex256float128Fc                 s   s    | ]
\}}t ||V  qd S N)ambiguous_types).0Zt0t1 r%   ./usr/lib/python3/dist-packages/pythran/spec.py	<genexpr>    s    z"ambiguous_types.<locals>.<genexpr>T__args__   )!numpyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    ImportError
isinstancetuplelenallzipfloatcomplexinttypehasattrr	   r"   r(   )Zty0Zty1r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    Zambiguous_float_typesZambiguous_cplx_typesZambiguous_int_typesr%   r%   r&   r"      s<     

r"   c                 C   sP   t | tsdS t| jd dkrdS | jd | jd   ko%tdd d kS   S )NFr)      )r,   r	   r.   r(   slicetr%   r%   r&   istransposed;   s
   
,r;   c                 C   s@   t | tsdS t| jd dkrdS tdd | jdd  D S )NFr)   r6   c                 s   s    | ]}|j d kV  qdS )r)   N)step)r#   sr%   r%   r&   r'   H   s    z!istransposable.<locals>.<genexpr>)r,   r	   r.   r(   r/   r9   r%   r%   r&   istransposableC   s
   
r>   c                   @   s6   e Zd ZdZdddZdd Zdd ZeZd	d
 ZdS )Specz
    Result of spec parsing.

    ``functions'' is a mapping from function name to a tuple of signatures
    ``capsule'' is a mapping from function name to signature
    Nc                 C   s\   t || _|p	t  | _| D ]\}}t|ts|f| j|< q| s,dd l}|d d S d S )Nr   z2No pythran specification, nothing will be exported)dict	functionscapsulesitemsr,   r-   loggingZwarning)selfrA   rB   fname
signaturesrD   r%   r%   r&   __init__S   s   

zSpec.__init__c                 C   s   t | j t | j  S r!   )listrA   keysrB   rE   r%   r%   r&   rJ   a   s   z	Spec.keysc                 C   s   t | jp| jS r!   )boolrA   rB   rK   r%   r%   r&   __bool__d      zSpec.__bool__c           	      C   s   | j  D ]9\}}t||}d|}||d}|s|}n|dd}|d d | }t|dkr:|d|d  7 }|||< qd S )NzSupported prototypes:
{} z

r)   r   z

    r6   )rA   rC   signatures_to_stringformatgetsplitr.   )	rE   Z
docstrings	func_namerG   sigdocsZdocstring_prototypesZdocstring_pyZ	docstringpartsr%   r%   r&   to_docstringsi   s   


zSpec.to_docstringsr!   )	__name__
__module____qualname____doc__rH   rJ   rM   Z__nonzero__rW   r%   r%   r%   r&   r?   K   s    
r?   c                   @   s  e Zd ZdZi dddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*Zd+d,d-d.d/d0d1d2d3d4d5d6Zee d7ee  Z	d8d9 e	D Z
d:d9 e
D Zd;Zd<Zd=Zd>Zd?Zd@ZdAZdBZdCZdDZdEZdFdG ZdHe_dIZdJdK ZdLdM ZdNe_dOdP ZdQe_dRdS ZdTe_dUdV ZdWe_dXdY ZdZe_d[d\ Zd]d^ e
 e_d_d` Z!dad^ e e!_dbdc Z"ddd^ e  e"_dedf Z#dge#_dhdi Z$dje$_dkdl Z%dme%_dndo Z&dpe&_dqdr Z'dse'_dtdu Z(dve(_dwdx Z)dye)_dzd{ Z*d|e*_d}d~ Z+de+_dd Z,de,_dd Z-de-_dd Z.de._dddZ/dd Z0dd Z1dddZ2dS )
SpecParsera8  
    A parser that scans a file lurking for lines such as the one below.

    It then generates a pythran-compatible signature to inject into compile.
#pythran export a((float,(int, uint8),str list) list list)
#pythran export a(str)
#pythran export a( (str,str), int, int16 list list)
#pythran export a( {str} )
rL   ZBOOLZbyteZBYTEr2   ZCOMPLEXr3   ZINTr1   ZFLOATr   ZUINT8r   ZUINT16r   ZUINT32r   ZUINT64r   ZUINTCr   ZUINTPr   ZINT8r   ZINT16r   ZINT32r   ZINT64r   ZINTCr   ZINTPZFLOAT32ZFLOAT64ZFLOAT128Z	COMPLEX64Z
COMPLEX128Z
COMPLEX256)r   r   r    r   r   r   PYTHRANZEXPORTZORDERZCAPSULEORLISTSETDICTZSLICEZSTRZNONE)#pythranZexportZorderZcapsuleorrI   setr@   r8   strNone)
IDENTIFIERZNUMZCOLUMNLPARENZRPARENZCRAPZOPTZLARRAYZRARRAYSTARCOMMAc                 C   s   g | ]}|d kr|qS )r]   r%   r#   tokr%   r%   r&   
<listcomp>       zSpecParser.<listcomp>c                 C   s   g | ]}|d vr|qS ))rh   rj   r%   rk   r%   r%   r&   rm      rn   z[^,:\(\)\[\]*?0-9],:z\(z\)z\]z\[z\*z\?z[1-9][0-9]*))leftr^   )rq   r_   ra   r`   c                 C   s   t j|jd|_|S )Nrg   )r\   reservedrR   valuer4   rE   r:   r%   r%   r&   t_IDENTIFER   s   zSpecParser.t_IDENTIFERz\#?[a-zA-Z_][a-zA-Z_0-9]*z 	
c                 C   s   |j d d S )Nr)   )lexerskiprt   r%   r%   r&   t_error   rN   zSpecParser.t_errorc                 C   sb   t |dkr-t |dk}t |dkr| jn| j}|t |d  D ]\}}||  |7  < q d S d S )Nr)         )r.   exportsnative_exports)rE   pZisnativetargetkeyvalr%   r%   r&   	p_exports   s   zSpecParser.p_exportszexports :
                   | PYTHRAN EXPORT export_list opt_craps exports
                   | PYTHRAN EXPORT CAPSULE export_list opt_craps exportsc                 C   s0   t |dkr|d fn|d |d f |d< d S )Nr6   r)   rz   r   r.   rE   r}   r%   r%   r&   p_export_list   s   0zSpecParser.p_export_listzAexport_list : export
                  | export_list COMMA exportc                 C   sP   t |dkr|d pd}nd}|d |f|d< | j|d   |df7  < d S )Nr6   rz   r%   r%   r)   r   )r.   export_infolexpos)rE   r}   Zsigsr%   r%   r&   p_export   s
   "zSpecParser.p_exportzexport : IDENTIFIER LPAREN opt_param_types RPAREN
                  | IDENTIFIER
                  | EXPORT LPAREN opt_param_types RPAREN
                  | ORDER LPAREN opt_param_types RPARENc                 C      d S r!   r%   r   r%   r%   r&   p_opt_craps      zSpecParser.p_opt_crapsz:opt_craps :
                     | some_crap opt_all_crapsc                 C   r   r!   r%   r   r%   r%   r&   p_opt_all_craps   r   zSpecParser.p_opt_all_crapsz9opt_all_craps :
                     | crap opt_all_crapsc                 C   r   r!   r%   r   r%   r%   r&   p_crap   r   zSpecParser.p_crapzcrap : z
| c                 C   r   r!   r%   r   r%   r%   r&   p_some_crap  r   zSpecParser.p_some_crapzsome_crap : c                 C   s"   dd l }t|d |jf|d< d S )Nr   r)   )r*   eval__dict__)rE   r}   r*   r%   r%   r&   p_dtype	  s   zSpecParser.p_dtypezdtype : c                 C   "   t |dkr
|d nt |d< d S Nr6   r)   r   r.   r-   r   r%   r%   r&   p_opt_param_types     "zSpecParser.p_opt_param_typesz4opt_param_types :
                     | param_typesc                 C   r   r   r   r   r%   r%   r&   p_opt_types  r   zSpecParser.p_opt_typesz(opt_types :
                     | typesc                    s   t  dkrtdd  d D  d< d S t  dkr,tdd  d D d  d< d S t  d	krCt fd
d d D  d< d S t fdd d D d  d< d S )Nr6   c                 s       | ]}|fV  qd S r!   r%   r#   r:   r%   r%   r&   r'         z+SpecParser.p_param_types.<locals>.<genexpr>r)   r   rz   c                 s   r   r!   r%   r   r%   r%   r&   r'     r   r      c                 3   (    | ]} d  D ]}|f| V  qqdS rz   Nr%   r#   r:   tsr}   r%   r&   r'   !     & c                 3   r   r   Nr%   r   r   r%   r&   r'   #  r   r   r   r%   r   r&   p_param_types  s   ""&zSpecParser.p_param_typeszparam_types : type
                       | type OPT
                       | type COMMA param_types
                       | type OPT COMMA default_typesc                    sT   t  dkrtdd  d D d  d< d S t fdd d D d  d< d S )Nrz   c                 s   r   r!   r%   r   r%   r%   r&   r'   ,  r   z-SpecParser.p_default_types.<locals>.<genexpr>r)   r   r   c                 3   r   r   r%   r   r   r%   r&   r'   .  r   r   r   r%   r   r&   p_default_types*  s   "&zSpecParser.p_default_typeszNdefault_types : type OPT
                       | type OPT COMMA default_typesc                    sL   t  dkrtdd  d D  d< d S t fdd d D  d< d S )Nr6   c                 s   r   r!   r%   r   r%   r%   r&   r'   4  r   z%SpecParser.p_types.<locals>.<genexpr>r)   r   c                 3   r   r   r%   r   r   r%   r&   r'   6  r   r   r   r%   r   r&   p_types2  s   "zSpecParser.p_typesz0types : type
                 | type COMMA typesc                    sn   t dkrd d fd< d S t dkr3d dkr5dd  t fd	d
d D d< d S d S d S )Nr6   r)   r      r   ]c                 S   s   t | tr| jS | fS r!   )r,   r	   r(   r9   r%   r%   r&   args?  s   z%SpecParser.p_array_type.<locals>.argsc                 3   s$    | ]}t  |d    V  qdS r   )r	   r   r   r}   r%   r&   r'   A  s   " z*SpecParser.p_array_type.<locals>.<genexpr>r   r   r%   r   r&   p_array_type;  s   $zSpecParser.p_array_typezKarray_type : dtype
                | array_type LARRAY array_indices RARRAYc                    sr  t  dkr d f d< d S t  dkr* d dkr*tdd  d D  d< d S t  dkrE d dkrEtd	d  d D  d< d S t  dkr d d u r|g } d D ]}|| t|rs|t|jd d
d d
d f  qWt| d< d S  d dkr d D ]}t|sd}|  | qtdd  d D  d< d S  d  d< d S t  dkrɈ d dkrt fdd d D  d< d S t  dkrt fdd d D  d< d S t  dkr d dkr d  d   d< d S t  dkr d dkrtdd  d D  d< d S t  dkr+ d dkr+ d  d< d S d j}|  | d S )Nr6   r)   r   rz   rI   c                 s       | ]}t | V  qd S r!   )r   r   r%   r%   r&   r'   J      z$SpecParser.p_type.<locals>.<genexpr>rd   c                 s   r   r!   )r   r   r%   r%   r&   r'   L  r   r7   Fz?Invalid Pythran spec. F order is only valid for 2D plain arraysc                 s   s,    | ]}t |jd  ddddf V  qdS )r   r7   N)r	   r(   )r#   ndr%   r%   r&   r'   [  s    $r   r   )c                 3   sF    | ]}t  d  dkrt d   n d  D ]	}t||f V  qqdS )rz   r)   N)r.   r   r   )r#   rr   r   r%   r&   r'   `  s    c                 3   s*    | ]} d  D ]	}t ||f V  qqdS r   )r   )r#   kvr   r%   r&   r'   e  s   ( rc   c                 s   r   r!   )r
   r   r%   r%   r&   r'   i  r   r   z(Invalid Pythran spec. Unknown text '{0}')	r.   r-   appendr>   r	   r(   p_errorrQ   rs   )rE   r}   Zexpandedr   msgr%   r   r&   p_typeF  sN   
""zSpecParser.p_typeaZ  type : term
                | array_type opt_order
                | pointer_type
                | type LIST
                | type SET
                | type LPAREN opt_types RPAREN
                | type COLUMN type DICT
                | LPAREN types RPAREN
                | LARRAY type RARRAY
                | type OR type
                c                 C   sN   t |dkr!|d dvrd|d }| || |d |d< d S d |d< d S )Nr)   rz   ZCFz(Invalid Pythran spec. Unknown order '{}'r   )r.   rQ   r   )rE   r}   r   r%   r%   r&   p_opt_order|  s   zSpecParser.p_opt_orderzAopt_order :
                     | ORDER LPAREN IDENTIFIER RPARENc                 C   s   t |d d  |d< d S )Nr)   r   )r   r   r%   r%   r&   p_pointer_type  s   zSpecParser.p_pointer_typezpointer_type : dtype STARc                 C   s8   t |dkr|d f|d< d S |d f|d  |d< d S )Nr6   r)   r   rz   r   r   r%   r%   r&   p_array_indices  s   zSpecParser.p_array_indiceszVarray_indices : array_index
                         | array_index COMMA array_indicesc                 C   sh   t |dkrtddd|d< d S t |dks|d dkr&tddd|d< d S tdt|d d|d< d S )Nrz   r   r7   r)   rp   )r.   r8   r3   r   r%   r%   r&   p_array_index  s
   zSpecParser.p_array_indexzqarray_index :
                       | NUM
                       | COLUMN
                       | COLUMN COLUMNc                 C   s`   |d dkrt |d< d S |d dkrt|d< d S |d dkr&td |d< d S |d d |d< d S )Nr)   re   r   r8   rf   )re   r8   r4   r   r%   r%   r&   p_term  s   zSpecParser.p_termzQterm : STR
                | NONE
                | SLICE
                | dtypeNc                 C   sV   t |}|d ur"| jdd|d }|| |_d| jdd| |_| jr)| j|_|S )N
r   r)   )r   
input_textrfindoffsetcountlineno
input_filefilename)rE   r   r   errZ
line_startr%   r%   r&   PythranSpecError  s   
zSpecParser.PythranSpecErrorc                 C   s6   |j dkr| d|j|j| d|j|j)Nrg   z(Unexpected identifier `{}` at that pointz#Unexpected token `{}` at that point)r4   r   rQ   rs   r   r   r%   r%   r&   r     s   


zSpecParser.p_errorc                 C   s&   t j | dd| _tj| ddd| _d S )NF)moduledebug)r   r   Zwrite_tables)lexrv   yaccparserrK   r%   r%   r&   rH     s
   zSpecParser.__init__c                 C   s
  t t| _t t| _t t| _|| _|| _g }d}|dD ]?}t	d|r3d}|
tdd| q|rQ| }|drH|
|dd q| }|
d q||  M }|
d qd|}| jj|| jdd | j D ]#\}}	t|	d	krd
|}
| j| d }| |
||	d | j|< qr| j D ]b\}}	t|	tddkr| d|t|	D ]G\}}t||}|	|d	 d  D ]5}t||}||krd|}
| j| d }| |
|t||rd||}
| j| | }| |
|qqqt| j| jS )NFr   z\s*#\s*pythranTrb   #rO   )rv   r   r)   z(Overloads not supported for capsule '{}'r7   r   typingZmax_export_overloadszToo many overloads for function '{}', probably due to automatic generation of C-style and Fortran-style memory layout. Please force a layout using `order(C)` or `order(F)` in the array signaturezDuplicate export entry {}.zAmbiguous overloads
	{}
	{}.)r   r-   r{   r|   r   r   r   rS   rematchr   substrip
startswithreplacejoinr   parserv   rC   r.   rQ   r   r   Zgetint	enumeratespec_to_stringr"   r?   )rE   textr   linesZin_pythran_exportlinestrippedZpythran_datar   Z	overloadsr   lociZty_iZsty_iZty_jZsty_jr%   r%   r&   __call__  sf   









zSpecParser.__call__r!   )3rX   rY   rZ   r[   Zdtypesrr   updater-   valuestokensZcrapZ	some_crapZt_CRAPZt_COMMAZt_COLUMNZt_LPARENZt_RPARENZt_RARRAYZt_LARRAYZt_STARZt_OPTZt_NUMZ
precedenceru   Zt_ignorerx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rH   r   r%   r%   r%   r&   r\   x   s    	


	
*	



r\   c                       s"   e Zd ZdZd fdd	Z  ZS )ExtraSpecParserzD
    Extension of SpecParser that works on extra .pythran files
    Nc                    s&   t jdd|t jd}tt| ||S )Nz
^\s*exportz#pythran export)flags)r   r   	MULTILINEsuperr   r   )rE   r   r   	__class__r%   r&   r     s   
zExtraSpecParser.__call__r!   )rX   rY   rZ   r[   r   __classcell__r%   r%   r   r&   r   	  s    r   c                 C   s    dd |D }d | d|S )Nc                 S   s   g | ]}t |qS r%   r   r   r%   r%   r&   rm     s    z"spec_to_string.<locals>.<listcomp>z{}({})z, )rQ   r   )Zfunction_namespecZarguments_typesr%   r%   r&   r     s   r   c                    s<    fdd|D }|s fdd|D }d dd |D S )Nc                    s(   g | ]}t d d |D st |qS )c                 s   s    | ]}t |V  qd S r!   )r;   r   r%   r%   r&   r'     r   z2signatures_to_string.<locals>.<listcomp>.<genexpr>)anyr   r#   ZsigrT   r%   r&   rm     s    z(signatures_to_string.<locals>.<listcomp>c                    s   g | ]}t  |qS r%   )r   r   r   r%   r&   rm      s    rO   c                 s   s    | ]}d | V  qdS )z
    - Nr%   )r#   Zsigdocr%   r%   r&   r'   !  r   z'signatures_to_string.<locals>.<genexpr>)r   )rT   rG   rU   r%   r   r&   rP     s   rP   c                 C   s
   t  | S r!   )r\   )r   r%   r%   r&   spec_parser$  s   
r   c                 C   s>   t | }t | | dW  d    S 1 sw   Y  d S )N)r   )openr   read)filepathfdr%   r%   r&   load_specfile(  s   
$r   )#r[   Zpythran.types.conversionr   collectionsr   	itertoolsr   r   Zply.lexr   Zply.yaccr   Zpythran.typingr   r   r   r	   r
   r   r   Zpythran.syntaxr   Zpythran.configr   r"   r;   r>   objectr?   r\   r   r   rP   r   r   r%   r%   r%   r&   <module>   s.    $)-   
