3 MZR@sddlmZddlmZmZddlmZmZyddlmZ Wne k rXddl Z YnXddl Z ddl Z ddl Z y(e jd#krddlZnddlZejZWne k rdZYnXdZe jd e je jBZe jd e je jBZe jd Ze jd Ze jd Ze jdZdae jde jZe jdZ e jdZ!e jdZ"e jdZ#e jdZ$e jdZ%e jdZ&ddZ'ddZ(ddZ)ddZ*dd Z+Gd!d"d"e,Z-dS)$)model) COMMON_TYPESresolve_common_type)FFIError CDefError) _pycparserNzz/\*.*?\*/|//([^\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*\.\.\.cCstdkrtjatS)N) _parser_cache pycparserZCParserr r /usr/lib64/python3.6/cparser.py _get_parser*srcCs g}xtj|}|sP|j|d|j|jdd}|j|j|j}|jd|rn|jd|d7}d}|}x`|t|kr||}|dkr|d7}n.|dkr|dkrP|d8}n|dkr|dkrP|d7}qxW||||||d}qW|j|dj|S)N()*rrz,;=) _r_star_const_spacesearchappendstartgroupend startswithlenjoin)csourcepartsmatchclosingendposlevelicr r r _workaround_for_old_pycparser0s<          r$cCs0g}xtj|}|sP|jd}|j|d|jd|jdkrT|jdn |jd||dkr|jd|}|dkrtd|jd|d|dkrtd |j||d|||dd}nB|jd |}|dkrtd |j|||d||dd}|jd qW|j|d j |S)NrCz(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_pythonrrrrrfindrNotImplementedErrorr)rrrr rZ semicolonr r r _preprocess_extern_pythonZs4         r,c Cs\tjd|}i}x6tj|D](}|j\}}|jddj}|||<qWtjd|}tjdkrft |}t jd|}t jd|}t jd|}t |}tjd|}ttj|}xtt|D]v\}}|j}||dkr|jd ||j}d |d||||d df}qd |d||||d df}qWtjd |}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_commentsub _r_definefinditergroupsreplacestripr __version__r$ _r_stdcall2 _r_stdcall1_r_cdeclr,_r_partial_arraylist_r_partial_enum enumeratereversedrr*r_r_int_dotdotdot_r_float_dotdotdot) rmacrosrZ macronameZ macrovalueZmatchesZnumberpZp2r r r _preprocesss4           rCcCstt}|jd|jd|jd|jd|jdt}d}d}d}xtj|D]}||kr|dkr|r|j||j|d}nj|dkrd }d}nX|dkr|d 7}nF|dkr|d 8}n4|dkr|r|dkr|j||j|n |j||}qXW|S) Nr(,rrtypedefFrrTr)setradd_r_wordsfindalldiscard)rZlook_for_wordsZ words_usedZ is_typedefZparenZ previous_wordZwordr r r _common_type_namess>             rKc@seZdZddZddZddZddZd8d d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZd9ddZdd Zd:d"d#Zd;d$d%Zdd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd!S)?ParsercCs:i|_t|_d|_tj|_i|_i|_g|_ d|_ dS)Nr) _declarationsrF_included_declarations_anonymous_counterweakrefWeakKeyDictionary_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" )rCrKsortedrMrrrJCDEF_SOURCE_STRINGrlockacquirerparser Zc_parserZ ParseErrorconvert_pycparser_errorrelease) rWrrAZctn typenamesname csourcelinestypenameZ fullcsourceaster r r _parses4          "  z Parser._parsecCsfd}t|}tjdtf|}|rbt|jdd}|j}d|koPt|knrb||d}|S)Nz %s:(\d+):r )strrerr\intr splitlinesr)rWrgrlinemsgrZlinenumrdr 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)overridepacked 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^|jst d|d}t|j j tjjr|j j jdjdr|j|}nxt|j tjjr.t|j j tjjr.t|j j j tjjr.|j j j jdjdr.|j|}n|j|j |jdd\}}|jd |j||d qT|jjd krnqTt d |jj|qTWWnt k r} z&t| jdkr| 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)rcpartial_length_okztypedef )qualsZPragmazBunexpected <%s>: this construct is valid C but not valid in cdef()z *** Err: %sry)rh_process_macrositerextrc_inside_extern_python isinstancer c_astZDecl _parse_declZTypedefrtypeIdentifierTypenamesr_get_unknown_typePtrDeclTypeDecl_get_unknown_ptr_type_get_type_and_quals_declare __class____name__rargsrrp) rWrrfrAiteratordeclZ current_declrxZrealtypergror r r rt?sV          zParser._internal_parsecCs8||jkr*|j||krdStd|f||j|<dS)Nz%multiple declarations of constant: %s)rTr)rWkeyvalr r r _add_constantsus   zParser._add_constantscCs|jjd}|jd}|r(|dd}|jdrV|dkrV|jd rVd|dd}t|d}|rj| }|j|||jd||dS) NZul-r00xZ0orzmacro )lowerrstriprrlrr)rWrcZint_strnegZpyvaluer r r _add_integer_constant}s     zParser._add_integer_constantcCshxb|jD]V\}}|j}tj|r2|j||q |dkrL|jd||q td||||fq WdS)Nz...zmacro zonly 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_literalrrrr)rWrArvaluer r r rzs zParser._process_macroscCsX|j||}|jjdrd}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_pointerrSgetr}rrc)rWtprxrtagr r r _declare_functions    zParser._declare_functioncCs|j}t|tjjr:|j||jd\}}|j|||nt|tjjrV|j d|nHt|tjj rr|j d|n,t|tjj r|j d|n|jst d||jr|j|dd\}}|j r|j|||n$|jot|dot|jd otj|jjr|j|j|jjn|jrxt|jtjjrx|jjd krxt|jjd rxtj|jjjrx|j|jd |jjjn||tjkr|jjd r|j|_nX|jd krt d |tj@r|j r|jd|j||dn|jd|j||ddS)N)rcstructunionenumz'construct does not declare any variableT)rwinitrrZ__cffi_extern_python_rvz|sz/Parser._parse_function_type..rxr rrZ __stdcallryryry)r;getattrrr=rrrr~rr rrrrpoprrrrxrtuple) rWrfuncnamerr"argZellipsisrresultrxabir )rWr rgs6       zParser._parse_function_typecCs6t|tjrtj|j|St|tjr.|jS|SdS)N)r~rrritemrr)rWrrxr r r rs   zParser._as_func_argc Cs,y |j|Stk rYnX|}|j}|dkrb|dk rDd|}n|jd7_d|j}d}n"|}d||f}|jj|d\}} |dkr|dkrtj|ddd}nP|dkrtj|ddd}n6|dkr|dkrt d |j ||j }nt d |f|dk r2|j ||n"|dkr2|j dk r2td ||jsD|j||jrjd |jkrj|j d |j|||j|<|dkr|S|jdkr|S|jdk rt d|g} g} g} g} x|jD]}t|jtjjrdj|jjdkr|j||q|jdkrd}n |j|j}d|_|j|jdd\}}|jrH|j||t|tjrj|jrj|j||| j |jpxd| j || j || j |qWt!| |_t!| |_"t!| |_#t!| |_$| dgt%| krt|tjr|jrtd|f|j&jd|_'|j(r(d|_(|j)j ||S)Nz$%srz$%dz%s %srrrruz!Enums cannot be declared with ...z kind = %rz^enum %s: the '{}' declaration should appear on the first time the enum is mentioned, not laterrz anonymous %sz"duplicate declaration of struct %srFT)rwz#%s: using both bitfields and '...;'rrr)NNryry)*rRKeyErrorrcrOrMrrZ StructTypeZ UnionTyper_build_enum_typevaluesAssertionErrorrr+rZforce_the_namedeclsfldnamesr~rr rrrr _make_partialbitsizer_partial_lengthrpartialrrfldtypes fldbitsizefldqualsrrSrrZ completedrU)rWkindrrcrZ force_name explicit_namerr_rrrrrrZfqualr r r rs                             z"Parser._get_struct_union_enum_typecCsBt|tjstd|f|j r8| r8td|fd|_dS)Nz%s cannot be partialz%s is partial but has no C nameT)r~rZ StructOrUnionrZ has_c_namer+r)rWrrr r r rs  zParser._make_partialcCst|tjjr|j}|jdrF|jds2|jdrsN               *27(