3 M��ZR��@s�ddlmZddlmZmZddlmZmZyddlmZ Wne k rXddl Z YnXddl Z ddl Z ddl Z y(e jd#kr�ddlZnddlZej�ZWne k r�dZYnXdZe jd e je jB�Ze jd e je jB�Ze jd �Ze jd �Ze jd �Ze jd�Zdae jde j�Ze jd�Z e jd�Z!e jd�Z"e jd�Z#e jd�Z$e jd�Z%e jd�Z&dd�Z'dd�Z(dd�Z)dd�Z*dd �Z+Gd!d"�d"e,�Z-dS)$�)�model)� COMMON_TYPES�resolve_common_type)�FFIError� CDefError)� _pycparser�N�zz/\*.*?\*/|//([^\n\\]|\\.)*?$z?^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)\b((?:[^\n\\]|\\.)*?)$z=\s*\.\.\.\s*[,}]|\.\.\.\s*\}z__dotdotdot\d+__$z\[\s*\.\.\.\s*\]z\w+|\Sz-?0?x?[0-9a-f]+[lu]*$z\b(__stdcall|WINAPI)\bz[(]\s*(__stdcall|WINAPI)\bz \b__cdecl\bz9\bextern\s*"(Python|Python\s*\+\s*C|C\s*\+\s*Python)"\s*.z'[*]\s*((const|volatile|restrict)\b\s*)+z3(\b(int|long|short|signed|unsigned|char)\s*)+\.\.\.z\b(double|float)\s*\.\.\.cCstdkrtj�atS)N)� _parser_cache� pycparserZCParser�r r �/usr/lib64/python3.6/cparser.py� _get_parser*srcCs g}x�tj|�}|sP|j|d|j���|jd�d}|j|j��|j�}|jd|�rn|jd�|d7}d}|}x`|t|�kr�||}|dkr�|d7}n.|dkr�|dkr�P|d8}n|dkr�|dkr�P|d7}qxW|||�|||d�}qW|j|�dj|�S)N�(�)�*rrz,;=�) �_r_star_const_space�search�append�start�group�end� startswith�len�join)�csource�parts�match�closing�endpos�level�i�cr r r �_workaround_for_old_pycparser0s<          r$cCs0g}�xtj|�}|sP|j�d}|j|d|j���d|jd�krT|jd�n |jd�||dkr�|jd|�}|dkr�td��|jd|d|�dkr�td ��|j||d|��||dd�}nB|jd |�}|dkr�td ��|j|||d��||dd�}|jd �qW|j|�d j |�S)Nr�Cz(void __cffi_extern_python_plus_c_start; z!void __cffi_extern_python_start; �{�}rz!'extern "Python" {': no '}' foundz7cannot use { } inside a block 'extern "Python" { ... }'�;z'extern "Python": no ';' foundz void __cffi_extern_python_stop;r) �_r_extern_pythonrrrrr�findr�NotImplementedErrorr)rrrr rZ semicolonr r r �_preprocess_extern_pythonZs4         r,c Cs�tjd|�}i}x6tj|�D](}|j�\}}|jdd�j�}|||<qWtjd|�}tjdkrft |�}t jd|�}t jd|�}t jd|�}t |�}tjd|�}ttj|��}x�tt|��D]�\}}|j�}||dk�r|jd ||j��}||ks�t�d |d|�|||d d�f}q�|||d �d k�s4t�d |d|�|||d d�f}q�Wtjd |�}tjd|�}|jd d�|fS)N� z\ rz2.14z volatile volatile const(z volatile volatile const z[__dotdotdotarray__]�=z...z%s,__dotdotdot%d__ %sr z%s __dotdotdot%d__ %sz __dotdotdotint__ z __dotdotdotfloat__ z __dotdotdot__ )� _r_comment�sub� _r_define�finditer�groups�replace�stripr � __version__r$� _r_stdcall2� _r_stdcall1�_r_cdeclr,�_r_partial_array�list�_r_partial_enum� enumerate�reversedrr*r�AssertionError�_r_int_dotdotdot�_r_float_dotdotdot) r�macrosrZ macronameZ macrovalueZmatchesZnumber�pZp2r r r � _preprocess�s8            rDcCstt�}|jd�|jd�|jd�|jd�|jd�t�}d}d}d}x�tj|�D]�}||kr�|dkr�|r�|j|�|j|�d}nj|dkr�d }d}nX|dkr�|d 7}nF|dkr�|d 8}n4|dkr�|r�|dkr�|j|�|j|�n |j|�|}qXW|S) Nr(�,rr�typedefFrrTr)�setr�add�_r_words�findall�discard)rZlook_for_wordsZ words_usedZ is_typedefZparenZ previous_wordZwordr r r �_common_type_names�s>             rLc@s�eZdZdd�Zdd�Zdd�Zdd�Zd8d d �Zd d �Zdd�Z dd�Z dd�Z dd�Z dd�Z dd�Zdd�Zd9dd�Zdd �Zd:d"d#�Zd;d$d%�Zdd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd!S)?�ParsercCs:i|_t�|_d|_tj�|_i|_i|_g|_ d|_ dS)Nr) � _declarationsrG�_included_declarations�_anonymous_counter�weakref�WeakKeyDictionary�_structnode2type�_options�_int_constants� _recomplete�_uses_new_feature)�selfr r r �__init__�s zParser.__init__c Cs4t|�\}}t|�}g}x"ztypedef int %s;z@typedef int __dotdotdotint__, __dotdotdotfloat__, __dotdotdot__;z# 1 "%s"� )rDrL�sortedrNrrrK�CDEF_SOURCE_STRINGr�lock�acquirer�parser Zc_parserZ ParseError�convert_pycparser_error�release) rXrrBZctn� typenames�name� csourcelines�typenameZ fullcsource�ast�er r r �_parse�s4          "  z Parser._parsecCsfd}t|�}tjdtf|�}|rbt|jd�d�}|j�}d|koPt|�knrb||d}|S)Nz %s:(\d+):r� )�str�rerr]�intr� splitlinesr)rXrhr�line�msgrZlinenumrer r r �_convert_pycparser_errors zParser._convert_pycparser_errorcCs@|j||�}t|�}|r*d|j�|f}n d|f}t|��dS)Nzcannot parse "%s" %szparse error %s)rqrkr5r)rXrhrrorpr r r ra+s   zParser.convert_pycparser_errorFc Cs0|j}z|||d�|_|j|�Wd||_XdS)N)�override�packed� dllexport)rT�_internal_parse)rXrrrrsrtZ prev_optionsr r r r`5s z Parser.parsec Cs$|j|�\}}}|j|�t|j�}x |D]}|jdkr*Pq*WdsFt�d}�yBd|_�x4|D�]*}|}t|tj j �r�|j |�q\t|tj j ��rf|js�t d|��d}t|jjtj j�r�|jjjdjd�r�|j|�}nxt|jtj j��r6t|jjtj j��r6t|jjjtj j��r6|jjjjdjd��r6|j|�}n|j|j|jdd�\}}|jd |j||d �q\|jjd k�rvq\t d |jj|��q\WWn�t k �r�} z&t| j�dk�r�| j|f| _�WYdd} ~ XnNtk �r} z0|j| |�} | �r | jdd | f| _�WYdd} ~ XnXdS)N� __dotdotdot__r�__cffi_extern_python_stopz!typedef does not declare any namerZ __dotdotdotT)rd�partial_length_okztypedef )�qualsZPragmazBunexpected <%s>: this construct is valid C but not valid in cdef()z *** Err: %s�����rz)ri�_process_macros�iter�extrdr?�_inside_extern_python� isinstancer �c_astZDecl� _parse_declZTypedefr�type�IdentifierType�namesr�_get_unknown_type�PtrDecl�TypeDecl�_get_unknown_ptr_type�_get_type_and_quals�_declare� __class__�__name__r�argsrrq) rXrrgrB�iterator�declZ current_declryZrealtyperhrpr r r ru?sX          zParser._internal_parsecCs8||jkr*|j||krdStd|f��||j|<dS)Nz%multiple declarations of constant: %s)rUr)rX�key�valr r r �_add_constantsus   zParser._add_constantscCs�|j�jd�}|jd�}|r(|dd�}|jd�rV|dkrV|jd� rVd|dd�}t|d�}|rj| }|j||�|jd||�dS) NZul�-r�0�0xZ0orzmacro )�lower�rstriprrmr�r�)rXrdZint_str�negZpyvaluer r r �_add_integer_constant}s     zParser._add_integer_constantcCshxb|j�D]V\}}|j�}tj|�r2|j||�q |dkrL|jd||�q td||||f��q WdS)Nz...zmacro z�only supports one of the following syntax: #define %s ... (literally dot-dot-dot) #define %s NUMBER (with NUMBER an integer constant, decimal/hex/octal) got: #define %s %s)�itemsr5�_r_int_literalrr�r�r)rXrBr��valuer r r r{�s zParser._process_macroscCsX|j||�}|jjd�rd}n$|jdkr.d}n|jdkr>d}nd}|j||j|�dS)Nrtzdllexport_python Z__cffi_extern_python_startzextern_python Z!__cffi_extern_python_plus_c_startzextern_python_plus_c z function )�_get_type_pointerrT�getr~r�rd)rX�tpryr��tagr r r �_declare_function�s    zParser._declare_functioncCs|j}t|tjj�rJ|j||jd�\}}t|tj�s8t �|j |||��n�t|tjj �rf|j d|�nHt|tjj �r�|j d|�n,t|tjj�r�|j d|�n|js�td|��|j�r |j|dd�\}}|jr�|j |||��n*|j��r(t|d��r(t|jd ��r(tj|jj��r(|j|j|jj�n�|j��r�t|jtjj��r�|jjd k�r�t|jjd ��r�tj|jjj��r�|j|jd |jjj�n||tjk�r�|jjd ��r�|j|_nX|jd k�r�td ��|tj@�r�|j �r�|j d|j||d�n|j d|j||d�dS)N)rd�struct�union�enumz'construct does not declare any variableT)rx�initr�r�Z__cffi_extern_python_rwz|sz/Parser._parse_function_type..ryr r�r�Z __stdcallrzrzrz�����)r;�getattrr�r=r�rrrr�r r�r�r�r��poprr�r�ryr��tuple) rXr��funcnamer�r"�argZellipsisr��resultry�abir )rXr r�gs6       zParser._parse_function_typecCs6t|tj�rtj|j|�St|tj�r.|j�S|SdS)N)rrr�r��itemr�r�)rXr�ryr r r r��s   zParser._as_func_argc Cs,y |j|Stk rYnX|}|j}|dkrb|dk rDd|}n|jd7_d|j}d}n"|}d||f}|jj|d�\}} |dk�r|dkr�tj|ddd�}nP|dkr�tj|ddd�}n6|dkr�|dkr�t d ��|j ||j �}nt d |f��|dk �r2|j ||�n"|dk�r2|j dk �r2td |��|j�sD|j|�|j�rjd |jk�rj|j d |j|�||j|<|dk�r�|S|jdk�r�|S|jdk �r�t d|��g} g} g} g} x�|jD]�}t|jtjj��r�dj|jj�dk�r�|j||��q�|jdk�rd}n |j|j�}d|_|j|jdd�\}}|j�rH|j||�t|tj��rj|j�rj|j||�| j |j�pxd�| j |�| j |�| j |��q�Wt!| �|_t!| �|_"t!| �|_#t!| �|_$| dgt%| �k�rt|tj��r|j�rtd|f��|j&jd�|_'|j(�r(d|_(|j)j |�|S)Nz$%srz$%dz%s %sr�r�r�rvz!Enums cannot be declared with ...z kind = %rz^enum %s: the '{}' declaration should appear on the first time the enum is mentioned, not laterr�z anonymous %sz"duplicate declaration of struct %srFT)rxz#%s: using both bitfields and '...;'rsr)NNrzrz)*rS�KeyErrorrdrPrNr�rZ StructTypeZ UnionTyper�_build_enum_type�valuesr?r�r+r�Zforce_the_name�decls�fldnamesrr�r r�r�rr�� _make_partial�bitsizer��_partial_lengthr��partialrr��fldtypes� fldbitsize�fldqualsrrTrsZ completedrV)rX�kindr�rdr�Z force_name� explicit_namer�r��_r�r�r�r�r�r�Zfqualr r r r��s�                             z"Parser._get_struct_union_enum_typecCsBt|tj�std|f��|j� r8| r8td|f��d|_dS)Nz%s cannot be partialz%s is partial but has no C nameT)rrZ StructOrUnionrZ has_c_namer+r�)rXr�r�r r r r�s  zParser._make_partialcCs�t|tjj�r�|j}|jd�rF|jd�s2|jd�rsN               *27(