1 #!/usr/bin/env python 2 3 """ 4 Common classes. 5 6 Copyright (C) 2007, 2008 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 # Program code representations. 75 76 class Label: 77 78 "A reference to a location." 79 80 def __init__(self, number, location=None): 81 self.number = number 82 self.location = location 83 84 def __repr__(self): 85 return "Label(%r, location=%r)" % (self.number, self.location) 86 87 # Inspection representations. 88 89 class AtLeast: 90 91 "A special representation for numbers of a given value or greater." 92 93 def __init__(self, count): 94 self.count = count 95 96 def __eq__(self, other): 97 return 0 98 99 __lt__ = __le__ = __eq__ 100 101 def __ne__(self, other): 102 return 1 103 104 def __gt__(self, other): 105 if isinstance(other, AtLeast): 106 return 0 107 else: 108 return self.count > other 109 110 def __ge__(self, other): 111 if isinstance(other, AtLeast): 112 return 0 113 else: 114 return self.count >= other 115 116 def __iadd__(self, other): 117 if isinstance(other, AtLeast): 118 self.count += other.count 119 else: 120 self.count += other 121 return self 122 123 def __radd__(self, other): 124 if isinstance(other, AtLeast): 125 return AtLeast(self.count + other.count) 126 else: 127 return AtLeast(self.count + other) 128 129 def __repr__(self): 130 return "AtLeast(%r)" % self.count 131 132 class Naming: 133 134 "A mix-in providing naming conveniences." 135 136 def full_name(self): 137 if self.name is not None: 138 return self.parent.full_name() + "." + self.name 139 else: 140 return self.parent.full_name() 141 142 class Undefined: 143 144 "A special class of undefined values." 145 146 pass 147 148 # vim: tabstop=4 expandtab shiftwidth=4