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

Function tokenize

Tools/cases_generator/lexer.py:294–359  ·  view source on GitHub ↗
(src: str, line: int = 1, filename: str = "")

Source from the content-addressed store, hash-verified

292
293
294def tokenize(src: str, line: int = 1, filename: str = "") -> Iterator[Token]:
295 linestart = -1
296 for m in matcher.finditer(src):
297 start, end = m.span()
298 macro_body = ""
299 text = m.group(0)
300 if text in keywords:
301 kind = keywords[text]
302 elif text in annotations:
303 kind = ANNOTATION
304 elif letter.match(text):
305 kind = IDENTIFIER
306 elif text == "...":
307 kind = ELLIPSIS
308 elif text == ".":
309 kind = PERIOD
310 elif text[0] in "0123456789.":
311 kind = NUMBER
312 elif text[0] == '"':
313 kind = STRING
314 elif text in opmap:
315 kind = opmap[text]
316 elif text == "\n":
317 linestart = start
318 line += 1
319 kind = "\n"
320 elif text[0] == "'":
321 kind = CHARACTER
322 elif text[0] == "#":
323 macro_body = text[1:].strip()
324 if macro_body.startswith("if"):
325 kind = CMACRO_IF
326 elif macro_body.startswith("else"):
327 kind = CMACRO_ELSE
328 elif macro_body.startswith("endif"):
329 kind = CMACRO_ENDIF
330 else:
331 kind = CMACRO_OTHER
332 elif text[0] == "/" and text[1] in "/*":
333 kind = COMMENT
334 else:
335 lineend = src.find("\n", start)
336 if lineend == -1:
337 lineend = len(src)
338 raise make_syntax_error(
339 f"Bad token: {text}",
340 filename,
341 line,
342 start - linestart + 1,
343 src[linestart:lineend],
344 )
345 if kind == COMMENT:
346 begin = line, start - linestart
347 newlines = text.count("\n")
348 if newlines:
349 linestart = start + text.rfind("\n")
350 line += newlines
351 else:

Callers 1

lexer.pyFile · 0.70

Calls 9

make_syntax_errorFunction · 0.85
TokenClass · 0.70
groupMethod · 0.45
matchMethod · 0.45
stripMethod · 0.45
startswithMethod · 0.45
findMethod · 0.45
countMethod · 0.45
rfindMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…