/usr/share/pyshared/terml/nodes.py is in python-parsley 1.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | from collections import namedtuple
_Term = namedtuple("Term", "tag data args span")
class Term(_Term):
def __new__(cls, tag, data, args, span):
#XXX AstroTag tracks (name, tag_code) and source span
if data and not isinstance(data, (str, unicode, int, long, float)):
raise ValueError("Term data can't be of type %r" % (type(data),))
if data and args:
raise ValueError("Term %s can't have both data and children" % (tag,))
if args is None:
args = ()
return _Term.__new__(cls, tag, data, tuple(args), span)
def __iter__(self):
#and now I feel a bit silly subclassing namedtuple
raise NotImplementedError()
def __eq__(self, other):
try:
return ( self.tag, self.data, self.args
) == (other.tag, other.data, other.args)
except AttributeError:
return False
def __repr__(self):
return "term('%s')" % (self._unparse(4).replace("'", "\\'"))
def _unparse(self, indentLevel=0):
newlineAndIndent = '\n' + (' ' * indentLevel)
if self.data is not None:
if self.tag.name == '.String.':
return '"%s"' % repr(self.data)[1:-1].replace("\\'", "'").replace('"', '\\\\"')
elif self.tag.name == '.char.':
return "'%s'" % repr(self.data)[1:-1].replace("'", "\\'").replace('\\"', '"')
else:
return str(self.data)
args = ', '.join([a._unparse() for a in self.args])
if self.tag.name == '.tuple.':
return "[%s]" % (args,)
elif self.tag.name == '.attr.':
return "%s: %s" % (self.args[0]._unparse(indentLevel),
self.args[1]._unparse(indentLevel))
elif self.tag.name == '.bag.':
return "{%s}" % (args,)
elif len(self.args) == 1 and self.args[0].tag.name == '.bag.':
return "%s%s" % (self.tag._unparse(indentLevel), args)
else:
if len(self.args) == 0:
return self.tag._unparse(indentLevel)
return "%s(%s)" % (self.tag._unparse(indentLevel), args)
def withSpan(self, span):
return Term(self.tag, self.data, self.args, span)
def build(self, builder):
if self.data is None:
f = builder.leafTag(self.tag, self.span)
else:
f = builder.leafData(self.data, self.span)
return builder.term(f, [arg.build(builder) for arg in self.args])
def __cmp__(self, other):
tagc = cmp(self.tag, other.tag)
if tagc:
return tagc
datac = cmp(self.data, other.data)
if datac:
return datac
return cmp(self.args, other.args)
def __int__(self):
return int(self.data)
def __float__(self):
return float(self.data)
def withoutArgs(self):
return Term(self.tag, self.data, (), self.span)
def asFunctor(self):
if self.args:
raise ValueError("Terms with args can't be used as functors")
else:
return self.tag
class Tag(object):
def __init__(self, name):
if name[0] == '':
raise ValueError("Tags must have names")
self.name = name
def __eq__(self, other):
return other.__class__ == self.__class__ and self.name == other.name
def __ne__(self, other):
return not self == other
def __repr__(self):
return "Tag(%r)" % (self.name,)
def _unparse(self, indentLevel=0):
return self.name
def coerceToTerm(val):
from ometa.runtime import character, unicodeCharacter
if isinstance(val, Term):
return val
if val is None:
return Term(Tag("null"), None, None, None)
if val is True:
return Term(Tag("true"), None, None, None)
if val is False:
return Term(Tag("false"), None, None, None)
if isinstance(val, (int, long)):
return Term(Tag(".int."), val, None, None)
if isinstance(val, float):
return Term(Tag(".float64."), val, None, None)
if isinstance(val, (character, unicodeCharacter)):
return Term(Tag(".char."), val, None, None)
if isinstance(val, basestring):
return Term(Tag(".String."), val, None, None)
if isinstance(val, (list, tuple)):
return Term(Tag(".tuple."), None, tuple(coerceToTerm(item) for item in val), None)
if isinstance(val, set):
return Term(Tag('.bag.'), None, tuple(coerceToTerm(item) for item in val), None)
if isinstance(val, dict):
return Term(Tag('.bag.'), None, tuple(Term(Tag('.attr.'), None,
(coerceToTerm(k), coerceToTerm(v)), None)
for (k, v) in val.iteritems()),
None)
raise ValueError("Could not coerce %r to Term" % (val,))
class TermMaker(object):
def __getattr__(self, name):
def mkterm(*args, **kwargs):
return Term(Tag(name), None,
tuple([coerceToTerm(a) for a in args]),
kwargs.get('span', None))
return mkterm
termMaker = TermMaker()
|