/
semantics.py
515 lines (475 loc) · 18.5 KB
/
semantics.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
"""
# Semantics of search templates
"""
import types
import re
from .relations import add_K_Relations, add_F_Relations, add_V_Relations
from .syntax import reTp, kRe, deContext
# SEMANTIC ANALYSIS OF SEARCH TEMPLATE ###
def semantics(searchExe):
if not searchExe.good:
return
error = searchExe.api.TF.error
_msgCache = searchExe._msgCache
searchExe.badSemantics = []
offset = searchExe.offset
_grammar(searchExe)
if not searchExe.good:
searchExe.showOuterTemplate(_msgCache)
for (i, line) in enumerate(searchExe.searchLines):
error(f"{i + offset:>2} {line}", tm=False, cache=_msgCache)
for (ln, eline) in searchExe.badSemantics:
txt = eline if ln is None else f"line {ln + offset}: {eline}"
error(txt, tm=False, cache=_msgCache)
return
if searchExe.good:
_validation(searchExe)
if not searchExe.good:
searchExe.showOuterTemplate(_msgCache)
for (i, line) in enumerate(searchExe.searchLines):
error(f"{i + offset:>2} {line}", tm=False, cache=_msgCache)
for (ln, eline) in searchExe.badSemantics:
txt = eline if ln is None else f"line {ln + offset}: {eline}"
error(txt, tm=False, cache=_msgCache)
def _grammar(searchExe):
prevKind = None
good = True
qnames = {}
qnodes = []
qedges = []
edgeLine = {}
nodeLine = {}
nTokens = len(searchExe.tokens)
def tokenSort(t):
return (nTokens + t["ln"]) if t["kind"] == "rel" else t["ln"]
tokens = sorted(searchExe.tokens, key=tokenSort)
# atomStack is a stack of qnodes with their indent levels
# such that every next member is one level deeper
# and every member is the last qnode encountered at that level
# The stack is implemented as a dict,
# keyed by the indent, and valued by the qnode
atomStack = {}
for token in tokens:
i = token["ln"]
kind = token["kind"]
if kind == "atom":
if "quantifiers" in token:
token["quantifiers"] = [
deContext(q, token["name"]) for q in token["quantifiers"]
]
indent = token["indent"]
op = token["op"]
if "name" in token:
name = token["name"]
otype = token["otype"]
features = token["features"]
src = token.get("src", "")
quantifiers = token.get("quantifiers", [])
qnodes.append((otype, features, src, quantifiers))
q = len(qnodes) - 1
nodeLine[q] = i
name = f":{i}" if name == "" else name
qnames[name] = q
if len(atomStack) == 0:
if indent > 0:
searchExe.badSemantics.append(
(i, f"Unexpected indent: {indent}, expected 0")
)
good = False
if op is not None:
searchExe.badSemantics.append(
(i, "Lonely relation: not allowed at outermost level")
)
good = False
if "name" in token:
atomStack[0] = q
else:
atomNest = sorted(atomStack.items(), key=lambda x: x[0])
top = atomNest[-1]
if indent == top[0]:
# sibling of previous atom
if len(atomNest) > 1:
if "name" in token:
# take the qnode of the subtop of the
# atomStack, if there is one
qedges.append((q, "]]", atomNest[-2][1]))
edgeLine[len(qedges) - 1] = i
if op is not None:
qedges.append((top[1], op, q))
edgeLine[len(qedges) - 1] = i
else:
# lonely operator:
# left is previous atom, right is parent atom
qedges.append((top[1], op, atomNest[-2][1]))
edgeLine[len(qedges) - 1] = i
else:
if op is not None:
qedges.append((top[1], op, q))
edgeLine[len(qedges) - 1] = i
elif indent > top[0]:
if "name" in token:
# child of previous atom
qedges.append((q, "]]", top[1]))
edgeLine[len(qedges) - 1] = i
if op is not None:
qedges.append((top[1], op, q))
edgeLine[len(qedges) - 1] = i
else:
searchExe.badSemantics.append(
(i, "Lonely relation: not allowed as first child")
)
good = False
else:
# outdent action:
# look up the proper parent in the stack
if indent in atomStack:
parents = [at[1] for at in atomNest if at[0] < indent]
if "name" in token:
if op is not None:
qedges.append((atomStack[indent], op, q))
edgeLine[len(qedges) - 1] = i
if len(parents) != 0: # if not already at outermost level
if "name" in token:
qedges.append((q, "]]", parents[-1]))
edgeLine[len(qedges) - 1] = i
else:
# connect previous sibling to parent
qedges.append((atomStack[indent], op, parents[-1]))
edgeLine[len(qedges) - 1] = i
removeKeys = [at[0] for at in atomNest if at[0] > indent]
for rk in removeKeys:
del atomStack[rk]
else:
# parent cannot be found: indentation error
searchExe.badSemantics.append(
(
i,
"Unexpected indent: {}, expected one of {}".format(
indent,
", ".join(
str(at[0]) for at in atomNest if at[0] < indent
),
),
)
)
good = False
atomStack[indent] = q
elif kind == "feat":
features = token["features"]
if prevKind is not None and prevKind not in {"atom", "feat"}:
searchExe.badSemantics.append(
(i, f'Features after {prevKind}: "{features}"')
)
good = False
else:
if len(qnodes):
qnodes[-1][1].update(features)
elif kind == "rel":
fName = token["f"]
tName = token["t"]
op = token["op"]
f = qnames.get(fName, None)
t = qnames.get(tName, None)
namesGood = True
for (q, n) in ((f, fName), (t, tName)):
if q is None:
searchExe.badSemantics.append(
(i, f'Relation with undefined name: "{n}"')
)
namesGood = False
if not namesGood:
good = False
else:
qedges.append((f, op, t))
edgeLine[len(qedges) - 1] = i
prevKind = kind
# resolve names when used in atoms
for (q, qdata) in enumerate(qnodes):
otype = qdata[0]
referQ = qnames.get(otype, None)
if referQ is not None:
referOtype = qnodes[referQ][0]
qnodes[q] = (referOtype, *qdata[1:])
qedges.append((q, "=", referQ))
if good:
searchExe.qnames = qnames
searchExe.qnodes = qnodes
searchExe.qedgesRaw = qedges
searchExe.nodeLine = nodeLine
searchExe.edgeLine = edgeLine
else:
searchExe.good = False
def _validateFeature(
searchExe,
q,
fName,
features,
missingFeatures,
wrongValues,
hasValues={},
asEdge=False,
):
values = features[fName]
fSet = "edges" if asEdge else "nodes"
if fName not in searchExe.api.TF.featureSets[fSet]:
missingFeatures.setdefault(fName, []).append(q)
else:
if asEdge:
doValues = searchExe.api.TF.features[fName].edgeValues
if not doValues and values is not True:
hasValues.setdefault(fName, {}).setdefault(values, []).append(q)
return
requiredType = searchExe.api.TF.features[fName].dataType
if values is True:
return
elif values is None:
return
elif isinstance(values, types.FunctionType):
if requiredType == "str":
wrongValues.setdefault(fName, {}).setdefault(values, []).append(q)
elif isinstance(values, reTp):
if requiredType == "int":
wrongValues.setdefault(fName, {}).setdefault(values, []).append(q)
else:
valuesCast = set()
if requiredType == "int":
(ident, values) = values
if type(values) is not bool:
for val in values:
try:
valCast = int(val)
except Exception:
valCast = val
wrongValues.setdefault(fName, {}).setdefault(
val, []
).append(q)
valuesCast.add(valCast)
features[fName] = (ident, frozenset(valuesCast))
def _validation(searchExe):
levels = searchExe.api.C.levels.data
otypes = {x[0] for x in levels}
qnodes = searchExe.qnodes
nodeLine = searchExe.nodeLine
edgeMap = searchExe.edgeMap
nodeMap = searchExe.nodeMap
edgeLine = searchExe.edgeLine
relationFromName = searchExe.relationFromName
offset = searchExe.offset
# check the object types of atoms
good = True
otypesGood = True
sets = searchExe.sets
for (q, qdata) in enumerate(qnodes):
otype = qdata[0]
if otype == ".":
continue
if sets is not None and otype in sets:
continue
if otype not in otypes:
searchExe.badSemantics.append(
(nodeLine[q], f'Unknown object type: "{otype}"')
)
otypesGood = False
if not otypesGood:
searchExe.badSemantics.append(
(
None,
"Valid object types are: {}".format(", ".join(x[0] for x in levels)),
)
)
if sets is not None:
searchExe.badSemantics.append(
(
None,
"Or choose a custom set from: {}".format(
", ".join(x for x in sorted(sets)),
),
)
)
good = False
# check the feature names of feature specs
# and check the types of their values
missingFeatures = {}
wrongValues = {}
wrongTypes = {}
hasValues = {}
for (q, qdata) in enumerate(qnodes):
features = qdata[1]
for fName in sorted(features):
_validateFeature(
searchExe, q, fName, features, missingFeatures, wrongValues
)
# check the relational operator token in edges
# and replace them by an index
# in the relations list of known relations
qedges = []
edgesGood = True
# relations may have a variable number k in them (k-nearness, etc.)
# make an entry in the relation map for each value of k
addRels = {}
for (e, (f, op, t)) in enumerate(searchExe.qedgesRaw):
if (
type(op) is tuple
or (op[0] == "-" and op[-1] == ">")
or (op[0] == "<" and op[-1] == "-")
or (op[0] == "<" and op[-1] == ">")
or (op[0] == "." and op[-1] == ".")
):
continue
match = kRe.findall(op)
if len(match):
(pre, k, post) = match[0]
opNameK = f"{pre}k{post}"
addRels.setdefault(opNameK, set()).add(int(k))
if not missingFeatures and not wrongValues:
add_K_Relations(searchExe, addRels)
# relations may have one or two node features f,g in them (feature-comparison)
# make an entry in the relation map for each value of (f, g)
fPatOne = r"^\.([^=#<>]+)\.$"
fPatBoth = r"^\.([^=#<>]+)([=#<>])(.*)\.$"
fPatMatch = r"^\.([^~]+)~(.*?)~([^~]+)\.$"
fOneRe = re.compile(fPatOne)
fBothRe = re.compile(fPatBoth)
fMatchRe = re.compile(fPatMatch)
addRels = {}
for (e, (f, op, t)) in enumerate(searchExe.qedgesRaw):
if type(op) is tuple:
continue
match = fMatchRe.findall(op)
if len(match):
(fF, r, gF) = match[0]
opNameFG = ".f~r~g."
addRels.setdefault(opNameFG, set()).add(((f, fF), r, (t, gF)))
for fName in (fF, gF):
fType = searchExe.api.TF.features[fName].dataType
if fType != "str":
wrongTypes.setdefault(fName, {}).setdefault(fType, set()).add(e)
else:
match = fBothRe.findall(op)
if len(match):
(fF, r, gF) = match[0]
opNameFG = f".f{r}g."
addRels.setdefault(opNameFG, set()).add(((f, fF), (t, gF)))
if r in {"<", ">"}:
for fName in (fF, gF):
fType = searchExe.api.TF.features[fName].dataType
if fType != "int":
wrongTypes.setdefault(fName, {}).setdefault(
fType, set()
).add(e)
else:
match = fOneRe.findall(op)
if len(match):
opNameF = ".f."
fF = match[0]
addRels.setdefault(opNameF, set()).add(((f, fF), (t, fF)))
if not missingFeatures and not wrongValues:
add_F_Relations(searchExe, addRels)
# edge relations may have a value spec in them
# make an entry in the relation map for each value spec
addRels = {}
for (e, (f, op, t)) in enumerate(searchExe.qedgesRaw):
if type(op) is not tuple:
continue
(opName, opFeatures) = op
for eName in sorted(opFeatures):
_validateFeature(
searchExe,
e,
eName,
opFeatures,
missingFeatures,
wrongValues,
hasValues,
asEdge=True,
)
addRels.setdefault(opName, set()).add((eName, opFeatures[eName]))
if not missingFeatures and not wrongValues:
add_V_Relations(searchExe, addRels)
# now look up each particalur relation in the relation map
for (e, (f, op, t)) in enumerate(searchExe.qedgesRaw):
theOp = op[0] if type(op) is tuple else op
rela = relationFromName.get(theOp, None)
if rela is None:
searchExe.badSemantics.append((edgeLine[e], f'Unknown relation: "{theOp}"'))
edgesGood = False
qedges.append((f, rela, t))
if not edgesGood:
searchExe.badSemantics.append(
(None, f"Allowed relations:\n{searchExe.relationLegend}")
)
good = False
# report error found above
if len(missingFeatures):
for (fName, qs) in sorted(missingFeatures.items()):
searchExe.badSemantics.append(
(
None,
'Missing feature "{}" in line(s) {}'.format(
fName, ", ".join(str(nodeLine[q] + offset) for q in qs),
),
)
)
good = False
if len(hasValues):
for (fName, wrongs) in sorted(hasValues.items()):
searchExe.badSemantics.append(
(None, f'Feature "{fName}" cannot have values:')
)
for (val, qs) in sorted(wrongs.items()):
searchExe.badSemantics.append(
(
None,
' "{}" superfluous: line(s) {}'.format(
val, ", ".join(str(nodeLine[q] + offset) for q in qs),
),
)
)
good = False
if len(wrongValues):
for (fName, wrongs) in sorted(wrongValues.items()):
searchExe.badSemantics.append(
(None, f'Feature "{fName}" has wrong values:')
)
for (val, qs) in sorted(wrongs.items()):
searchExe.badSemantics.append(
(
None,
' "{}" is not a number: line(s) {}'.format(
val, ", ".join(str(nodeLine[q] + offset) for q in qs),
),
)
)
good = False
if len(wrongTypes):
for (fName, wrongs) in sorted(wrongTypes.items()):
searchExe.badSemantics.append((None, f'Feature "{fName}" has wrong type:'))
for (val, qs) in sorted(wrongs.items()):
searchExe.badSemantics.append(
(
None,
' "{}" is the wrong type: line(s) {}'.format(
val, ", ".join(str(nodeLine[q] + offset) for q in qs),
),
)
)
good = False
searchExe.qedges = qedges
# determine which node and edge features are not yet loaded,
# and load them
eFeatsUsed = set()
for (f, rela, t) in qedges:
efName = edgeMap.get(rela, (None,))[0]
if efName is not None:
eFeatsUsed.add(efName)
nFeatsUsed = set()
for (n, qdata) in enumerate(qnodes):
features = qdata[1]
for nfName in features:
nFeatsUsed.add(nfName)
if n in nodeMap:
nFeatsUsed |= nodeMap[n]
if good:
searchExe.api.ensureLoaded(eFeatsUsed | nFeatsUsed)
else:
searchExe.good = False