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 NodeProcessingError(ProcessingError): 48 49 "A processing error associated with a particular program node." 50 51 def __init__(self, unit_name, node, message): 52 self.unit_name = unit_name 53 self.astnode = node 54 self.message = message 55 56 def __repr__(self): 57 return "Error in %r at line %d: %s" % (self.unit_name, self.astnode.lineno, self.message) 58 59 def __str__(self): 60 return repr(self) 61 62 class TranslateError(NodeProcessingError): 63 64 "An error during the module translation process." 65 66 pass 67 68 class TranslationNotImplementedError(TranslateError): 69 70 "An error caused by a node not being supported in translation." 71 72 pass 73 74 # Useful data. 75 76 comparison_methods = { 77 "==" : ("__eq__", "__eq__"), 78 "!=" : ("__ne__", "__ne__"), 79 "<" : ("__lt__", "__gt__"), 80 "<=" : ("__le__", "__ge__"), 81 ">=" : ("__ge__", "__le__"), 82 ">" : ("__gt__", "__lt__"), 83 "is" : None, 84 "is not" : None, 85 "in" : None, 86 "not in" : None 87 } 88 89 augassign_methods = { 90 "+=" : ("__iadd__", ("__add__", "__radd__")), 91 "-=" : ("__isub__", ("__sub__", "__rsub__")), 92 "*=" : ("__imul__", ("__mul__", "__rmul__")), 93 "/=" : ("__idiv__", ("__div__", "__rdiv__")), 94 "%=" : ("__imod__", ("__mod__", "__rmod__")), 95 "**=" : ("__ipow__", ("__pow__", "__rpow__")), 96 "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), 97 ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), 98 "&=" : ("__iand__", ("__and__", "__rand__")), 99 "^=" : ("__ixor__", ("__xor__", "__rxor__")), 100 "|=" : ("__ior__", ("__or__", "__ror__")) 101 } 102 103 binary_methods = { 104 "Add" : ("__add__", "__radd__"), 105 "Bitand" : ("__and__", "__rand__"), 106 "Bitor" : ("__or__", "__ror__"), 107 "Bitxor" : ("__xor__", "__rxor__"), 108 "Div" : ("__div__", "__rdiv__"), 109 "FloorDiv" : ("__floordiv__", "__rfloordiv__"), 110 "LeftShift" : ("__lshift__", "__rlshift__"), 111 "Mod" : ("__mod__", "__rmod__"), 112 "Mul" : ("__mul__", "__rmul__"), 113 "Power" : ("__pow__", "__rpow__"), 114 "RightShift" : ("__rshift__", "__rrshift__"), 115 "Sub" : ("__sub__", "__rsub__") 116 } 117 118 unary_methods = { 119 "Invert" : "__invert__", 120 "UnaryAdd" : "__pos__", 121 "UnarySub" : "__neg__" 122 } 123 124 # vim: tabstop=4 expandtab shiftwidth=4