Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 1 | "Usage: unparse.py <path to source file>" |
| 2 | import sys |
Mark Dickinson | 8042e28 | 2010-06-29 10:01:48 +0000 | [diff] [blame] | 3 | import math |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 4 | import ast |
Mark Dickinson | 82c8d93 | 2010-06-29 07:48:23 +0000 | [diff] [blame] | 5 | import tokenize |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 6 | import io |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7 | import os |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 8 | |
Mark Dickinson | cba8c10 | 2010-06-30 11:45:53 +0000 | [diff] [blame] | 9 | # Large float and imaginary literals get turned into infinities in the AST. |
| 10 | # We unparse those infinities to INFSTR. |
| 11 | INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1) |
| 12 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 13 | def interleave(inter, f, seq): |
| 14 | """Call f on each item in seq, calling inter() in between. |
| 15 | """ |
| 16 | seq = iter(seq) |
| 17 | try: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 18 | f(next(seq)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 19 | except StopIteration: |
| 20 | pass |
| 21 | else: |
| 22 | for x in seq: |
| 23 | inter() |
| 24 | f(x) |
| 25 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 26 | class Unparser: |
| 27 | """Methods in this class recursively traverse an AST and |
| 28 | output source code for the abstract syntax; original formatting |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 29 | is disregarded. """ |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 30 | |
| 31 | def __init__(self, tree, file = sys.stdout): |
| 32 | """Unparser(tree, file=sys.stdout) -> None. |
| 33 | Print the source for tree to file.""" |
| 34 | self.f = file |
| 35 | self._indent = 0 |
| 36 | self.dispatch(tree) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 37 | print("", file=self.f) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 38 | self.f.flush() |
| 39 | |
| 40 | def fill(self, text = ""): |
| 41 | "Indent a piece of text, according to the current indentation level" |
| 42 | self.f.write("\n"+" "*self._indent + text) |
| 43 | |
| 44 | def write(self, text): |
| 45 | "Append a piece of text to the current line." |
| 46 | self.f.write(text) |
| 47 | |
| 48 | def enter(self): |
| 49 | "Print ':', and increase the indentation." |
| 50 | self.write(":") |
| 51 | self._indent += 1 |
| 52 | |
| 53 | def leave(self): |
| 54 | "Decrease the indentation level." |
| 55 | self._indent -= 1 |
| 56 | |
| 57 | def dispatch(self, tree): |
| 58 | "Dispatcher function, dispatching tree type T to method _T." |
| 59 | if isinstance(tree, list): |
| 60 | for t in tree: |
| 61 | self.dispatch(t) |
| 62 | return |
| 63 | meth = getattr(self, "_"+tree.__class__.__name__) |
| 64 | meth(tree) |
| 65 | |
| 66 | |
| 67 | ############### Unparsing methods ###################### |
| 68 | # There should be one method per concrete grammar type # |
| 69 | # Constructors should be grouped by sum type. Ideally, # |
| 70 | # this would follow the order in the grammar, but # |
| 71 | # currently doesn't. # |
| 72 | ######################################################## |
| 73 | |
| 74 | def _Module(self, tree): |
| 75 | for stmt in tree.body: |
| 76 | self.dispatch(stmt) |
| 77 | |
| 78 | # stmt |
| 79 | def _Expr(self, tree): |
| 80 | self.fill() |
| 81 | self.dispatch(tree.value) |
| 82 | |
| 83 | def _Import(self, t): |
| 84 | self.fill("import ") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 85 | interleave(lambda: self.write(", "), self.dispatch, t.names) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 86 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 87 | def _ImportFrom(self, t): |
| 88 | self.fill("from ") |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 89 | self.write("." * t.level) |
| 90 | if t.module: |
| 91 | self.write(t.module) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 92 | self.write(" import ") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 93 | interleave(lambda: self.write(", "), self.dispatch, t.names) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 94 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 95 | def _Assign(self, t): |
| 96 | self.fill() |
| 97 | for target in t.targets: |
| 98 | self.dispatch(target) |
| 99 | self.write(" = ") |
| 100 | self.dispatch(t.value) |
| 101 | |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 102 | def _AugAssign(self, t): |
| 103 | self.fill() |
| 104 | self.dispatch(t.target) |
| 105 | self.write(" "+self.binop[t.op.__class__.__name__]+"= ") |
| 106 | self.dispatch(t.value) |
| 107 | |
| 108 | def _Return(self, t): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 109 | self.fill("return") |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 110 | if t.value: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 111 | self.write(" ") |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 112 | self.dispatch(t.value) |
| 113 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 114 | def _Pass(self, t): |
| 115 | self.fill("pass") |
| 116 | |
| 117 | def _Break(self, t): |
| 118 | self.fill("break") |
| 119 | |
| 120 | def _Continue(self, t): |
| 121 | self.fill("continue") |
| 122 | |
| 123 | def _Delete(self, t): |
| 124 | self.fill("del ") |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 125 | interleave(lambda: self.write(", "), self.dispatch, t.targets) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 126 | |
| 127 | def _Assert(self, t): |
| 128 | self.fill("assert ") |
| 129 | self.dispatch(t.test) |
| 130 | if t.msg: |
| 131 | self.write(", ") |
| 132 | self.dispatch(t.msg) |
| 133 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 134 | def _Global(self, t): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 135 | self.fill("global ") |
| 136 | interleave(lambda: self.write(", "), self.write, t.names) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 137 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 138 | def _Nonlocal(self, t): |
| 139 | self.fill("nonlocal ") |
| 140 | interleave(lambda: self.write(", "), self.write, t.names) |
| 141 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 142 | def _Yield(self, t): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 143 | self.write("(") |
| 144 | self.write("yield") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 145 | if t.value: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 146 | self.write(" ") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 147 | self.dispatch(t.value) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 148 | self.write(")") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 149 | |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 150 | def _YieldFrom(self, t): |
| 151 | self.write("(") |
| 152 | self.write("yield from") |
| 153 | if t.value: |
| 154 | self.write(" ") |
| 155 | self.dispatch(t.value) |
| 156 | self.write(")") |
| 157 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 158 | def _Raise(self, t): |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 159 | self.fill("raise") |
| 160 | if not t.exc: |
| 161 | assert not t.cause |
| 162 | return |
| 163 | self.write(" ") |
| 164 | self.dispatch(t.exc) |
| 165 | if t.cause: |
| 166 | self.write(" from ") |
| 167 | self.dispatch(t.cause) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 168 | |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 169 | def _Try(self, t): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 170 | self.fill("try") |
| 171 | self.enter() |
| 172 | self.dispatch(t.body) |
| 173 | self.leave() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 174 | for ex in t.handlers: |
| 175 | self.dispatch(ex) |
| 176 | if t.orelse: |
| 177 | self.fill("else") |
| 178 | self.enter() |
| 179 | self.dispatch(t.orelse) |
| 180 | self.leave() |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 181 | if t.finalbody: |
| 182 | self.fill("finally") |
Mark Dickinson | 81ad8cc | 2010-06-30 08:46:53 +0000 | [diff] [blame] | 183 | self.enter() |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 184 | self.dispatch(t.finalbody) |
Mark Dickinson | 81ad8cc | 2010-06-30 08:46:53 +0000 | [diff] [blame] | 185 | self.leave() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 186 | |
Benjamin Peterson | c4fe6f3 | 2008-08-19 18:57:56 +0000 | [diff] [blame] | 187 | def _ExceptHandler(self, t): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 188 | self.fill("except") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 189 | if t.type: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 190 | self.write(" ") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 191 | self.dispatch(t.type) |
| 192 | if t.name: |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 193 | self.write(" as ") |
| 194 | self.write(t.name) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 195 | self.enter() |
| 196 | self.dispatch(t.body) |
| 197 | self.leave() |
| 198 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 199 | def _ClassDef(self, t): |
| 200 | self.write("\n") |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 201 | for deco in t.decorator_list: |
| 202 | self.fill("@") |
| 203 | self.dispatch(deco) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 204 | self.fill("class "+t.name) |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 205 | self.write("(") |
| 206 | comma = False |
| 207 | for e in t.bases: |
| 208 | if comma: self.write(", ") |
| 209 | else: comma = True |
| 210 | self.dispatch(e) |
| 211 | for e in t.keywords: |
| 212 | if comma: self.write(", ") |
| 213 | else: comma = True |
| 214 | self.dispatch(e) |
| 215 | if t.starargs: |
| 216 | if comma: self.write(", ") |
| 217 | else: comma = True |
| 218 | self.write("*") |
| 219 | self.dispatch(t.starargs) |
| 220 | if t.kwargs: |
| 221 | if comma: self.write(", ") |
| 222 | else: comma = True |
Mark Dickinson | 578aa56 | 2010-06-29 18:38:59 +0000 | [diff] [blame] | 223 | self.write("**") |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 224 | self.dispatch(t.kwargs) |
| 225 | self.write(")") |
| 226 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 227 | self.enter() |
| 228 | self.dispatch(t.body) |
| 229 | self.leave() |
| 230 | |
| 231 | def _FunctionDef(self, t): |
| 232 | self.write("\n") |
Benjamin Peterson | c4fe6f3 | 2008-08-19 18:57:56 +0000 | [diff] [blame] | 233 | for deco in t.decorator_list: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 234 | self.fill("@") |
| 235 | self.dispatch(deco) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 236 | self.fill("def "+t.name + "(") |
| 237 | self.dispatch(t.args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 238 | self.write(")") |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 239 | if t.returns: |
| 240 | self.write(" -> ") |
| 241 | self.dispatch(t.returns) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 242 | self.enter() |
| 243 | self.dispatch(t.body) |
| 244 | self.leave() |
| 245 | |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 246 | def _For(self, t): |
| 247 | self.fill("for ") |
| 248 | self.dispatch(t.target) |
| 249 | self.write(" in ") |
| 250 | self.dispatch(t.iter) |
| 251 | self.enter() |
| 252 | self.dispatch(t.body) |
| 253 | self.leave() |
| 254 | if t.orelse: |
| 255 | self.fill("else") |
| 256 | self.enter() |
| 257 | self.dispatch(t.orelse) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 258 | self.leave() |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 259 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 260 | def _If(self, t): |
| 261 | self.fill("if ") |
| 262 | self.dispatch(t.test) |
| 263 | self.enter() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 264 | self.dispatch(t.body) |
| 265 | self.leave() |
Mark Dickinson | 8d6d760 | 2010-06-30 08:32:11 +0000 | [diff] [blame] | 266 | # collapse nested ifs into equivalent elifs. |
| 267 | while (t.orelse and len(t.orelse) == 1 and |
| 268 | isinstance(t.orelse[0], ast.If)): |
| 269 | t = t.orelse[0] |
| 270 | self.fill("elif ") |
| 271 | self.dispatch(t.test) |
| 272 | self.enter() |
| 273 | self.dispatch(t.body) |
| 274 | self.leave() |
| 275 | # final else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 276 | if t.orelse: |
| 277 | self.fill("else") |
| 278 | self.enter() |
| 279 | self.dispatch(t.orelse) |
| 280 | self.leave() |
| 281 | |
| 282 | def _While(self, t): |
| 283 | self.fill("while ") |
| 284 | self.dispatch(t.test) |
| 285 | self.enter() |
| 286 | self.dispatch(t.body) |
| 287 | self.leave() |
| 288 | if t.orelse: |
| 289 | self.fill("else") |
| 290 | self.enter() |
| 291 | self.dispatch(t.orelse) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 292 | self.leave() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 293 | |
| 294 | def _With(self, t): |
| 295 | self.fill("with ") |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 296 | interleave(lambda: self.write(", "), self.dispatch, t.items) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 297 | self.enter() |
| 298 | self.dispatch(t.body) |
| 299 | self.leave() |
| 300 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 301 | # expr |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 302 | def _Bytes(self, t): |
| 303 | self.write(repr(t.s)) |
| 304 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 305 | def _Str(self, tree): |
| 306 | self.write(repr(tree.s)) |
| 307 | |
| 308 | def _Name(self, t): |
| 309 | self.write(t.id) |
| 310 | |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 311 | def _Num(self, t): |
Mark Dickinson | cba8c10 | 2010-06-30 11:45:53 +0000 | [diff] [blame] | 312 | # Substitute overflowing decimal literal for AST infinities. |
| 313 | self.write(repr(t.n).replace("inf", INFSTR)) |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 314 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 315 | def _List(self, t): |
| 316 | self.write("[") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 317 | interleave(lambda: self.write(", "), self.dispatch, t.elts) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 318 | self.write("]") |
| 319 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 320 | def _ListComp(self, t): |
| 321 | self.write("[") |
| 322 | self.dispatch(t.elt) |
| 323 | for gen in t.generators: |
| 324 | self.dispatch(gen) |
| 325 | self.write("]") |
| 326 | |
| 327 | def _GeneratorExp(self, t): |
| 328 | self.write("(") |
| 329 | self.dispatch(t.elt) |
| 330 | for gen in t.generators: |
| 331 | self.dispatch(gen) |
| 332 | self.write(")") |
| 333 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 334 | def _SetComp(self, t): |
| 335 | self.write("{") |
| 336 | self.dispatch(t.elt) |
| 337 | for gen in t.generators: |
| 338 | self.dispatch(gen) |
| 339 | self.write("}") |
| 340 | |
| 341 | def _DictComp(self, t): |
| 342 | self.write("{") |
| 343 | self.dispatch(t.key) |
| 344 | self.write(": ") |
| 345 | self.dispatch(t.value) |
| 346 | for gen in t.generators: |
| 347 | self.dispatch(gen) |
| 348 | self.write("}") |
| 349 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 350 | def _comprehension(self, t): |
| 351 | self.write(" for ") |
| 352 | self.dispatch(t.target) |
| 353 | self.write(" in ") |
| 354 | self.dispatch(t.iter) |
| 355 | for if_clause in t.ifs: |
| 356 | self.write(" if ") |
| 357 | self.dispatch(if_clause) |
| 358 | |
| 359 | def _IfExp(self, t): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 360 | self.write("(") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 361 | self.dispatch(t.body) |
| 362 | self.write(" if ") |
| 363 | self.dispatch(t.test) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 364 | self.write(" else ") |
| 365 | self.dispatch(t.orelse) |
| 366 | self.write(")") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 367 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 368 | def _Set(self, t): |
| 369 | assert(t.elts) # should be at least one element |
| 370 | self.write("{") |
| 371 | interleave(lambda: self.write(", "), self.dispatch, t.elts) |
| 372 | self.write("}") |
| 373 | |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 374 | def _Dict(self, t): |
| 375 | self.write("{") |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 376 | def write_pair(pair): |
| 377 | (k, v) = pair |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 378 | self.dispatch(k) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 379 | self.write(": ") |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 380 | self.dispatch(v) |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 381 | interleave(lambda: self.write(", "), write_pair, zip(t.keys, t.values)) |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 382 | self.write("}") |
| 383 | |
| 384 | def _Tuple(self, t): |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 385 | self.write("(") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 386 | if len(t.elts) == 1: |
| 387 | (elt,) = t.elts |
| 388 | self.dispatch(elt) |
| 389 | self.write(",") |
| 390 | else: |
| 391 | interleave(lambda: self.write(", "), self.dispatch, t.elts) |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 392 | self.write(")") |
| 393 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 394 | unop = {"Invert":"~", "Not": "not", "UAdd":"+", "USub":"-"} |
| 395 | def _UnaryOp(self, t): |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 396 | self.write("(") |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 397 | self.write(self.unop[t.op.__class__.__name__]) |
| 398 | self.write(" ") |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 399 | self.dispatch(t.operand) |
| 400 | self.write(")") |
| 401 | |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 402 | binop = { "Add":"+", "Sub":"-", "Mult":"*", "Div":"/", "Mod":"%", |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 403 | "LShift":"<<", "RShift":">>", "BitOr":"|", "BitXor":"^", "BitAnd":"&", |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 404 | "FloorDiv":"//", "Pow": "**"} |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 405 | def _BinOp(self, t): |
| 406 | self.write("(") |
| 407 | self.dispatch(t.left) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 408 | self.write(" " + self.binop[t.op.__class__.__name__] + " ") |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 409 | self.dispatch(t.right) |
| 410 | self.write(")") |
| 411 | |
| 412 | cmpops = {"Eq":"==", "NotEq":"!=", "Lt":"<", "LtE":"<=", "Gt":">", "GtE":">=", |
| 413 | "Is":"is", "IsNot":"is not", "In":"in", "NotIn":"not in"} |
| 414 | def _Compare(self, t): |
| 415 | self.write("(") |
| 416 | self.dispatch(t.left) |
| 417 | for o, e in zip(t.ops, t.comparators): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 418 | self.write(" " + self.cmpops[o.__class__.__name__] + " ") |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 419 | self.dispatch(e) |
Mark Dickinson | f5451e5 | 2010-06-28 20:09:18 +0000 | [diff] [blame] | 420 | self.write(")") |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 421 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 422 | boolops = {ast.And: 'and', ast.Or: 'or'} |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 423 | def _BoolOp(self, t): |
| 424 | self.write("(") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 425 | s = " %s " % self.boolops[t.op.__class__] |
| 426 | interleave(lambda: self.write(s), self.dispatch, t.values) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 427 | self.write(")") |
| 428 | |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 429 | def _Attribute(self,t): |
| 430 | self.dispatch(t.value) |
Mark Dickinson | b67e15c | 2010-06-30 09:05:47 +0000 | [diff] [blame] | 431 | # Special case: 3.__abs__() is a syntax error, so if t.value |
| 432 | # is an integer literal then we need to either parenthesize |
| 433 | # it or add an extra space to get 3 .__abs__(). |
| 434 | if isinstance(t.value, ast.Num) and isinstance(t.value.n, int): |
| 435 | self.write(" ") |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 436 | self.write(".") |
| 437 | self.write(t.attr) |
| 438 | |
| 439 | def _Call(self, t): |
| 440 | self.dispatch(t.func) |
| 441 | self.write("(") |
| 442 | comma = False |
| 443 | for e in t.args: |
| 444 | if comma: self.write(", ") |
| 445 | else: comma = True |
| 446 | self.dispatch(e) |
| 447 | for e in t.keywords: |
| 448 | if comma: self.write(", ") |
| 449 | else: comma = True |
| 450 | self.dispatch(e) |
| 451 | if t.starargs: |
| 452 | if comma: self.write(", ") |
| 453 | else: comma = True |
| 454 | self.write("*") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 455 | self.dispatch(t.starargs) |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 456 | if t.kwargs: |
| 457 | if comma: self.write(", ") |
| 458 | else: comma = True |
| 459 | self.write("**") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 460 | self.dispatch(t.kwargs) |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 461 | self.write(")") |
| 462 | |
| 463 | def _Subscript(self, t): |
| 464 | self.dispatch(t.value) |
| 465 | self.write("[") |
| 466 | self.dispatch(t.slice) |
| 467 | self.write("]") |
| 468 | |
Mark Dickinson | 1b2e944 | 2012-05-06 17:27:39 +0100 | [diff] [blame] | 469 | def _Starred(self, t): |
| 470 | self.write("*") |
| 471 | self.dispatch(t.value) |
| 472 | |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 473 | # slice |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 474 | def _Ellipsis(self, t): |
| 475 | self.write("...") |
| 476 | |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 477 | def _Index(self, t): |
| 478 | self.dispatch(t.value) |
| 479 | |
| 480 | def _Slice(self, t): |
| 481 | if t.lower: |
| 482 | self.dispatch(t.lower) |
| 483 | self.write(":") |
| 484 | if t.upper: |
| 485 | self.dispatch(t.upper) |
| 486 | if t.step: |
| 487 | self.write(":") |
| 488 | self.dispatch(t.step) |
| 489 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 490 | def _ExtSlice(self, t): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 491 | interleave(lambda: self.write(', '), self.dispatch, t.dims) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 492 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 493 | # argument |
| 494 | def _arg(self, t): |
| 495 | self.write(t.arg) |
| 496 | if t.annotation: |
| 497 | self.write(": ") |
| 498 | self.dispatch(t.annotation) |
| 499 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 500 | # others |
| 501 | def _arguments(self, t): |
| 502 | first = True |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 503 | # normal arguments |
| 504 | defaults = [None] * (len(t.args) - len(t.defaults)) + t.defaults |
| 505 | for a, d in zip(t.args, defaults): |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 506 | if first:first = False |
| 507 | else: self.write(", ") |
| 508 | self.dispatch(a) |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 509 | if d: |
| 510 | self.write("=") |
| 511 | self.dispatch(d) |
| 512 | |
| 513 | # varargs, or bare '*' if no varargs but keyword-only arguments present |
| 514 | if t.vararg or t.kwonlyargs: |
Martin v. Löwis | 87a8b4f | 2006-02-28 21:41:30 +0000 | [diff] [blame] | 515 | if first:first = False |
| 516 | else: self.write(", ") |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 517 | self.write("*") |
| 518 | if t.vararg: |
| 519 | self.write(t.vararg) |
| 520 | if t.varargannotation: |
| 521 | self.write(": ") |
| 522 | self.dispatch(t.varargannotation) |
| 523 | |
| 524 | # keyword-only arguments |
| 525 | if t.kwonlyargs: |
| 526 | for a, d in zip(t.kwonlyargs, t.kw_defaults): |
| 527 | if first:first = False |
| 528 | else: self.write(", ") |
| 529 | self.dispatch(a), |
| 530 | if d: |
| 531 | self.write("=") |
| 532 | self.dispatch(d) |
| 533 | |
| 534 | # kwargs |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 535 | if t.kwarg: |
| 536 | if first:first = False |
| 537 | else: self.write(", ") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 538 | self.write("**"+t.kwarg) |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 539 | if t.kwargannotation: |
| 540 | self.write(": ") |
| 541 | self.dispatch(t.kwargannotation) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 542 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 543 | def _keyword(self, t): |
| 544 | self.write(t.arg) |
| 545 | self.write("=") |
| 546 | self.dispatch(t.value) |
| 547 | |
| 548 | def _Lambda(self, t): |
Mark Dickinson | 8042e28 | 2010-06-29 10:01:48 +0000 | [diff] [blame] | 549 | self.write("(") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 550 | self.write("lambda ") |
| 551 | self.dispatch(t.args) |
| 552 | self.write(": ") |
| 553 | self.dispatch(t.body) |
Mark Dickinson | 8042e28 | 2010-06-29 10:01:48 +0000 | [diff] [blame] | 554 | self.write(")") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 555 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 556 | def _alias(self, t): |
| 557 | self.write(t.name) |
| 558 | if t.asname: |
| 559 | self.write(" as "+t.asname) |
| 560 | |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 561 | def _withitem(self, t): |
| 562 | self.dispatch(t.context_expr) |
| 563 | if t.optional_vars: |
| 564 | self.write(" as ") |
| 565 | self.dispatch(t.optional_vars) |
| 566 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 567 | def roundtrip(filename, output=sys.stdout): |
Mark Dickinson | 82c8d93 | 2010-06-29 07:48:23 +0000 | [diff] [blame] | 568 | with open(filename, "rb") as pyfile: |
| 569 | encoding = tokenize.detect_encoding(pyfile.readline)[0] |
| 570 | with open(filename, "r", encoding=encoding) as pyfile: |
| 571 | source = pyfile.read() |
Mark Dickinson | 3d1bfbf | 2010-06-28 21:39:51 +0000 | [diff] [blame] | 572 | tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 573 | Unparser(tree, output) |
| 574 | |
| 575 | |
| 576 | |
| 577 | def testdir(a): |
| 578 | try: |
| 579 | names = [n for n in os.listdir(a) if n.endswith('.py')] |
| 580 | except OSError: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 581 | print("Directory not readable: %s" % a, file=sys.stderr) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 582 | else: |
| 583 | for n in names: |
| 584 | fullname = os.path.join(a, n) |
| 585 | if os.path.isfile(fullname): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 586 | output = io.StringIO() |
| 587 | print('Testing %s' % fullname) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 588 | try: |
| 589 | roundtrip(fullname, output) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 590 | except Exception as e: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 591 | print(' Failed to compile, exception is %s' % repr(e)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 592 | elif os.path.isdir(fullname): |
| 593 | testdir(fullname) |
| 594 | |
| 595 | def main(args): |
| 596 | if args[0] == '--testdir': |
| 597 | for a in args[1:]: |
| 598 | testdir(a) |
| 599 | else: |
| 600 | for a in args: |
| 601 | roundtrip(a) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 602 | |
| 603 | if __name__=='__main__': |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 604 | main(sys.argv[1:]) |