blob: 7f6ca24a8cb4938105a49b0ca51f2bc2b93c6fc2 (
plain)
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
|
(.module:
{#.doc (.example "Functionality for reading and writing values in the JSON format."
"For more information, please see: http://www.json.org/")}
[library
[lux #*
["." meta (#+ monad)]
[abstract
[equivalence (#+ Equivalence)]
[codec (#+ Codec)]
[predicate (#+ Predicate)]
["." monad (#+ do)]]
[control
pipe
["." maybe]
["." try (#+ Try)]
["<>" parser ("#\." monad)
["<.>" text (#+ Parser)]
["<.>" code]]]
[data
["." bit]
["." product]
["." text ("#\." equivalence monoid)]
[collection
["." list ("#\." fold functor)]
["." row (#+ Row row) ("#\." monad)]
["." dictionary (#+ Dictionary)]]]
[macro
[syntax (#+ syntax:)]
["." code]]
[math
[number
["n" nat]
["f" frac ("#\." decimal)]]]]])
(template [<name> <type>]
[(type: .public <name>
<type>)]
[Null Any]
[Boolean Bit]
[Number Frac]
[String Text]
)
(type: .public #rec JSON
(#Null Null)
(#Boolean Boolean)
(#Number Number)
(#String String)
(#Array (Row JSON))
(#Object (Dictionary String JSON)))
(template [<name> <type>]
[(type: .public <name>
<type>)]
[Array (Row JSON)]
[Object (Dictionary String JSON)]
)
(def: .public null?
(Predicate JSON)
(|>> (case> #Null true
_ false)))
(def: .public object
(-> (List [String JSON]) JSON)
(|>> (dictionary.of_list text.hash) #..Object))
(syntax: .public (json [token <code>.any])
{#.doc (example "A simple way to produce JSON literals."
(json #null)
(json #1)
(json +123.456)
(json "this is a string")
(json ["this" "is" "an" "array"])
(json {"this" "is"
"an" "object"}))}
(let [(^open ".") ..monad
wrapper (function (_ x) (` (..json (~ x))))]
(case token
(^template [<ast_tag> <ctor> <json_tag>]
[[_ (<ast_tag> value)]
(in (list (` (: JSON (<json_tag> (~ (<ctor> value)))))))])
([#.Bit code.bit #..Boolean]
[#.Frac code.frac #..Number]
[#.Text code.text #..String])
[_ (#.Tag ["" "null"])]
(in (list (` (: JSON #..Null))))
[_ (#.Tuple members)]
(in (list (` (: JSON (#..Array ((~! row) (~+ (list\map wrapper members))))))))
[_ (#.Record pairs)]
(do {! ..monad}
[pairs' (monad.map !
(function (_ [slot value])
(case slot
[_ (#.Text key_name)]
(in (` [(~ (code.text key_name)) (~ (wrapper value))]))
_
(meta.failure "Wrong syntax for JSON object.")))
pairs)]
(in (list (` (: JSON (#..Object ((~! dictionary.of_list)
(~! text.hash)
(list (~+ pairs')))))))))
_
(in (list token)))))
(def: .public (fields json)
{#.doc "Get all the fields in a JSON object."}
(-> JSON (Try (List String)))
(case json
(#Object obj)
(#try.Success (dictionary.keys obj))
_
(#try.Failure ($_ text\compose "Cannot get the fields of a non-object."))))
(def: .public (field key json)
{#.doc "A JSON object field getter."}
(-> String JSON (Try JSON))
(case json
(#Object obj)
(case (dictionary.value key obj)
(#.Some value)
(#try.Success value)
#.None
(#try.Failure ($_ text\compose "Missing field '" key "' on object.")))
_
(#try.Failure ($_ text\compose "Cannot get field '" key "' on a non-object."))))
(def: .public (has key value json)
{#.doc "A JSON object field setter."}
(-> String JSON JSON (Try JSON))
(case json
(#Object obj)
(#try.Success (#Object (dictionary.has key value obj)))
_
(#try.Failure ($_ text\compose "Cannot set field '" key "' on a non-object."))))
(template [<name> <tag> <type> <desc>]
[(def: .public (<name> key json)
{#.doc (code.text ($_ text\compose "A JSON object field getter for " <desc> "."))}
(-> Text JSON (Try <type>))
(case (field key json)
(#try.Success (<tag> value))
(#try.Success value)
(#try.Success _)
(#try.Failure ($_ text\compose "Wrong value type at key: " key))
(#try.Failure error)
(#try.Failure error)))]
[boolean_field #Boolean Boolean "booleans"]
[number_field #Number Number "numbers"]
[string_field #String String "strings"]
[array_field #Array Array "arrays"]
[object_field #Object Object "objects"]
)
(implementation: .public equivalence
(Equivalence JSON)
(def: (= x y)
(case [x y]
[#Null #Null]
#1
(^template [<tag> <struct>]
[[(<tag> x') (<tag> y')]
(\ <struct> = x' y')])
([#Boolean bit.equivalence]
[#Number f.equivalence]
[#String text.equivalence])
[(#Array xs) (#Array ys)]
(and (n.= (row.size xs) (row.size ys))
(list\fold (function (_ idx prev)
(and prev
(maybe.else #0
(do maybe.monad
[x' (row.item idx xs)
y' (row.item idx ys)]
(in (= x' y'))))))
#1
(list.indices (row.size xs))))
[(#Object xs) (#Object ys)]
(and (n.= (dictionary.size xs) (dictionary.size ys))
(list\fold (function (_ [xk xv] prev)
(and prev
(case (dictionary.value xk ys)
#.None #0
(#.Some yv) (= xv yv))))
#1
(dictionary.entries xs)))
_
#0)))
............................................................
............................................................
............................................................
(def: (null_format _)
(-> Null Text)
"null")
(def: boolean_format
(-> Boolean Text)
(|>> (case>
#0 "false"
#1 "true")))
(def: number_format
(-> Number Text)
(|>> (case>
(^or +0.0 -0.0) "0.0"
value (let [raw (\ f.decimal encode value)]
(if (f.< +0.0 value)
raw
(|> raw (text.split_at 1) maybe.assume product.right))))))
(def: escape "\")
(def: escaped_dq (text\compose ..escape text.double_quote))
(def: string_format
(-> String Text)
(|>> (text.replaced text.double_quote ..escaped_dq)
(text.enclosed [text.double_quote text.double_quote])))
(template [<token> <name>]
[(def: <name>
Text
<token>)]
["," value_separator]
[":" entry_separator]
["[" array_start]
["]" array_end]
["{" object_start]
["}" object_end]
)
(def: (array_format format)
(-> (-> JSON Text) (-> Array Text))
(|>> (row\map format)
row.list
(text.join_with ..value_separator)
(text.enclosed [..array_start ..array_end])))
(def: (kv_format format [key value])
(-> (-> JSON Text) (-> [String JSON] Text))
($_ text\compose
(..string_format key)
..entry_separator
(format value)
))
(def: (object_format format)
(-> (-> JSON Text) (-> Object Text))
(|>> dictionary.entries
(list\map (..kv_format format))
(text.join_with ..value_separator)
(text.enclosed [..object_start ..object_end])))
(def: .public (format json)
(-> JSON Text)
(case json
(^template [<tag> <format>]
[(<tag> value)
(<format> value)])
([#Null ..null_format]
[#Boolean ..boolean_format]
[#Number ..number_format]
[#String ..string_format]
[#Array (..array_format format)]
[#Object (..object_format format)])
))
............................................................
............................................................
............................................................
(def: space_parser
(Parser Text)
(<text>.some <text>.space))
(def: value_separator_parser
(Parser [Text Any Text])
($_ <>.and
..space_parser
(<text>.this ..value_separator)
..space_parser))
(def: null_parser
(Parser Null)
(do <>.monad
[_ (<text>.this "null")]
(in [])))
(template [<name> <token> <value>]
[(def: <name>
(Parser Boolean)
(do <>.monad
[_ (<text>.this <token>)]
(in <value>)))]
[true_parser "true" #1]
[false_parser "false" #0]
)
(def: boolean_parser
(Parser Boolean)
($_ <>.either
..true_parser
..false_parser))
(def: number_parser
(Parser Number)
(do {! <>.monad}
[signed? (<>.parses? (<text>.this "-"))
digits (<text>.many <text>.decimal)
decimals (<>.else "0"
(do !
[_ (<text>.this ".")]
(<text>.many <text>.decimal)))
exp (<>.else ""
(do !
[mark (<text>.one_of "eE")
signed?' (<>.parses? (<text>.this "-"))
offset (<text>.many <text>.decimal)]
(in ($_ text\compose mark (if signed?' "-" "") offset))))]
(case (f\decode ($_ text\compose (if signed? "-" "") digits "." decimals exp))
(#try.Failure message)
(<>.failure message)
(#try.Success value)
(in value))))
(def: escaped_parser
(Parser Text)
($_ <>.either
(<>.after (<text>.this "\t")
(<>\in text.tab))
(<>.after (<text>.this "\b")
(<>\in text.back_space))
(<>.after (<text>.this "\n")
(<>\in text.new_line))
(<>.after (<text>.this "\r")
(<>\in text.carriage_return))
(<>.after (<text>.this "\f")
(<>\in text.form_feed))
(<>.after (<text>.this (text\compose "\" text.double_quote))
(<>\in text.double_quote))
(<>.after (<text>.this "\\")
(<>\in "\"))))
(def: string_parser
(Parser String)
(<| (<text>.enclosed [text.double_quote text.double_quote])
(loop [_ []])
(do {! <>.monad}
[chars (<text>.some (<text>.none_of (text\compose "\" text.double_quote)))
stop <text>.next])
(if (text\= "\" stop)
(do !
[escaped escaped_parser
next_chars (recur [])]
(in ($_ text\compose chars escaped next_chars)))
(in chars))))
(def: (kv_parser json_parser)
(-> (Parser JSON) (Parser [String JSON]))
(do <>.monad
[key ..string_parser
_ ..space_parser
_ (<text>.this ..entry_separator)
_ ..space_parser
value json_parser]
(in [key value])))
(template [<name> <type> <open> <close> <elem_parser> <prep>]
[(def: (<name> json_parser)
(-> (Parser JSON) (Parser <type>))
(do <>.monad
[_ (<text>.this <open>)
_ space_parser
elems (<>.separated_by ..value_separator_parser <elem_parser>)
_ space_parser
_ (<text>.this <close>)]
(in (<prep> elems))))]
[array_parser Array ..array_start ..array_end json_parser row.of_list]
[object_parser Object ..object_start ..object_end (kv_parser json_parser) (dictionary.of_list text.hash)]
)
(def: json_parser
(Parser JSON)
(<>.rec
(function (_ json_parser)
($_ <>.or
null_parser
boolean_parser
number_parser
string_parser
(array_parser json_parser)
(object_parser json_parser)))))
(implementation: .public codec
(Codec Text JSON)
(def: encode ..format)
(def: decode (<text>.result json_parser)))
|