-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsyrup.rkt
More file actions
421 lines (395 loc) · 13.3 KB
/
Copy pathsyrup.rkt
File metadata and controls
421 lines (395 loc) · 13.3 KB
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
#lang racket/base
(provide record*
syrup-encode syrup-decode
syrup-read syrup-write)
(require racket/match
racket/set
racket/contract)
;; For whatever reason struct/contract is dramatically faster
;; than using #:guard on a struct, but then I can't use #:methods
;; to define a custom printer... :\
(struct record (label args)
#:transparent)
(provide
(contract-out
[struct record ((label any/c) (args list?))]))
(define (record* label . args)
(record label args))
(define (netstring-encode bstr #:joiner [joiner #":"])
(bytes-append (string->bytes/latin-1 (number->string (bytes-length bstr)))
joiner
bstr))
;; Booleans: t or f
;; Single flonum: F<ieee-single-float> (big endian)
;; Double flonum: D<ieee-double-float> (big endian)
;; Positive integer: <int>+
;; Negative integer: <int>-
;; Bytestrings: 3:cat
;; Strings: 3"cat
;; Symbols: 3'cat
;; Dictionary: {<key1><val1><key2><val2>}
;; Lists: [<item1><item2><item3>]
;; Records: <<label><val1><val2><val3>> (the outer <> for realsies tho)
;; Sets: #<item1><item2><item3>$
(define (syrup-encode obj
;; an alist of (predicate . marshaller)... translates unknown
;; object into a representation we can understand
#:marshallers [marshallers '()])
(define (encode obj)
(match obj
;; Bytes are like <bytes-len>:<bytes>
[(? bytes?)
(netstring-encode obj)]
;; Integers are like <integer>+ or <integer>-
[0 #"0+"]
[(? integer?)
(if (positive? obj)
(bytes-append (string->bytes/latin-1 (number->string obj)) #"+")
(bytes-append (string->bytes/latin-1 (number->string (* obj -1))) #"-"))]
;; Lists are like [<item1><item2><item3>]
[(? list?)
(bytes-append #"["
(apply bytes-append
(map encode obj))
#"]")]
;; Dictionaries are like {<key1><val1><key2><val2>}
;; We sort by the key being fully encoded.
[(? hash?)
(define keys-and-encoded
(for/list ([key (hash-keys obj)])
(cons (encode key) key)))
(define sorted-keys-and-encoded
(sort keys-and-encoded
(match-lambda*
[(list (and ke1 (cons encoded1 _k1))
(and ke2 (cons encoded2 _k2)))
(bytes<? encoded1 encoded2)])))
(define encoded-hash-pairs
(for/list ([ke sorted-keys-and-encoded])
(match ke
[(cons enc-key key)
(define val
(hash-ref obj key))
(bytes-append enc-key (encode val))])))
(bytes-append #"{"
(apply bytes-append encoded-hash-pairs)
#"}")]
;; Strings are like <encoded-bytes-len>"<utf8-encoded>
[(? string?)
(netstring-encode (string->bytes/utf-8 obj)
#:joiner #"\"")]
;; Symbols are like <encoded-bytes-len>'<utf8-encoded>
[(? symbol?)
(netstring-encode (string->bytes/utf-8
(symbol->string obj))
#:joiner #"'")]
;; Single flonum floats are like F<big-endian-encoded-single-float>
[(? single-flonum?)
(bytes-append #"F"
(real->floating-point-bytes obj 4 #t))]
;; Double flonum floats are like D<big-endian-encoded-double-float>
[(? double-flonum?)
(bytes-append #"D"
(real->floating-point-bytes obj 8 #t))]
;; Records are like <<tag><arg1><arg2>> but with the outer <> for realsies
[(? record?)
(bytes-append #"<"
(encode (record-label obj))
(apply bytes-append
(map encode (record-args obj)))
#">")]
;; #t is t, #f is f
[#t #"t"]
[#f #"f"]
;; Sets are like #<item1><item2><item3>$
[(? set?)
(define encoded-items
(for/list ([item obj])
(encode item)))
(define sorted-items
(sort encoded-items
bytes<?))
(bytes-append #"#"
(apply bytes-append sorted-items)
#"$")]
[_
(call/ec
(lambda (return)
(for ([marshaller marshallers])
(match marshaller
[(cons handles-it? translate)
(when (handles-it? obj)
(define translated (translate obj))
(if (record? translated)
(return (encode translated))
(error 'syrup-marshaller-returned-unsupported-type)))]))
(error 'syrup-unsupported-type
"~a"
obj)))]))
(encode obj))
(define (syrup-write obj op #:marshallers [marshallers '()])
(write-bytes (syrup-encode obj #:marshallers marshallers)
op))
(define digit-chars
(seteq #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9))
(define whitespace-chars
(seteq #\space #\tab #\newline))
(define (digit-char? char)
(set-member? digit-chars char))
(define (syrup-read in-port
;; inverse of syrup-encode's marshallers;
;; alist of (label-pred? . derecorify)
#:unmarshallers [unmarshallers '()])
(call/ec
(lambda (return-early)
(define (return-eof)
(return-early eof))
(define (_read-char)
(match (read-char in-port)
[(? eof-object?) (return-eof)]
[char char]))
(define (_peek-char)
(match (peek-char in-port)
[(? eof-object?) (return-eof)]
[char char]))
(define (read-next)
;; consume whitespace
(let lp ()
(when (set-member? whitespace-chars (_peek-char))
(_read-char)
(lp)))
(match (_peek-char)
;; it's either a bytestring, a symbol, a string, or an integer...
;; we tell via the divider
[(? digit-char?)
(define type #f)
(define int-prefix
(string->number
(list->string
(let lp ()
(match (_read-char)
;; Oh, it's a plus... that means it's a positive
;; integer. Ok.
[#\+
(set! type 'positive-int)
'()]
;; Or the inverse for a minus.
[#\-
(set! type 'negative-int)
'()]
[#\:
(set! type 'bstr)
'()]
[#\'
(set! type 'sym)
'()]
[#\"
(set! type 'str)
'()]
[(? digit-char? digit-char)
(cons digit-char
(lp))]
[other-char
(error 'syrup-invalid-digit
"Invalid digit at pos ~a: ~a"
(file-position in-port)
other-char)])))))
(match type
;; it's positive, so just return as-is
['positive-int int-prefix]
;; it's negative, so invert
['negative-int (* int-prefix -1)]
;; otherwise it's some byte-length thing
[_
(define bstr (read-bytes int-prefix in-port))
(match type
['bstr
bstr]
['sym
(string->symbol (bytes->string/utf-8 bstr))]
['str
(bytes->string/utf-8 bstr)])])]
;; it's a list
[(or #\[ #\( #\l)
(read-byte in-port)
(let lp ()
(match (_peek-char)
;; We've reached the end
[(or #\] #\) #\e)
(read-byte in-port)
'()]
;; one more loop
[_
(cons (read-next) (lp))]))]
;; it's a hashmap/dictionary
[(or #\{ #\d)
(read-byte in-port)
(let lp ([ht #hash()])
(match (_peek-char)
[(or #\} #\e)
(read-byte in-port)
ht]
[_
(define key
(read-next))
(define val
(read-next))
(lp (hash-set ht key val))]))]
;; it's a record
[#\<
(read-byte in-port)
(define label
(read-next))
(define args
(let lp ()
(match (_peek-char)
[#\>
(read-byte in-port)
'()]
[_ (cons (read-next) (lp))])))
(call/ec
(lambda (return)
(for ([unmarshaller unmarshallers])
(match unmarshaller
[(cons (and (? (or/c symbol? string? number? boolean? bytes?))
expected-label)
derecordify)
(when (equal? label expected-label)
(return (apply derecordify args)))]
[(cons label-pred? derecordify)
(when (label-pred? label)
(return (apply derecordify args)))]))
;; no handler, return as record
(record label args)))]
;; it's a single float
[#\F
(read-byte in-port)
(let ([val (floating-point-bytes->real (read-bytes 4 in-port) #t)])
(unless (real? val)
(error 'not-a-real-number val))
val)]
;; it's a double float
[#\D
(read-byte in-port)
(let ([val (floating-point-bytes->real (read-bytes 8 in-port) #t)])
(unless (real? val)
(error 'not-a-real-number val))
val)]
;; it's a boolean
[#\t
(read-byte in-port)
#t]
[#\f
(read-byte in-port)
#f]
;; it's a set
[#\#
(read-byte in-port)
(let lp ([s (set)])
(match (_peek-char)
[#\$
(read-byte in-port)
s]
[_
(lp (set-add s (read-next)))]))]
[_
(error 'syrup-invalid-char "Unexpected character at position ~a: ~a"
(file-position in-port)
(_peek-char))]))
(read-next))))
(define (syrup-decode bstr #:unmarshallers [unmarshallers '()])
(syrup-read (open-input-bytes bstr)
#:unmarshallers unmarshallers))
(module+ test
(require rackunit
racket/runtime-path
racket/port)
(define-runtime-path pwd
".")
(test-equal?
"eof anywhere in a syrup-read is an eof"
(call-with-input-bytes
#"[3:foo"
(lambda (ip)
(syrup-read ip)))
eof)
(define zoo-structure
(record* #"zoo"
"The Grand Menagerie"
`(#hash((species . #"cat")
(name . "Tabatha")
(age . 12)
(weight . 8.2)
(alive? . #t)
(eats . ,(set #"mice" #"fish" #"kibble")))
#hash((species . #"monkey")
(name . "George")
(age . 6)
(weight . 17.24)
(alive? . #f)
(eats . ,(set #"bananas" #"insects")))
#hash((species . #"ghost")
(name . "Casper")
(age . -12)
(weight . -34.5)
(alive? . #f)
(eats . ,(set))))))
(define zoo-expected-bytes
(call-with-input-file (build-path pwd ".." ".." ".." "test-data" "zoo.bin")
port->bytes))
(test-equal?
"Correctly encodes zoo structure"
(syrup-encode zoo-structure)
zoo-expected-bytes)
(test-equal?
"Correctly decodes zoo structure"
(syrup-decode zoo-expected-bytes)
zoo-structure)
(test-equal?
"Ignore whitespace"
(syrup-decode #"
<3:zoo 19\"The Grand Menagerie
[{3'age 12+
4'eats #4:fish
4:mice
6:kibble$
4'name 7\"Tabatha
6'alive? t
6'weight D@ ffffff
7'species 3:cat}
{3'age 6+
4'eats #7:bananas
7:insects$
4'name 6\"George
6'alive? f
6'weight D@1=p\243\327\n=
7'species 6:monkey}
{3'age 12-
4'eats #$
4'name 6\"Casper
6'alive? f
6'weight D\300A@\0\0\0\0\000
7'species 5:ghost}]>")
zoo-structure)
(test-equal?
"csexp backwards compat"
(syrup-decode #"(3:zoo (3:cat 7:tabatha))")
'(#"zoo" (#"cat" #"tabatha")))
(test-equal?
"bencode backwards compat"
(syrup-decode #"l3:zood4:name3:cat7:species7:tabathaee")
'(#"zoo" #hash((#"name" . #"cat") (#"species" . #"tabatha"))))
(struct foop (blorp blap)
#:transparent)
(define (foop->record fb)
(record* 'foop (foop-blorp fb) (foop-blap fb)))
(test-equal?
"marshaller works"
(syrup-encode (list 'meep 'moop (foop 'fizzy 'water) 'bop)
#:marshallers (list (cons foop? foop->record)))
#"[4'meep4'moop<4'foop5'fizzy5'water>3'bop]")
(test-equal?
"unmarshaller works"
(syrup-decode #"[4'meep4'moop<4'foop5'fizzy5'water>3'bop]"
#:unmarshallers (list (cons 'foop foop)))
(list 'meep 'moop (foop 'fizzy 'water) 'bop)))