/
spec.cljs
1220 lines (1094 loc) · 43.3 KB
/
spec.cljs
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns cljs.spec
(:refer-clojure :exclude [+ * and or cat def keys])
(:require-macros [cljs.core :as c]
[cljs.spec :as s])
(:require [goog.object :as gobj]
[cljs.core :as c]
[clojure.walk :as walk]
[cljs.spec.impl.gen :as gen]
[clojure.string :as str]))
(def ^:const MAX_INT 9007199254740991)
(def ^:dynamic *recursion-limit*
"A soft limit on how many times a branching spec (or/alt/*/opt-keys/multi-spec)
can be recursed through during generation. After this a
non-recursive branch will be chosen."
4)
(def ^:dynamic *fspec-iterations*
"The number of times an anonymous fn specified by fspec will be (generatively) tested during conform"
21)
(def ^:dynamic *coll-check-limit*
"The number of items validated in a collection spec'ed with 'every'"
101)
(def ^:dynamic *coll-error-limit*
"The number of errors reported by explain in a collection spec'ed with 'every'"
20)
(def ^:private ^:dynamic *instrument-enabled*
"if false, instrumented fns call straight through"
true)
(defprotocol Spec
(conform* [spec x])
(unform* [spec y])
(explain* [spec path via in x])
(gen* [spec overrides path rmap])
(with-gen* [spec gfn])
(describe* [spec]))
(defonce ^:private registry-ref (atom {}))
(defn- named? [x] (implements? INamed x))
(defn- with-name [spec name]
(with-meta spec (assoc (meta spec) ::name name)))
(defn- spec-name [spec]
(cond
(keyword? spec) spec
(implements? IMeta spec)
(-> (meta spec) ::name)))
(defn- reg-resolve
"returns the spec/regex at end of alias chain starting with k, nil if not found, k if k not Named"
[k]
(if (named? k)
(let [reg @registry-ref]
(loop [spec k]
(if (named? spec)
(recur (get reg spec))
(when spec
(with-name spec k)))))
k))
(defn- reg-resolve!
"returns the spec/regex at end of alias chain starting with k, throws if not found, k if k not ident"
[k]
(if (ident? k)
(c/or (reg-resolve k)
(throw (js/Error. (str "Unable to resolve spec: " k))))
k))
(defn spec?
"returns x if x is a spec object, else logical false"
[x]
(c/and (implements? Spec x) x))
(defn regex?
"returns x if x is a (clojure.spec) regex op, else logical false"
[x]
(c/and (::op x) x))
(declare spec-impl)
(declare regex-spec-impl)
(defn- maybe-spec
"spec-or-k must be a spec, regex or resolvable kw/sym, else returns nil."
[spec-or-k]
(let [s (c/or (spec? spec-or-k)
(regex? spec-or-k)
(c/and (named? spec-or-k) (reg-resolve spec-or-k))
nil)]
(if (regex? s)
(with-name (regex-spec-impl s nil) (spec-name s))
s)))
(defn- the-spec
"spec-or-k must be a spec, regex or kw/sym, else returns nil. Throws if unresolvable kw/sym"
[spec-or-k]
(c/or (maybe-spec spec-or-k)
(when (named? spec-or-k)
(throw (js/Error. (str "Unable to resolve spec: " spec-or-k))))))
(defn- specize [s]
(c/or (the-spec s) (spec-impl ::unknown s nil nil)))
(defn conform
"Given a spec and a value, returns :clojure.spec/invalid if value does not match spec,
else the (possibly destructured) value."
[spec x]
(conform* (specize spec) x))
(defn unform
"Given a spec and a value created by or compliant with a call to
'conform' with the same spec, returns a value with all conform
destructuring undone."
[spec x]
(unform* (specize spec) x))
(defn form
"returns the spec as data"
[spec]
;;TODO - incorporate gens
(describe* (specize spec)))
(defn abbrev [form]
(cond
(seq? form)
(walk/postwalk (fn [form]
(cond
(c/and (symbol? form) (namespace form))
(-> form name symbol)
(c/and (seq? form) (= 'fn (first form)) (= '[%] (second form)))
(last form)
:else form))
form)
(c/and (symbol? form) (namespace form))
(-> form name symbol)
:else form))
(defn describe
"returns an abbreviated description of the spec as data"
[spec]
(abbrev (form spec)))
(defn with-gen
"Takes a spec and a no-arg, generator-returning fn and returns a version of that spec that uses that generator"
[spec gen-fn]
(with-gen* (specize spec) gen-fn))
(defn explain-data* [spec path via in x]
(when-let [probs (explain* (specize spec) path via in x)]
{::problems probs}))
(defn explain-data
"Given a spec and a value x which ought to conform, returns nil if x
conforms, else a map with at least the key ::problems whose value is
a path->problem-map, where problem-map has at least :pred and :val
keys describing the predicate and the value that failed at that
path."
[spec x]
(explain-data* spec [] (if-let [name (spec-name spec)] [name] []) [] x))
(defn explain-out
"prints an explanation to *out*."
[ed]
(if ed
(print
(with-out-str
;;(prn {:ed ed})
(doseq [[path {:keys [pred val reason via in] :as prob}] (::problems ed)]
(when-not (empty? in)
(print "In:" (pr-str in) ""))
(print "val: ")
(pr val)
(print " fails")
(when-not (empty? via)
(print " spec:" (pr-str (last via))))
(when-not (empty? path)
(print " at:" (pr-str path)))
(print " predicate: ")
(pr pred)
(when reason (print ", " reason))
(doseq [[k v] prob]
(when-not (#{:pred :val :reason :via :in} k)
(print "\n\t" (pr-str k) " ")
(pr v)))
(newline))
(doseq [[k v] ed]
(when-not (#{::problems} k)
(print (pr-str k) " ")
(pr v)
(newline)))))
(println "Success!")))
(defn explain
"Given a spec and a value that fails to conform, prints an explanation to *out*."
[spec x]
(explain-out (explain-data spec x)))
(defn explain-str
"Given a spec and a value that fails to conform, returns an explanation as a string."
[spec x]
(with-out-str (explain spec x)))
(declare valid?)
(defn- gensub
[spec overrides path rmap form]
;;(prn {:spec spec :over overrides :path path :form form})
(let [spec (c/or (get overrides spec) spec)
spec (specize spec)]
(if-let [g (c/or (get overrides path) (gen* spec overrides path rmap))]
(gen/such-that #(valid? spec %) g 100)
(throw (js/Error. (str "Unable to construct gen at: " path " for: " (abbrev form)))))))
(defn gen
"Given a spec, returns the generator for it, or throws if none can
be constructed. Optionally an overrides map can be provided which
should map spec names or paths (vectors of keywords) to
generators. These will be used instead of the generators at those
names/paths. Note that parent generator (in the spec or overrides
map) will supersede those of any subtrees. A generator for a regex
op must always return a sequential collection (i.e. a generator for
s/? should return either an empty sequence/vector or a
sequence/vector with one item in it)"
([spec] (gen spec nil))
([spec overrides] (gensub spec overrides [] {::recursion-limit *recursion-limit*} spec)))
(defn ^:skip-wiki def-impl
"Do not call this directly, use 'def'"
[k form spec]
(assert (c/and (named? k) (namespace k)) "k must be namespaced keyword or resolveable symbol")
(let [spec (if (c/or (spec? spec) (regex? spec) (get @registry-ref spec))
spec
(spec-impl form spec nil nil))]
(swap! registry-ref assoc k spec)
k))
(defn registry
"returns the registry map, prefer 'get-spec' to lookup a spec by name"
[]
@registry-ref)
(defn- ->sym
"Returns a symbol from a symbol or var"
[x]
(if (var? x)
(.-sym x)
x))
(defn get-spec
"Returns spec registered for keyword/symbol/var k, or nil."
[k]
(get (registry) (if (keyword? k) k (->sym k))))
(declare map-spec)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; instrument ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- expect
"Returns nil if v conforms to spec, else throws ex-info with explain-data."
[spec v]
)
(defn- fn-spec?
"Fn-spec must include at least :args or :ret specs."
[m]
(c/or (:args m) (:ret m)))
(defn- spec-checking-fn
[v f]
(let [conform! (fn [v role spec data args]
(let [conformed (conform spec data)]
(if (= ::invalid conformed)
(let [ed (assoc (explain-data* spec [role] [] [] data)
::args args)]
(throw (ex-info
(str "Call to " (pr-str v) " did not conform to spec:\n" (with-out-str (explain-out ed)))
ed)))
conformed)))]
(cond->
(c/fn
[& args]
(if *instrument-enabled*
(s/with-instrument-disabled
(let [specs (get-spec v)]
(when (:args specs) (conform! v :args (:args specs) args args))
(binding [*instrument-enabled* true]
(apply f args))))
(apply f args)))
(not (instance? MultiFn f)) (doto (gobj/extend f)))))
(defn- macroexpand-check
[v args]
(let [specs (get-spec v)]
(when-let [arg-spec (:args specs)]
(when (= ::invalid (conform arg-spec args))
(let [ed (assoc (explain-data* arg-spec [:args]
(if-let [name (spec-name arg-spec)] [name] []) [] args)
::args args)]
(throw (js/Error.
(str
"Call to " (->sym v) " did not conform to spec:\n"
(with-out-str (explain-out ed))))))))))
(defn- no-fn-spec
[v specs]
(ex-info (str "Fn at " (pr-str v) " is not spec'ed.")
{:var v :specs specs}))
(def ^:private instrumented-vars
"Map for instrumented vars to :raw/:wrapped fns"
(atom {}))
(defn instrument*
[v]
(let [spec (get-spec v)]
(if (fn-spec? spec)
(locking instrumented-vars
(let [{:keys [raw wrapped]} (get @instrumented-vars v)
current @v]
(when-not (= wrapped current)
(let [checked (spec-checking-fn v current)]
(swap! instrumented-vars assoc v {:raw current :wrapped checked})
checked))))
(throw (no-fn-spec v spec)))))
(defn unstrument*
[v]
(locking instrumented-vars
(when-let [{:keys [raw wrapped]} (get @instrumented-vars v)]
(let [current @v]
(when (= wrapped current)
(swap! instrumented-vars dissoc v)
raw)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; impl ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- recur-limit? [rmap id path k]
(c/and (> (get rmap id) (::recursion-limit rmap))
(contains? (set path) k)))
(defn- inck [m k]
(assoc m k (inc (c/or (get m k) 0))))
(defn- dt
([pred x form] (dt pred x form nil))
([pred x form cpred?]
(if pred
(if-let [spec (the-spec pred)]
(conform spec x)
(if (ifn? pred)
(if cpred?
(pred x)
(if (pred x) x ::invalid))
(throw (js/Error. (str (pr-str form) " is not a fn, expected predicate fn")))))
x)))
(defn valid?
"Helper function that returns true when x is valid for spec."
([spec x]
(not= ::invalid (dt spec x ::unknown)))
([spec x form]
(not= ::invalid (dt spec x form))))
(defn- explain-1 [form pred path via in v]
;;(prn {:form form :pred pred :path path :in in :v v})
(let [pred (maybe-spec pred)]
(if (spec? pred)
(explain* pred path (if-let [name (spec-name pred)] (conj via name) via) in v)
{path {:pred (abbrev form) :val v :via via :in in}})))
(defn ^:skip-wiki map-spec-impl
"Do not call this directly, use 'spec' with a map argument"
[{:keys [req-un opt-un pred-exprs opt-keys req-specs req req-keys opt-specs pred-forms opt gfn]
:as argm}]
(let [keys-pred (apply every-pred pred-exprs)
k->s (zipmap (concat req-keys opt-keys) (concat req-specs opt-specs))
keys->specs #(c/or (k->s %) %)
id (random-uuid)]
(reify
Spec
(conform* [_ m]
(if (keys-pred m)
(let [reg (registry)]
(loop [ret m, [k & ks :as keys] (c/keys m)]
(if keys
(if (contains? reg (keys->specs k))
(let [v (get m k)
cv (conform (keys->specs k) v)]
(if (= cv ::invalid)
::invalid
(recur (if (identical? cv v) ret (assoc ret k cv))
ks)))
(recur ret ks))
ret)))
::invalid))
(unform* [_ m]
(let [reg (registry)]
(loop [ret m, [k & ks :as keys] (c/keys m)]
(if keys
(if (contains? reg (keys->specs k))
(let [cv (get m k)
v (unform (keys->specs k) cv)]
(recur (if (identical? cv v) ret (assoc ret k v))
ks))
(recur ret ks))
ret))))
(explain* [_ path via in x]
(if-not (map? x)
{path {:pred 'map? :val x :via via :in in}}
(let [reg (registry)]
(apply merge
(when-let [probs (->> (map (fn [pred form] (when-not (pred x) (abbrev form)))
pred-exprs pred-forms)
(keep identity)
seq)]
{path {:pred (vec probs) :val x :via via :in in}})
(map (fn [[k v]]
(when-not (c/or (not (contains? reg (keys->specs k)))
(valid? (keys->specs k) v k))
(explain-1 (keys->specs k) (keys->specs k) (conj path k) via (conj in k) v)))
(seq x))))))
(gen* [_ overrides path rmap]
(if gfn
(gfn)
(let [rmap (inck rmap id)
gen (fn [k s] (gensub s overrides (conj path k) rmap k))
ogen (fn [k s]
(when-not (recur-limit? rmap id path k)
[k (gen/delay (gensub s overrides (conj path k) rmap k))]))
req-gens (map gen req-keys req-specs)
opt-gens (remove nil? (map ogen opt-keys opt-specs))]
(when (every? identity (concat req-gens opt-gens))
(let [reqs (zipmap req-keys req-gens)
opts (into {} opt-gens)]
(gen/bind (gen/choose 0 (count opts))
#(let [args (concat (seq reqs) (when (seq opts) (shuffle (seq opts))))]
(->> args
(take (c/+ % (count reqs)))
(apply concat)
(apply gen/hash-map)))))))))
(with-gen* [_ gfn] (map-spec-impl (assoc argm :gfn gfn)))
(describe* [_] (cons `keys
(cond-> []
req (conj :req req)
opt (conj :opt opt)
req-un (conj :req-un req-un)
opt-un (conj :opt-un opt-un)))))))
(defn ^:skip-wiki spec-impl
"Do not call this directly, use 'spec'"
([form pred gfn cpred?] (spec-impl form pred gfn cpred? nil))
([form pred gfn cpred? unc]
(cond
(spec? pred) (cond-> pred gfn (with-gen gfn))
(regex? pred) (regex-spec-impl pred gfn)
(named? pred) (cond-> (the-spec pred) gfn (with-gen gfn))
:else
(reify
Spec
(conform* [_ x] (dt pred x form cpred?))
(unform* [_ x] (if cpred?
(if unc
(unc x)
(throw (js/Error. "no unform fn for conformer")))
x))
(explain* [_ path via in x]
(when (= ::invalid (dt pred x form cpred?))
{path {:pred (abbrev form) :val x :via via :in in}}))
(gen* [_ _ _ _] (if gfn
(gfn)
(gen/gen-for-pred pred)))
(with-gen* [_ gfn] (spec-impl form pred gfn cpred?))
(describe* [_] form)))))
(defn ^:skip-wiki multi-spec-impl
"Do not call this directly, use 'multi-spec'"
([form mmvar retag] (multi-spec-impl form mmvar retag nil))
([form mmvar retag gfn]
(let [id (random-uuid)
predx #(let [mm @mmvar]
(c/and (contains? (methods mm)
((-dispatch-fn mm) %))
(mm %)))
dval #((-dispatch-fn @mmvar) %)
tag (if (keyword? retag)
#(assoc %1 retag %2)
retag)]
(reify
Spec
(conform* [_ x] (if-let [pred (predx x)]
(dt pred x form)
::invalid))
(unform* [_ x] (if-let [pred (predx x)]
(unform pred x)
(throw (js/Error. (str "No method of: " form " for dispatch value: " (dval x))))))
(explain* [_ path via in x]
(let [dv (dval x)
path (conj path dv)]
(if-let [pred (predx x)]
(explain-1 form pred path via in x)
{path {:pred form :val x :reason "no method" :via via :in in}})))
(gen* [_ overrides path rmap]
(if gfn
(gfn)
(let [gen (fn [[k f]]
(let [p (f nil)]
(let [rmap (inck rmap id)]
(when-not (recur-limit? rmap id path k)
(gen/delay
(gen/fmap
#(tag % k)
(gensub p overrides (conj path k) rmap (list 'method form k))))))))
gs (->> (methods @mmvar)
(remove (fn [[k]] (= k ::invalid)))
(map gen)
(remove nil?))]
(when (every? identity gs)
(gen/one-of gs)))))
(with-gen* [_ gfn] (multi-spec-impl form mmvar retag gfn))
(describe* [_] `(multi-spec ~form))))))
(defn ^:skip-wiki tuple-impl
"Do not call this directly, use 'tuple'"
([forms preds] (tuple-impl forms preds nil))
([forms preds gfn]
(reify
Spec
(conform* [_ x]
(if-not (c/and (vector? x)
(= (count x) (count preds)))
::invalid
(loop [ret x, i 0]
(if (= i (count x))
ret
(let [v (x i)
cv (dt (preds i) v (forms i))]
(if (= ::invalid cv)
::invalid
(recur (if (identical? cv v) ret (assoc ret i cv))
(inc i))))))))
(unform* [_ x]
(assert (c/and (vector? x)
(= (count x) (count preds))))
(loop [ret x, i 0]
(if (= i (count x))
ret
(let [cv (x i)
v (unform (preds i) cv)]
(recur (if (identical? cv v) ret (assoc ret i v))
(inc i))))))
(explain* [_ path via in x]
(cond
(not (vector? x))
{path {:pred 'vector? :val x :via via :in in}}
(not= (count x) (count preds))
{path {:pred `(= (count ~'%) ~(count preds)) :val x :via via :in in}}
:else
(apply merge
(map (fn [i form pred]
(let [v (x i)]
(when-not (valid? pred v)
(explain-1 form pred (conj path i) via (conj in i) v))))
(range (count preds)) forms preds))))
(gen* [_ overrides path rmap]
(if gfn
(gfn)
(let [gen (fn [i p f]
(gensub p overrides (conj path i) rmap f))
gs (map gen (range (count preds)) preds forms)]
(when (every? identity gs)
(apply gen/tuple gs)))))
(with-gen* [_ gfn] (tuple-impl forms preds gfn))
(describe* [_] `(tuple ~@forms)))))
(defn- tagged-ret [v]
(specify! v
IMapEntry
(-key [_] (-nth v 0))
(-val [_] (-nth v 1))))
(defn ^:skip-wiki or-spec-impl
"Do not call this directly, use 'or'"
[keys forms preds gfn]
(let [id (random-uuid)
kps (zipmap keys preds)
cform (fn [x]
(loop [i 0]
(if (< i (count preds))
(let [pred (preds i)]
(let [ret (dt pred x (nth forms i))]
(if (= ::invalid ret)
(recur (inc i))
(tagged-ret [(keys i) ret]))))
::invalid)))]
(reify
Spec
(conform* [_ x] (cform x))
(unform* [_ [k x]] (unform (kps k) x))
(explain* [this path via in x]
(when-not (valid? this x)
(apply merge
(map (fn [k form pred]
(when-not (valid? pred x)
(explain-1 form pred (conj path k) via in x)))
keys forms preds))))
(gen* [_ overrides path rmap]
(if gfn
(gfn)
(let [gen (fn [k p f]
(let [rmap (inck rmap id)]
(when-not (recur-limit? rmap id path k)
(gen/delay
(gensub p overrides (conj path k) rmap f)))))
gs (remove nil? (map gen keys preds forms))]
(when-not (empty? gs)
(gen/one-of gs)))))
(with-gen* [_ gfn] (or-spec-impl keys forms preds gfn))
(describe* [_] `(or ~@(mapcat vector keys forms))))))
(defn- and-preds [x preds forms]
(loop [ret x
[pred & preds] preds
[form & forms] forms]
(if pred
(let [nret (dt pred ret form)]
(if (= ::invalid nret)
::invalid
;;propagate conformed values
(recur nret preds forms)))
ret)))
(defn- explain-pred-list
[forms preds path via in x]
(loop [ret x
[form & forms] forms
[pred & preds] preds]
(when pred
(let [nret (dt pred ret form)]
(if (not= ::invalid nret)
(recur nret forms preds)
(explain-1 form pred path via in ret))))))
(defn ^:skip-wiki and-spec-impl
"Do not call this directly, use 'and'"
[forms preds gfn]
(reify
Spec
(conform* [_ x] (and-preds x preds forms))
(unform* [_ x] (reduce #(unform %2 %1) x (reverse preds)))
(explain* [_ path via in x] (explain-pred-list forms preds path via in x))
(gen* [_ overrides path rmap] (if gfn (gfn) (gensub (first preds) overrides path rmap (first forms))))
(with-gen* [_ gfn] (and-spec-impl forms preds gfn))
(describe* [_] `(and ~@forms))))
(defn ^:skip-wiki every-impl
"Do not call this directly, use 'every'"
([form pred opts] (every-impl form pred opts nil))
([form pred {:keys [count max-count min-count distinct gen-max gen-into ::kfn]
:or {gen-max 20, gen-into []}
:as opts}
gfn]
(let [check? #(valid? pred %)
kfn (c/or kfn (fn [i v] i))]
(reify
Spec
(conform* [_ x]
(cond
(c/or (not (seqable? x))
(c/and distinct (not (empty? x)) (not (apply distinct? x)))
(c/and count (not= count (bounded-count (inc count) x)))
(c/and (c/or min-count max-count)
(not (<= (c/or min-count 0)
(bounded-count (if max-count (inc max-count) min-count) x)
(c/or max-count MAX_INT)))))
::invalid
:else
(if (indexed? x)
(let [step (max 1 (long (/ (c/count x) *coll-check-limit*)))]
(loop [i 0]
(if (>= i (c/count x))
x
(if (check? (nth x i))
(recur (c/+ i step))
::invalid))))
(c/or (c/and (every? check? (take *coll-check-limit* x)) x)
::invalid))))
(unform* [_ x] x)
(explain* [_ path via in x]
(cond
(not (seqable? x))
{path {:pred 'seqable? :val x :via via :in in}}
(c/and distinct (not (empty? x)) (not (apply distinct? x)))
{path {:pred 'distinct? :val x :via via :in in}}
(c/and count (not= count (bounded-count count x)))
{path {:pred `(= ~count (c/count %)) :val x :via via :in in}}
(c/and (c/or min-count max-count)
(not (<= (c/or min-count 0)
(bounded-count (if max-count (inc max-count) min-count) x)
(c/or max-count MAX_INT))))
{path {:pred `(<= ~(c/or min-count 0) (c/count %) ~(c/or max-count 'js/Number.MAX_SAFE_INTEGER)) :val x :via via :in in}}
:else
(apply merge
(take *coll-error-limit*
(keep identity
(map (fn [i v]
(let [k (kfn i v)]
(when-not (check? v)
(let [prob (explain-1 form pred (conj path k) via (conj in k) v)]
prob))))
(range) x))))))
(gen* [_ overrides path rmap]
(if gfn
(gfn)
(let [init (empty gen-into)
pgen (gensub pred overrides path rmap form)]
(gen/fmap
#(if (vector? init) % (into init %))
(cond
distinct
(if count
(gen/vector-distinct pgen {:num-elements count :max-tries 100})
(gen/vector-distinct pgen {:min-elements (c/or min-count 0)
:max-elements (c/or max-count (max gen-max (c/* 2 (c/or min-count 0))))
:max-tries 100}))
count
(gen/vector pgen count)
(c/or min-count max-count)
(gen/vector pgen (c/or min-count 0) (c/or max-count (max gen-max (c/* 2 (c/or min-count 0)))))
:else
(gen/vector pgen 0 gen-max))))))
(with-gen* [_ gfn] (every-impl form pred opts gfn))
(describe* [_] `(every ~form ~@(mapcat identity opts)))))))
;;;;;;;;;;;;;;;;;;;;;;; regex ;;;;;;;;;;;;;;;;;;;
;;See:
;; http://matt.might.net/articles/implementation-of-regular-expression-matching-in-scheme-with-derivatives/
;; http://www.ccs.neu.edu/home/turon/re-deriv.pdf
;;ctors
(defn- accept [x] {::op ::accept :ret x})
(defn- accept? [{:keys [::op]}]
(= ::accept op))
(defn- pcat* [{[p1 & pr :as ps] :ps, [k1 & kr :as ks] :ks, [f1 & fr :as forms] :forms, ret :ret, rep+ :rep+}]
(when (every? identity ps)
(if (accept? p1)
(let [rp (:ret p1)
ret (conj ret (if ks {k1 rp} rp))]
(if pr
(pcat* {:ps pr :ks kr :forms fr :ret ret})
(accept ret)))
{::op ::pcat, :ps ps, :ret ret, :ks ks, :forms forms :rep+ rep+})))
(defn- pcat [& ps] (pcat* {:ps ps :ret []}))
(defn ^:skip-wiki cat-impl
"Do not call this directly, use 'cat'"
[ks ps forms]
(pcat* {:ks ks, :ps ps, :forms forms, :ret {}}))
(defn- rep* [p1 p2 ret splice form]
(when p1
(let [r {::op ::rep, :p2 p2, :splice splice, :forms form :id (random-uuid)}]
(if (accept? p1)
(assoc r :p1 p2 :ret (conj ret (:ret p1)))
(assoc r :p1 p1, :ret ret)))))
(defn ^:skip-wiki rep-impl
"Do not call this directly, use '*'"
[form p] (rep* p p [] false form))
(defn ^:skip-wiki rep+impl
"Do not call this directly, use '+'"
[form p]
(pcat* {:ps [p (rep* p p [] true form)] :forms `[~form (* ~form)] :ret [] :rep+ form}))
(defn ^:skip-wiki amp-impl
"Do not call this directly, use '&'"
[re preds pred-forms]
{::op ::amp :p1 re :ps preds :forms pred-forms})
(defn- filter-alt [ps ks forms f]
(if (c/or ks forms)
(let [pks (->> (map vector ps
(c/or (seq ks) (repeat nil))
(c/or (seq forms) (repeat nil)))
(filter #(-> % first f)))]
[(seq (map first pks)) (when ks (seq (map second pks))) (when forms (seq (map #(nth % 2) pks)))])
[(seq (filter f ps)) ks forms]))
(defn- alt* [ps ks forms]
(let [[[p1 & pr :as ps] [k1 :as ks] forms] (filter-alt ps ks forms identity)]
(when ps
(let [ret {::op ::alt, :ps ps, :ks ks :forms forms}]
(if (nil? pr)
(if k1
(if (accept? p1)
(accept (tagged-ret [k1 (:ret p1)]))
ret)
p1)
ret)))))
(defn- alts [& ps] (alt* ps nil nil))
(defn- alt2 [p1 p2] (if (c/and p1 p2) (alts p1 p2) (c/or p1 p2)))
(defn ^:skip-wiki alt-impl
"Do not call this directly, use 'alt'"
[ks ps forms] (assoc (alt* ps ks forms) :id (random-uuid)))
(defn ^:skip-wiki maybe-impl
"Do not call this directly, use '?'"
[p form] (assoc (alt* [p (accept ::nil)] nil [form ::nil]) :maybe form))
(defn- noret? [p1 pret]
(c/or (= pret ::nil)
(c/and (#{::rep ::pcat} (::op (reg-resolve! p1))) ;;hrm, shouldn't know these
(empty? pret))
nil))
(declare preturn)
(defn- accept-nil? [p]
(let [{:keys [::op ps p1 p2 forms] :as p} (reg-resolve! p)]
(case op
::accept true
nil nil
::amp (c/and (accept-nil? p1)
(c/or (noret? p1 (preturn p1))
(let [ret (-> (preturn p1) (and-preds ps (next forms)))]
(not= ret ::invalid))))
::rep (c/or (identical? p1 p2) (accept-nil? p1))
::pcat (every? accept-nil? ps)
::alt (c/some accept-nil? ps))))
(declare add-ret)
(defn- preturn [p]
(let [{[p0 & pr :as ps] :ps, [k :as ks] :ks, :keys [::op p1 ret forms] :as p} (reg-resolve! p)]
(case op
::accept ret
nil nil
::amp (let [pret (preturn p1)]
(if (noret? p1 pret)
::nil
(and-preds pret ps forms)))
::rep (add-ret p1 ret k)
::pcat (add-ret p0 ret k)
::alt (let [[[p0] [k0]] (filter-alt ps ks forms accept-nil?)
r (if (nil? p0) ::nil (preturn p0))]
(if k0 (tagged-ret [k0 r]) r)))))
(defn- op-unform [p x]
;;(prn {:p p :x x})
(let [{[p0 & pr :as ps] :ps, [k :as ks] :ks, :keys [::op p1 ret forms rep+ maybe] :as p} (reg-resolve! p)
kps (zipmap ks ps)]
(case op
::accept [ret]
nil [(unform p x)]
::amp (let [px (reduce #(unform %2 %1) x (reverse ps))]
(op-unform p1 px))
::rep (mapcat #(op-unform p1 %) x)
::pcat (if rep+
(mapcat #(op-unform p0 %) x)
(mapcat (fn [k]
(when (contains? x k)
(op-unform (kps k) (get x k))))
ks))
::alt (if maybe
[(unform p0 x)]
(let [[k v] x]
(op-unform (kps k) v))))))
(defn- add-ret [p r k]
(let [{:keys [::op ps splice] :as p} (reg-resolve! p)
prop #(let [ret (preturn p)]
(if (empty? ret) r ((if splice into conj) r (if k {k ret} ret))))]
(case op
nil r
(::alt ::accept ::amp)
(let [ret (preturn p)]
;;(prn {:ret ret})
(if (= ret ::nil) r (conj r (if k {k ret} ret))))
(::rep ::pcat) (prop))))
(defn- deriv
[p x]
(let [{[p0 & pr :as ps] :ps, [k0 & kr :as ks] :ks, :keys [::op p1 p2 ret splice forms] :as p} (reg-resolve! p)]
(when p
(case op
::accept nil
nil (let [ret (dt p x p)]
(when-not (= ::invalid ret) (accept ret)))
::amp (when-let [p1 (deriv p1 x)]
(if (= ::accept (::op p1))
(let [ret (-> (preturn p1) (and-preds ps (next forms)))]
(when-not (= ret ::invalid)
(accept ret)))
(amp-impl p1 ps forms)))
::pcat (alt2 (pcat* {:ps (cons (deriv p0 x) pr), :ks ks, :forms forms, :ret ret})
(when (accept-nil? p0) (deriv (pcat* {:ps pr, :ks kr, :forms (next forms), :ret (add-ret p0 ret k0)}) x)))
::alt (alt* (map #(deriv % x) ps) ks forms)
::rep (alt2 (rep* (deriv p1 x) p2 ret splice forms)
(when (accept-nil? p1) (deriv (rep* p2 p2 (add-ret p1 ret nil) splice forms) x)))))))
(defn- op-describe [p]
(let [{:keys [::op ps ks forms splice p1 rep+ maybe] :as p} (reg-resolve! p)]
;;(prn {:op op :ks ks :forms forms :p p})
(when p
(case op
::accept nil
nil p
::amp (list* 'clojure.spec/& (op-describe p1) forms)
::pcat (if rep+
(list `+ rep+)
(cons `cat (mapcat vector (c/or (seq ks) (repeat :_)) (c/or (seq forms) (repeat nil)))))
::alt (if maybe
(list `? maybe)
(cons `alt (mapcat vector ks forms)))
::rep (list (if splice `+ `*) forms)))))
(defn- op-explain [form p path via in input]
;;(prn {:form form :p p :path path :input input})
(let [[x :as input] input
{:keys [::op ps ks forms splice p1 p2] :as p} (reg-resolve! p)
via (if-let [name (spec-name p)] (conj via name) via)
insufficient (fn [path form]
{path {:reason "Insufficient input"
:pred (abbrev form)
:val ()
:via via
:in in}})]
(when p
(case op
::accept nil
nil (if (empty? input)
(insufficient path form)
(explain-1 form p path via in x))
::amp (if (empty? input)
(if (accept-nil? p1)
(explain-pred-list forms ps path via in (preturn p1))
(insufficient path (op-describe p1)))
(if-let [p1 (deriv p1 x)]
(explain-pred-list forms ps path via in (preturn p1))
(op-explain (op-describe p1) p1 path via in input)))
::pcat (let [pkfs (map vector
ps
(c/or (seq ks) (repeat nil))
(c/or (seq forms) (repeat nil)))
[pred k form] (if (= 1 (count pkfs))
(first pkfs)
(first (remove (fn [[p]] (accept-nil? p)) pkfs)))
path (if k (conj path k) path)
form (c/or form (op-describe pred))]
(if (c/and (empty? input) (not pred))
(insufficient path form)
(op-explain form pred path via in input)))
::alt (if (empty? input)
(insufficient path (op-describe p))
(apply merge
(map (fn [k form pred]
(op-explain (c/or form (op-describe pred))
pred
(if k (conj path k) path)
via
in
input))