aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/parser.lux
blob: 01dbd1415ef62b17d98066709396ddf96291decc (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]
    [equivalence (#+ Equivalence)]
    {[0 #test]
     [/
      ["$." functor (#+ Injection Comparison)]
      ["$." apply]
      ["$." monad]]}]
   [control
    [parser
     ["s" code (#+ Parser)]]]
   [data
    ["." error (#+ Error)]
    [number
     ["." nat]]
    ["." text ("#;." equivalence)
     format]
    [collection
     ["." list ("#;." functor)]]]
   [math
    ["r" random]]
   [macro
    ["." code]
    [syntax (#+ syntax:)]]]
  {1
   ["." / (#+ Parser)]})

(def: (should-fail expected input)
  (All [a] (-> Text (Error a) Bit))
  (case input
    (#error.Failure actual)
    (text;= expected actual)
    
    _
    #0))

(def: (enforced? parser input)
  (All [s] (-> (Parser s Any) s Bit))
  (case (/.run input parser)
    (#error.Success [_ []])
    #1

    _
    #0))

(def: (found? parser input)
  (All [s] (-> (Parser s Bit) s Bit))
  (case (/.run input parser)
    (#error.Success [_ #1])
    #1

    _
    #0))

(def: (fails? input)
  (All [a] (-> (Error a) Bit))
  (case input
    (#error.Failure _)
    #1

    _
    #0))

(syntax: (match pattern then input)
  (wrap (list (` (case (~ input)
                   (^ (#error.Success [(~' _) (~ pattern)]))
                   (~ then)

                   (~' _)
                   #0)))))

(def: combinators-0
  Test
  (do r.monad
    [expected0 r.nat
     variadic (:: @ map (|>> (n/max 1) (n/min 20)) r.nat)
     expected+ (r.list variadic r.nat)
     even0 (r.filter n/even? r.nat)
     odd0 (r.filter n/odd? r.nat)
     not0 r.bit]
    ($_ _.and
        (_.test "Can optionally succeed with some parser."
                (and (|> (/.maybe s.nat)
                         (/.run (list (code.nat expected0)))
                         (match (#.Some actual)
                                (n/= expected0 actual)))
                     (|> (/.maybe s.nat)
                         (/.run (list (code.int (.int expected0))))
                         (match #.None
                                #1))))
        (_.test "Can apply a parser 0 or more times."
                (and (|> (/.some s.nat)
                         (/.run (list;map code.nat expected+))
                         (match actual
                                (:: (list.equivalence nat.equivalence) = expected+ actual)))
                     (|> (/.some s.nat)
                         (/.run (list;map (|>> .int code.int) expected+))
                         (match #.Nil
                                #1))))
        (_.test "Can apply a parser 1 or more times."
                (and (|> (/.many s.nat)
                         (/.run (list;map code.nat expected+))
                         (match actual
                                (:: (list.equivalence nat.equivalence) = expected+ actual)))
                     (|> (/.many s.nat)
                         (/.run (list (code.nat expected0)))
                         (match (list actual)
                                (n/= expected0 actual)))
                     (|> (/.many s.nat)
                         (/.run (list;map (|>> .int code.int) expected+))
                         fails?)))
        (_.test "Can use either parser."
                (let [even (/.filter n/even? s.nat)
                      odd (/.filter n/odd? s.nat)]
                  (and (|> (/.either even odd)
                           (/.run (list (code.nat even0)))
                           (match actual (n/= even0 actual)))
                       (|> (/.either even odd)
                           (/.run (list (code.nat odd0)))
                           (match actual (n/= odd0 actual)))
                       (|> (/.either even odd)
                           (/.run (list (code.bit not0)))
                           fails?))))
        (_.test "Can create the opposite/negation of any parser."
                (and (|> (/.not s.nat)
                         (/.run (list (code.nat expected0)))
                         fails?)
                     (|> (/.not s.nat)
                         (/.run (list (code.bit not0)))
                         (match [] #1))))
        )))

(def: combinators-1
  Test
  (do r.monad
    [failure (r.ascii 1)
     variadic (:: @ map (|>> (n/max 1) (n/min 20)) r.nat)
     times (:: @ map (n/% variadic) r.nat)
     expected+ (r.list variadic r.nat)
     separator (r.ascii 1)]
    ($_ _.and
        (_.test "Can fail at will."
                (|> (/.fail failure)
                    (/.run (list))
                    (should-fail failure)))
        (_.test "Can apply a parser N times."
                (and (|> (/.exactly times s.nat)
                         (/.run (list;map code.nat expected+))
                         (match actual
                                (:: (list.equivalence nat.equivalence) =
                                    (list.take times expected+)
                                    actual)))
                     (|> (/.exactly (inc variadic) s.nat)
                         (/.run (list;map code.nat expected+))
                         fails?)))
        (_.test "Can apply a parser at-least N times."
                (and (|> (/.at-least times s.nat)
                         (/.run (list;map code.nat expected+))
                         (match actual
                                (:: (list.equivalence nat.equivalence) =
                                    expected+
                                    actual)))
                     (|> (/.at-least (inc variadic) s.nat)
                         (/.run (list;map code.nat expected+))
                         fails?)))
        (_.test "Can apply a parser at-most N times."
                (and (|> (/.at-most times s.nat)
                         (/.run (list;map code.nat expected+))
                         (match actual
                                (:: (list.equivalence nat.equivalence) =
                                    (list.take times expected+)
                                    actual)))
                     (|> (/.at-most (inc variadic) s.nat)
                         (/.run (list;map code.nat expected+))
                         (match actual
                                (:: (list.equivalence nat.equivalence) =
                                    expected+
                                    actual)))))
        (_.test "Can apply a parser between N and M times."
                (and (|> (/.between times variadic s.nat)
                         (/.run (list;map code.nat expected+))
                         (match actual
                                (:: (list.equivalence nat.equivalence) =
                                    expected+
                                    actual)))
                     (|> (/.between times variadic s.nat)
                         (/.run (list;map code.nat (list.take times expected+)))
                         (match actual
                                (:: (list.equivalence nat.equivalence) =
                                    (list.take times expected+)
                                    actual)))))
        (_.test "Can parse while taking separators into account."
                (|> (/.sep-by (s.this (code.text separator)) s.nat)
                    (/.run (list.interpose (code.text separator) (list;map code.nat expected+)))
                    (match actual
                           (:: (list.equivalence nat.equivalence) =
                               expected+
                               actual))))
        (_.test "Can obtain the whole of the remaining input."
                (|> /.remaining
                    (/.run (list;map code.nat expected+))
                    (match actual
                           (:: (list.equivalence code.equivalence) =
                               (list;map code.nat expected+)
                               actual))))
        )))

(def: (injection value)
  (Injection (All [a i] (Parser i a)))
  (:: /.monad wrap value))

(def: comparison
  (Comparison (All [a i] (Parser i a)))
  (function (_ == left right)
    (case [(/.run [] left) (/.run [] right)]
      [(#error.Success [_ left]) (#error.Success [_ right])]
      (== left right)

      _
      false)))

(def: #export test
  Test
  (do r.monad
    [assertion (r.ascii 1)]
    (<| (_.context (%name (name-of /.Parser)))
        ($_ _.and
            ($functor.spec ..injection ..comparison /.functor)
            ($apply.spec ..injection ..comparison /.apply)
            ($monad.spec ..injection ..comparison /.monad)

            (_.test "Can make assertions while parsing."
                    (and (|> (/.assert assertion #1)
                             (/.run (list (code.bit #1) (code.int +123)))
                             (match [] #1))
                         (|> (/.assert assertion #0)
                             (/.run (list (code.bit #1) (code.int +123)))
                             fails?)))
            ..combinators-0
            ..combinators-1
            ))))