aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/control/parser/xml.lux
blob: bc8c6ad931477d3deaa18729d9ba53e90750eafc (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
(.module:
  [lux #*
   [abstract
    [monad (#+ do)]]
   [control
    ["." try (#+ Try)]
    ["." exception (#+ exception:)]]
   [data
    ["." name ("#\." equivalence codec)]
    ["." text
     ["%" format (#+ format)]]
    [collection
     ["." list]
     ["." dictionary]]
    [format
     ["/" xml (#+ Attribute Tag XML)]]]]
  ["." //])

(type: #export (Parser a)
  (//.Parser (List XML) a))

(exception: #export empty-input)
(exception: #export unexpected-input)

(exception: #export (wrong-tag {expected Tag} {actual Tag})
  (exception.report
   ["Expected" (%.text (/.tag expected))]
   ["Actual" (%.text (/.tag actual))]))

(exception: #export (unknown-attribute {expected Attribute} {available (List Attribute)})
  (exception.report
   ["Expected" (%.text (/.attribute expected))]
   ["Available" (exception.enumerate (|>> /.attribute %.text) available)]))

(exception: #export (unconsumed-inputs {inputs (List XML)})
  (exception.report
   ["Inputs" (exception.enumerate (\ /.codec encode) inputs)]))

(def: #export text
  (Parser Text)
  (function (_ docs)
    (case docs
      #.Nil
      (exception.throw ..empty-input [])
      
      (#.Cons head tail)
      (case head
        (#/.Text value)
        (#try.Success [tail value])
        
        (#/.Node _)
        (exception.throw ..unexpected-input [])))))

(def: #export (node expected)
  (-> Tag (Parser Any))
  (function (_ docs)
    (case docs
      #.Nil
      (exception.throw ..empty-input [])
      
      (#.Cons head _)
      (case head
        (#/.Text _)
        (exception.throw ..unexpected-input [])
        
        (#/.Node actual _attributes _children)
        (if (name\= expected actual)
          (#try.Success [docs []])
          (exception.throw ..wrong-tag [expected actual]))))))

(def: #export tag
  (Parser Tag)
  (function (_ docs)
    (case docs
      #.Nil
      (exception.throw ..empty-input [])
      
      (#.Cons head _)
      (case head
        (#/.Text _)
        (exception.throw ..unexpected-input [])
        
        (#/.Node tag _attributes _children)
        (#try.Success [docs tag])))))

(def: #export (attribute name)
  (-> Attribute (Parser Text))
  (function (_ docs)
    (case docs
      #.Nil
      (exception.throw ..empty-input [])
      
      (#.Cons head _)
      (case head
        (#/.Text _)
        (exception.throw ..unexpected-input [])
        
        (#/.Node tag attributes children)
        (case (dictionary.get name attributes)
          #.None
          (exception.throw ..unknown-attribute [name (dictionary.keys attributes)])
          
          (#.Some value)
          (#try.Success [docs value]))))))

(def: (run' parser docs)
  (All [a] (-> (Parser a) (List XML) (Try a)))
  (case (//.run parser docs)
    (#try.Success [remaining output])
    (if (list.empty? remaining)
      (#try.Success output)
      (exception.throw ..unconsumed-inputs remaining))
    
    (#try.Failure error)
    (#try.Failure error)))

(def: #export (children parser)
  (All [a] (-> (Parser a) (Parser a)))
  (function (_ docs)
    (case docs
      #.Nil
      (exception.throw ..empty-input [])
      
      (#.Cons head tail)
      (case head
        (#/.Text _)
        (exception.throw ..unexpected-input [])
        
        (#/.Node _tag _attributes children)
        (do try.monad
          [output (run' parser children)]
          (wrap [tail output]))))))

(def: #export ignore
  (Parser Any)
  (function (_ docs)
    (case docs
      #.Nil
      (exception.throw ..empty-input [])
      
      (#.Cons head tail)
      (#try.Success [tail []]))))

(def: #export (run parser document)
  (All [a] (-> (Parser a) XML (Try a)))
  (..run' parser (list document)))

(exception: #export nowhere)

(def: #export (somewhere parser)
  (All [a] (-> (Parser a) (Parser a)))
  (function (recur input)
    (case (//.run parser input)
      (#try.Success [remaining output])
      (#try.Success [remaining output])
      
      (#try.Failure error)
      (case input
        #.Nil
        (exception.throw ..nowhere [])
        
        (#.Cons head tail)
        (do try.monad
          [[tail' output] (recur tail)]
          (wrap [(#.Cons head tail')
                 output]))))))