/usr/share/pyshared/terml/qnodes.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 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 227 228 229 230 231 232 233 234 235 | import itertools
from collections import namedtuple
from terml.nodes import Term, Tag, coerceToTerm
class QTerm(namedtuple("QTerm", "functor data args span")):
"""
A quasiterm, representing a template or pattern for a term tree.
"""
@property
def tag(self):
return self.functor.tag
def _substitute(self, map):
candidate = self.functor._substitute(map)[0]
args = tuple(itertools.chain.from_iterable(a._substitute(map) for a in self.args))
term = Term(candidate.tag, candidate.data, args, self.span)
return [term]
def substitute(self, map):
"""
Fill $-holes with named values.
@param map: A mapping of names to values to be inserted into
the term tree.
"""
return self._substitute(map)[0]
def match(self, specimen, substitutionArgs=()):
"""
Search a term tree for matches to this pattern. Returns a
mapping of names to matched values.
@param specimen: A term tree to extract values from.
"""
bindings = {}
if self._match(substitutionArgs, [specimen], bindings, (), 1) == 1:
return bindings
raise TypeError("%r doesn't match %r" % (self, specimen))
def _reserve(self):
return 1
def _match(self, args, specimens, bindings, index, max):
if not specimens:
return -1
spec = self._coerce(specimens[0])
if spec is None:
return -1
matches = self.functor._match(args, [spec.withoutArgs()], bindings, index, 1)
if not matches:
return -1
if matches > 1:
raise TypeError("Functor may only match 0 or 1 specimen")
num = matchArgs(self.args, spec.args, args, bindings, index, len(spec.args))
if len(spec.args) == num:
if max >= 1:
return 1
return -1
def _coerce(self, spec):
if isinstance(spec, Term):
newf = coerceToQuasiMatch(spec.withoutArgs(),
self.functor.isFunctorHole,
self.tag)
if newf is None:
return None
return Term(newf.asFunctor(), None, spec.args, None)
else:
return coerceToQuasiMatch(spec, self.functor.isFunctorHole,
self.tag)
def __eq__(self, other):
return ( self.functor, self.data, self.args
) == (other.functor, other.data, other.args)
def asFunctor(self):
if self.args:
raise ValueError("Terms with args can't be used as functors")
else:
return self.functor
class QFunctor(namedtuple("QFunctor", "tag data span")):
isFunctorHole = False
def _reserve(self):
return 1
@property
def name(self):
return self.tag.name
def _unparse(self, indentLevel=0):
return self.tag._unparse(indentLevel)
def _substitute(self, map):
return [Term(self.tag, self.data, None, self.span)]
def _match(self, args, specimens, bindings, index, max):
if not specimens:
return -1
spec = coerceToQuasiMatch(specimens[0], False, self.tag)
if spec is None:
return -1
if self.data is not None and self.data != spec.data:
return -1
if max >= 1:
return 1
return -1
def asFunctor(self):
return self
def matchArgs(quasiArglist, specimenArglist, args, bindings, index, max):
specs = specimenArglist
reserves = [q._reserve() for q in quasiArglist]
numConsumed = 0
for i, qarg in enumerate(quasiArglist):
num = qarg._match(args, specs, bindings, index, max - sum(reserves[i + 1:]))
if num == -1:
return -1
specs = specs[num:]
max -= num
numConsumed += num
return numConsumed
def coerceToQuasiMatch(val, isFunctorHole, tag):
if isFunctorHole:
if val is None:
result = Term(Tag("null"), None, None, None)
elif isinstance(val, Term):
if len(val.args) != 0:
return None
else:
result = val
elif isinstance(val, basestring):
result = Term(Tag(val), None, None, None)
elif isinstance(val, bool):
result = Term(Tag(["false", "true"][val]), None, None, None)
else:
return None
else:
result = coerceToTerm(val)
if tag is not None and result.tag != tag:
return None
return result
class _Hole(namedtuple("_Hole", "tag name isFunctorHole")):
def _reserve(self):
return 1
def __repr__(self):
return "term('%s')" % (self._unparse(4).replace("'", "\\'"))
def match(self, specimen, substitutionArgs=()):
bindings = {}
if self._match(substitutionArgs, [specimen], bindings, (), 1) != -1:
return bindings
raise TypeError("%r doesn't match %r" % (self, specimen))
def _multiget(args, holenum, index, repeat):
result = args[holenum]
for i in index:
if not isinstance(result, list):
return result
result = result[i]
return result
def _multiput(bindings, holenum, index, newval):
bits = bindings
dest = holenum
for it in index:
next = bits[dest]
if next is None:
next = {}
bits[dest] = next
bits = next
dest = it
result = None
if dest in bits:
result = bits[dest]
bits[dest] = newval
return result
class ValueHole(_Hole):
def _unparse(self, indentLevel=0):
return "${%s}" % (self.name,)
def _substitute(self, map):
termoid = map[self.name]
val = coerceToQuasiMatch(termoid, self.isFunctorHole, self.tag)
if val is None:
raise TypeError("%r doesn't match %r" % (termoid, self))
return [val]
def asFunctor(self):
if self.isFunctorHole:
return self
else:
return ValueHole(self.tag, self.name, True)
class PatternHole(_Hole):
def _unparse(self, indentLevel=0):
if self.tag:
return "%s@{%s}" % (self.tag.name, self.name)
else:
return "@{%s}" % (self.name,)
def _match(self, args, specimens, bindings, index, max):
if not specimens:
return -1
spec = coerceToQuasiMatch(specimens[0], self.isFunctorHole, self.tag)
if spec is None:
return -1
oldval = _multiput(bindings, self.name, index, spec)
if oldval is None or oldval != spec:
if max >= 1:
return 1
return -1
def asFunctor(self):
if self.isFunctorHole:
return self
else:
return PatternHole(self.tag, self.name, True)
class QSome(namedtuple("_QSome", "value quant")):
def _reserve(self):
if self.quant == "+":
return 1
else:
return 0
|