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 ResolvedNameRef(NameRef): 112 113 "A resolved name-based reference." 114 115 def __init__(self, name, ref, expr=None): 116 NameRef.__init__(self, name, expr) 117 self.ref = ref 118 119 def reference(self): 120 return self.ref 121 122 def get_name(self): 123 return self.ref and self.ref.get_name() or None 124 125 def get_origin(self): 126 return self.ref and self.ref.get_origin() or None 127 128 def static(self): 129 return self.ref and self.ref.static() or None 130 131 def final(self): 132 return self.ref and self.ref.final() or None 133 134 def has_kind(self, kinds): 135 return self.ref and self.ref.has_kind(kinds) 136 137 def __repr__(self): 138 return "ResolvedNameRef(%r, %r, %r)" % (self.name, self.ref, self.expr) 139 140 class ConstantValueRef(ResolvedNameRef): 141 142 "A constant reference representing a single literal value." 143 144 def __init__(self, name, ref, value, number=None): 145 ResolvedNameRef.__init__(self, name, ref) 146 self.value = value 147 self.number = number 148 149 def __repr__(self): 150 return "ConstantValueRef(%r, %r, %r, %r)" % (self.name, self.ref, self.value, self.number) 151 152 class InstanceRef(Result): 153 154 "An instance reference." 155 156 def __init__(self, ref): 157 self.ref = ref 158 159 def reference(self): 160 return self.ref 161 162 def __repr__(self): 163 return "InstanceRef(%r)" % self.ref 164 165 class LiteralSequenceRef(ResolvedNameRef): 166 167 "A reference representing a sequence of values." 168 169 def __init__(self, name, ref, node, items=None): 170 ResolvedNameRef.__init__(self, name, ref) 171 self.node = node 172 self.items = items 173 174 def __repr__(self): 175 return "LiteralSequenceRef(%r, %r, %r, %r)" % (self.name, self.ref, self.node, self.items) 176 177 class VariableRef(Result): 178 179 "A variable reference." 180 181 def __repr__(self): 182 return "VariableRef()" 183 184 # vim: tabstop=4 expandtab shiftwidth=4