1 #!/usr/bin/env python 2 3 """ 4 Common classes. 5 6 Copyright (C) 2007, 2008, 2009 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 try: 23 set 24 except NameError: 25 from sets import Set as set 26 27 # Errors. 28 29 class ProcessingError(Exception): 30 31 "A processing error." 32 33 pass 34 35 class InspectError(ProcessingError): 36 37 "An error during the module inspection process." 38 39 pass 40 41 class TableError(ProcessingError): 42 43 "An error occurring during access to a lookup table." 44 45 pass 46 47 class TableGenerationError(ProcessingError): 48 49 "An error occurring when generating a lookup table." 50 51 pass 52 53 class NodeProcessingError(ProcessingError): 54 55 "A processing error associated with a particular program node." 56 57 def __init__(self, unit_name, node, message): 58 self.unit_name = unit_name 59 self.astnode = node 60 self.message = message 61 62 def __repr__(self): 63 return "Error in %r at line %d: %s" % (self.unit_name, self.astnode.lineno, self.message) 64 65 def __str__(self): 66 return repr(self) 67 68 class TranslateError(NodeProcessingError): 69 70 "An error during the module translation process." 71 72 pass 73 74 class TranslationNotImplementedError(TranslateError): 75 76 "An error caused by a node not being supported in translation." 77 78 pass 79 80 # Useful data. 81 82 comparison_methods = { 83 "==" : ("__eq__", "__eq__"), 84 "!=" : ("__ne__", "__ne__"), 85 "<" : ("__lt__", "__gt__"), 86 "<=" : ("__le__", "__ge__"), 87 ">=" : ("__ge__", "__le__"), 88 ">" : ("__gt__", "__lt__"), 89 "is" : None, 90 "is not" : None, 91 "in" : None, 92 "not in" : None 93 } 94 95 augassign_methods = { 96 "+=" : ("__iadd__", ("__add__", "__radd__")), 97 "-=" : ("__isub__", ("__sub__", "__rsub__")), 98 "*=" : ("__imul__", ("__mul__", "__rmul__")), 99 "/=" : ("__idiv__", ("__div__", "__rdiv__")), 100 "%=" : ("__imod__", ("__mod__", "__rmod__")), 101 "**=" : ("__ipow__", ("__pow__", "__rpow__")), 102 "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), 103 ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), 104 "&=" : ("__iand__", ("__and__", "__rand__")), 105 "^=" : ("__ixor__", ("__xor__", "__rxor__")), 106 "|=" : ("__ior__", ("__or__", "__ror__")) 107 } 108 109 binary_methods = { 110 "Add" : ("__add__", "__radd__"), 111 "Bitand" : ("__and__", "__rand__"), 112 "Bitor" : ("__or__", "__ror__"), 113 "Bitxor" : ("__xor__", "__rxor__"), 114 "Div" : ("__div__", "__rdiv__"), 115 "FloorDiv" : ("__floordiv__", "__rfloordiv__"), 116 "LeftShift" : ("__lshift__", "__rlshift__"), 117 "Mod" : ("__mod__", "__rmod__"), 118 "Mul" : ("__mul__", "__rmul__"), 119 "Power" : ("__pow__", "__rpow__"), 120 "RightShift" : ("__rshift__", "__rrshift__"), 121 "Sub" : ("__sub__", "__rsub__") 122 } 123 124 unary_methods = { 125 "Invert" : "__invert__", 126 "UnaryAdd" : "__pos__", 127 "UnarySub" : "__neg__" 128 } 129 130 # vim: tabstop=4 expandtab shiftwidth=4