tokenize.py 17 KB

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