1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
#############################################################################
# Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1993, 1994, 1995.
# Unpublished work. All Rights Reserved.
#
# The software contained on this media is the property of the
# DSTC Pty Ltd. Use of this software is strictly in accordance
# with the license agreement in the accompanying LICENSE.DOC file.
# If your distribution of this software does not contain a
# LICENSE.DOC file then you have no rights to use this software
# in any manner and should contact DSTC at the address below
# to determine an appropriate licensing arrangement.
#
# DSTC Pty Ltd
# Level 7, Gehrmann Labs
# University of Queensland
# St Lucia, 4072
# Australia
# Tel: +61 7 3365 4310
# Fax: +61 7 3365 4311
# Email: enquiries@dstc.edu.au
#
# This software is being provided "AS IS" without warranty of
# any kind. In no event shall DSTC Pty Ltd be liable for
# damage of any kind arising out of or in connection with
# the use or performance of this software.
#
# Project: Python Tools
#
# File: ASTWalker.py
#
# Description:
# Classes to walk Python ASTs at a higher level than tuple
# representation.
#
# History:
# 19-Dec-1995 Martin Chilvers (martin) : created
#
# "@(#)$RCSfile: ASTWalker.py,v $ $Revision: 1 $"
#
#$Log: /Gendoc/ASTWalker.py $
#
# 1 98-04-01 13:15 Daniel
# Revision 1.2 1996/07/08 05:10:45 omfadmin
# Incorporated changes Fred Drake made to docco (support for Python 1.4).
#
# Revision 1.1 1996/06/13 17:57:56 omfadmin
# Initial revision
#
# Revision 1.3 1995/12/19 01:43:04 martin
# Added DSTC header.
#
#
#############################################################################
""" Classes to walk Python ASTs at a higher level than tuple representation.
The 'ASTWalker' class allows Python ASTs to be walked at a 'higher' level by
allowing a builder object to translate AST nodes in tuple form into some other
representation. The translated nodes are then passed to the method that
handles nodes of the appropriate type.
"""
# Built-in/standard modules.
import string
# Local modules.
import ast
import ast_doc
import symbol
import token
class ASTWalker:
""" Walk python ASTs translating nodes and passing them to methods."""
def __init__(self, node_builder = None):
""" Constructor.
'node_builder' is the object that is responsible for translating
AST nodes from tuple form into a nicer(TM!) form.
"""
# Allow the builder to be specified to create different node
# representations.
if node_builder != None:
self.node_builder = node_builder
else:
self.node_builder = ASTNodeBuilder()
# Map methods to the nodes that we are interested in (ie. the ones
# that we have got around to defining make functions for!).
self.d_node_map = node_map = {}
for num, name in symbol.sym_name.items() + [
(token.DEDENT, 'dedent'), (token.INDENT, 'indent')]:
try:
node_map[num] = getattr(self, name)
except AttributeError:
pass
return
def walk(self, the_ast):
""" Walk the AST calling the appropriate method at each node. """
ast.map_fn(the_ast, self.doit)
return
def doit(self, node):
""" Translate the node and pass it to the appropriate method. """
node_object = self.node_builder.make_node(node)
try:
# Call the appropriate method with the translated node object as
# its only parameter.
map_fn = self.d_node_map[ast.node_type(node)]
apply(map_fn, (node_object,))
except KeyError:
pass
return
# Override the following functions in a derived-class to do what you want
# with the various nodes!
def file_input(self, ref_module):
""" Override this to handle module nodes. """
pass
def import_stmt(self, ref_import):
""" Override this to handle imported module nodes. """
pass
def classdef(self, ref_classdef):
""" Override this to handle class definition nodes. """
pass
def funcdef(self, ref_funcdef):
""" Override this to handle function definition nodes. """
pass
def indent(self, ref_indent):
""" Override this to handle indent nodes. """
pass
def dedent(self, ref_dedent):
""" Override this to handle dedent nodes. """
pass
class ASTNodeBuilder:
""" Translates AST nodes from tuples into object instances. """
def __init__(self):
self.d_make_map = {symbol.file_input: ModuleNode,
symbol.classdef: ClassDefNode,
symbol.funcdef: FunctionDefNode,
symbol.import_stmt:ImportNode,
token.INDENT: IndentNode,
token.DEDENT: DedentNode}
return
def make_node(self, node):
""" Make an object instance to represent the AST node.
'node' is an AST node in tuple form.
"""
try:
make_fn = self.d_make_map[ast.node_type(node)]
new_node = apply(make_fn, (node,))
except KeyError:
new_node = None
return new_node
class ASTNode:
""" Abstract class representation of an AST node. """
def __init__(self, ast):
""" Constructor.
'ast' is a Python AST in tuple form.
"""
self.ast = ast
return
class ModuleNode(ASTNode):
""" Class representation of a module node.
'self.doc' is the module documentation.
"""
def __init__(self, node):
""" Constructor.
'node' is a module node in tuple form.
"""
ASTNode.__init__(self, node)
# Get the module documentation.
self.doc = ast_doc.module_doc(node)
return
class ImportNode(ASTNode):
"""Class representation of an import statement node."""
def __init__(self, node):
ASTNode.__init__(self, node)
self.name = ast_doc.import_name(node)
class ClassDefNode(ASTNode):
""" Class representation of a class definition node.
'self.name' is the classname.
'self.inheritance is a list of the base classes.
'self.doc' is the class documentation.
"""
def __init__(self, node):
""" Constructor.
'node' is a class definition node in tuple form.
"""
ASTNode.__init__(self, node)
# Get the classname, base classes and documentation.
(self.name, self.inheritance, self.doc) = ast_doc.class_doc(node)
return
def __str__(self):
""" Return a string representation of the class header. """
str = 'class %s' % self.name
if self.inheritance != []:
str = str + '('
str = str + string.joinfields(self.inheritance, ', ')
str = str + ')'
str = str + ':'
return str
class FunctionDefNode(ASTNode):
""" Class representation of a function definition node.
'self.name' is the function name.
'self.parameters is a list of the parameters [(Parameter, DefaultValue)].
'self.doc' is the function documentation.
"""
def __init__(self, node):
""" Constructor.
'node' is a function definition node in tuple form.
"""
ASTNode.__init__(self, node)
# Get the function name, parameters, and documentation.
(self.name, self.parameters, self.doc) = ast_doc.function_doc(node)
return
def __str__(self):
""" Return a string representation of the function header. """
str = 'def %s(' % self.name
if self.parameters != []:
lst_parameters = map(self.format_parameter, self.parameters)
str = str + string.joinfields(lst_parameters, ', ')
str = str + '):'
return str
def format_parameter(self, (name, default)):
""" Return a string representation of a formal parameter. """
str = name
if default != None:
str = str + '=' + default
return str
class IndentNode(ASTNode):
""" Class representation of an indent node. """
def __init__(self, node):
""" Constructor.
'node' is an indent node in tuple form.
"""
ASTNode.__init__(self, node)
return
class DedentNode(ASTNode):
""" Class representation of a dedent node. """
def __init__(self, node):
""" Constructor.
'node' is a dedent node in tuple form.
"""
ASTNode.__init__(self, node)
return
|