-
Notifications
You must be signed in to change notification settings - Fork 0
/
match0.arc
370 lines (295 loc) · 8.07 KB
/
match0.arc
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
(implicit pos*)
(implicit fail)
; failf must be called *outside* the w/fail scope,
; thus the code to pass the normal/fail result of calling f
; out through the w/fail
(def onmatch-impl (matcher successf failf)
(with (return-value nil failed nil)
(catch
(w/fail (fn ()
(assign failed t)
(throw nil))
(assign return-value (matcher))))
(if failed
(failf)
(successf return-value))))
(mac onmatch (var matcher onsuccess onfail)
`(onmatch-impl (fn () ,matcher)
(fn (,var) ,onsuccess)
(fn () ,onfail)))
(mac onfail (onfail . body)
(w/uniq gv
`(onmatch ,gv (do ,@body)
,gv
,onfail)))
(def at-impl (matcher)
(let mark pos*
(after (matcher)
(assign pos* mark))))
(mac at body
`(at-impl (fn () ,@body)))
(def try-impl (matcher successf failf)
(let mark pos*
(onmatch r (matcher)
(successf r)
(do (assign pos* mark)
(failf)))))
(mac try (v matcher successf failf)
`(try-impl (fn () ,matcher)
(fn (,v) ,successf)
(fn () ,failf)))
(def ascons (x)
(if (or (no x) (acons x))
x
; todo
;(isa x 'input)
; (drain (readc x))
(isa x 'string)
(coerce x 'cons)
(err "don't know how to parse" x)))
(def fmatch (seq body)
(onfail nil
(w/pos* (ascons seq)
(body))))
(mac match (s . body)
`(fmatch ,s (fn () ,@body)))
(mac test-matchpos (input matcher expected)
`(testis (ascons ,expected)
(match ,input (do ,matcher) pos*)))
(mac test-match1 (str matcher expected)
`(testf ,str (fn (_)
(match _ (onfail '<<parse-fail>> ,matcher)))
,matcher
,expected))
(mac test-match (matcher . body)
`(do ,@(map1 (fn (a)
(with (input (car a) expected (cadr a))
`(test-match1 ,input ,matcher ,expected)))
(pair body))))
; todo fail this?
(def at-end ()
(no pos*))
; todo rename?
(def fail-at-end ()
(if (at-end) (fail)))
(def next ()
(fail-at-end)
(do1 (car pos*)
(assign pos* (cdr pos*))))
(def one (item)
(ret v (next)
(unless ((testify item) v) (fail))))
(test-match (one #\a)
"abc" #\a
"xyz" '<<parse-fail>>)
(test-match (one (fn (_) (is _ #\a)))
"abc" '#\a
"#bc" '<<parse-fail>>)
(test-match (one (fn (x) t))
"" '<<parse-fail>>)
(test-marker
(match "abcd"
(test-iso (at (one (fn (_) (is _ #\a)))) #\a)
(test-iso pos* '(#\a #\b #\c #\d))))
(test-match (do (next)
(try x (n-of 2 (one letter))
'didnt-expect-success
pos*))
"ab123" '(#\b #\1 #\2 #\3))
; todo: confusing name
(def onenot (item)
(ret v (next)
(unless ((complement (testify item)) v) (fail))))
; don't have [ _ ] yet
(def oneof items
(ret v (next)
(unless (some (fn (_) ((testify _) v)) items) (fail))))
(test-match (oneof #\a #\b #\c)
"a56" #\a
"c56" #\c
"56" '<<parse-fail>>)
(def falt2 (f1 f2)
(let mark pos*
(onfail
(do (assign pos* mark)
(f2))
(f1))))
(mac alt2 (p1 p2)
`(falt2 (fn () ,p1) (fn () ,p2)))
(mac alt ps
(if (no ps)
`(fail)
(no (cdr ps))
(car ps)
`(alt2 ,(car ps) (alt ,@(cdr ps)))))
(test-match (alt) "greetings" '<<parse-fail>>)
(mac optional body
`(alt (do ,@body) nil))
(def mliteral1 (pat val)
(let pat (ascons pat)
(if (begins pos* pat)
(do (assign pos* (nthcdr (len pat) pos*))
val)
(fail))))
; todo don't like the name "mliteral"
(def mliteral args
(if (no args)
(fail)
(no (cdr args))
(mliteral1 (car args) (car args))
(alt (mliteral1 (car args) (cadr args))
(apply mliteral (cddr args)))))
(test-match (mliteral "xyz")
"greetings" '<<parse-fail>>)
(test-match (mliteral "greet")
"greetings" "greet")
(test-match (list (mliteral "greet")
(mliteral "ings"))
"greetings" '("greet" "ings"))
(test-match (list (mliteral "greetx") (mliteral "ings"))
"greetings" '<<parse-fail>>)
(test-match (list (mliteral "greet") (mliteral "xings"))
"greetings" '<<parse-fail>>)
(test-match (alt (mliteral "greet")) "greeting" "greet")
(test-match (alt (mliteral "greet")
(mliteral "foo"))
"greetings" "greet")
(test-match (alt (mliteral "foo")
(mliteral "greet"))
"greetings" "greet")
(test-match (alt (mliteral "foo")
(mliteral "bar")
(mliteral "greet"))
"greetings" "greet")
(test-match (list (mliteral "greet")
(optional (mliteral "ings")))
"greetings" '("greet" "ings"))
(test-match (list (mliteral "greet")
(optional (mliteral "xyz")))
"greetings" '("greet" nil))
(def rfmany (matchf acc)
(try v (matchf)
(rfmany matchf (cons v acc))
acc))
(def fmany (matchf)
(rev (rfmany matchf nil)))
(mac many body
`(fmany (fn () ,@body)))
(test-match (many (mliteral "x"))
"xxxyz" '("x" "x" "x"))
(test-match (list (many (mliteral "x"))
(mliteral "y"))
"xxxyz" '(("x" "x" "x") "y")
"yzzz" '(nil "y")
"xxxyzzz" '(("x" "x" "x") "y"))
(def manyis (v)
(many (one v)))
(def manyisnt (v)
(let test (complement (testify v))
(many (one test))))
(mac many1 parser
`(cons (do ,@parser) (many (do ,@parser))))
(test-match (list (many1 (mliteral "x"))
(mliteral "y"))
"yzzz" '<<parse-fail>>)
;; todo don't like this name either
(def many1is (v)
(let test (testify v)
(many1 (one test))))
(mac must (errmsg . body)
`(onfail (err ,errmsg)
,@body))
(test-match (must "want cookie!" (mliteral "cookie"))
"xyzzy" (makeerr "want cookie!"))
(mac parse-intersperse (separator parser must-message)
`(optional
(cons ,parser
(many ,separator
(must ,must-message ,parser)))))
(test-match (parse-intersperse
(one #\,)
(one alphadig)
"comma must be followed by alphadig")
"a,b,c" '(#\a #\b #\c))
(def skipwhite ()
(manyis whitec))
(test-match (do (skipwhite)
(one #\a))
" abc" #\a)
(mac comma-separated (parser (o must-message "a comma must be followed by a value"))
`(parse-intersperse
(do (skipwhite) (one #\,))
,parser
,must-message))
(test-match (comma-separated
(one #\a)
"comma must be followed by 'a'")
"a ,b ,c"
(makeerr "comma must be followed by 'a'"))
(test-match (comma-separated
(one alphadig)
"comma must be followed by alphadig")
"a ,b ,c" '(#\a #\b #\c))
(mac entire body
`(do1 (do ,@body)
(unless (at-end) (fail))))
(test-match (entire (mliteral "abc")) "abc" "abc")
(test-match (entire (mliteral "ab")) "abc" '<<parse-fail>>)
(def fmatched-input (matcher)
(let mark pos*
(matcher)
(accum a
(xloop (p mark)
(when (and p (isnt p pos*))
(a (car p))
(next (cdr p)))))))
(mac matched-input body
`(fmatched-input (fn () ,@body)))
(test-match (matched-input (one #\a) (one #\b) (one #\c))
"abcd" '(#\a #\b #\c))
(mac not body
(w/uniq v
`(try ,v (do ,@body)
(fail)
t)))
(test-match (do (mliteral "ab")
(not (one #\!))
(mliteral "c"))
"abc" "c")
(test-match (do (mliteral "ab")
(not (one #\!)))
"ab" t
"ab!c" '<<parse-fail>>)
(def fupto (n parser)
(if (< n 1)
'()
(try v (parser)
(cons v (fupto (- n 1) parser))
nil)))
(mac upto (n . body)
`(fupto ,n (fn () ,@body)))
(test-match (upto 3 (one #\a))
"b" '()
"ab" '(#\a)
"aaab" '(#\a #\a #\a)
"aaaab" '(#\a #\a #\a))
(def ffrom1upto (n parser)
(or (upto n (parser))
(fail)))
(mac from1upto (n . body)
`(ffrom1upto ,n (fn () ,@body)))
(test-match (from1upto 3 (one #\a))
"" '<<parse-fail>>
"b" '<<parse-fail>>
"ab" '(#\a)
"aaab" '(#\a #\a #\a)
"aaaab" '(#\a #\a #\a))
(mac defalt (name . body)
(if (bound name)
(w/uniq orig
`(let ,orig ,name
(assign ,name
(fn ()
(alt (do ,@body)
(,orig))))))
`(def ,name () ,@body)))