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