MCPcopy Index your code
hub / github.com/python/cpython / _process_tokens

Function _process_tokens

Lib/tabnanny.py:280–332  ·  view source on GitHub ↗
(tokens)

Source from the content-addressed store, hash-verified

278 raise NannyNag(e.lineno, e.msg, e.text)
279
280def _process_tokens(tokens):
281 INDENT = tokenize.INDENT
282 DEDENT = tokenize.DEDENT
283 NEWLINE = tokenize.NEWLINE
284 JUNK = tokenize.COMMENT, tokenize.NL
285 indents = [Whitespace("")]
286 check_equal = 0
287
288 for (type, token, start, end, line) in tokens:
289 if type == NEWLINE:
290 # a program statement, or ENDMARKER, will eventually follow,
291 # after some (possibly empty) run of tokens of the form
292 # (NL | COMMENT)* (INDENT | DEDENT+)?
293 # If an INDENT appears, setting check_equal is wrong, and will
294 # be undone when we see the INDENT.
295 check_equal = 1
296
297 elif type == INDENT:
298 check_equal = 0
299 thisguy = Whitespace(token)
300 if not indents[-1].less(thisguy):
301 witness = indents[-1].not_less_witness(thisguy)
302 msg = "indent not greater e.g. " + format_witnesses(witness)
303 raise NannyNag(start[0], msg, line)
304 indents.append(thisguy)
305
306 elif type == DEDENT:
307 # there's nothing we need to check here! what's important is
308 # that when the run of DEDENTs ends, the indentation of the
309 # program statement (or ENDMARKER) that triggered the run is
310 # equal to what's left at the top of the indents stack
311
312 # Ouch! This assert triggers if the last line of the source
313 # is indented *and* lacks a newline -- then DEDENTs pop out
314 # of thin air.
315 # assert check_equal # else no earlier NEWLINE, or an earlier INDENT
316 check_equal = 1
317
318 del indents[-1]
319
320 elif check_equal and type not in JUNK:
321 # this is the first "real token" following a NEWLINE, so it
322 # must be the first token of the next program statement, or an
323 # ENDMARKER; the "line" argument exposes the leading whitespace
324 # for this statement; in the case of ENDMARKER, line is an empty
325 # string, so will properly match the empty string with which the
326 # "indents" stack was seeded
327 check_equal = 0
328 thisguy = Whitespace(line)
329 if not indents[-1].equal(thisguy):
330 witness = indents[-1].not_equal_witness(thisguy)
331 msg = "indent not equal e.g. " + format_witnesses(witness)
332 raise NannyNag(start[0], msg, line)
333
334
335def __getattr__(name):

Callers 1

process_tokensFunction · 0.85

Calls 8

WhitespaceClass · 0.85
format_witnessesFunction · 0.85
NannyNagClass · 0.85
lessMethod · 0.80
not_less_witnessMethod · 0.80
equalMethod · 0.80
not_equal_witnessMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…