aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Peter McGoron 2021-07-26 14:10:15 -0400
committerGravatar - 2021-07-26 22:55:59 -0400
commitf963aaeddbfe248a7b4a5f45a9c72a6cadb7bfe3 (patch)
tree5d051f1ea89049a551b88bad814ca914f9833c91
parentgitea wants .md suffixes for web rendering (diff)
queryparse: collect literals into set and return it
-rw-r--r--queryparse.py28
-rwxr-xr-xtest/t_queryparse.py10
2 files changed, 22 insertions, 16 deletions
diff --git a/queryparse.py b/queryparse.py
index 94d1451..9bab83e 100644
--- a/queryparse.py
+++ b/queryparse.py
@@ -69,45 +69,46 @@ e4 ::= TAG | "[" e1 "]";
class ParseError(Exception):
pass
-def e4(l):
+def e4(lits, l):
typ,s = classify_head(l)
if typ == TokType.OPEN:
- pt,l = e1(l[1:])
+ pt,l = e1(lits, l[1:])
typ,_ = classify_head(l)
if typ != TokType.CLOSE:
raise ParseError
return pt,l[1:]
elif typ in LitType:
+ lits.add((typ, s))
return Atom(typ, s), l[1:]
else:
raise ParseError
-def e3(l):
+def e3(lits, l):
typ,_ = classify_head(l)
if typ == TokType.NOT:
- pt,l = e4(l[1:])
+ pt,l = e4(lits, l[1:])
return encapsulate(pt, TokType.NOT), l
- return e4(l)
+ return e4(lits, l)
-def e2(l):
- pt,l = e3(l)
+def e2(lits, l):
+ pt,l = e3(lits, l)
try:
while True:
- pt2,l = e3(l)
+ pt2,l = e3(lits, l)
pt = encapsulate(pt, TokType.AND)
pt.push(pt2)
except ParseError:
pass
return pt,l
-def e1(l):
- pt,l = e2(l)
+def e1(lits, l):
+ pt,l = e2(lits, l)
typ,_ = classify_head(l)
try:
while typ == TokType.OR:
- pt2,l = e2(l[1:])
+ pt2,l = e2(lits, l[1:])
pt = encapsulate(pt, TokType.OR)
pt.push(pt2)
typ,_ = classify_head(l)
@@ -116,7 +117,8 @@ def e1(l):
return pt,l
def parse(l):
- x,l = e1(l)
+ lits = set()
+ x,l = e1(lits, l)
if l != []:
raise ParseError
- return x
+ return x,lits
diff --git a/test/t_queryparse.py b/test/t_queryparse.py
index ac9cf24..df21639 100755
--- a/test/t_queryparse.py
+++ b/test/t_queryparse.py
@@ -5,12 +5,16 @@ import queryparse
def f(x):
print(x)
- print(queryparse.parse(x))
+ v,lits = queryparse.parse(x)
+ print(v)
+ print(lits)
+ print()
f(["x", "OR", "y"])
-f(["x", "y", "OR", "a", "b"])
+f(["x", "y", "OR", "x", "b"])
f(["x", "y", "z", "w"])
f(["NOT", "x", "OR", "y"])
f(["NOT", "[", "x", "y", "z", "]"])
-f(["x", "y", "NOT", "z", "[", "a", "OR", "b", "]"])
+f(["x", "y", "NOT", "z", "[", "a", "OR", "b", "]", "c"])
+f(["x", "y", "z", "OR", "NOT", "x", "a", "z"])
f(["~~internal-*", "~-Application*", "NOT", "~~*xyz*"])