This repository has been archived by the owner on Jan 28, 2019. It is now read-only.
/
parser.clj
196 lines (164 loc) · 6.05 KB
/
parser.clj
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
(ns shelving.query.parser
"Implementation details of the shelving query parser."
{:authors ["Reid \"arrdem\" McKenzie <me@arrdem.com>"],
:license "Eclipse Public License 1.0",
:added "0.0.0"}
(:require [clojure.spec.alpha :as s]
[clojure.core.specs.alpha :as cs]
[clojure.test.check.generators :as gen]
[shelving.query.common :refer [lvar?]]))
(s/def ::lvar
(s/with-gen
lvar?
#(gen/fmap (fn [s]
(symbol (str "?" s)))
(s/gen simple-symbol?))))
;; More stupid due to CLJ-2003
(s/def ::lvar+spec?
(s/and (s/or :simple
::lvar
:spec+lvar+coll
(s/cat :from #{:from}
:spec qualified-keyword?
:lvar ::lvar
:coll? #{'...})
:spec+lvar
(s/cat :from #{:from}
:spec qualified-keyword?
:lvar ::lvar)
:lvar+coll
(s/cat :lvar ::lvar
:coll? #{'...})
:lvar
(s/cat :lvar ::lvar))
(s/conformer
(fn [o]
(if (= o ::s/invalid) o
(let [[tag e] o]
(if (= tag :simple)
{:lvar e} e))))
(fn [{:keys [spec lvar coll?]}]
(cond (and spec lvar coll?) [:spec+lvar+coll {:from :from :spec spec :lvar lvar :coll? '...}]
(and spec lvar) [:spec+lvar {:from :from :spec spec :lvar lvar}]
(and lvar coll?) [:lvar+coll {:lvar lvar :coll? '...}]
lvar [:simple lvar])))))
(s/def ::lvars
(s/alt :inline (s/+ ::lvar+spec?)
:wrapped (s/coll-of ::lvar+spec? :into [])))
(s/def ::full-tuple
(s/tuple ::lvar :shelving.core/rel-id some?))
(s/def ::terse-tuple
(s/tuple ::lvar qualified-keyword? some?))
(s/def ::tuple
(s/or :explicit-rel ::full-tuple
:inferred-rel ::terse-tuple))
(s/def ::negation
(s/and (s/cat :not #{:not}
:clause ::clause)
(s/conformer
(fn [{:keys [clause] :as o}]
(if (= ::s/invalid o) o clause))
(fn [clause]
{:not :not
:clause clause}))))
(s/def ::guard
(s/and (s/cat :guard #{:guard}
:fn (s/with-gen any? ;; FIXME lolsob
#(s/gen #{'clojure.core/even? 'clojure.core/odd?}))
:lvars (s/* ::lvar))
(s/conformer
(fn [{:keys [fn lvars] :as o}]
(if (= ::s/invalid o) o
(cons fn lvars)))
(fn [[fn & lvars]]
{:guard :guard
:fn fn
:lvars lvars}))))
(s/def ::clause
(s/or :tuple ::tuple
#_:guard #_::guard
#_:negation #_::negation))
(s/def ::clauses
(s/alt :wrapped (s/coll-of ::clause :into [])
:inline (s/* ::clause)))
;; seq-style datalog
;;
;; Note that these apparently can't use conformers to simplify because they all inhabit the same seq
;; of inputs? Not sure how spec is handling the regex walk.
(s/def :shelving.query.parser.seq/find
(s/cat :find #{:find}
:symbols ::lvars))
(s/def :shelving.query.parser.seq/in
(s/cat :in #{:in}
:parameters ::lvars))
(s/def :shelving.query.parser.seq/where
(s/cat :where #{:where}
:rels ::clauses))
(defn- conform-datalog-seq
"Provides a datalog front end to the Shelving query system."
[o]
(if (= ::s/invalid o) o
(let [[tag v] o
{{find :symbols} :find
{where :rels} :where
{in :parameters} :in} v]
{:find (or (second find) [])
:where (or (second where) [])
:in (or (second in) [])})))
(defn- unform-datalog-seq
[{:keys [find in where]}]
[(cond (and find in where)
:find+in+where
(and find in (not where))
:find+in
(and find (not in) where)
:find+where
:else :find)
{:find {:find :find, :symbols (vector :wrapped find)}
:in {:in :in, :parameters (vector :wrapped in)}
:where {:where :where, :rels (vector :wrapped where)}}])
(s/def :shelving.query.parser.seq/datalog
;; workaround to https://dev.clojure.org/jira/browse/CLJ-2003
;; just write out the possibilities as an s/alt which will unform
(s/and (s/alt :find+in+where
(s/cat :find :shelving.query.parser.seq/find
:in :shelving.query.parser.seq/in
:where :shelving.query.parser.seq/where)
:find+in
(s/cat :find :shelving.query.parser.seq/find
:in :shelving.query.parser.seq/in)
:find+where
(s/cat :find :shelving.query.parser.seq/find
:where :shelving.query.parser.seq/where)
:find
(s/cat :find :shelving.query.parser.seq/find))
(s/conformer conform-datalog-seq
unform-datalog-seq)))
;; map-style datalog
(s/def :shelving.query.parser.map/find
(s/coll-of ::lvar+spec? :into []))
(s/def :shelving.query.parser.map/in
(s/coll-of ::lvar+spec? :into []))
(s/def :shelving.query.parser.map/where
(s/coll-of ::clause :into []))
(defn- conform-datalog-map [o]
(if (= o ::s/invalid) o
(let [{:keys [find in where]} o]
{:find (or find [])
:in (or in [])
:where (or where [])})))
(defn- unform-datalog-map [{:keys [find in where] :as v}] v)
(s/def :shelving.query.parser.map/datalog
(s/and (s/keys :req-un [:shelving.query.parser.map/find]
:opt-un [:shelving.query.parser.map/in
:shelving.query.parser.map/where])
(s/conformer conform-datalog-map
unform-datalog-map)))
;; expose both styles as ::datalog
(s/def ::datalog
(s/and (s/or :seq :shelving.query.parser.seq/datalog
:map :shelving.query.parser.map/datalog)
(s/conformer (fn [o]
(if (= ::s/invalid o) o
(second o)))
(fn [v] [:seq v]))))