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 # Program code representations. 69 70 class Label: 71 72 "A reference to a location." 73 74 def __init__(self, number, location=None): 75 self.number = number 76 self.location = location 77 78 def __repr__(self): 79 return "Label(%r, location=%r)" % (self.number, self.location) 80 81 # Inspection representations. 82 83 class AtLeast: 84 85 "A special representation for numbers of a given value or greater." 86 87 def __init__(self, count): 88 self.count = count 89 90 def __eq__(self, other): 91 return 0 92 93 __lt__ = __le__ = __eq__ 94 95 def __ne__(self, other): 96 return 1 97 98 def __gt__(self, other): 99 if isinstance(other, AtLeast): 100 return 0 101 else: 102 return self.count > other 103 104 def __ge__(self, other): 105 if isinstance(other, AtLeast): 106 return 0 107 else: 108 return self.count >= other 109 110 def __iadd__(self, other): 111 if isinstance(other, AtLeast): 112 self.count += other.count 113 else: 114 self.count += other 115 return self 116 117 def __radd__(self, other): 118 if isinstance(other, AtLeast): 119 return AtLeast(self.count + other.count) 120 else: 121 return AtLeast(self.count + other) 122 123 def __repr__(self): 124 return "AtLeast(%r)" % self.count 125 126 class Naming: 127 128 "A mix-in providing naming conveniences." 129 130 def full_name(self): 131 if self.name is not None: 132 return self.parent.full_name() + "." + self.name 133 else: 134 return self.parent.full_name() 135 136 # vim: tabstop=4 expandtab shiftwidth=4