o bcR @sdZdZdZddlZddlZddlmZmZddlTddl m Z d d e e Dgd Z [ ze Wn ey=eZ Ynwd 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`ZIdadbZJdcddZKeLdekrddlMZMeNeMjOdkreBePeMjOdjQdSeBeMjRjQdSdS)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/lib/python3.10/lib2to3/pgen2/tokenize.py %sr )tokenizegenerate_tokens untokenizecGsdd|dS)N(|))joinchoicesrrr group0rcG t|dS)Nrrrrrr any1 rcGr)N?rrrrr maybe2rrcstfddDS)Nc3s6|]}dD]}||kr||VqqdS))N)casefold)r r ylrr 4s  z _combinations..)setr!rr!r _combinations3s r%z[ \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'cCi|]}|dtqSr&) single3progr prefixrrr yr<cCr7r') double3progr:rrr r<zr=cCsi|]}|dqSNrr:rrr r<{rcCh|]}|dqSr8rr:rrr rBcCrAr>rr:rrr rBrCcCrA)r)rr:rrr rBrCcCrA)r*rr:rrr rBrCc@ eZdZdS) TokenErrorN__name__ __module__ __qualname__rrrr rFrrFc@rE)StopTokenizingNrGrrrr rKrrKc Cs4|\}}|\}}td||||t|t|fdS)Nz%d,%d-%d,%d: %s %s)printtok_namerepr) typerxxx_todo_changemexxx_todo_changeme1linesrowscolerowecolrrr printtokens  rWcCs&zt||WdStyYdSw)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_looprK)readline tokeneaterrrr r s   r cCst|D]}||qdSr@)r)rYrZ token_inforrr rXs  rXc@s,eZdZddZddZddZddZd S) UntokenizercCsg|_d|_d|_dS)Nrr)tokensprev_rowprev_col)selfrrr __init__s zUntokenizer.__init__cCs<|\}}||jks J||j}|r|jd|dSdS)N )r^r_r]append)r`startrowcol col_offsetrrr add_whitespaces  zUntokenizer.add_whitespacecCs|D]8}t|dkr|||n)|\}}}}}|||j||\|_|_|ttfvr:|jd7_d|_qd |jS)Nrrr) lencompatrhr]rcr^r_NEWLINENLr)r`iterablettok_typerrdendrRrrr rs       zUntokenizer.untokenizec Csd}g}|jj}|\}}|ttfvr|d7}|ttfvrd}|D]B}|dd\}}|ttttfvr6|d7}|tkr@||q |t krI| q |ttfvrRd}n |r^|r^||dd}||q dS)NFrbTri) r]rcNAMENUMBERrlrmASYNCAWAITINDENTDEDENTpop) r`rrn startlineindents toks_appendtoknumtokvaltokrrr rks2      zUntokenizer.compatN)rHrIrJrarhrrkrrrr r\s  r\z&^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)s^[ \t\f]*(?:[#\r\n]|$)cCsH|dddd}|dks|drdS|dvs |dr"d 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_encencrrr _get_normal_namesrcsdd}d}fdd}fdd}|}|tr$d|d d}d }|s*|gfS||}|r5||gfSt|s?||gfS|}|sI||gfS||}|rU|||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. FNrcs"zWStytYSwr@) StopIterationbytesr)rYrr read_or_stops   z%detect_encoding..read_or_stopcsz|d}Wn tyYdSwt|}|sdSt|d}zt|}Wn ty5td|wrE|j dkrAtd|d7}|S)Nasciirzunknown encoding: rzencoding problem: utf-8z-sig) decodeUnicodeDecodeError cookie_rematchrrr LookupError SyntaxErrorname)rR line_stringrencodingcodec) bom_foundrr find_cookie s&      z$detect_encoding..find_cookieTz utf-8-sig)rrblank_rer)rYrdefaultrrfirstsecondr)rrYr detect_encodings0          rcCst}||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 )r\r)rnutrrr r:s rccsd}}}d\}}d}dg}d}d} d} d} z|} Wn ty)d} Ynw|d}dt| } }|r| s>td||| }|rf|d} }t|| d||||f|| fVd\}}d}nN|r| ddd kr| d dd krt|| ||t| f|fVd}d}q|| }|| }q|dkr|s| snd}| |kr| | d kr|d}n| | d kr|tdt}n | | dkrd}nn| d} | |ks| |krؐn|r|Vd}| | dvr:| | dkr| | dd}| t|}t ||| f|| t|f| fVt | |d||f|t| f| fVnt t f| | dk| | d|| f|t| f| fVq||dkrV| |t | d| |df|| f| fV||dkr||vrkt dd|| | f|dd}| r| |dkrd} d} d} td|| f|| f| fV||dks]| r| r| |dkrd} d} d} n | std|dfd}| |krt| | }|r}|d\}}||f||f|}}} | ||| |}}|tjvs|dkr|dkrt|||| fVn|dvr t}|dkr t }n| rd} |r|Vd}||||| fVnq|dkr?|dr-J|r5|Vd}t |||| fVnR|tvr~t|}|| | }|rp|d} | || }|rd|Vd}t|||| f| fVn!||f}| |d}| }n|tvs|ddtvs|ddtvr|ddkr||f}t|pt|dpt|d}| |dd}}| }n|r|Vd}t|||| fVn|r?|dvr| r|dkrtnt|||| fVqt|||| f}|dkr|s|}q|dvr3|r3|dtkr3|ddkr3|dkr!d} |d} t|d|d|d|dfVd}|r;|Vd}|VnR|d krY|rL|Vd}t |||| f| fVd}n8|d!vrc|d}n |d"vrl|d}|rt|Vd}t|||| fVnt| | || f|| df| fV| d} | |ksq|r|Vd}|ddD]}td|df|dfdfVqtd|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 stringz\ z\ rb  z# #z rrz3unindent does not match any outer indentation levelz zEOF in multi-line statement.T rir)asyncawaitr)defforr\z([{z)]})rrjrFrrqSTRING ERRORTOKENtabsizerstripCOMMENTrmrcrwIndentationErrorrx pseudoprogspanstringdigitsrtrlendswith triple_quotedendprogs single_quoted isidentifierrurvrsOP ENDMARKER)rYlnumparenlev continuedcontstrneedcontcontliner{stashed async_defasync_def_indent async_def_nlrRposmaxstrstartendprogendmatchrqcolumn comment_tokennl_pos pseudomatchrdsposeposrinitialnewlinerindentrrr rOs     $                            @r__main__)S__doc__ __author__ __credits__rrecodecsrrlib2to3.pgen2.tokenrrdir__all__r NameErrorstrrrrr% WhitespaceCommentIgnoreName Binnumber Hexnumber Octnumber Decnumber IntnumberExponent PointfloatExpfloat Floatnumber ImagnumberNumberSingleDoubleSingle3Double3 _litprefixTripleStringOperatorBracketSpecialFunny PlainTokenTokenContStr PseudoExtras PseudoTokenmapcompile tokenprogrr9r? _strprefixesrrrr ExceptionrFrKrWr rXr\ASCIIrrrrrrrHsysrjargvopenrYstdinrrrr s                   8 I c(