You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

577 lines
16 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. codegen
  4. ~~~~~~~
  5. Extension to ast that allow ast -> python code generation.
  6. :copyright: Copyright 2008 by Armin Ronacher.
  7. :license: BSD.
  8. """
  9. from ast import *
  10. BOOLOP_SYMBOLS = {
  11. And: 'and',
  12. Or: 'or'
  13. }
  14. BINOP_SYMBOLS = {
  15. Add: '+',
  16. Sub: '-',
  17. Mult: '*',
  18. Div: '/',
  19. FloorDiv: '//',
  20. Mod: '%',
  21. LShift: '<<',
  22. RShift: '>>',
  23. BitOr: '|',
  24. BitAnd: '&',
  25. BitXor: '^'
  26. }
  27. CMPOP_SYMBOLS = {
  28. Eq: '==',
  29. Gt: '>',
  30. GtE: '>=',
  31. In: 'in',
  32. Is: 'is',
  33. IsNot: 'is not',
  34. Lt: '<',
  35. LtE: '<=',
  36. NotEq: '!=',
  37. NotIn: 'not in'
  38. }
  39. UNARYOP_SYMBOLS = {
  40. Invert: '~',
  41. Not: 'not',
  42. UAdd: '+',
  43. USub: '-'
  44. }
  45. ALL_SYMBOLS = {}
  46. ALL_SYMBOLS.update(BOOLOP_SYMBOLS)
  47. ALL_SYMBOLS.update(BINOP_SYMBOLS)
  48. ALL_SYMBOLS.update(CMPOP_SYMBOLS)
  49. ALL_SYMBOLS.update(UNARYOP_SYMBOLS)
  50. def to_source(node, indent_with=' ' * 4, add_line_information=False):
  51. """This function can convert a node tree back into python sourcecode.
  52. This is useful for debugging purposes, especially if you're dealing with
  53. custom asts not generated by python itself.
  54. It could be that the sourcecode is evaluable when the AST itself is not
  55. compilable / evaluable. The reason for this is that the AST contains some
  56. more data than regular sourcecode does, which is dropped during
  57. conversion.
  58. Each level of indentation is replaced with `indent_with`. Per default this
  59. parameter is equal to four spaces as suggested by PEP 8, but it might be
  60. adjusted to match the application's styleguide.
  61. If `add_line_information` is set to `True` comments for the line numbers
  62. of the nodes are added to the output. This can be used to spot wrong line
  63. number information of statement nodes.
  64. """
  65. generator = SourceGenerator(indent_with, add_line_information)
  66. generator.visit(node)
  67. return ''.join(str(s) for s in generator.result)
  68. class SourceGenerator(NodeVisitor):
  69. """This visitor is able to transform a well formed syntax tree into python
  70. sourcecode. For more details have a look at the docstring of the
  71. `node_to_source` function.
  72. """
  73. def __init__(self, indent_with, add_line_information=False):
  74. self.result = []
  75. self.indent_with = indent_with
  76. self.add_line_information = add_line_information
  77. self.indentation = 0
  78. self.new_lines = 0
  79. def write(self, x):
  80. if self.new_lines:
  81. if self.result:
  82. self.result.append('\n' * self.new_lines)
  83. self.result.append(self.indent_with * self.indentation)
  84. self.new_lines = 0
  85. self.result.append(x)
  86. def newline(self, node=None, extra=0):
  87. self.new_lines = max(self.new_lines, 1 + extra)
  88. if node is not None and self.add_line_information:
  89. self.write('# line: %s' % node.lineno)
  90. self.new_lines = 1
  91. def body(self, statements):
  92. self.new_line = True
  93. self.indentation += 1
  94. for stmt in statements:
  95. self.visit(stmt)
  96. self.indentation -= 1
  97. def body_or_else(self, node):
  98. self.body(node.body)
  99. if node.orelse:
  100. self.newline()
  101. self.write('else:')
  102. self.body(node.orelse)
  103. def signature(self, node):
  104. want_comma = []
  105. def write_comma():
  106. if want_comma:
  107. self.write(', ')
  108. else:
  109. want_comma.append(True)
  110. padding = [None] * (len(node.args) - len(node.defaults))
  111. for arg, default in zip(node.args, padding + node.defaults):
  112. write_comma()
  113. self.visit(arg)
  114. if default is not None:
  115. self.write('=')
  116. self.visit(default)
  117. if node.vararg is not None:
  118. write_comma()
  119. self.write('*' + node.vararg)
  120. if node.kwarg is not None:
  121. write_comma()
  122. self.write('**' + node.kwarg)
  123. def decorators(self, node):
  124. for decorator in node.decorator_list:
  125. self.newline(decorator)
  126. self.write('@')
  127. self.visit(decorator)
  128. # Statements
  129. def visit_Assign(self, node):
  130. self.newline(node)
  131. for idx, target in enumerate(node.targets):
  132. if idx:
  133. self.write(', ')
  134. self.visit(target)
  135. self.write(' = ')
  136. self.visit(node.value)
  137. def visit_AugAssign(self, node):
  138. self.newline(node)
  139. self.visit(node.target)
  140. self.write(BINOP_SYMBOLS[type(node.op)] + '=')
  141. self.visit(node.value)
  142. def visit_ImportFrom(self, node):
  143. self.newline(node)
  144. self.write('from %s%s import ' % ('.' * node.level, node.module))
  145. for idx, item in enumerate(node.names):
  146. if idx:
  147. self.write(', ')
  148. self.write(item.name)
  149. def visit_Import(self, node):
  150. self.newline(node)
  151. for item in node.names:
  152. self.write('import ')
  153. self.visit(item)
  154. def visit_Expr(self, node):
  155. self.newline(node)
  156. self.generic_visit(node)
  157. def visit_FunctionDef(self, node):
  158. self.newline(extra=1)
  159. self.decorators(node)
  160. self.newline(node)
  161. self.write('def %s(' % node.name)
  162. self.signature(node.args)
  163. self.write('):')
  164. self.body(node.body)
  165. def visit_ClassDef(self, node):
  166. have_args = []
  167. def paren_or_comma():
  168. if have_args:
  169. self.write(', ')
  170. else:
  171. have_args.append(True)
  172. self.write('(')
  173. self.newline(extra=2)
  174. self.decorators(node)
  175. self.newline(node)
  176. self.write('class %s' % node.name)
  177. for base in node.bases:
  178. paren_or_comma()
  179. self.visit(base)
  180. # XXX: the if here is used to keep this module compatible
  181. # with python 2.6.
  182. if hasattr(node, 'keywords'):
  183. for keyword in node.keywords:
  184. paren_or_comma()
  185. self.write(keyword.arg + '=')
  186. self.visit(keyword.value)
  187. if node.starargs is not None:
  188. paren_or_comma()
  189. self.write('*')
  190. self.visit(node.starargs)
  191. if node.kwargs is not None:
  192. paren_or_comma()
  193. self.write('**')
  194. self.visit(node.kwargs)
  195. self.write(have_args and '):' or ':')
  196. self.body(node.body)
  197. def visit_If(self, node):
  198. self.newline(node)
  199. self.write('if ')
  200. self.visit(node.test)
  201. self.write(':')
  202. self.body(node.body)
  203. while True:
  204. else_ = node.orelse
  205. if len(else_) == 1 and isinstance(else_[0], If):
  206. node = else_[0]
  207. self.newline()
  208. self.write('elif ')
  209. self.visit(node.test)
  210. self.write(':')
  211. self.body(node.body)
  212. else:
  213. self.newline()
  214. self.write('else:')
  215. self.body(else_)
  216. break
  217. def visit_For(self, node):
  218. self.newline(node)
  219. self.write('for ')
  220. self.visit(node.target)
  221. self.write(' in ')
  222. self.visit(node.iter)
  223. self.write(':')
  224. self.body_or_else(node)
  225. def visit_While(self, node):
  226. self.newline(node)
  227. self.write('while ')
  228. self.visit(node.test)
  229. self.write(':')
  230. self.body_or_else(node)
  231. def visit_With(self, node):
  232. self.newline(node)
  233. self.write('with ')
  234. self.visit(node.context_expr)
  235. if node.optional_vars is not None:
  236. self.write(' as ')
  237. self.visit(node.optional_vars)
  238. self.write(':')
  239. self.body(node.body)
  240. def visit_Pass(self, node):
  241. self.newline(node)
  242. self.write('pass')
  243. def visit_Print(self, node):
  244. # XXX: python 2.6 only
  245. self.newline(node)
  246. self.write('print ')
  247. want_comma = False
  248. if node.dest is not None:
  249. self.write(' >> ')
  250. self.visit(node.dest)
  251. want_comma = True
  252. for value in node.values:
  253. if want_comma:
  254. self.write(', ')
  255. self.visit(value)
  256. want_comma = True
  257. if not node.nl:
  258. self.write(',')
  259. def visit_Delete(self, node):
  260. self.newline(node)
  261. self.write('del ')
  262. for idx, target in enumerate(node):
  263. if idx:
  264. self.write(', ')
  265. self.visit(target)
  266. def visit_TryExcept(self, node):
  267. self.newline(node)
  268. self.write('try:')
  269. self.body(node.body)
  270. for handler in node.handlers:
  271. self.visit(handler)
  272. def visit_TryFinally(self, node):
  273. self.newline(node)
  274. self.write('try:')
  275. self.body(node.body)
  276. self.newline(node)
  277. self.write('finally:')
  278. self.body(node.finalbody)
  279. def visit_Global(self, node):
  280. self.newline(node)
  281. self.write('global ' + ', '.join(node.names))
  282. def visit_Nonlocal(self, node):
  283. self.newline(node)
  284. self.write('nonlocal ' + ', '.join(node.names))
  285. def visit_Return(self, node):
  286. self.newline(node)
  287. if node.value:
  288. self.write('return ')
  289. self.visit(node.value)
  290. else:
  291. self.write('return')
  292. def visit_Break(self, node):
  293. self.newline(node)
  294. self.write('break')
  295. def visit_Continue(self, node):
  296. self.newline(node)
  297. self.write('continue')
  298. def visit_Raise(self, node):
  299. # XXX: Python 2.6 / 3.0 compatibility
  300. self.newline(node)
  301. self.write('raise')
  302. if hasattr(node, 'exc') and node.exc is not None:
  303. self.write(' ')
  304. self.visit(node.exc)
  305. if node.cause is not None:
  306. self.write(' from ')
  307. self.visit(node.cause)
  308. elif hasattr(node, 'type') and node.type is not None:
  309. self.visit(node.type)
  310. if node.inst is not None:
  311. self.write(', ')
  312. self.visit(node.inst)
  313. if node.tback is not None:
  314. self.write(', ')
  315. self.visit(node.tback)
  316. # Expressions
  317. def visit_Attribute(self, node):
  318. self.visit(node.value)
  319. self.write('.' + node.attr)
  320. def visit_Call(self, node):
  321. want_comma = []
  322. def write_comma():
  323. if want_comma:
  324. self.write(', ')
  325. else:
  326. want_comma.append(True)
  327. self.visit(node.func)
  328. self.write('(')
  329. for arg in node.args:
  330. write_comma()
  331. self.visit(arg)
  332. for keyword in node.keywords:
  333. write_comma()
  334. self.write(keyword.arg + '=')
  335. self.visit(keyword.value)
  336. if node.starargs is not None:
  337. write_comma()
  338. self.write('*')
  339. self.visit(node.starargs)
  340. if node.kwargs is not None:
  341. write_comma()
  342. self.write('**')
  343. self.visit(node.kwargs)
  344. self.write(')')
  345. def visit_Name(self, node):
  346. self.write(node.id)
  347. def visit_Str(self, node):
  348. self.write(repr(node.s))
  349. def visit_Bytes(self, node):
  350. self.write(repr(node.s))
  351. def visit_Num(self, node):
  352. self.write(repr(node.n))
  353. def visit_Tuple(self, node):
  354. self.write('(')
  355. idx = -1
  356. for idx, item in enumerate(node.elts):
  357. if idx:
  358. self.write(', ')
  359. self.visit(item)
  360. self.write(idx and ')' or ',)')
  361. def sequence_visit(left, right):
  362. def visit(self, node):
  363. self.write(left)
  364. for idx, item in enumerate(node.elts):
  365. if idx:
  366. self.write(', ')
  367. self.visit(item)
  368. self.write(right)
  369. return visit
  370. visit_List = sequence_visit('[', ']')
  371. visit_Set = sequence_visit('{', '}')
  372. del sequence_visit
  373. def visit_Dict(self, node):
  374. self.write('{')
  375. for idx, (key, value) in enumerate(zip(node.keys, node.values)):
  376. if idx:
  377. self.write(', ')
  378. self.visit(key)
  379. self.write(': ')
  380. self.visit(value)
  381. self.write('}')
  382. def visit_BinOp(self, node):
  383. self.visit(node.left)
  384. self.write(' %s ' % BINOP_SYMBOLS[type(node.op)])
  385. self.visit(node.right)
  386. def visit_BoolOp(self, node):
  387. self.write('(')
  388. for idx, value in enumerate(node.values):
  389. if idx:
  390. self.write(' %s ' % BOOLOP_SYMBOLS[type(node.op)])
  391. self.visit(value)
  392. self.write(')')
  393. def visit_Compare(self, node):
  394. self.write('(')
  395. self.write(node.left)
  396. for op, right in zip(node.ops, node.comparators):
  397. self.write(' %s %%' % CMPOP_SYMBOLS[type(op)])
  398. self.visit(right)
  399. self.write(')')
  400. def visit_UnaryOp(self, node):
  401. self.write('(')
  402. op = UNARYOP_SYMBOLS[type(node.op)]
  403. self.write(op)
  404. if op == 'not':
  405. self.write(' ')
  406. self.visit(node.operand)
  407. self.write(')')
  408. def visit_Subscript(self, node):
  409. self.visit(node.value)
  410. self.write('[')
  411. self.visit(node.slice)
  412. self.write(']')
  413. def visit_Slice(self, node):
  414. if node.lower is not None:
  415. self.visit(node.lower)
  416. self.write(':')
  417. if node.upper is not None:
  418. self.visit(node.upper)
  419. if node.step is not None:
  420. self.write(':')
  421. if not (isinstance(node.step, Name) and node.step.id == 'None'):
  422. self.visit(node.step)
  423. def visit_ExtSlice(self, node):
  424. for idx, item in node.dims:
  425. if idx:
  426. self.write(', ')
  427. self.visit(item)
  428. def visit_Yield(self, node):
  429. self.write('yield ')
  430. self.visit(node.value)
  431. def visit_Lambda(self, node):
  432. self.write('lambda ')
  433. self.signature(node.args)
  434. self.write(': ')
  435. self.visit(node.body)
  436. def visit_Ellipsis(self, node):
  437. self.write('Ellipsis')
  438. def generator_visit(left, right):
  439. def visit(self, node):
  440. self.write(left)
  441. self.visit(node.elt)
  442. for comprehension in node.generators:
  443. self.visit(comprehension)
  444. self.write(right)
  445. return visit
  446. visit_ListComp = generator_visit('[', ']')
  447. visit_GeneratorExp = generator_visit('(', ')')
  448. visit_SetComp = generator_visit('{', '}')
  449. del generator_visit
  450. def visit_DictComp(self, node):
  451. self.write('{')
  452. self.visit(node.key)
  453. self.write(': ')
  454. self.visit(node.value)
  455. for comprehension in node.generators:
  456. self.visit(comprehension)
  457. self.write('}')
  458. def visit_IfExp(self, node):
  459. self.visit(node.body)
  460. self.write(' if ')
  461. self.visit(node.test)
  462. self.write(' else ')
  463. self.visit(node.orelse)
  464. def visit_Starred(self, node):
  465. self.write('*')
  466. self.visit(node.value)
  467. def visit_Repr(self, node):
  468. # XXX: python 2.6 only
  469. self.write('`')
  470. self.visit(node.value)
  471. self.write('`')
  472. # Helper Nodes
  473. def visit_alias(self, node):
  474. self.write(node.name)
  475. if node.asname is not None:
  476. self.write(' as ' + node.asname)
  477. def visit_comprehension(self, node):
  478. self.write(' for ')
  479. self.visit(node.target)
  480. self.write(' in ')
  481. self.visit(node.iter)
  482. if node.ifs:
  483. for if_ in node.ifs:
  484. self.write(' if ')
  485. self.visit(if_)
  486. def visit_excepthandler(self, node):
  487. self.newline(node)
  488. self.write('except')
  489. if node.type is not None:
  490. self.write(' ')
  491. self.visit(node.type)
  492. if node.name is not None:
  493. self.write(' as ')
  494. self.visit(node.name)
  495. self.write(':')
  496. self.body(node.body)