aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/control/parser.lux
blob: 606d8d4487a670a32c3c354397ea62d6540821aa (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
(;module:
  [lux #- not]
  (lux (control [functor #+ Functor]
                [applicative #+ Applicative]
                [monad #+ do Monad]
                [codec])
       (data (coll [list "list/" Functor<List> Monoid<List>])
             [product]
             ["R" result])))

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

## [Structures]
(struct: #export Functor<Parser> (All [s] (Functor (Parser s)))
  (def: (map f ma)
    (function [input]
      (case (ma input)
        (#R;Error msg)
        (#R;Error msg)

        (#R;Success [input' a])
        (#R;Success [input' (f a)])))))

(struct: #export Applicative<Parser> (All [s] (Applicative (Parser s)))
  (def: functor Functor<Parser>)

  (def: (wrap x)
    (function [input]
      (#R;Success [input x])))

  (def: (apply ff fa)
    (function [input]
      (case (ff input)
        (#R;Success [input' f])
        (case (fa input')
          (#R;Success [input'' a])
          (#R;Success [input'' (f a)])

          (#R;Error msg)
          (#R;Error msg))

        (#R;Error msg)
        (#R;Error msg)))))

(struct: #export Monad<Parser> (All [s] (Monad (Parser s)))
  (def: applicative Applicative<Parser>)

  (def: (join mma)
    (function [input]
      (case (mma input)
        (#R;Error msg)
        (#R;Error msg)

        (#R;Success [input' ma])
        (ma input')))))

## [Parsers]
(def: #export (assert message test)
  {#;doc "Fails with the given message if the test is false."}
  (All [s] (-> Text Bool (Parser s Unit)))
  (function [input]
    (if test
      (#R;Success [input []])
      (#R;Error message))))

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

(def: #export (run input p)
  (All [s a]
    (-> s (Parser s a) (R;Result [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)
      (#R;Error _)            (#R;Success [input (list)])
      (#R;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)
      (#R;Success [tokens' x1]) (#R;Success [tokens' (+0 x1)])
      (#R;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)
      (#R;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 (n.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)
        (#R;Error msg)
        (#R;Success [input (list)])

        (#R;Success [input' x])
        (run input'
             (do Monad<Parser>
               [xs (at-most (n.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 Unit)))
  (function [input]
    (case (p input)
      (#R;Error msg)
      (#R;Success [input []])
      
      _
      (#R;Error "Expected to fail; yet succeeded."))))

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

(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)
      (#R;Error error)
      (#R;Success [input value])

      (#R;Success [input' output])
      (#R;Success [input' output]))))

(def: #export remaining
  (All [s] (Parser s s))
  (function [inputs]
    (#R;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 Bool) (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;Codec a z) (Parser s a) (Parser s z)))
  (function [input]
    (case (parser input)
      (#R;Error error)
      (#R;Error error)

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