-
Notifications
You must be signed in to change notification settings - Fork 21
/
parser.cljc
110 lines (87 loc) · 2.47 KB
/
parser.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
(ns matcher-combinators.parser
(:require [matcher-combinators.core :as core]
[matcher-combinators.matchers :as matchers])
#?(:cljs (:import goog.Uri)
:clj (:import (clojure.lang IPersistentMap)
(java.util.regex Pattern))))
#?(:cljs
(extend-protocol
core/Matcher
;; function as predicate
function
(-match [this actual]
(core/match (matchers/pred this) actual))
;; equals base types
nil
(-match [this actual]
(core/match (matchers/equals this) actual))
number
(-match [this actual]
(core/match (matchers/equals this) actual))
string
(-match [this actual]
(core/match (matchers/equals this) actual))
boolean
(-match [this actual]
(core/match (matchers/equals this) actual))
Keyword
(-match [this actual]
(core/match (matchers/equals this) actual))
Symbol
(-match [this actual]
(core/match (matchers/equals this) actual))
UUID
(-match [this actual]
(core/match (matchers/equals this) actual))
goog.Uri
(-match [this actual]
(core/match (matchers/cljs-uri this) actual))
js/Date
(-match [this actual]
(core/match (matchers/equals this) actual))
Var
(-match [this actual]
(core/match (matchers/equals this) actual))
;; equals nested types
Cons
(-match [this actual]
(core/match (matchers/equals this) actual))
Repeat
(-match [this actual]
(core/match (matchers/equals this) actual))
default
(-match [this actual]
(cond
(satisfies? IMap this)
(core/match (matchers/embeds this) actual)
(or (satisfies? ISet this)
(satisfies? ISequential this))
(core/match (matchers/equals this) actual)))
js/RegExp
(-match [this actual]
(core/match (matchers/regex this) actual))))
#?(:clj (do
(defmacro mimic-matcher [matcher t]
`(extend-type ~t
core/Matcher
(~'-matcher-for
([this#] ~matcher)
([this# t->m#] (matchers/lookup-matcher this# t->m#)))
(~'-match [this# actual#]
(core/match (~matcher this#) actual#))))
;; default for most objects
(mimic-matcher matchers/equals Object)
;; nil is a special case
(mimic-matcher matchers/equals nil)
;; regex
(mimic-matcher matchers/regex Pattern)
;; collections
(mimic-matcher matchers/embeds IPersistentMap)
;; functions are special, too
(extend-type clojure.lang.Fn
core/Matcher
(-matcher-for
([this] (matchers/pred this))
([this t->m] (matchers/pred this)))
(-match [this actual]
(core/match (matchers/pred this) actual)))))