o Ƌ|cAe@sTdZdZdZddlmZddlmZmZddl Z ddl Z ddl m Z ddl ZddlZddlZddlTdd lmZed ejZed ejZddlZejgd Z[Gd dde ddZddZddZddZdZdZeedeeeZdZ dZ!dZ"dZ#dZ$ee!e"e#e$Z%dZ&edd ee&Z'd!e&Z(ee'e(Z)ed"e)d#Z*ee*e)e%Z+d$d%Z,e j-d&d'Z.ee,Z/d(Z0d)Z1d*Z2d+Z3ee/d,e/d-Z4ee/d.e/d/Z5ee6ej7e8ed0d1Z9ed2e9Z:ee+e:e5e Z;ee;Zeee>e+e:e=e Z?iZ@e,D]ZAe0e@eAd4<e1e@eAd6<e2e@eAd,<e3e@eAd-<qeBZCeBZDe,D]%ZEeEd6eEd4fD]ZFeCGeFqIeEd-eEd,fD]ZFeDGeFqZq?d8ZHGd9d:d:eIZJGd;d<dd>ZLd?d@ZMdAdBZNdCdDZOdEdFZdGdHZPdIdJZQdKdLZRdMdNZSeTdOkreSdSdS)PaoTokenization help for Python programs. tokenize(readline) is a generator that breaks a stream of bytes into Python tokens. It decodes the bytes according to PEP-0263 for determining source file encoding. It accepts a readline-like method which is called repeatedly to get the next line of input (or b"" 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. Additionally, all token lists start with an ENCODING token which tells you which encoding was used to decode the bytes stream. zKa-Ping Yee zpGvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro, Raymond Hettinger, Trent Nelson, Michael Foord)open)lookupBOM_UTF8N) TextIOWrapper)*)EXACT_TOKEN_TYPESz&^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)s^[ \t\f]*(?:[#\r\n]|$))tokenizegenerate_tokensdetect_encoding untokenize TokenInfoc@s eZdZddZeddZdS)r cCs$d|jt|jf}d|j|dS)Nz%d (%s)z8TokenInfo(type=%s, string=%r, start=%r, end=%r, line=%r))type)r tok_name_replace)selfZannotated_typer%/opt/conda/lib/python3.10/tokenize.py__repr__/s zTokenInfo.__repr__cCs$|jtkr|jtvrt|jS|jSN)r OPstringrrrrr exact_type4s zTokenInfo.exact_typeN)__name__ __module__ __qualname__rpropertyrrrrrr .sr ztype string start end linecGsdd|dS)N(|))joinchoicesrrrgroup;sr#cG t|dS)Nrr#r!rrrany< r&cGr$)N?r%r!rrrmaybe=r'r)z[ \f\t]*z #[^\r\n]*z\\\r?\nz\w+z0[xX](?:_?[0-9a-fA-F])+z0[bB](?:_?[01])+z0[oO](?:_?[0-7])+z(?:0(?:_?0)*|[1-9](?:_?[0-9])*)z[eE][-+]?[0-9](?:_?[0-9])*z)[0-9](?:_?[0-9])*\.(?:[0-9](?:_?[0-9])*)?z\.[0-9](?:_?[0-9])*z[0-9](?:_?[0-9])*z[0-9](?:_?[0-9])*[jJ]z[jJ]cCsVgd}dh}|D]}t|D]}tjdd|DD] }|d|qqq |S)N)brufZbrZfrcSsg|]}||fqSr)upper).0crrr _sz(_all_string_prefixes..) _itertools permutationsproductaddr )Z_valid_string_prefixesresultprefixtr,rrr_all_string_prefixesTsr:cCst|tjSr)recompileUNICODE)Zexprrrr_compilecsr>z[^'\\]*(?:\\.[^'\\]*)*'z[^"\\]*(?:\\.[^"\\]*)*"z%[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''z%[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""z'''z"""z'[^\n'\\]*(?:\\.[^\n'\\]*)*'z"[^\n"\\]*(?:\\.[^\n"\\]*)*"T)reversez\r?\nz'[^\n'\\]*(?:\\.[^\n'\\]*)*'z"[^\n"\\]*(?:\\.[^\n"\\]*)*"z \\\r?\n|\Zc@ eZdZdS) TokenErrorNrrrrrrrrDr'rDc@rC)StopTokenizingNrErrrrrFr'rFc@s,eZdZddZddZddZddZd S) UntokenizercCsg|_d|_d|_d|_dS)Nr)tokensprev_rowprev_colencodingrrrr__init__s zUntokenizer.__init__cCs|\}}||jks||jkr||jkrtd|||j|j||j}|r1|jd|d|_||j}|rB|jd|dSdS)Nz+start ({},{}) precedes previous end ({},{})\ r )rJrK ValueErrorformatrIappend)rstartrowZcolZ row_offset col_offsetrrradd_whitespaces  zUntokenizer.add_whitespacec Cs,t|}g}d}|D]}t|dkr|||nv|\}}}} } |tkr)||_q |tkr/na|tkr9||q |tkrH| | \|_ |_ q |t t fvrQd}n|rn|rn|d} |dt| krl|j| t| |_ d}|||j|| \|_ |_ |t t fvr|j d7_ d|_ q d|jS)NFTrHrr.)iterlencompatENCODINGrL ENDMARKERINDENTrRDEDENTpoprJrKNEWLINENLrIrVr ) riterableitindents startliner9Ztok_typetokenrSendlineindentrrrr sH            zUntokenizer.untokenizec Csg}|jj}|dttfv}d}t|g|D]W}|dd\}} |tkr)| |_q|tt fvr3| d7} |t kr@|r=d| } d}nd}|t krL|| q|t krU| q|ttfvr^d}n |rj|rj||dd}|| qdS)NrFrWrOTrX)rIrRrarbr3chainr\rLNAMENUMBERSTRINGr^r_r`) rrgrcreZ toks_appendrfZ prevstringZtokZtoknumZtokvalrrrr[s:     zUntokenizer.compatN)rrrrMrVr r[rrrrrGs  %rGcCs*t}||}|jdur||j}|S)aTransform tokens back into Python source code. It returns a bytes object, encoded using the ENCODING token, which is the first token sequence output by tokenize. 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 bytes will tokenize back to the input t1 = [tok[:2] for tok in tokenize(f.readline)] newcode = untokenize(t1) readline = BytesIO(newcode).readline t2 = [tok[:2] for tok in tokenize(readline)] assert t1 == t2 N)rGr rLencode)rcZutoutrrrr s    r cCsH|dddd}|dks|drdS|dvs |dr"d S|S) z(Imitates get_normal_name in tokenizer.c.N _-utf-8zutf-8-)zlatin-1 iso-8859-1z iso-latin-1)zlatin-1-z iso-8859-1-z iso-latin-1-ru)lowerreplace startswith)Zorig_encZencrrr_get_normal_namesrycszjjWn tydYnwdd}d}fdd}fdd}|}|tr7d|d d}d }|s=|gfS||}|rH||gfSt|sR||gfS|}|s\||gfS||}|rh|||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. NFrtcszWStyYdSw)N) StopIterationrreadlinerr read_or_stopAs  z%detect_encoding..read_or_stopcsz|d}Wntyd}durd|}t|wt|}|s(dSt|d}zt|}Wnt yRdurHd|}t|d|}t|wrp|dkrldurcd}t|d}t||d 7}|S) Nrtz'invalid or missing encoding declarationz {} for {!r}rHzunknown encoding: zunknown encoding for {!r}: {}zencoding problem: utf-8z encoding problem for {!r}: utf-8z-sig) decodeUnicodeDecodeErrorrQ SyntaxError cookie_rematchryr#r LookupError)riZ line_stringmsgrrLZcodec) bom_foundfilenamerr find_cookieGs@       z$detect_encoding..find_cookieT utf-8-sig)__self__nameAttributeErrorrxrblank_rer)r}rLdefaultr~rfirstsecondr)rrr}rr )s:   &        r cCsNt|d}zt|j\}}|dt||dd}d|_|WS|)zXOpen a file in read only mode using the encoding detected by detect_encoding(). rbrT)line_bufferingr+) _builtin_openr r}seekrmodeclose)rbufferrLlinestextrrrrs  rcCs6t|\}}td}t|t|d|}t|j|S)a The tokenize() 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 bytes. Alternatively, readline can be a callable function terminating with StopIteration: readline = open(myfile, 'rb').__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. The first token sequence will always be an ENCODING token which tells you which encoding was used to decode the bytes stream. rz)r r3repeatrkrY _tokenize__next__)r}rLconsumedemptyZrl_genrrrrs   rccs~d}}}d}d\}}d}dg} |dur%|dkrd}tt|dddVd} d} z| } |} Wn ty<d} Ynw|durF| |} |d 7}dt| } } |r| sZtd ||| }|r|d} }tt|| d||||f|| Vd\}}d}n|r| d dd kr| dddkrtt || ||t| f|Vd}d}q)|| }|| }q)|dkr|s| sn2d}| | kr| | dkr|d 7}n| | dkr|t d t }n | | dkrd}nn| d 7} | | ks| | krn| | dvr8| | dkr$| | d d}tt ||| f|| t|f| V| t|7} tt | | d|| f|t| f| Vq)|| dkrU| |tt| d| |df|| f| V|| dkr|| vrjtdd|| | f| dd} ttd|| f|| f| V|| dks\n | std|dfd}| | krtt| | }|r|d \}}||f||f|}}} ||krq| ||| |}}||vs|dkr|dkr|dkrtt|||| Vn|dvr|dkrtt |||| Vntt|||| Vn|dkr|drJtt |||| Vn|tvrRtt|}|| | }|rE|d} | || }tt|||| f| Vn||f}| |d}| }n|tvsi|ddtvsi|ddtvr|ddkr||f}tt|pt|d pt|d}| |dd }}| }nXtt|||| VnI|rtt|||| Vn:|dkrd }n2|d vr|d 7}n |d!vr|d 8}tt|||| Vntt | | || f|| d f| V| d 7} | | ksq*| r| ddvr|  dsttd|d t| f|d t| d fdV| d dD]}ttd|df|dfdVqtt!d|df|dfdVdS)"NrZ 0123456789)r.rrrt)rrr.rzTrHzEOF in multi-line stringrNz\ rO  z# #z rXz3unindent does not match any outer indentation levelz zEOF in multi-line statement.z... rWr\z([{z)]})"r r\r{rrZrDrrhrnZ ERRORTOKENtabsizerstripZCOMMENTrbrRr^IndentationErrorr_r> PseudoTokenspanrmraendswith triple_quotedendpats single_quotedget isidentifierrlrstriprxr])r}rLZlnumZparenlevZ continuedZnumcharsZcontstrZneedcontZcontlinere last_lineriposmaxZstrstartZendprogZendmatchrhcolumnZ comment_tokenZ pseudomatchrSZsposZeposrgZinitialrjrrrrs,      $        "                          $#.rcCs t|dS)zTokenize a source reading Python code as unicode strings. This has the same API as tokenize(), except that it expects the *readline* callable to return str objects instead of bytes. N)rr|rrrr fs r c s2ddl}dddfdd }|jdd}|jdd d d d |jd ddddd|}zQ|jrP|j}t|d}tt|j}Wdn1sJwYn d}t t j jd}|D]}|j }|j rf|j}d|j|j} td| t||jfq[WdSty} z| jddd\} } || jd|| | fWYd} ~ dSd} ~ wty} z| jd\} } || jd|| | fWYd} ~ dSd} ~ wty} z || |WYd} ~ dSd} ~ wty} z || WYd} ~ dSd} ~ wtytdYdSty} zd| d} ~ ww)NrcSstj|tjddS)Nr)sysstderrwrite)messagerrrperrorrs zmain..perrorcsR|r|f||f}d|n|rd||fnd|tddS)Nz%s:%d:%d: error: %sz %s: error: %sz error: %srH)rexit)rrlocationargsrrrerrorvs zmain..errorzpython -m tokenize)Zprogrr(z filename.pyz'the file to tokenize; defaults to stdin)destZnargsZmetavarhelpz-ez--exactexactZ store_truez(display token names using the exact type)ractionrrzz %d,%d-%d,%d:z%-20s%-15s%-15rrHrz interrupted zunexpected error: %s)NN)argparseZArgumentParserZ add_argument parse_argsrrlistrr}rrstdinr rrrSrhprintrrrrrDrOSErrorKeyboardInterrupt Exception) rrZparserrrr-rIrgZ token_typeZ token_rangeerrrirrrrmainnsf   $$ r__main__)U__doc__ __author__Z __credits__builtinsrrcodecsrr collections functoolsior itertoolsr3r;rrgrr<ASCIIrr__all__ namedtupler r#r&r)Z WhitespaceZCommentZIgnoreZNameZ HexnumberZ BinnumberZ OctnumberZ DecnumberZ IntnumberZExponentZ PointfloatZExpfloatZ FloatnumberZ ImagnumberZNumberr: lru_cacher>Z StringPrefixZSingleZDoubleZSingle3ZDouble3ZTripleZStringmapescapesortedZSpecialZFunnyZ PlainTokenZTokenZContStrZ PseudoExtrasrrZ_prefixsetrrr9r,r6rrrDrFrGr ryr rrr rrrrrrs               _ ]8 =