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. 30 31 class ASTVisitor(compiler.visitor.ASTVisitor): 32 33 "A base class for visitors." 34 35 def dispatch(self, node, *args): 36 try: 37 return compiler.visitor.ASTVisitor.dispatch(self, node, *args) 38 except NodeProcessingError, exc: 39 if exc.astnode is None: 40 exc.astnode = node 41 42 # NOTE: Should perhaps specialise the subclasses appropriately. 43 44 if hasattr(self, "unit"): 45 exc.unit_name = self.unit.full_name() 46 else: 47 exc.unit_name = self.full_name() 48 raise 49 50 def used_by_unit(node): 51 52 """ 53 Return whether the definition made by a 'node' is actually employed by the 54 program unit within which it is found. 55 """ 56 57 return node.unit.parent.has_key(node.unit.name) 58 59 # Errors. 60 61 class ProcessingError(Exception): 62 63 "A processing error." 64 65 pass 66 67 class TableError(ProcessingError): 68 69 "An error occurring during access to a lookup table." 70 71 pass 72 73 class TableGenerationError(ProcessingError): 74 75 "An error occurring when generating a lookup table." 76 77 pass 78 79 class NodeProcessingError(ProcessingError): 80 81 "A processing error associated with a particular program node." 82 83 def __init__(self, message): 84 self.message = message 85 self.unit_name = None 86 self.astnode = None 87 88 def get_lineno(self, node): 89 lineno = node.lineno 90 if lineno is not None: 91 return lineno 92 else: 93 for child in node.getChildNodes(): 94 lineno = self.get_lineno(child) 95 if lineno is not None: 96 return lineno 97 return None 98 99 def __repr__(self): 100 return "Error in %r at line %r: %s" % (self.unit_name, self.get_lineno(self.astnode), self.message) 101 102 def __str__(self): 103 return repr(self) 104 105 class InspectError(NodeProcessingError): 106 107 "An error during the module inspection process." 108 109 pass 110 111 class TranslateError(NodeProcessingError): 112 113 "An error during the module translation process." 114 115 pass 116 117 class TranslationNotImplementedError(TranslateError): 118 119 "An error caused by a node not being supported in translation." 120 121 pass 122 123 # Special representations. 124 125 class AtLeast: 126 127 "A special representation for numbers of a given value or greater." 128 129 def __init__(self, count): 130 self.count = count 131 132 def __eq__(self, other): 133 return 0 134 135 __lt__ = __le__ = __eq__ 136 137 def __ne__(self, other): 138 return 1 139 140 def __gt__(self, other): 141 if isinstance(other, AtLeast): 142 return 0 143 else: 144 return self.count > other 145 146 def __ge__(self, other): 147 if isinstance(other, AtLeast): 148 return 0 149 else: 150 return self.count >= other 151 152 def __iadd__(self, other): 153 if isinstance(other, AtLeast): 154 self.count += other.count 155 else: 156 self.count += other 157 return self 158 159 def __radd__(self, other): 160 if isinstance(other, AtLeast): 161 return AtLeast(self.count + other.count) 162 else: 163 return AtLeast(self.count + other) 164 165 def __repr__(self): 166 return "AtLeast(%r)" % self.count 167 168 # Useful data. 169 170 comparison_methods = { 171 "==" : ("__eq__", "__eq__"), 172 "!=" : ("__ne__", "__ne__"), 173 "<" : ("__lt__", "__gt__"), 174 "<=" : ("__le__", "__ge__"), 175 ">=" : ("__ge__", "__le__"), 176 ">" : ("__gt__", "__lt__"), 177 "is" : None, 178 "is not" : None, 179 "in" : None, 180 "not in" : None 181 } 182 183 augassign_methods = { 184 "+=" : ("__iadd__", ("__add__", "__radd__")), 185 "-=" : ("__isub__", ("__sub__", "__rsub__")), 186 "*=" : ("__imul__", ("__mul__", "__rmul__")), 187 "/=" : ("__idiv__", ("__div__", "__rdiv__")), 188 "//=" : ("__ifloordiv__", ("__floordiv__", "__rfloordiv__")), 189 "%=" : ("__imod__", ("__mod__", "__rmod__")), 190 "**=" : ("__ipow__", ("__pow__", "__rpow__")), 191 "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), 192 ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), 193 "&=" : ("__iand__", ("__and__", "__rand__")), 194 "^=" : ("__ixor__", ("__xor__", "__rxor__")), 195 "|=" : ("__ior__", ("__or__", "__ror__")) 196 } 197 198 binary_methods = { 199 "Add" : ("__add__", "__radd__"), 200 "Bitand" : ("__and__", "__rand__"), 201 "Bitor" : ("__or__", "__ror__"), 202 "Bitxor" : ("__xor__", "__rxor__"), 203 "Div" : ("__div__", "__rdiv__"), 204 "FloorDiv" : ("__floordiv__", "__rfloordiv__"), 205 "LeftShift" : ("__lshift__", "__rlshift__"), 206 "Mod" : ("__mod__", "__rmod__"), 207 "Mul" : ("__mul__", "__rmul__"), 208 "Power" : ("__pow__", "__rpow__"), 209 "RightShift" : ("__rshift__", "__rrshift__"), 210 "Sub" : ("__sub__", "__rsub__") 211 } 212 213 unary_methods = { 214 "Invert" : "__invert__", 215 "UnaryAdd" : "__pos__", 216 "UnarySub" : "__neg__" 217 } 218 219 # vim: tabstop=4 expandtab shiftwidth=4