aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/control/parser.lux
blob: 2f25d13da641426f90cb11aeb777b8bdfed3cd16 (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
(.module:
  [lux (#- not)
   [control
    [functor (#+ Functor)]
    [apply (#+ Apply)]
    [monad (#+ do Monad)]
    [codec (#+ Codec)]]
   [data
    [collection
     ["." list ("list/." Functor<List> Monoid<List>)]]
    ["." product]
    ["e" error (#+ Error)]]])

(type: #export (Parser s a)
  {#.doc "A generic parser."}
  (-> s (e.Error [s a])))

## [Structures]
(structure: #export Functor<Parser> (All [s] (Functor (Parser s)))
  (def: (map f ma)
    (function (_ input)
      (case (ma input)
        (#e.Error msg)
        (#e.Error msg)

        (#e.Success [input' a])
        (#e.Success [input' (f a)])))))

(structure: #export Apply<Parser> (All [s] (Apply (Parser s)))
  (def: functor Functor<Parser>)

  (def: (apply ff fa)
    (function (_ input)
      (case (ff input)
        (#e.Success [input' f])
        (case (fa input')
          (#e.Success [input'' a])
          (#e.Success [input'' (f a)])

          (#e.Error msg)
          (#e.Error msg))

        (#e.Error msg)
        (#e.Error msg)))))

(structure: #export Monad<Parser> (All [s] (Monad (Parser s)))
  (def: functor Functor<Parser>)

  (def: (wrap x)
    (function (_ input)
      (#e.Success [input x])))

  (def: (join mma)
    (function (_ input)
      (case (mma input)
        (#e.Error msg)
        (#e.Error msg)

        (#e.Success [input' ma])
        (ma input')))))

## [Parsers]
(def: #export (assert message test)
  {#.doc "Fails with the given message if the test is #0."}
  (All [s] (-> Text Bit (Parser s Any)))
  (function (_ input)
    (if test
      (#e.Success [input []])
      (#e.Error message))))

(def: #export (maybe p)
  {#.doc "Optionality combinator."}
  (All [s a]
    (-> (Parser s a) (Parser s (Maybe a))))
  (function (_ input)
    (case (p input)
      (#e.Error _)            (#e.Success [input #.None])
      (#e.Success [input' x]) (#e.Success [input' (#.Some x)]))))

(def: #export (run input p)
  (All [s a]
    (-> s (Parser s a) (e.Error [s a])))
  (p input))

(def: #export (some p)
  {#.doc "0-or-more combinator."}
  (All [s a]
    (-> (Parser s a) (Parser s (List a))))
  (function (_ input)
    (case (p input)
      (#e.Error _)            (#e.Success [input (list)])
      (#e.Success [input' x]) (run input'
                                   (do Monad<Parser>
                                     [xs (some p)]
                                     (wrap (list& x xs)))
                                   ))))

(def: #export (many p)
  {#.doc "1-or-more combinator."}
  (All [s a]
    (-> (Parser s a) (Parser s (List a))))
  (do Monad<Parser>
    [x p
     xs (some p)]
    (wrap (list& x xs))))

(def: #export (seq p1 p2)
  {#.doc "Sequencing combinator."}
  (All [s a b]
    (-> (Parser s a) (Parser s b) (Parser s [a b])))
  (do Monad<Parser>
    [x1 p1
     x2 p2]
    (wrap [x1 x2])))

(def: #export (alt p1 p2)
  {#.doc "Heterogeneous alternative combinator."}
  (All [s a b]
    (-> (Parser s a) (Parser s b) (Parser s (| a b))))
  (function (_ tokens)
    (case (p1 tokens)
      (#e.Success [tokens' x1]) (#e.Success [tokens' (+0 x1)])
      (#e.Error _)             (run tokens
                                    (do Monad<Parser>
                                      [x2 p2]
                                      (wrap (+1 x2))))
      )))

(def: #export (either pl pr)
  {#.doc "Homogeneous alternative combinator."}
  (All [s a]
    (-> (Parser s a) (Parser s a) (Parser s a)))
  (function (_ tokens)
    (case (pl tokens)
      (#e.Error _) (pr tokens)
      output     output
      )))

(def: #export (exactly n p)
  {#.doc "Parse exactly N times."}
  (All [s a] (-> Nat (Parser s a) (Parser s (List a))))
  (if (n/> +0 n)
    (do Monad<Parser>
      [x p
       xs (exactly (dec n) p)]
      (wrap (#.Cons x xs)))
    (:: Monad<Parser> wrap (list))))

(def: #export (at-least n p)
  {#.doc "Parse at least N times."}
  (All [s a] (-> Nat (Parser s a) (Parser s (List a))))
  (do Monad<Parser>
    [min (exactly n p)
     extra (some p)]
    (wrap (list/compose min extra))))

(def: #export (at-most n p)
  {#.doc "Parse at most N times."}
  (All [s a] (-> Nat (Parser s a) (Parser s (List a))))
  (if (n/> +0 n)
    (function (_ input)
      (case (p input)
        (#e.Error msg)
        (#e.Success [input (list)])

        (#e.Success [input' x])
        (run input'
             (do Monad<Parser>
               [xs (at-most (dec n) p)]
               (wrap (#.Cons x xs))))
        ))
    (:: Monad<Parser> wrap (list))))

(def: #export (between from to p)
  {#.doc "Parse between N and M times."}
  (All [s a] (-> Nat Nat (Parser s a) (Parser s (List a))))
  (do Monad<Parser>
    [min-xs (exactly from p)
     max-xs (at-most (n/- from to) p)]
    (wrap (:: list.Monad<List> join (list min-xs max-xs)))))

(def: #export (sep-by sep p)
  {#.doc "Parsers instances of 'p' that are separated by instances of 'sep'."}
  (All [s a b] (-> (Parser s b) (Parser s a) (Parser s (List a))))
  (do Monad<Parser>
    [?x (maybe p)]
    (case ?x
      #.None
      (wrap #.Nil)
      
      (#.Some x)
      (do @
        [xs' (some (seq sep p))]
        (wrap (#.Cons x (list/map product.right xs'))))
      )))

(def: #export (not p)
  (All [s a] (-> (Parser s a) (Parser s Any)))
  (function (_ input)
    (case (p input)
      (#e.Error msg)
      (#e.Success [input []])
      
      _
      (#e.Error "Expected to fail; yet succeeded."))))

(def: #export (fail message)
  (All [s a] (-> Text (Parser s a)))
  (function (_ input)
    (#e.Error message)))

(def: #export (lift operation)
  (All [s a] (-> (Error a) (Parser s a)))
  (function (_ input)
    (case operation
      (#e.Success output)
      (#e.Success [input output])
      
      (#e.Error error)
      (#e.Error error))))

(def: #export (default value parser)
  {#.doc "If the given parser fails, returns the default value."}
  (All [s a] (-> a (Parser s a) (Parser s a)))
  (function (_ input)
    (case (parser input)
      (#e.Error error)
      (#e.Success [input value])

      (#e.Success [input' output])
      (#e.Success [input' output]))))

(def: #export remaining
  (All [s] (Parser s s))
  (function (_ inputs)
    (#e.Success [inputs inputs])))

(def: #export (rec parser)
  {#.doc "Combinator for recursive parser."}
  (All [s a] (-> (-> (Parser s a) (Parser s a)) (Parser s a)))
  (function (_ inputs)
    (run inputs (parser (rec parser)))))

(def: #export (after param subject)
  (All [s _ a] (-> (Parser s _) (Parser s a) (Parser s a)))
  (do Monad<Parser>
    [_ param]
    subject))

(def: #export (before param subject)
  (All [s _ a] (-> (Parser s _) (Parser s a) (Parser s a)))
  (do Monad<Parser>
    [output subject
     _ param]
    (wrap output)))

(def: #export (filter test parser)
  (All [s a] (-> (-> a Bit) (Parser s a) (Parser s a)))
  (do Monad<Parser>
    [output parser
     _ (assert "Constraint failed." (test output))]
    (wrap output)))

(def: #export (codec Codec<a,z> parser)
  (All [s a z] (-> (Codec a z) (Parser s a) (Parser s z)))
  (function (_ input)
    (case (parser input)
      (#e.Error error)
      (#e.Error error)

      (#e.Success [input' to-decode])
      (case (:: Codec<a,z> decode to-decode)
        (#e.Error error)
        (#e.Error error)
        
        (#e.Success value)
        (#e.Success [input' value])))))