Lichen

results.py

407:7fcbb79569d8
2016-12-15 Paul Boddie Access method defaults even if the accessor is not yet determined.
     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     def is_constant_alias(self):   134         return self.ref and self.ref.is_constant_alias()   135    136 class ResolvedNameRef(ResolvedRef, NameRef):   137    138     "A resolved name-based reference."   139    140     def __init__(self, name, ref, expr=None):   141         NameRef.__init__(self, name, expr)   142         self.ref = ref   143    144     def __repr__(self):   145         return "ResolvedNameRef(%r, %r, %r)" % (self.name, self.ref, self.expr)   146    147 class ConstantValueRef(ResolvedNameRef):   148    149     "A constant reference representing a single literal value."   150    151     def __init__(self, name, ref, value, number=None):   152         ResolvedNameRef.__init__(self, name, ref)   153         self.value = value   154         self.number = number   155    156     def __repr__(self):   157         return "ConstantValueRef(%r, %r, %r, %r)" % (self.name, self.ref, self.value, self.number)   158    159 class InstanceRef(ResolvedRef, Result):   160    161     "An instance reference."   162    163     def __init__(self, ref):   164         self.ref = ref   165    166     def reference(self):   167         return self.ref   168    169     def __repr__(self):   170         return "InstanceRef(%r)" % self.ref   171    172 class LiteralSequenceRef(ResolvedNameRef):   173    174     "A reference representing a sequence of values."   175    176     def __init__(self, name, ref, node, items=None):   177         ResolvedNameRef.__init__(self, name, ref)   178         self.node = node   179         self.items = items   180    181     def __repr__(self):   182         return "LiteralSequenceRef(%r, %r, %r, %r)" % (self.name, self.ref, self.node, self.items)   183    184 class VariableRef(Result):   185    186     "A variable reference."   187    188     def __repr__(self):   189         return "VariableRef()"   190    191 # vim: tabstop=4 expandtab shiftwidth=4