U ifd@sHdZdZdZddlmZddlmZmZddl Z ddl m Z ddl Z ddlZddlZddlTdd lmZed ejZed ejZddlZejd d dddgZ[Gddde ddZddZddZddZdZdZeedeeeZdZdZ dZ!dZ"d Z#ee e!e"e#Z$d!Z%ed"d#ee%Z&d$e%Z'ee&e'Z(ed%e(d&Z)ee)e(e$Z*d'd(Z+d)d*Z,ee+Z-d+Z.d,Z/d-Z0d.Z1ee-d/e-d0Z2ee-d1e-d2Z3ee4ej5e6ed3d4Z7ed5e7Z8ee*e8e3eZ9ee9Z:ee-d6ed7de-d8ed9dZ;ed:ee2Ze+D]6Z?e.e>e?d7<e/e>e?d9<e0e>e?d/<e1e>e?d0<q,e@ZAe@ZBe+D]JZCeCd9eCd7fD]ZDeAEeDqeCd0eCd/fD]ZDeBEeDqqvd;ZFGdd?d?eGZIGd@dAdAZJdBdZKdCdDZLdEdZMdFdGZdHd ZNdIdJZOdKd ZPdLdMZQeRdNkrDeQdS)OaoTokenization 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)selfannotated_typer-/opt/alt/python38/lib64/python3.8/tokenize.py__repr__.s zTokenInfo.__repr__cCs(|jtkr|jtkrt|jS|jSdSN)r OPstringrrrrr exact_type3s zTokenInfo.exact_typeN)__name__ __module__ __qualname__rpropertyrrrrrr -sztype string start end linecGsdd|dS)N(|))joinchoicesrrrgroup:r$cGs t|dS)Nrr$r"rrrany;r%r'cGs t|dS)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]cCs^ddddddg}dh}|D]>}t|D].}tjdd |DD]}|d|q@q(q|S) NbrufbrfrcSsg|]}||fqSr)upper).0crrr ^sz(_all_string_prefixes..) _itertools permutationsproductaddr!)_valid_string_prefixesresultprefixtr,rrr_all_string_prefixesSsr=cCst|tjSr)recompileUNICODE)exprrrr_compilebsrBz[^'\\]*(?:\\.[^'\\]*)*'z[^"\\]*(?:\\.[^"\\]*)*"z%[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''z%[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""z'''z"""z'[^\n'\\]*(?:\\.[^\n'\\]*)*'z"[^\n"\\]*(?:\\.[^\n"\\]*)*"T)reversez\r?\nz'[^\n'\\]*(?:\\.[^\n'\\]*)*'z"[^\n"\\]*(?:\\.[^\n"\\]*)*"z \\\r?\n|\Zc@s eZdZdS) TokenErrorNrrrrrrrrGsrGc@s eZdZdS)StopTokenizingNrHrrrrrIsrIc@s,eZdZddZddZddZddZd S) UntokenizercCsg|_d|_d|_d|_dS)Nr)tokensprev_rowprev_colencodingrrrr__init__szUntokenizer.__init__cCs|\}}||jks&||jkr>||jkr>td|||j|j||j}|rb|jd|d|_||j}|r|jd|dS)Nz+start ({},{}) precedes previous end ({},{})\ r )rMrN ValueErrorformatrLappend)rstartrowcol row_offset col_offsetrrradd_whitespaces  zUntokenizer.add_whitespacec Cs6t|}g}d}|D]}t|dkr8|||q*|\}}}} } |tkrV||_q|tkrdq*|tkrz||qnl|tkr| | \|_ |_ qnL|t t fkrd}n:|r|r|d} |dt| kr|j| t| |_ d}|||j|| \|_ |_ |t t fkr|j d7_ d|_ qd|jS)NFTrKrr0)iterlencompatENCODINGrO ENDMARKERINDENTrUDEDENTpoprMrNNEWLINENLrLr[r!) riterableitindents startliner<tok_typetokenrVendlineindentrrrr sF            zUntokenizer.untokenizec Csg}|jj}|dttfk}d}t|g|D]}|dd\}} |tkrR| |_q.|tt fkrf| d7} |t kr|rzd| } d}nd}|t kr|| q.n>|t kr| q.n*|ttfkrd}n|r|r||dd}|| q.dS)NrFr\rRTr])rLrUrfrgr5chainrarONAMENUMBERSTRINGrcrdre) rrmrhrj toks_appendrk prevstringtoktoknumtokvalrrrr`s8    zUntokenizer.compatN)rrrrPr[r r`rrrrrJs %rJcCs*t}||}|jdk r&||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)rJr rOencode)rhutoutrrrr s    cCsH|dddd}|dks*|dr.dS|dks@|drDd 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-r)lowerreplace startswith)orig_encencrrr_get_normal_namesrcsz jjWntk r$dYnXdd}d}fdd}fdd}|}|trpd|d d}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. NFrcs$zWStk rYdSXdS)Nr%) StopIterationrreadlinerr read_or_stop?sz%detect_encoding..read_or_stopcsz|d}Wn4tk rBd}dk r6d|}t|YnXt|}|sVdSt|d}z t|}Wn:t k rdkrd|}n d|}t|YnXr|dkr؈dkrd}n d}t||d 7}|S) Nrz'invalid or missing encoding declarationz {} for {!r}rKzunknown encoding: zunknown encoding for {!r}: {}zencoding problem: utf-8z encoding problem for {!r}: utf-8z-sig) decodeUnicodeDecodeErrorrT SyntaxError cookie_rematchrr$r LookupError)ro line_stringmsgrrOcodec) bom_foundfilenamerr find_cookieEs8     z$detect_encoding..find_cookieT utf-8-sig)__self__nameAttributeErrorrrblank_rer)rrOdefaultrrfirstsecondr)rrrrr 's8   &       cCsXt|d}z2t|j\}}|dt||dd}d|_|WS|YnXdS)zXOpen a file in read only mode using the encoding detected by detect_encoding(). rbrT)line_bufferingr+N) _builtin_openr rseekrmodeclose)rbufferrOlinestextrrrrs  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. r%)r r5repeatrqr^ _tokenize__next__)rrOconsumedemptyrl_genrrrrs  ccsld}}}d}d\}}d}dg} |dk rH|dkr6d}tt|dddVd} d} z| } |} Wntk rvd} YnX|dk r| |} |d 7}dt| } } |rp| std ||| }|r|d} }tt|| d||||f|| Vd\}}d}nf|rZ| d dd krZ| d ddkrZtt || ||t| f|Vd}d}qPn|| }|| }qPn|dkr|s| sqd}| | kr| | dkr|d 7}n8| | dkr|t d t }n| | dkrd}nq| d 7} q| | krq| | dkr| | dkr^| | d d}tt ||| f|| t|f| V| t|7} tt | | d|| f|t| f| VqP|| dkr| |tt| d| |df|| f| V|| dkr.|| krtdd|| | f| dd} ttd|| f|| f| Vqn| s*td|dfd}| | krPtt| | }|r|d \}}||f||f|}}} ||krq.| ||| |}}||ks|dkr|dkr|dkrtt|||| Vq|dkr|dkrtt |||| Vntt|||| Vq|dkr2tt |||| Vq|tkrtt|}|| | }|r|d} | || }tt|||| f| Vn||f}| |d}| }qPq|tks|ddtks|ddtkrF|ddkr2||f}tt|pt|d pt|d}| |dd }}| }qPntt|||| Vnf|rdtt|||| VnH|dkrtd }n8|dkr|d 7}n|d kr|d 8}tt|||| Vn*tt | | || f|| d f| V| d 7} q.qP| r | ddkr ttd|d t| f|d t| d fdV| d dD] }ttd|df|dfdVq,ttd|df|dfdVdS)!Nr 0123456789)r0rrr)rrr0r%rKzEOF in multi-line stringrQz\ rR  z# #z r]z3unindent does not match any outer indentation levelz zEOF in multi-line statement.z...r\r \z([{z)]})r rarrr_rGrrnrt ERRORTOKENtabsizerstripCOMMENTrgrUrcIndentationErrorrdrB PseudoTokenspanrsrf triple_quotedendpats single_quotedget isidentifierrrrrb)rrOlnumparenlev continuednumcharscontstrneedcontcontlinerj last_lineroposmaxstrstartendprogendmatchrncolumn comment_token pseudomatchrVsposeposrminitialrprrrrs<      *         "                         .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)rrrrrr dsc s$ddl}dddfdd }|jdd}|jdd d d d |jd ddddd|}z|jr|j}t|d}tt|j}W5QRXnd}t t j jd}|D]>}|j }|j r|j}d|j|j} td| t||jfqWn8tk r6} z0| jddd\} } || jd|| | fW5d} ~ XYntk r|} z(| jd\} } || jd|| | fW5d} ~ XYntk r} z|| |W5d} ~ XYnxtk r} z || W5d} ~ XYnNtk rtdYn2tk r} zd| W5d} ~ XYnXdS)NrcSstj|tjddS)Nr)sysstderrwrite)messagerrrperrorps zmain..perrorcsR|r"|f||f}d|n"|r8d||fn d|tddS)Nz%s:%d:%d: error: %sz %s: error: %sz error: %srK)rexit)rrlocationargsrrrerrorts zmain..errorzpython -m tokenize)progrr(z filename.pyz'the file to tokenize; defaults to stdin)destnargsmetavarhelpz-ez--exactexact store_truez(display token names using the exact type)ractionrrzz %d,%d-%d,%d:z%-20s%-15s%-15rrKrz interrupted zunexpected error: %s)NN)argparseArgumentParser add_argument parse_argsrrlistrrrrstdinr rrrVrnprintrrrrrGrOSErrorKeyboardInterrupt Exception) rrparserrrr-rLrm token_type token_rangeerrrorrrrmainlsT   &&  r__main__)S__doc__ __author__ __credits__builtinsrrcodecsrr collectionsior itertoolsr5r>rrmrr?ASCIIrr__all__ namedtupler r$r'r) WhitespaceCommentIgnoreName Hexnumber Binnumber Octnumber Decnumber IntnumberExponent PointfloatExpfloat Floatnumber ImagnumberNumberr=rB StringPrefixSingleDoubleSingle3Double3TripleStringmapescapesortedSpecialFunny PlainTokenTokenContStr PseudoExtrasrr_prefixsetrrr<r,r8rrrGrIrJr rr rrr rrrrrrs               _ ]8=