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.

572 lines
16 KiB

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(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)
  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. self.write('return ')
  288. self.visit(node.value)
  289. def visit_Break(self, node):
  290. self.newline(node)
  291. self.write('break')
  292. def visit_Continue(self, node):
  293. self.newline(node)
  294. self.write('continue')
  295. def visit_Raise(self, node):
  296. # XXX: Python 2.6 / 3.0 compatibility
  297. self.newline(node)
  298. self.write('raise')
  299. if hasattr(node, 'exc') and node.exc is not None:
  300. self.write(' ')
  301. self.visit(node.exc)
  302. if node.cause is not None:
  303. self.write(' from ')
  304. self.visit(node.cause)
  305. elif hasattr(node, 'type') and node.type is not None:
  306. self.visit(node.type)
  307. if node.inst is not None:
  308. self.write(', ')
  309. self.visit(node.inst)
  310. if node.tback is not None:
  311. self.write(', ')
  312. self.visit(node.tback)
  313. # Expressions
  314. def visit_Attribute(self, node):
  315. self.visit(node.value)
  316. self.write('.' + node.attr)
  317. def visit_Call(self, node):
  318. want_comma = []
  319. def write_comma():
  320. if want_comma:
  321. self.write(', ')
  322. else:
  323. want_comma.append(True)
  324. self.visit(node.func)
  325. self.write('(')
  326. for arg in node.args:
  327. write_comma()
  328. self.visit(arg)
  329. for keyword in node.keywords:
  330. write_comma()
  331. self.write(keyword.arg + '=')
  332. self.visit(keyword.value)
  333. if node.starargs is not None:
  334. write_comma()
  335. self.write('*')
  336. self.visit(node.starargs)
  337. if node.kwargs is not None:
  338. write_comma()
  339. self.write('**')
  340. self.visit(node.kwargs)
  341. self.write(')')
  342. def visit_Name(self, node):
  343. self.write(node.id)
  344. def visit_Str(self, node):
  345. self.write(repr(node.s))
  346. def visit_Bytes(self, node):
  347. self.write(repr(node.s))
  348. def visit_Num(self, node):
  349. self.write(repr(node.n))
  350. def visit_Tuple(self, node):
  351. self.write('(')
  352. idx = -1
  353. for idx, item in enumerate(node.elts):
  354. if idx:
  355. self.write(', ')
  356. self.visit(item)
  357. self.write(idx and ')' or ',)')
  358. def sequence_visit(left, right):
  359. def visit(self, node):
  360. self.write(left)
  361. for idx, item in enumerate(node.elts):
  362. if idx:
  363. self.write(', ')
  364. self.visit(item)
  365. self.write(right)
  366. return visit
  367. visit_List = sequence_visit('[', ']')
  368. visit_Set = sequence_visit('{', '}')
  369. del sequence_visit
  370. def visit_Dict(self, node):
  371. self.write('{')
  372. for idx, (key, value) in enumerate(zip(node.keys, node.values)):
  373. if idx:
  374. self.write(', ')
  375. self.visit(key)
  376. self.write(': ')
  377. self.visit(value)
  378. self.write('}')
  379. def visit_BinOp(self, node):
  380. self.visit(node.left)
  381. self.write(' %s ' % BINOP_SYMBOLS[type(node.op)])
  382. self.visit(node.right)
  383. def visit_BoolOp(self, node):
  384. self.write('(')
  385. for idx, value in enumerate(node.values):
  386. if idx:
  387. self.write(' %s ' % BOOLOP_SYMBOLS[type(node.op)])
  388. self.visit(value)
  389. self.write(')')
  390. def visit_Compare(self, node):
  391. self.write('(')
  392. self.write(node.left)
  393. for op, right in zip(node.ops, node.comparators):
  394. self.write(' %s %%' % CMPOP_SYMBOLS[type(op)])
  395. self.visit(right)
  396. self.write(')')
  397. def visit_UnaryOp(self, node):
  398. self.write('(')
  399. op = UNARYOP_SYMBOLS[type(node.op)]
  400. self.write(op)
  401. if op == 'not':
  402. self.write(' ')
  403. self.visit(node.operand)
  404. self.write(')')
  405. def visit_Subscript(self, node):
  406. self.visit(node.value)
  407. self.write('[')
  408. self.visit(node.slice)
  409. self.write(']')
  410. def visit_Slice(self, node):
  411. if node.lower is not None:
  412. self.visit(node.lower)
  413. self.write(':')
  414. if node.upper is not None:
  415. self.visit(node.upper)
  416. if node.step is not None:
  417. self.write(':')
  418. if not (isinstance(node.step, Name) and node.step.id == 'None'):
  419. self.visit(node.step)
  420. def visit_ExtSlice(self, node):
  421. for idx, item in node.dims:
  422. if idx:
  423. self.write(', ')
  424. self.visit(item)
  425. def visit_Yield(self, node):
  426. self.write('yield ')
  427. self.visit(node.value)
  428. def visit_Lambda(self, node):
  429. self.write('lambda ')
  430. self.signature(node.args)
  431. self.write(': ')
  432. self.visit(node.body)
  433. def visit_Ellipsis(self, node):
  434. self.write('Ellipsis')
  435. def generator_visit(left, right):
  436. def visit(self, node):
  437. self.write(left)
  438. self.visit(node.elt)
  439. for comprehension in node.generators:
  440. self.visit(comprehension)
  441. self.write(right)
  442. return visit
  443. visit_ListComp = generator_visit('[', ']')
  444. visit_GeneratorExp = generator_visit('(', ')')
  445. visit_SetComp = generator_visit('{', '}')
  446. del generator_visit
  447. def visit_DictComp(self, node):
  448. self.write('{')
  449. self.visit(node.key)
  450. self.write(': ')
  451. self.visit(node.value)
  452. for comprehension in node.generators:
  453. self.visit(comprehension)
  454. self.write('}')
  455. def visit_IfExp(self, node):
  456. self.visit(node.body)
  457. self.write(' if ')
  458. self.visit(node.test)
  459. self.write(' else ')
  460. self.visit(node.orelse)
  461. def visit_Starred(self, node):
  462. self.write('*')
  463. self.visit(node.value)
  464. def visit_Repr(self, node):
  465. # XXX: python 2.6 only
  466. self.write('`')
  467. self.visit(node.value)
  468. self.write('`')
  469. # Helper Nodes
  470. def visit_alias(self, node):
  471. self.write(node.name)
  472. if node.asname is not None:
  473. self.write(' as ' + node.asname)
  474. def visit_comprehension(self, node):
  475. self.write(' for ')
  476. self.visit(node.target)
  477. self.write(' in ')
  478. self.visit(node.iter)
  479. if node.ifs:
  480. for if_ in node.ifs:
  481. self.write(' if ')
  482. self.visit(if_)
  483. def visit_excepthandler(self, node):
  484. self.newline(node)
  485. self.write('except')
  486. if node.type is not None:
  487. self.write(' ')
  488. self.visit(node.type)
  489. if node.name is not None:
  490. self.write(' as ')
  491. self.visit(node.name)
  492. self.write(':')
  493. self.body(node.body)