aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/parser/lux/data/format/xml.lux
blob: d413db61e89b626198709c6a536216425adf560a (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
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except Tag)
   [abstract
    [monad (.only do)]]
   [control
    ["//" parser]
    ["[0]" try (.only Try) (.use "[1]#[0]" functor)]
    ["[0]" exception (.only Exception)]]
   [data
    ["[0]" text
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list]
     ["[0]" dictionary]]]
   [meta
    ["[0]" symbol (.use "[1]#[0]" equivalence codec)]]]]
 [\\library
  ["[0]" / (.only Attribute Attrs Tag XML)]])

(type .public (Parser a)
  (//.Parser [Attrs (List XML)] a))

(exception.def .public empty_input)
(exception.def .public unexpected_input)

(exception.def .public (wrong_tag [expected actual])
  (Exception [Tag Tag])
  (exception.report
   (list ["Expected" (%.text (/.tag expected))]
         ["Actual" (%.text (/.tag actual))])))

(exception.def .public (unknown_attribute [expected available])
  (Exception [Attribute (List Attribute)])
  (exception.report
   (list ["Expected" (%.text (/.attribute expected))]
         ["Available" (exception.listing (|>> /.attribute %.text) available)])))

(exception.def .public (unconsumed_inputs inputs)
  (Exception (List XML))
  (exception.report
   (list ["Inputs" (exception.listing (of /.codec encoded) inputs)])))

(def (result' parser attrs documents)
  (All (_ a) (-> (Parser a) Attrs (List XML) (Try a)))
  (when (//.result parser [attrs documents])
    {try.#Success [[attrs' remaining] output]}
    (if (list.empty? remaining)
      {try.#Success output}
      (exception.except ..unconsumed_inputs remaining))
    
    {try.#Failure error}
    {try.#Failure error}))

(def .public (result parser documents)
  (All (_ a) (-> (Parser a) (List XML) (Try a)))
  (..result' parser /.attributes documents))

(def .public text
  (Parser Text)
  (function (_ [attrs documents])
    (when documents
      {.#End}
      (exception.except ..empty_input [])
      
      {.#Item head tail}
      (when head
        {/.#Text value}
        {try.#Success [[attrs tail] value]}
        
        {/.#Node _}
        (exception.except ..unexpected_input [])))))

(def .public tag
  (Parser Tag)
  (function (_ [attrs documents])
    (when documents
      {.#End}
      (exception.except ..empty_input [])
      
      {.#Item head _}
      (when head
        {/.#Text _}
        (exception.except ..unexpected_input [])
        
        {/.#Node tag _ _}
        {try.#Success [[attrs documents] tag]}))))

(def .public (attribute name)
  (-> Attribute (Parser Text))
  (function (_ [attrs documents])
    (when (dictionary.value name attrs)
      {.#None}
      (exception.except ..unknown_attribute [name (dictionary.keys attrs)])
      
      {.#Some value}
      {try.#Success [[attrs documents] value]})))

(def .public (node expected parser)
  (All (_ a) (-> Tag (Parser a) (Parser a)))
  (function (_ [attrs documents])
    (when documents
      {.#End}
      (exception.except ..empty_input [])
      
      {.#Item head tail}
      (when head
        {/.#Text _}
        (exception.except ..unexpected_input [])
        
        {/.#Node actual attrs' children}
        (if (symbol#= expected actual)
          (|> children
              (..result' parser attrs')
              (try#each (|>> [[attrs tail]])))
          (exception.except ..wrong_tag [expected actual]))))))

(def .public any
  (Parser XML)
  (function (_ [attrs documents])
    (when documents
      {.#End}
      (exception.except ..empty_input [])
      
      {.#Item head tail}
      {try.#Success [[attrs tail] head]})))

(exception.def .public nowhere)

(def .public (somewhere parser)
  (All (_ a) (-> (Parser a) (Parser a)))
  (function (again [attrs input])
    (when (//.result parser [attrs input])
      {try.#Success [[attrs remaining] output]}
      {try.#Success [[attrs remaining] output]}
      
      {try.#Failure error}
      (when input
        {.#End}
        (exception.except ..nowhere [])
        
        {.#Item head tail}
        (do try.monad
          [[[attrs tail'] output] (again [attrs tail])]
          (in [[attrs {.#Item head tail'}]
               output]))))))