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