-
Notifications
You must be signed in to change notification settings - Fork 10
/
unparser.cljc
180 lines (145 loc) · 4.01 KB
/
unparser.cljc
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
(ns datalog.unparser
(:require [datalog.parser :as parser]
#?(:cljs
[datalog.parser.type :refer
[Aggregate And BindColl BindIgnore BindScalar BindTuple Constant
DefaultSrc FindColl FindRel FindScalar FindTuple Function Not Or
Pattern Placeholder PlainSymbol Predicate Pull Query Rule
RuleBranch RuleExpr RulesVar RuleVars SrcVar Variable]]))
#?(:clj
(:import [datalog.parser.type
Aggregate And BindColl BindIgnore BindScalar BindTuple Constant
DefaultSrc FindColl FindRel FindScalar FindTuple Function Not Or
Pattern Placeholder PlainSymbol Predicate Pull Query Rule
RuleBranch RuleExpr RulesVar RuleVars SrcVar Variable])))
#?(:clj (set! *warn-on-reflection* true))
(defprotocol PUnparse
(-unparse [this]))
(defn unparse
"Unparsing of datalog parser records back to the query DSL datastructure.
Specifically it is the inverse to datalog.parser/parse-query."
[v]
(-unparse v))
;; ===== Missing Pull records
;; PullSpec
;; PullAttrName
;; PullReverseAttrName
;; PullLimitExpr
;; PullDefaultExpr
;; PullWildcard
;; PullRecursionLimit
;; PullMapSpecEntry
;; PullAttrWithOpts
(extend-protocol PUnparse
Aggregate
(-unparse [{:keys [fn args]}]
(conj (map -unparse args)
(-unparse fn)))
;; TODO test
And
(-unparse [{:keys [clauses]}]
(concat (list 'and)
(map -unparse clauses)))
BindColl
(-unparse [bc]
[(-unparse (:binding bc)) '...])
;; TODO test
BindIgnore
(-unparse [v] '_)
BindScalar
(-unparse [v]
(-unparse (:variable v)))
BindTuple
(-unparse [bt]
(mapv -unparse (:bindings bt)))
Constant
(-unparse [c] (:value c))
;; TODO test
DefaultSrc
(-unparse [s])
;; TODO test
FindColl
(-unparse [{:keys [element]}]
[[(-unparse element) '...]])
FindRel
(-unparse [fr]
(map -unparse (:elements fr)))
FindScalar
(-unparse [s]
[(-unparse (:element s)) '.])
;; TODO test
FindTuple
(-unparse [{:keys [elements]}]
[(-unparse elements)])
Function
(-unparse [f]
[(concat [(-unparse (:fn f))]
(map -unparse (:args f)))
(-unparse (:binding f))])
;; TODO test, check not-join
Not
(-unparse [{:keys [source vars clauses]}]
(concat (if-let [src (-unparse source)]
(list src 'not)
(list 'not))
(map -unparse clauses)))
;; TODO test, check or-join
Or
(-unparse [{:keys [source rule-vars clauses]}]
(concat (list 'or)
(map -unparse clauses)))
Pattern
(-unparse [{:keys [source pattern] :as p}]
(concat [(-unparse source)] (map -unparse pattern)))
;; TODO test
Placeholder
(-unparse [_] '_)
PlainSymbol
(-unparse [s] (:symbol s))
Predicate
(-unparse [{:keys [fn args] :as p}]
[(conj (map -unparse args)
(-unparse fn))])
;; TODO test
Pull
(-unparse [{:keys [source variable pattern]}]
(list 'pull
(-unparse source) ;; TODO sugar remove $
(-unparse variable)
(-unparse pattern)))
Query
(-unparse [{:keys [qfind qwith qin qwhere]}]
(vec
(concat
[:find] (-unparse qfind)
(when-not (empty? qwith)
(conj (map -unparse qwith) :with))
(conj (map -unparse qin) :in)
[:where]
(mapv (comp vec -unparse) qwhere))))
;; TODO test
Rule
(-unparse [{:keys [name branches]}]
(concat (list (-unparse name))
(map -unparse branches)))
;; TODO test
RuleBranch
(-unparse [{:keys [vars clauses]}]
(mapv -unparse clauses))
;; TODO test
RuleExpr
(-unparse [{:keys [source name args]}]
[(-unparse source) (concat (list (-unparse name))
(map -unparse args))])
;; TODO test
RulesVar
(-unparse [s] '%)
;; TODO implement and test
RuleVars
(-unparse [{:keys [required free] :as v}]
(throw (ex-info "Rule Vars not supported yet." {:v v})))
SrcVar
(-unparse [s] (:symbol s))
Variable
(-unparse [v]
(:symbol v)))