-
Notifications
You must be signed in to change notification settings - Fork 0
/
private.zp
237 lines (211 loc) · 7.17 KB
/
private.zp
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
(define-syntax delay-parser
(syntax-rules ()
((_ label parser)
(lambda args
(let ((res (apply parser args)))
(monads:either res
(val => (monads:right (list (list label (car val)) (cadr val))))
(_ => res)))))
((_ parser)
(lambda args
(apply parser args)))))
(define-syntax define-parser
(syntax-rules ()
((_ parser label body)
(define parser
(delay-parser label body)))
((_ parser body)
(define parser
(delay-parser body)))))
(module "parsecomb"
(export
(list "succeed" succeed)
(list "fail" fail)
(list "skip" skip)
(list "either" either)
(list "seq" seq)
(list "seq*" seq*)
(list "sep-by" sep-by)
(list "char" char)
(list "unless" unless-)
(list "char-ci" char-ci)
(list "one-of" one-of)
(list "bin" bin)
(list "space" space)
(list "digit" digit)
(list "hex" hex)
(list "oct" oct)
(list "parse-all" parse)
(list "many" many)
(list "many1" many1)
(list "string" string))
(parse (lambda (parser str)
(let ((res (parser str)))
(monads:either res
(val =>
(if (falsy? (get-from val 1))
(monads:right val)
(monads:left (++ "Unconsumed input: " (get-from val 1)))))
(err =>
(monads:left (++ "Error in: " err)))))))
(memoize (lambda (f)
(let ((table (make-hash)))
(lambda x
(let ((previously-computed-result (table (make-simple-list x))))
(if (nil? previously-computed-result)
(let ((result (apply f x)))
(begin
(set! table (make-hash table (make-simple-list x) result))
result))
previously-computed-result))))))
(succeed (lambda (x y)
(monads:right (list x y))))
(fail (lambda (x) (monads:left x)))
(success-on-eq (lambda (a b rest str)
(if (eq? a b)
(succeed a rest)
(fail str))))
(skip
(lambda (parser)
(memoize
(lambda (str)
(let ((res (parser str)))
(monads:either res
(val => (monads:right (list '() (get-from val 1 ""))))
(err => (monads:left err))))))))
(either
(lambda (a b)
(memoize
(lambda (str)
(let ((result (a str)))
(monads:either result
(val => (monads:right val))
(_ => (b str))))))))
(one-of
(lambda args
(memoize
(lambda (str)
(if (null? args)
(monads:left str)
(let ((res ((head args) str)))
(monads:either res
(val => (monads:right val))
(_ => ((apply one-of (tail args)) str)))))))))
(seq
(lambda (a b)
(memoize
(lambda (str)
(let ((res (a str)))
((monads:bind res) (lambda (val)
(let ((res2 (b (get-from val 1 ""))))
((monads:bind res2) (lambda (val2)
(succeed (list (car val) (car val2)) (get-from val2 1 ""))))))))))))
(seq*
(lambda args
(memoize
(lambda (str)
(define (internal str state parsers)
(if (null? parsers)
(succeed (car state) (cadr state))
(if (null? str)
(fail str)
(let ((res ((head parsers) str)))
((monads:bind res) (lambda (val)
(internal (get-from val 1 "")
(list (++ (car state) (car val)) (get-from val 1 ""))
(tail parsers))))))))
(internal str (list '() str) args)))))
(many
(lambda (parser)
(memoize
(lambda (str)
(let loop ((before (monads:right (list "" str)))
(res (parser str))
(acc-res []))
(if (monads:right? res)
(let* ((mon-val (monads:get-val! res))
(value (cadr mon-val))
(parsed (car mon-val)))
(if (falsy? value)
(monads:right (++ acc-res parsed))
(loop res (parser value) (++ acc-res parsed))))
(if (monads:right? before)
(monads:right (list acc-res (cadr (monads:get-val! before))))
before)))))))
(many1
(lambda (parser)
(memoize
(lambda (str)
(let loop ((before (monads:left str))
(res (parser str))
(acc-res []))
(if (monads:right? res)
(let* ((mon-val (monads:get-val! res))
(value (cadr mon-val))
(parsed (car mon-val)))
(if (falsy? value)
(monads:right (++ acc-res parsed))
(loop res (parser value) (++ acc-res parsed))))
(if (monads:right? before)
(monads:right (list acc-res (cadr (monads:get-val! before))))
before)))))))
(sep-by
(lambda (parser sep)
(memoize
(lambda (str)
(let loop ((before (monads:right (list "" str)))
(res (parser str))
(acc-res []))
(if (monads:right? res)
(let* ((mon-val (monads:get-val! res))
(value (cadr mon-val))
(parsed (car mon-val)))
(if (falsy? value)
(monads:right (++ acc-res parsed))
(begin
(let ((sep-there? (sep value)))
(if (monads:right? sep-there?)
(loop res (parser (get-from (monads:get-val! sep-there?) 1 ""))
(++ acc-res parsed))
(succeed (++ acc-res parsed) value))))))
(if (monads:right? before)
(monads:right (list acc-res (cadr (monads:get-val! before))))
before)))))))
(char
(lambda (match)
(memoize
(lambda (str)
(success-on-eq (string:head str) match (string:tail str) str)))))
(unless-
(lambda (match)
(memoize
(lambda (str)
(if (eq? match (string:head str))
(fail str)
(succeed (string:head str) (string:tail str)))))))
(space (lambda ()
(memoize
(lambda (str)
(if (char:whitespace? (string:head str))
(succeed (string:head str) (string:tail str))
(fail str))))))
(char-ci
(lambda (match)
(either (char (char:lower-case match))
(char (char:upper-case match)))))
(bin (delay (either (char #\0) (char #\1))))
(oct (delay
(one-of (bin) (char #\2) (char #\3) (char #\4)
(char #\5) (char #\6) (char #\7))))
(digit (delay (one-of (oct) (char #\8) (char #\9))))
(hex (delay
(one-of (digit) (char-ci #\a) (char-ci #\b)
(char-ci #\c) (char-ci #\d))))
(string
(lambda (match)
(memoize
(lambda (str)
(let* ((len (min (string:length str) (string:length match)))
(h (substring str 0 len))
(t (substring str len (string:length str))))
(success-on-eq h match t str)))))))