PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2.7/tokenize.py

https://bitbucket.org/pypy/pypy/
Python | 432 lines | 392 code | 19 blank | 21 comment | 22 complexity | 4d5dfd046504f13b264041e4d16682b9 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  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. for t in it:
  172. if len(t) == 2:
  173. self.compat(t, it)
  174. break
  175. tok_type, token, start, end, line = t
  176. if tok_type == ENDMARKER:
  177. break
  178. self.add_whitespace(start)
  179. self.tokens.append(token)
  180. self.prev_row, self.prev_col = end
  181. if tok_type in (NEWLINE, NL):
  182. self.prev_row += 1
  183. self.prev_col = 0
  184. return "".join(self.tokens)
  185. def compat(self, token, iterable):
  186. indents = []
  187. toks_append = self.tokens.append
  188. startline = token[0] in (NEWLINE, NL)
  189. prevstring = False
  190. for tok in chain([token], iterable):
  191. toknum, tokval = tok[:2]
  192. if toknum in (NAME, NUMBER):
  193. tokval += ' '
  194. # Insert a space between two consecutive strings
  195. if toknum == STRING:
  196. if prevstring:
  197. tokval = ' ' + tokval
  198. prevstring = True
  199. else:
  200. prevstring = False
  201. if toknum == INDENT:
  202. indents.append(tokval)
  203. continue
  204. elif toknum == DEDENT:
  205. indents.pop()
  206. continue
  207. elif toknum in (NEWLINE, NL):
  208. startline = True
  209. elif startline and indents:
  210. toks_append(indents[-1])
  211. startline = False
  212. toks_append(tokval)
  213. def untokenize(iterable):
  214. """Transform tokens back into Python source code.
  215. Each element returned by the iterable must be a token sequence
  216. with at least two elements, a token number and token value. If
  217. only two tokens are passed, the resulting output is poor.
  218. Round-trip invariant for full input:
  219. Untokenized source will match input source exactly
  220. Round-trip invariant for limited intput:
  221. # Output text will tokenize the back to the input
  222. t1 = [tok[:2] for tok in generate_tokens(f.readline)]
  223. newcode = untokenize(t1)
  224. readline = iter(newcode.splitlines(1)).next
  225. t2 = [tok[:2] for tok in generate_tokens(readline)]
  226. assert t1 == t2
  227. """
  228. ut = Untokenizer()
  229. return ut.untokenize(iterable)
  230. def generate_tokens(readline):
  231. """
  232. The generate_tokens() generator requires one argument, readline, which
  233. must be a callable object which provides the same interface as the
  234. readline() method of built-in file objects. Each call to the function
  235. should return one line of input as a string. Alternately, readline
  236. can be a callable function terminating with StopIteration:
  237. readline = open(myfile).next # Example of alternate readline
  238. The generator produces 5-tuples with these members: the token type; the
  239. token string; a 2-tuple (srow, scol) of ints specifying the row and
  240. column where the token begins in the source; a 2-tuple (erow, ecol) of
  241. ints specifying the row and column where the token ends in the source;
  242. and the line on which the token was found. The line passed is the
  243. logical line; continuation lines are included.
  244. """
  245. lnum = parenlev = continued = 0
  246. namechars, numchars = string.ascii_letters + '_', '0123456789'
  247. contstr, needcont = '', 0
  248. contline = None
  249. indents = [0]
  250. while 1: # loop over lines in stream
  251. try:
  252. line = readline()
  253. except StopIteration:
  254. line = ''
  255. lnum += 1
  256. pos, max = 0, len(line)
  257. if contstr: # continued string
  258. if not line:
  259. raise TokenError, ("EOF in multi-line string", strstart)
  260. endmatch = endprog.match(line)
  261. if endmatch:
  262. pos = end = endmatch.end(0)
  263. yield (STRING, contstr + line[:end],
  264. strstart, (lnum, end), contline + line)
  265. contstr, needcont = '', 0
  266. contline = None
  267. elif needcont and line[-2:] != '\\\n' and line[-3:] != '\\\r\n':
  268. yield (ERRORTOKEN, contstr + line,
  269. strstart, (lnum, len(line)), contline)
  270. contstr = ''
  271. contline = None
  272. continue
  273. else:
  274. contstr = contstr + line
  275. contline = contline + line
  276. continue
  277. elif parenlev == 0 and not continued: # new statement
  278. if not line: break
  279. column = 0
  280. while pos < max: # measure leading whitespace
  281. if line[pos] == ' ':
  282. column += 1
  283. elif line[pos] == '\t':
  284. column = (column//tabsize + 1)*tabsize
  285. elif line[pos] == '\f':
  286. column = 0
  287. else:
  288. break
  289. pos += 1
  290. if pos == max:
  291. break
  292. if line[pos] in '#\r\n': # skip comments or blank lines
  293. if line[pos] == '#':
  294. comment_token = line[pos:].rstrip('\r\n')
  295. nl_pos = pos + len(comment_token)
  296. yield (COMMENT, comment_token,
  297. (lnum, pos), (lnum, pos + len(comment_token)), line)
  298. yield (NL, line[nl_pos:],
  299. (lnum, nl_pos), (lnum, len(line)), line)
  300. else:
  301. yield ((NL, COMMENT)[line[pos] == '#'], line[pos:],
  302. (lnum, pos), (lnum, len(line)), line)
  303. continue
  304. if column > indents[-1]: # count indents or dedents
  305. indents.append(column)
  306. yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line)
  307. while column < indents[-1]:
  308. if column not in indents:
  309. raise IndentationError(
  310. "unindent does not match any outer indentation level",
  311. ("<tokenize>", lnum, pos, line))
  312. indents = indents[:-1]
  313. yield (DEDENT, '', (lnum, pos), (lnum, pos), line)
  314. else: # continued statement
  315. if not line:
  316. raise TokenError, ("EOF in multi-line statement", (lnum, 0))
  317. continued = 0
  318. while pos < max:
  319. pseudomatch = pseudoprog.match(line, pos)
  320. if pseudomatch: # scan for tokens
  321. start, end = pseudomatch.span(1)
  322. spos, epos, pos = (lnum, start), (lnum, end), end
  323. if start == end:
  324. continue
  325. token, initial = line[start:end], line[start]
  326. if initial in numchars or \
  327. (initial == '.' and token != '.'): # ordinary number
  328. yield (NUMBER, token, spos, epos, line)
  329. elif initial in '\r\n':
  330. yield (NL if parenlev > 0 else NEWLINE,
  331. token, spos, epos, line)
  332. elif initial == '#':
  333. assert not token.endswith("\n")
  334. yield (COMMENT, token, spos, epos, line)
  335. elif token in triple_quoted:
  336. endprog = endprogs[token]
  337. endmatch = endprog.match(line, pos)
  338. if endmatch: # all on one line
  339. pos = endmatch.end(0)
  340. token = line[start:pos]
  341. yield (STRING, token, spos, (lnum, pos), line)
  342. else:
  343. strstart = (lnum, start) # multiple lines
  344. contstr = line[start:]
  345. contline = line
  346. break
  347. elif initial in single_quoted or \
  348. token[:2] in single_quoted or \
  349. token[:3] in single_quoted:
  350. if token[-1] == '\n': # continued string
  351. strstart = (lnum, start)
  352. endprog = (endprogs[initial] or endprogs[token[1]] or
  353. endprogs[token[2]])
  354. contstr, needcont = line[start:], 1
  355. contline = line
  356. break
  357. else: # ordinary string
  358. yield (STRING, token, spos, epos, line)
  359. elif initial in namechars: # ordinary name
  360. yield (NAME, token, spos, epos, line)
  361. elif initial == '\\': # continued stmt
  362. continued = 1
  363. else:
  364. if initial in '([{':
  365. parenlev += 1
  366. elif initial in ')]}':
  367. parenlev -= 1
  368. yield (OP, token, spos, epos, line)
  369. else:
  370. yield (ERRORTOKEN, line[pos],
  371. (lnum, pos), (lnum, pos+1), line)
  372. pos += 1
  373. for indent in indents[1:]: # pop remaining indent levels
  374. yield (DEDENT, '', (lnum, 0), (lnum, 0), '')
  375. yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '')
  376. if __name__ == '__main__': # testing
  377. import sys
  378. if len(sys.argv) > 1:
  379. tokenize(open(sys.argv[1]).readline)
  380. else:
  381. tokenize(sys.stdin.readline)