- Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathscoped_name.py
226 lines (153 loc) · 5.69 KB
/
scoped_name.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2024 Phil Thompson <phil@riverbankcomputing.com>
# Specify how scopes should be stripped. Any other value is the number of
# scopes to strip.
STRIP_NONE=0# Don't strip any scopes.
STRIP_GLOBAL=-1# Strip the global scope.
classScopedName:
""" Encapsulate a scoped name. """
def__init__(self, name):
""" Initialise the scoped name. """
ifisinstance(name, list):
# This use is internal to this class.
self._name=name
elifisinstance(name, ScopedName):
# Make a copy of another ScopedName object.
self._name=list(name._name)
else:
# Create a simple name.
self._name= [name]
def__eq__(self, other):
""" Compare with another scoped name for equality. """
ifisinstance(other, ScopedName):
returnself._name==other._name
returnNotImplemented
def__delitem__(self, index):
""" Remove the requested name. """
delself._name[self._normalised_index(index)]
def__getitem__(self, index):
""" Get the requested name. """
returnself._name[self._normalised_index(index)]
def__len__(self):
""" Return the length of the name, ie. the number of individual names.
"""
nr_names=len(self._name)
ifself.is_absolute:
nr_names-=1
returnnr_names
def__lt__(self, other):
""" Compare with another scoped name to allow lists to be sorted. """
ifisinstance(other, ScopedName):
returnself._name<other._name
returnNotImplemented
def__setitem__(self, index, name):
""" Set the requested name. """
self._name[self._normalised_index(index)] =name
def__str__(self):
""" Return the C++ string representation. """
# The special treatment is a hack to simplify how the result is used.
# We ignore any leading '::' and truncate the conversion if we think we
# are converting an encoded template name (ie. we find a word that
# starts with a digit).
s_l= []
forsinself:
ifs[0].isdigit():
break
s_l.append(s)
return'::'.join(s_l)
@property
defabsolute(self):
""" The absolute version of the name. """
ifself.is_absolute:
returnself
copy=type(self)(self)
copy.make_absolute()
returncopy
defappend(self, name):
""" Append a simple name. """
self._name.append(name)
@property
defas_cpp(self):
""" The C++ representation of the name. """
return'::'.join(self._name)
@property
defas_py(self):
""" The Python representation of the name. """
start=1ifself.is_absoluteelse0
return'.'.join(self._name[start:])
@property
defas_word(self):
""" The word representation of the name. """
start=1ifself.is_absoluteelse0
return'_'.join(self._name[start:])
@property
defbase_name(self):
""" The base name of the scoped name. """
returnself._name[-1]
defcpp_stripped(self, strip):
""" Return the C++ representation of the name with leading scopes
stripped.
"""
ifstrip==STRIP_NONE:
start=0
else:
start=1ifself.is_absoluteelse0
ifstrip!=STRIP_GLOBAL:
start+=strip
# Never strip the base name.
ifstart>=len(self._name):
returnself._name[-1]
return'::'.join(self._name[start:])
@property
defis_absolute(self):
""" True if the scoped name is absolute. """
returnself._name[0] ==''
@property
defis_simple(self):
""" Return True if the name is simple, ie. relative and unscoped. """
returnlen(self) ==1
defmake_absolute(self):
""" Make sure the scoped name is absolute. """
ifself._name[0] !='':
self._name.insert(0, '')
defmatches(self, scoped_name, scope=None):
""" Return True if a scoped name matches this taking account of an
optional scope if the scoped name is relative.
"""
assertself.is_absolute
# Check for the simple case.
ifscoped_name.is_absolute:
returnself==scoped_name
# Try each scope, inner to outer.
whilescopeisnotNone:
fq_name=ScopedName(scope.iface_file.fq_cpp_name)
fq_name._name.extend(scoped_name._name)
ifself==fq_name:
returnTrue
scope=scope.scope
# Treat the name as if it were absolute.
returnself._name[1:] ==scoped_name._name
@classmethod
defparse(cls, raw):
""" Return a ScopedName object by parsing a raw string. """
returncls(raw.split('::'))
defprepend(self, scoped_name):
""" Prepend a scoped name. """
new_name=list(scoped_name._name)
new_name.extend(self._name)
self._name=new_name
@property
defscope(self):
""" The scoped name that is the enclosing scope of this one. It will
be None if there isn't one.
"""
iflen(self) ==1:
returnNone
returntype(self)(self._name[:-1])
def_normalised_index(self, index):
""" Return a normalised index. """
ifnotisinstance(index, int):
raiseTypeError("'{}' is an invalid index".format(type(index)))
ifindex>=0andself.is_absolute:
index+=1
returnindex