2000-02-15 20:49:47 -04:00
|
|
|
import types
|
|
|
|
|
|
|
|
def flatten(tup):
|
|
|
|
elts = []
|
|
|
|
for elt in tup:
|
|
|
|
if type(elt) == types.TupleType:
|
|
|
|
elts = elts + flatten(elt)
|
|
|
|
else:
|
|
|
|
elts.append(elt)
|
|
|
|
return elts
|
|
|
|
|
2000-02-03 20:28:21 -04:00
|
|
|
class Set:
|
|
|
|
def __init__(self):
|
2000-02-21 18:46:00 -04:00
|
|
|
self.elts = {}
|
2000-02-10 16:54:27 -04:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.elts)
|
2000-11-05 23:43:11 -04:00
|
|
|
def __contains__(self, elt):
|
|
|
|
return self.elts.has_key(elt)
|
2000-02-03 20:28:21 -04:00
|
|
|
def add(self, elt):
|
2000-02-21 18:46:00 -04:00
|
|
|
self.elts[elt] = elt
|
2000-03-16 16:02:38 -04:00
|
|
|
def elements(self):
|
2000-02-21 18:46:00 -04:00
|
|
|
return self.elts.keys()
|
2000-02-03 20:28:21 -04:00
|
|
|
def has_elt(self, elt):
|
2000-02-21 18:46:00 -04:00
|
|
|
return self.elts.has_key(elt)
|
2000-02-09 20:43:22 -04:00
|
|
|
def remove(self, elt):
|
|
|
|
del self.elts[elt]
|
2000-11-05 23:43:11 -04:00
|
|
|
def copy(self):
|
|
|
|
c = Set()
|
|
|
|
c.elts.update(self.elts)
|
|
|
|
return c
|
2000-02-03 20:28:21 -04:00
|
|
|
|
|
|
|
class Stack:
|
|
|
|
def __init__(self):
|
2000-02-21 18:46:00 -04:00
|
|
|
self.stack = []
|
|
|
|
self.pop = self.stack.pop
|
2000-02-10 16:54:27 -04:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.stack)
|
2000-02-03 20:28:21 -04:00
|
|
|
def push(self, elt):
|
2000-02-21 18:46:00 -04:00
|
|
|
self.stack.append(elt)
|
2000-02-03 20:28:21 -04:00
|
|
|
def top(self):
|
2000-02-21 18:46:00 -04:00
|
|
|
return self.stack[-1]
|
2001-08-29 19:26:35 -03:00
|
|
|
def __getitem__(self, index): # needed by visitContinue()
|
|
|
|
return self.stack[index]
|
2001-08-27 19:56:16 -03:00
|
|
|
|
|
|
|
MANGLE_LEN = 256 # magic constant from compile.c
|
|
|
|
|
|
|
|
def mangle(name, klass):
|
|
|
|
if not name.startswith('__'):
|
|
|
|
return name
|
|
|
|
if len(name) + 2 >= MANGLE_LEN:
|
|
|
|
return name
|
|
|
|
if name.endswith('__'):
|
|
|
|
return name
|
|
|
|
try:
|
|
|
|
i = 0
|
|
|
|
while klass[i] == '_':
|
|
|
|
i = i + 1
|
|
|
|
except IndexError:
|
|
|
|
return name
|
|
|
|
klass = klass[i:]
|
|
|
|
|
|
|
|
tlen = len(klass) + len(name)
|
|
|
|
if tlen > MANGLE_LEN:
|
|
|
|
klass = klass[:MANGLE_LEN-tlen]
|
|
|
|
|
|
|
|
return "_%s%s" % (klass, name)
|
2001-09-17 15:02:21 -03:00
|
|
|
|
|
|
|
def set_filename(filename, tree):
|
|
|
|
"""Set the filename attribute to filename on every node in tree"""
|
|
|
|
worklist = [tree]
|
|
|
|
while worklist:
|
|
|
|
node = worklist.pop(0)
|
|
|
|
node.filename = filename
|
|
|
|
worklist.extend(node.getChildNodes())
|
|
|
|
|