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 # Errors. 51 52 class ProcessingError(Exception): 53 54 "A processing error." 55 56 pass 57 58 class TableError(ProcessingError): 59 60 "An error occurring during access to a lookup table." 61 62 pass 63 64 class TableGenerationError(ProcessingError): 65 66 "An error occurring when generating a lookup table." 67 68 pass 69 70 class NodeProcessingError(ProcessingError): 71 72 "A processing error associated with a particular program node." 73 74 def __init__(self, message): 75 self.message = message 76 self.unit_name = None 77 self.astnode = None 78 79 def get_lineno(self, node): 80 lineno = node.lineno 81 if lineno is not None: 82 return lineno 83 else: 84 for child in node.getChildNodes(): 85 lineno = self.get_lineno(child) 86 if lineno is not None: 87 return lineno 88 return None 89 90 def __repr__(self): 91 return "Error in %r at line %r: %s" % (self.unit_name, self.get_lineno(self.astnode), self.message) 92 93 def __str__(self): 94 return repr(self) 95 96 class InspectError(NodeProcessingError): 97 98 "An error during the module inspection process." 99 100 pass 101 102 class TranslateError(NodeProcessingError): 103 104 "An error during the module translation process." 105 106 pass 107 108 class TranslationNotImplementedError(TranslateError): 109 110 "An error caused by a node not being supported in translation." 111 112 pass 113 114 # Useful data. 115 116 comparison_methods = { 117 "==" : ("__eq__", "__eq__"), 118 "!=" : ("__ne__", "__ne__"), 119 "<" : ("__lt__", "__gt__"), 120 "<=" : ("__le__", "__ge__"), 121 ">=" : ("__ge__", "__le__"), 122 ">" : ("__gt__", "__lt__"), 123 "is" : None, 124 "is not" : None, 125 "in" : None, 126 "not in" : None 127 } 128 129 augassign_methods = { 130 "+=" : ("__iadd__", ("__add__", "__radd__")), 131 "-=" : ("__isub__", ("__sub__", "__rsub__")), 132 "*=" : ("__imul__", ("__mul__", "__rmul__")), 133 "/=" : ("__idiv__", ("__div__", "__rdiv__")), 134 "//=" : ("__ifloordiv__", ("__floordiv__", "__rfloordiv__")), 135 "%=" : ("__imod__", ("__mod__", "__rmod__")), 136 "**=" : ("__ipow__", ("__pow__", "__rpow__")), 137 "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), 138 ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), 139 "&=" : ("__iand__", ("__and__", "__rand__")), 140 "^=" : ("__ixor__", ("__xor__", "__rxor__")), 141 "|=" : ("__ior__", ("__or__", "__ror__")) 142 } 143 144 binary_methods = { 145 "Add" : ("__add__", "__radd__"), 146 "Bitand" : ("__and__", "__rand__"), 147 "Bitor" : ("__or__", "__ror__"), 148 "Bitxor" : ("__xor__", "__rxor__"), 149 "Div" : ("__div__", "__rdiv__"), 150 "FloorDiv" : ("__floordiv__", "__rfloordiv__"), 151 "LeftShift" : ("__lshift__", "__rlshift__"), 152 "Mod" : ("__mod__", "__rmod__"), 153 "Mul" : ("__mul__", "__rmul__"), 154 "Power" : ("__pow__", "__rpow__"), 155 "RightShift" : ("__rshift__", "__rrshift__"), 156 "Sub" : ("__sub__", "__rsub__") 157 } 158 159 unary_methods = { 160 "Invert" : "__invert__", 161 "UnaryAdd" : "__pos__", 162 "UnarySub" : "__neg__" 163 } 164 165 # vim: tabstop=4 expandtab shiftwidth=4