j fyK&@s6dZdZdZddlZddlZddlmZmZddlTddl m Z d d e e Dd d d gZ [ ye Wnek reZ YnXddZddZddZdZdZeedeeeZdZdZdZdZdZeeeeeZdZeddeeZdeZeeeZed ed!Z ee eeZ!d"Z"d#Z#d$Z$d%Z%ed&d'Z&ed(d)Z'ed*d+d,d-d.d/d0d1d2 Z(d3Z)ed4d5Z*ee(e)e*Z+ee!e+e'eZ,ee,Z-ed6ed7dd8ed9dZ.edee&Z/eee/e!e+e.eZ0e1e2ej3e-e0e$e%f\Z4Z5Z6Z7i&ej3e"d76ej3e#d96e6d:6e7d;6e6d<6e7d=6e6d>6e7d?6e6d@6e7dA6e6dB6e7dC6e6dD6e7dE6e6dF6e7dG6e6dH6e7dI6e6dJ6e7dK6e6dL6e7dM6e6dN6e7dO6e6dP6e7dQ6e6dR6e7dS6e6dT6e7dU6e6dV6e7dW6ddX6ddY6ddZ6dd[6dd\6dd]6Z8iZ9xdD]Z:e:e9e:Gd}d~d~e=Z?ddZ@e@dd ZAddZBGdddZCej3dejDZEej3dejDZFddZGddZHdd ZIdd ZJeKdkr2ddlLZLeMeLjNdkreAeOeLjNdjPq2eAeLjQjPndS)aTokenization 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)*)tokencCs&g|]}|ddkr|qS)r_).0xrr;/opt/alt/python34/lib64/python3.4/lib2to3/pgen2/tokenize.py %s r tokenizegenerate_tokens untokenizecGsddj|dS)N(|))join)choicesrrr group0srcGst|dS)Nr)r)rrrr any1srcGst|dS)N?)r)rrrr maybe2srz[ \f\t]*z #[^\r\n]*z\\\r?\nz [a-zA-Z_]\w*z 0[bB][01]*z0[xX][\da-fA-F]*[lL]?z0[oO]?[0-7]*[lL]?z [1-9]\d*[lL]?z [eE][-+]?\d+z\d+\.\d*z\.\d+z\d+z\d+[jJ]z[jJ]z[^'\\]*(?:\\.[^'\\]*)*'z[^"\\]*(?:\\.[^"\\]*)*"z%[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''z%[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""z[ubUB]?[rR]?'''z[ubUB]?[rR]?"""z&[uU]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*'z&[uU]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*"z\*\*=?z>>=?z<<=?z<>z!=z//=?z->z[+\-*/%&@|^=<>]=?~z[][(){}]z\r?\nz[:;.,`@]z'[uUbB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*'z'[uUbB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*"'''"""r'''r"""u'''u"""b'''b"""ur'''ur"""br'''br"""R'''R"""U'''U"""B'''B"""uR'''uR"""Ur'''Ur"""UR'''UR"""bR'''bR"""Br'''Br"""BR'''BR"""rRuUbBr'r"R'R"u'u"U'U"b'b"B'B"ur'ur"Ur'Ur"uR'uR"UR'UR"br'br"Br'Br"bR'bR"BR'BR"c@seZdZdS) TokenErrorN)__name__ __module__ __qualname__rrrr r]s r]c@seZdZdS)StopTokenizingN)r^r_r`rrrr ras rac CsF|\}}|\}}td||||t|t|fdS)Nz%d,%d-%d,%d: %s %s)printtok_namerepr) typerZxxx_todo_changemeZxxx_todo_changeme1lineZsrowZscolZerowZecolrrr printtokens  rgc Cs*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)readline tokeneaterrrr r s  cCs%xt|D]}||q WdS)N)r)rirjZ token_inforrr rhsrhc@s@eZdZddZddZddZddZd S) UntokenizercCsg|_d|_d|_dS)Nrr)tokensprev_rowprev_col)selfrrr __init__s  zUntokenizer.__init__cCsO|\}}||jks!t||j}|rK|jjd|ndS)N )rmAssertionErrorrnrlappend)rostartrowcol col_offsetrrr add_whitespaces   zUntokenizer.add_whitespacecCsx|D]}t|dkr3|j||Pn|\}}}}}|j||jj||\|_|_|ttfkr|jd7_d|_qqWdj |jS)Nrr) lencompatrxrlrsrmrnNEWLINENLr)roiterablettok_typerrtendrfrrr rs  zUntokenizer.untokenizec Cs+d}g}|jj}|\}}|ttfkrC|d7}n|ttfkr^d}nx|D]}|dd\}}|ttfkr|d7}n|tkr|j|qenZ|tkr|jqen>|ttfkrd}n#|r|r||dd}n||qeWdS)NFrqTryr) rlrsNAMENUMBERr}r~INDENTDEDENTpop) rorr startlineindents toks_appendtoknumtokvaltokrrr r|s0             zUntokenizer.compatN)r^r_r`rprxrr|rrrr rks    rkz%^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)s^[ \t\f]*(?:[#\r\n]|$)cCsd|ddjjdd}|dks=|jdrAdS|d ks\|jdr`dS|S)z(Imitates get_normal_name in tokenizer.c.N r-zutf-8zutf-8-latin-1 iso-8859-1 iso-latin-1latin-1- iso-8859-1- iso-latin-1-)rrr)rrr)lowerreplace startswith)orig_encencrrr _get_normal_names" rcsdd}d}fdd}fdd}|}|jtrmd|d d}d }n|s}|gfS||}|r||gfStj|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-8c s+y SWntk r&tSYnXdS)N) StopIterationbytesr)rirr read_or_stops  z%detect_encoding..read_or_stopcsy|jd}Wntk r+dSYnXtj|}|sEdSt|jd}yt|}Wn"tk rtd|YnXr|j dkrtdn|d7}n|S)Nasciirzunknown encoding: zutf-8zencoding problem: utf-8z-sig) decodeUnicodeDecodeError cookie_rematchrrr LookupError SyntaxErrorname)rf line_stringrencodingcodec) bom_foundrr find_cookies"    z$detect_encoding..find_cookieTz utf-8-sig)rrblank_rer)rirdefaultrrfirstsecondr)rrir detect_encodings0         rcCst}|j|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 intput: # 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 )rkr)rutrrr rFs ccsd}}}tjdd}}d\}}d}dg} x y |} Wntk rgd} YnX|d}dt| } } |r| std| n|j| }|r |jd} }t|| d|| ||f|| fVd\}}d}q |rt| ddd krt| ddd krtt|| | |t| f|fVd}d}q@q || }|| }q@n~|dkr| r| sPnd}xv| | kr)| | d kr|d}n?| | d kr|t dt }n| | dkrd}nP| d} qW| | kr:Pn| | dkr*| | dkr| | dj d}| t|}t ||| f|| t|f| fVt | |d||f|t| f| fVq@t t f| | dk| | d|| f|t| f| fVq@n|| dkrt| j |t| d| |df|| f| fVnx|| dkr|| krtdd|| | fn| dd } td|| f|| f| fVqwWn$| std|dfnd}x9| | krGtj| | }|r|jd\}}||f||f|}}} | ||| |}}||ks|dkr|dkrt|||| fVqD|dkrt}|dkrt }n||||| fVqD|dkr5|jd stt |||| fVqD|tkrt|}|j| | }|r|jd} | || }t|||| f| fVq||f} | |d}| }PqD|tks|ddtks|dd tkrw|d!dkr`||f} t|p<t|dp<t|d}| |dd}}| }Pqt|||| fVqD||krt|||| fVqD|dkrt |||| f| fVd}qD|dkr|d}n|dkr|d}nt|||| fVqt| | || f|| df| fV| d} qWq@Wx8| ddD]&}td|df|dfdfVq]Wtd|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. rr 0123456789rzNrzEOF in multi-line stringryz\ rz\ rq  z# #z z3unindent does not match any outer indentation levelz zEOF in multi-line statement. \z([{z)]})rzr)rzrrrrr)stringZ ascii_lettersrr{r]rrSTRING ERRORTOKENtabsizerstripCOMMENTr~rsrIndentationErrorr pseudoprogspanrr}endswithrr triple_quotedendprogs single_quotedrOP ENDMARKER)rilnumparenlev continuedZ namecharsnumcharscontstrneedcontcontlinerrfposmaxstrstartendprogendmatchrcolumn comment_tokennl_pos pseudomatchrtsposeposrinitialnewlineindentrrr r[s        2      $#$  - '                 $__main__)rrrrr(r)r r!r*r+r"r#r,r-r$r%r0r1r.r/r2r3r&r'r6r7r4r5r8r9)rrr@rArBrCrDrErFrGrHrIrJrKrLrMrNrOrPrQrRrSrTrUrVrWrXrYrZr[)R__doc__ __author__ __credits__rrecodecsrrZlib2to3.pgen2.tokenrzrdir__all__r NameErrorstrrrr WhitespaceCommentIgnoreName Binnumber Hexnumber Octnumber Decnumber IntnumberExponent PointfloatExpfloat Floatnumber ImagnumberNumberSingleDoubleSingle3Double3TripleStringOperatorBracketSpecialFunny PlainTokenTokenContStr PseudoExtras PseudoTokenlistmapcompileZ tokenprogrZ single3progZ double3progrrrrr Exceptionr]rargr rhrkASCIIrrrrrrr^sysr{argvopenristdinrrrr s            *#    8 I