paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Inspect source files, obtaining details of classes and attributes. |
paul@0 | 5 | |
paul@0 | 6 | Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 11 | version. |
paul@0 | 12 | |
paul@0 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 16 | details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public License along with |
paul@0 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 20 | """ |
paul@0 | 21 | |
paul@0 | 22 | import compiler.ast |
paul@0 | 23 | from compiler.visitor import ASTVisitor |
paul@0 | 24 | try: |
paul@0 | 25 | set |
paul@0 | 26 | except NameError: |
paul@0 | 27 | from sets import Set as set |
paul@0 | 28 | |
paul@0 | 29 | class InspectError(Exception): |
paul@0 | 30 | |
paul@0 | 31 | "An inspection error." |
paul@0 | 32 | |
paul@0 | 33 | pass |
paul@0 | 34 | |
paul@3 | 35 | class NamespaceDict: |
paul@3 | 36 | |
paul@3 | 37 | "A mix-in providing dictionary methods." |
paul@3 | 38 | |
paul@3 | 39 | def __getitem__(self, name): |
paul@3 | 40 | return self.namespace[name] |
paul@3 | 41 | |
paul@3 | 42 | def keys(self): |
paul@3 | 43 | return self.namespace.keys() |
paul@3 | 44 | |
paul@7 | 45 | def values(self): |
paul@7 | 46 | return self.namespace.values() |
paul@7 | 47 | |
paul@7 | 48 | def items(self): |
paul@7 | 49 | return self.namespace.items() |
paul@7 | 50 | |
paul@4 | 51 | class Naming: |
paul@4 | 52 | |
paul@4 | 53 | "A mix-in providing naming conveniences." |
paul@4 | 54 | |
paul@4 | 55 | def full_name(self): |
paul@4 | 56 | if self.name is not None: |
paul@7 | 57 | return self.parent_name + "." + self.name |
paul@4 | 58 | else: |
paul@7 | 59 | return self.parent_name |
paul@4 | 60 | |
paul@10 | 61 | class Attr: |
paul@10 | 62 | |
paul@10 | 63 | "An attribute entry." |
paul@10 | 64 | |
paul@10 | 65 | def __init__(self, position, parent, value=None): |
paul@10 | 66 | self.position = position |
paul@10 | 67 | self.parent = parent |
paul@10 | 68 | self.value = value |
paul@10 | 69 | |
paul@10 | 70 | def __repr__(self): |
paul@10 | 71 | return "Attr(%d, %r, %r)" % (self.position, self.parent, self.value) |
paul@10 | 72 | |
paul@4 | 73 | class Class(NamespaceDict, Naming): |
paul@0 | 74 | |
paul@0 | 75 | "An inspected class." |
paul@0 | 76 | |
paul@7 | 77 | def __init__(self, name, parent_name): |
paul@0 | 78 | self.name = name |
paul@7 | 79 | self.parent_name = parent_name |
paul@0 | 80 | self.bases = [] |
paul@0 | 81 | self.namespace = {} |
paul@9 | 82 | self.instattr = set() # instance attributes |
paul@7 | 83 | self.instattr_names = [] # from instattr |
paul@9 | 84 | self.classattr = None # cache for class_attributes |
paul@10 | 85 | self.classattr_names = None # from classattr |
paul@9 | 86 | self.allattr = None # cache for all_attributes |
paul@10 | 87 | self.allattr_names = None # from allattr |
paul@9 | 88 | |
paul@9 | 89 | # Image generation details. |
paul@9 | 90 | |
paul@9 | 91 | self.location = None |
paul@0 | 92 | |
paul@0 | 93 | def add_base(self, base): |
paul@0 | 94 | self.bases.append(base) |
paul@0 | 95 | |
paul@4 | 96 | def __repr__(self): |
paul@9 | 97 | return "Class(%r, %r, location=%r)" % (self.name, self.parent_name, self.location) |
paul@9 | 98 | |
paul@9 | 99 | def class_attribute_names(self): |
paul@9 | 100 | |
paul@9 | 101 | "Return the attribute names provided by the class." |
paul@9 | 102 | |
paul@10 | 103 | if self.classattr_names is None: |
paul@10 | 104 | self.class_attributes() |
paul@9 | 105 | return self.classattr_names |
paul@4 | 106 | |
paul@4 | 107 | def class_attributes(self): |
paul@4 | 108 | |
paul@4 | 109 | "Return all class attributes, indicating the class which provides them." |
paul@4 | 110 | |
paul@9 | 111 | if self.classattr is None: |
paul@9 | 112 | self.classattr = {} |
paul@10 | 113 | self.classattr_names = self.namespace.keys() |
paul@10 | 114 | |
paul@9 | 115 | reversed_bases = self.bases[:] |
paul@9 | 116 | reversed_bases.reverse() |
paul@9 | 117 | for cls in reversed_bases: |
paul@9 | 118 | self.classattr.update(cls.class_attributes()) |
paul@10 | 119 | |
paul@10 | 120 | # Record attributes provided by this class, along with their |
paul@10 | 121 | # positions. |
paul@10 | 122 | |
paul@10 | 123 | for i, name in enumerate(self.classattr_names): |
paul@10 | 124 | self.classattr[name] = Attr(i, self, self.namespace[name]) |
paul@9 | 125 | |
paul@9 | 126 | return self.classattr |
paul@4 | 127 | |
paul@7 | 128 | def instance_attribute_names(self): |
paul@7 | 129 | |
paul@7 | 130 | "Return the instance attribute names provided by the class." |
paul@7 | 131 | |
paul@7 | 132 | self.instattr_names = self.instattr_names or list(self.instattr) |
paul@7 | 133 | return self.instattr_names |
paul@7 | 134 | |
paul@4 | 135 | def instance_attributes(self): |
paul@4 | 136 | |
paul@9 | 137 | "Return instance-only attributes for instances of this class." |
paul@9 | 138 | |
paul@9 | 139 | return self.instattr |
paul@9 | 140 | |
paul@9 | 141 | def all_attribute_names(self): |
paul@9 | 142 | |
paul@9 | 143 | """ |
paul@9 | 144 | Return the names of all attributes provided by instances of this class. |
paul@9 | 145 | """ |
paul@9 | 146 | |
paul@9 | 147 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@9 | 148 | return self.allattr_names |
paul@9 | 149 | |
paul@9 | 150 | def all_attributes(self): |
paul@9 | 151 | |
paul@4 | 152 | """ |
paul@4 | 153 | Return all attributes for an instance, indicating either the class which |
paul@4 | 154 | provides them or that the instance itself provides them. |
paul@4 | 155 | """ |
paul@4 | 156 | |
paul@9 | 157 | if self.allattr is None: |
paul@9 | 158 | self.allattr = {} |
paul@9 | 159 | self.allattr.update(self.class_attributes()) |
paul@9 | 160 | for i, name in enumerate(self.instance_attribute_names()): |
paul@9 | 161 | if self.allattr.has_key(name): |
paul@9 | 162 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@10 | 163 | self.allattr[name] = Attr(i, None) |
paul@9 | 164 | return self.allattr |
paul@4 | 165 | |
paul@7 | 166 | class Function(Naming): |
paul@0 | 167 | |
paul@0 | 168 | "An inspected function." |
paul@0 | 169 | |
paul@7 | 170 | def __init__(self, name, parent_name, argnames, has_star, has_dstar): |
paul@3 | 171 | self.name = name |
paul@7 | 172 | self.parent_name = parent_name |
paul@3 | 173 | self.argnames = argnames |
paul@3 | 174 | self.has_star = has_star |
paul@3 | 175 | self.has_dstar = has_dstar |
paul@3 | 176 | |
paul@9 | 177 | # Image generation details. |
paul@9 | 178 | |
paul@9 | 179 | self.location = None |
paul@9 | 180 | |
paul@4 | 181 | def __repr__(self): |
paul@9 | 182 | return "Function(%r, %r, %r, %r, %r, location=%r)" % ( |
paul@9 | 183 | self.name, self.parent_name, self.argnames, self.has_star, self.has_dstar, self.location |
paul@4 | 184 | ) |
paul@4 | 185 | |
paul@7 | 186 | def parameters(self): |
paul@7 | 187 | |
paul@7 | 188 | """ |
paul@7 | 189 | Return a dictionary mapping parameter names to their position in the |
paul@7 | 190 | parameter list. |
paul@7 | 191 | """ |
paul@7 | 192 | |
paul@7 | 193 | parameters = {} |
paul@7 | 194 | for i, name in enumerate(self.argnames): |
paul@7 | 195 | parameters[name] = i |
paul@7 | 196 | return parameters |
paul@7 | 197 | |
paul@4 | 198 | class UnresolvedName(NamespaceDict, Naming): |
paul@3 | 199 | |
paul@3 | 200 | "A module, class or function which was mentioned but could not be imported." |
paul@3 | 201 | |
paul@7 | 202 | def __init__(self, name, parent_name): |
paul@0 | 203 | self.name = name |
paul@7 | 204 | self.parent_name = parent_name |
paul@3 | 205 | self.namespace = {} |
paul@0 | 206 | |
paul@7 | 207 | def class_attributes(self): |
paul@7 | 208 | return {} |
paul@7 | 209 | |
paul@4 | 210 | def __repr__(self): |
paul@7 | 211 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@4 | 212 | |
paul@3 | 213 | class Module(ASTVisitor, NamespaceDict): |
paul@0 | 214 | |
paul@0 | 215 | "An inspected module." |
paul@0 | 216 | |
paul@1 | 217 | def __init__(self, name, importer=None): |
paul@0 | 218 | ASTVisitor.__init__(self) |
paul@0 | 219 | self.visitor = self |
paul@0 | 220 | |
paul@1 | 221 | self.name = name |
paul@0 | 222 | self.importer = importer |
paul@2 | 223 | self.loaded = 0 |
paul@0 | 224 | |
paul@1 | 225 | # Module namespace. |
paul@1 | 226 | |
paul@0 | 227 | self.namespace = {} |
paul@7 | 228 | self.namespace_names = [] # from namespace |
paul@7 | 229 | |
paul@10 | 230 | # Module attributes. |
paul@10 | 231 | |
paul@10 | 232 | self.modattr = None # cache for module_attributes |
paul@10 | 233 | self.modattr_names = None # from modattr |
paul@10 | 234 | |
paul@7 | 235 | # Complete lists of classes and functions. |
paul@7 | 236 | |
paul@7 | 237 | self.all_objects = [] |
paul@0 | 238 | |
paul@1 | 239 | # Current expression state. |
paul@1 | 240 | |
paul@1 | 241 | self.expr = None |
paul@1 | 242 | |
paul@1 | 243 | # Namespace state. |
paul@1 | 244 | |
paul@0 | 245 | self.in_global = 1 |
paul@0 | 246 | self.in_init = 0 |
paul@0 | 247 | self.classes = [] |
paul@0 | 248 | self.module = None |
paul@0 | 249 | |
paul@9 | 250 | # Image generation details. |
paul@9 | 251 | |
paul@9 | 252 | self.location = None |
paul@9 | 253 | |
paul@0 | 254 | def parse(self, filename): |
paul@9 | 255 | |
paul@9 | 256 | "Parse the file having the given 'filename'." |
paul@9 | 257 | |
paul@0 | 258 | module = compiler.parseFile(filename) |
paul@0 | 259 | self.process(module) |
paul@0 | 260 | |
paul@0 | 261 | def process(self, module): |
paul@9 | 262 | |
paul@9 | 263 | "Process the given 'module'." |
paul@9 | 264 | |
paul@0 | 265 | self.module = module |
paul@1 | 266 | processed = self.dispatch(module) |
paul@1 | 267 | if self.namespace.has_key("__all__"): |
paul@1 | 268 | all = self.namespace["__all__"] |
paul@1 | 269 | if isinstance(all, compiler.ast.List): |
paul@1 | 270 | for n in all.nodes: |
paul@1 | 271 | self.namespace[n.value] = self.importer.add_module(self.name + "." + n.value) |
paul@1 | 272 | return processed |
paul@0 | 273 | |
paul@3 | 274 | def vacuum(self): |
paul@9 | 275 | |
paul@9 | 276 | "Vacuum the module namespace, removing unloaded module references." |
paul@9 | 277 | |
paul@3 | 278 | for name, value in self.namespace.items(): |
paul@3 | 279 | if isinstance(value, Module) and not value.loaded: |
paul@3 | 280 | del self.namespace[name] |
paul@0 | 281 | |
paul@4 | 282 | def full_name(self): |
paul@4 | 283 | return self.name |
paul@4 | 284 | |
paul@4 | 285 | def __repr__(self): |
paul@9 | 286 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@4 | 287 | |
paul@7 | 288 | # Attribute methods. |
paul@7 | 289 | |
paul@7 | 290 | def module_attribute_names(self): |
paul@7 | 291 | |
paul@7 | 292 | "Return the module attribute names provided by the module." |
paul@7 | 293 | |
paul@10 | 294 | if self.modattr_names is None: |
paul@10 | 295 | self.module_attributes() |
paul@10 | 296 | return self.modattr_names |
paul@7 | 297 | |
paul@7 | 298 | def module_attributes(self): |
paul@7 | 299 | |
paul@9 | 300 | "Return a dictionary mapping names to module attributes." |
paul@7 | 301 | |
paul@10 | 302 | if self.modattr is None: |
paul@10 | 303 | self.modattr = {} |
paul@10 | 304 | self.modattr_names = self.namespace.keys() |
paul@10 | 305 | for i, name in enumerate(self.modattr_names): |
paul@10 | 306 | self.modattr[name] = Attr(i, self, self.namespace[name]) |
paul@10 | 307 | |
paul@10 | 308 | return self.modattr |
paul@7 | 309 | |
paul@3 | 310 | # Namespace methods. |
paul@0 | 311 | |
paul@0 | 312 | def store(self, name, obj): |
paul@0 | 313 | if self.in_global: |
paul@0 | 314 | if not self.classes: |
paul@0 | 315 | self.namespace[name] = obj |
paul@0 | 316 | else: |
paul@0 | 317 | self.classes[-1].namespace[name] = obj |
paul@7 | 318 | self.all_objects.append(obj) |
paul@0 | 319 | |
paul@0 | 320 | def store_attr(self, name): |
paul@0 | 321 | if self.in_init: |
paul@0 | 322 | self.classes[-1].instattr.add(name) |
paul@0 | 323 | |
paul@7 | 324 | def get_parent(self): |
paul@7 | 325 | return (self.classes[-1:] or [self])[0] |
paul@7 | 326 | |
paul@3 | 327 | # Visitor methods. |
paul@3 | 328 | |
paul@3 | 329 | def default(self, node, *args): |
paul@3 | 330 | raise InspectError, node.__class__ |
paul@3 | 331 | |
paul@3 | 332 | def dispatch(self, node, *args): |
paul@3 | 333 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 334 | |
paul@0 | 335 | def NOP(self, node): |
paul@0 | 336 | return node |
paul@0 | 337 | |
paul@1 | 338 | visitAdd = NOP |
paul@1 | 339 | |
paul@0 | 340 | visitAnd = NOP |
paul@0 | 341 | |
paul@4 | 342 | visitAssert = NOP |
paul@4 | 343 | |
paul@0 | 344 | def visitAssign(self, node): |
paul@0 | 345 | self.expr = self.dispatch(node.expr) |
paul@0 | 346 | for n in node.nodes: |
paul@0 | 347 | self.dispatch(n) |
paul@0 | 348 | return None |
paul@0 | 349 | |
paul@0 | 350 | def visitAssAttr(self, node): |
paul@0 | 351 | expr = self.dispatch(node.expr) |
paul@0 | 352 | if expr is not None and isinstance(expr, Self): |
paul@0 | 353 | self.store_attr(node.attrname) |
paul@0 | 354 | return None |
paul@0 | 355 | |
paul@0 | 356 | def visitAssList(self, node): |
paul@0 | 357 | for n in node.nodes: |
paul@0 | 358 | self.dispatch(n) |
paul@0 | 359 | return None |
paul@0 | 360 | |
paul@0 | 361 | def visitAssName(self, node): |
paul@0 | 362 | self.store(node.name, self.expr) |
paul@0 | 363 | return None |
paul@0 | 364 | |
paul@0 | 365 | visitAssTuple = visitAssList |
paul@0 | 366 | |
paul@2 | 367 | visitAugAssign = NOP |
paul@2 | 368 | |
paul@4 | 369 | visitBackquote = NOP |
paul@4 | 370 | |
paul@2 | 371 | visitBitand = NOP |
paul@2 | 372 | |
paul@2 | 373 | visitBitor = NOP |
paul@2 | 374 | |
paul@4 | 375 | visitBitxor = NOP |
paul@4 | 376 | |
paul@4 | 377 | visitBreak = NOP |
paul@4 | 378 | |
paul@0 | 379 | visitCallFunc = NOP |
paul@0 | 380 | |
paul@0 | 381 | def visitClass(self, node): |
paul@0 | 382 | if not self.in_global: |
paul@4 | 383 | print "Class %r in %r is not global: ignored." % (node.name, self) |
paul@0 | 384 | else: |
paul@7 | 385 | cls = Class(node.name, self.get_parent().full_name()) |
paul@0 | 386 | for base in node.bases: |
paul@0 | 387 | base_ref = self.dispatch(base) |
paul@0 | 388 | if base_ref is None: |
paul@4 | 389 | raise InspectError, "Base class %r for class %r in %r is not found: it may be hidden in some way." % (base, self, cls) |
paul@0 | 390 | cls.add_base(base_ref) |
paul@0 | 391 | |
paul@0 | 392 | # Make an entry for the class. |
paul@0 | 393 | |
paul@0 | 394 | self.store(node.name, cls) |
paul@0 | 395 | |
paul@0 | 396 | self.classes.append(cls) |
paul@0 | 397 | self.dispatch(node.code) |
paul@0 | 398 | self.classes.pop() |
paul@0 | 399 | |
paul@0 | 400 | return node |
paul@0 | 401 | |
paul@4 | 402 | visitCompare = NOP |
paul@4 | 403 | |
paul@0 | 404 | visitConst = NOP |
paul@0 | 405 | |
paul@4 | 406 | visitContinue = NOP |
paul@4 | 407 | |
paul@4 | 408 | visitDecorators = NOP |
paul@4 | 409 | |
paul@0 | 410 | visitDict = NOP |
paul@0 | 411 | |
paul@0 | 412 | visitDiscard = NOP |
paul@0 | 413 | |
paul@4 | 414 | visitDiv = NOP |
paul@4 | 415 | |
paul@4 | 416 | visitEllipsis = NOP |
paul@4 | 417 | |
paul@4 | 418 | visitExec = NOP |
paul@4 | 419 | |
paul@4 | 420 | visitExpression = NOP |
paul@4 | 421 | |
paul@4 | 422 | visitFloorDiv = NOP |
paul@4 | 423 | |
paul@0 | 424 | visitFor = NOP |
paul@0 | 425 | |
paul@0 | 426 | def visitFrom(self, node): |
paul@0 | 427 | if self.importer is None: |
paul@0 | 428 | raise InspectError, "Please use the micropython.Importer class for code which uses the 'from' statement." |
paul@0 | 429 | |
paul@0 | 430 | module = self.importer.load(node.modname, 1) |
paul@0 | 431 | |
paul@0 | 432 | if module is None: |
paul@0 | 433 | print "Warning:", node.modname, "not imported." |
paul@0 | 434 | |
paul@0 | 435 | for name, alias in node.names: |
paul@0 | 436 | if name != "*": |
paul@4 | 437 | if module is not None and module.namespace.has_key(name): |
paul@3 | 438 | self.namespace[alias or name] = attr = module.namespace[name] |
paul@2 | 439 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 440 | self.importer.load(attr.name) |
paul@3 | 441 | else: |
paul@4 | 442 | self.namespace[alias or name] = UnresolvedName(name, node.modname) |
paul@3 | 443 | else: |
paul@3 | 444 | if module is not None: |
paul@3 | 445 | for n in module.namespace.keys(): |
paul@3 | 446 | self.namespace[n] = attr = module.namespace[n] |
paul@3 | 447 | if isinstance(attr, Module) and not attr.loaded: |
paul@3 | 448 | self.importer.load(attr.name) |
paul@0 | 449 | |
paul@0 | 450 | return None |
paul@0 | 451 | |
paul@0 | 452 | def visitFunction(self, node): |
paul@3 | 453 | function = Function( |
paul@3 | 454 | node.name, |
paul@7 | 455 | self.get_parent().full_name(), |
paul@3 | 456 | node.argnames, |
paul@3 | 457 | has_star=(node.flags & 4 != 0), |
paul@3 | 458 | has_dstar=(node.flags & 8 != 0) |
paul@3 | 459 | ) |
paul@0 | 460 | |
paul@0 | 461 | in_global = self.in_global |
paul@0 | 462 | self.in_global = 0 |
paul@0 | 463 | if node.name == "__init__" and self.classes: |
paul@0 | 464 | self.in_init = 1 |
paul@0 | 465 | self.dispatch(node.code) |
paul@0 | 466 | self.in_init = 0 |
paul@0 | 467 | self.in_global = in_global |
paul@3 | 468 | |
paul@3 | 469 | self.store(node.name, function) |
paul@0 | 470 | return None |
paul@0 | 471 | |
paul@4 | 472 | visitGenExpr = NOP |
paul@4 | 473 | |
paul@4 | 474 | visitGenExprFor = NOP |
paul@4 | 475 | |
paul@4 | 476 | visitGenExprIf = NOP |
paul@4 | 477 | |
paul@4 | 478 | visitGenExprInner = NOP |
paul@4 | 479 | |
paul@0 | 480 | def visitGetattr(self, node): |
paul@0 | 481 | expr = self.dispatch(node.expr) |
paul@4 | 482 | if expr is not None: |
paul@4 | 483 | if isinstance(expr, Module): |
paul@4 | 484 | return expr.namespace.get(node.attrname) |
paul@4 | 485 | elif isinstance(expr, UnresolvedName): |
paul@4 | 486 | return UnresolvedName(node.attrname, expr.full_name()) |
paul@4 | 487 | return builtins_namespace.get(node.attrname) |
paul@4 | 488 | |
paul@4 | 489 | visitGlobal = NOP |
paul@0 | 490 | |
paul@0 | 491 | def visitIf(self, node): |
paul@0 | 492 | for test, body in node.tests: |
paul@0 | 493 | self.dispatch(body) |
paul@0 | 494 | if node.else_ is not None: |
paul@0 | 495 | self.dispatch(node.else_) |
paul@0 | 496 | return None |
paul@0 | 497 | |
paul@0 | 498 | def visitImport(self, node): |
paul@0 | 499 | if self.importer is None: |
paul@0 | 500 | raise InspectError, "Please use the micropython.Importer class for code which uses the 'import' statement." |
paul@0 | 501 | |
paul@0 | 502 | for name, alias in node.names: |
paul@2 | 503 | if alias is not None: |
paul@4 | 504 | self.namespace[alias] = self.importer.load(name, 1) or UnresolvedName(None, name) |
paul@2 | 505 | else: |
paul@4 | 506 | self.namespace[name.split(".")[0]] = self.importer.load(name) or UnresolvedName(None, name.split(".")[0]) |
paul@0 | 507 | |
paul@0 | 508 | return None |
paul@0 | 509 | |
paul@4 | 510 | visitInvert = NOP |
paul@4 | 511 | |
paul@4 | 512 | visitKeyword = NOP |
paul@4 | 513 | |
paul@4 | 514 | visitLambda = NOP |
paul@4 | 515 | |
paul@1 | 516 | visitLeftShift = NOP |
paul@1 | 517 | |
paul@0 | 518 | visitList = NOP |
paul@0 | 519 | |
paul@4 | 520 | visitListComp = NOP |
paul@4 | 521 | |
paul@4 | 522 | visitListCompFor = NOP |
paul@4 | 523 | |
paul@4 | 524 | visitListCompIf = NOP |
paul@4 | 525 | |
paul@4 | 526 | visitMod = NOP |
paul@4 | 527 | |
paul@0 | 528 | def visitModule(self, node): |
paul@0 | 529 | return self.dispatch(node.node) |
paul@0 | 530 | |
paul@2 | 531 | visitMul = NOP |
paul@2 | 532 | |
paul@0 | 533 | def visitName(self, node): |
paul@0 | 534 | name = node.name |
paul@0 | 535 | if name == "self": |
paul@0 | 536 | return Self() |
paul@0 | 537 | elif self.namespace.has_key(name): |
paul@0 | 538 | return self.namespace[name] |
paul@3 | 539 | elif builtins_namespace.has_key(name): |
paul@3 | 540 | return builtins_namespace[name] |
paul@0 | 541 | else: |
paul@0 | 542 | return None |
paul@0 | 543 | |
paul@0 | 544 | visitNot = NOP |
paul@0 | 545 | |
paul@0 | 546 | visitOr = NOP |
paul@0 | 547 | |
paul@0 | 548 | visitPass = NOP |
paul@0 | 549 | |
paul@4 | 550 | visitPower = NOP |
paul@4 | 551 | |
paul@4 | 552 | visitPrint = NOP |
paul@4 | 553 | |
paul@4 | 554 | visitPrintnl = NOP |
paul@4 | 555 | |
paul@0 | 556 | visitRaise = NOP |
paul@0 | 557 | |
paul@4 | 558 | visitReturn = NOP |
paul@4 | 559 | |
paul@1 | 560 | visitRightShift = NOP |
paul@1 | 561 | |
paul@4 | 562 | visitSlice = NOP |
paul@4 | 563 | |
paul@0 | 564 | def visitStmt(self, node): |
paul@0 | 565 | for n in node.nodes: |
paul@0 | 566 | self.dispatch(n) |
paul@0 | 567 | return None |
paul@0 | 568 | |
paul@4 | 569 | visitSub = NOP |
paul@4 | 570 | |
paul@0 | 571 | visitSubscript = NOP |
paul@0 | 572 | |
paul@0 | 573 | def visitTryExcept(self, node): |
paul@0 | 574 | self.dispatch(node.body) |
paul@0 | 575 | for name, var, n in node.handlers: |
paul@0 | 576 | self.dispatch(n) |
paul@0 | 577 | if node.else_ is not None: |
paul@0 | 578 | self.dispatch(node.else_) |
paul@0 | 579 | return None |
paul@0 | 580 | |
paul@4 | 581 | visitTryFinally = NOP |
paul@4 | 582 | |
paul@1 | 583 | visitTuple = NOP |
paul@1 | 584 | |
paul@4 | 585 | visitUnaryAdd = NOP |
paul@4 | 586 | |
paul@4 | 587 | visitUnarySub = NOP |
paul@4 | 588 | |
paul@4 | 589 | visitWhile = NOP |
paul@4 | 590 | |
paul@4 | 591 | visitWith = NOP |
paul@4 | 592 | |
paul@4 | 593 | visitYield = NOP |
paul@4 | 594 | |
paul@0 | 595 | class Self: |
paul@0 | 596 | |
paul@0 | 597 | "A reference to an object within a method." |
paul@0 | 598 | |
paul@0 | 599 | pass |
paul@0 | 600 | |
paul@4 | 601 | # Built-in types initialisation. |
paul@4 | 602 | |
paul@0 | 603 | builtins_namespace = {} |
paul@0 | 604 | for key in ['ArithmeticError', 'AssertionError', 'AttributeError', |
paul@0 | 605 | 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis', |
paul@0 | 606 | 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', |
paul@0 | 607 | 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
paul@0 | 608 | 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', |
paul@0 | 609 | 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', |
paul@0 | 610 | 'None', 'NotImplemented', 'NotImplementedError', 'OSError', |
paul@0 | 611 | 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', |
paul@0 | 612 | 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', |
paul@0 | 613 | 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', |
paul@0 | 614 | 'TabError', 'True', 'TypeError', 'UnboundLocalError', |
paul@0 | 615 | 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', |
paul@0 | 616 | 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', |
paul@4 | 617 | 'ValueError', 'Warning', 'ZeroDivisionError', |
paul@4 | 618 | 'bool', 'buffer', 'complex', 'dict', 'file', 'float', 'int', 'list', |
paul@4 | 619 | 'long', 'object', 'slice', 'str', 'tuple', 'type', 'unicode']: |
paul@4 | 620 | builtins_namespace[key] = Class(key, "__builtins__") |
paul@0 | 621 | |
paul@0 | 622 | # vim: tabstop=4 expandtab shiftwidth=4 |