/
syntax.py
678 lines (598 loc) · 21.9 KB
/
syntax.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
"""
# Syntax of search templates
"""
import re
# SYNTACTIC ANALYSIS OF SEARCH TEMPLATE ###
QWHERE = "/where/"
QHAVE = "/have/"
QWITHOUT = "/without/"
QWITH = "/with/"
QOR = "/or/"
QEND = "/-/"
QINIT = {QWHERE, QWITHOUT, QWITH}
QCONT = {QHAVE, QOR}
QTERM = {QEND}
PARENT_REF = ".."
ESCAPES = (
"\\\\",
"\\ ",
"\\t",
"\\n",
"\\|",
"\\=",
)
VAL_ESCAPES = {
"\\|",
"\\=",
}
opPat = r"(?:[.#&|\[\]<>:=-]+\S*)"
atomOpPat = r"(\s*)({op})\s+([^ \t=#<>~*]+)(?:(?:\s*\Z)|(?:\s+(.*)))$".format(op=opPat)
atomPat = r"(\s*)([^ \t=#<>~*]+)(?:(?:\s*\Z)|(?:\s+(.*)))$"
compPat = r"^([a-zA-Z0-9-@_]+)([<>])(.*)$"
identPat = r"^([a-zA-Z0-9-@_]+)([=#])(.+)$"
indentLinePat = r"^(\s*)(.*)"
kPat = r"^([^0-9]*)([0-9]+)([^0-9]+)$"
namePat = r"[A-Za-z0-9_.-]+"
namesPat = r"^\s*(?:{op}\s+)?([^ \t:=#<>~*]+):"
nonePat = r"^([a-zA-Z0-9-@_]+)(#?)\s*$"
truePat = r"^([a-zA-Z0-9-@_]+)[*]\s*$"
numPat = r"^-?[0-9]+$"
opLinePat = r"^(\s*)({op})\s*$".format(op=opPat)
opStripPat = r"^\s*{op}\s+(.*)$".format(op=opPat)
quPat = f"(?:{QWHERE}|{QHAVE}|{QWITHOUT}|{QWITH}|{QOR}|{QEND})"
quLinePat = r"^(\s*)({qu})\s*$".format(qu=quPat)
relPat = r"^(\s*)({nm})\s+({op})\s+({nm})\s*$".format(nm=namePat, op=opPat)
rePat = r"^([a-zA-Z0-9-@_]+)~(.*)$"
atomOpRe = re.compile(atomOpPat)
atomRe = re.compile(atomPat)
compRe = re.compile(compPat)
identRe = re.compile(identPat)
indentLineRe = re.compile(indentLinePat)
kRe = re.compile(kPat)
nameRe = re.compile(f"^{namePat}$")
namesRe = re.compile(namesPat)
numRe = re.compile(numPat)
noneRe = re.compile(nonePat)
trueRe = re.compile(truePat)
opLineRe = re.compile(opLinePat)
opStripRe = re.compile(opStripPat)
quLineRe = re.compile(quLinePat)
relRe = re.compile(relPat)
reRe = re.compile(rePat)
whiteRe = re.compile(r"^\s*(%|$)")
reTp = type(reRe)
def syntax(searchExe):
error = searchExe.api.TF.error
_msgCache = searchExe._msgCache
searchExe.good = True
searchExe.badSyntax = []
searchExe.searchLines = searchExe.searchTemplate.split("\n")
offset = searchExe.offset
_tokenize(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.badSyntax:
txt = eline if ln is None else f"line {ln + offset}: {eline}"
error(txt, tm=False, cache=_msgCache)
def _tokenize(searchExe):
tokens = []
def lastAtomToken():
for token in reversed(tokens):
kind = token["kind"]
if kind == "feat":
continue
if kind == "atom" and "otype" in token:
return token
return None
return None
def readFeatures(x, i):
features = {}
featureString = x.replace("\\ ", chr(1)) if x is not None else ""
featureList = featureString.split()
good = True
for featStr in featureList:
if not parseFeatureVals(searchExe, featStr, features, i):
good = False
return features if good else None
searchLines = searchExe.searchLines
allGood = True
# the template may contain nested quantifiers
# However, we detect only the outer level of quantifiers.
# Everything contained in a quantifiers is collected in
# a new search template, verbatim, without interpretion,
# because it will be fed to search() on another instance.
# We only strip the quantified lines of the outermost quantifiers.
# We can maintain the current quantifier, None if there is none.
# We also remember the current indentation of the current quantifier
# We collect the templates within the quantifier in a list of strings.
# We add all the material into a quantifier token of the shape
#
# Because indentation is not indicative of quantifier nesting
# we need to maintain a stack of inner quantifiers,
# just to be able to determine wich quantifier words
# belong to the outerlevel quantifiers.
curQu = []
curQuTemplates = None
for (i, line) in enumerate(searchLines):
if whiteRe.match(line):
continue
opFeatures = {}
# first check whether we have a line with a quantifier
# and what the indent on the line is
match = quLineRe.match(line)
if match:
(indent, lineQuKind) = match.groups()
else:
lineQuKind = None
match = indentLineRe.match(line)
indent = match.group(1)
lineIndent = len(indent)
# QUANTIFIER FILTERING
#
# now check whether we are in a quantifier or not
# and determine whether a quantifier starts or ends here
# we have the following possible situations:
#
# UUO no outer - no q-keyword
#
# UBO no outer - q-keyword
# * ES no start keyword
# * ET no preceding token
# * EA no preceding atom
# * EI preceding atom not the same indentation
#
# PBI outer - q-keyword init
#
# PPO outer - no q-keyword
#
# PPI inner - no q-keyword
#
# PCO outer - q-keyword continue
# * EP wrong precursor
# * EK preceding keyword not the same indentation
#
# PCI inner - q-keyword continue
# * EP wrong precursor
# * EK preceding keyword not the same indentation
#
# PEO outer - q-keyword end
# * EP wrong precursor
# * EK preceding keyword not the same indentation
#
# PEI inner - q-keyword end
# * EP wrong precursor
# * EK preceding keyword not the same indentation
#
# at the end we may have a non-empty quantifier stack:
# * generate an unterminated quantifier error for each member
# of the stack
# first we determine what is the case and we store it in booleans
curQuLine = None
curQuKind = None
curQuIndent = None
curQuDepth = len(curQu)
if curQuDepth:
(curQuLine, curQuKind, curQuIndent) = curQu[-1]
UUO = not curQuDepth and not lineQuKind
UBO = not curQuDepth and lineQuKind
PBI = curQuDepth and lineQuKind in QINIT
PPO = curQuDepth == 1 and not lineQuKind
PPI = curQuDepth > 1 and not lineQuKind
PCO = curQuDepth == 1 and lineQuKind in QCONT
PCI = curQuDepth > 1 and lineQuKind in QCONT
PEO = curQuDepth == 1 and lineQuKind in QTERM
PEI = curQuDepth > 1 and lineQuKind in QTERM
(ES, ET, EA, EI, EP, EK) = (False,) * 6
if UBO:
ES = lineQuKind not in QINIT
ET = len(tokens) == 0
lastAtom = lastAtomToken()
EA = len(tokens) and not lastAtom
# EA = len(tokens) and not lastAtomToken
EI = len(tokens) and lastAtom and lastAtom["indent"] != lineIndent
# EA = (len(tokens) and tokens[-1]['kind'] != 'atom' and 'otype' not in tokens[-1])
# EI = (len(tokens) and tokens[-1]['indent'] != lineIndent)
if PCO or PCI:
EP = (lineQuKind == QHAVE and curQuKind != QWHERE) or (
lineQuKind == QOR and curQuKind not in {QWITH, QOR}
)
EK = curQu[-1][2] != lineIndent
if PEO or PEI:
EP = curQuKind in {QWHERE}
EK = curQu[-1][2] != lineIndent
# QUANTIFIER HANDLING
#
# Based on what is the case, we take actions.
# * we swallow quantified templates
# * we handle quantifier lines
# * we let all other lines pass through
good = True
for x in [True]:
if UUO:
# no quantifier business
continue
if UBO:
# start new quantifier from nothing
if ES:
searchExe.badSyntax.append(
(i, f'Quantifier: Can not start with "{lineQuKind}:"')
)
good = False
if ET:
searchExe.badSyntax.append((i, "Quantifier: No preceding tokens"))
good = False
if EA or EI:
searchExe.badSyntax.append(
(
i,
"Quantifier: Does not immediately follow an atom at the same level",
)
)
good = False
if not good:
continue
# prevAtom = tokens[-1]
prevAtom = lastAtomToken()
curQu.append((i, lineQuKind, lineIndent))
curQuTemplates = [[]]
quantifiers = prevAtom.setdefault("quantifiers", [])
quantifiers.append((lineQuKind, curQuTemplates, i))
continue
if PBI:
# start inner quantifier
# lines are passed with stripped indentation
# based on the outermost quantifier level
outerIndent = curQu[0][2]
strippedLine = line[outerIndent:]
curQuTemplates[-1].append(strippedLine)
curQu.append((i, lineQuKind, lineIndent))
if PPO:
# inside an outer quantifier
# lines are passed with stripped indentation
strippedLine = line[curQuIndent:]
curQuTemplates[-1].append(strippedLine)
continue
if PPI:
# inside an inner quantifier
# lines are passed with stripped indentation
# based on the outermost quantifier level
outerIndent = curQu[0][2]
strippedLine = line[outerIndent:]
curQuTemplates[-1].append(strippedLine)
if PCO or PCI:
if EP:
searchExe.badSyntax.append(
(
i,
f'Quantifier: "{lineQuKind}" can not follow "{curQuKind}" on line {curQuLine}',
)
)
good = False
if EK:
searchExe.badSyntax.append(
(
i,
(
f'Quantifier "{lineQuKind}"'
f' has not same indentation as "{curQuKind}" on line {curQuLine}'
),
)
)
good = False
if PCO:
curQuTemplates.append([])
else:
outerIndent = curQu[0][2]
strippedLine = line[outerIndent:]
curQuTemplates[-1].append(strippedLine)
curQu[-1] = (i, lineQuKind, lineIndent)
continue
if PEO or PEI:
if EP:
searchExe.badSyntax.append(
(
i,
(
f'Quantifier: "{lineQuKind}"'
f' : premature end of "{curQuKind}" on line {curQuLine}'
),
)
)
good = False
if EK:
searchExe.badSyntax.append(
(
i,
(
f'Quantifier "{lineQuKind}"'
f' has not same indentation as "{curQuKind}" on line {curQuLine}'
),
)
)
good = False
if PEO:
curQuTemplates = None
else:
outerIndent = curQu[0][2]
strippedLine = line[outerIndent:]
curQuTemplates[-1].append(strippedLine)
curQu.pop()
continue
if not good:
allGood = False
if UUO:
# go on with normal template tokenization
pass
else:
# quantifiers stuff has been dealt with
continue
# QUANTIFIER FREE HANDLING
good = False
for x in [True]:
(kind, data) = parseLine(line)
if kind == "op":
(indent, op) = data
if not parseFeatureVals(searchExe, op, opFeatures, i, asEdge=True):
good = False
else:
if opFeatures:
op = (op, opFeatures)
tokens.append(dict(ln=i, kind="atom", indent=len(indent), op=op))
good = True
break
if kind == "rel":
(indent, f, op, t) = data
if not parseFeatureVals(searchExe, op, opFeatures, i, asEdge=True):
good = False
else:
if opFeatures:
op = (op, opFeatures)
tokens.append(dict(ln=i, kind="rel", f=f, op=op, t=t))
good = True
break
if kind == "atom":
(indent, op, name, otype, features) = data
good = True
if name != "":
mt = nameRe.match(name)
if not mt:
searchExe.badSyntax.append((i, f'Illegal name: "{name}"'))
good = False
features = readFeatures(features, i)
if features is None:
good = False
else:
if op is not None:
if not parseFeatureVals(
searchExe, op, opFeatures, i, asEdge=True
):
good = False
if good:
if opFeatures:
op = (op, opFeatures)
tokens.append(
dict(
ln=i,
kind="atom",
indent=len(indent),
op=op,
name=name,
otype=otype,
src=line.lstrip(),
features=features,
)
)
break
if kind == "feat":
features = data[0]
features = readFeatures(features, i)
if features is None:
good = False
else:
tokens.append(dict(ln=i, kind="feat", features=features))
good = True
break
good = False
searchExe.badSyntax.append((i, f"Unrecognized line: {line}"))
if not good:
allGood = False
if curQu:
for (curQuLine, curQuKind, curQuIndent) in curQu:
searchExe.badSyntax.append(
(curQuLine, f'Quantifier: Unterminated "{curQuKind}"')
)
good = False
allGood = False
if allGood:
searchExe.tokens = tokens
else:
searchExe.good = False
def parseLine(line):
for x in [True]:
escLine = _esc(line)
match = opLineRe.match(escLine)
if match:
(indent, op) = match.groups()
kind = "op"
data = (indent, op)
break
match = relRe.match(escLine)
if match:
(indent, f, op, t) = match.groups()
kind = "rel"
data = (indent, f, op, t)
break
matchOp = atomOpRe.match(escLine)
if matchOp:
(indent, op, atom, features) = matchOp.groups()
else:
match = atomRe.match(escLine)
if match:
op = None
(indent, atom, features) = match.groups()
if matchOp or match:
atomComps = atom.split(":", 1)
if len(atomComps) == 1:
name = ""
otype = atomComps[0]
else:
name = atomComps[0]
otype = atomComps[1]
kind = "atom"
if features is None:
features = ""
data = (indent, op, name, otype, features)
break
kind = "feat"
data = (escLine,)
return (kind, data)
def parseFeatureVals(searchExe, featStr, features, i, asEdge=False):
if asEdge:
if not (
(featStr[0] == "-" and featStr[-1] == ">")
or (featStr[0] == "<" and featStr[-1] == "-")
or (featStr[0] == "<" and featStr[-1] == ">")
):
return True
feat = featStr[1:-1]
else:
feat = featStr.replace(chr(1), " ")
good = True
for x in [True]:
match = trueRe.match(feat)
if match:
(featN,) = match.groups()
featName = _unesc(featN)
featVals = (None, True)
break
match = noneRe.match(feat)
if match:
(featN, unequal) = match.groups()
featName = _unesc(featN)
featVals = None if unequal else True
break
match = identRe.match(feat)
if match:
(featN, comp, featValStr) = match.groups()
featName = _unesc(featN)
featValSet = frozenset(_unesc(featVal) for featVal in featValStr.split("|"))
featVals = (comp == "=", featValSet)
break
match = compRe.match(feat)
if match:
(featN, comp, limit) = match.groups()
featName = _unesc(featN)
if not numRe.match(limit):
searchExe.badSyntax.append((i, f'Limit is non numeric "{limit}"'))
good = False
featVals = None
else:
featVals = _makeLimit(int(limit), comp == ">")
break
match = reRe.match(feat)
if match:
(featN, valRe) = match.groups()
featName = _unesc(featN)
valRe = _unesc(valRe, inRe=True)
try:
featVals = re.compile(valRe)
except Exception() as err:
searchExe.badSyntax.append(
(i, f'Wrong regular expression "{valRe}": "{err}"')
)
good = False
featVals = None
break
searchExe.badSyntax.append((i, f'Unrecognized feature condition "{feat}"'))
good = False
featVals = None
if good:
features[featName] = featVals
return good
def _genLine(kind, data):
result = None
for x in [True]:
if kind == "op":
(indent, op) = data
result = f"{indent}{_unesc(op)}"
break
if kind == "rel":
(indent, f, op, t) = data
result = f"{indent}{f} {_unesc(op)} {t}"
break
if kind == "atom":
(indent, op, name, otype, features) = data
opRep = "" if op is None else f"{_unesc(op)} "
nameRep = "" if name == "" else f"{name}:"
featRep = _unesc(features)
if featRep:
featRep = f" {featRep}"
result = f"{indent}{opRep}{nameRep}{otype}{featRep}"
break
features = data[0]
result = _unesc(features)
return result
def cleanParent(atom, parentName):
(kind, data) = parseLine(atom)
(indent, op, name, otype, features) = data
if name == "":
name = parentName
return _genLine(kind, (indent, None, name, otype, features))
def deContext(quantifier, parentName):
(quKind, quTemplates, ln) = quantifier
# choose a name for the parent
# either the given name
if not parentName:
# or make a new name
# collect all used names
# to avoid choosing a name that is already used
usedNames = set()
for template in quTemplates:
for line in template:
for name in namesRe.findall(line):
usedNames.add(name)
parentName = "parent"
while parentName in usedNames:
parentName += "x"
newQuTemplates = []
newQuantifier = (quKind, newQuTemplates, parentName, ln)
# replace .. (PARENT_REF) by parentName
# wherever it is applicable
for template in quTemplates:
newLines = []
for line in template:
(kind, data) = parseLine(line)
newLine = line
if kind == "rel":
(indent, f, op, t) = data
if f == PARENT_REF or t == PARENT_REF:
newF = parentName if f == PARENT_REF else f
newT = parentName if t == PARENT_REF else t
newData = (indent, newF, op, newT)
newLine = _genLine(kind, newData)
elif kind == "atom":
(indent, op, name, otype, features) = data
if name == "" and otype == PARENT_REF:
newData = (indent, op, name, parentName, features)
newLine = _genLine(kind, newData)
newLines.append(newLine)
templateStr = "\n".join(newLines)
newQuTemplates.append(templateStr)
return newQuantifier
def _makeLimit(n, isLower):
if isLower:
return lambda x: x is not None and x > n
return lambda x: x is not None and x < n
def _esc(x):
for (i, c) in enumerate(ESCAPES):
x = x.replace(c, chr(i))
return x
def _unesc(x, inRe=False):
for (i, c) in enumerate(ESCAPES):
if inRe and c in VAL_ESCAPES:
x = x.replace(chr(i), f"\\{c[1]}")
else:
x = x.replace(chr(i), c[1])
return x