Lichen

results.py

26:f47c63967c59
2016-09-05 Paul Boddie Separated inspection-related naming methods from common module methods.
     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 # Classes representing inspection and translation observations.    23     24 class Result:    25     26     "An abstract expression result."    27     28     def is_name(self):    29         return False    30     def get_origin(self):    31         return None    32     33 class AccessRef(Result):    34     35     """    36     A reference to an attribute access that is generally only returned from a    37     processed access for possible initialiser resolution for assignments.    38     """    39     40     def __init__(self, original_name, attrnames, number):    41         self.original_name = original_name    42         self.attrnames = attrnames    43         self.number = number    44     45     def reference(self):    46         return None    47     48     def __repr__(self):    49         return "AccessRef(%r, %r, %r)" % (self.original_name, self.attrnames, self.number)    50     51 class InvocationRef(Result):    52     53     "An invocation of a name reference."    54     55     def __init__(self, name_ref):    56         self.name_ref = name_ref    57     58     def __repr__(self):    59         return "InvocationRef(%r)" % self.name_ref    60     61 class NameRef(Result):    62     63     "A reference to a name."    64     65     def __init__(self, name, expr=None):    66         self.name = name    67         self.expr = expr    68     69     def is_name(self):    70         return True    71     72     def reference(self):    73         return None    74     75     def final(self):    76         return None    77     78     def __repr__(self):    79         return "NameRef(%r, %r)" % (self.name, self.expr)    80     81 class LocalNameRef(NameRef):    82     83     "A reference to a local name."    84     85     def __init__(self, name, number):    86         NameRef.__init__(self, name)    87         self.number = number    88     89     def __repr__(self):    90         return "LocalNameRef(%r, %r)" % (self.name, self.number)    91     92 class ResolvedNameRef(NameRef):    93     94     "A resolved name-based reference."    95     96     def __init__(self, name, ref, expr=None):    97         NameRef.__init__(self, name, expr)    98         self.ref = ref    99    100     def reference(self):   101         return self.ref   102    103     def get_name(self):   104         return self.ref and self.ref.get_name() or None   105    106     def get_origin(self):   107         return self.ref and self.ref.get_origin() or None   108    109     def static(self):   110         return self.ref and self.ref.static() or None   111    112     def final(self):   113         return self.ref and self.ref.final() or None   114    115     def has_kind(self, kinds):   116         return self.ref and self.ref.has_kind(kinds)   117    118     def __repr__(self):   119         return "ResolvedNameRef(%r, %r, %r)" % (self.name, self.ref, self.expr)   120    121 class ConstantValueRef(ResolvedNameRef):   122    123     "A constant reference representing a single literal value."   124    125     def __init__(self, name, ref, value, number=None):   126         ResolvedNameRef.__init__(self, name, ref)   127         self.value = value   128         self.number = number   129    130     def __repr__(self):   131         return "ConstantValueRef(%r, %r, %r, %r)" % (self.name, self.ref, self.value, self.number)   132    133 class InstanceRef(Result):   134    135     "An instance reference."   136    137     def __init__(self, ref):   138         self.ref = ref   139    140     def reference(self):   141         return self.ref   142    143     def __repr__(self):   144         return "InstanceRef(%r)" % self.ref   145    146 class LiteralSequenceRef(ResolvedNameRef):   147    148     "A reference representing a sequence of values."   149    150     def __init__(self, name, ref, node, items=None):   151         ResolvedNameRef.__init__(self, name, ref)   152         self.node = node   153         self.items = items   154    155     def __repr__(self):   156         return "LiteralSequenceRef(%r, %r, %r, %r)" % (self.name, self.ref, self.node, self.items)   157    158 # vim: tabstop=4 expandtab shiftwidth=4