paulb@213 | 1 | #!/usr/bin/env python |
paulb@213 | 2 | |
paulb@213 | 3 | """ |
paulb@213 | 4 | Simplified program utilities. |
paulb@213 | 5 | |
paulb@213 | 6 | Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@213 | 7 | |
paulb@213 | 8 | This software is free software; you can redistribute it and/or |
paulb@213 | 9 | modify it under the terms of the GNU General Public License as |
paulb@213 | 10 | published by the Free Software Foundation; either version 2 of |
paulb@213 | 11 | the License, or (at your option) any later version. |
paulb@213 | 12 | |
paulb@213 | 13 | This software is distributed in the hope that it will be useful, |
paulb@213 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@213 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@213 | 16 | GNU General Public License for more details. |
paulb@213 | 17 | |
paulb@213 | 18 | You should have received a copy of the GNU General Public |
paulb@213 | 19 | License along with this library; see the file LICENCE.txt |
paulb@213 | 20 | If not, write to the Free Software Foundation, Inc., |
paulb@213 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@213 | 22 | """ |
paulb@213 | 23 | |
paulb@213 | 24 | from compiler.visitor import ASTVisitor |
paulb@213 | 25 | |
paulb@213 | 26 | # Exceptions. |
paulb@213 | 27 | |
paulb@213 | 28 | class SimplifiedError(Exception): |
paulb@213 | 29 | |
paulb@213 | 30 | "An error in the annotation process." |
paulb@213 | 31 | |
paulb@213 | 32 | def __init__(self, exc, node, *args): |
paulb@213 | 33 | |
paulb@213 | 34 | """ |
paulb@213 | 35 | Initialise the error with an existing exception 'exc', the 'node' at |
paulb@213 | 36 | which this error occurs, along with additional optional arguments. |
paulb@213 | 37 | """ |
paulb@213 | 38 | |
paulb@213 | 39 | Exception.__init__(self, *args) |
paulb@213 | 40 | self.nodes = [node] |
paulb@213 | 41 | self.exc = exc |
paulb@213 | 42 | |
paulb@213 | 43 | def add(self, node): |
paulb@213 | 44 | |
paulb@213 | 45 | "Add the given 'node' to the path of nodes leading from the exception." |
paulb@213 | 46 | |
paulb@213 | 47 | self.nodes.append(node) |
paulb@213 | 48 | |
paulb@213 | 49 | def __str__(self): |
paulb@213 | 50 | |
paulb@213 | 51 | "Return a string showing the principal exception details." |
paulb@213 | 52 | |
paulb@213 | 53 | return "%s, %s" % (self.exc, self.nodes) |
paulb@213 | 54 | |
paulb@213 | 55 | # Elementary visitor support. |
paulb@213 | 56 | |
paulb@213 | 57 | class Visitor(ASTVisitor): |
paulb@213 | 58 | |
paulb@213 | 59 | "A visitor base class." |
paulb@213 | 60 | |
paulb@213 | 61 | def __init__(self): |
paulb@213 | 62 | ASTVisitor.__init__(self) |
paulb@213 | 63 | |
paulb@213 | 64 | def default(self, node, *args): |
paulb@213 | 65 | raise SimplifiedError, (None, node) |
paulb@213 | 66 | |
paulb@213 | 67 | def dispatch(self, node, *args): |
paulb@213 | 68 | return ASTVisitor.dispatch(self, node, *args) |
paulb@213 | 69 | |
paulb@213 | 70 | def dispatches(self, nodes, *args): |
paulb@213 | 71 | results = [] |
paulb@213 | 72 | for node in nodes: |
paulb@213 | 73 | results.append(self.dispatch(node, *args)) |
paulb@213 | 74 | return results |
paulb@213 | 75 | |
paulb@213 | 76 | def dispatch_dict(self, d, *args): |
paulb@213 | 77 | results = {} |
paulb@213 | 78 | for name, node in d.items(): |
paulb@213 | 79 | results[name] = self.dispatch(node, *args) |
paulb@213 | 80 | return results |
paulb@213 | 81 | |
paulb@213 | 82 | # Unique name registration. |
paulb@213 | 83 | |
paulb@213 | 84 | class Naming: |
paulb@213 | 85 | |
paulb@213 | 86 | "Maintain records of unique names for each simple name." |
paulb@213 | 87 | |
paulb@213 | 88 | index_separator = "-" |
paulb@213 | 89 | |
paulb@213 | 90 | def __init__(self): |
paulb@213 | 91 | self.names = {} |
paulb@213 | 92 | |
paulb@213 | 93 | def get(self, obj): |
paulb@213 | 94 | return obj._unique_name |
paulb@213 | 95 | |
paulb@213 | 96 | def set(self, obj, name): |
paulb@213 | 97 | if hasattr(obj, "_unique_name"): |
paulb@213 | 98 | return |
paulb@213 | 99 | if not self.names.has_key(name): |
paulb@213 | 100 | self.names[name] = 0 |
paulb@213 | 101 | n = self.names[name] + 1 |
paulb@213 | 102 | self.names[name] = n |
paulb@213 | 103 | obj._unique_name = "%s%s%d" % (name, self.index_separator, n) |
paulb@213 | 104 | |
paulb@213 | 105 | def name(obj, name): |
paulb@213 | 106 | |
paulb@213 | 107 | "Return a unique name for the given 'obj', indicating the base 'name'." |
paulb@213 | 108 | |
paulb@213 | 109 | naming.set(obj, name) |
paulb@213 | 110 | return naming.get(obj) |
paulb@213 | 111 | |
paulb@213 | 112 | # Naming singleton. |
paulb@213 | 113 | |
paulb@213 | 114 | naming = Naming() |
paulb@213 | 115 | |
paulb@213 | 116 | # Named nodes are those which can be referenced in some way. |
paulb@213 | 117 | |
paulb@213 | 118 | class WithName: |
paulb@213 | 119 | |
paulb@213 | 120 | "Node naming." |
paulb@213 | 121 | |
paulb@213 | 122 | def __init__(self): |
paulb@213 | 123 | |
paulb@213 | 124 | "Initialise the object's full name." |
paulb@213 | 125 | |
paulb@213 | 126 | self._full_name = name(self, self.name or "$untitled") |
paulb@213 | 127 | |
paulb@213 | 128 | def full_name(self): |
paulb@213 | 129 | |
paulb@213 | 130 | "Return the object's full name." |
paulb@213 | 131 | |
paulb@213 | 132 | return self._full_name |
paulb@213 | 133 | |
paulb@213 | 134 | # Comparable nodes based on naming. |
paulb@213 | 135 | |
paulb@213 | 136 | class Comparable: |
paulb@213 | 137 | |
paulb@213 | 138 | "Comparable nodes implementing the 'full_name' method." |
paulb@213 | 139 | |
paulb@213 | 140 | def __eq__(self, other): |
paulb@213 | 141 | |
paulb@213 | 142 | "This object is equal to 'other' if the full names are the same." |
paulb@213 | 143 | |
paulb@213 | 144 | # NOTE: Single instance: all instances are the same |
paulb@213 | 145 | # NOTE: Multiple instances: all instances are different |
paulb@213 | 146 | if hasattr(other, "full_name"): |
paulb@213 | 147 | return self.full_name() == other.full_name() |
paulb@213 | 148 | else: |
paulb@213 | 149 | return NotImplemented |
paulb@213 | 150 | |
paulb@213 | 151 | def __hash__(self): |
paulb@213 | 152 | |
paulb@213 | 153 | "The hash of this object is based on its full name." |
paulb@213 | 154 | |
paulb@213 | 155 | return hash(self.full_name()) |
paulb@213 | 156 | |
paulb@213 | 157 | # Structure nodes indicating namespace-bearing objects. |
paulb@213 | 158 | |
paulb@213 | 159 | class Structure(Comparable): |
paulb@213 | 160 | |
paulb@213 | 161 | "A non-program node containing some kind of namespace." |
paulb@213 | 162 | |
paulb@213 | 163 | def __init__(self, **kw): |
paulb@213 | 164 | for name, value in kw.items(): |
paulb@213 | 165 | setattr(self, name, value) |
paulb@213 | 166 | |
paulb@224 | 167 | def __repr__(self): |
paulb@224 | 168 | return "%s '%s'" % (self.__class__.__name__, self.full_name()) |
paulb@224 | 169 | |
paulb@213 | 170 | # vim: tabstop=4 expandtab shiftwidth=4 |