\dR PdZdZdZddlZddlZddlmZmZddlTddl m Z d e e Dgd zZ [ e n #e$reZ YnwxYwd Zd Zd ZdZdZdZeedezzeezZdZdZdZdZeddZeeeeeZdZeddeezZdezZeeeZ ede dzZ!ee!e eZ"dZ#dZ$d Z%d!Z&d"Z'ee'd#ze'd$zZ(ee'd%ze'd&zZ)ed'd(d)d*d+d,d-d.d/ Z*d0Z+ed1d2d3Z,ee*e+e,Z-ee"e-e)eZ.ee.zZ/ee'd4zed5dze'd6zed7dzZ0edee(Z1eee1e"e-e0ezZ2e3ej4e/e2e%e&f\Z5Z6Z7Z8ed8d9d:d;ed8d9dzZ9ej4e#ej4e$e7e8d?d@e9DdAe9DdBe9DZ:d#d$hdCe9DzdDe9DzZ;d5d7hdEe9DzdFe9DzZZ?GdJdKe>Z@dLZAeAfdMZBdNZCGdOdPZDej4dQejEZFej4dRejEZGdSZHdTZIdUZJdVZKeLdWkrUddlMZMeNeMjOdkr&eBePeMjOdjQdSeBeMjRjQdSdS)XaTokenization help for Python programs. generate_tokens(readline) is a generator that breaks a stream of text into Python tokens. It accepts a readline-like method which is called repeatedly to get the next line of input (or "" for EOF). It generates 5-tuples with these members: the token type (see token.py) the token (a string) the starting (row, column) indices of the token (a 2-tuple of ints) the ending (row, column) indices of the token (a 2-tuple of ints) the original line (string) It is designed to match the working of the Python tokenizer exactly, except that it produces COMMENT tokens for comments and gives type OP for all operators Older entry points tokenize_loop(readline, tokeneater) tokenize(readline, tokeneater=printtoken) are the same, except instead of generating tokens, tokeneater is a callback function to which the 5 fields described above are passed as 5 arguments, each time a new token is found.zKa-Ping Yee z@GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip MontanaroN)BOM_UTF8lookup)*)tokenc*g|]}|ddk|S)r_).0xs '..\python\lib\lib2to3\pgen2\tokenize.py r%s! 0 0 0AaDCKK1KKK)tokenizegenerate_tokens untokenizec8dd|zdzS)N(|))joinchoicess r groupr0sC#((7"3"33c99rct|dzS)Nrrrs r anyr1s%/C//rct|dzS)N?rrs r mayber 2sE7Oc11rc:tfdDS)Nc3K|];}dzD]3}||k,||zV4z _combinations..4s`!e)qzz||qzz||/K/KA/K/K/K/K/Kr)set)r&s`r _combinationsr)3s;   rz[ \f\t]*z #[^\r\n]*z\\\r?\nz\w+z0[bB]_?[01]+(?:_[01]+)*z(0[xX]_?[\da-fA-F]+(?:_[\da-fA-F]+)*[lL]?z0[oO]?_?[0-7]+(?:_[0-7]+)*[lL]?z[1-9]\d*(?:_\d+)*[lL]?z0[lL]?z[eE][-+]?\d+(?:_\d+)*z\d+(?:_\d+)*\.(?:\d+(?:_\d+)*)?z\.\d+(?:_\d+)*z \d+(?:_\d+)*z\d+(?:_\d+)*[jJ]z[jJ]z[^'\\]*(?:\\.[^'\\]*)*'z[^"\\]*(?:\\.[^"\\]*)*"z%[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''z%[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""z'(?:[uUrRbBfF]|[rR][fFbB]|[fFbBuU][rR])?'''"""z'[^\n'\\]*(?:\\.[^\n'\\]*)*'z"[^\n"\\]*(?:\\.[^\n"\\]*)*"z\*\*=?z>>=?z<<=?z<>z!=z//=?z->z[+\-*/%&@|^=<>]=?~z[][(){}]z\r?\nz:=z[:;.,`@]z'[^\n'\\]*(?:\\.[^\n'\\]*)*'z"[^\n"\\]*(?:\\.[^\n"\\]*)*"rRfFbB>UuURUruRur)r-r.r*r+c$i|] }|dtSr*) single3progr prefixs r r@y FFFv&~~~{FFFrc$i|] }|dtSr+) double3progr>s r r@r@zrArci|]}|dSNr r>s r r@r@{s777vt777rch|]}|dSr<r r>s r rH///^^^///rch|]}|dSrCr r>s r rHrHrIrch|]}|dS)r-r r>s r rHrH---f\\\---rch|]}|dS)r.r r>s r rHrHrLrceZdZdS) TokenErrorN__name__ __module__ __qualname__r rr rPrPrrPceZdZdS)StopTokenizingNrQr rr rWrWrUrrWc z|\}}|\}}td||||t|t|fzdS)Nz%d,%d-%d,%d: %s %s)printtok_namerepr) typerxxx_todo_changemexxx_todo_changeme1linesrowscolerowecols r printtokenrdsR$LT4%LT4 tT4$e= >?????rcJ t||dS#t$rYdSwxYw)a: The tokenize() function accepts two parameters: one representing the input stream, and one providing an output mechanism for tokenize(). The first parameter, readline, must be a callable object which provides the same interface as the readline() method of built-in file objects. Each call to the function should return one line of input as a string. The second parameter, tokeneater, must also be a callable object. It is called once for each token, with five arguments, corresponding to the tuples generated by generate_tokens(). N) tokenize_looprW)readline tokeneaters r rrs? h +++++      s  ""c4t|D]}||dSrF)r)rgrh token_infos r rfrfs3%h//   J  rc&eZdZdZdZdZdZdS) Untokenizerc0g|_d|_d|_dS)Nrr)tokensprev_rowprev_col)selfs r __init__zUntokenizer.__init__s   rc|\}}||jksJ||jz }|r|jd|zdSdS)N )rorprnappend)rqstartrowcol col_offsets r add_whitespacezUntokenizer.add_whitespaces]Sdm####4=(  1 K  sZ/ 0 0 0 0 0 1 1rcp|D]}t|dkr|||nn|\}}}}}|||j||\|_|_|ttfvr|xjdz c_d|_d |jS)Nrrr#) lencompatrzrnrurorpNEWLINENLr)rqiterablettok_typerrvendr_s r rzUntokenizer.untokenizes " "A1vv{{ Ax(((01 -HeUC    & & & K  u % % %+. (DM4=GR=(( " ! wwt{###rcd}g}|jj}|\}}|ttfvr|dz }|tt fvrd}|D]}|dd\}}|ttt tfvr|dz }|tkr||Q|tkr| q|tt fvrd}n|r|r||dd}||dS)NFrtTr|) rnruNAMENUMBERrrASYNCAWAITINDENTDEDENTpop) rqrr startlineindents toks_appendtoknumtokvaltoks r r~zUntokenizer.compats k(  dF^ # # cMF gr] " "I  C !WNFF$u555# v&&&6!! GR=((  "w " GBK(((! K    #  rN)rRrSrTrrrzrr~r rr rlrlsP 111 $ $ $     rrlz&^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)s^[ \t\f]*(?:[#\r\n]|$)c|dddd}|dks|drdS|dvs|drd S|S) z(Imitates get_normal_name in tokenizer.c.N r -utf-8zutf-8-)zlatin-1 iso-8859-1z iso-latin-1)zlatin-1-z iso-8859-1-z iso-latin-1-r)lowerreplace startswith)orig_encencs r _get_normal_namersv 3B3-     ' 'S 1 1C g~~11~w 666 ~~ABB7| OrcLdd}d}fd}fd}|}|trd|dd}d}|s|gfS||}|r||gfSt|s||gfS|}|s||gfS||}|r|||gfS|||gfS) a The detect_encoding() function is used to detect the encoding that should be used to decode a Python source file. It requires one argument, readline, in the same way as the tokenize() generator. It will call readline a maximum of twice, and return the encoding used (as a string) and a list of any lines (left as bytes) it has read in. It detects the encoding from the presence of a utf-8 bom or an encoding cookie as specified in pep-0263. If both a bom and a cookie are present, but disagree, a SyntaxError will be raised. If the encoding cookie is an invalid charset, raise a SyntaxError. Note that if a utf-8 bom is found, 'utf-8-sig' is returned. If no encoding is specified, then the default of 'utf-8' will be returned. FNrcV S#t$rtcYSwxYwrF) StopIterationbytes)rgsr read_or_stopz%detect_encoding..read_or_stops< 8::    77NNN s ((c| |d}n#t$rYdSwxYwt|}|sdSt |d} t |}n #t$rtd|zwxYwr|j dkrtd|dz }|S)Nasciirzunknown encoding: rzencoding problem: utf-8z-sig) decodeUnicodeDecodeError cookie_rematchrrr LookupError SyntaxErrorname)r_ line_stringrencodingcodec bom_founds r find_cookiez$detect_encoding..find_cookie s ++g..KK!   44  ,, 4#EKKNN33 ?8$$EE ? ? ?2X=>> > ?  zW$$!";<<<  Hs ''+A;;BTz utf-8-sig)rrblank_rer)rgrdefaultrrfirstsecondrs` @r detect_encodingrs-$IHG , LNNE !! abb  {{5!!H!%  >>%   \^^F  {6""H)%(( UFO ##rcHt}||S)aTransform tokens back into Python source code. Each element returned by the iterable must be a token sequence with at least two elements, a token number and token value. If only two tokens are passed, the resulting output is poor. Round-trip invariant for full input: Untokenized source will match input source exactly Round-trip invariant for limited input: # Output text will tokenize the back to the input t1 = [tok[:2] for tok in generate_tokens(f.readline)] newcode = untokenize(t1) readline = iter(newcode.splitlines(1)).next t2 = [tok[:2] for tokin generate_tokens(readline)] assert t1 == t2 )rlr)ruts r rr:s$ B == " ""rc# Kdx}x}}d\}}d}dg}d}d} d} d} |} n#t$rd} YnwxYw|dz}dt| }} |r| std||| }|r>|dx} }t || d|z|||f|| zfVd\}}d}nZ|rA| ddd kr3| d dd kr%t || z||t| f|fVd}d}|| z}|| z}|dkr|s| snd}| |krO| | d kr|dz}n2| | d kr|tzdztz}n| | dkrd}nn | dz} | |kO| |krn|r|Vd}| | dvr| | dkry| | dd}| t|z}t||| f|| t|zf| fVt| |d||f|t| f| fVn>ttf| | dk| | d|| f|t| f| fV0||dkr/| |t| d| |df|| f| fV||dkrT||vrtdd|| | f|dd}| r| |dkrd} d} d} td|| f|| f| fV||dkT| r| r| |dkrd} d} d} n| std|dfd}| |krt| | }|r|d\}}||f||f|} }}| ||| |}}|t"jvs |dkr|dkrt&|||| fVn|dvr,t(}|dkrt}n| rd} |r|Vd}||||| fVna|dkr*|jdrJ|r|Vd}t|||| fVn1|t,vrpt.|}|| | }|r9|d} | || }|r|Vd}t |||| f| fVn||f}| |d}| }n|t0vs"|ddt0vs|ddt0vrk|ddkrG||f}t.|p%t.|dpt.|d}| |dd}}| }nA|r|Vd}t |||| fVn"|r|dvr| r|dkrt4nt6|||| fV)t8|||| f}|dkr|s|}A|dvrW|rU|dt8krD|ddkr8|dkr d} |d} t4|d|d|d|dfVd}|r|Vd}|Vnk|d kr|r|Vd}t|||| f| fVd}nJ|d!vr|dz}n |d"vr|dz }|r|Vd}t:|||| fVn t | | || f|| dzf| fV| dz} | |k|r|Vd}|ddD]}td|df|dfdfVt<d|df|dfdfVdS)#a4 The generate_tokens() generator requires one argument, readline, which must be a callable object which provides the same interface as the readline() method of built-in file objects. Each call to the function should return one line of input as a string. Alternately, readline can be a callable function terminating with StopIteration: readline = open(myfile).next # Example of alternate readline The generator produces 5-tuples with these members: the token type; the token string; a 2-tuple (srow, scol) of ints specifying the row and column where the token begins in the source; a 2-tuple (erow, ecol) of ints specifying the row and column where the token ends in the source; and the line on which the token was found. The line passed is the physical line. r)r#rNFrr#zEOF in multi-line stringz\ z\ rt  z# #z rz3unindent does not match any outer indentation levelz zEOF in multi-line statement.T r|r)asyncawaitr)defforr\z([{z)]})rr}rPrrSTRING ERRORTOKENtabsizerstripCOMMENTrrurIndentationErrorr pseudoprogspanstringdigitsrrendswith triple_quotedendprogs single_quoted isidentifierrrrOP ENDMARKER)rglnumparenlev continuedcontstrneedcontcontlinerstashed async_defasync_def_indent async_def_nlr_posmaxstrstartendprogendmatchrcolumn comment_tokennl_pos pseudomatchrvsposeposrinitialnewlinerindents r rrOs #$#D#8iGXHcGGIL} 8::DD   DDD axc$iiS J  G !;XFFF}}T**H $LLOO+cwdsd3$ho????$)! d233i611d233i86K6K!7T>#dCII%6BBBB!D.#d? ]]9] F))9##fqjVV#Y$&&&'/A2Ew1N#Y$&&Ag )) czz5  CyG##9##$(J$5$5f$=$=M 3}#5#55F"M #;sS5G5G/G(H$PPPPtFGG} &>D#d))+'"+.M.M! $ #$  K !>q JJJICii$**455Kg (--a00 s#'-$cCd!%eCi$u+wfm++sNNu||!5$d;;;;;&&%G!||"$",'+ '% "&"E4t<<<<<^^-u~d33333'% "&"E4t<<<<<m++&uoG&}}T377H &ll1oo $U3Y"+")MMM&*G%udT3KFFFFF$(%="&uvv,#' --"1"I.."1"I..RyD(($(%=#+G#4$6q8J$6#+E!H#5 ,0L!#'"+")MMM&*G%udD$?????))++,8 222$%,1W,<,<55%#($d#<<<<$dD9C'''"% ..# +$+AJ$$6$6$+AJ'$9$9$~~,0 3:2; 0#('!*#*1:wqz#*1:#////'+G'% "&IIII__'% "&udT3K>>>> !II%''HqL E))hl8'% "&udD$77777!49 #;s1u t====AgSCiii}~ !""+55rD!9tQi44444 b4)dAY 333333s ( 77__main__)S__doc__ __author__ __credits__rrecodecsrrlib2to3.pgen2.tokenr#rdir__all__r NameErrorstrrrr r) WhitespaceCommentIgnoreName Binnumber Hexnumber Octnumber Decnumber IntnumberExponent PointfloatExpfloat Floatnumber ImagnumberNumberSingleDoubleSingle3Double3 _litprefixTripleStringOperatorBracketSpecialFunny PlainTokenTokenContStr PseudoExtras PseudoTokenmapcompile tokenprogrr=rD _strprefixesrrrr ExceptionrPrWrdrrfrlASCIIrrrrrrrRsysr}argvopenrgstdinr rr r%s##0* F ########!!!! 0 0cc%jj 0 0 04,4,4, ,  EE EEE :99///111   cc*z122 2UU7^^ C  & 7 . E+X 6 6 E)Y 9 = = # U57H I IEERZOO [ X %eJ)) U& g(= > > z; 2 2 $ # 2 2 7 zE!:#5 6 6 z;;;; = = 5GWeU%%    %% - - h)) U65&$ / /  % ;;c:&&';;c:&&' ( (uZ&11 55vugtLLL 25#J Wg63838/ :{KM#sC%%M#sC%%&&&&' F##*"*V*<*<{ 9 9FFFFF 9GFFFF 987,777  9 EN//,///0//,///0  #J-- ---.-- ---. !!!!!!!!%%%%%Y%%%??? #-    &   6 6 6 6 6 6 6 6 p BJ@"( K K 2:0"( ; ;   G$G$G$R###*`4`4`4D zJJJ s38}}q((44 #4#4#=>>>>> (39% & & & & &sAA  A