-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedn-impl.scm
309 lines (269 loc) · 9.05 KB
/
edn-impl.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
;; EDN Reading
;; ===========
(define edn->atom
(case-lambda
((skip-fn end-fn finalizer) (lambda (subparser input)
(edn->atom subparser skip-fn end-fn finalizer '() '() input)))
((subparser skip-fn end-fn finalizer result pile input)
(cond ((or (eq? #!eof (peek-char input))
(end-fn result pile input))
(cons (finalizer (reverse result))
(if (or (not (char-ready? input))
(char=? #\# (peek-char input))
(char=? #\) (peek-char input))
(char=? #\] (peek-char input))
(char=? #\} (peek-char input)))
input
(begin (read-char input) input))))
((skip-fn result pile input)
(edn->atom subparser skip-fn end-fn finalizer result (cons (read-char input) pile) input))
(else (edn->atom subparser skip-fn end-fn finalizer (cons (peek-char input) result) (cons (peek-char input) pile)
(if (null? input) input (begin (read-char input) input))))))))
(define edn->string
(edn->atom (lambda (result pile input)
(or (char=? #\\ (peek-char input))
(and (null? result)
(char=? #\" (peek-char input)))))
(lambda (result pile input)
(and (char=? #\" (peek-char input))
(not (null? pile))
(or (not (char=? #\\ (car pile)))
(char=? #\" (car pile)))))
list->string))
(define edn->keyword
(edn->atom (lambda (result pile input)
(char=? #\: (peek-char input)))
(lambda (result pile input)
(or (char-whitespace? (peek-char input))
(char=? #\# (peek-char input))
(char=? #\, (peek-char input))
(char=? #\) (peek-char input))
(char=? #\] (peek-char input))
(char=? #\} (peek-char input))))
(lambda (in) (string->keyword (list->string in)))))
(define edn->symbol
(edn->atom (lambda (result pile input) #f)
(lambda (result pile input)
(or (char-whitespace? (peek-char input))
(char=? #\# (peek-char input))
(char=? #\, (peek-char input))
(char=? #\) (peek-char input))
(char=? #\] (peek-char input))
(char=? #\} (peek-char input))))
(lambda (in) (let ((res-string (list->string in)))
(cond
((equal? "true" res-string) #t)
((equal? "false" res-string) #f)
((equal? "nil" res-string) '())
(else (string->symbol res-string)))))))
(define edn->number
(edn->atom (lambda (result pile input) #f)
(lambda (result pile input)
(or (char-whitespace? (peek-char input))
(char=? #\# (peek-char input))
(char=? #\M (peek-char input))
(char=? #\N (peek-char input))
(char=? #\, (peek-char input))
(char=? #\) (peek-char input))
(char=? #\] (peek-char input))
(char=? #\} (peek-char input))))
(lambda (in) (string->number (list->string in)))))
(define edn->rtag
(edn->atom (lambda (result pile input)
(char=? #\# (peek-char input)))
(lambda (result pile input)
(or (char-whitespace? (peek-char input))
(char=? #\( (peek-char input))
(char=? #\[ (peek-char input))
(and (not (null? pile))
(char=? #\{ (car pile)))))
(lambda (in) (cons edn/tag: (string->keyword (list->string in))))))
(define edn->coll
(case-lambda
((ld rd finalize) (lambda (subparser input) (edn->coll subparser ld rd finalize '() input #t)))
((subparser ld rd finalize result input fresh?)
(cond
;; End of sequence
((or (eq? #!eof (peek-char input))
(char=? rd (peek-char input)))
(cons (finalize (reverse result)) (begin (read-char input) input)))
;; First character of sequence
((and (char=? ld (peek-char input))
fresh?)
(edn->coll subparser ld rd finalize result (begin (read-char input) input) #f))
;; Sub-sequence of same type
((char=? ld (peek-char input))
(let ((sub-result (subparser input)))
(edn->coll subparser ld rd finalize (cons (cadr sub-result) result) (caddr sub-result) #f)))
;; Stuff in the data!
(else (let ((compiled (subparser input)))
(edn->coll (first compiled)
ld rd finalize
(if (equal? (second compiled) edn/omit:)
result
(cons (second compiled) result))
(third compiled) #f)))))))
(define edn->list (edn->coll #\( #\) (lambda (x) x)))
(define edn->vector (edn->coll #\[ #\] (lambda (x) (list->vector x))))
(define edn->htable
(case-lambda
((subparser input) (edn->htable subparser (make-hash-table) '() input #t))
((subparser result key input fresh?)
(cond ((or (eq? #!eof (peek-char input))
(char=? #\} (peek-char input)))
(cons result (begin (read-char input) input)))
((and (char=? #\{ (peek-char input))
fresh?)
(edn->htable subparser result key (begin (read-char input) input) #f))
(else (let ((compiled (subparser input)))
(cond
((eq? edn/omit: (second compiled))
(edn->htable (first compiled) result key (third compiled) #f))
((null? key)
(edn->htable (first compiled) result (second compiled) (third compiled) #f))
(else
(edn->htable (first compiled) (begin (hash-table-set! result key (second compiled)) result)
'() (third compiled) #f)))))))))
(define (edn->whitespace subparser input)
(if (char-whitespace? (peek-char input))
(cons edn/omit: (begin (read-char input) input))
(cons (read-char input) input)))
(define (is-char? a)
(lambda (b)
(and (char? b)
(char=? a b))))
(define (is-number? c)
(or (char-numeric? c)
(char=? #\+ c)
(char=? #\- c)))
(define (guard-charcheck fun)
(lambda (x)
(and (char? x)
(fun x))))
(define tag-handlers
(list (cons _: (lambda (input) edn/omit:))))
(define reader-handlers
(list (cons (is-char? #\() edn->list)
(cons (is-char? #\)) edn->list)
(cons (is-char? #\[) edn->vector)
(cons (is-char? #\]) edn->vector)
(cons (is-char? #\{) edn->htable)
(cons (is-char? #\}) edn->htable)
(cons (is-char? #\#) edn->rtag)
(cons (is-char? #\:) edn->keyword)
(cons (is-char? #\") edn->string)
(cons (guard-charcheck char-alphabetic?) edn->symbol)
(cons (guard-charcheck is-number?) edn->number)
(cons (guard-charcheck char-whitespace?) edn->whitespace)))
(define (is-tag? in)
(and (pair? in)
(pair? (car in))
(equal? (caar in) edn/tag:)
(contains-tag-handler? (car in))))
(define (contains-tag-handler? tag)
(assoc (cdr tag) tag-handlers))
(define (call-tag tag data)
((cdr (assoc (cdr tag) tag-handlers)) data))
(define (parse-edn state)
(lambda (in-port)
(let* ((struct-handler (cdr
(find (lambda (item) ((car item) (peek-char in-port)))
reader-handlers)))
(result (struct-handler (parse-edn state) in-port)))
(list (if (is-tag? result)
(parse-edn result)
(parse-edn '()))
(cond ((is-tag? state)
(call-tag (car state) (car result)))
((is-tag? result)
edn/omit:)
(else (car result)))
(cdr result)))))
(define (read-edn)
(second ((parse-edn '()) (current-input-port))))
;; EDN writing
;; ===========
(define (pair->reader-tag subparser in)
(string-append "#" (keyword->string (cdr in))))
(define (scm-kw->edn-kw subparser in)
(string-append ":" (keyword->string in)))
(define (boolean->edn subparser in)
(case in
((#t) "true")
((#f) "false")
(else "nil")))
(define (char->edn subparser in)
(string #\\ in))
(define (string->edn subparser in)
(string-append "\"" in "\""))
(define (number->edn subparser in)
(number->string in))
(define (sequential->edn subparser ld rd in)
(string-append ld
(foldr (lambda (elem init)
(string-append (subparser elem)
(if (equal? "" init) "" " ")
init))
"" in)
rd))
(define (list->edn subparser in)
(sequential->edn subparser "(" ")" in))
(define (vector->edn subparser in)
(sequential->edn subparser "[" "]" (vector->list in)))
(define (map->edn subparser in)
(string-append "{"
(foldr (lambda (elem init)
(string-append (subparser (car elem))
" "
(subparser (cdr elem))
(if (equal? "" init) "" " ")
init))
"" in)
"}"))
(define (htable->edn subparser in)
(string-append "{"
(hash-table-fold in
(lambda (hkey hval folded)
(string-append (subparser hkey)
" "
(subparser hval)
(if (equal? "" folded) "" " ")
folded))
"")
"}"))
(define (nil->edn subparser in)
"nil")
(define (symbol->edn subparser in)
(symbol->string in))
(define (edn-readertag? in)
(and
(not (list? in))
(pair? in)
(equal? edn/reader-tag: (car in))))
(define (edn-alist? in)
(and (list? in)
(any (lambda (item) (and (not (list? item)) (pair? item)))
in)))
(define (edn-htable? in)
(hash-table? in))
(define writer-handlers
(list (cons null? nil->edn)
(cons string? string->edn)
(cons char? char->edn)
(cons boolean? boolean->edn)
(cons number? number->edn)
(cons keyword? scm-kw->edn-kw)
(cons symbol? symbol->edn)
(cons vector? vector->edn)
(cons edn-alist? map->edn)
(cons edn-htable? htable->edn)
(cons edn-readertag? pair->reader-tag)
(cons list? list->edn)))
(define (parse-entry in)
((cdr
(find (lambda (item) ((car item) in))
writer-handlers))
parse-entry in))
(define (write-edn struct)
(lambda ()
(display (parse-entry struct) (current-output-port))))