B y `X @sBdZddlmZmZmZmZmZmZmZm Z m Z m Z ddl Tddl mZdZdZddlZddlmZmZddl Td d lmZd d eeDd ddgZ[ddZddZddZddZdZdZeedeeeZ dZ!dZ"dZ#dZ$edd 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'Z-d(Z.d)Z/d*Z0d+Z1ee1d,e1d-Z2ee1d.e1d/Z3ed0d1d2d3d4d5d6d7d8 Z4d9Z5ed:d;Z6ee4e5e6Z7ee1d<ed=de1d>ed?dZ8edee2Z9eee9e,e7e8e!Z:e;e:ej<Z=e;e/Z>e;e0Z?ed@dAdBdCed@dAdDdEBdFdGdHdIdJdKhBZ@e;e-e;e.e>e?dLdMdNe@DdOdNe@DdPdNe@DZAd,d-hdQdRe@DBdSdRe@DBZBd=d?hdTdRe@DBdUdRe@DBZCdVZDGdWdXdXeEZFGdYdZdZeEZGd[d\ZHeeIeIfZJeeIeeJeJegdfZKeHfegefeKdd]d^d ZLd_d`ZMeeIeeJeJefZNe eeIeOfeNfZPGdadbdbZQe;dcejRZSe;ddejRZTeOeOdedfdgZUegeVfeeOeeVfdhdidjZWeePedkdldZXdpegefeeeeNdmdndZYeZdokr>ddl[Z[e\e[j]d kr2eLe^e[j]d j_n eLe[j`j_dS)qaTokenization 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.) CallableIterableIteratorListOptionalTextTuplePatternUnioncast)*)GrammarzKa-Ping Yee z@GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip MontanaroN)BOM_UTF8lookup)tokencCsg|]}|ddkr|qS)r_).0xrr;/tmp/pip-unpacked-wheel-qq0fnpma/blib2to3/pgen2/tokenize.py 6srtokenizegenerate_tokens untokenizecGsdd|dS)N(|))join)choicesrrrgroup>sr cGs t|dS)Nr )r )rrrranyBsr!cGs t|dS)N?)r )rrrrmaybeFsr#cstfddDS)Nc3s4|],}dD]}||kr||VqqdS))N)casefold)rry)lrr Ksz _combinations..)set)r'r)r'r _combinationsJsr*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'''z"""z'[^\n'\\]*(?:\\.[^\n'\\]*)*'z"[^\n"\\]*(?:\\.[^\n"\\]*)*"z\*\*=?z>>=?z<<=?z<>z!=z//=?z->z[+\-*/%&@|^=<>:]=?~z[][(){}]z\r?\nz[:;.,`@]z'[^\n'\\]*(?:\\.[^\n'\\]*)*'z"[^\n"\\]*(?:\\.[^\n"\\]*)*"rRfFbBuUurZuRZUrZUR)r,r-z'''z"""cCsi|]}t|dqS)z''') single3prog)rprefixrrr sr9cCsi|]}t|dqS)z""") double3prog)rr8rrrr9scCsi|] }d|qS)Nr)rr8rrrr9scCsh|]}|dqS)z'''r)rr8rrr sr;cCsh|]}|dqS)z"""r)rr8rrrr;scCsh|]}|dqS)r,r)rr8rrrr;scCsh|]}|dqS)r-r)rr8rrrr;sc@s eZdZdS) TokenErrorN)__name__ __module__ __qualname__rrrrr=sr=c@s eZdZdS)StopTokenizingN)r>r?r@rrrrrAsrAc Cs4|\}}|\}}td||||t|t|fdS)Nz%d,%d-%d,%d: %s %s)printtok_namerepr) typerZxxx_todo_changemeZxxx_todo_changeme1lineZsrowZscolZerowZecolrrr printtokensrG)readline tokeneaterreturncCs(yt||Wntk r"YnXdS)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_looprA)rHrIrrrrs cCsxt|D] }||q WdS)N)r)rHrIZ token_inforrrrKsrKc@szeZdZUeeed<eed<eed<ddddZeddd d Z e e ed d d Z e eefe e ddddZdS) Untokenizertokensprev_rowprev_colN)rJcCsg|_d|_d|_dS)Nrr)rMrNrO)selfrrr__init__szUntokenizer.__init__)startrJcCs8|\}}||jkst||j}|r4|jd|dS)N )rNAssertionErrorrOrMappend)rPrRrowcol col_offsetrrradd_whitespaces  zUntokenizer.add_whitespace)iterablerJcCsx|D]}t|dkr2|ttttf||Ptttttttf|\}}}}}|||j ||\|_ |_ |t tfkr|j d7_ d|_ qWd|j S)Nrrr$)lencompatr rintstrrCoordrYrMrUrNrONEWLINENLr)rPrZttok_typerrRendrFrrrrs        zUntokenizer.untokenize)rrZrJc Csd}g}|jj}|\}}|ttfkr,|d7}|ttfkr|t kr| qBn*|ttfkrd}n|r|r||dd}||qBWdS)NFrSTr[) rMrUNAMENUMBERrarbASYNCAWAITINDENTDEDENTpop) rPrrZ startlineindents toks_appendtoknumtokvaltokrrrr]s0      zUntokenizer.compat)r>r?r@rr__annotations__r^rQr`rYr TokenInforrr]rrrrrLs  rLz&^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)s^[ \t\f]*(?:[#\r\n]|$))orig_encrJcCsH|dddd}|dks*|dr.dS|dks@|drDd S|S) z(Imitates get_normal_name in tokenizer.c.N r-zutf-8zutf-8-)zlatin-1z iso-8859-1z iso-latin-1)zlatin-1-z iso-8859-1-z iso-latin-1-z iso-8859-1)lowerreplace startswith)rvencrrr_get_normal_name$s r})rHrJcsdd}d}tdfdd }tttdfdd }|}|trZd |d d}d }|sf|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. FNzutf-8)rJcs"yStk rtSXdS)N) StopIterationbytesr)rHrr read_or_stopGsz%detect_encoding..read_or_stop)rFrJcsy|d}Wntk r"dSXt|}|s6dSt|d}y t|}Wn tk rptd|YnXr|j dkrtd|d7}|S)Nasciirzunknown encoding: zutf-8zencoding problem: utf-8z-sig) decodeUnicodeDecodeError cookie_rematchr}r r LookupError SyntaxErrorname)rF line_stringrencodingcodec) bom_foundrr find_cookieMs"   z$detect_encoding..find_cookieTz utf-8-sig)rrr_r{rblank_rer)rHrdefaultrrfirstsecondr)rrHrdetect_encoding1s0       r)rZrJcCst}||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)rZutrrrr|s)rHgrammarrJc"csd}}}d}d\}}d}dg} |dkr.dn|j} d} d} d} d}xy |}Wntk rjd}YnX|d}dt|}}|rX|dk st|std|||}|r|d}}t||d||||f||fVd\}}d}nd|rB|d dd krB|d dd krBt||||t|f|fVd}d}qHn||}||}qHn4|dkrt|st|spPd}xf||kr||d kr|d}n6||dkr|t dt }n||dkrd}nP|d}qvW||krP| r| Vd} ||dkr,t ||d||f|t|f|fVqH||dkr||d d}|t|}t |||f||t|f|fVt ||d||f|t|f|fVqH|| dkr| |t|d||df||f|fVxt|| dkrJ|| krtdd|||f| dd} | r.| | dkr.d} d}d} td||f||f|fVqW| r|r| | dkrd} d}d} n|std|dfd}x||krVt||}|r*|d\}}||f||f|}}}|||||}}||ks|dkr|dkrt||||fVqR|dkrft}|dkr8t }n | rBd}| rR| Vd} |||||fVqR|dkr|drt| r| Vd} t ||||fVqR|tkr"t|}|||}|r|d}|||}| r| Vd} t||||f|fVn||f}||d}|}PqR|tksP|ddtksP|ddtkr|ddkr||f}t|pt|dpt|d}||dd}}|}Pn | r| Vd} t||||fVqR|r|dkr | s| r |dkrtnt||||fVqt||||f} |dkr0| s0| } q|dkr| r| dtkr| ddkr|dkrrd} | d} t| d| d| d| dfVd} | r| Vd} | Vnz|d kr| r| Vd} t ||||f|fVd}nF|d!kr|d}n|d"kr|d}| r| Vd} t||||fVn(t||||f||df|fV|d}qWqHW| rl| Vd} x.| ddD]}!td|df|dfdfVqzWtd|df|dfdfVdS)#aT 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 logical line; continuation lines are included. r 0123456789)r$rNFr$rzEOF in multi-line stringz\ z\ rS  z #rfz3unindent does not match any outer indentation levelz zEOF in multi-line statement.T r[r)asyncawaitr)defforr\z([{z)]})async_keywordsr~r\rTr=rreSTRING ERRORTOKENtabsizerbrstripCOMMENTrUrkIndentationErrorrl pseudoprogspanrhraendswith triple_quotedendprogs single_quoted isidentifierrirjrgOP ENDMARKER)"rHrlnumparenlev continuednumcharscontstrneedcontcontlinerorZstashedZ async_defZasync_def_indentZ async_def_nlrFposmaxstrstartendprogendmatchrecolumn comment_tokenZnl_pos pseudomatchrRsposeposrinitialnewlinersindentrrrrs       *    $ $                     "       __main__)N)a__doc__typingrrrrrrrr r r Zblib2to3.pgen2.tokenZblib2to3.pgen2.grammarr __author__ __credits__regexrecodecsrrr$rdir__all__r r!r#r* WhitespaceCommentIgnoreName Binnumber Hexnumber Octnumber Decnumber IntnumberExponent PointfloatExpfloat Floatnumber ImagnumberNumberSingleDoubleSingle3Double3Z _litprefixTripleStringOperatorBracketSpecialFunnyContStr PseudoExtras PseudoTokencompileUNICODErr7r:Z _strprefixesrrrr Exceptionr=rArGr^r`Z TokenEaterrrKZ GoodTokenInfor_rurLASCIIrrr}rrrrr>sysr\argvopenrHstdinrrrrs0             ,  $$ ? $K