blob: a990e690118bc2d2bbf566a20db11ab654d29679 (
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
|
(.module: {#.doc "Functionality for reading, generating and processing values in the XML format."}
[lux #*
[control
monad
[equivalence (#+ Equivalence)]
codec
["p" parser ("parser/." Monad<Parser>)]
["ex" exception (#+ exception:)]]
[data
["." number]
["E" error]
["." product]
["." name ("name/." Equivalence<Name> Codec<Text,Name>)]
["." text ("text/." Equivalence<Text> Monoid<Text>)
["l" lexer]]
[collection
["." list ("list/." Monad<List>)]
["d" dictionary]]]])
(type: #export Tag Name)
(type: #export Attrs (d.Dictionary Name Text))
(def: #export attrs Attrs (d.new name.Hash<Name>))
(type: #export #rec XML
(#Text Text)
(#Node Tag Attrs (List XML)))
(def: xml-standard-escape-char^
(l.Lexer Text)
($_ p.either
(p.after (l.this "<") (parser/wrap "<"))
(p.after (l.this ">") (parser/wrap ">"))
(p.after (l.this "&") (parser/wrap "&"))
(p.after (l.this "'") (parser/wrap "'"))
(p.after (l.this """) (parser/wrap "\""))))
(def: xml-unicode-escape-char^
(l.Lexer Text)
(|> (do p.Monad<Parser>
[hex? (p.maybe (l.this "x"))
code (case hex?
#.None
(p.codec number.Codec<Text,Int> (l.many l.decimal))
(#.Some _)
(p.codec number.Hex@Codec<Text,Int> (l.many l.hexadecimal)))]
(wrap (|> code .nat text.from-code)))
(p.before (l.this ";"))
(p.after (l.this "&#"))))
(def: xml-escape-char^
(l.Lexer Text)
(p.either xml-standard-escape-char^
xml-unicode-escape-char^))
(def: xml-char^
(l.Lexer Text)
(p.either (l.none-of "<>&'\"")
xml-escape-char^))
(def: xml-identifier
(l.Lexer Text)
(do p.Monad<Parser>
[head (p.either (l.one-of "_")
l.alpha)
tail (l.some (p.either (l.one-of "_.-")
l.alpha-num))]
(wrap ($_ text/compose head tail))))
(def: namespaced-symbol^
(l.Lexer Name)
(do p.Monad<Parser>
[first-part xml-identifier
?second-part (<| p.maybe (p.after (l.this ":")) xml-identifier)]
(case ?second-part
#.None
(wrap ["" first-part])
(#.Some second-part)
(wrap [first-part second-part]))))
(def: tag^ namespaced-symbol^)
(def: attr-name^ namespaced-symbol^)
(def: spaced^
(All [a] (-> (l.Lexer a) (l.Lexer a)))
(let [white-space^ (p.some l.space)]
(|>> (p.before white-space^)
(p.after white-space^))))
(def: attr-value^
(l.Lexer Text)
(let [value^ (l.some xml-char^)]
(p.either (l.enclosed ["\"" "\""] value^)
(l.enclosed ["'" "'"] value^))))
(def: attrs^
(l.Lexer Attrs)
(<| (:: p.Monad<Parser> map (d.from-list name.Hash<Name>))
p.some
(p.seq (spaced^ attr-name^))
(p.after (l.this "="))
(spaced^ attr-value^)))
(def: (close-tag^ expected)
(-> Tag (l.Lexer []))
(do p.Monad<Parser>
[actual (|> tag^
spaced^
(p.after (l.this "/"))
(l.enclosed ["<" ">"]))]
(p.assert ($_ text/compose "Close tag does not match open tag.\n"
"Expected: " (name/encode expected) "\n"
" Actual: " (name/encode actual) "\n")
(name/= expected actual))))
(def: comment^
(l.Lexer Text)
(|> (l.not (l.this "--"))
l.some
(l.enclosed ["<--" "-->"])
spaced^))
(def: xml-header^
(l.Lexer Attrs)
(|> (spaced^ attrs^)
(p.before (l.this "?>"))
(p.after (l.this "<?xml"))
spaced^))
(def: cdata^
(l.Lexer Text)
(let [end (l.this "]]>")]
(|> (l.some (l.not end))
(p.after end)
(p.after (l.this "<![CDATA["))
spaced^)))
(def: text^
(l.Lexer XML)
(|> (p.either cdata^
(l.many xml-char^))
(parser/map (|>> #Text))))
(def: xml^
(l.Lexer XML)
(|> (p.rec
(function (_ node^)
(p.either text^
(spaced^
(do p.Monad<Parser>
[_ (l.this "<")
tag (spaced^ tag^)
attrs (spaced^ attrs^)
#let [no-children^ (do p.Monad<Parser>
[_ (l.this "/>")]
(wrap (#Node tag attrs (list))))
with-children^ (do p.Monad<Parser>
[_ (l.this ">")
children (p.some node^)
_ (close-tag^ tag)]
(wrap (#Node tag attrs children)))]]
(p.either no-children^
with-children^))))))
## This is put outside of the call to "rec" because comments
## cannot be located inside of XML nodes.
## This way, the comments can only be before or after the main document.
(p.before (p.some comment^))
(p.after (p.some comment^))
(p.after (p.maybe xml-header^))))
(def: #export (read input)
(-> Text (E.Error XML))
(l.run input xml^))
(def: (sanitize-value input)
(-> Text Text)
(|> input
(text.replace-all "&" "&")
(text.replace-all "<" "<")
(text.replace-all ">" ">")
(text.replace-all "'" "'")
(text.replace-all "\"" """)))
(def: (write-tag [namespace name])
(-> Tag Text)
(case namespace
"" name
_ ($_ text/compose namespace ":" name)))
(def: (write-attrs attrs)
(-> Attrs Text)
(|> attrs
d.entries
(list/map (function (_ [key value])
($_ text/compose (write-tag key) "=" "\""(sanitize-value value) "\"")))
(text.join-with " ")))
(def: xml-header
Text
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
(def: #export (write input)
(-> XML Text)
($_ text/compose xml-header
(loop [input input]
(case input
(#Text value)
(sanitize-value value)
(#Node xml-tag xml-attrs xml-children)
(let [tag (write-tag xml-tag)
attrs (if (d.empty? xml-attrs)
""
($_ text/compose " " (write-attrs xml-attrs)))]
(if (list.empty? xml-children)
($_ text/compose "<" tag attrs "/>")
($_ text/compose "<" tag attrs ">"
(|> xml-children
(list/map recur)
(text.join-with ""))
"</" tag ">")))))))
(structure: #export _ (Codec Text XML)
(def: encode write)
(def: decode read))
(structure: #export _ (Equivalence XML)
(def: (= reference sample)
(case [reference sample]
[(#Text reference/value) (#Text sample/value)]
(text/= reference/value sample/value)
[(#Node reference/tag reference/attrs reference/children)
(#Node sample/tag sample/attrs sample/children)]
(and (name/= reference/tag sample/tag)
(:: (d.Equivalence<Dictionary> text.Equivalence<Text>) = reference/attrs sample/attrs)
(n/= (list.size reference/children)
(list.size sample/children))
(|> (list.zip2 reference/children sample/children)
(list.every? (product.uncurry =))))
_
#0)))
(type: #export (Reader a)
(p.Parser (List XML) a))
(exception: #export empty-input)
(exception: #export unexpected-input)
(exception: #export unknown-attribute)
(exception: #export (wrong-tag {tag Name})
(name/encode tag))
(exception: #export (unconsumed-inputs {inputs (List XML)})
(|> inputs
(list/map (:: Codec<Text,XML> encode))
(text.join-with "\n\n")))
(def: #export text
(Reader Text)
(function (_ docs)
(case docs
#.Nil
(ex.throw empty-input [])
(#.Cons head tail)
(case head
(#Text value)
(#E.Success [tail value])
(#Node _)
(ex.throw unexpected-input [])))))
(def: #export (attr name)
(-> Name (Reader Text))
(function (_ docs)
(case docs
#.Nil
(ex.throw empty-input [])
(#.Cons head _)
(case head
(#Text _)
(ex.throw unexpected-input [])
(#Node tag attrs children)
(case (d.get name attrs)
#.None
(ex.throw unknown-attribute [])
(#.Some value)
(#E.Success [docs value]))))))
(def: (run' docs reader)
(All [a] (-> (List XML) (Reader a) (E.Error a)))
(case (p.run docs reader)
(#E.Success [remaining output])
(if (list.empty? remaining)
(#E.Success output)
(ex.throw unconsumed-inputs remaining))
(#E.Error error)
(#E.Error error)))
(def: #export (node tag)
(-> Name (Reader Any))
(function (_ docs)
(case docs
#.Nil
(ex.throw empty-input [])
(#.Cons head _)
(case head
(#Text _)
(ex.throw unexpected-input [])
(#Node _tag _attrs _children)
(if (name/= tag _tag)
(#E.Success [docs []])
(ex.throw wrong-tag tag))))))
(def: #export (children reader)
(All [a] (-> (Reader a) (Reader a)))
(function (_ docs)
(case docs
#.Nil
(ex.throw empty-input [])
(#.Cons head tail)
(case head
(#Text _)
(ex.throw unexpected-input [])
(#Node _tag _attrs _children)
(do E.Monad<Error>
[output (run' _children reader)]
(wrap [tail output]))))))
(def: #export ignore
(Reader Any)
(function (_ docs)
(case docs
#.Nil
(ex.throw empty-input [])
(#.Cons head tail)
(#E.Success [tail []]))))
(def: #export (run document reader)
(All [a] (-> XML (Reader a) (E.Error a)))
(run' (list document) reader))
|