Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: tokenize.py
"""Tokenization help for Python programs.
[0] Fix | Delete
[1] Fix | Delete
generate_tokens(readline) is a generator that breaks a stream of
[2] Fix | Delete
text into Python tokens. It accepts a readline-like method which is called
[3] Fix | Delete
repeatedly to get the next line of input (or "" for EOF). It generates
[4] Fix | Delete
5-tuples with these members:
[5] Fix | Delete
[6] Fix | Delete
the token type (see token.py)
[7] Fix | Delete
the token (a string)
[8] Fix | Delete
the starting (row, column) indices of the token (a 2-tuple of ints)
[9] Fix | Delete
the ending (row, column) indices of the token (a 2-tuple of ints)
[10] Fix | Delete
the original line (string)
[11] Fix | Delete
[12] Fix | Delete
It is designed to match the working of the Python tokenizer exactly, except
[13] Fix | Delete
that it produces COMMENT tokens for comments and gives type OP for all
[14] Fix | Delete
operators
[15] Fix | Delete
[16] Fix | Delete
Older entry points
[17] Fix | Delete
tokenize_loop(readline, tokeneater)
[18] Fix | Delete
tokenize(readline, tokeneater=printtoken)
[19] Fix | Delete
are the same, except instead of generating tokens, tokeneater is a callback
[20] Fix | Delete
function to which the 5 fields described above are passed as 5 arguments,
[21] Fix | Delete
each time a new token is found."""
[22] Fix | Delete
[23] Fix | Delete
__author__ = 'Ka-Ping Yee <ping@lfw.org>'
[24] Fix | Delete
__credits__ = ('GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, '
[25] Fix | Delete
'Skip Montanaro, Raymond Hettinger')
[26] Fix | Delete
[27] Fix | Delete
from itertools import chain
[28] Fix | Delete
import string, re
[29] Fix | Delete
from token import *
[30] Fix | Delete
[31] Fix | Delete
import token
[32] Fix | Delete
__all__ = [x for x in dir(token) if not x.startswith("_")]
[33] Fix | Delete
__all__ += ["COMMENT", "tokenize", "generate_tokens", "NL", "untokenize"]
[34] Fix | Delete
del x
[35] Fix | Delete
del token
[36] Fix | Delete
[37] Fix | Delete
COMMENT = N_TOKENS
[38] Fix | Delete
tok_name[COMMENT] = 'COMMENT'
[39] Fix | Delete
NL = N_TOKENS + 1
[40] Fix | Delete
tok_name[NL] = 'NL'
[41] Fix | Delete
N_TOKENS += 2
[42] Fix | Delete
[43] Fix | Delete
def group(*choices): return '(' + '|'.join(choices) + ')'
[44] Fix | Delete
def any(*choices): return group(*choices) + '*'
[45] Fix | Delete
def maybe(*choices): return group(*choices) + '?'
[46] Fix | Delete
[47] Fix | Delete
Whitespace = r'[ \f\t]*'
[48] Fix | Delete
Comment = r'#[^\r\n]*'
[49] Fix | Delete
Ignore = Whitespace + any(r'\\\r?\n' + Whitespace) + maybe(Comment)
[50] Fix | Delete
Name = r'[a-zA-Z_]\w*'
[51] Fix | Delete
[52] Fix | Delete
Hexnumber = r'0[xX][\da-fA-F]+[lL]?'
[53] Fix | Delete
Octnumber = r'(0[oO][0-7]+)|(0[0-7]*)[lL]?'
[54] Fix | Delete
Binnumber = r'0[bB][01]+[lL]?'
[55] Fix | Delete
Decnumber = r'[1-9]\d*[lL]?'
[56] Fix | Delete
Intnumber = group(Hexnumber, Binnumber, Octnumber, Decnumber)
[57] Fix | Delete
Exponent = r'[eE][-+]?\d+'
[58] Fix | Delete
Pointfloat = group(r'\d+\.\d*', r'\.\d+') + maybe(Exponent)
[59] Fix | Delete
Expfloat = r'\d+' + Exponent
[60] Fix | Delete
Floatnumber = group(Pointfloat, Expfloat)
[61] Fix | Delete
Imagnumber = group(r'\d+[jJ]', Floatnumber + r'[jJ]')
[62] Fix | Delete
Number = group(Imagnumber, Floatnumber, Intnumber)
[63] Fix | Delete
[64] Fix | Delete
# Tail end of ' string.
[65] Fix | Delete
Single = r"[^'\\]*(?:\\.[^'\\]*)*'"
[66] Fix | Delete
# Tail end of " string.
[67] Fix | Delete
Double = r'[^"\\]*(?:\\.[^"\\]*)*"'
[68] Fix | Delete
# Tail end of ''' string.
[69] Fix | Delete
Single3 = r"[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''"
[70] Fix | Delete
# Tail end of """ string.
[71] Fix | Delete
Double3 = r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'
[72] Fix | Delete
Triple = group("[uUbB]?[rR]?'''", '[uUbB]?[rR]?"""')
[73] Fix | Delete
# Single-line ' or " string.
[74] Fix | Delete
String = group(r"[uUbB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*'",
[75] Fix | Delete
r'[uUbB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*"')
[76] Fix | Delete
[77] Fix | Delete
# Because of leftmost-then-longest match semantics, be sure to put the
[78] Fix | Delete
# longest operators first (e.g., if = came before ==, == would get
[79] Fix | Delete
# recognized as two instances of =).
[80] Fix | Delete
Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"<>", r"!=",
[81] Fix | Delete
r"//=?",
[82] Fix | Delete
r"[+\-*/%&|^=<>]=?",
[83] Fix | Delete
r"~")
[84] Fix | Delete
[85] Fix | Delete
Bracket = '[][(){}]'
[86] Fix | Delete
Special = group(r'\r?\n', r'[:;.,`@]')
[87] Fix | Delete
Funny = group(Operator, Bracket, Special)
[88] Fix | Delete
[89] Fix | Delete
PlainToken = group(Number, Funny, String, Name)
[90] Fix | Delete
Token = Ignore + PlainToken
[91] Fix | Delete
[92] Fix | Delete
# First (or only) line of ' or " string.
[93] Fix | Delete
ContStr = group(r"[uUbB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*" +
[94] Fix | Delete
group("'", r'\\\r?\n'),
[95] Fix | Delete
r'[uUbB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' +
[96] Fix | Delete
group('"', r'\\\r?\n'))
[97] Fix | Delete
PseudoExtras = group(r'\\\r?\n|\Z', Comment, Triple)
[98] Fix | Delete
PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name)
[99] Fix | Delete
[100] Fix | Delete
tokenprog, pseudoprog, single3prog, double3prog = map(
[101] Fix | Delete
re.compile, (Token, PseudoToken, Single3, Double3))
[102] Fix | Delete
endprogs = {"'": re.compile(Single), '"': re.compile(Double),
[103] Fix | Delete
"'''": single3prog, '"""': double3prog,
[104] Fix | Delete
"r'''": single3prog, 'r"""': double3prog,
[105] Fix | Delete
"u'''": single3prog, 'u"""': double3prog,
[106] Fix | Delete
"ur'''": single3prog, 'ur"""': double3prog,
[107] Fix | Delete
"R'''": single3prog, 'R"""': double3prog,
[108] Fix | Delete
"U'''": single3prog, 'U"""': double3prog,
[109] Fix | Delete
"uR'''": single3prog, 'uR"""': double3prog,
[110] Fix | Delete
"Ur'''": single3prog, 'Ur"""': double3prog,
[111] Fix | Delete
"UR'''": single3prog, 'UR"""': double3prog,
[112] Fix | Delete
"b'''": single3prog, 'b"""': double3prog,
[113] Fix | Delete
"br'''": single3prog, 'br"""': double3prog,
[114] Fix | Delete
"B'''": single3prog, 'B"""': double3prog,
[115] Fix | Delete
"bR'''": single3prog, 'bR"""': double3prog,
[116] Fix | Delete
"Br'''": single3prog, 'Br"""': double3prog,
[117] Fix | Delete
"BR'''": single3prog, 'BR"""': double3prog,
[118] Fix | Delete
'r': None, 'R': None, 'u': None, 'U': None,
[119] Fix | Delete
'b': None, 'B': None}
[120] Fix | Delete
[121] Fix | Delete
triple_quoted = {}
[122] Fix | Delete
for t in ("'''", '"""',
[123] Fix | Delete
"r'''", 'r"""', "R'''", 'R"""',
[124] Fix | Delete
"u'''", 'u"""', "U'''", 'U"""',
[125] Fix | Delete
"ur'''", 'ur"""', "Ur'''", 'Ur"""',
[126] Fix | Delete
"uR'''", 'uR"""', "UR'''", 'UR"""',
[127] Fix | Delete
"b'''", 'b"""', "B'''", 'B"""',
[128] Fix | Delete
"br'''", 'br"""', "Br'''", 'Br"""',
[129] Fix | Delete
"bR'''", 'bR"""', "BR'''", 'BR"""'):
[130] Fix | Delete
triple_quoted[t] = t
[131] Fix | Delete
single_quoted = {}
[132] Fix | Delete
for t in ("'", '"',
[133] Fix | Delete
"r'", 'r"', "R'", 'R"',
[134] Fix | Delete
"u'", 'u"', "U'", 'U"',
[135] Fix | Delete
"ur'", 'ur"', "Ur'", 'Ur"',
[136] Fix | Delete
"uR'", 'uR"', "UR'", 'UR"',
[137] Fix | Delete
"b'", 'b"', "B'", 'B"',
[138] Fix | Delete
"br'", 'br"', "Br'", 'Br"',
[139] Fix | Delete
"bR'", 'bR"', "BR'", 'BR"' ):
[140] Fix | Delete
single_quoted[t] = t
[141] Fix | Delete
[142] Fix | Delete
tabsize = 8
[143] Fix | Delete
[144] Fix | Delete
class TokenError(Exception): pass
[145] Fix | Delete
[146] Fix | Delete
class StopTokenizing(Exception): pass
[147] Fix | Delete
[148] Fix | Delete
def printtoken(type, token, srow_scol, erow_ecol, line): # for testing
[149] Fix | Delete
srow, scol = srow_scol
[150] Fix | Delete
erow, ecol = erow_ecol
[151] Fix | Delete
print "%d,%d-%d,%d:\t%s\t%s" % \
[152] Fix | Delete
(srow, scol, erow, ecol, tok_name[type], repr(token))
[153] Fix | Delete
[154] Fix | Delete
def tokenize(readline, tokeneater=printtoken):
[155] Fix | Delete
"""
[156] Fix | Delete
The tokenize() function accepts two parameters: one representing the
[157] Fix | Delete
input stream, and one providing an output mechanism for tokenize().
[158] Fix | Delete
[159] Fix | Delete
The first parameter, readline, must be a callable object which provides
[160] Fix | Delete
the same interface as the readline() method of built-in file objects.
[161] Fix | Delete
Each call to the function should return one line of input as a string.
[162] Fix | Delete
[163] Fix | Delete
The second parameter, tokeneater, must also be a callable object. It is
[164] Fix | Delete
called once for each token, with five arguments, corresponding to the
[165] Fix | Delete
tuples generated by generate_tokens().
[166] Fix | Delete
"""
[167] Fix | Delete
try:
[168] Fix | Delete
tokenize_loop(readline, tokeneater)
[169] Fix | Delete
except StopTokenizing:
[170] Fix | Delete
pass
[171] Fix | Delete
[172] Fix | Delete
# backwards compatible interface
[173] Fix | Delete
def tokenize_loop(readline, tokeneater):
[174] Fix | Delete
for token_info in generate_tokens(readline):
[175] Fix | Delete
tokeneater(*token_info)
[176] Fix | Delete
[177] Fix | Delete
class Untokenizer:
[178] Fix | Delete
[179] Fix | Delete
def __init__(self):
[180] Fix | Delete
self.tokens = []
[181] Fix | Delete
self.prev_row = 1
[182] Fix | Delete
self.prev_col = 0
[183] Fix | Delete
[184] Fix | Delete
def add_whitespace(self, start):
[185] Fix | Delete
row, col = start
[186] Fix | Delete
if row < self.prev_row or row == self.prev_row and col < self.prev_col:
[187] Fix | Delete
raise ValueError("start ({},{}) precedes previous end ({},{})"
[188] Fix | Delete
.format(row, col, self.prev_row, self.prev_col))
[189] Fix | Delete
row_offset = row - self.prev_row
[190] Fix | Delete
if row_offset:
[191] Fix | Delete
self.tokens.append("\\\n" * row_offset)
[192] Fix | Delete
self.prev_col = 0
[193] Fix | Delete
col_offset = col - self.prev_col
[194] Fix | Delete
if col_offset:
[195] Fix | Delete
self.tokens.append(" " * col_offset)
[196] Fix | Delete
[197] Fix | Delete
def untokenize(self, iterable):
[198] Fix | Delete
it = iter(iterable)
[199] Fix | Delete
indents = []
[200] Fix | Delete
startline = False
[201] Fix | Delete
for t in it:
[202] Fix | Delete
if len(t) == 2:
[203] Fix | Delete
self.compat(t, it)
[204] Fix | Delete
break
[205] Fix | Delete
tok_type, token, start, end, line = t
[206] Fix | Delete
if tok_type == ENDMARKER:
[207] Fix | Delete
break
[208] Fix | Delete
if tok_type == INDENT:
[209] Fix | Delete
indents.append(token)
[210] Fix | Delete
continue
[211] Fix | Delete
elif tok_type == DEDENT:
[212] Fix | Delete
indents.pop()
[213] Fix | Delete
self.prev_row, self.prev_col = end
[214] Fix | Delete
continue
[215] Fix | Delete
elif tok_type in (NEWLINE, NL):
[216] Fix | Delete
startline = True
[217] Fix | Delete
elif startline and indents:
[218] Fix | Delete
indent = indents[-1]
[219] Fix | Delete
if start[1] >= len(indent):
[220] Fix | Delete
self.tokens.append(indent)
[221] Fix | Delete
self.prev_col = len(indent)
[222] Fix | Delete
startline = False
[223] Fix | Delete
self.add_whitespace(start)
[224] Fix | Delete
self.tokens.append(token)
[225] Fix | Delete
self.prev_row, self.prev_col = end
[226] Fix | Delete
if tok_type in (NEWLINE, NL):
[227] Fix | Delete
self.prev_row += 1
[228] Fix | Delete
self.prev_col = 0
[229] Fix | Delete
return "".join(self.tokens)
[230] Fix | Delete
[231] Fix | Delete
def compat(self, token, iterable):
[232] Fix | Delete
indents = []
[233] Fix | Delete
toks_append = self.tokens.append
[234] Fix | Delete
startline = token[0] in (NEWLINE, NL)
[235] Fix | Delete
prevstring = False
[236] Fix | Delete
[237] Fix | Delete
for tok in chain([token], iterable):
[238] Fix | Delete
toknum, tokval = tok[:2]
[239] Fix | Delete
[240] Fix | Delete
if toknum in (NAME, NUMBER):
[241] Fix | Delete
tokval += ' '
[242] Fix | Delete
[243] Fix | Delete
# Insert a space between two consecutive strings
[244] Fix | Delete
if toknum == STRING:
[245] Fix | Delete
if prevstring:
[246] Fix | Delete
tokval = ' ' + tokval
[247] Fix | Delete
prevstring = True
[248] Fix | Delete
else:
[249] Fix | Delete
prevstring = False
[250] Fix | Delete
[251] Fix | Delete
if toknum == INDENT:
[252] Fix | Delete
indents.append(tokval)
[253] Fix | Delete
continue
[254] Fix | Delete
elif toknum == DEDENT:
[255] Fix | Delete
indents.pop()
[256] Fix | Delete
continue
[257] Fix | Delete
elif toknum in (NEWLINE, NL):
[258] Fix | Delete
startline = True
[259] Fix | Delete
elif startline and indents:
[260] Fix | Delete
toks_append(indents[-1])
[261] Fix | Delete
startline = False
[262] Fix | Delete
toks_append(tokval)
[263] Fix | Delete
[264] Fix | Delete
def untokenize(iterable):
[265] Fix | Delete
"""Transform tokens back into Python source code.
[266] Fix | Delete
[267] Fix | Delete
Each element returned by the iterable must be a token sequence
[268] Fix | Delete
with at least two elements, a token number and token value. If
[269] Fix | Delete
only two tokens are passed, the resulting output is poor.
[270] Fix | Delete
[271] Fix | Delete
Round-trip invariant for full input:
[272] Fix | Delete
Untokenized source will match input source exactly
[273] Fix | Delete
[274] Fix | Delete
Round-trip invariant for limited intput:
[275] Fix | Delete
# Output text will tokenize the back to the input
[276] Fix | Delete
t1 = [tok[:2] for tok in generate_tokens(f.readline)]
[277] Fix | Delete
newcode = untokenize(t1)
[278] Fix | Delete
readline = iter(newcode.splitlines(1)).next
[279] Fix | Delete
t2 = [tok[:2] for tok in generate_tokens(readline)]
[280] Fix | Delete
assert t1 == t2
[281] Fix | Delete
"""
[282] Fix | Delete
ut = Untokenizer()
[283] Fix | Delete
return ut.untokenize(iterable)
[284] Fix | Delete
[285] Fix | Delete
def generate_tokens(readline):
[286] Fix | Delete
"""
[287] Fix | Delete
The generate_tokens() generator requires one argument, readline, which
[288] Fix | Delete
must be a callable object which provides the same interface as the
[289] Fix | Delete
readline() method of built-in file objects. Each call to the function
[290] Fix | Delete
should return one line of input as a string. Alternately, readline
[291] Fix | Delete
can be a callable function terminating with StopIteration:
[292] Fix | Delete
readline = open(myfile).next # Example of alternate readline
[293] Fix | Delete
[294] Fix | Delete
The generator produces 5-tuples with these members: the token type; the
[295] Fix | Delete
token string; a 2-tuple (srow, scol) of ints specifying the row and
[296] Fix | Delete
column where the token begins in the source; a 2-tuple (erow, ecol) of
[297] Fix | Delete
ints specifying the row and column where the token ends in the source;
[298] Fix | Delete
and the line on which the token was found. The line passed is the
[299] Fix | Delete
logical line; continuation lines are included.
[300] Fix | Delete
"""
[301] Fix | Delete
lnum = parenlev = continued = 0
[302] Fix | Delete
namechars, numchars = string.ascii_letters + '_', '0123456789'
[303] Fix | Delete
contstr, needcont = '', 0
[304] Fix | Delete
contline = None
[305] Fix | Delete
indents = [0]
[306] Fix | Delete
[307] Fix | Delete
while 1: # loop over lines in stream
[308] Fix | Delete
try:
[309] Fix | Delete
line = readline()
[310] Fix | Delete
except StopIteration:
[311] Fix | Delete
line = ''
[312] Fix | Delete
lnum += 1
[313] Fix | Delete
pos, max = 0, len(line)
[314] Fix | Delete
[315] Fix | Delete
if contstr: # continued string
[316] Fix | Delete
if not line:
[317] Fix | Delete
raise TokenError, ("EOF in multi-line string", strstart)
[318] Fix | Delete
endmatch = endprog.match(line)
[319] Fix | Delete
if endmatch:
[320] Fix | Delete
pos = end = endmatch.end(0)
[321] Fix | Delete
yield (STRING, contstr + line[:end],
[322] Fix | Delete
strstart, (lnum, end), contline + line)
[323] Fix | Delete
contstr, needcont = '', 0
[324] Fix | Delete
contline = None
[325] Fix | Delete
elif needcont and line[-2:] != '\\\n' and line[-3:] != '\\\r\n':
[326] Fix | Delete
yield (ERRORTOKEN, contstr + line,
[327] Fix | Delete
strstart, (lnum, len(line)), contline)
[328] Fix | Delete
contstr = ''
[329] Fix | Delete
contline = None
[330] Fix | Delete
continue
[331] Fix | Delete
else:
[332] Fix | Delete
contstr = contstr + line
[333] Fix | Delete
contline = contline + line
[334] Fix | Delete
continue
[335] Fix | Delete
[336] Fix | Delete
elif parenlev == 0 and not continued: # new statement
[337] Fix | Delete
if not line: break
[338] Fix | Delete
column = 0
[339] Fix | Delete
while pos < max: # measure leading whitespace
[340] Fix | Delete
if line[pos] == ' ':
[341] Fix | Delete
column += 1
[342] Fix | Delete
elif line[pos] == '\t':
[343] Fix | Delete
column = (column//tabsize + 1)*tabsize
[344] Fix | Delete
elif line[pos] == '\f':
[345] Fix | Delete
column = 0
[346] Fix | Delete
else:
[347] Fix | Delete
break
[348] Fix | Delete
pos += 1
[349] Fix | Delete
if pos == max:
[350] Fix | Delete
break
[351] Fix | Delete
[352] Fix | Delete
if line[pos] in '#\r\n': # skip comments or blank lines
[353] Fix | Delete
if line[pos] == '#':
[354] Fix | Delete
comment_token = line[pos:].rstrip('\r\n')
[355] Fix | Delete
nl_pos = pos + len(comment_token)
[356] Fix | Delete
yield (COMMENT, comment_token,
[357] Fix | Delete
(lnum, pos), (lnum, pos + len(comment_token)), line)
[358] Fix | Delete
yield (NL, line[nl_pos:],
[359] Fix | Delete
(lnum, nl_pos), (lnum, len(line)), line)
[360] Fix | Delete
else:
[361] Fix | Delete
yield ((NL, COMMENT)[line[pos] == '#'], line[pos:],
[362] Fix | Delete
(lnum, pos), (lnum, len(line)), line)
[363] Fix | Delete
continue
[364] Fix | Delete
[365] Fix | Delete
if column > indents[-1]: # count indents or dedents
[366] Fix | Delete
indents.append(column)
[367] Fix | Delete
yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line)
[368] Fix | Delete
while column < indents[-1]:
[369] Fix | Delete
if column not in indents:
[370] Fix | Delete
raise IndentationError(
[371] Fix | Delete
"unindent does not match any outer indentation level",
[372] Fix | Delete
("<tokenize>", lnum, pos, line))
[373] Fix | Delete
indents = indents[:-1]
[374] Fix | Delete
yield (DEDENT, '', (lnum, pos), (lnum, pos), line)
[375] Fix | Delete
[376] Fix | Delete
else: # continued statement
[377] Fix | Delete
if not line:
[378] Fix | Delete
raise TokenError, ("EOF in multi-line statement", (lnum, 0))
[379] Fix | Delete
continued = 0
[380] Fix | Delete
[381] Fix | Delete
while pos < max:
[382] Fix | Delete
pseudomatch = pseudoprog.match(line, pos)
[383] Fix | Delete
if pseudomatch: # scan for tokens
[384] Fix | Delete
start, end = pseudomatch.span(1)
[385] Fix | Delete
spos, epos, pos = (lnum, start), (lnum, end), end
[386] Fix | Delete
if start == end:
[387] Fix | Delete
continue
[388] Fix | Delete
token, initial = line[start:end], line[start]
[389] Fix | Delete
[390] Fix | Delete
if initial in numchars or \
[391] Fix | Delete
(initial == '.' and token != '.'): # ordinary number
[392] Fix | Delete
yield (NUMBER, token, spos, epos, line)
[393] Fix | Delete
elif initial in '\r\n':
[394] Fix | Delete
yield (NL if parenlev > 0 else NEWLINE,
[395] Fix | Delete
token, spos, epos, line)
[396] Fix | Delete
elif initial == '#':
[397] Fix | Delete
assert not token.endswith("\n")
[398] Fix | Delete
yield (COMMENT, token, spos, epos, line)
[399] Fix | Delete
elif token in triple_quoted:
[400] Fix | Delete
endprog = endprogs[token]
[401] Fix | Delete
endmatch = endprog.match(line, pos)
[402] Fix | Delete
if endmatch: # all on one line
[403] Fix | Delete
pos = endmatch.end(0)
[404] Fix | Delete
token = line[start:pos]
[405] Fix | Delete
yield (STRING, token, spos, (lnum, pos), line)
[406] Fix | Delete
else:
[407] Fix | Delete
strstart = (lnum, start) # multiple lines
[408] Fix | Delete
contstr = line[start:]
[409] Fix | Delete
contline = line
[410] Fix | Delete
break
[411] Fix | Delete
elif initial in single_quoted or \
[412] Fix | Delete
token[:2] in single_quoted or \
[413] Fix | Delete
token[:3] in single_quoted:
[414] Fix | Delete
if token[-1] == '\n': # continued string
[415] Fix | Delete
strstart = (lnum, start)
[416] Fix | Delete
endprog = (endprogs[initial] or endprogs[token[1]] or
[417] Fix | Delete
endprogs[token[2]])
[418] Fix | Delete
contstr, needcont = line[start:], 1
[419] Fix | Delete
contline = line
[420] Fix | Delete
break
[421] Fix | Delete
else: # ordinary string
[422] Fix | Delete
yield (STRING, token, spos, epos, line)
[423] Fix | Delete
elif initial in namechars: # ordinary name
[424] Fix | Delete
yield (NAME, token, spos, epos, line)
[425] Fix | Delete
elif initial == '\\': # continued stmt
[426] Fix | Delete
continued = 1
[427] Fix | Delete
else:
[428] Fix | Delete
if initial in '([{':
[429] Fix | Delete
parenlev += 1
[430] Fix | Delete
elif initial in ')]}':
[431] Fix | Delete
parenlev -= 1
[432] Fix | Delete
yield (OP, token, spos, epos, line)
[433] Fix | Delete
else:
[434] Fix | Delete
yield (ERRORTOKEN, line[pos],
[435] Fix | Delete
(lnum, pos), (lnum, pos+1), line)
[436] Fix | Delete
pos += 1
[437] Fix | Delete
[438] Fix | Delete
for indent in indents[1:]: # pop remaining indent levels
[439] Fix | Delete
yield (DEDENT, '', (lnum, 0), (lnum, 0), '')
[440] Fix | Delete
yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '')
[441] Fix | Delete
[442] Fix | Delete
if __name__ == '__main__': # testing
[443] Fix | Delete
import sys
[444] Fix | Delete
if len(sys.argv) > 1:
[445] Fix | Delete
tokenize(open(sys.argv[1]).readline)
[446] Fix | Delete
else:
[447] Fix | Delete
tokenize(sys.stdin.readline)
[448] Fix | Delete
[449] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function