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 Cstjd|}i}x6tj|D](}|j\}}|jddj}|||<qWtjd|}tjdkrft |}t jd|}t jd|}t jd|}t |}tjd|}ttj|}xtt|D]\}}|j}||dkr|jd ||j}||kstd |d||||d df}q|||d d ks4td |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*rAssertionError_r_int_dotdotdot_r_float_dotdotdot) rmacrosrZ macronameZ macrovalueZmatchesZnumberpZp2r r r _preprocesss8            rDcCstt}|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>             rLc@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) _declarationsrG_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" )rDrLsortedrNrrrKCDEF_SOURCE_STRINGrlockacquirerparser Zc_parserZ ParseErrorconvert_pycparser_errorrelease) rXrrBZctn 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)rXrhrlinemsgrZlinenumrer 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)overridepacked dllexport)rT_internal_parse)rXrrrrsrtZ prev_optionsr r r r`5s z Parser.parsec Cs$|j|\}}}|j|t|j}x |D]}|jdkr*Pq*WdsFtd}yBd|_x4|D]*}|}t|tj j r|j |q\t|tj j rf|jst d|d}t|jjtj jr|jjjdjdr|j|}nxt|jtj jr6t|jjtj jr6t|jjjtj jr6|jjjjdjdr6|j|}n|j|j|jdd\}}|jd |j||d q\|jjd krvq\t d |jj|q\WWnt k r} z&t| jdkr| 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)rdpartial_length_okztypedef )qualsZPragmazBunexpected <%s>: this construct is valid C but not valid in cdef()z *** Err: %srz)ri_process_macrositerextrdr?_inside_extern_python isinstancer c_astZDecl _parse_declZTypedefrtypeIdentifierTypenamesr_get_unknown_typePtrDeclTypeDecl_get_unknown_ptr_type_get_type_and_quals_declare __class____name__rargsrrq) rXrrgrBiteratordeclZ current_declryZrealtyperhrpr r r ru?sX          zParser._internal_parsecCs8||jkr*|j||krdStd|f||j|<dS)Nz%multiple declarations of constant: %s)rUr)rXkeyvalr r r _add_constantsus   zParser._add_constantscCs|jjd}|jd}|r(|dd}|jdrV|dkrV|jd rVd|dd}t|d}|rj| }|j|||jd||dS) NZul-r00xZ0orzmacro )lowerrstriprrmrr)rXrdZint_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)rXrBrvaluer r r r{s zParser._process_macroscCsX|j||}|jjdrd}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_pointerrTgetr~rrd)rXtpryrtagr r r _declare_functions    zParser._declare_functioncCs|j}t|tjjrJ|j||jd\}}t|tjs8t |j |||nt|tjj rf|j d|nHt|tjj r|j d|n,t|tjjr|j d|n|jstd||jr |j|dd\}}|jr|j |||n*|jr(t|dr(t|jd r(tj|jjr(|j|j|jjn|jrt|jtjjr|jjd krt|jjd rtj|jjjr|j|jd |jjjn||tjkr|jjd r|j|_nX|jd krtd |tj@r|j r|j d|j||dn|j d|j||ddS)N)rdstructunionenumz'construct does not declare any variableT)rxinitrrZ__cffi_extern_python_rwz|sz/Parser._parse_function_type..ryr rrZ __stdcallrzrzrz)r;getattrrr=rrrrrr rrrrpoprrrryrtuple) rXrfuncnamerr"argZellipsisrresultryabir )rXr rgs6       zParser._parse_function_typecCs6t|tjrtj|j|St|tjr.|jS|SdS)N)rrrritemrr)rXrryr 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 %srrrrvz!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)rxz#%s: using both bitfields and '...;'rsr)NNrzrz)*rSKeyErrorrdrPrNrrZ StructTypeZ UnionTyper_build_enum_typevaluesr?rr+rZforce_the_namedeclsfldnamesrrr rrrr _make_partialbitsizer_partial_lengthrpartialrrfldtypes fldbitsizefldqualsrrTrsZ completedrV)rXkindrrdrZ 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)rrZ StructOrUnionrZ has_c_namer+r)rXrrr r r rs  zParser._make_partialcCst|tjjr|j}|jdrF|jds2|jdrsN               *27(