3 rW @slddlmZddlZejjdkr*eefZneZeZ d3Z dZ ddZ dZ dZdZddZeZdZddZddZddZddZddZddlZddlZddlZddlZejd Zd!d"d#d$d%d&d'd(d)d* Zd+d,Z Gd-d.d.e!Z"Gd/d0d0e!Z#e$d1krhddl%j&Z&e&j&Z'ddlZe(ej)d2Z*e*j+Z,e#e'Z-e-j.e,ej)d2x"e-j/Z0e0sVPe1e-j2e0qFWdS)4) generatorsNCPP_ID CPP_INTEGER CPP_FLOAT CPP_STRINGCPP_CHARCPP_WS CPP_COMMENT1 CPP_COMMENT2 CPP_POUND CPP_DPOUNDz+-*/%|&~^<>=!?()[]{}.,;:\'"cCs|jj|jjd7_|S)z\s+ )lexerlinenovaluecount)tr/usr/lib/python3.6/cpp.pyt_CPP_WS!srz\#z\#\#z[A-Za-z_][\w_]*cCs|S)zA(((((0x)|(0X))[0-9a-fA-F]+)|(\d+))([uU][lL]|[lL][uU]|[uU]|[lL])?)r)rrrrr-sz?((\d+)(\.\d+)(e(\+|-)?(\d+))? | (\d+)e(\+|-)?(\d+))([lL]|[fF])?cCs|jj|jjd7_|S)z\"([^\\\n]|(\\(.|\n)))*?\"r)rrrr)rrrr t_CPP_STRING7srcCs|jj|jjd7_|S)z(L)?\'([^\\\n]|(\\(.|\n)))*?\'r)rrrr)rrrr t_CPP_CHAR=srcCs8|jjd}|jj|7_d|_|r.d|nd|_|S)z(/\*(.|\n)*?\*/)rr  )rrrrtype)rZncrrrrt_CPP_COMMENT1Cs  rcCsd|_d|_dS)z (//.*?(\n|$))r rN)rr)rrrrt_CPP_COMMENT2LsrcCs(|jd|_|jd|_|jjd|S)Nr)rrrskip)rrrrt_errorQs   rz\?\?[=/\'\(\)\!<>\-]#\^[]|{}~) =/'()!<>-cCstjdd|S)NcSst|jdS)Nr) _trigraph_repgroup)grrr{sztrigraph..) _trigraph_patsub)inputrrrtrigraphzsr:c@seZdZdddZdS)MacroNFcCs0||_||_||_||_|r&|d|_d|_dS)Nrr2)namerarglistvariadicvarargsource)selfr<rr=r>rrr__init__s zMacro.__init__)NF)__name__ __module__ __qualname__rBrrrrr;sr;c@seZdZd&ddZddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ d'ddZ ddZd(ddZddZddZd d!Zdifd"d#Zd$d%ZdS)) PreprocessorNcCsl|dkrtj}||_i|_g|_g|_|jtj}|jdtj d||jdtj d|d|_ dS)Nz __DATE__ "%s"z%b %d %Yz __TIME__ "%s"z%H:%M:%S) lexrmacrospath temp_pathlexprobetimeZ localtimedefineZstrftimeparser)rArZtmrrrrBszPreprocessor.__init__cCs4g}|jj|x|jj}|s"P|j|qW|S)N)rr9tokenappend)rAtexttokenstokrrrtokenizes  zPreprocessor.tokenizecCstd|||fdS)Nz%s:%d %s)print)rAfilelinemsgrrrerrorszPreprocessor.errorc Cs|jjd|jj}| s&|jdkr0tdn|j|_|jjd|jj}| sbt|jdkrltdn|j|_t|j|_ |jjd|jj}| s|jdkrtdn|j|_ |jjd|jj}| s|jdkrd|_ n|j|_ |jjd |jj}| s|jd kr(d|_ td n|j|_ |j |j f|_ d d d ddddddg }xD|D]<}|jj||jj}| s|j|krZtd|qZWdS)NZ identifierz"Couldn't determine identifier typeZ12345i90zCouldn't determine integer typez "filename"zCouldn't determine string typez rz%Couldn't determine token for newlinesr/r0r z##r!r,r-,.z,Unable to lex '%s' required for preprocessor)rr9rOrrUrt_IDint t_INTEGERt_INTEGER_TYPEt_STRINGZt_SPACEZ t_NEWLINEt_WS)rArScharscrrrrKsD                  zPreprocessor.lexprobecCs|jj|dS)N)rIrP)rArIrrradd_path szPreprocessor.add_pathccs|jj}dd|jD}xhtt|D]X}|d}xJ||jdr|t|kr||dd||||<d||<|d7}q8Wq*Wdj|}|j|d|_g}x<|j }|sP|j ||j |j krd|j kr|Vg}qW|r|VdS)NcSsg|] }|jqSr)rstrip).0xrrr sz,Preprocessor.group_lines..rr!rr2)rZclone splitlinesxrangelenendswithjoinr9rrOrPrrar)rAr9rGlinesijZ current_linerSrrr group_liness,    zPreprocessor.group_linescCs|d}x(|t|kr,||j|jkr,|d7}qW|d|=t|d}x$|dkrh||j|jkrh|d8}qFW||dd=|S)Nrr)rlrra)rArRrprrr tokenstrip9s    zPreprocessor.tokenstripc Csg}g}g}d}t|}d}x$||kr@||j|jkr@|d7}qW||krh||jdkrh|j|dn |j|j|djddggfS|d7}x||krf||}|jdkr|j||d7}n|jdkr|d8}|dkr |r|j|j||j||d||fS|j|nD|jdkrR|dkrR|j|j||j|dg}n |j||d7}qW|j|j|djddggfS) Nrrr,zMissing '(' in macro argumentsr-rZzMissing ')' in macro argumentsr2) rlrrarrPrYr@rrs) rAZ tokenlistargs positionsZ current_argZnestingZtokenlenrprrrr collect_argsUsD            zPreprocessor.collect_argscCsg|_g|_g|_d}x|t|jkr|j|j|jkoL|j|j|jkrh|jj|j|j}|dkr|j|djdkrt j |j||j|<|j |j|_|j|d=|jj ||dfqn|dko|j|djdkr |jj d||df|j|d=qnZ|dt|jkrT|j|djdkrT|jj d||f|d7}qn|jj d||fn|j|jdkr|j r|dkr|j|djdkr|dt|jkr|j|dj|jkr|j|dj|j kr|jj |d|d7}qW|jjdd d d dS) Nrrr z##rcerZcSs|dS)Nr)rgrrrr6sz,Preprocessor.macro_prescan..T)keyreverse)patch str_patchvar_comma_patchrlrrr\r=indexcopyr`rPr>r?sort)rAmacrorpargnumrrr macro_prescans:&  *(, zPreprocessor.macro_prescanc Cs0dd|jD}i}xb|jD]X\}}||krTddjdd||Djdd||<tj||||<||||_qWd}|jr|d rx|jD]}d||<d }qWi}xj|jD]`\} }}| d kr|||||d <q| d kr||kr|j||||<|||||d <qW|r,d d|D}|S)NcSsg|]}tj|qSr)r)rf_xrrrrhsz2Preprocessor.macro_expand_args..z"%s"ricSsg|] }|jqSr)r)rfrgrrrrhsr!z\\FrTrcrwcSsg|] }|r|qSrr)rf_irrrrhsr2) rr|rnreplacerr>r}r{ expand_macros) rArrtrepZ str_expansionrrpZ comma_patchexpandedZptyperrrmacro_expand_argss.(  zPreprocessor.macro_expand_argscCs|dkr i}d}x|t|kr||}|j|jkr|j|jkoL|j|krd||j<|j|j}|js|jdd|jD|}x|D] }|j|_qW||||d<|t|7}n|d}x(|t|kr||j|jkr|d7}qW||jdkr|j ||d\} } } |j r`t| t|jkr`|j |j |jd|jt|jf|| }nD|j rt| t|jdkrt|jdkr|j |j |jd |jt|jdfn&|j |j |jd |jt|jdf|| }n|j rXt| t|jdkr| j gnD||| t|jd|| d| t|jd<| t|jd=|j|| } |j| |} x| D]} |j| _qvW| |||| <|t| 7}||j=qn"|jd kr|j|_|j|j|_|d7}qW|S) NrTcSsg|]}tj|qSr)r)rfrrrrrhsz.Preprocessor.expand_macros..rr,zMacro %s requires %d argumentsrxz(Macro %s must have at least %d argumentsz'Macro %s must have at least %d argumentZ__LINE__)rlrr\rrHr=rrrarvr>rYr@rPrr^r_)rArRrrprmZexrwrqtokcountrtrurrrrrrs\     "  (&  4      zPreprocessor.expand_macrosc Cs^d}x|t|kr"||j|jko2||jdkr|d}d}d}x|t|kr||j|jkrp|d7}qHnn||j|jkr||j|jkrd}nd}|sPn<||jdkrd}n(||jd krPn|j|j||jd |d7}qHW|j ||_|j |||_||d|d=|d7}qW|j |}xt |D]\}}|j|jkrzt j |||<|j ||_|j d||_nd|j|j kr8t j |||<t||j||_x2||jdd kr||jdd||_qWq8Wd jd d|D}|jdd}|jdd}|jdd}y t|}Wn0tk rX|j|j|djdd}YnX|S)NrZdefinedrFZ0LZ1Lr,Tr-zMalformed defined()Z0123456789abcdefABCDEFricSsg|]}t|jqSr)strr)rfrgrrrrhTsz)Preprocessor.evalexpr..z&&z and z||z or r.z not zCouldn't evaluate expressionr2r2)rlrr\rrarHrYr@rr^r_r enumeraterrrnreval Exception)rArRrprqZ needparenresultrexprrrrevalexpr)s^      $     zPreprocessor.evalexprccst|}|j|}|sd}|jd|||_g}d}d}g}xN|D]D} x"t| D]\} } | j|jkrVPqVW| jdkr~x,| D]$} | j|jkrd| jkr|j| qW|j | | dd} | r| dj} |j | dd}nd} g}| d kr(|r|x|j |D] } | VqWg}|j|q| d kr|r|x|j |D] } | VqDWg}|j d }x|j |D] } | VqnW||j d <||_q| d kr|r|x|j |D] } | VqWg}|j |q| d kr|j||f|r||dj|j kr d}d}nd}q| dkrT|j||f|r||dj|j krLd}d}nd}q| dkr|j||f|r||j|}|sd}d}nd}n| dkr|r|ddr|rd}n|s|j|}|rd}d}n|j|j| djdn| dkrF|r.|ddrD|rd}n|sDd}d}n|j|j| djdn6| dkr|rd|j\}}n|j|j| djdnqF|rF|j| qFWx|j |D] } | VqWg}dS)Nriz __FILE__ "%s"TFr rrrrMincludeZ__FILE__undefZifdefZifndefifelifzMisplaced #elifelsezMisplaced #elseZendifzMisplaced #endifr2r2)r:rrrMr@rrrarrPrsrrHrrrrYrpopextend)rAr9r@rrochunkenableZ iftriggerZifstackrgrprSZ dirtokensr<rtZoldfilerrrrparsegends                        zPreprocessor.parsegenc cs|sdS|r|djdkr4|dj|jkr4|j|}|djdkrd}x4|t|krn||jdkrdP|d7}qHWtddSdjdd|d|D}|jdg|j}nB|dj|jkr|djdd }|jdg|j}n td dSx|D]}t jj||}y`t |d j }t jj |}|r6|jj d|x|j||D] } | VqDW|rb|jd=PWqtk r|YqXqWtd |dS) Nrr/rr0zMalformed #include <...>ricSsg|] }|jqSr)r)rfrgrrrrhsz(Preprocessor.include..zMalformed #include statementrzCouldn't find '%s'r2)rrr`rrlrUrnrIrJosopenreaddirnameinsertrIOError) rArRrpfilenamerIpZinamedataZdnamerSrrrrsF      zPreprocessor.includec Cst|tr|j|}|}y||d}t|dkr:|d}nd}|s^t|jg}||j|j<n6|j|jkrt|j|j |dd}||j|j<n|jdkr|j |dd\}}}d} x|D]} | rt dPdj dd | D} | d kr d } |j | d_d | d_d } | dd=qnb| ddd kr| dj|j krd } | dd=| djddd kr| djdd| d_qt| dks| dj|j krt dPqW|j |d|d} d} x| t| krX| dt| krL| | j|jkr| | djdkr| | =qn0| | jdkrL| | dj|jkrL| | d=| d7} qWt|j| dd |D| }|j|||j|j<nt dWntk rt dYnXdS)Nrrrxr,Fz0No more arguments may follow a variadic argumentricSsg|]}t|jqSr)rr)rfrrrrrh2sz'Preprocessor.define..z...TZ __VA_ARGS__rzInvalid macro argumentz##cSsg|]}|djqS)r)r)rfrgrrrrhPszBad macro definitionrr) isinstance STRING_TYPESrTrlr;rrHrrarsrvrUrnr\r LookupError)rArRZlinetokr<Zmtyperrrtrur>aZastrZmvaluerprrrrMsl            $ &&   zPreprocessor.definec Cs0|dj}y |j|=Wntk r*YnXdS)Nr)rrHr)rArRidrrrr^s   zPreprocessor.undefcCs||_|j|||_dS)N)ignorerrN)rAr9r@rrrrparsejszPreprocessor.parsec CsDy$xt|j}|j|jkr|SqWWntk r>d|_dSXdS)N)nextrNrr StopIteration)rArSrrrrOss  zPreprocessor.token)N)N)N)rCrDrErBrTrYrKrdrrrsrvrrrrrrrMrrrOrrrrrFs&  < !5+2 B; 1F  rF__main__r) rrrrrr r r r r )3Z __future__rsys version_infomajorrZunicoderrangerkrRliteralsrZ t_CPP_POUNDZ t_CPP_DPOUNDZt_CPP_IDrZ t_CPP_INTEGERZ t_CPP_FLOATrrrrrrerrLZos.pathrcompiler7r3r:objectr;rFrCZply.lexrGrrargvfrr9rrrOrSrUr@rrrr sl     c