blob: c0bd6d92e49745ca94fb36be7d7e68c4bc74e205 (
plain)
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
|
(.module:
[lux #*
["_" test (#+ Test)]
[abstract
[monad (#+ do)]
[equivalence (#+ Equivalence)]
{[0 #test]
[/
["$." functor (#+ Injection Comparison)]
["$." apply]
["$." monad]]}]
[control
[parser
["s" code (#+ Parser)]]]
[data
["." error (#+ Error)]
[number
["." nat]]
["." text ("#;." equivalence)
format]
[collection
["." list ("#;." functor)]]]
[math
["r" random]]
[macro
["." code]
[syntax (#+ syntax:)]]]
{1
["." / (#+ Parser)]})
(def: (should-fail expected input)
(All [a] (-> Text (Error a) Bit))
(case input
(#error.Failure actual)
(text;= expected actual)
_
#0))
(def: (enforced? parser input)
(All [s] (-> (Parser s Any) s Bit))
(case (/.run parser input)
(#error.Success [_ []])
#1
_
#0))
(def: (found? parser input)
(All [s] (-> (Parser s Bit) s Bit))
(case (/.run parser input)
(#error.Success [_ #1])
#1
_
#0))
(def: (fails? input)
(All [a] (-> (Error a) Bit))
(case input
(#error.Failure _)
#1
_
#0))
(syntax: (match pattern then input)
(wrap (list (` (case (~ input)
(^ (#error.Success [(~' _) (~ pattern)]))
(~ then)
(~' _)
#0)))))
(def: combinators-0
Test
(do r.monad
[expected0 r.nat
variadic (:: @ map (|>> (n/max 1) (n/min 20)) r.nat)
expected+ (r.list variadic r.nat)
even0 (r.filter n/even? r.nat)
odd0 (r.filter n/odd? r.nat)
not0 r.bit]
($_ _.and
(_.test "Can optionally succeed with some parser."
(and (|> (list (code.nat expected0))
(/.run (/.maybe s.nat))
(match (#.Some actual)
(n/= expected0 actual)))
(|> (list (code.int (.int expected0)))
(/.run (/.maybe s.nat))
(match #.None
#1))))
(_.test "Can apply a parser 0 or more times."
(and (|> (list;map code.nat expected+)
(/.run (/.some s.nat))
(match actual
(:: (list.equivalence nat.equivalence) = expected+ actual)))
(|> (list;map (|>> .int code.int) expected+)
(/.run (/.some s.nat))
(match #.Nil
#1))))
(_.test "Can apply a parser 1 or more times."
(and (|> (list;map code.nat expected+)
(/.run (/.many s.nat))
(match actual
(:: (list.equivalence nat.equivalence) = expected+ actual)))
(|> (list (code.nat expected0))
(/.run (/.many s.nat))
(match (list actual)
(n/= expected0 actual)))
(|> (list;map (|>> .int code.int) expected+)
(/.run (/.many s.nat))
fails?)))
(_.test "Can use either parser."
(let [even (/.filter n/even? s.nat)
odd (/.filter n/odd? s.nat)]
(and (|> (list (code.nat even0))
(/.run (/.either even odd))
(match actual (n/= even0 actual)))
(|> (list (code.nat odd0))
(/.run (/.either even odd))
(match actual (n/= odd0 actual)))
(|> (list (code.bit not0))
(/.run (/.either even odd))
fails?))))
(_.test "Can create the opposite/negation of any parser."
(and (|> (list (code.nat expected0))
(/.run (/.not s.nat))
fails?)
(|> (list (code.bit not0))
(/.run (/.not s.nat))
(match [] #1))))
)))
(def: combinators-1
Test
(do r.monad
[failure (r.ascii 1)
variadic (:: @ map (|>> (n/max 1) (n/min 20)) r.nat)
times (:: @ map (n/% variadic) r.nat)
expected+ (r.list variadic r.nat)
separator (r.ascii 1)]
($_ _.and
(_.test "Can fail at will."
(|> (list)
(/.run (/.fail failure))
(should-fail failure)))
(_.test "Can apply a parser N times."
(and (|> (list;map code.nat expected+)
(/.run (/.exactly times s.nat))
(match actual
(:: (list.equivalence nat.equivalence) =
(list.take times expected+)
actual)))
(|> (list;map code.nat expected+)
(/.run (/.exactly (inc variadic) s.nat))
fails?)))
(_.test "Can apply a parser at-least N times."
(and (|> (list;map code.nat expected+)
(/.run (/.at-least times s.nat))
(match actual
(:: (list.equivalence nat.equivalence) =
expected+
actual)))
(|> (list;map code.nat expected+)
(/.run (/.at-least (inc variadic) s.nat))
fails?)))
(_.test "Can apply a parser at-most N times."
(and (|> (list;map code.nat expected+)
(/.run (/.at-most times s.nat))
(match actual
(:: (list.equivalence nat.equivalence) =
(list.take times expected+)
actual)))
(|> (list;map code.nat expected+)
(/.run (/.at-most (inc variadic) s.nat))
(match actual
(:: (list.equivalence nat.equivalence) =
expected+
actual)))))
(_.test "Can apply a parser between N and M times."
(and (|> (list;map code.nat expected+)
(/.run (/.between times variadic s.nat))
(match actual
(:: (list.equivalence nat.equivalence) =
expected+
actual)))
(|> (list;map code.nat (list.take times expected+))
(/.run (/.between times variadic s.nat))
(match actual
(:: (list.equivalence nat.equivalence) =
(list.take times expected+)
actual)))))
(_.test "Can parse while taking separators into account."
(|> (list.interpose (code.text separator) (list;map code.nat expected+))
(/.run (/.sep-by (s.this! (code.text separator)) s.nat))
(match actual
(:: (list.equivalence nat.equivalence) =
expected+
actual))))
(_.test "Can obtain the whole of the remaining input."
(|> (list;map code.nat expected+)
(/.run /.remaining)
(match actual
(:: (list.equivalence code.equivalence) =
(list;map code.nat expected+)
actual))))
)))
(def: (injection value)
(Injection (All [a i] (Parser i a)))
(:: /.monad wrap value))
(def: comparison
(Comparison (All [a i] (Parser i a)))
(function (_ == left right)
(case [(/.run left []) (/.run right [])]
[(#error.Success [_ left]) (#error.Success [_ right])]
(== left right)
_
false)))
(def: #export test
Test
(do r.monad
[assertion (r.ascii 1)]
(<| (_.context (%name (name-of /.Parser)))
($_ _.and
($functor.spec ..injection ..comparison /.functor)
($apply.spec ..injection ..comparison /.apply)
($monad.spec ..injection ..comparison /.monad)
(_.test "Can make assertions while parsing."
(and (|> (list (code.bit #1) (code.int +123))
(/.run (/.assert assertion #1))
(match [] #1))
(|> (list (code.bit #1) (code.int +123))
(/.run (/.assert assertion #0))
fails?)))
..combinators-0
..combinators-1
))))
|