3 ]m8@s<ddlZddlZddlmZddlmZGdddeZdS)N)lex)TOKENc<@sleZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ d Z iZ xdZ?ddZ@dZAdZBdZCdZDdZEdZFdZGdZHdZIdZJdZKdZLdZMdZNdZOdZPdZQdZRdZSdZTdZUdZVdZWdZXdZYdZZdZ[dZ\dZ]dZ^dZ_dZ`dZadZbdZcdZddZedZfdZgdZhdZidZjdZkdZle2dddZme2dddZne'Zoe2e,ddZpe2e/ddZqe2eddZre2eddZse2eddZte2eddZue2eddZve2e"ddZwe2e#ddZxe2e$ddZye2e%ddZze2e(ddZ{e2e)ddZ|e2eddZ}d d Z~d S(CLexera A lexer for the C language. After building it, set the input text with input(), and call token() to get new tokens. The public attribute filename can be set to an initial filaneme, but the lexer will update it upon #line directives. cCs@||_||_||_||_d|_d|_tjd|_tjd|_ dS)ab Create a new Lexer. error_func: An error function. Will be called with an error message, line and column as arguments, in case of an error during lexing. on_lbrace_func, on_rbrace_func: Called when an LBRACE or RBRACE is encountered (likely to push/pop type_lookup_func's scope) type_lookup_func: A type lookup function. Given a string, it must return True IFF this string is a name of a type that was defined with a typedef earlier. Nz([ ]*line\W)|([ ]*\d+)z [ ]*pragma\W) error_funcon_lbrace_funcon_rbrace_functype_lookup_funcfilename last_tokenrecompile line_patternpragma_pattern)selfrrrr r/usr/lib/python3.6/c_lexer.py__init__s zCLexer.__init__cKstjfd|i||_dS)z Builds the lexer from the specification. Must be called after the lexer object is created. This method exists separately, because the PLY manual warns against calling lex.lex inside __init__ objectN)rlexer)rkwargsrrrbuild:sz CLexer.buildcCs d|j_dS)z? Resets the internal line number counter of the lexer. N)rlineno)rrrr reset_linenoDszCLexer.reset_linenocCs|jj|dS)N)rinput)rtextrrrrIsz CLexer.inputcCs|jj|_|jS)N)rtokenr )rrrrrLs z CLexer.tokencCs|jjjdd|j}|j|S)z3 Find the column of the token in its line.  r)rlexdatarfindlexpos)rrZlast_crrrrfind_tok_columnPszCLexer.find_tok_columncCs0|j|}|j||d|d|jjddS)Nrr)_make_tok_locationrrskip)rmsgrlocationrrr_error[s z CLexer._errorcCs|j|j|fS)N)rr")rrrrrr#`szCLexer._make_tok_location_BOOL_COMPLEXAUTOBREAKCASECHARCONSTCONTINUEDEFAULTDODOUBLEELSEENUMEXTERNFLOATFORGOTOIFINLINEINTLONGREGISTEROFFSETOFRESTRICTRETURNSHORTSIGNEDSIZEOFSTATICSTRUCTSWITCHTYPEDEFUNIONUNSIGNEDVOIDVOLATILEWHILEZ_BoolZ_ComplexIDTYPEID INT_CONST_DEC INT_CONST_OCT INT_CONST_HEX INT_CONST_BIN FLOAT_CONSTHEX_FLOAT_CONST CHAR_CONST WCHAR_CONSTSTRING_LITERALWSTRING_LITERALPLUSMINUSTIMESDIVIDEMODORANDNOTXORLSHIFTRSHIFTLORLANDLNOTLTLEGTGEEQNEEQUALS TIMESEQUALDIVEQUALMODEQUAL PLUSEQUAL MINUSEQUAL LSHIFTEQUAL RSHIFTEQUALANDEQUALXOREQUALOREQUALPLUSPLUS MINUSMINUSARROWCONDOPLPARENRPARENLBRACKETRBRACKETLBRACERBRACECOMMAPERIODSEMICOLONELLIPSISPPHASHz[a-zA-Z_$][0-9a-zA-Z_$]*z0[xX]z [0-9a-fA-F]+z0[bB]z[01]+zD(([uU]ll)|([uU]LL)|(ll[uU]?)|(LL[uU]?)|([uU][lL])|([lL][uU]?)|[uU])?z(0z)|([1-9][0-9]*)z0[0-7]*z 0[0-7]*[89]z([a-zA-Z._~!=&\^\-\\?'"])z(\d+)z(x[0-9a-fA-F]+)z#([\\][^a-zA-Z._~^!=&\^\-\\?'"x0-7])z(\\(|z))z ([^'\\\n]|'Lz('z*\n)|('z*$)z[^' ]+')|('')|('z [^'\n]*')z ([^"\\\n]|"z*"*z([eE][-+]?[0-9]+)z([0-9]*\.[0-9]+)|([0-9]+\.)z((((z ?)|([0-9]+z ))[FfLl]?)z([pP][+-]?[0-9]+)z(((z)?\.z)|(z\.))(z[FfLl]?)ppline exclusivepppragmacCsf|jj|jj|jjdr2|jjdd|_|_n0|jj|jj|jjdrX|jjdn d|_ |SdS)z[ \t]*\#)posrNrr) rmatchrrr!beginpp_line pp_filenamertype)rtrrrt_PPHASHs zCLexer.t_PPHASHcCs0|jdkr|jd|n|jjdjd|_dS)Nz$filename before line number in #liner)rr'valuelstriprstripr)rrrrrt_ppline_FILENAMEs zCLexer.t_ppline_FILENAMEcCs|jdkr|j|_ndS)N)rr)rrrrrt_ppline_LINE_NUMBER s  zCLexer.t_ppline_LINE_NUMBERcCsH|jdkr|jd|n t|j|j_|jdk r8|j|_|jjddS)z\nNzline number missing in #lineINITIAL)rr'intrrrr r)rrrrrt_ppline_NEWLINEs   zCLexer.t_ppline_NEWLINEcCsdS)lineNr)rrrrrt_ppline_PPLINE szCLexer.t_ppline_PPLINEz cCs|jd|dS)Nzinvalid #line directive)r')rrrrrt_ppline_error&szCLexer.t_ppline_errorcCs |jjd7_|jjddS)z\nrrN)rrr)rrrrrt_pppragma_NEWLINE,szCLexer.t_pppragma_NEWLINEcCsdS)ZpragmaNr)rrrrrt_pppragma_PPPRAGMA1szCLexer.t_pppragma_PPPRAGMAz$ <>.-{}();=+-*/$%@&^~!?:,0123456789cCsdS)Nr)rrrrrt_pppragma_STR7szCLexer.t_pppragma_STRcCsdS)Nr)rrrrr t_pppragma_ID:szCLexer.t_pppragma_IDcCs|jd|dS)Nzinvalid #pragma directive)r')rrrrrt_pppragma_error=szCLexer.t_pppragma_errorcCs|jj|jjd7_dS)z\n+rN)rrrcount)rrrrr t_NEWLINEFszCLexer.t_NEWLINEz\+-z\*/%z\|&~z\^z<>z\|\|z&&!<>z<=z>=z==z!==z\*=z/=z%=z\+=z-=z<<=z>>=z&=z\|=z\^=z\+\+z--z->z\?z\(z\)z\[z\],z\.;:z\.\.\.z\{cCs |j|S)N)r)rrrrrt_LBRACEszCLexer.t_LBRACEz\}cCs |j|S)N)r)rrrrrt_RBRACEszCLexer.t_RBRACEcCs|S)Nr)rrrrr t_FLOAT_CONSTszCLexer.t_FLOAT_CONSTcCs|S)Nr)rrrrrt_HEX_FLOAT_CONSTszCLexer.t_HEX_FLOAT_CONSTcCs|S)Nr)rrrrrt_INT_CONST_HEXszCLexer.t_INT_CONST_HEXcCs|S)Nr)rrrrrt_INT_CONST_BINszCLexer.t_INT_CONST_BINcCsd}|j||dS)NzInvalid octal constant)r')rrr%rrrt_BAD_CONST_OCTszCLexer.t_BAD_CONST_OCTcCs|S)Nr)rrrrrt_INT_CONST_OCTszCLexer.t_INT_CONST_OCTcCs|S)Nr)rrrrrt_INT_CONST_DECszCLexer.t_INT_CONST_DECcCs|S)Nr)rrrrr t_CHAR_CONSTszCLexer.t_CHAR_CONSTcCs|S)Nr)rrrrr t_WCHAR_CONSTszCLexer.t_WCHAR_CONSTcCsd}|j||dS)Nz Unmatched ')r')rrr%rrrt_UNMATCHED_QUOTEszCLexer.t_UNMATCHED_QUOTEcCsd|j}|j||dS)NzInvalid char constant %s)rr')rrr%rrrt_BAD_CHAR_CONSTs zCLexer.t_BAD_CHAR_CONSTcCs|S)Nr)rrrrrt_WSTRING_LITERALszCLexer.t_WSTRING_LITERALcCsd}|j||dS)Nz#String contains invalid escape code)r')rrr%rrrt_BAD_STRING_LITERALszCLexer.t_BAD_STRING_LITERALcCs2|jj|jd|_|jdkr.|j|jr.d|_|S)NrMrN) keyword_mapgetrrr )rrrrrt_IDsz CLexer.t_IDcCs"dt|jd}|j||dS)NzIllegal character %sr)reprrr')rrr%rrrt_errorszCLexer.t_errorN)%r(r)r*r+r,r-r.r/r0r1r2r3r4r5r6r7r8r9r:r;r<r=r>r?r@rArBrCrDrErFrGrHrIrJrKrL);rMrNrOrPrQrRrSrTrUrVrWrXrYrZr[r\r]r^r_r`rarbrcrdrerfrgrhrirjrkrlrmrnrorprqrrrsrtrurvrwrxryrzr{r|r}r~rrrrrrrrrrrrr)rr)__name__ __module__ __qualname____doc__rrrrrr"r'r#keywordsrkeywordlowertokensZ identifierZ hex_prefixZ hex_digitsZ bin_prefixZ bin_digitsZinteger_suffix_optZdecimal_constantZoctal_constantZ hex_constantZ bin_constantZbad_octal_constantZ simple_escapeZdecimal_escapeZ hex_escapeZ bad_escapeZescape_sequenceZ cconst_charZ char_constZ wchar_constZunmatched_quoteZbad_char_constZ string_charZstring_literalZwstring_literalZbad_string_literalZ exponent_partZfractional_constantZfloating_constantZbinary_exponent_partZhex_fractional_constantZhex_floating_constantZstatesrrrrrrZt_ppline_ignorerrrZt_pppragma_ignorerrrZt_ignorerZt_PLUSZt_MINUSZt_TIMESZt_DIVIDEZt_MODZt_ORZt_ANDZt_NOTZt_XORZt_LSHIFTZt_RSHIFTZt_LORZt_LANDZt_LNOTZt_LTZt_GTZt_LEZt_GEZt_EQZt_NEZt_EQUALSZ t_TIMESEQUALZ t_DIVEQUALZ t_MODEQUALZ t_PLUSEQUALZ t_MINUSEQUALZ t_LSHIFTEQUALZ t_RSHIFTEQUALZ t_ANDEQUALZ t_OREQUALZ t_XOREQUALZ t_PLUSPLUSZ t_MINUSMINUSZt_ARROWZt_CONDOPZt_LPARENZt_RPARENZ t_LBRACKETZ t_RBRACKETZt_COMMAZt_PERIODZt_SEMIZt_COLONZ t_ELLIPSISrrZt_STRING_LITERALrrrrrrrrrrrrrrrrrrrrs@!             $  r)r sysZplyrZply.lexrrrrrrr s