blob: 690fec4921adabcc32e655a64cb5ecf03a3278dc [file] [log] [blame]
Martin v. Löwisef04c442008-03-19 05:04:44 +00001# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 Python Software Foundation.
2# All rights reserved.
3
4"""Tokenization help for Python programs.
5
6generate_tokens(readline) is a generator that breaks a stream of
7text into Python tokens. It accepts a readline-like method which is called
8repeatedly to get the next line of input (or "" for EOF). It generates
95-tuples with these members:
10
11 the token type (see token.py)
12 the token (a string)
13 the starting (row, column) indices of the token (a 2-tuple of ints)
14 the ending (row, column) indices of the token (a 2-tuple of ints)
15 the original line (string)
16
17It is designed to match the working of the Python tokenizer exactly, except
18that it produces COMMENT tokens for comments and gives type OP for all
19operators
20
21Older entry points
22 tokenize_loop(readline, tokeneater)
23 tokenize(readline, tokeneater=printtoken)
24are the same, except instead of generating tokens, tokeneater is a callback
25function to which the 5 fields described above are passed as 5 arguments,
26each time a new token is found."""
27
28__author__ = 'Ka-Ping Yee <ping@lfw.org>'
29__credits__ = \
30 'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro'
31
32import string, re
Benjamin Petersond481e3d2009-05-09 19:42:23 +000033from codecs import BOM_UTF8, lookup
Martin v. Löwisef04c442008-03-19 05:04:44 +000034from lib2to3.pgen2.token import *
35
36from . import token
37__all__ = [x for x in dir(token) if x[0] != '_'] + ["tokenize",
38 "generate_tokens", "untokenize"]
39del token
40
Benjamin Peterson8d26b0b2010-05-07 19:10:11 +000041try:
42 bytes
43except NameError:
44 # Support bytes type in Python <= 2.5, so 2to3 turns itself into
45 # valid Python 3 code.
46 bytes = str
47
Martin v. Löwisef04c442008-03-19 05:04:44 +000048def group(*choices): return '(' + '|'.join(choices) + ')'
49def any(*choices): return group(*choices) + '*'
50def maybe(*choices): return group(*choices) + '?'
51
52Whitespace = r'[ \f\t]*'
53Comment = r'#[^\r\n]*'
54Ignore = Whitespace + any(r'\\\r?\n' + Whitespace) + maybe(Comment)
55Name = r'[a-zA-Z_]\w*'
56
57Binnumber = r'0[bB][01]*'
58Hexnumber = r'0[xX][\da-fA-F]*[lL]?'
59Octnumber = r'0[oO]?[0-7]*[lL]?'
60Decnumber = r'[1-9]\d*[lL]?'
61Intnumber = group(Binnumber, Hexnumber, Octnumber, Decnumber)
62Exponent = r'[eE][-+]?\d+'
63Pointfloat = group(r'\d+\.\d*', r'\.\d+') + maybe(Exponent)
64Expfloat = r'\d+' + Exponent
65Floatnumber = group(Pointfloat, Expfloat)
66Imagnumber = group(r'\d+[jJ]', Floatnumber + r'[jJ]')
67Number = group(Imagnumber, Floatnumber, Intnumber)
68
69# Tail end of ' string.
70Single = r"[^'\\]*(?:\\.[^'\\]*)*'"
71# Tail end of " string.
72Double = r'[^"\\]*(?:\\.[^"\\]*)*"'
73# Tail end of ''' string.
74Single3 = r"[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''"
75# Tail end of """ string.
76Double3 = r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'
77Triple = group("[ubUB]?[rR]?'''", '[ubUB]?[rR]?"""')
78# Single-line ' or " string.
79String = group(r"[uU]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*'",
80 r'[uU]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*"')
81
82# Because of leftmost-then-longest match semantics, be sure to put the
83# longest operators first (e.g., if = came before ==, == would get
84# recognized as two instances of =).
85Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"<>", r"!=",
86 r"//=?", r"->",
Benjamin Peterson4ab92c82014-04-10 00:12:47 -040087 r"[+\-*/%&@|^=<>]=?",
Martin v. Löwisef04c442008-03-19 05:04:44 +000088 r"~")
89
90Bracket = '[][(){}]'
91Special = group(r'\r?\n', r'[:;.,`@]')
92Funny = group(Operator, Bracket, Special)
93
94PlainToken = group(Number, Funny, String, Name)
95Token = Ignore + PlainToken
96
97# First (or only) line of ' or " string.
98ContStr = group(r"[uUbB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*" +
99 group("'", r'\\\r?\n'),
100 r'[uUbB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' +
101 group('"', r'\\\r?\n'))
102PseudoExtras = group(r'\\\r?\n', Comment, Triple)
103PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name)
104
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +0000105tokenprog, pseudoprog, single3prog, double3prog = list(map(
106 re.compile, (Token, PseudoToken, Single3, Double3)))
Martin v. Löwisef04c442008-03-19 05:04:44 +0000107endprogs = {"'": re.compile(Single), '"': re.compile(Double),
108 "'''": single3prog, '"""': double3prog,
109 "r'''": single3prog, 'r"""': double3prog,
110 "u'''": single3prog, 'u"""': double3prog,
111 "b'''": single3prog, 'b"""': double3prog,
112 "ur'''": single3prog, 'ur"""': double3prog,
113 "br'''": single3prog, 'br"""': double3prog,
114 "R'''": single3prog, 'R"""': double3prog,
115 "U'''": single3prog, 'U"""': double3prog,
116 "B'''": single3prog, 'B"""': double3prog,
117 "uR'''": single3prog, 'uR"""': double3prog,
118 "Ur'''": single3prog, 'Ur"""': double3prog,
119 "UR'''": single3prog, 'UR"""': double3prog,
120 "bR'''": single3prog, 'bR"""': double3prog,
121 "Br'''": single3prog, 'Br"""': double3prog,
122 "BR'''": single3prog, 'BR"""': double3prog,
123 'r': None, 'R': None,
124 'u': None, 'U': None,
125 'b': None, 'B': None}
126
127triple_quoted = {}
128for t in ("'''", '"""',
129 "r'''", 'r"""', "R'''", 'R"""',
130 "u'''", 'u"""', "U'''", 'U"""',
131 "b'''", 'b"""', "B'''", 'B"""',
132 "ur'''", 'ur"""', "Ur'''", 'Ur"""',
133 "uR'''", 'uR"""', "UR'''", 'UR"""',
134 "br'''", 'br"""', "Br'''", 'Br"""',
135 "bR'''", 'bR"""', "BR'''", 'BR"""',):
136 triple_quoted[t] = t
137single_quoted = {}
138for t in ("'", '"',
139 "r'", 'r"', "R'", 'R"',
140 "u'", 'u"', "U'", 'U"',
141 "b'", 'b"', "B'", 'B"',
142 "ur'", 'ur"', "Ur'", 'Ur"',
143 "uR'", 'uR"', "UR'", 'UR"',
144 "br'", 'br"', "Br'", 'Br"',
145 "bR'", 'bR"', "BR'", 'BR"', ):
146 single_quoted[t] = t
147
148tabsize = 8
149
150class TokenError(Exception): pass
151
152class StopTokenizing(Exception): pass
153
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +0000154def printtoken(type, token, xxx_todo_changeme, xxx_todo_changeme1, line): # for testing
155 (srow, scol) = xxx_todo_changeme
156 (erow, ecol) = xxx_todo_changeme1
157 print("%d,%d-%d,%d:\t%s\t%s" % \
158 (srow, scol, erow, ecol, tok_name[type], repr(token)))
Martin v. Löwisef04c442008-03-19 05:04:44 +0000159
160def tokenize(readline, tokeneater=printtoken):
161 """
162 The tokenize() function accepts two parameters: one representing the
163 input stream, and one providing an output mechanism for tokenize().
164
165 The first parameter, readline, must be a callable object which provides
166 the same interface as the readline() method of built-in file objects.
167 Each call to the function should return one line of input as a string.
168
169 The second parameter, tokeneater, must also be a callable object. It is
170 called once for each token, with five arguments, corresponding to the
171 tuples generated by generate_tokens().
172 """
173 try:
174 tokenize_loop(readline, tokeneater)
175 except StopTokenizing:
176 pass
177
178# backwards compatible interface
179def tokenize_loop(readline, tokeneater):
180 for token_info in generate_tokens(readline):
181 tokeneater(*token_info)
182
183class Untokenizer:
184
185 def __init__(self):
186 self.tokens = []
187 self.prev_row = 1
188 self.prev_col = 0
189
190 def add_whitespace(self, start):
191 row, col = start
192 assert row <= self.prev_row
193 col_offset = col - self.prev_col
194 if col_offset:
195 self.tokens.append(" " * col_offset)
196
197 def untokenize(self, iterable):
198 for t in iterable:
199 if len(t) == 2:
200 self.compat(t, iterable)
201 break
202 tok_type, token, start, end, line = t
203 self.add_whitespace(start)
204 self.tokens.append(token)
205 self.prev_row, self.prev_col = end
206 if tok_type in (NEWLINE, NL):
207 self.prev_row += 1
208 self.prev_col = 0
209 return "".join(self.tokens)
210
211 def compat(self, token, iterable):
212 startline = False
213 indents = []
214 toks_append = self.tokens.append
215 toknum, tokval = token
216 if toknum in (NAME, NUMBER):
217 tokval += ' '
218 if toknum in (NEWLINE, NL):
219 startline = True
220 for tok in iterable:
221 toknum, tokval = tok[:2]
222
Yury Selivanov75445082015-05-11 22:57:16 -0400223 if toknum in (NAME, NUMBER, ASYNC, AWAIT):
Martin v. Löwisef04c442008-03-19 05:04:44 +0000224 tokval += ' '
225
226 if toknum == INDENT:
227 indents.append(tokval)
228 continue
229 elif toknum == DEDENT:
230 indents.pop()
231 continue
232 elif toknum in (NEWLINE, NL):
233 startline = True
234 elif startline and indents:
235 toks_append(indents[-1])
236 startline = False
237 toks_append(tokval)
238
Serhiy Storchakadafea852013-09-16 23:51:56 +0300239cookie_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII)
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200240blank_re = re.compile(br'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII)
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000241
Benjamin Petersond9af52b2009-11-02 18:16:28 +0000242def _get_normal_name(orig_enc):
243 """Imitates get_normal_name in tokenizer.c."""
244 # Only care about the first 12 characters.
245 enc = orig_enc[:12].lower().replace("_", "-")
246 if enc == "utf-8" or enc.startswith("utf-8-"):
247 return "utf-8"
248 if enc in ("latin-1", "iso-8859-1", "iso-latin-1") or \
249 enc.startswith(("latin-1-", "iso-8859-1-", "iso-latin-1-")):
250 return "iso-8859-1"
251 return orig_enc
252
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000253def detect_encoding(readline):
254 """
255 The detect_encoding() function is used to detect the encoding that should
Ezio Melotti4bcc7962013-11-25 05:14:51 +0200256 be used to decode a Python source file. It requires one argument, readline,
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000257 in the same way as the tokenize() generator.
258
259 It will call readline a maximum of twice, and return the encoding used
260 (as a string) and a list of any lines (left as bytes) it has read
261 in.
262
263 It detects the encoding from the presence of a utf-8 bom or an encoding
Benjamin Peterson0af93982010-03-23 03:22:05 +0000264 cookie as specified in pep-0263. If both a bom and a cookie are present, but
265 disagree, a SyntaxError will be raised. If the encoding cookie is an invalid
266 charset, raise a SyntaxError. Note that if a utf-8 bom is found,
267 'utf-8-sig' is returned.
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000268
269 If no encoding is specified, then the default of 'utf-8' will be returned.
270 """
271 bom_found = False
272 encoding = None
Benjamin Peterson0af93982010-03-23 03:22:05 +0000273 default = 'utf-8'
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000274 def read_or_stop():
275 try:
276 return readline()
277 except StopIteration:
Benjamin Peterson8d26b0b2010-05-07 19:10:11 +0000278 return bytes()
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000279
280 def find_cookie(line):
281 try:
282 line_string = line.decode('ascii')
283 except UnicodeDecodeError:
284 return None
Serhiy Storchakadafea852013-09-16 23:51:56 +0300285 match = cookie_re.match(line_string)
286 if not match:
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000287 return None
Serhiy Storchakadafea852013-09-16 23:51:56 +0300288 encoding = _get_normal_name(match.group(1))
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000289 try:
290 codec = lookup(encoding)
291 except LookupError:
292 # This behaviour mimics the Python interpreter
293 raise SyntaxError("unknown encoding: " + encoding)
294
Benjamin Peterson20211002009-11-25 18:34:42 +0000295 if bom_found:
296 if codec.name != 'utf-8':
297 # This behaviour mimics the Python interpreter
298 raise SyntaxError('encoding problem: utf-8')
Benjamin Peterson0af93982010-03-23 03:22:05 +0000299 encoding += '-sig'
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000300 return encoding
301
302 first = read_or_stop()
303 if first.startswith(BOM_UTF8):
304 bom_found = True
305 first = first[3:]
Benjamin Peterson0af93982010-03-23 03:22:05 +0000306 default = 'utf-8-sig'
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000307 if not first:
Benjamin Peterson0af93982010-03-23 03:22:05 +0000308 return default, []
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000309
310 encoding = find_cookie(first)
311 if encoding:
312 return encoding, [first]
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200313 if not blank_re.match(first):
314 return default, [first]
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000315
316 second = read_or_stop()
317 if not second:
Benjamin Peterson0af93982010-03-23 03:22:05 +0000318 return default, [first]
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000319
320 encoding = find_cookie(second)
321 if encoding:
322 return encoding, [first, second]
323
Benjamin Peterson0af93982010-03-23 03:22:05 +0000324 return default, [first, second]
Benjamin Petersond481e3d2009-05-09 19:42:23 +0000325
Martin v. Löwisef04c442008-03-19 05:04:44 +0000326def untokenize(iterable):
327 """Transform tokens back into Python source code.
328
329 Each element returned by the iterable must be a token sequence
330 with at least two elements, a token number and token value. If
331 only two tokens are passed, the resulting output is poor.
332
333 Round-trip invariant for full input:
334 Untokenized source will match input source exactly
335
336 Round-trip invariant for limited intput:
337 # Output text will tokenize the back to the input
338 t1 = [tok[:2] for tok in generate_tokens(f.readline)]
339 newcode = untokenize(t1)
340 readline = iter(newcode.splitlines(1)).next
341 t2 = [tok[:2] for tokin generate_tokens(readline)]
342 assert t1 == t2
343 """
344 ut = Untokenizer()
345 return ut.untokenize(iterable)
346
347def generate_tokens(readline):
348 """
Ezio Melotti4bcc7962013-11-25 05:14:51 +0200349 The generate_tokens() generator requires one argument, readline, which
Martin v. Löwisef04c442008-03-19 05:04:44 +0000350 must be a callable object which provides the same interface as the
351 readline() method of built-in file objects. Each call to the function
352 should return one line of input as a string. Alternately, readline
353 can be a callable function terminating with StopIteration:
354 readline = open(myfile).next # Example of alternate readline
355
356 The generator produces 5-tuples with these members: the token type; the
357 token string; a 2-tuple (srow, scol) of ints specifying the row and
358 column where the token begins in the source; a 2-tuple (erow, ecol) of
359 ints specifying the row and column where the token ends in the source;
360 and the line on which the token was found. The line passed is the
361 logical line; continuation lines are included.
362 """
363 lnum = parenlev = continued = 0
364 namechars, numchars = string.ascii_letters + '_', '0123456789'
365 contstr, needcont = '', 0
366 contline = None
367 indents = [0]
368
Yury Selivanov75445082015-05-11 22:57:16 -0400369 # 'stashed' and 'ctx' are used for async/await parsing
370 stashed = None
371 ctx = [('sync', 0)]
372
Martin v. Löwisef04c442008-03-19 05:04:44 +0000373 while 1: # loop over lines in stream
374 try:
375 line = readline()
376 except StopIteration:
377 line = ''
378 lnum = lnum + 1
379 pos, max = 0, len(line)
380
381 if contstr: # continued string
382 if not line:
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +0000383 raise TokenError("EOF in multi-line string", strstart)
Martin v. Löwisef04c442008-03-19 05:04:44 +0000384 endmatch = endprog.match(line)
385 if endmatch:
386 pos = end = endmatch.end(0)
387 yield (STRING, contstr + line[:end],
388 strstart, (lnum, end), contline + line)
389 contstr, needcont = '', 0
390 contline = None
391 elif needcont and line[-2:] != '\\\n' and line[-3:] != '\\\r\n':
392 yield (ERRORTOKEN, contstr + line,
393 strstart, (lnum, len(line)), contline)
394 contstr = ''
395 contline = None
396 continue
397 else:
398 contstr = contstr + line
399 contline = contline + line
400 continue
401
402 elif parenlev == 0 and not continued: # new statement
403 if not line: break
404 column = 0
405 while pos < max: # measure leading whitespace
406 if line[pos] == ' ': column = column + 1
Benjamin Petersond9af52b2009-11-02 18:16:28 +0000407 elif line[pos] == '\t': column = (column//tabsize + 1)*tabsize
Martin v. Löwisef04c442008-03-19 05:04:44 +0000408 elif line[pos] == '\f': column = 0
409 else: break
410 pos = pos + 1
411 if pos == max: break
412
Yury Selivanov75445082015-05-11 22:57:16 -0400413 if stashed:
414 yield stashed
415 stashed = None
416
Martin v. Löwisef04c442008-03-19 05:04:44 +0000417 if line[pos] in '#\r\n': # skip comments or blank lines
418 if line[pos] == '#':
419 comment_token = line[pos:].rstrip('\r\n')
420 nl_pos = pos + len(comment_token)
421 yield (COMMENT, comment_token,
422 (lnum, pos), (lnum, pos + len(comment_token)), line)
423 yield (NL, line[nl_pos:],
424 (lnum, nl_pos), (lnum, len(line)), line)
425 else:
426 yield ((NL, COMMENT)[line[pos] == '#'], line[pos:],
427 (lnum, pos), (lnum, len(line)), line)
428 continue
429
430 if column > indents[-1]: # count indents or dedents
431 indents.append(column)
432 yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line)
433 while column < indents[-1]:
434 if column not in indents:
435 raise IndentationError(
436 "unindent does not match any outer indentation level",
437 ("<tokenize>", lnum, pos, line))
438 indents = indents[:-1]
439 yield (DEDENT, '', (lnum, pos), (lnum, pos), line)
440
441 else: # continued statement
442 if not line:
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +0000443 raise TokenError("EOF in multi-line statement", (lnum, 0))
Martin v. Löwisef04c442008-03-19 05:04:44 +0000444 continued = 0
445
446 while pos < max:
447 pseudomatch = pseudoprog.match(line, pos)
448 if pseudomatch: # scan for tokens
449 start, end = pseudomatch.span(1)
450 spos, epos, pos = (lnum, start), (lnum, end), end
451 token, initial = line[start:end], line[start]
452
453 if initial in numchars or \
454 (initial == '.' and token != '.'): # ordinary number
455 yield (NUMBER, token, spos, epos, line)
456 elif initial in '\r\n':
457 newline = NEWLINE
458 if parenlev > 0:
459 newline = NL
Yury Selivanov75445082015-05-11 22:57:16 -0400460 if stashed:
461 yield stashed
462 stashed = None
Martin v. Löwisef04c442008-03-19 05:04:44 +0000463 yield (newline, token, spos, epos, line)
464 elif initial == '#':
465 assert not token.endswith("\n")
Yury Selivanov75445082015-05-11 22:57:16 -0400466 if stashed:
467 yield stashed
468 stashed = None
Martin v. Löwisef04c442008-03-19 05:04:44 +0000469 yield (COMMENT, token, spos, epos, line)
470 elif token in triple_quoted:
471 endprog = endprogs[token]
472 endmatch = endprog.match(line, pos)
473 if endmatch: # all on one line
474 pos = endmatch.end(0)
475 token = line[start:pos]
Yury Selivanov75445082015-05-11 22:57:16 -0400476 if stashed:
477 yield stashed
478 stashed = None
Martin v. Löwisef04c442008-03-19 05:04:44 +0000479 yield (STRING, token, spos, (lnum, pos), line)
480 else:
481 strstart = (lnum, start) # multiple lines
482 contstr = line[start:]
483 contline = line
484 break
485 elif initial in single_quoted or \
486 token[:2] in single_quoted or \
487 token[:3] in single_quoted:
488 if token[-1] == '\n': # continued string
489 strstart = (lnum, start)
490 endprog = (endprogs[initial] or endprogs[token[1]] or
491 endprogs[token[2]])
492 contstr, needcont = line[start:], 1
493 contline = line
494 break
495 else: # ordinary string
Yury Selivanov75445082015-05-11 22:57:16 -0400496 if stashed:
497 yield stashed
498 stashed = None
Martin v. Löwisef04c442008-03-19 05:04:44 +0000499 yield (STRING, token, spos, epos, line)
500 elif initial in namechars: # ordinary name
Yury Selivanov75445082015-05-11 22:57:16 -0400501 if token in ('async', 'await'):
502 if ctx[-1][0] == 'async' and ctx[-1][1] < indents[-1]:
503 yield (ASYNC if token == 'async' else AWAIT,
504 token, spos, epos, line)
505 continue
506
507 tok = (NAME, token, spos, epos, line)
508 if token == 'async' and not stashed:
509 stashed = tok
510 continue
511
512 if token == 'def':
513 if (stashed
514 and stashed[0] == NAME
515 and stashed[1] == 'async'):
516
517 ctx.append(('async', indents[-1]))
518
519 yield (ASYNC, stashed[1],
520 stashed[2], stashed[3],
521 stashed[4])
522 stashed = None
523 else:
524 ctx.append(('sync', indents[-1]))
525
526 if stashed:
527 yield stashed
528 stashed = None
529
530 yield tok
Martin v. Löwisef04c442008-03-19 05:04:44 +0000531 elif initial == '\\': # continued stmt
532 # This yield is new; needed for better idempotency:
Yury Selivanov75445082015-05-11 22:57:16 -0400533 if stashed:
534 yield stashed
535 stashed = None
Martin v. Löwisef04c442008-03-19 05:04:44 +0000536 yield (NL, token, spos, (lnum, pos), line)
537 continued = 1
538 else:
539 if initial in '([{': parenlev = parenlev + 1
540 elif initial in ')]}': parenlev = parenlev - 1
Yury Selivanov75445082015-05-11 22:57:16 -0400541 if stashed:
542 yield stashed
543 stashed = None
Martin v. Löwisef04c442008-03-19 05:04:44 +0000544 yield (OP, token, spos, epos, line)
545 else:
546 yield (ERRORTOKEN, line[pos],
547 (lnum, pos), (lnum, pos+1), line)
548 pos = pos + 1
549
Yury Selivanov75445082015-05-11 22:57:16 -0400550 if stashed:
551 yield stashed
552 stashed = None
553
Martin v. Löwisef04c442008-03-19 05:04:44 +0000554 for indent in indents[1:]: # pop remaining indent levels
555 yield (DEDENT, '', (lnum, 0), (lnum, 0), '')
556 yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '')
557
558if __name__ == '__main__': # testing
559 import sys
560 if len(sys.argv) > 1: tokenize(open(sys.argv[1]).readline)
561 else: tokenize(sys.stdin.readline)