-
Notifications
You must be signed in to change notification settings - Fork 0
/
pc.scm
458 lines (370 loc) · 11.1 KB
/
pc.scm
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
;;; pc.scm
;;; A simple implementation of parsing combinators
;;;
;;; Programmer: Mayer Goldberg, 2016
#lang scheme
(define *marker-length* 8)
(define with (lambda (s f) (apply f s)))
(define list-head
(lambda (s n)
(cond ((null? s) '())
((zero? n) '(#\space #\e #\t #\c))
(else (cons (car s)
(list-head (cdr s) (- n 1)))))))
(define <end-of-input>
(lambda (s ret-match ret-none)
(if (null? s)
(ret-match #t '())
(ret-none '()))))
(define const
(lambda (pred?)
(lambda (s ret-match ret-none)
(cond ((null? s) (ret-none '()))
((pred? (car s)) (ret-match (car s) (cdr s)))
(else (ret-none '()))))))
(define <epsilon>
(lambda (s ret-match ret-none)
(ret-match '() s)))
(define caten
(letrec ((binary-caten
(lambda (p1 p2)
(lambda (s ret-match ret-none)
(p1 s
(lambda (e1 s)
(p2 s
(lambda (e2 s)
(ret-match (cons e1 e2) s))
ret-none))
ret-none))))
(loop
(lambda (ps)
(if (null? ps)
<epsilon>
(binary-caten (car ps)
(loop (cdr ps)))))))
(lambda ps
(loop ps))))
(define <fail>
(lambda (s ret-match ret-none)
(ret-none '())))
(define disj
(letrec ((binary-disj
(lambda (p1 p2)
(lambda (s ret-match ret-none)
(p1 s ret-match
(lambda (w1)
(p2 s ret-match
(lambda (w2)
(ret-none `(,@w1 ,@w2)))))))))
(loop
(lambda (ps)
(if (null? ps)
<fail>
(binary-disj (car ps)
(loop (cdr ps)))))))
(lambda ps
(loop ps))))
(define delay
(lambda (thunk)
(lambda (s ret-match ret-none)
((thunk) s ret-match ret-none))))
(define star
(lambda (p)
(disj (pack-with (caten p (delay (lambda () (star p))))
cons)
<epsilon>)))
(define plus
(lambda (p)
(pack-with (caten p (star p))
cons)))
(define times
(lambda (<p> n)
(if (zero? n)
<epsilon>
(pack-with
(caten <p> (times <p> (- n 1)))
cons))))
(define pack
(lambda (p f)
(lambda (s ret-match ret-none)
(p s (lambda (e s) (ret-match (f e) s)) ret-none))))
(define pack-with
(lambda (p f)
(lambda (s ret-match ret-none)
(p s (lambda (e s) (ret-match (apply f e) s)) ret-none))))
(define diff
(lambda (p1 p2)
(lambda (s ret-match ret-none)
(p1 s
(lambda (e w)
(p2 s (lambda _ (ret-none '()))
(lambda (w1) (ret-match e w))))
ret-none))))
(define maybe
(lambda (p)
(lambda (s ret-match ret-none)
(p s
(lambda (e s) (ret-match `(#t ,e) s))
(lambda (w) (ret-match `(#f #f) s))))))
(define maybe?
(lambda (?result)
(car ?result)))
(define maybe->value
(lambda (?result)
(cadr ?result)))
(define fence
(lambda (p pred?)
(lambda (s ret-match ret-none)
(p s
(lambda (e s)
(if (pred? e)
(ret-match e s)
(ret-none '())))
ret-none))))
(define otherwise
(lambda (p message)
(lambda (s ret-match ret-none)
(p s
ret-match
(let ((marker
(format "-->[~a]"
(list->string
(list-head s *marker-length*)))))
(lambda (w) (ret-none `(,@w ,message ,marker))))))))
;;;
(define ^char
(lambda (char=?)
(lambda (character)
(const
(lambda (ch)
(char=? ch character))))))
(define char (^char char=?))
(define char-ci (^char char-ci=?))
(define ^word
(lambda (char)
(lambda (word)
(apply caten (map char (string->list word))))))
(define word (^word char))
(define word-ci (^word char-ci))
(define ^word-suffixes
(lambda (char)
(letrec ((loop
(lambda (s)
(if (null? s)
<epsilon>
(maybe
(caten (char (car s))
(loop (cdr s))))))))
(lambda (suffix)
(loop (string->list suffix))))))
(define word-suffixes (^word-suffixes char))
(define word-suffixes-ci (^word-suffixes char-ci))
(define ^word+suffixes
(lambda (word-suffixes)
(lambda (prefix suffix)
(caten (word prefix)
(word-suffixes suffix)))))
(define word+suffixes (^word+suffixes word-suffixes))
(define word+suffixes-ci (^word+suffixes word-suffixes-ci))
(define ^one-of
(lambda (char)
(lambda (word)
(apply disj (map char (string->list word))))))
(define one-of (^one-of char))
(define one-of-ci (^one-of char-ci))
(define ^range
(lambda (char<=?)
(lambda (char-from char-to)
(const
(lambda (ch)
(and (char<=? char-from ch)
(char<=? ch char-to)))))))
(define range (^range char<=?))
(define range-ci (^range char-ci<=?))
(define <any-char> (const (lambda (ch) #t)))
(define <any> <any-char>)
;;; <expr> {<sep> <expr>}*
(define ^<separated-exprs>
(lambda (<expr> <sep>)
(new (*parser <expr>)
(*parser <sep>)
(*parser <expr>)
(*caten 2)
(*pack-with (lambda (_sep expr) expr))
*star
(*caten 2)
(*pack-with cons)
done)))
;;;
(define continue
(lambda (ds cs)
(with cs
(lambda (c . cs)
(c ds cs)))))
(define new
(lambda cs
(continue '() cs)))
(define done
(lambda (ds cs)
(with ds
(lambda (parser . ds)
(if (null? ds)
parser
(error 'done
(format "The parser stack still contains ~a parsers!"
(length ds))))))))
(define *parser
(lambda (p)
(lambda (ds cs)
(continue `(,p . ,ds) cs))))
(define unary
(lambda (f-unary)
(lambda (ds cs)
(with ds
(lambda (d . ds)
(continue `(,(f-unary d) . ,ds) cs))))))
(define *delayed
(lambda (thunk)
(lambda (ds cs)
(continue `(,(delay thunk) . ,ds) cs))))
(define binary
(lambda (f-binary)
(lambda (ds cs)
(with ds
(lambda (d2 d1 . ds)
(continue `(,(f-binary d1 d2) . ,ds) cs))))))
(define *dup
(lambda (ds cs)
(with ds
(lambda (d1 . ds)
(continue `(,d1 ,d1 . ,ds) cs)))))
(define *swap
(lambda (ds cs)
(with ds
(lambda (d1 d2 . ds)
(continue `(,d2 ,d1 . ,ds) cs)))))
(define *star (unary star))
(define *plus (unary plus))
(define *diff (binary diff))
(define *pack (lambda (f) (unary (lambda (p) (pack p f)))))
(define *pack-with (lambda (f) (unary (lambda (p) (pack-with p f)))))
(define *fence (lambda (pred?) (unary (lambda (p) (fence p pred?)))))
(define *guard (lambda (pred?) (unary (lambda (p) (fence p pred?)))))
(define split-list
(lambda (s n ret-s1+s2)
(if (zero? n)
(ret-s1+s2 '() s)
(split-list (cdr s) (- n 1)
(lambda (s1 s2)
(ret-s1+s2 (cons (car s) s1) s2))))))
(define nary
(lambda (f-n-ary n)
(lambda (ds cs)
(split-list ds n
(lambda (sgra ds)
(continue
`(,(apply f-n-ary (reverse sgra)) . ,ds) cs))))))
(define *caten (lambda (n) (nary caten n)))
(define *disj (lambda (n) (nary disj n)))
(define *maybe (unary maybe))
(define *otherwise
(lambda (string)
(unary
(lambda (p)
(otherwise p string)))))
(define *times
(lambda (n)
(unary
(lambda (<p>)
(times <p> n)))))
(define not-followed-by
(lambda (<p1> <p2>)
(new (*parser <p1>)
(*parser <p2>) *maybe
(*caten 2)
(*pack-with
(lambda (e1 ?e2)
(with ?e2
(lambda (found-e2? _)
`(,e1 ,found-e2?)))))
(*guard
(lambda (e1+found-e2?)
(with e1+found-e2?
(lambda (_ found-e2?)
(not found-e2?)))))
(*pack-with
(lambda (e1 _) e1))
done)))
(define *not-followed-by (binary not-followed-by))
(define *transformer
(lambda (^<p>)
(unary (lambda (<p>) (^<p> <p>)))))
;;;
(define test-string
(lambda (parser string)
(parser (string->list string)
(lambda (e s)
`((match ,e)
(remaining ,(list->string s))))
(lambda (w) `(failed with report: ,@w)))))
(define test
(lambda (parser s)
(parser s
(lambda (e s)
`((match ,e)
(remaining ,s)))
(lambda (w) `(failed with report: ,@w)))))
;;;
(define file->string
(lambda (filename)
(let ((input (open-input-file filename)))
(letrec ((run
(lambda ()
(let ((e (read-char input)))
(if (eof-object? e)
(begin
(close-input-port input)
'())
(cons e (run)))))))
(list->string (run))))))
(define read-stdin-to
(lambda (end-of-input)
(let ((end-of-input-list (string->list end-of-input)))
(letrec ((state-init
(lambda (seen)
(let ((ch (read-char)))
(cond ((eof-object? ch)
(error 'read-stdin-to
(format "Marker ~a not reached"
end-of-input)))
((char=? ch (car end-of-input-list))
(state-seen seen `(,ch) (cdr end-of-input-list)))
(else (state-init `(,ch ,@seen)))))))
(state-seen
(lambda (seen-before seen-now end-of-input-list-rest)
(if (null? end-of-input-list-rest)
(list->string
(reverse seen-before))
(let ((ch (read-char)))
(cond ((eof-object? ch)
(format "Marker ~a not reached"
end-of-input))
((char=? ch (car end-of-input-list-rest))
(state-seen seen-before
`(,ch ,@seen-now)
(cdr end-of-input-list-rest)))
(else (state-init
`(,ch ,@seen-now ,@seen-before)))))))))
(state-init '())))))
;;; end-of-input
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; start of compiler.scm ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define <boolean>
(new (*parser (char #\#))
(*parser (char #\t))
(*parser (char #\T))
(*parser (char #\f))
(*parser (char #\F))
(*disj 4)
(*caten 2)
done))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; end of compiler.scm ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;