aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/format/xml.lux
blob: 4e358f91de45832712cf37512ccaeca34efd1156 (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
(.module:
  [library
   [lux #*
    [abstract
     [monad (#+ do)]
     [equivalence (#+ Equivalence)]
     [codec (#+ Codec)]]
    [control
     [try (#+ Try)]
     ["<>" parser ("#\." monad)
      ["<.>" text (#+ Parser)]]]
    [data
     ["." product]
     ["." name ("#\." equivalence codec)]
     ["." text ("#\." equivalence monoid)]
     [collection
      ["." list ("#\." functor)]
      ["." dictionary (#+ Dictionary)]]]
    [math
     [number
      ["n" nat]
      ["." int]]]]])

(type: .public Tag
  Name)

(type: .public Attribute
  Name)

(type: .public Attrs
  (Dictionary Attribute Text))

(def: .public attributes
  Attrs
  (dictionary.empty name.hash))

(type: .public #rec XML
  (#Text Text)
  (#Node Tag Attrs (List XML)))

(def: namespace_separator
  ":")

(def: xml_standard_escape_char^
  (Parser Text)
  ($_ <>.either
      (<>.after (<text>.this "&lt;") (<>\in "<"))
      (<>.after (<text>.this "&gt;") (<>\in ">"))
      (<>.after (<text>.this "&amp;") (<>\in "&"))
      (<>.after (<text>.this "&apos;") (<>\in "'"))
      (<>.after (<text>.this "&quot;") (<>\in text.double_quote))
      ))

(def: xml_unicode_escape_char^
  (Parser Text)
  (|> (do <>.monad
        [hex? (<>.maybe (<text>.this "x"))
         code (case hex?
                #.None
                (<>.codec int.decimal (<text>.many <text>.decimal))

                (#.Some _)
                (<>.codec int.decimal (<text>.many <text>.hexadecimal)))]
        (in (|> code .nat text.of_char)))
      (<>.before (<text>.this ";"))
      (<>.after (<text>.this "&#"))))

(def: xml_escape_char^
  (Parser Text)
  (<>.either xml_standard_escape_char^
             xml_unicode_escape_char^))

(def: xml_char^
  (Parser Text)
  (<>.either (<text>.none_of ($_ text\compose "<>&" text.double_quote))
             xml_escape_char^))

(def: xml_identifier
  (Parser Text)
  (do <>.monad
    [head (<>.either (<text>.one_of "_")
                     <text>.alpha)
     tail (<text>.some (<>.either (<text>.one_of "_.-")
                                  <text>.alpha_num))]
    (in ($_ text\compose head tail))))

(def: namespaced_symbol^
  (Parser Name)
  (do <>.monad
    [first_part xml_identifier
     ?second_part (<| <>.maybe (<>.after (<text>.this ..namespace_separator)) xml_identifier)]
    (case ?second_part
      #.None
      (in ["" first_part])

      (#.Some second_part)
      (in [first_part second_part]))))

(def: tag^ namespaced_symbol^)
(def: attr_name^ namespaced_symbol^)

(def: spaced^
  (All [a] (-> (Parser a) (Parser a)))
  (let [white_space^ (<>.some <text>.space)]
    (|>> (<>.before white_space^)
         (<>.after white_space^))))

(def: attr_value^
  (Parser Text)
  (let [value^ (<text>.some xml_char^)]
    (<>.either (<text>.enclosed [text.double_quote text.double_quote] value^)
               (<text>.enclosed ["'" "'"] value^))))

(def: attrs^
  (Parser Attrs)
  (<| (\ <>.monad map (dictionary.of_list name.hash))
      <>.some
      (<>.and (..spaced^ attr_name^))
      (<>.after (<text>.this "="))
      (..spaced^ attr_value^)))

(def: (close_tag^ expected)
  (-> Tag (Parser []))
  (do <>.monad
    [actual (|> tag^
                ..spaced^
                (<>.after (<text>.this "/"))
                (<text>.enclosed ["<" ">"]))]
    (<>.assertion ($_ text\compose "Close tag does not match open tag." text.new_line
                      "Expected: " (name\encode expected) text.new_line
                      "  Actual: " (name\encode actual) text.new_line)
                  (name\= expected actual))))

(def: comment^
  (Parser Text)
  (|> (<text>.not (<text>.this "--"))
      <text>.some
      (<text>.enclosed ["<!--" "-->"])
      ..spaced^))

(def: xml_header^
  (Parser Attrs)
  (|> (..spaced^ attrs^)
      (<>.before (<text>.this "?>"))
      (<>.after (<text>.this "<?xml"))
      ..spaced^))

(def: cdata^
  (Parser Text)
  (let [end (<text>.this "]]>")]
    (|> (<text>.some (<text>.not end))
        (<>.after end)
        (<>.after (<text>.this "<![CDATA["))
        ..spaced^)))

(def: text^
  (Parser XML)
  (|> (..spaced^ (<text>.many xml_char^))
      (<>.either cdata^)
      (<>\map (|>> #Text))))

(def: null^
  (Parser Any)
  (<text>.this (text.of_char 0)))

(def: xml^
  (Parser XML)
  (|> (<>.rec
       (function (_ node^)
         (|> (do <>.monad
               [_ (<text>.this "<")
                tag (..spaced^ tag^)
                attrs (..spaced^ attrs^)
                .let [no_children^ ($_ <>.either
                                       (do <>.monad
                                         [_ (<text>.this "/>")]
                                         (in (#Node tag attrs (list))))
                                       (do <>.monad
                                         [_ (<text>.this ">")
                                          _ (<>.some (<>.either <text>.space
                                                                ..comment^))
                                          _ (..close_tag^ tag)]
                                         (in (#Node tag attrs (list)))))
                      with_children^ (do <>.monad
                                       [_ (<text>.this ">")
                                        children (<>.many node^)
                                        _ (..close_tag^ tag)]
                                       (in (#Node tag attrs children)))]]
               ($_ <>.either
                   no_children^
                   with_children^))
             ..spaced^
             (<>.before (<>.some ..comment^))
             (<>.after (<>.some ..comment^))
             (<>.either ..text^))))
      (<>.before (<>.some ..null^))
      (<>.after (<>.maybe ..xml_header^))))

(def: (sanitize_value input)
  (-> Text Text)
  (|> input
      (text.replaced "&" "&amp;")
      (text.replaced "<" "&lt;")
      (text.replaced ">" "&gt;")
      (text.replaced "'" "&apos;")
      (text.replaced text.double_quote "&quot;")))

(def: .public (tag [namespace name])
  (-> Tag Text)
  (case namespace
    "" name
    _ ($_ text\compose namespace ..namespace_separator name)))

(def: .public attribute
  (-> Attribute Text)
  ..tag)

(def: xml_header
  Text
  (let [quote (: (-> Text Text)
                 (function (_ value)
                   ($_ text\compose text.double_quote value text.double_quote)))]
    ($_ text\compose
        "<?xml"
        " version=" (quote "1.0")
        " encoding=" (quote "UTF-8")
        "?>")))

(implementation: .public codec
  (Codec Text XML)
  
  (def: encode
    (let [attributes (: (-> Attrs Text)
                        (function (_ attrs)
                          (|> attrs
                              dictionary.entries
                              (list\map (function (_ [key value])
                                          ($_ text\compose (..attribute key) "=" text.double_quote (sanitize_value value) text.double_quote)))
                              (text.interposed " "))))]
      (function (_ input)
        ($_ text\compose
            ..xml_header text.new_line
            (loop [prefix ""
                   input input]
              (case input
                (#Text value)
                (sanitize_value value)

                (^ (#Node xml_tag xml_attrs (list (#Text value))))
                (let [tag (..tag xml_tag)
                      attrs (if (dictionary.empty? xml_attrs)
                              ""
                              ($_ text\compose " " (attributes xml_attrs)))]
                  ($_ text\compose
                      prefix "<" tag attrs ">"
                      (sanitize_value value)
                      "</" tag ">"))
                
                (#Node xml_tag xml_attrs xml_children)
                (let [tag (..tag xml_tag)
                      attrs (if (dictionary.empty? xml_attrs)
                              ""
                              ($_ text\compose " " (attributes xml_attrs)))]
                  (if (list.empty? xml_children)
                    ($_ text\compose prefix "<" tag attrs "/>")
                    ($_ text\compose prefix "<" tag attrs ">"
                        (|> xml_children
                            (list\map (|>> (recur (text\compose prefix text.tab)) (text\compose text.new_line)))
                            text.together)
                        text.new_line prefix "</" tag ">")))))
            ))))
  (def: decode
    (<text>.result ..xml^)))

(implementation: .public equivalence
  (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)
           (\ (dictionary.equivalence text.equivalence) = reference/attrs sample/attrs)
           (n.= (list.size reference/children)
                (list.size sample/children))
           (|> (list.zipped/2 reference/children sample/children)
               (list.every? (product.uncurried =))))

      _
      false)))