1 #!/usr/bin/env python 2 3 """ 4 Result abstractions. 5 6 Copyright (C) 2016 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 from referencing import Reference 23 24 # Classes representing inspection and translation observations. 25 26 class Result: 27 28 "An abstract expression result." 29 30 def is_name(self): 31 return False 32 33 def reference(self): 34 return None 35 36 def get_name(self): 37 return None 38 39 def get_origin(self): 40 return None 41 42 def static(self): 43 return None 44 45 def final(self): 46 return None 47 48 def has_kind(self, kinds): 49 return False 50 51 class AccessRef(Result): 52 53 """ 54 A reference to an attribute access that is generally only returned from a 55 processed access for possible initialiser resolution for assignments. 56 """ 57 58 def __init__(self, original_name, attrnames, number): 59 self.original_name = original_name 60 self.attrnames = attrnames 61 self.number = number 62 63 def __repr__(self): 64 return "AccessRef(%r, %r, %r)" % (self.original_name, self.attrnames, self.number) 65 66 class InvocationRef(Result): 67 68 "An invocation of a name reference." 69 70 def __init__(self, name_ref): 71 self.name_ref = name_ref 72 73 def reference(self): 74 origin = self.name_ref.get_origin() 75 if origin: 76 return Reference("<invoke>", origin) 77 else: 78 return Reference("<var>") 79 80 def __repr__(self): 81 return "InvocationRef(%r)" % self.name_ref 82 83 class NameRef(Result): 84 85 "A reference to a name." 86 87 def __init__(self, name, expr=None): 88 self.name = name 89 self.expr = expr 90 91 def is_name(self): 92 return True 93 94 def final(self): 95 return None 96 97 def __repr__(self): 98 return "NameRef(%r, %r)" % (self.name, self.expr) 99 100 class LocalNameRef(NameRef): 101 102 "A reference to a local name." 103 104 def __init__(self, name, number): 105 NameRef.__init__(self, name) 106 self.number = number 107 108 def __repr__(self): 109 return "LocalNameRef(%r, %r)" % (self.name, self.number) 110 111 class ResolvedRef: 112 113 "A resolved reference mix-in." 114 115 def reference(self): 116 return self.ref 117 118 def get_name(self): 119 return self.ref and self.ref.get_name() or None 120 121 def get_origin(self): 122 return self.ref and self.ref.get_origin() or None 123 124 def static(self): 125 return self.ref and self.ref.static() or None 126 127 def final(self): 128 return self.ref and self.ref.final() or None 129 130 def has_kind(self, kinds): 131 return self.ref and self.ref.has_kind(kinds) 132 133 class ResolvedNameRef(ResolvedRef, NameRef): 134 135 "A resolved name-based reference." 136 137 def __init__(self, name, ref, expr=None): 138 NameRef.__init__(self, name, expr) 139 self.ref = ref 140 141 def __repr__(self): 142 return "ResolvedNameRef(%r, %r, %r)" % (self.name, self.ref, self.expr) 143 144 class ConstantValueRef(ResolvedNameRef): 145 146 "A constant reference representing a single literal value." 147 148 def __init__(self, name, ref, value, number=None): 149 ResolvedNameRef.__init__(self, name, ref) 150 self.value = value 151 self.number = number 152 153 def __repr__(self): 154 return "ConstantValueRef(%r, %r, %r, %r)" % (self.name, self.ref, self.value, self.number) 155 156 class InstanceRef(ResolvedRef, Result): 157 158 "An instance reference." 159 160 def __init__(self, ref): 161 self.ref = ref 162 163 def reference(self): 164 return self.ref 165 166 def __repr__(self): 167 return "InstanceRef(%r)" % self.ref 168 169 class LiteralSequenceRef(ResolvedNameRef): 170 171 "A reference representing a sequence of values." 172 173 def __init__(self, name, ref, node, items=None): 174 ResolvedNameRef.__init__(self, name, ref) 175 self.node = node 176 self.items = items 177 178 def __repr__(self): 179 return "LiteralSequenceRef(%r, %r, %r, %r)" % (self.name, self.ref, self.node, self.items) 180 181 class VariableRef(Result): 182 183 "A variable reference." 184 185 def __repr__(self): 186 return "VariableRef()" 187 188 # vim: tabstop=4 expandtab shiftwidth=4