f _?Rc @sdZdZdZddlZddlZddlmZmZddlTddl m Z d d e e Dgd Z [ ze WneyzeZ Yn0d d ZddZddZddZdZdZeedeeeZdZdZdZdZeddZeeeeeZdZeddeeZd eZeeeZ ed!e d"Z!ee!e eZ"d#Z#d$Z$d%Z%d&Z&d'Z'ee'd(e'd)Z(ee'd*e'd+Z)ed,d-d.d/d0d1d2d3d4 Z*d5Z+ed6d7d8Z,ee*e+e,Z-ee"e-e)eZ.ee.Z/ee'd9ed:de'd;edd?d@ed=d>dAdBBhdCBZ9e4e#e4e$e7e8dDdEdFe9DdGdFe9DdHdFe9DZ:d(d)hdIdJe9DBdKdJe9DBZ;d:dZ?GdQdRdRe>Z@dSdTZAeAfdUdVZBdWdXZCGdYdZdZZDe4d[ejEZFe4d\ejEZGd]d^ZHd_d`ZIdadbZJdcddZKeLdekrddlMZMeNeMjOdkr|eBePeMjOdjQn eBeMjRjQdS)faTokenization 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)*)tokencCsg|]}|ddkr|qS)r_).0xrr//usr/lib64/python3.10/lib2to3/pgen2/tokenize.py %r )tokenizegenerate_tokens untokenizecGsdd|dS)Nz(z|z))joinZchoicesrrr group0r rcG t|dS)Nrrrrrr any1r rcGr)Nz?rrrrr maybe2r rcstfddDS)Nc3s4|],}dD]}||kr||VqqdS))N)Zcasefold)rr ZyZlrr Z 4sz _combinations..)Zsetrrrr _combinations3s 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[][(){}]z\r?\nz:=z[:;.,`@]z'[^\n'\\]*(?:\\.[^\n'\\]*)*'z"[^\n"\\]*(?:\\.[^\n"\\]*)*"ZrZRZfZFZbZB>ZUZUrZurZURZuZuR)rrrrcCi|]}|dtqSr) single3progrZprefixrrr yr r"cCrr) double3progr!rrr r"zr cCsi|] }|dqSNrr!rrr r"{r cCh|]}|dqSrrr!rrr r r'cCr&r#rr!rrr r'r cCr&)rrr!rrr r'r cCr&)rrr!rrr r'r ic@ eZdZdS) TokenErrorN__name__ __module__ __qualname__rrrr r)r r)c@r()StopTokenizingNr*rrrr r.r r.c Cs4|\}}|\}}td||||t|t|fdS)Nz%d,%d-%d,%d: %s %s)ZprintZtok_nameZrepr) ZtyperZxxx_todo_changemeZxxx_todo_changeme1lineZsrowZscolZerowZecolrrr printtokens r0cCs&zt||Wnty Yn0dS)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_loopr.)readline tokeneaterrrr r s  r cCst|D] }||qdSr%)r)r2r3Z token_inforrr r1s r1c@s,eZdZddZddZddZddZd S) UntokenizercCsg|_d|_d|_dS)Nrr)tokensprev_rowprev_col)selfrrr __init__szUntokenizer.__init__cCs*|\}}||j}|r&|jd|dS)N )r7r5append)r8startZrowZcolZ col_offsetrrr add_whitespaces zUntokenizer.add_whitespacecCs|D]p}t|dkr$|||qv|\}}}}}|||j||\|_|_|ttfvr|jd7_d|_qd |jS)Nrrr) lencompatr=r5r;r6r7NEWLINENLr)r8iterableZtZtok_typerr<endr/rrr rs      zUntokenizer.untokenizec Csd}g}|jj}|\}}|ttfvr,|d7}|ttfvr|t kr| q@n*|ttfvrd}n|r|r||dd}||q@dS)NFr:Tr>) r5r;NAMENUMBERrArBASYNCAWAITINDENTDEDENTZpop) r8rrCZ startlineindentsZ toks_appendZtoknumZtokvaltokrrr r@s0     zUntokenizer.compatN)r+r,r-r9r=rr@rrrr r4sr4z&^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)s^[ \t\f]*(?:[#\r\n]|$)cCsH|dddd}|dks*|dr.dS|dvs@|drDd S|S) z(Imitates get_normal_name in tokenizer.c.Ni rz-utf-8zutf-8-)zlatin-1 iso-8859-1z iso-latin-1)zlatin-1-z iso-8859-1-z iso-latin-1-rO)ZlowerZreplace startswith)Zorig_encZencrrr _get_normal_namesrQcsdd}d}fdd}fdd}|}|trHd|d d}d }|sT|gfS||}|rj||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. FNrNcs"zWSty tYS0r%) StopIterationbytesr)r2rr read_or_stops z%detect_encoding..read_or_stopcsz|d}Wnty"YdS0t|}|s6dSt|d}z t|}Wntyntd|Yn0r|j dkrtd|d7}|S)NZasciirzunknown encoding: rNzencoding problem: utf-8z-sig) ZdecodeZUnicodeDecodeError cookie_rematchrQrrZ LookupErrorZ SyntaxErrorZname)r/Z line_stringrVencodingZcodec) bom_foundrr find_cookie s"     z$detect_encoding..find_cookieTz utf-8-sig)rPrblank_rerV)r2rWZdefaultrTrYZfirstZsecondr)rXr2r detect_encodings0         r\cCst}||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 )r4r)rCZutrrr r:srccs|d}}}d\}}d}dg}d}d} d} d} z |} WntyNd} Yn0|d}dt| } }|r4| sztd||| }|r|d} }t|| d||||f|| fVd\}}d}n|r| ddd kr| d dd krt|| ||t| f|fVd}d}q.nX|| }|| }q.nB|dkr^|s^| sNq$d}| |kr| | d krt|d}n8| | d kr|tdt}n| | dkrd}nq| d} qR| |krƐq$|r|Vd}| | dvr| | dkrV| | dd}| t|}t ||| f|| t|f| fVt | |d||f|t| f| fVq.t t f| | dk| | d|| f|t| f| fVq.||dkr| |t | d| |df|| f| fV||dkr6||vrt dd|| | f|dd}| r| |dkrd} d} d} td|| f|| f| fVq| rv| rv| |dkrvd} d} d} n| srtd|dfd}| |kr.t| | }|r|d\}}||f||f|}}} | ||| |}}|tjvs|dkr|dkrt|||| fVn|dvrLt}|dkrt }n | r(d} |r8|Vd}||||| fVn|dkrz|rf|Vd}t |||| fVn|tvrt|}|| | }|r|d} | || }|r|Vd}t|||| f| fVn||f}| |d}| }q.n$|tvs(|ddtvs(|ddtvr|ddkr~||f}t|p`t|dp`t|d}| |dd}}| }q.nt|r|Vd}t|||| fVn||rx|dvr| r|dkrtnt|||| fVqvt|||| f}|dkr|s|}qv|dkr`|r`|dtkr`|ddkr`d} |d} t|d|d|d|dfVd}|rp|Vd}|Vn|dkr|r|Vd}t |||| f| fVd}nr|d vr|d}n|d!vr|d}|r|Vd}t|||| fVqvt| | || f|| df| fV| d} qvq.|r4|Vd}|ddD]}td|df|dfdfVq@td|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)rrNFrrzEOF in multi-line stringiz\ iz\ r:z z z# z#z rEz3unindent does not match any outer indentation levelz zEOF in multi-line statementZ.Tr>rZZ )asyncZawaitr]Zdefiz\z([{z)]})rRr?r)rVrDZSTRINGZ ERRORTOKENtabsizeZrstripZCOMMENTrBr;rJZIndentationErrorrK pseudoprogZspanstringZdigitsrGrA triple_quotedendprogs single_quotedZ isidentifierrHrIrFZOPZ ENDMARKER)r2ZlnumZparenlevZ continuedZcontstrZneedcontZcontlinerLZstashedZ async_defZasync_def_indentZ async_def_nlr/ZposZmaxZstrstartZendprogZendmatchrDZcolumnZ comment_tokenZnl_posZ pseudomatchr<ZsposZeposrZinitialZnewlinerMZindentrrr rOs      *                              rZ__main__)SZ__doc__Z __author__Z __credits__r`ZreZcodecsrrZlib2to3.pgen2.tokenrrZdirZ__all__rSZ NameErrorZstrrrrrZ WhitespaceZCommentZIgnoreZNameZ BinnumberZ HexnumberZ OctnumberZ DecnumberZ IntnumberZExponentZ PointfloatZExpfloatZ FloatnumberZ ImagnumberZNumberZSingleZDoubleZSingle3ZDouble3Z _litprefixZTripleZStringZOperatorZBracketZSpecialZFunnyZ PlainTokenZTokenZContStrZ PseudoExtrasZ PseudoTokenZmapZcompileZ tokenprogr_r r$Z _strprefixesrbrarcr^Z Exceptionr)r.r0r r1r4ZASCIIrUr[rQr\rrr+Zsysr?ZargvZopenr2Zstdinrrrr Zs                     8 Ib &