1.1 --- a/annotate.py Sun Aug 13 18:57:43 2006 +0200
1.2 +++ b/annotate.py Sun Aug 13 19:31:12 2006 +0200
1.3 @@ -45,139 +45,6 @@
1.4
1.5 system = System()
1.6
1.7 -# Namespace-related abstractions.
1.8 -
1.9 -class Namespace:
1.10 -
1.11 - """
1.12 - A local namespace which may either relate to a genuine set of function
1.13 - locals or the initialisation of a structure.
1.14 - """
1.15 -
1.16 - def __init__(self):
1.17 - self.names = {}
1.18 -
1.19 - def store(self, name, types):
1.20 - self.names[name] = types
1.21 -
1.22 - def load(self, name):
1.23 - return self.names[name]
1.24 -
1.25 - def merge(self, name, types):
1.26 - if not self.names.has_key(name):
1.27 - self.names[name] = types[:]
1.28 - else:
1.29 - existing = self.names[name]
1.30 - for type in types:
1.31 - if type not in existing:
1.32 - existing.append(type)
1.33 -
1.34 - def merge_namespace(self, namespace):
1.35 - self.merge_items(namespace.names.items())
1.36 -
1.37 - def merge_items(self, items):
1.38 - for name, types in items:
1.39 - self.merge(name, types)
1.40 -
1.41 - def __repr__(self):
1.42 - return repr(self.names)
1.43 -
1.44 -class Attribute:
1.45 -
1.46 - """
1.47 - An attribute abstraction, indicating the type of the attribute along with
1.48 - its context or origin.
1.49 - """
1.50 -
1.51 - def __init__(self, context, type):
1.52 - self.context = context
1.53 - self.type = type
1.54 -
1.55 - def __eq__(self, other):
1.56 - return hasattr(other, "type") and other.type == self.type or other == self.type
1.57 -
1.58 - def __repr__(self):
1.59 - return "Attribute of type %s (context %s)" % (self.type, self.context)
1.60 -
1.61 -def find_attributes(structure, name):
1.62 -
1.63 - """
1.64 - Find for the given 'structure' all attributes for the given 'name', visiting
1.65 - base classes where appropriate and returning the attributes in order of
1.66 - descending precedence for all possible base classes.
1.67 -
1.68 - The elements in the result list are 2-tuples which contain the attribute and
1.69 - the structure involved in accessing the attribute.
1.70 - """
1.71 -
1.72 - # First attempt to search the instance/class namespace.
1.73 -
1.74 - try:
1.75 - l = structure.namespace.load(name)
1.76 - attributes = []
1.77 - for attribute in l:
1.78 - attributes.append((attribute, structure))
1.79 -
1.80 - # If that does not work, attempt to investigate any class or base classes.
1.81 -
1.82 - except KeyError:
1.83 - attributes = []
1.84 -
1.85 - # Investigate any instance's implementing class.
1.86 -
1.87 - if isinstance(structure, Instance):
1.88 - for cls in structure.namespace.load("__class__"):
1.89 - l = find_attributes(cls, name)
1.90 - for attribute in l:
1.91 - if attribute not in attributes:
1.92 - attributes.append(attribute)
1.93 -
1.94 - # Investigate any class's base classes.
1.95 -
1.96 - elif isinstance(structure, Class):
1.97 -
1.98 - # If no base classes exist, return an indicator that no attribute
1.99 - # exists.
1.100 -
1.101 - if not structure.base_refs:
1.102 - return [(None, structure)]
1.103 -
1.104 - # Otherwise, find all possible base classes.
1.105 -
1.106 - for base_refs in structure.base_refs:
1.107 - base_attributes = []
1.108 -
1.109 - # For each base class, find attributes either in the base
1.110 - # class or its own base classes.
1.111 -
1.112 - for base_ref in base_refs:
1.113 - l = find_attributes(base_ref, name)
1.114 - for attribute in l:
1.115 - if attribute not in base_attributes:
1.116 - base_attributes.append(attribute)
1.117 -
1.118 - attributes += base_attributes
1.119 -
1.120 - return attributes
1.121 -
1.122 -def get_attributes(structure, name):
1.123 -
1.124 - """
1.125 - Return all possible attributes for the given 'structure' having the given
1.126 - 'name', wrapping each attribute in an Attribute object which includes
1.127 - context information for the attribute access.
1.128 -
1.129 - The elements in the result list are 2-tuples which contain the attribute and
1.130 - the structure involved in accessing the attribute.
1.131 - """
1.132 -
1.133 - if isinstance(structure, Attribute):
1.134 - structure = structure.type
1.135 - attributes = find_attributes(structure, name)
1.136 - for i, (attribute, accessor) in enumerate(attributes):
1.137 - attributes[i] = Attribute(structure, attribute), accessor
1.138 - return attributes
1.139 -
1.140 # Annotation.
1.141
1.142 class Annotator(Visitor):
1.143 @@ -658,4 +525,137 @@
1.144 namespace.merge_items(items)
1.145 return namespace
1.146
1.147 +# Namespace-related abstractions.
1.148 +
1.149 +class Namespace:
1.150 +
1.151 + """
1.152 + A local namespace which may either relate to a genuine set of function
1.153 + locals or the initialisation of a structure.
1.154 + """
1.155 +
1.156 + def __init__(self):
1.157 + self.names = {}
1.158 +
1.159 + def store(self, name, types):
1.160 + self.names[name] = types
1.161 +
1.162 + def load(self, name):
1.163 + return self.names[name]
1.164 +
1.165 + def merge(self, name, types):
1.166 + if not self.names.has_key(name):
1.167 + self.names[name] = types[:]
1.168 + else:
1.169 + existing = self.names[name]
1.170 + for type in types:
1.171 + if type not in existing:
1.172 + existing.append(type)
1.173 +
1.174 + def merge_namespace(self, namespace):
1.175 + self.merge_items(namespace.names.items())
1.176 +
1.177 + def merge_items(self, items):
1.178 + for name, types in items:
1.179 + self.merge(name, types)
1.180 +
1.181 + def __repr__(self):
1.182 + return repr(self.names)
1.183 +
1.184 +class Attribute:
1.185 +
1.186 + """
1.187 + An attribute abstraction, indicating the type of the attribute along with
1.188 + its context or origin.
1.189 + """
1.190 +
1.191 + def __init__(self, context, type):
1.192 + self.context = context
1.193 + self.type = type
1.194 +
1.195 + def __eq__(self, other):
1.196 + return hasattr(other, "type") and other.type == self.type or other == self.type
1.197 +
1.198 + def __repr__(self):
1.199 + return "Attribute of type %s (context %s)" % (self.type, self.context)
1.200 +
1.201 +def find_attributes(structure, name):
1.202 +
1.203 + """
1.204 + Find for the given 'structure' all attributes for the given 'name', visiting
1.205 + base classes where appropriate and returning the attributes in order of
1.206 + descending precedence for all possible base classes.
1.207 +
1.208 + The elements in the result list are 2-tuples which contain the attribute and
1.209 + the structure involved in accessing the attribute.
1.210 + """
1.211 +
1.212 + # First attempt to search the instance/class namespace.
1.213 +
1.214 + try:
1.215 + l = structure.namespace.load(name)
1.216 + attributes = []
1.217 + for attribute in l:
1.218 + attributes.append((attribute, structure))
1.219 +
1.220 + # If that does not work, attempt to investigate any class or base classes.
1.221 +
1.222 + except KeyError:
1.223 + attributes = []
1.224 +
1.225 + # Investigate any instance's implementing class.
1.226 +
1.227 + if isinstance(structure, Instance):
1.228 + for cls in structure.namespace.load("__class__"):
1.229 + l = find_attributes(cls, name)
1.230 + for attribute in l:
1.231 + if attribute not in attributes:
1.232 + attributes.append(attribute)
1.233 +
1.234 + # Investigate any class's base classes.
1.235 +
1.236 + elif isinstance(structure, Class):
1.237 +
1.238 + # If no base classes exist, return an indicator that no attribute
1.239 + # exists.
1.240 +
1.241 + if not structure.base_refs:
1.242 + return [(None, structure)]
1.243 +
1.244 + # Otherwise, find all possible base classes.
1.245 +
1.246 + for base_refs in structure.base_refs:
1.247 + base_attributes = []
1.248 +
1.249 + # For each base class, find attributes either in the base
1.250 + # class or its own base classes.
1.251 +
1.252 + for base_ref in base_refs:
1.253 + l = find_attributes(base_ref, name)
1.254 + for attribute in l:
1.255 + if attribute not in base_attributes:
1.256 + base_attributes.append(attribute)
1.257 +
1.258 + attributes += base_attributes
1.259 +
1.260 + return attributes
1.261 +
1.262 +def get_attributes(structure, name):
1.263 +
1.264 + """
1.265 + Return all possible attributes for the given 'structure' having the given
1.266 + 'name', wrapping each attribute in an Attribute object which includes
1.267 + context information for the attribute access.
1.268 +
1.269 + The elements in the result list are 2-tuples which contain the attribute and
1.270 + the structure involved in accessing the attribute.
1.271 + """
1.272 +
1.273 + if isinstance(structure, Attribute):
1.274 + structure = structure.type
1.275 + attributes = find_attributes(structure, name)
1.276 + for i, (attribute, accessor) in enumerate(attributes):
1.277 + attributes[i] = Attribute(structure, attribute), accessor
1.278 + return attributes
1.279 +
1.280 # vim: tabstop=4 expandtab shiftwidth=4