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]v\}}|j�}||dk�r|jd ||j��}d |d|�|||d d�f}q�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�_r_int_dotdotdot�_r_float_dotdotdot) r�macrosrZ macronameZ macrovalueZmatchesZnumber�pZp2r r r � _preprocess�s4           rCcCstt�}|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>             rKc@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) � _declarationsrF�_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"� )rCrK�sortedrMrrrJ�CDEF_SOURCE_STRINGr�lock�acquirer�parser Zc_parserZ ParseError�convert_pycparser_error�release) rWrrAZctn� 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)rWrgr�line�msgrZlinenumrdr 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)rprjr5r)rWrgrrnror r r r`+s   zParser.convert_pycparser_errorFc Cs0|j}z|||d�|_|j|�Wd||_XdS)N)�override�packed� dllexport)rS�_internal_parse)rWrrqrrrsZ prev_optionsr r r r_5s z Parser.parsec Cs|j|�\}}}|j|�t|j�}x|D]}|jdkr*Pq*Wd}�yBd|_�x4|D�]*}|}t|tjj �rx|j |�qTt|tjj ��r^|js�t d|��d}t|j j tjj�r�|j j jdjd�r�|j|�}nxt|j tjj��r.t|j j tjj��r.t|j j j tjj��r.|j j j jdjd��r.|j|�}n|j|j |jdd�\}}|jd |j||d �qT|jjd k�rnqTt d |jj|��qTWWn�t k �r�} z&t| j�dk�r�| j|f| _�WYdd} ~ XnNtk �r} z0|j| |�} | �r| jdd | f| _�WYdd} ~ XnXdS)N� __dotdotdot__�__cffi_extern_python_stopz!typedef does not declare any namerrZ __dotdotdotT)rc�partial_length_okztypedef )�qualsZPragmazBunexpected <%s>: this construct is valid C but not valid in cdef()z *** Err: %s�����ry)rh�_process_macros�iter�extrc�_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�argsrrp) rWrrfrA�iterator�declZ current_declrxZrealtypergror r r rt?sV          zParser._internal_parsecCs8||jkr*|j||krdStd|f��||j|<dS)Nz%multiple declarations of constant: %s)rTr)rW�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�rstriprrlr�r�)rWrcZint_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)rWrAr��valuer r r rz�s zParser._process_macroscCsX|j||�}|jjd�rd}n$|jdkr.d}n|jdkr>d}nd}|j||j|�dS)Nrszdllexport_python Z__cffi_extern_python_startzextern_python Z!__cffi_extern_python_plus_c_startzextern_python_plus_c z function )�_get_type_pointerrS�getr}r�rc)rW�tprxr��tagr r r �_declare_function�s    zParser._declare_functioncCs�|j}t|tjj�r:|j||jd�\}}|j|||��n�t|tjj�rV|j d|�nHt|tjj �rr|j d|�n,t|tjj �r�|j d|�n|js�t d|��|j�r�|j|dd�\}}|j r�|j|||��n$|j�o�t|d�o�t|jd �o�tj|jj��r|j|j|jj�n�|j��rxt|jtjj��rx|jjd k�rxt|jjd ��rxtj|jjj��rx|j|jd |jjj�n||tjk�r�|jjd ��r�|j|_nX|jd k�r�t d ��|tj@�r�|j �r�|jd|j||d�n|jd|j||d�dS)N)rc�struct�union�enumz'construct does not declare any variableT)rw�initr�r�Z__cffi_extern_python_rvz|sz/Parser._parse_function_type..rxr r�r�Z __stdcallryryry�����)r;�getattrr�r=r�rrr~r�r rr�r�r��poprr�r�rxr��tuple) rWr��funcnamer�r"�argZellipsisr��resultrx�abir )rWr r�gs6       zParser._parse_function_typecCs6t|tj�rtj|j|�St|tj�r.|j�S|SdS)N)r~rr�r��itemr�r�)rWr�rxr 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�ruz!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)rwz#%s: using both bitfields and '...;'rrr)NNryry)*rR�KeyErrorrcrOrMr�rZ StructTypeZ UnionTyper�_build_enum_type�values�AssertionErrorr�r+r�Zforce_the_name�decls�fldnamesr~r�r rr�rr�� _make_partial�bitsizer��_partial_lengthr��partialrr��fldtypes� fldbitsize�fldqualsrrSrrZ completedrU)rW�kindr�rcr�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)r~rZ StructOrUnionrZ has_c_namer+r�)rWr�r�r r r r�s  zParser._make_partialcCs�t|tjj�r�|j}|jd�rF|jd�s2|jd�rsN               *27(