-
Notifications
You must be signed in to change notification settings - Fork 21
/
parser.cljc
157 lines (134 loc) · 4.03 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
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
(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))
(-matcher-for
([this] (matchers/pred this))
([this _t->m] (matchers/pred this)))
;; equals base types
nil
(-match [this actual]
(core/match (matchers/equals this) actual))
(-matcher-for
([_this] matchers/equals)
([this t->m] (matchers/lookup-matcher this t->m)))
number
(-match [this actual]
(core/match (matchers/equals this) actual))
(-matcher-for
([_this] matchers/equals)
([this t->m] (matchers/lookup-matcher this t->m)))
string
(-match [this actual]
(core/match (matchers/equals this) actual))
(-matcher-for
([_this] matchers/equals)
([this t->m] (matchers/lookup-matcher this t->m)))
boolean
(-match [this actual]
(core/match (matchers/equals this) actual))
(-matcher-for
([_this] matchers/equals)
([this t->m] (matchers/lookup-matcher this t->m)))
Keyword
(-match [this actual]
(core/match (matchers/equals this) actual))
(-matcher-for
([_this] matchers/equals)
([this t->m] (matchers/lookup-matcher this t->m)))
Symbol
(-match [this actual]
(core/match (matchers/equals this) actual))
(-matcher-for
([_this] matchers/equals)
([this t->m] (matchers/lookup-matcher this t->m)))
UUID
(-match [this actual]
(core/match (matchers/equals this) actual))
(-matcher-for
([_this] matchers/equals)
([this t->m] (matchers/lookup-matcher this t->m)))
goog.Uri
(-match [this actual]
(core/match (matchers/cljs-uri this) actual))
(-matcher-for
([_this] matchers/cljs-uri)
([this t->m] (matchers/lookup-matcher this t->m)))
js/Date
(-match [this actual]
(core/match (matchers/equals this) actual))
(-matcher-for
([_this] matchers/equals)
([this t->m] (matchers/lookup-matcher this t->m)))
Var
(-match [this actual]
(core/match (matchers/equals this) actual))
(-matcher-for
([_this] matchers/equals)
([this t->m] (matchers/lookup-matcher this t->m)))
;; equals nested types
Cons
(-match [this actual]
(core/match (matchers/equals this) actual))
(-matcher-for
([_this] matchers/equals)
([this t->m] (matchers/lookup-matcher this t->m)))
Repeat
(-match [this actual]
(core/match (matchers/equals this) actual))
(-matcher-for
([_this] matchers/equals)
([this t->m] (matchers/lookup-matcher this t->m)))
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)))
(-matcher-for
([this] (if (satisfies? IMap this)
matchers/embeds
matchers/equals))
([this t->m] (matchers/lookup-matcher this t->m)))
js/RegExp
(-match [this actual]
(core/match (matchers/regex this) actual))
(-matcher-for
([_this] matchers/equals)
([this t->m] (matchers/lookup-matcher this t->m)))))
#?(: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/lookup-matcher this t->m)))
(-match [this actual]
(core/match (matchers/pred this) actual)))))