-
Notifications
You must be signed in to change notification settings - Fork 54
/
delta.cljc
2000 lines (1616 loc) · 48 KB
/
delta.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
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
(ns meander.syntax.delta
#?(:clj
(:require [clojure.set :as set]
[clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as s.gen]
[clojure.string :as string]
[cljs.tagged-literals]
[meander.util.delta :as r.util])
:cljs
(:require [clojure.set :as set]
[cljs.spec.alpha :as s :include-macros true]
[cljs.spec.gen.alpha :as s.gen :include-macros true]
[clojure.string :as string]
[meander.util.delta :as r.util]))
#?(:cljs
(:require-macros [meander.syntax.delta]))
#?(:clj
(:import (cljs.tagged_literals JSValue))))
#?(:clj (set! *warn-on-reflection* true))
;; ---------------------------------------------------------------------
;; AST specs and predicates
(s/def :meander.syntax.delta/tag
keyword?)
(s/def :meander.syntax.delta/node
(s/keys :req-un [:meander.syntax.delta/tag]))
(defn any-form?
"true if x is a symbol beginning with _."
[x]
(and (simple-symbol? x)
(r.util/re-matches? #"_.*" (name x))))
(s/def :meander.syntax.delta/any
(s/with-gen
(s/conformer
(fn [x]
(if (any-form? x)
x
::s/invalid))
identity)
(fn []
(s.gen/fmap
(fn [sym]
(symbol (str "_" (name sym))))
(s.gen/symbol)))))
(s/def :meander.syntax.delta.node.any/tag
#{:any})
(s/def :meander.syntax.delta.node.any/symbol
:meander.syntax.delta/any)
(s/def :meander.syntax.delta.node/any
(s/keys :req-un [:meander.syntax.delta.node.any/tag
:meander.syntax.delta.node.any/symbol]))
(defn any-node?
"true if x is an :any node, false otherwise."
[x]
(s/valid? :meander.syntax.delta.node/any x))
(s/fdef logic-variable-form?
:args (s/cat :x any?)
:ret boolean?)
(defn logic-variable-form?
"true if x is in the form of a logic variable i.e. a simple symbol
with a name beginning with \\?."
[x]
(and (simple-symbol? x)
(r.util/re-matches? #"\?.+" (name x))))
(s/def :meander.syntax.delta/logic-variable
(s/with-gen
(s/conformer
(fn [x]
(if (logic-variable-form? x)
x
::s/invalid))
identity)
(fn []
(s.gen/fmap
(fn [x]
(symbol (str \? (name x))))
(s/gen simple-symbol?)))))
(s/def :meander.syntax.delta.node.lvr/tag
#{:lvr})
(s/def :meander.syntax.delta.node.lvr/symbol
:meander.syntax.delta/logic-variable)
(s/def :meander.syntax.delta.node/lvr
(s/keys :req-un [:meander.syntax.delta.node.lvr/tag
:meander.syntax.delta.node.lvr/symbol]))
(defn lvr-node?
[x]
(s/valid? :meander.syntax.delta.node/lvr x))
(defn memory-variable-form?
"true if x is in the form of a memory variable i.e. a simple symbol
with a name beginning with \\!."
[x]
(and (simple-symbol? x)
(r.util/re-matches? #"!.+" (name x))))
(s/def :meander.syntax.delta/memory-variable
(s/with-gen
(s/conformer
(fn [x]
(if (memory-variable-form? x)
x
::s/invalid))
identity)
(fn []
(s.gen/fmap
(fn [x]
(symbol (str \! (name x))))
(s/gen simple-symbol?)))))
(s/def :meander.syntax.delta.node.mvr/tag
#{:mvr})
(s/def :meander.syntax.delta.node.mvr/symbol
:meander.syntax.delta/memory-variable)
(s/def :meander.syntax.delta.node/mvr
(s/keys :req-un [:meander.syntax.delta.node.mvr/tag
:meander.syntax.delta.node.mvr/symbol]))
(defn mvr-node?
[x]
(s/valid? :meander.syntax.delta.node/mvr x))
(defn variable-node?
[x]
(or (mvr-node? x)
(lvr-node? x)))
(defn ref-sym?
[x]
(and (simple-symbol? x)
(boolean (re-matches #"%.+" (name x)))))
(s/def :meander.syntax.delta/reference
(s/with-gen
(s/conformer
(fn [x]
(if (ref-sym? x)
x
::s/invalid))
identity)
(fn []
(s.gen/fmap
(fn [x]
(symbol (str \% (name x))))
(s/gen simple-symbol?)))))
(s/def :meander.syntax.delta.node.ref/tag
#{:ref})
(s/def :meander.syntax.delta.node.ref/symbol
:meander.syntax.delta/reference)
(s/def :meander.syntax.delta.node/ref
(s/keys :req-un [:meander.syntax.delta.node.ref/symbol
:meander.syntax.delta.node.ref/tag]))
(defn ref-node?
"true if x is a :ref node, false otherwise."
[x]
(s/valid? :meander.syntax.delta.node/ref x))
(s/def :meander.syntax.delta.node.with/tag
#{:wth})
(s/def :meander.syntax.delta.node.with.binding/ref
:meander.syntax.delta.node/ref)
(s/def :meander.syntax.delta.node.with.binding/pattern
:meander.syntax.delta/node)
(s/def :meander.syntax.delta.node.with/binding
(s/keys :req-un [:meander.syntax.delta.node.with.binding/pattern
:meander.syntax.delta.node.with.binding/ref]))
(s/def :meander.syntax.delta.node.with/bindings
(s/coll-of :meander.syntax.delta.node.with/binding
:kind sequential?))
(s/def :meander.syntax.delta.node.with/body
(s/nilable :meander.syntax.delta/node))
(s/def :meander.syntax.delta.node/with
(s/keys :req-un [:meander.syntax.delta.node.with/tag
:meander.syntax.delta.node.with/bindings]
:opt-un [:meander.syntax.delta.node.with/body]))
(defn with-node? [x]
(s/valid? :meander.syntax.delta.node/with x))
(defn node?
"true if x is an AST node."
[x]
(s/valid? :meander.syntax.delta/node x))
;; ---------------------------------------------------------------------
;; AST API
(defn tag
"Return the tag of node."
[node]
(s/assert :meander.syntax.delta/node node)
(:tag node))
;; children
;; --------
(s/fdef children
:args (s/cat :node :meander.syntax.delta/node)
:ret (s/coll-of :meander.syntax.delta/node
:kind sequential?))
(defmulti children
"Return a sequential? of all children of node."
{:arglists '([node])}
#'tag)
(defmethod children :default
[node]
[])
(defn subnodes
"Return a sequence of all subnodes of node."
[node]
(cons node (mapcat subnodes (children node))))
(defn proper-subnodes
"Return the all subnodes in node excluding node."
[node]
(rest (subnodes node)))
(s/fdef max-length
:args (s/cat :node :meander.syntax.delta/node)
:ret (s/or :nat nat-int?
:inf #{##Inf}))
(defmulti max-length
"The maximum possible length the pattern described by node can
be. Note, this mutlimethod will throw an error wheneven node does
not have a method to handle it. This behavior is intentional as the
implementations should only exist for things which have can have
length."
{:arglists '([node])}
#'tag)
(defmulti min-length
"The maximum possible length the pattern described by node can be.
Note, this mutlimethod will throw an error wheneven node does not
have a method to handle it. This behavior is intentional as the
implementations should only exist for things which have can have
length."
{:arglists '([node])}
#'tag)
(s/fdef variable-length?
:args (s/cat :node :meander.syntax.delta/node)
:ret boolean?)
(defn variable-length?
"true if node may have a variable length, false otherwise. Note this
function will throw an error if node does not implement methods for
both min-length and max-length."
[node]
(not (= (min-length node) (max-length node))))
(defmulti ground?
"true if node is ground i.e. it contains no variables or is not a
match operator."
{:arglists '([node])}
#'tag)
(s/fdef search?
:args (s/cat :node :meander.syntax.delta/node)
:ret boolean?)
(defmulti search?
"true if node represents a search, false otherwise."
{:arglists '([node])}
#'tag)
(defmulti unparse
"In pre-order fashion rewrite a node into a Clojure form."
{:arglists '([node])}
#'tag)
(defn fold
"Same as clojure.core/reduce but specifically for ASTs. f must be a
binary (arity 2) function and will receive as arguments the current
accumulated value and an AST node. fold is eager and will visit each
subnode in node."
[f value node]
(reduce f value (subnodes node)))
(defn variables*
{:private true}
[node]
(fold
(fn [vars node]
(let [tag (tag node)]
(case tag
(:lvr :mut :mvr :ref)
(update vars tag conj node)
:wth
(transduce (comp (map :pattern)
(map variables*))
(fn
([a] a)
([a b]
(merge-with set/union a b)))
vars
(:bindings node))
;; else
vars)))
{:lvr #{}
:mut #{}
:mvr #{}
:ref #{}}
node))
(defn variables
"Return all :lvr and :mvr nodes in node."
[node]
(s/assert :meander.syntax.delta/node node)
(let [vars (variables* node)]
(set/union (:lvr vars) (:mvr vars))))
(defn memory-variables
"Return all :mvr nodes in node."
[node]
(s/assert :meander.syntax.delta/node node)
(:mvr (variables* node)))
(defn logic-variables
"Return all :lvr nodes in node."
[node]
(s/assert :meander.syntax.delta/node node)
(:lvr (variables* node)))
(s/fdef references
:args (s/cat :node :meander.syntax.delta/node)
:ret (s/coll-of :meander.syntax.delta.node/ref
:kind set?
:into #{}))
(defn references
"Return all :ref nodes in node."
[node]
(:ref (variables* node)))
(defn top-level
[node]
(case (tag node)
(:cnj :dsj :wth)
(mapcat top-level (children node))
;; else
[node]))
;; ---------------------------------------------------------------------
;; Parse implementation
(declare parse)
(defn parse-all
{:private true}
[xs env]
(map (fn [x] (parse x env)) xs))
(defn expand-prt
{:private true}
[xs]
(let [[l r] (split-with
(fn [{tag :tag}]
(case tag
(:dot :dt+ :dt*)
false
;; else
true))
xs)]
(if (seq l)
(let [node (first r)]
{:tag :prt
:left (case (:tag node)
:dt*
(let [c (bounded-count 2 l)]
(cond
(and (= c 1)
(= :any (:tag (first l))))
{:tag :drp
:symbol (:symbol (first l))}
(and (= c 1)
(= :mvr (:tag (first l))))
{:tag :rst
:mvr (first l)}
:else
{:tag :rp*
:cat {:tag :cat
:elements l}}))
:dt+
{:tag :rp+
:cat {:tag :cat
:elements l}
:n (:n node)}
(nil :dot)
{:tag :cat
:elements l})
:right (expand-prt (next r))})
(if (seq r)
(let [node (first r)]
{:tag :prt
:left (case (:tag node)
:dt*
{:tag :rp*
:cat {:tag :cat
:elements l}}
:dt+
{:tag :rp+
:cat {:tag :cat
:elements l}
:n (:n node)}
(nil :dot)
{:tag :cat
:elements l})
:right (expand-prt (next r))})
{:tag :cat
:elements []}))))
(defn expand-symbol
{:private true}
[sym env]
#?(:clj (if-some [cljs-ns (:ns env)]
;; ClojureScript
(if (qualified-symbol? sym)
(let [ns-sym (symbol (namespace sym))]
(if-some [ns (get (:requires cljs-ns) ns-sym)]
(symbol (name ns) (name sym))
sym))
(if (contains? (:defs cljs-ns) sym)
(symbol (name (:name cljs-ns)) (name sym))
sym))
;; Clojure
(if (qualified-symbol? sym)
(let [ns-sym (symbol (namespace sym))]
(if-some [ns (get (ns-aliases *ns*) ns-sym)]
(symbol (name (ns-name ns)) (name sym))
sym))
(symbol (name (ns-name *ns*)) (name sym))))
:cljs sym))
(defmulti expand-seq
{:arglists '([seq env])}
(fn [xs env]
(if (seq? xs)
(let [x (first xs)]
(if (symbol? x)
(expand-symbol x env)
::default))
::default)))
(defmethod expand-seq :default
[xs env]
xs)
(defn parse-scan
{:private true}
[xs env]
(if (and (seq? xs)
(= (first xs) 'scan))
(let [nothing (gensym)]
(if (identical? (nth xs 1 nothing) nothing)
(throw (ex-info "scan expects at least one argument"
{:pattern xs
:meta (meta xs)}))
(parse
`(~'pred coll?
;; Will cause compiler to emit a useless seq? check.
(~'app seq (~@'(_ ...) ~@(rest xs) ~@'(. _ ...))))
env)))
(parse xs env)))
(defn parse-vscan
{:private true}
[xs env]
(if (and (seq? xs)
(= (first xs) 'vscan))
(let [nothing (gensym)]
(if (identical? (nth xs 1 nothing) nothing)
(throw (ex-info "vscan expects at least one argument"
{:pattern xs
:meta (meta xs)}))
(parse
`(~'pred coll?
;; Will cause compiler to emit a useless vector?
;; check.
(~'app vec [~@'(_ ...) ~@(rest xs) ~@'(. _ ...)]))
env)))
(parse xs env)))
(defn parse-contain
{:private true}
[xs env]
(if (and (seq? xs) (= (first xs) '$))
(case (long (bounded-count 2 (rest xs)))
1
{:tag :ctn
:pattern (parse (nth xs 1) env)}
2
{:tag :ctn
;; Should be an :lvr or :mvr.
:context (parse (nth xs 1) env)
:pattern (parse (nth xs 2) env)}
;; else
(throw (ex-info "$ expects one or two arguments"
{:form xs
:meta (meta xs)})))
(parse xs env)))
(defn parse-with
{:private true}
[xs env]
(if (and (seq? xs) (= 'with (first xs)))
(let [bindings (nth xs 1)]
(if (and (vector? bindings)
(even? (count bindings))
(every? ref-sym? (take-nth 2 bindings)))
{:tag :wth
:bindings (map
(fn [[ref-sym x]]
{:ref {:tag :ref
:symbol ref-sym}
:pattern (parse x env)})
(partition 2 bindings))
:body (let [nothing (gensym)
x (nth xs 2 nothing)]
(if (identical? x nothing)
nil
(parse x env)))}
(throw
(ex-info "second argument to with must be vector of the form [%ref-name pattern ...]"
{:form xs
:meta (meta xs)}))))
(parse xs env)))
(defn parse-seq-no-head
{:private true}
[xs env]
(let [c (count xs)
as-index (- c 2)]
(if (and (<= 2 c)
(= (nth xs as-index) :as))
(let [xs* (take as-index xs)
as-pattern (last xs)
as-node (parse as-pattern env)]
(case (:tag as-node)
(:lvr :mvr)
{:tag :seq
:as as-node
:prt (expand-prt (parse-all xs* env))}
;; else
(throw (ex-info "Seq :as pattern must be a logic variable or memory variable"
{:form xs
:meta (meta xs)}))))
{:tag :seq
:prt (expand-prt (parse-all xs env))})))
(defn parse-seq
"Parses a seq? into a :meander.syntax.delta/node.
seqs? of the following form are handled specially, all other seqs
are parsed as :seq nodes.
($ <pattern>)
($ ?<context-name> <pattern>)
(and <pattern_0> ... <pattern_n>)
(app <expr> <pattern> ...)
(guard <expr>)
(let <pattern_0> <expr_0> ... <pattern_n> <expr_n>)
(not <pattern>)
(or <pattern_0> ... <pattern_n>)
(pred <expr> <pattern_0> ... <pattern_n>)
(quote <form>)
(re <regex-expr>)
(re <regex-expr> <pattern>)
(with [%<simple-symbol> <pattern> ...] <pattern>)
(clojure.core/unquote <form>)
(clojure.core/unquote-splicig <form>)
(<symbol*> <form_0> ... <form_n>)
where symbol* is a fully qualified symbol with respect to the
current namespace."
{:private true}
[xs env]
(let [x (first xs)]
(if (symbol? x)
(case x
$
(parse-contain xs env)
and
{:tag :cnj
:arguments (parse-all (rest xs) env)}
app
{:tag :app
:fn-expr (second xs)
:arguments (parse-all (nnext xs) env)}
guard
{:tag :grd
:expr (second xs)}
let
(let [xs* (rest xs)]
(if (odd? (count xs*))
(throw (ex-info "let pattern requires an even number of arguments"
{:pattern xs
:meta (meta xs)}))
{:tag :let
:bindings (map
(fn [[pattern expr]]
{:binding (parse pattern env)
:expr expr})
(partition-all 2 xs*))}))
not
(if (= 1 (bounded-count 2 (drop 1 xs)))
{:tag :not
:argument (parse (second xs) env)}
(throw (ex-info "not pattern requires at one argument"
{:pattern xs
:meta (meta xs)})))
or
{:tag :dsj
:arguments (parse-all (rest xs) env)}
pred
{:tag :prd
:form (second xs)
:arguments (parse-all (nnext xs) env)}
quote
{:tag :quo
:form (second xs)}
re
(let [nothing (gensym)
regex (nth xs 1 nothing)]
(if (identical? regex nothing)
(throw (ex-info "re pattern expects at least one argument"
{:pattern xs
:meta (meta xs)}))
(let [capture (nth xs 2 nothing)]
(if (identical? capture nothing)
{:tag :rxt
:regex regex}
{:tag :rxc
:regex regex
:capture (parse capture env)}))))
scan
(parse-scan xs env)
vscan
(parse-vscan xs env)
with
(parse-with xs env)
clojure.core/unquote
{:tag :unq
:expr (second xs)}
clojure.core/unquote-splicing
{:tag :uns
:expr (second xs)}
;; else
(let [xs* (expand-seq xs env)]
(if (= xs* xs)
(parse-seq-no-head xs env)
(parse xs* env))))
(parse-seq-no-head xs env))))
(defn parse-symbol
{:private true}
[sym]
(if (namespace sym)
{:tag :lit
:value sym}
(let [s (name sym)
[$0 $N] (re-matches #"\.(?:\.(?:\.|(\d+))?)?" s)]
(case $N
nil
(case $0
;; Internal tag for postfix partition.
"."
{:tag :dot}
;; Internal tag for postfix n or more operator.
".."
{:tag :dt+
:n $N}
;; Internal tag for postfix 0 or more operator.
"..."
{:tag :dt*}
nil
(cond
(r.util/re-matches? #"^_.*" s)
{:tag :any
:symbol sym}
(r.util/re-matches? #"^\?.+" s)
{:tag :lvr
:symbol sym}
(r.util/re-matches? #"^!.+" s)
{:tag :mvr
:symbol sym}
(r.util/re-matches? #"^%.+" s)
{:tag :ref
:symbol sym}
(r.util/re-matches? #"^\*.+" s)
{:tag :mut
:symbol sym}
:else
{:tag :lit
:value sym}))
;; `..0` is the same as `...`.
"0"
{:tag :dt*}
;; else
;; Inteneral tag for postfix n or more operator.
{:tag :dt+
:n (r.util/parse-int $N)}))))
(defn parse-js-value
{:private true}
[^JSValue js-value env]
(let [x (.val js-value)]
(cond
(vector? x)
{:tag :jsa
:prt (expand-prt (parse-all x env))}
(map? x)
{:tag :jso
:object (into {}
(map
(fn [[k v]]
(let [k* (if (keyword? k)
(subs (str k) 1)
k)]
[(parse k*) (parse v)])))
x)})))
(defn parse-vector
{:private true}
[v env]
(if (vector? v)
(let [c (count v)
as-index (- c 2)]
(if (and (<= 2 c)
(= (nth v as-index) :as))
(let [v* (subvec v 0 as-index)
[_ as-pattern] (subvec v as-index)
as-node (parse as-pattern env)]
(case (:tag as-node)
(:lvr :mvr)
{:tag :vec
:as as-node
:prt (expand-prt (parse-all v* env))}
;; else
(throw (ex-info "Vector :as pattern must be a logic variable or memory variable"
{:form v
:meta (meta v)}))))
{:tag :vec
:prt (expand-prt (parse-all v env))}))
(parse v env)))
(defn parse-map
{:private true}
[m env]
(if (and (map? m)
(not (record? m)))
(let [as (if-some [[_ y] (find m :as)]
(if (or (logic-variable-form? y)
(memory-variable-form? y))
(parse y env)))
m (if (some? as)
(dissoc m :as)
m)
rest-map (if-some [[_ y] (find m '&)]
(parse y env))
m (if (some? rest-map)
(dissoc m '&)
m)]
{:tag :map
:as as
:rest-map rest-map
:map (into {}
(map
(fn [[k v]]
[(parse k env) (parse v env)]))
m)})
(parse m env)))
(defn parse-set [s env]
(if (set? s)
(let [as-form (some
(fn [x]
(when (= (:as (meta x)) true)
x))
s)
s (if (some? as-form)
(disj s as-form)
s)
rest-form (some
(fn [x]
(when (= (:tag (meta x)) '&)
x))
s)
s (if (some? rest-form)
(disj s rest-form)
s)]
{:tag :set
:as (if (some? as-form)
(parse as-form env))
:rest (if (some? rest-form)
(parse rest-form env))
:elements (parse-all s env)})
(parse s env)))
(s/fdef parse
:args (s/alt :a1 (s/cat :x any?)
:a2 (s/cat :x any? :env map?))
:ret :meander.syntax.delta/node)
(defn parse
"Parse `x` into an abstract syntax tree (AST) optionally with
respect to the environment `env`.
(parse '(?x1 ?x2 :as ?xs))
;; =>
{:tag :seq
:as {:tag :lvr
:symbol ?xs}
:prt {:tag :prt
:left {:tag :cat
:elements ({:tag :lvr :symbol ?x1}
{:tag :lvr :symbol ?x2})}
:right {:tag :cat
:elements []}}}"
([x]
(parse x {}))
([x env]
(let [node (cond
(seq? x)
(parse-seq x env)
(vector? x)
(parse-vector x env)
(and (map? x)
(not (record? x)))
(parse-map x env)
(set? x)
(parse-set x env)
(symbol? x)
(parse-symbol x)
#?@(:clj [(instance? JSValue x)
(parse-js-value x env)])
:else
{:tag :lit
:value x})]
(if-some [meta (meta x)]
(with-meta node meta)
node))))
;; ---------------------------------------------------------------------
;; AST method implementations
;; :any
(defmethod ground? :any [_]
false)
(defmethod unparse :any [_]
'_)
(defmethod search? :any [_]
false)
;; :app
(defmethod children :app [node]
(:arguments node))
(defmethod ground? :app [_]
false)
(defmethod unparse :app [node]
`(~'app ~(:fn-exp node) ~@(map unparse (:arguments node))))
(defmethod search? :app
[_] false)
;; :cat
(defmethod ground? :cat [node]
(every? ground? (:elements node)))
(defmethod children :cat [node]
(:elements node))
(defmethod min-length :cat [node]
(count (:elements node)))
(defmethod max-length :cat [node]
(count (:elements node)))
(defmethod unparse :cat [node]
(apply list (map unparse (:elements node))))
(defmethod search? :cat [node]
(boolean (some search? (:elements node))))
;; :cnj
(defmethod children :cnj [node]
(:arguments node))
(defmethod ground? :cnj [_]
false)
(defmethod unparse :cnj [node]
`(~'and ~@(sequence (map unparse) (:arguments node))))
(defmethod search? :cnj
[node]
(boolean (some search? (:arguments node))))
;; :ctn
(defmethod children :ctn [node]
(if-some [[_ pattern] (find node :pattern)]
[pattern]
[]))
(defmethod ground? :ctn [_]
false)
(defmethod unparse :ctn [node]
`(~'$
~@(if-some [[_ context] (find node :context)]
[(unparse context)])
~@(if-some [[_ pattern] (find node :pattern)]
[(unparse pattern)])))
(defmethod search? :ctn [_]
true)