1 #!/usr/bin/env python 2 3 """ 4 Common classes. 5 6 Copyright (C) 2007, 2008, 2009, 2010 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 import compiler.visitor 23 24 try: 25 set 26 except NameError: 27 from sets import Set as set 28 29 # Visitors and activities related to node annotations. 30 31 class ASTVisitor(compiler.visitor.ASTVisitor): 32 33 "A base class for visitors." 34 35 def dispatch(self, node, *args): 36 37 "Dispatch using 'node', annotating any raised exceptions." 38 39 try: 40 return compiler.visitor.ASTVisitor.dispatch(self, node, *args) 41 except NodeProcessingError, exc: 42 if exc.astnode is None: 43 exc.astnode = node 44 45 # NOTE: Should perhaps specialise the subclasses appropriately. 46 47 if hasattr(self, "unit"): 48 exc.unit_name = self.unit.full_name() 49 else: 50 exc.unit_name = self.full_name() 51 raise 52 53 def possible_accessor_types(self, node): 54 55 """ 56 Given annotations made during the inspection process, return all possible 57 types for a 'node' involved in attribute access, or return None if no 58 annotations are available. 59 """ 60 61 if hasattr(node, "_attrusers"): 62 target_names = set() 63 64 for user in node._attrusers: 65 target_names.update(self.objtable.all_possible_objects(user._attrnames[node._username])) 66 67 return target_names 68 69 else: 70 return None 71 72 def used_by_unit(node): 73 74 """ 75 Return whether the definition made by a 'node' is actually employed by the 76 program unit within which it is found. 77 """ 78 79 return node.unit.parent.has_key(node.unit.name) 80 81 # Errors. 82 83 class ProcessingError(Exception): 84 85 "A processing error." 86 87 pass 88 89 class TableError(ProcessingError): 90 91 "An error occurring during access to a lookup table." 92 93 pass 94 95 class TableGenerationError(ProcessingError): 96 97 "An error occurring when generating a lookup table." 98 99 pass 100 101 class NodeProcessingError(ProcessingError): 102 103 "A processing error associated with a particular program node." 104 105 def __init__(self, message): 106 self.message = message 107 self.unit_name = None 108 self.astnode = None 109 110 def get_lineno(self, node): 111 lineno = node.lineno 112 if lineno is not None: 113 return lineno 114 else: 115 for child in node.getChildNodes(): 116 lineno = self.get_lineno(child) 117 if lineno is not None: 118 return lineno 119 return None 120 121 def __repr__(self): 122 return "Error in %r at line %r: %s" % (self.unit_name, self.get_lineno(self.astnode), self.message) 123 124 def __str__(self): 125 return repr(self) 126 127 class InspectError(NodeProcessingError): 128 129 "An error during the module inspection process." 130 131 pass 132 133 class TranslateError(NodeProcessingError): 134 135 "An error during the module translation process." 136 137 pass 138 139 class TranslationNotImplementedError(TranslateError): 140 141 "An error caused by a node not being supported in translation." 142 143 pass 144 145 # Special representations. 146 147 class AtLeast: 148 149 "A special representation for numbers of a given value or greater." 150 151 def __init__(self, count): 152 self.count = count 153 154 def __eq__(self, other): 155 return 0 156 157 __lt__ = __le__ = __eq__ 158 159 def __ne__(self, other): 160 return 1 161 162 def __gt__(self, other): 163 if isinstance(other, AtLeast): 164 return 0 165 else: 166 return self.count > other 167 168 def __ge__(self, other): 169 if isinstance(other, AtLeast): 170 return 0 171 else: 172 return self.count >= other 173 174 def __iadd__(self, other): 175 if isinstance(other, AtLeast): 176 self.count += other.count 177 else: 178 self.count += other 179 return self 180 181 def __radd__(self, other): 182 if isinstance(other, AtLeast): 183 return AtLeast(self.count + other.count) 184 else: 185 return AtLeast(self.count + other) 186 187 def __repr__(self): 188 return "AtLeast(%r)" % self.count 189 190 # Useful data. 191 192 comparison_methods = { 193 "==" : ("__eq__", "__eq__"), 194 "!=" : ("__ne__", "__ne__"), 195 "<" : ("__lt__", "__gt__"), 196 "<=" : ("__le__", "__ge__"), 197 ">=" : ("__ge__", "__le__"), 198 ">" : ("__gt__", "__lt__"), 199 "is" : None, 200 "is not" : None, 201 "in" : None, 202 "not in" : None 203 } 204 205 augassign_methods = { 206 "+=" : ("__iadd__", ("__add__", "__radd__")), 207 "-=" : ("__isub__", ("__sub__", "__rsub__")), 208 "*=" : ("__imul__", ("__mul__", "__rmul__")), 209 "/=" : ("__idiv__", ("__div__", "__rdiv__")), 210 "//=" : ("__ifloordiv__", ("__floordiv__", "__rfloordiv__")), 211 "%=" : ("__imod__", ("__mod__", "__rmod__")), 212 "**=" : ("__ipow__", ("__pow__", "__rpow__")), 213 "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), 214 ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), 215 "&=" : ("__iand__", ("__and__", "__rand__")), 216 "^=" : ("__ixor__", ("__xor__", "__rxor__")), 217 "|=" : ("__ior__", ("__or__", "__ror__")) 218 } 219 220 binary_methods = { 221 "Add" : ("__add__", "__radd__"), 222 "Bitand" : ("__and__", "__rand__"), 223 "Bitor" : ("__or__", "__ror__"), 224 "Bitxor" : ("__xor__", "__rxor__"), 225 "Div" : ("__div__", "__rdiv__"), 226 "FloorDiv" : ("__floordiv__", "__rfloordiv__"), 227 "LeftShift" : ("__lshift__", "__rlshift__"), 228 "Mod" : ("__mod__", "__rmod__"), 229 "Mul" : ("__mul__", "__rmul__"), 230 "Power" : ("__pow__", "__rpow__"), 231 "RightShift" : ("__rshift__", "__rrshift__"), 232 "Sub" : ("__sub__", "__rsub__") 233 } 234 235 unary_methods = { 236 "Invert" : "__invert__", 237 "UnaryAdd" : "__pos__", 238 "UnarySub" : "__neg__" 239 } 240 241 operator_functions = { 242 243 # Binary operations. 244 245 "Add" : "add", 246 "Bitand" : "and_", 247 "Bitor" : "or_", 248 "Bitxor" : "xor", 249 "Div" : "div", 250 "FloorDiv" : "floordiv", 251 "LeftShift" : "lshift", 252 "Mod" : "mod", 253 "Mul" : "mul", 254 "Power" : "pow", 255 "RightShift" : "rshift", 256 "Sub" : "sub", 257 258 # Unary operations. 259 260 "Invert" : "invert", 261 "UnaryAdd" : "pos", 262 "UnarySub" : "neg", 263 264 # Augmented assignment. 265 266 "+=" : "iadd", 267 "-=" : "isub", 268 "*=" : "imul", 269 "/=" : "idiv", 270 "//=" : "ifloordiv", 271 "%=" : "imod", 272 "**=" : "ipow", 273 "<<=" : "ilshift", 274 ">>=" : "irshift", 275 "&=" : "iand", 276 "^=" : "ixor", 277 "|=" : "ior", 278 279 # Comparisons. 280 281 "==" : "eq", 282 "!=" : "ne", 283 "<" : "lt", 284 "<=" : "le", 285 ">=" : "ge", 286 ">" : "gt" 287 } 288 289 # vim: tabstop=4 expandtab shiftwidth=4