Lichen

results.py

681:c94d43f85d6f
2017-03-08 Paul Boddie Changed the key used in the aliased and initialised names mappings to incorporate namespace path details.
     1 #!/usr/bin/env python     2      3 """     4 Result abstractions.     5      6 Copyright (C) 2016, 2017 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 is_global_name(self):    34         return False    35     36     def reference(self):    37         return None    38     39     def references(self):    40         return None    41     42     def access_location(self):    43         return None    44     45     def context_identity(self):    46         return None    47     48     def discards_temporary(self, test=True):    49         return None    50     51     def get_name(self):    52         return None    53     54     def get_origin(self):    55         return None    56     57     def static(self):    58         return None    59     60     def final(self):    61         return None    62     63     def has_kind(self, kinds):    64         return False    65     66 class AccessRef(Result):    67     68     """    69     A reference to an attribute access that is generally only returned from a    70     processed access for possible initialiser resolution for assignments.    71     """    72     73     def __init__(self, original_name, attrnames, number):    74         self.original_name = original_name    75         self.attrnames = attrnames    76         self.number = number    77     78     def __repr__(self):    79         return "AccessRef(%r, %r, %r)" % (self.original_name, self.attrnames, self.number)    80     81 class InvocationRef(Result):    82     83     "An invocation of a name reference."    84     85     def __init__(self, name_ref):    86         self.name_ref = name_ref    87     88     def reference(self):    89         origin = self.name_ref.get_origin()    90         if origin:    91             return Reference("<invoke>", origin)    92         else:    93             return Reference("<var>")    94     95     def __repr__(self):    96         return "InvocationRef(%r)" % self.name_ref    97     98 class NameRef(Result):    99    100     "A reference to a name."   101    102     def __init__(self, name, expr=None, is_global=False):   103         self.name = name   104         self.expr = expr   105         self.is_global = is_global   106    107     def is_name(self):   108         return True   109    110     def is_global_name(self):   111         return self.is_global   112    113     def final(self):   114         return None   115    116     def __repr__(self):   117         return "NameRef(%r, %r, %r)" % (self.name, self.expr, self.is_global)   118    119 class LocalNameRef(NameRef):   120    121     "A reference to a local name."   122    123     def __init__(self, name, number):   124         NameRef.__init__(self, name, is_global=False)   125         self.number = number   126    127     def __repr__(self):   128         return "LocalNameRef(%r, %r)" % (self.name, self.number)   129    130 class ResolvedRef:   131    132     "A resolved reference mix-in."   133    134     def __init__(self, ref):   135         self.ref = ref   136    137     def reference(self):   138         return self.ref   139    140     def references(self):   141         return [self.ref]   142    143     def get_name(self):   144         return self.ref and self.ref.get_name() or None   145    146     def get_origin(self):   147         return self.ref and self.ref.get_origin() or None   148    149     def static(self):   150         return self.ref and self.ref.static() or None   151    152     def final(self):   153         return self.ref and self.ref.final() or None   154    155     def has_kind(self, kinds):   156         return self.ref and self.ref.has_kind(kinds)   157    158     def is_constant_alias(self):   159         return self.ref and self.ref.is_constant_alias()   160    161 class ResolvedNameRef(ResolvedRef, NameRef):   162    163     "A resolved name-based reference."   164    165     def __init__(self, name, ref, expr=None, is_global=False):   166         NameRef.__init__(self, name, expr, is_global)   167         ResolvedRef.__init__(self, ref)   168    169     def __repr__(self):   170         return "ResolvedNameRef(%r, %r, %r, %r)" % (self.name, self.ref, self.expr, self.is_global)   171    172 class ConstantValueRef(ResolvedNameRef):   173    174     "A constant reference representing a single literal value."   175    176     def __init__(self, name, ref, value, number=None):   177         ResolvedNameRef.__init__(self, name, ref)   178         self.value = value   179         self.number = number   180    181     def __repr__(self):   182         return "ConstantValueRef(%r, %r, %r, %r)" % (self.name, self.ref, self.value, self.number)   183    184 class InstanceRef(ResolvedRef, Result):   185    186     "An instance reference."   187    188     def reference(self):   189         return self.ref   190    191     def __repr__(self):   192         return "InstanceRef(%r)" % self.ref   193    194 class LiteralSequenceRef(ResolvedNameRef):   195    196     "A reference representing a sequence of values."   197    198     def __init__(self, name, ref, node, items=None):   199         ResolvedNameRef.__init__(self, name, ref)   200         self.node = node   201         self.items = items   202    203     def __repr__(self):   204         return "LiteralSequenceRef(%r, %r, %r, %r)" % (self.name, self.ref, self.node, self.items)   205    206 class VariableRef(Result):   207    208     "A variable reference."   209    210     def __repr__(self):   211         return "VariableRef()"   212    213 # vim: tabstop=4 expandtab shiftwidth=4