paul@0 | 1 | """Python abstract syntax node definitions |
paul@0 | 2 | |
paul@0 | 3 | This file was originally generated by Tools/compiler/astgen.py |
paul@0 | 4 | """ |
paul@0 | 5 | from compiler.consts import CO_VARARGS, CO_VARKEYWORDS |
paul@0 | 6 | |
paul@0 | 7 | def flatten(seq): |
paul@0 | 8 | l = [] |
paul@0 | 9 | for elt in seq: |
paul@0 | 10 | if isinstance(elt, (tuple, list)): |
paul@0 | 11 | for elt2 in flatten(elt): |
paul@0 | 12 | l.append(elt2) |
paul@0 | 13 | else: |
paul@0 | 14 | l.append(elt) |
paul@0 | 15 | return l |
paul@0 | 16 | |
paul@0 | 17 | def flatten_nodes(seq): |
paul@0 | 18 | return [n for n in flatten(seq) if isinstance(n, Node)] |
paul@0 | 19 | |
paul@0 | 20 | def flatten_statement(seq): |
paul@0 | 21 | l = [] |
paul@0 | 22 | for elt in seq: |
paul@0 | 23 | if isinstance(elt, Stmt): |
paul@0 | 24 | l += flatten_statement(elt) |
paul@0 | 25 | else: |
paul@0 | 26 | l.append(elt) |
paul@0 | 27 | return l |
paul@0 | 28 | |
paul@0 | 29 | def flatten_assignment(node): |
paul@0 | 30 | l = [] |
paul@0 | 31 | if isinstance(node, (AssList, AssTuple)): |
paul@0 | 32 | for n in node.nodes: |
paul@0 | 33 | l += flatten_assignment(n) |
paul@0 | 34 | else: |
paul@0 | 35 | l.append(node) |
paul@0 | 36 | return l |
paul@0 | 37 | |
paul@0 | 38 | def is_deletion(node): |
paul@0 | 39 | return isinstance(node, (AssAttr, AssName)) and node.flags == "OP_DELETE" |
paul@0 | 40 | |
paul@0 | 41 | def docstring(s): |
paul@0 | 42 | if s.find("\n") != -1: |
paul@0 | 43 | if s.find("'''") != -1: |
paul@0 | 44 | return '"""%s"""' % s.replace('"""', '\\"\\"\\"') |
paul@0 | 45 | else: |
paul@0 | 46 | return "'''%s'''" % s.replace("'''", "\\'\\'\\'") |
paul@0 | 47 | else: |
paul@0 | 48 | return repr(s) |
paul@0 | 49 | |
paul@0 | 50 | def indent(s): |
paul@0 | 51 | return s.replace("\n", "\n\t") |
paul@0 | 52 | |
paul@0 | 53 | def get_defaults(node): |
paul@0 | 54 | |
paul@0 | 55 | "Return a list of (argument name, default) tuples for the 'node'." |
paul@0 | 56 | |
paul@0 | 57 | star = (node.flags & 4 != 0) and 1 or 0 |
paul@0 | 58 | dstar = (node.flags & 8 != 0) and 1 or 0 |
paul@0 | 59 | argnames = node.argnames[:] |
paul@0 | 60 | |
paul@0 | 61 | # Add stars to star and dstar parameters. |
paul@0 | 62 | |
paul@0 | 63 | if star: |
paul@0 | 64 | argnames[-dstar-star] = "*%s" % argnames[-dstar-star] |
paul@0 | 65 | if dstar: |
paul@0 | 66 | argnames[-dstar] = "**%s" % argnames[-dstar] |
paul@0 | 67 | |
paul@0 | 68 | # Map defaults to parameters. |
paul@0 | 69 | |
paul@0 | 70 | defaults = [None] * (len(node.argnames) - star - dstar - len(node.defaults)) + list(node.defaults) + [None] * (star + dstar) |
paul@0 | 71 | return zip(argnames, defaults) |
paul@0 | 72 | |
paul@0 | 73 | def decode_function(node): |
paul@0 | 74 | return [(default and "%s=%s" % (argname, default) or argname) for (argname, default) in get_defaults(node)] |
paul@0 | 75 | |
paul@0 | 76 | nodes = {} |
paul@0 | 77 | |
paul@0 | 78 | class OperatorUser: |
paul@0 | 79 | |
paul@0 | 80 | "Operator-related node." |
paul@0 | 81 | |
paul@0 | 82 | pass |
paul@0 | 83 | |
paul@0 | 84 | class Operator(OperatorUser): |
paul@0 | 85 | |
paul@0 | 86 | "Operator node." |
paul@0 | 87 | |
paul@0 | 88 | pass |
paul@0 | 89 | |
paul@0 | 90 | class Node: |
paul@0 | 91 | |
paul@0 | 92 | "Abstract base class for ast nodes." |
paul@0 | 93 | |
paul@0 | 94 | def getChildren(self): |
paul@0 | 95 | pass # implemented by subclasses |
paul@0 | 96 | |
paul@0 | 97 | def __iter__(self): |
paul@0 | 98 | for n in self.getChildren(): |
paul@0 | 99 | yield n |
paul@0 | 100 | |
paul@0 | 101 | def getChildNodes(self): |
paul@0 | 102 | pass # implemented by subclasses |
paul@0 | 103 | |
paul@0 | 104 | class EmptyNode(Node): |
paul@0 | 105 | pass |
paul@0 | 106 | |
paul@0 | 107 | class Expression(Node): |
paul@0 | 108 | # Expression is an artificial node class to support "eval" |
paul@0 | 109 | nodes["expression"] = "Expression" |
paul@0 | 110 | def __init__(self, node): |
paul@0 | 111 | self.node = node |
paul@0 | 112 | |
paul@0 | 113 | def getChildren(self): |
paul@0 | 114 | return self.node, |
paul@0 | 115 | |
paul@0 | 116 | def getChildNodes(self): |
paul@0 | 117 | return self.node, |
paul@0 | 118 | |
paul@0 | 119 | def __repr__(self): |
paul@0 | 120 | return "Expression(%r)" % (self.node,) |
paul@0 | 121 | |
paul@0 | 122 | def __str__(self): |
paul@0 | 123 | return str(self.node) |
paul@0 | 124 | |
paul@0 | 125 | class Add(Node, Operator): |
paul@0 | 126 | def __init__(self, leftright, lineno=None): |
paul@0 | 127 | self.left = leftright[0] |
paul@0 | 128 | self.right = leftright[1] |
paul@0 | 129 | self.lineno = lineno |
paul@0 | 130 | |
paul@0 | 131 | def getChildren(self): |
paul@0 | 132 | return self.left, self.right |
paul@0 | 133 | |
paul@0 | 134 | def getChildNodes(self): |
paul@0 | 135 | return self.left, self.right |
paul@0 | 136 | |
paul@0 | 137 | def __repr__(self): |
paul@0 | 138 | return "Add((%r, %r))" % (self.left, self.right) |
paul@0 | 139 | |
paul@0 | 140 | def __str__(self): |
paul@0 | 141 | return "(%s + %s)" % (self.left, self.right) |
paul@0 | 142 | |
paul@0 | 143 | class And(Node): |
paul@0 | 144 | def __init__(self, nodes, lineno=None): |
paul@0 | 145 | self.nodes = nodes |
paul@0 | 146 | self.lineno = lineno |
paul@0 | 147 | |
paul@0 | 148 | def getChildren(self): |
paul@0 | 149 | return tuple(flatten(self.nodes)) |
paul@0 | 150 | |
paul@0 | 151 | def getChildNodes(self): |
paul@0 | 152 | nodelist = [] |
paul@0 | 153 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 154 | return tuple(nodelist) |
paul@0 | 155 | |
paul@0 | 156 | def __repr__(self): |
paul@0 | 157 | return "And(%r)" % (self.nodes,) |
paul@0 | 158 | |
paul@0 | 159 | def __str__(self): |
paul@0 | 160 | return "(%s)" % " and ".join(map(str, self.nodes)) |
paul@0 | 161 | |
paul@0 | 162 | class AssAttr(Node): |
paul@0 | 163 | def __init__(self, expr, attrname, flags, lineno=None): |
paul@0 | 164 | self.expr = expr |
paul@0 | 165 | self.attrname = attrname |
paul@0 | 166 | self.flags = flags |
paul@0 | 167 | self.lineno = lineno |
paul@0 | 168 | |
paul@0 | 169 | def getChildren(self): |
paul@0 | 170 | return self.expr, self.attrname, self.flags |
paul@0 | 171 | |
paul@0 | 172 | def getChildNodes(self): |
paul@0 | 173 | return self.expr, |
paul@0 | 174 | |
paul@0 | 175 | def __repr__(self): |
paul@0 | 176 | return "AssAttr(%r, %r, %r)" % (self.expr, self.attrname, self.flags) |
paul@0 | 177 | |
paul@0 | 178 | def __str__(self): |
paul@0 | 179 | return "%s%s.%s" % (self.flags == "OP_DELETE" and "del " or "", self.expr, self.attrname) |
paul@0 | 180 | |
paul@0 | 181 | class AssList(Node): |
paul@0 | 182 | def __init__(self, nodes, lineno=None): |
paul@0 | 183 | self.nodes = nodes |
paul@0 | 184 | self.lineno = lineno |
paul@0 | 185 | |
paul@0 | 186 | def getChildren(self): |
paul@0 | 187 | return tuple(flatten(self.nodes)) |
paul@0 | 188 | |
paul@0 | 189 | def getChildNodes(self): |
paul@0 | 190 | nodelist = [] |
paul@0 | 191 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 192 | return tuple(nodelist) |
paul@0 | 193 | |
paul@0 | 194 | def __repr__(self): |
paul@0 | 195 | return "AssList(%r)" % (self.nodes,) |
paul@0 | 196 | |
paul@0 | 197 | def __str__(self): |
paul@0 | 198 | nodes = flatten_assignment(self) |
paul@0 | 199 | if nodes and is_deletion(nodes[0]): |
paul@0 | 200 | return "; ".join(map(str, self.nodes)) |
paul@0 | 201 | else: |
paul@0 | 202 | return "[%s]" % ", ".join(map(str, self.nodes)) |
paul@0 | 203 | |
paul@0 | 204 | class AssName(Node): |
paul@0 | 205 | def __init__(self, name, flags, lineno=None): |
paul@0 | 206 | self.name = name |
paul@0 | 207 | self.flags = flags |
paul@0 | 208 | self.lineno = lineno |
paul@0 | 209 | |
paul@0 | 210 | def getChildren(self): |
paul@0 | 211 | return self.name, self.flags |
paul@0 | 212 | |
paul@0 | 213 | def getChildNodes(self): |
paul@0 | 214 | return () |
paul@0 | 215 | |
paul@0 | 216 | def __repr__(self): |
paul@0 | 217 | return "AssName(%r, %r)" % (self.name, self.flags) |
paul@0 | 218 | |
paul@0 | 219 | def __str__(self): |
paul@0 | 220 | return "%s%s" % (self.flags == "OP_DELETE" and "del " or "", self.name) |
paul@0 | 221 | |
paul@0 | 222 | class AssTuple(Node): |
paul@0 | 223 | def __init__(self, nodes, lineno=None): |
paul@0 | 224 | self.nodes = nodes |
paul@0 | 225 | self.lineno = lineno |
paul@0 | 226 | |
paul@0 | 227 | def getChildren(self): |
paul@0 | 228 | return tuple(flatten(self.nodes)) |
paul@0 | 229 | |
paul@0 | 230 | def getChildNodes(self): |
paul@0 | 231 | nodelist = [] |
paul@0 | 232 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 233 | return tuple(nodelist) |
paul@0 | 234 | |
paul@0 | 235 | def __repr__(self): |
paul@0 | 236 | return "AssTuple(%r)" % (self.nodes,) |
paul@0 | 237 | |
paul@0 | 238 | def __str__(self): |
paul@0 | 239 | nodes = flatten_assignment(self) |
paul@0 | 240 | if nodes and is_deletion(nodes[0]): |
paul@0 | 241 | return "; ".join(map(str, self.nodes)) |
paul@0 | 242 | else: |
paul@0 | 243 | return "(%s)" % ", ".join(map(str, self.nodes)) |
paul@0 | 244 | |
paul@0 | 245 | class Assert(Node): |
paul@0 | 246 | def __init__(self, test, fail, lineno=None): |
paul@0 | 247 | self.test = test |
paul@0 | 248 | self.fail = fail |
paul@0 | 249 | self.lineno = lineno |
paul@0 | 250 | |
paul@0 | 251 | def getChildren(self): |
paul@0 | 252 | children = [] |
paul@0 | 253 | children.append(self.test) |
paul@0 | 254 | children.append(self.fail) |
paul@0 | 255 | return tuple(children) |
paul@0 | 256 | |
paul@0 | 257 | def getChildNodes(self): |
paul@0 | 258 | nodelist = [] |
paul@0 | 259 | nodelist.append(self.test) |
paul@0 | 260 | if self.fail is not None: |
paul@0 | 261 | nodelist.append(self.fail) |
paul@0 | 262 | return tuple(nodelist) |
paul@0 | 263 | |
paul@0 | 264 | def __repr__(self): |
paul@0 | 265 | return "Assert(%r, %r)" % (self.test, self.fail) |
paul@0 | 266 | |
paul@0 | 267 | def __str__(self): |
paul@0 | 268 | return "assert %s%s" % (self.test, self.fail and ", %s" % self.fail or "") |
paul@0 | 269 | |
paul@0 | 270 | class Assign(Node): |
paul@0 | 271 | def __init__(self, nodes, expr, lineno=None): |
paul@0 | 272 | self.nodes = nodes |
paul@0 | 273 | self.expr = expr |
paul@0 | 274 | self.lineno = lineno |
paul@0 | 275 | |
paul@0 | 276 | def getChildren(self): |
paul@0 | 277 | children = [] |
paul@0 | 278 | children.extend(flatten(self.nodes)) |
paul@0 | 279 | children.append(self.expr) |
paul@0 | 280 | return tuple(children) |
paul@0 | 281 | |
paul@0 | 282 | def getChildNodes(self): |
paul@0 | 283 | nodelist = [] |
paul@0 | 284 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 285 | nodelist.append(self.expr) |
paul@0 | 286 | return tuple(nodelist) |
paul@0 | 287 | |
paul@0 | 288 | def __repr__(self): |
paul@0 | 289 | return "Assign(%r, %r)" % (self.nodes, self.expr) |
paul@0 | 290 | |
paul@0 | 291 | def __str__(self): |
paul@0 | 292 | return "%s = %s" % (", ".join(map(str, self.nodes)), self.expr) |
paul@0 | 293 | |
paul@0 | 294 | class AugAssign(Node, OperatorUser): |
paul@0 | 295 | def __init__(self, node, op, expr, lineno=None): |
paul@0 | 296 | self.node = node |
paul@0 | 297 | self.op = op |
paul@0 | 298 | self.expr = expr |
paul@0 | 299 | self.lineno = lineno |
paul@0 | 300 | |
paul@0 | 301 | def getChildren(self): |
paul@0 | 302 | return self.node, self.op, self.expr |
paul@0 | 303 | |
paul@0 | 304 | def getChildNodes(self): |
paul@0 | 305 | return self.node, self.expr |
paul@0 | 306 | |
paul@0 | 307 | def __repr__(self): |
paul@0 | 308 | return "AugAssign(%r, %r, %r)" % (self.node, self.op, self.expr) |
paul@0 | 309 | |
paul@0 | 310 | def __str__(self): |
paul@0 | 311 | return "%s %s %s" % (self.node, self.op, self.expr) |
paul@0 | 312 | |
paul@0 | 313 | class Backquote(Node): |
paul@0 | 314 | def __init__(self, expr, lineno=None): |
paul@0 | 315 | self.expr = expr |
paul@0 | 316 | self.lineno = lineno |
paul@0 | 317 | |
paul@0 | 318 | def getChildren(self): |
paul@0 | 319 | return self.expr, |
paul@0 | 320 | |
paul@0 | 321 | def getChildNodes(self): |
paul@0 | 322 | return self.expr, |
paul@0 | 323 | |
paul@0 | 324 | def __repr__(self): |
paul@0 | 325 | return "Backquote(%r)" % (self.expr,) |
paul@0 | 326 | |
paul@0 | 327 | def __str__(self): |
paul@0 | 328 | return "`%s`" % self.expr |
paul@0 | 329 | |
paul@0 | 330 | class Bitand(Node, Operator): |
paul@0 | 331 | def __init__(self, nodes, lineno=None): |
paul@0 | 332 | self.nodes = nodes |
paul@0 | 333 | self.lineno = lineno |
paul@0 | 334 | |
paul@0 | 335 | def getChildren(self): |
paul@0 | 336 | return tuple(flatten(self.nodes)) |
paul@0 | 337 | |
paul@0 | 338 | def getChildNodes(self): |
paul@0 | 339 | nodelist = [] |
paul@0 | 340 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 341 | return tuple(nodelist) |
paul@0 | 342 | |
paul@0 | 343 | def __repr__(self): |
paul@0 | 344 | return "Bitand(%r)" % (self.nodes,) |
paul@0 | 345 | |
paul@0 | 346 | def __str__(self): |
paul@0 | 347 | return "(%s)" % " & ".join(map(str, self.nodes)) |
paul@0 | 348 | |
paul@0 | 349 | class Bitor(Node, Operator): |
paul@0 | 350 | def __init__(self, nodes, lineno=None): |
paul@0 | 351 | self.nodes = nodes |
paul@0 | 352 | self.lineno = lineno |
paul@0 | 353 | |
paul@0 | 354 | def getChildren(self): |
paul@0 | 355 | return tuple(flatten(self.nodes)) |
paul@0 | 356 | |
paul@0 | 357 | def getChildNodes(self): |
paul@0 | 358 | nodelist = [] |
paul@0 | 359 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 360 | return tuple(nodelist) |
paul@0 | 361 | |
paul@0 | 362 | def __repr__(self): |
paul@0 | 363 | return "Bitor(%r)" % (self.nodes,) |
paul@0 | 364 | |
paul@0 | 365 | def __str__(self): |
paul@0 | 366 | return "(%s)" % " | ".join(map(str, self.nodes)) |
paul@0 | 367 | |
paul@0 | 368 | class Bitxor(Node, Operator): |
paul@0 | 369 | def __init__(self, nodes, lineno=None): |
paul@0 | 370 | self.nodes = nodes |
paul@0 | 371 | self.lineno = lineno |
paul@0 | 372 | |
paul@0 | 373 | def getChildren(self): |
paul@0 | 374 | return tuple(flatten(self.nodes)) |
paul@0 | 375 | |
paul@0 | 376 | def getChildNodes(self): |
paul@0 | 377 | nodelist = [] |
paul@0 | 378 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 379 | return tuple(nodelist) |
paul@0 | 380 | |
paul@0 | 381 | def __repr__(self): |
paul@0 | 382 | return "Bitxor(%r)" % (self.nodes,) |
paul@0 | 383 | |
paul@0 | 384 | def __str__(self): |
paul@0 | 385 | return "(%s)" % " ^ ".join(map(str, self.nodes)) |
paul@0 | 386 | |
paul@0 | 387 | class Break(Node): |
paul@0 | 388 | def __init__(self, lineno=None): |
paul@0 | 389 | self.lineno = lineno |
paul@0 | 390 | |
paul@0 | 391 | def getChildren(self): |
paul@0 | 392 | return () |
paul@0 | 393 | |
paul@0 | 394 | def getChildNodes(self): |
paul@0 | 395 | return () |
paul@0 | 396 | |
paul@0 | 397 | def __repr__(self): |
paul@0 | 398 | return "Break()" |
paul@0 | 399 | |
paul@0 | 400 | def __str__(self): |
paul@0 | 401 | return "break" |
paul@0 | 402 | |
paul@0 | 403 | class CallFunc(Node): |
paul@0 | 404 | def __init__(self, node, args, star_args = None, dstar_args = None, lineno=None): |
paul@0 | 405 | self.node = node |
paul@0 | 406 | self.args = args |
paul@0 | 407 | self.star_args = star_args |
paul@0 | 408 | self.dstar_args = dstar_args |
paul@0 | 409 | self.lineno = lineno |
paul@0 | 410 | |
paul@0 | 411 | def getChildren(self): |
paul@0 | 412 | children = [] |
paul@0 | 413 | children.append(self.node) |
paul@0 | 414 | children.extend(flatten(self.args)) |
paul@0 | 415 | children.append(self.star_args) |
paul@0 | 416 | children.append(self.dstar_args) |
paul@0 | 417 | return tuple(children) |
paul@0 | 418 | |
paul@0 | 419 | def getChildNodes(self): |
paul@0 | 420 | nodelist = [] |
paul@0 | 421 | nodelist.append(self.node) |
paul@0 | 422 | nodelist.extend(flatten_nodes(self.args)) |
paul@0 | 423 | if self.star_args is not None: |
paul@0 | 424 | nodelist.append(self.star_args) |
paul@0 | 425 | if self.dstar_args is not None: |
paul@0 | 426 | nodelist.append(self.dstar_args) |
paul@0 | 427 | return tuple(nodelist) |
paul@0 | 428 | |
paul@0 | 429 | def __repr__(self): |
paul@0 | 430 | args = [] |
paul@0 | 431 | if self.dstar_args: |
paul@0 | 432 | args.insert(0, repr(self.dstar_args)) |
paul@0 | 433 | if args or self.star_args: |
paul@0 | 434 | args.insert(0, repr(self.star_args)) |
paul@0 | 435 | return "CallFunc(%r, %r%s)" % (self.node, self.args, args and (", %s" % ", ".join(args)) or "") |
paul@0 | 436 | |
paul@0 | 437 | def __str__(self): |
paul@0 | 438 | star_args = self.star_args and ["*%s" % self.star_args] or [] |
paul@0 | 439 | dstar_args = self.dstar_args and ["**%s" % self.dstar_args] or [] |
paul@120 | 440 | return "%s(%s)" % (self.node, ", ".join(map(str, tuple(self.args) + tuple(star_args) + tuple(dstar_args)))) |
paul@0 | 441 | |
paul@0 | 442 | class Class(Node): |
paul@0 | 443 | def __init__(self, name, bases, doc, code, decorators = None, lineno=None): |
paul@0 | 444 | self.name = name |
paul@0 | 445 | self.bases = bases |
paul@0 | 446 | self.doc = doc |
paul@0 | 447 | self.code = code |
paul@0 | 448 | self.decorators = decorators |
paul@0 | 449 | self.lineno = lineno |
paul@0 | 450 | |
paul@0 | 451 | def getChildren(self): |
paul@0 | 452 | children = [] |
paul@0 | 453 | children.append(self.name) |
paul@0 | 454 | children.extend(flatten(self.bases)) |
paul@0 | 455 | children.append(self.doc) |
paul@0 | 456 | children.append(self.code) |
paul@0 | 457 | children.append(self.decorators) |
paul@0 | 458 | return tuple(children) |
paul@0 | 459 | |
paul@0 | 460 | def getChildNodes(self): |
paul@0 | 461 | nodelist = [] |
paul@0 | 462 | nodelist.extend(flatten_nodes(self.bases)) |
paul@0 | 463 | nodelist.append(self.code) |
paul@0 | 464 | if self.decorators is not None: |
paul@0 | 465 | nodelist.append(self.decorators) |
paul@0 | 466 | return tuple(nodelist) |
paul@0 | 467 | |
paul@0 | 468 | def __repr__(self): |
paul@0 | 469 | return "Class(%r, %r, %r, %r, %r)" % (self.name, self.bases, self.doc, self.code, self.decorators) |
paul@0 | 470 | |
paul@0 | 471 | def __str__(self): |
paul@0 | 472 | return "%sclass %s%s:%s%s\n" % ( |
paul@0 | 473 | self.decorators and "%s\n" % "\n".join([("@%s" % decorator) for decorator in self.decorators]) or "", |
paul@0 | 474 | self.name, |
paul@0 | 475 | self.bases and "(%s)" % ", ".join(map(str, self.bases)) or "", |
paul@0 | 476 | self.doc and "\n\t" + docstring(self.doc) or "", |
paul@0 | 477 | indent("\n%s" % self.code) |
paul@0 | 478 | ) |
paul@0 | 479 | |
paul@0 | 480 | class Compare(Node, OperatorUser): |
paul@0 | 481 | def __init__(self, expr, ops, lineno=None): |
paul@0 | 482 | self.expr = expr |
paul@0 | 483 | self.ops = ops |
paul@0 | 484 | self.lineno = lineno |
paul@0 | 485 | |
paul@0 | 486 | def getChildren(self): |
paul@0 | 487 | children = [] |
paul@0 | 488 | children.append(self.expr) |
paul@0 | 489 | children.extend(flatten(self.ops)) |
paul@0 | 490 | return tuple(children) |
paul@0 | 491 | |
paul@0 | 492 | def getChildNodes(self): |
paul@0 | 493 | nodelist = [] |
paul@0 | 494 | nodelist.append(self.expr) |
paul@0 | 495 | nodelist.extend(flatten_nodes(self.ops)) |
paul@0 | 496 | return tuple(nodelist) |
paul@0 | 497 | |
paul@0 | 498 | def __repr__(self): |
paul@0 | 499 | return "Compare(%r, %r)" % (self.expr, self.ops) |
paul@0 | 500 | |
paul@0 | 501 | def __str__(self): |
paul@0 | 502 | return "%s %s" % (self.expr, " ".join([("%s %s" % op) for op in self.ops])) |
paul@0 | 503 | |
paul@0 | 504 | class Const(Node): |
paul@537 | 505 | def __init__(self, value, literals=None, lineno=None): |
paul@0 | 506 | self.value = value |
paul@537 | 507 | self.literals = literals |
paul@0 | 508 | self.lineno = lineno |
paul@0 | 509 | |
paul@0 | 510 | def getChildren(self): |
paul@0 | 511 | return self.value, |
paul@0 | 512 | |
paul@0 | 513 | def getChildNodes(self): |
paul@0 | 514 | return () |
paul@0 | 515 | |
paul@0 | 516 | def __repr__(self): |
paul@537 | 517 | return "Const(%r, %r)" % (self.value, self.literals) |
paul@0 | 518 | |
paul@0 | 519 | def __str__(self): |
paul@0 | 520 | return repr(self.value) |
paul@0 | 521 | |
paul@0 | 522 | class Continue(Node): |
paul@0 | 523 | def __init__(self, lineno=None): |
paul@0 | 524 | self.lineno = lineno |
paul@0 | 525 | |
paul@0 | 526 | def getChildren(self): |
paul@0 | 527 | return () |
paul@0 | 528 | |
paul@0 | 529 | def getChildNodes(self): |
paul@0 | 530 | return () |
paul@0 | 531 | |
paul@0 | 532 | def __repr__(self): |
paul@0 | 533 | return "Continue()" |
paul@0 | 534 | |
paul@0 | 535 | def __str__(self): |
paul@0 | 536 | return "continue" |
paul@0 | 537 | |
paul@0 | 538 | class Decorators(Node): |
paul@0 | 539 | def __init__(self, nodes, lineno=None): |
paul@0 | 540 | self.nodes = nodes |
paul@0 | 541 | self.lineno = lineno |
paul@0 | 542 | |
paul@0 | 543 | def getChildren(self): |
paul@0 | 544 | return tuple(flatten(self.nodes)) |
paul@0 | 545 | |
paul@0 | 546 | def getChildNodes(self): |
paul@0 | 547 | nodelist = [] |
paul@0 | 548 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 549 | return tuple(nodelist) |
paul@0 | 550 | |
paul@0 | 551 | def __repr__(self): |
paul@0 | 552 | return "Decorators(%r)" % (self.nodes,) |
paul@0 | 553 | |
paul@0 | 554 | def __str__(self): |
paul@0 | 555 | return "\n".join([("@%s" % node) for node in self.nodes]) |
paul@0 | 556 | |
paul@0 | 557 | class Dict(Node): |
paul@0 | 558 | def __init__(self, items, lineno=None): |
paul@0 | 559 | self.items = items |
paul@0 | 560 | self.lineno = lineno |
paul@0 | 561 | |
paul@0 | 562 | def getChildren(self): |
paul@0 | 563 | return tuple(flatten(self.items)) |
paul@0 | 564 | |
paul@0 | 565 | def getChildNodes(self): |
paul@0 | 566 | nodelist = [] |
paul@0 | 567 | nodelist.extend(flatten_nodes(self.items)) |
paul@0 | 568 | return tuple(nodelist) |
paul@0 | 569 | |
paul@0 | 570 | def __repr__(self): |
paul@0 | 571 | return "Dict(%r)" % (self.items,) |
paul@0 | 572 | |
paul@0 | 573 | def __str__(self): |
paul@0 | 574 | return "{%s}" % ", ".join([("%s : %s" % (key, value)) for (key, value) in self.items]) |
paul@0 | 575 | |
paul@0 | 576 | class Discard(Node): |
paul@0 | 577 | def __init__(self, expr, lineno=None): |
paul@0 | 578 | self.expr = expr |
paul@0 | 579 | self.lineno = lineno |
paul@0 | 580 | |
paul@0 | 581 | def getChildren(self): |
paul@0 | 582 | return self.expr, |
paul@0 | 583 | |
paul@0 | 584 | def getChildNodes(self): |
paul@0 | 585 | return self.expr, |
paul@0 | 586 | |
paul@0 | 587 | def __repr__(self): |
paul@0 | 588 | return "Discard(%r)" % (self.expr,) |
paul@0 | 589 | |
paul@0 | 590 | def __str__(self): |
paul@0 | 591 | return str(self.expr) |
paul@0 | 592 | |
paul@0 | 593 | class Div(Node, Operator): |
paul@0 | 594 | def __init__(self, leftright, lineno=None): |
paul@0 | 595 | self.left = leftright[0] |
paul@0 | 596 | self.right = leftright[1] |
paul@0 | 597 | self.lineno = lineno |
paul@0 | 598 | |
paul@0 | 599 | def getChildren(self): |
paul@0 | 600 | return self.left, self.right |
paul@0 | 601 | |
paul@0 | 602 | def getChildNodes(self): |
paul@0 | 603 | return self.left, self.right |
paul@0 | 604 | |
paul@0 | 605 | def __repr__(self): |
paul@0 | 606 | return "Div((%r, %r))" % (self.left, self.right) |
paul@0 | 607 | |
paul@0 | 608 | def __str__(self): |
paul@0 | 609 | return "(%s / %s)" % (self.left, self.right) |
paul@0 | 610 | |
paul@0 | 611 | class Ellipsis(Node): |
paul@0 | 612 | def __init__(self, lineno=None): |
paul@0 | 613 | self.lineno = lineno |
paul@0 | 614 | |
paul@0 | 615 | def getChildren(self): |
paul@0 | 616 | return () |
paul@0 | 617 | |
paul@0 | 618 | def getChildNodes(self): |
paul@0 | 619 | return () |
paul@0 | 620 | |
paul@0 | 621 | def __repr__(self): |
paul@0 | 622 | return "Ellipsis()" |
paul@0 | 623 | |
paul@0 | 624 | def __str__(self): |
paul@0 | 625 | return "..." |
paul@0 | 626 | |
paul@0 | 627 | class Exec(Node): |
paul@0 | 628 | def __init__(self, expr, locals, globals, lineno=None): |
paul@0 | 629 | self.expr = expr |
paul@0 | 630 | self.locals = locals |
paul@0 | 631 | self.globals = globals |
paul@0 | 632 | self.lineno = lineno |
paul@0 | 633 | |
paul@0 | 634 | def getChildren(self): |
paul@0 | 635 | children = [] |
paul@0 | 636 | children.append(self.expr) |
paul@0 | 637 | children.append(self.locals) |
paul@0 | 638 | children.append(self.globals) |
paul@0 | 639 | return tuple(children) |
paul@0 | 640 | |
paul@0 | 641 | def getChildNodes(self): |
paul@0 | 642 | nodelist = [] |
paul@0 | 643 | nodelist.append(self.expr) |
paul@0 | 644 | if self.locals is not None: |
paul@0 | 645 | nodelist.append(self.locals) |
paul@0 | 646 | if self.globals is not None: |
paul@0 | 647 | nodelist.append(self.globals) |
paul@0 | 648 | return tuple(nodelist) |
paul@0 | 649 | |
paul@0 | 650 | def __repr__(self): |
paul@0 | 651 | return "Exec(%r, %r, %r)" % (self.expr, self.locals, self.globals) |
paul@0 | 652 | |
paul@0 | 653 | def __str__(self): |
paul@0 | 654 | return "exec %s%s%s" % (self.expr, self.locals and "in %s" % self.locals or "", |
paul@0 | 655 | self.globals and ", %s" % self.globals or "") |
paul@0 | 656 | |
paul@0 | 657 | class FloorDiv(Node, Operator): |
paul@0 | 658 | def __init__(self, leftright, lineno=None): |
paul@0 | 659 | self.left = leftright[0] |
paul@0 | 660 | self.right = leftright[1] |
paul@0 | 661 | self.lineno = lineno |
paul@0 | 662 | |
paul@0 | 663 | def getChildren(self): |
paul@0 | 664 | return self.left, self.right |
paul@0 | 665 | |
paul@0 | 666 | def getChildNodes(self): |
paul@0 | 667 | return self.left, self.right |
paul@0 | 668 | |
paul@0 | 669 | def __repr__(self): |
paul@0 | 670 | return "FloorDiv((%r, %r))" % (self.left, self.right) |
paul@0 | 671 | |
paul@0 | 672 | def __str__(self): |
paul@0 | 673 | return "(%s // %s)" % (self.left, self.right) |
paul@0 | 674 | |
paul@0 | 675 | class For(Node): |
paul@0 | 676 | def __init__(self, assign, list, body, else_, lineno=None): |
paul@0 | 677 | self.assign = assign |
paul@0 | 678 | self.list = list |
paul@0 | 679 | self.body = body |
paul@0 | 680 | self.else_ = else_ |
paul@0 | 681 | self.lineno = lineno |
paul@0 | 682 | |
paul@0 | 683 | def getChildren(self): |
paul@0 | 684 | children = [] |
paul@0 | 685 | children.append(self.assign) |
paul@0 | 686 | children.append(self.list) |
paul@0 | 687 | children.append(self.body) |
paul@0 | 688 | children.append(self.else_) |
paul@0 | 689 | return tuple(children) |
paul@0 | 690 | |
paul@0 | 691 | def getChildNodes(self): |
paul@0 | 692 | nodelist = [] |
paul@0 | 693 | nodelist.append(self.assign) |
paul@0 | 694 | nodelist.append(self.list) |
paul@0 | 695 | nodelist.append(self.body) |
paul@0 | 696 | if self.else_ is not None: |
paul@0 | 697 | nodelist.append(self.else_) |
paul@0 | 698 | return tuple(nodelist) |
paul@0 | 699 | |
paul@0 | 700 | def __repr__(self): |
paul@0 | 701 | return "For(%r, %r, %r, %r)" % (self.assign, self.list, self.body, self.else_) |
paul@0 | 702 | |
paul@0 | 703 | def __str__(self): |
paul@0 | 704 | return "for %s in %s:%s%s" % ( |
paul@0 | 705 | self.assign, self.list, |
paul@0 | 706 | indent("\n%s" % self.body), |
paul@0 | 707 | self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or "" |
paul@0 | 708 | ) |
paul@0 | 709 | |
paul@0 | 710 | class From(Node): |
paul@0 | 711 | def __init__(self, modname, names, level, lineno=None): |
paul@0 | 712 | self.modname = modname |
paul@0 | 713 | self.names = names |
paul@0 | 714 | self.level = level |
paul@0 | 715 | self.lineno = lineno |
paul@0 | 716 | |
paul@0 | 717 | def getChildren(self): |
paul@0 | 718 | return self.modname, self.names, self.level |
paul@0 | 719 | |
paul@0 | 720 | def getChildNodes(self): |
paul@0 | 721 | return () |
paul@0 | 722 | |
paul@0 | 723 | def __repr__(self): |
paul@0 | 724 | return "From(%r, %r, %r)" % (self.modname, self.names, self.level) |
paul@0 | 725 | |
paul@0 | 726 | def __str__(self): |
paul@0 | 727 | return "from %s import %s" % (self.modname, |
paul@0 | 728 | ", ".join([(alias and "%s as %s" % (name, alias) or name) for (name, alias) in self.names])) |
paul@0 | 729 | |
paul@0 | 730 | class Function(Node): |
paul@0 | 731 | def __init__(self, decorators, name, argnames, defaults, flags, doc, code, lineno=None): |
paul@0 | 732 | self.decorators = decorators |
paul@0 | 733 | self.name = name |
paul@0 | 734 | self.argnames = argnames |
paul@0 | 735 | self.defaults = defaults |
paul@0 | 736 | self.flags = flags |
paul@0 | 737 | self.doc = doc |
paul@0 | 738 | self.code = code |
paul@0 | 739 | self.lineno = lineno |
paul@0 | 740 | self.varargs = self.kwargs = None |
paul@0 | 741 | if flags & CO_VARARGS: |
paul@0 | 742 | self.varargs = 1 |
paul@0 | 743 | if flags & CO_VARKEYWORDS: |
paul@0 | 744 | self.kwargs = 1 |
paul@0 | 745 | |
paul@0 | 746 | def getChildren(self): |
paul@0 | 747 | children = [] |
paul@0 | 748 | children.append(self.decorators) |
paul@0 | 749 | children.append(self.name) |
paul@0 | 750 | children.append(self.argnames) |
paul@0 | 751 | children.extend(flatten(self.defaults)) |
paul@0 | 752 | children.append(self.flags) |
paul@0 | 753 | children.append(self.doc) |
paul@0 | 754 | children.append(self.code) |
paul@0 | 755 | return tuple(children) |
paul@0 | 756 | |
paul@0 | 757 | def getChildNodes(self): |
paul@0 | 758 | nodelist = [] |
paul@0 | 759 | if self.decorators is not None: |
paul@0 | 760 | nodelist.append(self.decorators) |
paul@0 | 761 | nodelist.extend(flatten_nodes(self.defaults)) |
paul@0 | 762 | nodelist.append(self.code) |
paul@0 | 763 | return tuple(nodelist) |
paul@0 | 764 | |
paul@0 | 765 | def __repr__(self): |
paul@0 | 766 | return "Function(%r, %r, %r, %r, %r, %r, %r)" % (self.decorators, self.name, self.argnames, self.defaults, self.flags, self.doc, self.code) |
paul@0 | 767 | |
paul@0 | 768 | def __str__(self): |
paul@0 | 769 | parameters = decode_function(self) |
paul@0 | 770 | |
paul@0 | 771 | return "%sdef %s(%s):%s%s\n" % ( |
paul@0 | 772 | self.decorators and "%s\n" % "\n".join([("@%s" % decorator) for decorator in self.decorators]) or "", |
paul@0 | 773 | self.name, |
paul@0 | 774 | ", ".join(parameters), |
paul@0 | 775 | self.doc and "\n\n\t%s\n" % docstring(self.doc) or "", |
paul@0 | 776 | indent("\n%s" % self.code) |
paul@0 | 777 | ) |
paul@0 | 778 | |
paul@0 | 779 | class GenExpr(Node): |
paul@0 | 780 | def __init__(self, code, lineno=None): |
paul@0 | 781 | self.code = code |
paul@0 | 782 | self.lineno = lineno |
paul@0 | 783 | self.argnames = ['.0'] |
paul@0 | 784 | self.varargs = self.kwargs = None |
paul@0 | 785 | |
paul@0 | 786 | def getChildren(self): |
paul@0 | 787 | return self.code, |
paul@0 | 788 | |
paul@0 | 789 | def getChildNodes(self): |
paul@0 | 790 | return self.code, |
paul@0 | 791 | |
paul@0 | 792 | def __repr__(self): |
paul@0 | 793 | return "GenExpr(%r)" % (self.code,) |
paul@0 | 794 | |
paul@0 | 795 | def __str__(self): |
paul@0 | 796 | return str(self.code) |
paul@0 | 797 | |
paul@0 | 798 | class GenExprFor(Node): |
paul@0 | 799 | def __init__(self, assign, iter, ifs, lineno=None): |
paul@0 | 800 | self.assign = assign |
paul@0 | 801 | self.iter = iter |
paul@0 | 802 | self.ifs = ifs |
paul@0 | 803 | self.lineno = lineno |
paul@0 | 804 | self.is_outmost = False |
paul@0 | 805 | |
paul@0 | 806 | def getChildren(self): |
paul@0 | 807 | children = [] |
paul@0 | 808 | children.append(self.assign) |
paul@0 | 809 | children.append(self.iter) |
paul@0 | 810 | children.extend(flatten(self.ifs)) |
paul@0 | 811 | return tuple(children) |
paul@0 | 812 | |
paul@0 | 813 | def getChildNodes(self): |
paul@0 | 814 | nodelist = [] |
paul@0 | 815 | nodelist.append(self.assign) |
paul@0 | 816 | nodelist.append(self.iter) |
paul@0 | 817 | nodelist.extend(flatten_nodes(self.ifs)) |
paul@0 | 818 | return tuple(nodelist) |
paul@0 | 819 | |
paul@0 | 820 | def __repr__(self): |
paul@0 | 821 | return "GenExprFor(%r, %r, %r)" % (self.assign, self.iter, self.ifs) |
paul@0 | 822 | |
paul@0 | 823 | def __str__(self): |
paul@0 | 824 | return "for %s in %s%s" % ( |
paul@0 | 825 | self.assign, self.iter, |
paul@0 | 826 | self.ifs and " ".join(map(str, self.ifs)) or "" |
paul@0 | 827 | ) |
paul@0 | 828 | |
paul@0 | 829 | class GenExprIf(Node): |
paul@0 | 830 | def __init__(self, test, lineno=None): |
paul@0 | 831 | self.test = test |
paul@0 | 832 | self.lineno = lineno |
paul@0 | 833 | |
paul@0 | 834 | def getChildren(self): |
paul@0 | 835 | return self.test, |
paul@0 | 836 | |
paul@0 | 837 | def getChildNodes(self): |
paul@0 | 838 | return self.test, |
paul@0 | 839 | |
paul@0 | 840 | def __repr__(self): |
paul@0 | 841 | return "GenExprIf(%r)" % (self.test,) |
paul@0 | 842 | |
paul@0 | 843 | def __str__(self): |
paul@0 | 844 | return "if %s" % self.test |
paul@0 | 845 | |
paul@0 | 846 | class GenExprInner(Node): |
paul@0 | 847 | def __init__(self, expr, quals, lineno=None): |
paul@0 | 848 | self.expr = expr |
paul@0 | 849 | self.quals = quals |
paul@0 | 850 | self.lineno = lineno |
paul@0 | 851 | |
paul@0 | 852 | def getChildren(self): |
paul@0 | 853 | children = [] |
paul@0 | 854 | children.append(self.expr) |
paul@0 | 855 | children.extend(flatten(self.quals)) |
paul@0 | 856 | return tuple(children) |
paul@0 | 857 | |
paul@0 | 858 | def getChildNodes(self): |
paul@0 | 859 | nodelist = [] |
paul@0 | 860 | nodelist.append(self.expr) |
paul@0 | 861 | nodelist.extend(flatten_nodes(self.quals)) |
paul@0 | 862 | return tuple(nodelist) |
paul@0 | 863 | |
paul@0 | 864 | def __repr__(self): |
paul@0 | 865 | return "GenExprInner(%r, %r)" % (self.expr, self.quals) |
paul@0 | 866 | |
paul@0 | 867 | def __str__(self): |
paul@0 | 868 | return "%s %s" % (self.expr, " ".join(map(str, self.quals))) |
paul@0 | 869 | |
paul@0 | 870 | class Getattr(Node): |
paul@0 | 871 | def __init__(self, expr, attrname, lineno=None): |
paul@0 | 872 | self.expr = expr |
paul@0 | 873 | self.attrname = attrname |
paul@0 | 874 | self.lineno = lineno |
paul@0 | 875 | |
paul@0 | 876 | def getChildren(self): |
paul@0 | 877 | return self.expr, self.attrname |
paul@0 | 878 | |
paul@0 | 879 | def getChildNodes(self): |
paul@0 | 880 | return self.expr, |
paul@0 | 881 | |
paul@0 | 882 | def __repr__(self): |
paul@0 | 883 | return "Getattr(%r, %r)" % (self.expr, self.attrname) |
paul@0 | 884 | |
paul@0 | 885 | def __str__(self): |
paul@0 | 886 | return "%s.%s" % (self.expr, self.attrname) |
paul@0 | 887 | |
paul@0 | 888 | class Global(Node): |
paul@0 | 889 | def __init__(self, names, lineno=None): |
paul@0 | 890 | self.names = names |
paul@0 | 891 | self.lineno = lineno |
paul@0 | 892 | |
paul@0 | 893 | def getChildren(self): |
paul@0 | 894 | return self.names, |
paul@0 | 895 | |
paul@0 | 896 | def getChildNodes(self): |
paul@0 | 897 | return () |
paul@0 | 898 | |
paul@0 | 899 | def __repr__(self): |
paul@0 | 900 | return "Global(%r)" % (self.names,) |
paul@0 | 901 | |
paul@0 | 902 | def __str__(self): |
paul@0 | 903 | return "global %s" % ", ".join(map(str, self.names)) |
paul@0 | 904 | |
paul@0 | 905 | class If(Node): |
paul@0 | 906 | def __init__(self, tests, else_, lineno=None): |
paul@0 | 907 | self.tests = tests |
paul@0 | 908 | self.else_ = else_ |
paul@0 | 909 | self.lineno = lineno |
paul@0 | 910 | |
paul@0 | 911 | def getChildren(self): |
paul@0 | 912 | children = [] |
paul@0 | 913 | children.extend(flatten(self.tests)) |
paul@0 | 914 | children.append(self.else_) |
paul@0 | 915 | return tuple(children) |
paul@0 | 916 | |
paul@0 | 917 | def getChildNodes(self): |
paul@0 | 918 | nodelist = [] |
paul@0 | 919 | nodelist.extend(flatten_nodes(self.tests)) |
paul@0 | 920 | if self.else_ is not None: |
paul@0 | 921 | nodelist.append(self.else_) |
paul@0 | 922 | return tuple(nodelist) |
paul@0 | 923 | |
paul@0 | 924 | def __repr__(self): |
paul@0 | 925 | return "If(%r, %r)" % (self.tests, self.else_) |
paul@0 | 926 | |
paul@0 | 927 | def __str__(self): |
paul@0 | 928 | tests = [("%sif %s:%s" % (i > 0 and "el" or "", test, indent("\n%s" % body))) for (i, (test, body)) in enumerate(self.tests)] |
paul@0 | 929 | return "%s%s" % ( |
paul@0 | 930 | "\n".join(tests), |
paul@0 | 931 | self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or "" |
paul@0 | 932 | ) |
paul@0 | 933 | |
paul@0 | 934 | class Import(Node): |
paul@0 | 935 | def __init__(self, names, lineno=None): |
paul@0 | 936 | self.names = names |
paul@0 | 937 | self.lineno = lineno |
paul@0 | 938 | |
paul@0 | 939 | def getChildren(self): |
paul@0 | 940 | return self.names, |
paul@0 | 941 | |
paul@0 | 942 | def getChildNodes(self): |
paul@0 | 943 | return () |
paul@0 | 944 | |
paul@0 | 945 | def __repr__(self): |
paul@0 | 946 | return "Import(%r)" % (self.names,) |
paul@0 | 947 | |
paul@0 | 948 | def __str__(self): |
paul@0 | 949 | return "import %s" % ( |
paul@0 | 950 | ", ".join([(alias and "%s as %s" % (name, alias) or name) for (name, alias) in self.names])) |
paul@0 | 951 | |
paul@0 | 952 | class Invert(Node, Operator): |
paul@0 | 953 | def __init__(self, expr, lineno=None): |
paul@0 | 954 | self.expr = expr |
paul@0 | 955 | self.lineno = lineno |
paul@0 | 956 | |
paul@0 | 957 | def getChildren(self): |
paul@0 | 958 | return self.expr, |
paul@0 | 959 | |
paul@0 | 960 | def getChildNodes(self): |
paul@0 | 961 | return self.expr, |
paul@0 | 962 | |
paul@0 | 963 | def __repr__(self): |
paul@0 | 964 | return "Invert(%r)" % (self.expr,) |
paul@0 | 965 | |
paul@0 | 966 | def __str__(self): |
paul@0 | 967 | return "~%s" % self.expr |
paul@0 | 968 | |
paul@0 | 969 | class Keyword(Node): |
paul@0 | 970 | def __init__(self, name, expr, lineno=None): |
paul@0 | 971 | self.name = name |
paul@0 | 972 | self.expr = expr |
paul@0 | 973 | self.lineno = lineno |
paul@0 | 974 | |
paul@0 | 975 | def getChildren(self): |
paul@0 | 976 | return self.name, self.expr |
paul@0 | 977 | |
paul@0 | 978 | def getChildNodes(self): |
paul@0 | 979 | return self.expr, |
paul@0 | 980 | |
paul@0 | 981 | def __repr__(self): |
paul@0 | 982 | return "Keyword(%r, %r)" % (self.name, self.expr) |
paul@0 | 983 | |
paul@0 | 984 | def __str__(self): |
paul@0 | 985 | return "%s=%s" % (self.name, self.expr) |
paul@0 | 986 | |
paul@0 | 987 | class Lambda(Node): |
paul@0 | 988 | def __init__(self, argnames, defaults, flags, code, lineno=None): |
paul@0 | 989 | self.argnames = argnames |
paul@0 | 990 | self.defaults = defaults |
paul@0 | 991 | self.flags = flags |
paul@0 | 992 | self.code = code |
paul@0 | 993 | self.lineno = lineno |
paul@0 | 994 | self.varargs = self.kwargs = None |
paul@0 | 995 | if flags & CO_VARARGS: |
paul@0 | 996 | self.varargs = 1 |
paul@0 | 997 | if flags & CO_VARKEYWORDS: |
paul@0 | 998 | self.kwargs = 1 |
paul@0 | 999 | |
paul@0 | 1000 | def getChildren(self): |
paul@0 | 1001 | children = [] |
paul@0 | 1002 | children.append(self.argnames) |
paul@0 | 1003 | children.extend(flatten(self.defaults)) |
paul@0 | 1004 | children.append(self.flags) |
paul@0 | 1005 | children.append(self.code) |
paul@0 | 1006 | return tuple(children) |
paul@0 | 1007 | |
paul@0 | 1008 | def getChildNodes(self): |
paul@0 | 1009 | nodelist = [] |
paul@0 | 1010 | nodelist.extend(flatten_nodes(self.defaults)) |
paul@0 | 1011 | nodelist.append(self.code) |
paul@0 | 1012 | return tuple(nodelist) |
paul@0 | 1013 | |
paul@0 | 1014 | def __repr__(self): |
paul@0 | 1015 | return "Lambda(%r, %r, %r, %r)" % (self.argnames, self.defaults, self.flags, self.code) |
paul@0 | 1016 | |
paul@0 | 1017 | def __str__(self): |
paul@0 | 1018 | parameters = decode_function(self) |
paul@0 | 1019 | return "lambda %s: %s" % (", ".join(parameters), self.code) |
paul@0 | 1020 | |
paul@0 | 1021 | class LeftShift(Node, Operator): |
paul@0 | 1022 | def __init__(self, leftright, lineno=None): |
paul@0 | 1023 | self.left = leftright[0] |
paul@0 | 1024 | self.right = leftright[1] |
paul@0 | 1025 | self.lineno = lineno |
paul@0 | 1026 | |
paul@0 | 1027 | def getChildren(self): |
paul@0 | 1028 | return self.left, self.right |
paul@0 | 1029 | |
paul@0 | 1030 | def getChildNodes(self): |
paul@0 | 1031 | return self.left, self.right |
paul@0 | 1032 | |
paul@0 | 1033 | def __repr__(self): |
paul@0 | 1034 | return "LeftShift((%r, %r))" % (self.left, self.right) |
paul@0 | 1035 | |
paul@0 | 1036 | def __str__(self): |
paul@0 | 1037 | return "(%s << %s)" % (self.left, self.right) |
paul@0 | 1038 | |
paul@0 | 1039 | class List(Node): |
paul@0 | 1040 | def __init__(self, nodes, lineno=None): |
paul@0 | 1041 | self.nodes = nodes |
paul@0 | 1042 | self.lineno = lineno |
paul@0 | 1043 | |
paul@0 | 1044 | def getChildren(self): |
paul@0 | 1045 | return tuple(flatten(self.nodes)) |
paul@0 | 1046 | |
paul@0 | 1047 | def getChildNodes(self): |
paul@0 | 1048 | nodelist = [] |
paul@0 | 1049 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 1050 | return tuple(nodelist) |
paul@0 | 1051 | |
paul@0 | 1052 | def __repr__(self): |
paul@0 | 1053 | return "List(%r)" % (self.nodes,) |
paul@0 | 1054 | |
paul@0 | 1055 | def __str__(self): |
paul@0 | 1056 | return "[%s]" % ", ".join(map(str, self.nodes)) |
paul@0 | 1057 | |
paul@0 | 1058 | class Mod(Node, Operator): |
paul@0 | 1059 | def __init__(self, leftright, lineno=None): |
paul@0 | 1060 | self.left = leftright[0] |
paul@0 | 1061 | self.right = leftright[1] |
paul@0 | 1062 | self.lineno = lineno |
paul@0 | 1063 | |
paul@0 | 1064 | def getChildren(self): |
paul@0 | 1065 | return self.left, self.right |
paul@0 | 1066 | |
paul@0 | 1067 | def getChildNodes(self): |
paul@0 | 1068 | return self.left, self.right |
paul@0 | 1069 | |
paul@0 | 1070 | def __repr__(self): |
paul@0 | 1071 | return "Mod((%r, %r))" % (self.left, self.right) |
paul@0 | 1072 | |
paul@0 | 1073 | def __str__(self): |
paul@0 | 1074 | return "(%s %% %s)" % (self.left, self.right) |
paul@0 | 1075 | |
paul@0 | 1076 | class Module(Node): |
paul@0 | 1077 | def __init__(self, doc, node, lineno=None): |
paul@0 | 1078 | self.doc = doc |
paul@0 | 1079 | self.node = node |
paul@0 | 1080 | self.lineno = lineno |
paul@0 | 1081 | |
paul@0 | 1082 | def getChildren(self): |
paul@0 | 1083 | return self.doc, self.node |
paul@0 | 1084 | |
paul@0 | 1085 | def getChildNodes(self): |
paul@0 | 1086 | return self.node, |
paul@0 | 1087 | |
paul@0 | 1088 | def __repr__(self): |
paul@0 | 1089 | return "Module(%r, %r)" % (self.doc, self.node) |
paul@0 | 1090 | |
paul@0 | 1091 | def __str__(self): |
paul@0 | 1092 | return "%s%s" % (self.doc and "%s\n\n" % docstring(self.doc) or "", self.node) |
paul@0 | 1093 | |
paul@0 | 1094 | class Mul(Node, Operator): |
paul@0 | 1095 | def __init__(self, leftright, lineno=None): |
paul@0 | 1096 | self.left = leftright[0] |
paul@0 | 1097 | self.right = leftright[1] |
paul@0 | 1098 | self.lineno = lineno |
paul@0 | 1099 | |
paul@0 | 1100 | def getChildren(self): |
paul@0 | 1101 | return self.left, self.right |
paul@0 | 1102 | |
paul@0 | 1103 | def getChildNodes(self): |
paul@0 | 1104 | return self.left, self.right |
paul@0 | 1105 | |
paul@0 | 1106 | def __repr__(self): |
paul@0 | 1107 | return "Mul((%r, %r))" % (self.left, self.right) |
paul@0 | 1108 | |
paul@0 | 1109 | def __str__(self): |
paul@0 | 1110 | return "(%s * %s)" % (self.left, self.right) |
paul@0 | 1111 | |
paul@0 | 1112 | class Name(Node): |
paul@0 | 1113 | def __init__(self, name, lineno=None): |
paul@0 | 1114 | self.name = name |
paul@0 | 1115 | self.lineno = lineno |
paul@0 | 1116 | |
paul@0 | 1117 | def getChildren(self): |
paul@0 | 1118 | return self.name, |
paul@0 | 1119 | |
paul@0 | 1120 | def getChildNodes(self): |
paul@0 | 1121 | return () |
paul@0 | 1122 | |
paul@0 | 1123 | def __repr__(self): |
paul@0 | 1124 | return "Name(%r)" % (self.name,) |
paul@0 | 1125 | |
paul@0 | 1126 | def __str__(self): |
paul@0 | 1127 | return str(self.name) |
paul@0 | 1128 | |
paul@0 | 1129 | class Not(Node): |
paul@0 | 1130 | def __init__(self, expr, lineno=None): |
paul@0 | 1131 | self.expr = expr |
paul@0 | 1132 | self.lineno = lineno |
paul@0 | 1133 | |
paul@0 | 1134 | def getChildren(self): |
paul@0 | 1135 | return self.expr, |
paul@0 | 1136 | |
paul@0 | 1137 | def getChildNodes(self): |
paul@0 | 1138 | return self.expr, |
paul@0 | 1139 | |
paul@0 | 1140 | def __repr__(self): |
paul@0 | 1141 | return "Not(%r)" % (self.expr,) |
paul@0 | 1142 | |
paul@0 | 1143 | def __str__(self): |
paul@0 | 1144 | return "not %s" % self.expr |
paul@0 | 1145 | |
paul@0 | 1146 | class Or(Node): |
paul@0 | 1147 | def __init__(self, nodes, lineno=None): |
paul@0 | 1148 | self.nodes = nodes |
paul@0 | 1149 | self.lineno = lineno |
paul@0 | 1150 | |
paul@0 | 1151 | def getChildren(self): |
paul@0 | 1152 | return tuple(flatten(self.nodes)) |
paul@0 | 1153 | |
paul@0 | 1154 | def getChildNodes(self): |
paul@0 | 1155 | nodelist = [] |
paul@0 | 1156 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 1157 | return tuple(nodelist) |
paul@0 | 1158 | |
paul@0 | 1159 | def __repr__(self): |
paul@0 | 1160 | return "Or(%r)" % (self.nodes,) |
paul@0 | 1161 | |
paul@0 | 1162 | def __str__(self): |
paul@0 | 1163 | return "(%s)" % " or ".join(map(str, self.nodes)) |
paul@0 | 1164 | |
paul@0 | 1165 | class Pass(Node): |
paul@0 | 1166 | def __init__(self, lineno=None): |
paul@0 | 1167 | self.lineno = lineno |
paul@0 | 1168 | |
paul@0 | 1169 | def getChildren(self): |
paul@0 | 1170 | return () |
paul@0 | 1171 | |
paul@0 | 1172 | def getChildNodes(self): |
paul@0 | 1173 | return () |
paul@0 | 1174 | |
paul@0 | 1175 | def __repr__(self): |
paul@0 | 1176 | return "Pass()" |
paul@0 | 1177 | |
paul@0 | 1178 | def __str__(self): |
paul@0 | 1179 | return "pass" |
paul@0 | 1180 | |
paul@0 | 1181 | class Power(Node, Operator): |
paul@0 | 1182 | def __init__(self, leftright, lineno=None): |
paul@0 | 1183 | self.left = leftright[0] |
paul@0 | 1184 | self.right = leftright[1] |
paul@0 | 1185 | self.lineno = lineno |
paul@0 | 1186 | |
paul@0 | 1187 | def getChildren(self): |
paul@0 | 1188 | return self.left, self.right |
paul@0 | 1189 | |
paul@0 | 1190 | def getChildNodes(self): |
paul@0 | 1191 | return self.left, self.right |
paul@0 | 1192 | |
paul@0 | 1193 | def __repr__(self): |
paul@0 | 1194 | return "Power((%r, %r))" % (self.left, self.right) |
paul@0 | 1195 | |
paul@0 | 1196 | def __str__(self): |
paul@0 | 1197 | return "(%s ** %s)" % (self.left, self.right) |
paul@0 | 1198 | |
paul@0 | 1199 | class Print(Node): |
paul@0 | 1200 | def __init__(self, nodes, dest, lineno=None): |
paul@0 | 1201 | self.nodes = nodes |
paul@0 | 1202 | self.dest = dest |
paul@0 | 1203 | self.lineno = lineno |
paul@0 | 1204 | |
paul@0 | 1205 | def getChildren(self): |
paul@0 | 1206 | children = [] |
paul@0 | 1207 | children.extend(flatten(self.nodes)) |
paul@0 | 1208 | children.append(self.dest) |
paul@0 | 1209 | return tuple(children) |
paul@0 | 1210 | |
paul@0 | 1211 | def getChildNodes(self): |
paul@0 | 1212 | nodelist = [] |
paul@0 | 1213 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 1214 | if self.dest is not None: |
paul@0 | 1215 | nodelist.append(self.dest) |
paul@0 | 1216 | return tuple(nodelist) |
paul@0 | 1217 | |
paul@0 | 1218 | def __repr__(self): |
paul@0 | 1219 | return "Print(%r, %r)" % (self.nodes, self.dest) |
paul@0 | 1220 | |
paul@0 | 1221 | def __str__(self): |
paul@0 | 1222 | dest = self.dest and [">>%s" % self.dest] or [] |
paul@0 | 1223 | return "print %s," % ", ".join(map(str, dest + self.nodes)) |
paul@0 | 1224 | |
paul@0 | 1225 | class Printnl(Node): |
paul@0 | 1226 | def __init__(self, nodes, dest, lineno=None): |
paul@0 | 1227 | self.nodes = nodes |
paul@0 | 1228 | self.dest = dest |
paul@0 | 1229 | self.lineno = lineno |
paul@0 | 1230 | |
paul@0 | 1231 | def getChildren(self): |
paul@0 | 1232 | children = [] |
paul@0 | 1233 | children.extend(flatten(self.nodes)) |
paul@0 | 1234 | children.append(self.dest) |
paul@0 | 1235 | return tuple(children) |
paul@0 | 1236 | |
paul@0 | 1237 | def getChildNodes(self): |
paul@0 | 1238 | nodelist = [] |
paul@0 | 1239 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 1240 | if self.dest is not None: |
paul@0 | 1241 | nodelist.append(self.dest) |
paul@0 | 1242 | return tuple(nodelist) |
paul@0 | 1243 | |
paul@0 | 1244 | def __repr__(self): |
paul@0 | 1245 | return "Printnl(%r, %r)" % (self.nodes, self.dest) |
paul@0 | 1246 | |
paul@0 | 1247 | def __str__(self): |
paul@0 | 1248 | dest = self.dest and [">>%s" % self.dest] or [] |
paul@0 | 1249 | return "print %s" % ", ".join(map(str, dest + self.nodes)) |
paul@0 | 1250 | |
paul@0 | 1251 | class Raise(Node): |
paul@0 | 1252 | def __init__(self, expr1, expr2, expr3, lineno=None): |
paul@0 | 1253 | self.expr1 = expr1 |
paul@0 | 1254 | self.expr2 = expr2 |
paul@0 | 1255 | self.expr3 = expr3 |
paul@0 | 1256 | self.lineno = lineno |
paul@0 | 1257 | |
paul@0 | 1258 | def getChildren(self): |
paul@0 | 1259 | children = [] |
paul@0 | 1260 | children.append(self.expr1) |
paul@0 | 1261 | children.append(self.expr2) |
paul@0 | 1262 | children.append(self.expr3) |
paul@0 | 1263 | return tuple(children) |
paul@0 | 1264 | |
paul@0 | 1265 | def getChildNodes(self): |
paul@0 | 1266 | nodelist = [] |
paul@0 | 1267 | if self.expr1 is not None: |
paul@0 | 1268 | nodelist.append(self.expr1) |
paul@0 | 1269 | if self.expr2 is not None: |
paul@0 | 1270 | nodelist.append(self.expr2) |
paul@0 | 1271 | if self.expr3 is not None: |
paul@0 | 1272 | nodelist.append(self.expr3) |
paul@0 | 1273 | return tuple(nodelist) |
paul@0 | 1274 | |
paul@0 | 1275 | def __repr__(self): |
paul@0 | 1276 | return "Raise(%r, %r, %r)" % (self.expr1, self.expr2, self.expr3) |
paul@0 | 1277 | |
paul@0 | 1278 | def __str__(self): |
paul@0 | 1279 | args = self.expr1 and [self.expr1] or [] |
paul@0 | 1280 | args += self.expr2 and [self.expr2] or [] |
paul@0 | 1281 | args += self.expr3 and [self.expr3] or [] |
paul@0 | 1282 | return "raise %s" % ", ".join(map(str, args)) |
paul@0 | 1283 | |
paul@0 | 1284 | class Return(Node): |
paul@0 | 1285 | def __init__(self, value, lineno=None): |
paul@0 | 1286 | self.value = value |
paul@0 | 1287 | self.lineno = lineno |
paul@0 | 1288 | |
paul@0 | 1289 | def getChildren(self): |
paul@0 | 1290 | return self.value, |
paul@0 | 1291 | |
paul@0 | 1292 | def getChildNodes(self): |
paul@0 | 1293 | return self.value, |
paul@0 | 1294 | |
paul@0 | 1295 | def __repr__(self): |
paul@0 | 1296 | return "Return(%r)" % (self.value,) |
paul@0 | 1297 | |
paul@0 | 1298 | def __str__(self): |
paul@0 | 1299 | return "return %s" % self.value |
paul@0 | 1300 | |
paul@0 | 1301 | class RightShift(Node, Operator): |
paul@0 | 1302 | def __init__(self, leftright, lineno=None): |
paul@0 | 1303 | self.left = leftright[0] |
paul@0 | 1304 | self.right = leftright[1] |
paul@0 | 1305 | self.lineno = lineno |
paul@0 | 1306 | |
paul@0 | 1307 | def getChildren(self): |
paul@0 | 1308 | return self.left, self.right |
paul@0 | 1309 | |
paul@0 | 1310 | def getChildNodes(self): |
paul@0 | 1311 | return self.left, self.right |
paul@0 | 1312 | |
paul@0 | 1313 | def __repr__(self): |
paul@0 | 1314 | return "RightShift((%r, %r))" % (self.left, self.right) |
paul@0 | 1315 | |
paul@0 | 1316 | def __str__(self): |
paul@0 | 1317 | return "(%s >> %s)" % (self.left, self.right) |
paul@0 | 1318 | |
paul@0 | 1319 | class Set(Node): |
paul@0 | 1320 | def __init__(self, nodes, lineno=None): |
paul@0 | 1321 | self.nodes = nodes |
paul@0 | 1322 | self.lineno = lineno |
paul@0 | 1323 | |
paul@0 | 1324 | def getChildren(self): |
paul@0 | 1325 | return tuple(flatten(self.nodes)) |
paul@0 | 1326 | |
paul@0 | 1327 | def getChildNodes(self): |
paul@0 | 1328 | nodelist = [] |
paul@0 | 1329 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 1330 | return tuple(nodelist) |
paul@0 | 1331 | |
paul@0 | 1332 | def __repr__(self): |
paul@0 | 1333 | return "Set(%r)" % (self.nodes,) |
paul@0 | 1334 | |
paul@0 | 1335 | def __str__(self): |
paul@0 | 1336 | return "{%s}" % ", ".join(map(str, self.nodes)) |
paul@0 | 1337 | |
paul@0 | 1338 | class Slice(Node, OperatorUser): |
paul@0 | 1339 | def __init__(self, expr, flags, lower, upper, lineno=None): |
paul@0 | 1340 | self.expr = expr |
paul@0 | 1341 | self.flags = flags |
paul@0 | 1342 | self.lower = lower |
paul@0 | 1343 | self.upper = upper |
paul@0 | 1344 | self.lineno = lineno |
paul@0 | 1345 | |
paul@0 | 1346 | def getChildren(self): |
paul@0 | 1347 | children = [] |
paul@0 | 1348 | children.append(self.expr) |
paul@0 | 1349 | children.append(self.flags) |
paul@0 | 1350 | children.append(self.lower) |
paul@0 | 1351 | children.append(self.upper) |
paul@0 | 1352 | return tuple(children) |
paul@0 | 1353 | |
paul@0 | 1354 | def getChildNodes(self): |
paul@0 | 1355 | nodelist = [] |
paul@0 | 1356 | nodelist.append(self.expr) |
paul@0 | 1357 | if self.lower is not None: |
paul@0 | 1358 | nodelist.append(self.lower) |
paul@0 | 1359 | if self.upper is not None: |
paul@0 | 1360 | nodelist.append(self.upper) |
paul@0 | 1361 | return tuple(nodelist) |
paul@0 | 1362 | |
paul@0 | 1363 | def __repr__(self): |
paul@0 | 1364 | return "Slice(%r, %r, %r, %r)" % (self.expr, self.flags, self.lower, self.upper) |
paul@0 | 1365 | |
paul@0 | 1366 | def __str__(self): |
paul@0 | 1367 | args = [self.lower or "", self.upper or ""] |
paul@0 | 1368 | return "%s%s[%s]" % (self.flags == "OP_DELETE" and "del " or "", self.expr, ":".join(map(str, args))) |
paul@0 | 1369 | |
paul@0 | 1370 | class Sliceobj(Node): |
paul@0 | 1371 | def __init__(self, nodes, lineno=None): |
paul@0 | 1372 | self.nodes = nodes |
paul@0 | 1373 | self.lineno = lineno |
paul@0 | 1374 | |
paul@0 | 1375 | def getChildren(self): |
paul@0 | 1376 | return tuple(flatten(self.nodes)) |
paul@0 | 1377 | |
paul@0 | 1378 | def getChildNodes(self): |
paul@0 | 1379 | nodelist = [] |
paul@0 | 1380 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 1381 | return tuple(nodelist) |
paul@0 | 1382 | |
paul@0 | 1383 | def __repr__(self): |
paul@0 | 1384 | return "Sliceobj(%r)" % (self.nodes,) |
paul@0 | 1385 | |
paul@0 | 1386 | def __str__(self): |
paul@0 | 1387 | return ":".join(map(str, self.nodes)) |
paul@0 | 1388 | |
paul@0 | 1389 | class Stmt(Node): |
paul@0 | 1390 | def __init__(self, nodes, lineno=None): |
paul@0 | 1391 | self.nodes = nodes |
paul@0 | 1392 | self.lineno = lineno |
paul@0 | 1393 | |
paul@0 | 1394 | def getChildren(self): |
paul@0 | 1395 | return tuple(flatten(self.nodes)) |
paul@0 | 1396 | |
paul@0 | 1397 | def getChildNodes(self): |
paul@0 | 1398 | nodelist = [] |
paul@0 | 1399 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 1400 | return tuple(nodelist) |
paul@0 | 1401 | |
paul@0 | 1402 | def __repr__(self): |
paul@0 | 1403 | return "Stmt(%r)" % (self.nodes,) |
paul@0 | 1404 | |
paul@0 | 1405 | def __str__(self): |
paul@0 | 1406 | return "\n".join(map(str, flatten_statement(self.nodes))) |
paul@0 | 1407 | |
paul@0 | 1408 | class Sub(Node, Operator): |
paul@0 | 1409 | def __init__(self, leftright, lineno=None): |
paul@0 | 1410 | self.left = leftright[0] |
paul@0 | 1411 | self.right = leftright[1] |
paul@0 | 1412 | self.lineno = lineno |
paul@0 | 1413 | |
paul@0 | 1414 | def getChildren(self): |
paul@0 | 1415 | return self.left, self.right |
paul@0 | 1416 | |
paul@0 | 1417 | def getChildNodes(self): |
paul@0 | 1418 | return self.left, self.right |
paul@0 | 1419 | |
paul@0 | 1420 | def __repr__(self): |
paul@0 | 1421 | return "Sub((%r, %r))" % (self.left, self.right) |
paul@0 | 1422 | |
paul@0 | 1423 | def __str__(self): |
paul@0 | 1424 | return "(%s - %s)" % (self.left, self.right) |
paul@0 | 1425 | |
paul@0 | 1426 | class Subscript(Node, OperatorUser): |
paul@0 | 1427 | def __init__(self, expr, flags, subs, lineno=None): |
paul@0 | 1428 | self.expr = expr |
paul@0 | 1429 | self.flags = flags |
paul@0 | 1430 | self.subs = subs |
paul@0 | 1431 | self.lineno = lineno |
paul@0 | 1432 | |
paul@0 | 1433 | def getChildren(self): |
paul@0 | 1434 | children = [] |
paul@0 | 1435 | children.append(self.expr) |
paul@0 | 1436 | children.append(self.flags) |
paul@0 | 1437 | children.extend(flatten(self.subs)) |
paul@0 | 1438 | return tuple(children) |
paul@0 | 1439 | |
paul@0 | 1440 | def getChildNodes(self): |
paul@0 | 1441 | nodelist = [] |
paul@0 | 1442 | nodelist.append(self.expr) |
paul@0 | 1443 | nodelist.extend(flatten_nodes(self.subs)) |
paul@0 | 1444 | return tuple(nodelist) |
paul@0 | 1445 | |
paul@0 | 1446 | def __repr__(self): |
paul@0 | 1447 | return "Subscript(%r, %r, %r)" % (self.expr, self.flags, self.subs) |
paul@0 | 1448 | |
paul@0 | 1449 | def __str__(self): |
paul@0 | 1450 | return "%s%s[%s]" % (self.flags == "OP_DELETE" and "del " or "", self.expr, ",".join(map(str, self.subs))) |
paul@0 | 1451 | |
paul@0 | 1452 | class TryExcept(Node): |
paul@0 | 1453 | def __init__(self, body, handlers, else_, lineno=None): |
paul@0 | 1454 | self.body = body |
paul@0 | 1455 | self.handlers = handlers |
paul@0 | 1456 | self.else_ = else_ |
paul@0 | 1457 | self.lineno = lineno |
paul@0 | 1458 | |
paul@0 | 1459 | def getChildren(self): |
paul@0 | 1460 | children = [] |
paul@0 | 1461 | children.append(self.body) |
paul@0 | 1462 | children.extend(flatten(self.handlers)) |
paul@0 | 1463 | children.append(self.else_) |
paul@0 | 1464 | return tuple(children) |
paul@0 | 1465 | |
paul@0 | 1466 | def getChildNodes(self): |
paul@0 | 1467 | nodelist = [] |
paul@0 | 1468 | nodelist.append(self.body) |
paul@0 | 1469 | nodelist.extend(flatten_nodes(self.handlers)) |
paul@0 | 1470 | if self.else_ is not None: |
paul@0 | 1471 | nodelist.append(self.else_) |
paul@0 | 1472 | return tuple(nodelist) |
paul@0 | 1473 | |
paul@0 | 1474 | def __repr__(self): |
paul@0 | 1475 | return "TryExcept(%r, %r, %r)" % (self.body, self.handlers, self.else_) |
paul@0 | 1476 | |
paul@0 | 1477 | def __str__(self): |
paul@0 | 1478 | handlers = [ |
paul@0 | 1479 | ("\nexcept%s%s:%s" % (spec and " %s" % spec or "", assign and ", %s" % assign or "", indent("\n%s" % statement))) |
paul@0 | 1480 | for (spec, assign, statement) in self.handlers |
paul@0 | 1481 | ] |
paul@0 | 1482 | |
paul@0 | 1483 | return "try:%s%s%s" % ( |
paul@0 | 1484 | indent("\n%s" % self.body), |
paul@0 | 1485 | "".join(handlers), |
paul@0 | 1486 | self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or "" |
paul@0 | 1487 | ) |
paul@0 | 1488 | |
paul@0 | 1489 | class TryFinally(Node): |
paul@0 | 1490 | def __init__(self, body, final, lineno=None): |
paul@0 | 1491 | self.body = body |
paul@0 | 1492 | self.final = final |
paul@0 | 1493 | self.lineno = lineno |
paul@0 | 1494 | |
paul@0 | 1495 | def getChildren(self): |
paul@0 | 1496 | return self.body, self.final |
paul@0 | 1497 | |
paul@0 | 1498 | def getChildNodes(self): |
paul@0 | 1499 | return self.body, self.final |
paul@0 | 1500 | |
paul@0 | 1501 | def __repr__(self): |
paul@0 | 1502 | return "TryFinally(%r, %r)" % (self.body, self.final) |
paul@0 | 1503 | |
paul@0 | 1504 | def __str__(self): |
paul@0 | 1505 | return "try:%s\nfinally:%s" % ( |
paul@0 | 1506 | indent("\n%s" % self.body), |
paul@0 | 1507 | indent("\n%s" % self.final) |
paul@0 | 1508 | ) |
paul@0 | 1509 | |
paul@0 | 1510 | class Tuple(Node): |
paul@0 | 1511 | def __init__(self, nodes, lineno=None): |
paul@0 | 1512 | self.nodes = nodes |
paul@0 | 1513 | self.lineno = lineno |
paul@0 | 1514 | |
paul@0 | 1515 | def getChildren(self): |
paul@0 | 1516 | return tuple(flatten(self.nodes)) |
paul@0 | 1517 | |
paul@0 | 1518 | def getChildNodes(self): |
paul@0 | 1519 | nodelist = [] |
paul@0 | 1520 | nodelist.extend(flatten_nodes(self.nodes)) |
paul@0 | 1521 | return tuple(nodelist) |
paul@0 | 1522 | |
paul@0 | 1523 | def __repr__(self): |
paul@0 | 1524 | return "Tuple(%r)" % (self.nodes,) |
paul@0 | 1525 | |
paul@0 | 1526 | def __str__(self): |
paul@0 | 1527 | return "(%s)" % ", ".join(map(str, self.nodes)) |
paul@0 | 1528 | |
paul@0 | 1529 | class UnaryAdd(Node, Operator): |
paul@0 | 1530 | def __init__(self, expr, lineno=None): |
paul@0 | 1531 | self.expr = expr |
paul@0 | 1532 | self.lineno = lineno |
paul@0 | 1533 | |
paul@0 | 1534 | def getChildren(self): |
paul@0 | 1535 | return self.expr, |
paul@0 | 1536 | |
paul@0 | 1537 | def getChildNodes(self): |
paul@0 | 1538 | return self.expr, |
paul@0 | 1539 | |
paul@0 | 1540 | def __repr__(self): |
paul@0 | 1541 | return "UnaryAdd(%r)" % (self.expr,) |
paul@0 | 1542 | |
paul@0 | 1543 | def __str__(self): |
paul@0 | 1544 | return "+%s" % self.expr |
paul@0 | 1545 | |
paul@0 | 1546 | class UnarySub(Node, Operator): |
paul@0 | 1547 | def __init__(self, expr, lineno=None): |
paul@0 | 1548 | self.expr = expr |
paul@0 | 1549 | self.lineno = lineno |
paul@0 | 1550 | |
paul@0 | 1551 | def getChildren(self): |
paul@0 | 1552 | return self.expr, |
paul@0 | 1553 | |
paul@0 | 1554 | def getChildNodes(self): |
paul@0 | 1555 | return self.expr, |
paul@0 | 1556 | |
paul@0 | 1557 | def __repr__(self): |
paul@0 | 1558 | return "UnarySub(%r)" % (self.expr,) |
paul@0 | 1559 | |
paul@0 | 1560 | def __str__(self): |
paul@0 | 1561 | return "-%s" % self.expr |
paul@0 | 1562 | |
paul@0 | 1563 | class While(Node): |
paul@0 | 1564 | def __init__(self, test, body, else_, lineno=None): |
paul@0 | 1565 | self.test = test |
paul@0 | 1566 | self.body = body |
paul@0 | 1567 | self.else_ = else_ |
paul@0 | 1568 | self.lineno = lineno |
paul@0 | 1569 | |
paul@0 | 1570 | def getChildren(self): |
paul@0 | 1571 | children = [] |
paul@0 | 1572 | children.append(self.test) |
paul@0 | 1573 | children.append(self.body) |
paul@0 | 1574 | children.append(self.else_) |
paul@0 | 1575 | return tuple(children) |
paul@0 | 1576 | |
paul@0 | 1577 | def getChildNodes(self): |
paul@0 | 1578 | nodelist = [] |
paul@0 | 1579 | nodelist.append(self.test) |
paul@0 | 1580 | nodelist.append(self.body) |
paul@0 | 1581 | if self.else_ is not None: |
paul@0 | 1582 | nodelist.append(self.else_) |
paul@0 | 1583 | return tuple(nodelist) |
paul@0 | 1584 | |
paul@0 | 1585 | def __repr__(self): |
paul@0 | 1586 | return "While(%r, %r, %r)" % (self.test, self.body, self.else_) |
paul@0 | 1587 | |
paul@0 | 1588 | def __str__(self): |
paul@0 | 1589 | return "while %s:%s%s" % ( |
paul@0 | 1590 | self.test, |
paul@0 | 1591 | indent("\n%s" % self.body), |
paul@0 | 1592 | self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or "" |
paul@0 | 1593 | ) |
paul@0 | 1594 | |
paul@0 | 1595 | class With(Node): |
paul@0 | 1596 | def __init__(self, expr, vars, body, lineno=None): |
paul@0 | 1597 | self.expr = expr |
paul@0 | 1598 | self.vars = vars |
paul@0 | 1599 | self.body = body |
paul@0 | 1600 | self.lineno = lineno |
paul@0 | 1601 | |
paul@0 | 1602 | def getChildren(self): |
paul@0 | 1603 | children = [] |
paul@0 | 1604 | children.append(self.expr) |
paul@0 | 1605 | children.append(self.vars) |
paul@0 | 1606 | children.append(self.body) |
paul@0 | 1607 | return tuple(children) |
paul@0 | 1608 | |
paul@0 | 1609 | def getChildNodes(self): |
paul@0 | 1610 | nodelist = [] |
paul@0 | 1611 | nodelist.append(self.expr) |
paul@0 | 1612 | if self.vars is not None: |
paul@0 | 1613 | nodelist.append(self.vars) |
paul@0 | 1614 | nodelist.append(self.body) |
paul@0 | 1615 | return tuple(nodelist) |
paul@0 | 1616 | |
paul@0 | 1617 | def __repr__(self): |
paul@0 | 1618 | return "With(%r, %r, %r)" % (self.expr, self.vars, self.body) |
paul@0 | 1619 | |
paul@0 | 1620 | def __str__(self): |
paul@0 | 1621 | return "with %s%s:%s" % ( |
paul@0 | 1622 | self.expr, |
paul@0 | 1623 | self.vars and " as %s" % ", ".join(map(str, self.vars)), |
paul@0 | 1624 | indent("\n%s" % self.body), |
paul@0 | 1625 | ) |
paul@0 | 1626 | |
paul@0 | 1627 | for name, obj in globals().items(): |
paul@0 | 1628 | if isinstance(obj, type) and issubclass(obj, Node): |
paul@0 | 1629 | nodes[name.lower()] = obj |