-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.rkt
213 lines (185 loc) · 6.07 KB
/
json.rkt
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
#lang racket
(require "parse.rkt")
(provide (struct-out json-pair)
string->json
; TODO: Remove these provides.
parse-json-string
parse-json-number
parse-json-pair
parse-json-array
parse-json-object
parse-json-value
STRING
NUMBER
ARRAY
PAIR
OBJECT
VALUE)
; -------------------------------------
; ----- JSON Context Free Grammar -----
; -------------------------------------
; Parser
(define-parser SPACE
(alt*
(lit " ") (lit "\t") (lit "\n") (lit "\r")))
; Parser -> Parser
(define (spacey l)
(unwrap (star SPACE) l (star SPACE)))
; Parser
(define-parser HEXIT
(alt* DIGIT (lit "a") (lit "b") (lit "c") (lit "d") (lit "e") (lit "f")
(lit "A") (lit "B") (lit "C") (lit "D") (lit "E") (lit "F")))
; Parser
(define-parser STRING
(wrap 'string
(lit "\"")
(star (alt* (char-except "\\" "\"")
(lit "\\\"")
(lit "\\\\")
(lit "\\/")
(lit "\\b")
(lit "\\f")
(lit "\\n")
(lit "\\r")
(lit "\\t")
(seq* 'hex (lit "\\u") HEXIT HEXIT HEXIT HEXIT)))
(lit "\"")))
; Parser
(define-parser DIGIT+
(alt* (lit "1") (lit "2") (lit "3") (lit "4") (lit "5") (lit "6") (lit "7")
(lit "8") (lit "9")))
; Parser
(define-parser DIGIT
(alt* (lit "0") DIGIT+))
; Parser
(define-parser NUMBER
(seq* 'number
(opt (lit "-"))
(alt* (lit "0") (seq* 'significand
DIGIT+
(star DIGIT)))
(opt (seq* 'decimal (lit ".") (star DIGIT)))
(opt (seq* 'exponent (alt* (lit "e") (lit "E"))
(opt (alt* (lit "+") (lit "-")))
(star DIGIT)))))
; Parser
(define-parser PAIR
(seq* 'pair STRING (spacey (lit ":")) VALUE))
; Parser
(define-parser OBJECT
(wrap 'object
(spacey (lit "{"))
(opt (seq* 'object-first
PAIR
(star (seq* 'object-rest
(spacey (lit ","))
PAIR))))
(spacey (lit "}"))))
; Parser
(define-parser ARRAY
(wrap 'array
(spacey (lit "["))
(opt (seq* 'array-first
VALUE
(star (seq* 'array-rest
(spacey (lit ","))
VALUE))))
(spacey (lit "]"))))
; Parser
(define-parser VALUE
(alt* (spacey STRING)
(spacey NUMBER)
(spacey OBJECT)
(spacey ARRAY)
(spacey (lit "true"))
(spacey (lit "false"))
(spacey (lit "null"))))
; ------------------------
; ----- JSON Parsing -----
; ------------------------
; A JSON is one of:
; - String
; - Number
; - Boolean
; - 'null
; - JSONArray
; - JSONObject
; A JSONArray is a [Listof JSON], representing an ordered
; collection of values.
; A JSONObject is a [Listof JSONPair], representing a set
; of named values, similar to a structure.
; A JSONPair is a (make-pair String JSON),
; representing a named value.
(define-struct json-pair (name value) #:transparent)
; (wrap-node 'string AST) -> JSONString
(define (parse-json-string ast)
(flatten-ast ast))
; (seq-node 'number AST AST) -> JSONNumber
(define (parse-json-number ast)
(string->number (string-append "#e" (flatten-ast ast))))
; (seq-node 'json-pair AST) -> JSONPair
(define (parse-json-pair ast)
(make-json-pair (parse-json-string (seq-node-left ast))
(parse-json-value (seq-node-right (seq-node-right ast)))))
; (wrap-node 'array AST) -> JSONArray
(define (parse-json-array ast)
(let* ([v (wrap-node-value ast)]
[f (lambda (a b) (cons (parse-json-value (seq-node-right a)) b))])
(cond [(emp-node? v)
empty]
[(and (seq-node? v)
(symbol=? (seq-node-label v) 'array-first)
(emp-node? (seq-node-right v)))
(list (parse-json-value (seq-node-left v)))]
[(and (seq-node? v)
(symbol=? (seq-node-label v) 'array-first)
(seq-node? (seq-node-right v)))
(cons (parse-json-value (seq-node-left v))
(star-foldr f empty (seq-node-right v)))]
[else
(error 'parse-json-array "bad input ~s" ast)])))
; (wrap-node 'object AST) -> JSONObject
(define (parse-json-object ast)
(let* ([v (wrap-node-value ast)]
[f (lambda (a b) (cons (parse-json-pair (seq-node-right a)) b))])
(cond [(emp-node? v)
empty]
[(and (seq-node? v)
(symbol=? (seq-node-label v) 'object-first)
(emp-node? (seq-node-right v)))
(list (parse-json-pair (seq-node-left v)))]
[(and (seq-node? v)
(symbol=? (seq-node-label v) 'object-first)
(seq-node? (seq-node-right v)))
(cons (parse-json-pair (seq-node-left v))
(star-foldr f empty (seq-node-right v)))]
[else
(error 'parse-json-object "bad input ~s" ast)])))
; AST -> JSON
(define (parse-json-value ast)
(cond [(and (wrap-node? ast)
(symbol=? (wrap-node-label ast) 'string))
(parse-json-string ast)]
[(and (seq-node? ast)
(symbol=? (seq-node-label ast) 'number))
(parse-json-number ast)]
[(and (wrap-node? ast)
(symbol=? (wrap-node-label ast) 'array))
(parse-json-array ast)]
[(and (wrap-node? ast)
(symbol=? (wrap-node-label ast) 'object))
(parse-json-object ast)]
[(and (lit-node? ast)
(string=? (lit-node-value ast) "true"))
true]
[(and (lit-node? ast)
(string=? (lit-node-value ast) "false"))
false]
[(and (lit-node? ast)
(string=? (lit-node-value ast) "null"))
'null]
[else
(error 'parse-json-value "bad input ~s" ast)]))
; String -> JSON
(define (string->json str)
(parse-json-value (parse VALUE str)))