forked from carp-lang/Carp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_parser.carp
More file actions
308 lines (278 loc) · 10.2 KB
/
Copy pathjson_parser.carp
File metadata and controls
308 lines (278 loc) · 10.2 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
(Project.no-echo)
(deftype Json
JNull
(JBool [Bool])
(JNumber [Double])
(JString [String])
(JArray [(Array (Box Json))])
(JObject [(Array (Pair String (Box Json)))])
)
(defn ok [x]
(Result.Success x))
(defn err [msg]
(Result.Error msg))
(defn whitespace? [c]
(or (= c \space)
(= c \tab)
(= c \newline)
(= c \return)))
(defn char-at-safe [s i]
(if (< i (String.length s))
(Maybe.Just (String.char-at s i))
(Maybe.Nothing)))
(defn skip-ws [s i]
(match (char-at-safe s i)
(Maybe.Just c) (if (whitespace? c) (skip-ws s (Int.inc i)) i)
(Maybe.Nothing) i))
(defn parse-literal [s i literal value]
(let [lit-len (String.length literal)
end (+ i lit-len)]
(if (> end (String.length s))
(err @"Unexpected end of input")
(if (= literal &(String.slice s i end))
(ok (Pair.init value end))
(err @"Unexpected literal")))))
(defn escape-for-string [c]
(case c
\" (Maybe.Just \" )
\\ (Maybe.Just \\ )
\/ (Maybe.Just \/ )
\b (Maybe.Just \backspace)
\f (Maybe.Just \formfeed)
\n (Maybe.Just \newline)
\r (Maybe.Just \return)
\t (Maybe.Just \tab)
(Maybe.Nothing)))
(defn parse-string-chars [s i acc]
(match (char-at-safe s i)
(Maybe.Nothing) (err @"Unterminated string")
(Maybe.Just c)
(if (= c \" )
(ok (Pair.init (String.from-chars &acc) (Int.inc i)))
(if (= c \\)
(match (char-at-safe s (Int.inc i))
(Maybe.Nothing) (err @"Unterminated escape")
(Maybe.Just esc)
(match (escape-for-string esc)
(Maybe.Nothing) (err @"Unsupported escape")
(Maybe.Just out)
(do
(Array.push-back! &acc out)
(parse-string-chars s (+ i 2) acc))))
(do
(Array.push-back! &acc c)
(parse-string-chars s (Int.inc i) acc))))))
(defn parse-string [s i]
(if (= (String.char-at s i) \" )
(parse-string-chars s (Int.inc i) (the (Array Char) []))
(err @"Expected string")))
(defn scan-number [s i saw-dot saw-digit]
(match (char-at-safe s i)
(Maybe.Nothing) (Pair.init i saw-digit)
(Maybe.Just c)
(if (Char.num? c)
(scan-number s (Int.inc i) saw-dot true)
(if (and (= c \. ) (not saw-dot))
(scan-number s (Int.inc i) true saw-digit)
(Pair.init i saw-digit)))))
(defn parse-number [s i]
(let [start i
first (String.char-at s i)
j (if (= first \- ) (Int.inc i) i)
scan (scan-number s j false false)
end @(Pair.a &scan)
saw-digit @(Pair.b &scan)]
(if (not saw-digit)
(err @"Invalid number")
(let [num-str (String.slice s start end)]
(match (Double.from-string &num-str)
(Maybe.Nothing) (err @"Invalid number")
(Maybe.Just n) (ok (Pair.init (Box.init (Json.JNumber n)) end)))))))
(register parse-value (Fn [&String Int] (Result (Pair (Box Json) Int) String)))
(defn parse-array-elements [s i acc]
(match (parse-value s i)
(Result.Error e) (Result.Error e)
(Result.Success res)
(let [val @(Pair.a &res)
next (skip-ws s @(Pair.b &res))]
(do
(Array.push-back! &acc val)
(match (char-at-safe s next)
(Maybe.Just c)
(if (= c \])
(ok (Pair.init (Box.init (Json.JArray acc)) (Int.inc next)))
(if (= c \,)
(parse-array-elements s (skip-ws s (Int.inc next)) acc)
(err @"Expected ',' or ']'")))
_
(err @"Expected ',' or ']'"))))))
(defn parse-array [s i]
(let [start (skip-ws s (Int.inc i))]
(match (char-at-safe s start)
(Maybe.Just c)
(if (= c \])
(ok (Pair.init (Box.init (Json.JArray (the (Array (Box Json)) []))) (Int.inc start)))
(parse-array-elements s start (the (Array (Box Json)) [])))
(Maybe.Nothing)
(err @"Unexpected end of input"))))
(defn parse-object-members [s i acc]
(let [i0 (skip-ws s i)]
(match (parse-string s i0)
(Result.Error e) (Result.Error e)
(Result.Success key-res)
(let [key @(Pair.a &key-res)
after-key (skip-ws s @(Pair.b &key-res))]
(match (char-at-safe s after-key)
(Maybe.Just c)
(if (= c \:)
(match (parse-value s (skip-ws s (Int.inc after-key)))
(Result.Error e) (Result.Error e)
(Result.Success val-res)
(let [val @(Pair.a &val-res)
next (skip-ws s @(Pair.b &val-res))]
(do
(Array.push-back! &acc (Pair.init key val))
(match (char-at-safe s next)
(Maybe.Just c2)
(if (= c2 \})
(ok (Pair.init (Box.init (Json.JObject acc)) (Int.inc next)))
(if (= c2 \,)
(parse-object-members s (skip-ws s (Int.inc next)) acc)
(err @"Expected ',' or '}'")))
_
(err @"Expected ',' or '}'")))))
(err @"Expected ':'"))
_
(err @"Expected ':'"))))))
(defn parse-object [s i]
(let [start (skip-ws s (Int.inc i))]
(match (char-at-safe s start)
(Maybe.Just c)
(if (= c \})
(ok (Pair.init (Box.init (Json.JObject (the (Array (Pair String (Box Json))) []))) (Int.inc start)))
(parse-object-members s start (the (Array (Pair String (Box Json))) [])))
(Maybe.Nothing)
(err @"Unexpected end of input"))))
(defn parse-value [s i]
(let [j (skip-ws s i)]
(match (char-at-safe s j)
(Maybe.Nothing) (err @"Unexpected end of input")
(Maybe.Just c)
(if (= c \" )
(match (parse-string s j)
(Result.Error e) (Result.Error e)
(Result.Success res)
(let [str @(Pair.a &res)
next @(Pair.b &res)]
(ok (Pair.init (Box.init (Json.JString str)) next))))
(if (= c \[) (parse-array s j)
(if (= c \{) (parse-object s j)
(if (= c \t) (parse-literal s j "true" (Box.init (Json.JBool true)))
(if (= c \f) (parse-literal s j "false" (Box.init (Json.JBool false)))
(if (= c \n) (parse-literal s j "null" (Box.init (Json.JNull)))
(if (or (= c \- ) (Char.num? c))
(parse-number s j)
(err @"Unexpected token")))))))))))
(defn parse-json [s]
(match (parse-value s 0)
(Result.Error e) (Result.Error e)
(Result.Success res)
(let [end (skip-ws s @(Pair.b &res))]
(if (< end (String.length s))
(err @"Trailing characters")
(ok @(Pair.a &res))))))
(defn escape-char [c]
(let [ch @c]
(case ch
\" @"\\\""
\\ @"\\\\"
\newline @"\\n"
\tab @"\\t"
\return @"\\r"
\backspace @"\\b"
\formfeed @"\\f"
(Char.str ch))))
(defn escape-string [s]
(String.concat &(Array.copy-map &escape-char &(String.chars &s))))
(defn quote-string [s]
(let [escaped (escape-string s)]
(String.concat &[@"\"" escaped @"\""])))
(defn indentation [level]
(String.repeat (* level 2) " "))
(register pretty-json (Fn [&Json Int] String))
(defn build-array-lines [arr indent]
(let-do [inner (Int.inc indent)
pad (indentation inner)
len (Array.length &arr)
last (Int.dec len)
lines (the (Array String) [])
comma @","]
(for [i 0 len]
(let-do [item (Array.unsafe-nth &arr i)
item-str (pretty-json (Box.peek item) inner)]
(if (< i last)
(Array.push-back! &lines (String.append &(String.concat &[(String.copy &pad) item-str]) &comma))
(Array.push-back! &lines (String.concat &[(String.copy &pad) item-str])))))
lines))
(defn pretty-array [arr indent]
(if (= 0 (Array.length &arr))
@"[]"
(let [lines (build-array-lines arr indent)
newline @"\n"
body (String.join &newline &lines)
close-pad (indentation indent)
open @"["
close-bracket @"]"
close (String.append &close-pad &close-bracket)
out1 (String.append &open &newline)
out2 (String.append &out1 &body)
out3 (String.append &out2 &newline)
out4 (String.append &out3 &close)]
out4)))
(defn build-object-lines [pairs indent]
(let-do [inner (Int.inc indent)
pad (indentation inner)
len (Array.length &pairs)
last (Int.dec len)
lines (the (Array String) [])
comma @","]
(for [i 0 len]
(let-do [p (Array.unsafe-nth &pairs i)
key @(Pair.a p)
val (Pair.b p)
key-str (quote-string key)
val-str (pretty-json (Box.peek val) inner)]
(if (< i last)
(Array.push-back! &lines (String.append &(String.concat &[(String.copy &pad) key-str @": " val-str]) &comma))
(Array.push-back! &lines (String.concat &[(String.copy &pad) key-str @": " val-str])))))
lines))
(defn pretty-object [pairs indent]
(if (= 0 (Array.length &pairs))
@"{}"
(let [lines (build-object-lines pairs indent)
newline @"\n"
body (String.join &newline &lines)
close-pad (indentation indent)
open @"{"
close-brace @"}"
close (String.append &close-pad &close-brace)
out1 (String.append &open &newline)
out2 (String.append &out1 &body)
out3 (String.append &out2 &newline)
out4 (String.append &out3 &close)]
out4)))
(defn pretty-json [j indent]
(match @j
(Json.JNull) @"null"
(Json.JBool b) (if b @"true" @"false")
(Json.JNumber n) (Double.str n)
(Json.JString s) (quote-string s)
(Json.JArray arr) (pretty-array arr indent)
(Json.JObject pairs) (pretty-object pairs indent)))
(defn main []
(let [input @"{\"ok\": true, \"nums\": [1, 2.5, null], \"msg\": \"hi\"}"]
(match (parse-json &input)
(Result.Error e)
(println* @"Parse error: " e)
(Result.Success json-box)
(println* (pretty-json (Box.peek &json-box) 0)))))