aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/text/regex.lux
blob: f9e57e97f0da9cbfbb76af9c76fefaecac59e1be (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
(.using
 [library
  [lux (.except pattern)
   ["[0]" meta]
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" maybe]
    ["[0]" try]
    ["[0]" exception (.only exception:)]
    ["<>" parser ("[1]#[0]" monad)
     ["<[0]>" text (.only Parser)]
     ["<[0]>" code]]]
   [data
    ["[0]" product]
    [collection
     ["[0]" list ("[1]#[0]" mix monad)]]]
   [macro (.only with_symbols)
    [syntax (.only syntax:)]
    ["^" pattern]
    ["[0]" code]]
   [math
    [number (.only hex)
     ["n" nat ("[1]#[0]" decimal)]]]]]
 ["[0]" // (.only)
  ["%" format (.only format)]])

(def: regex_char^
  (Parser Text)
  (<text>.none_of "\.|&()[]{}"))

(def: escaped_char^
  (Parser Text)
  (do <>.monad
    [? (<>.parses? (<text>.this "\"))]
    (if ?
      <text>.any
      regex_char^)))

(def: (refine^ refinement^ base^)
  (All (_ a) (-> (Parser a) (Parser Text) (Parser Text)))
  (do <>.monad
    [output base^
     _ (<text>.local output refinement^)]
    (in output)))

(def: word^
  (Parser Text)
  (<>.either <text>.alpha_num
             (<text>.one_of "_")))

(def: (copy reference)
  (-> Text (Parser Text))
  (<>.after (<text>.this reference) (<>#in reference)))

(def: together^
  (-> (Parser (List Text)) (Parser Text))
  (# <>.monad each //.together))

(def: symbol_char^
  (Parser Text)
  (<text>.none_of (format "[]{}()s.<>" //.double_quote)))

(def: symbol_part^
  (Parser Text)
  (do <>.monad
    [head (refine^ (<text>.not <text>.decimal)
                   symbol_char^)
     tail (<text>.some symbol_char^)]
    (in (format head tail))))

(def: (symbol^ current_module)
  (-> Text (Parser Symbol))
  (all <>.either
       (<>.and (<>#in current_module) (<>.after (<text>.this "..") symbol_part^))
       (<>.and symbol_part^ (<>.after (<text>.this ".") symbol_part^))
       (<>.and (<>#in .prelude_module) (<>.after (<text>.this ".") symbol_part^))
       (<>.and (<>#in "") symbol_part^)))

(def: (re_var^ current_module)
  (-> Text (Parser Code))
  (do <>.monad
    [symbol (<text>.enclosed ["\@<" ">"] (symbol^ current_module))]
    (in (` (is ((~! <text>.Parser) Text) (~ (code.symbol symbol)))))))

(def: re_range^
  (Parser Code)
  (do [! <>.monad]
    [from (|> regex_char^ (# ! each (|>> (//.char 0) maybe.trusted)))
     _ (<text>.this "-")
     to (|> regex_char^ (# ! each (|>> (//.char 0) maybe.trusted)))]
    (in (` ((~! <text>.range) (~ (code.nat from)) (~ (code.nat to)))))))

(def: re_char^
  (Parser Code)
  (do <>.monad
    [char escaped_char^]
    (in (` ((~! ..copy) (~ (code.text char)))))))

(def: re_options^
  (Parser Code)
  (do <>.monad
    [options (<text>.many escaped_char^)]
    (in (` ((~! <text>.one_of) (~ (code.text options)))))))

(def: re_user_class^'
  (Parser Code)
  (do <>.monad
    [negate? (<>.maybe (<text>.this "^"))
     parts (<>.many (all <>.either
                         re_range^
                         re_options^))]
    (in (case negate?
          {.#Some _} (` ((~! <text>.not) (all ((~! <>.either)) (~+ parts))))
          {.#None}   (` (all ((~! <>.either)) (~+ parts)))))))

(def: re_user_class^
  (Parser Code)
  (do <>.monad
    [init ..re_user_class^'
     rest (<>.some (<>.after (<text>.this "&&")
                             (<text>.enclosed ["[" "]"]
                                              ..re_user_class^')))]
    (in (list#mix (function (_ refinement base)
                    (` ((~! refine^) (~ refinement) (~ base))))
                  init
                  rest))))

(def: blank^
  (Parser Text)
  (<text>.one_of (format " " //.tab)))

(def: ascii^
  (Parser Text)
  (<text>.range (hex "0") (hex "7F")))

(def: control^
  (Parser Text)
  (<>.either (<text>.range (hex "0") (hex "1F"))
             (<text>.one_of (//.of_char (hex "7F")))))

(def: punct^
  (Parser Text)
  (<text>.one_of (format "!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
                         //.double_quote)))

(def: graph^
  (Parser Text)
  (<>.either punct^ <text>.alpha_num))

(def: print^
  (Parser Text)
  (<>.either graph^
             (<text>.one_of (//.of_char (hex "20")))))

(def: re_system_class^
  (Parser Code)
  (do <>.monad
    []
    (all <>.either
         (<>.after (<text>.this ".") (in (` (~! <text>.any))))
         (<>.after (<text>.this "\d") (in (` (~! <text>.decimal))))
         (<>.after (<text>.this "\D") (in (` ((~! <text>.not) (~! <text>.decimal)))))
         (<>.after (<text>.this "\s") (in (` (~! <text>.space))))
         (<>.after (<text>.this "\S") (in (` ((~! <text>.not) (~! <text>.space)))))
         (<>.after (<text>.this "\w") (in (` (~! word^))))
         (<>.after (<text>.this "\W") (in (` ((~! <text>.not) (~! word^)))))

         (<>.after (<text>.this "\p{Lower}") (in (` (~! <text>.lower))))
         (<>.after (<text>.this "\p{Upper}") (in (` (~! <text>.upper))))
         (<>.after (<text>.this "\p{Alpha}") (in (` (~! <text>.alpha))))
         (<>.after (<text>.this "\p{Digit}") (in (` (~! <text>.decimal))))
         (<>.after (<text>.this "\p{Alnum}") (in (` (~! <text>.alpha_num))))
         (<>.after (<text>.this "\p{Space}") (in (` (~! <text>.space))))
         (<>.after (<text>.this "\p{HexDigit}") (in (` (~! <text>.hexadecimal))))
         (<>.after (<text>.this "\p{OctDigit}") (in (` (~! <text>.octal))))
         (<>.after (<text>.this "\p{Blank}") (in (` (~! blank^))))
         (<>.after (<text>.this "\p{ASCII}") (in (` (~! ascii^))))
         (<>.after (<text>.this "\p{Contrl}") (in (` (~! control^))))
         (<>.after (<text>.this "\p{Punct}") (in (` (~! punct^))))
         (<>.after (<text>.this "\p{Graph}") (in (` (~! graph^))))
         (<>.after (<text>.this "\p{Print}") (in (` (~! print^))))
         )))

(def: re_class^
  (Parser Code)
  (<>.either re_system_class^
             (<text>.enclosed ["[" "]"] re_user_class^)))

(def: number^
  (Parser Nat)
  (|> (<text>.many <text>.decimal)
      (<>.codec n.decimal)))

(def: re_back_reference^
  (Parser Code)
  (<>.either (do <>.monad
               [_ (<text>.this "\")
                id number^]
               (in (` ((~! ..copy) (~ (code.symbol ["" (n#encoded id)]))))))
             (do <>.monad
               [_ (<text>.this "\k<")
                captured_symbol symbol_part^
                _ (<text>.this ">")]
               (in (` ((~! ..copy) (~ (code.symbol ["" captured_symbol]))))))))

(def: (re_simple^ current_module)
  (-> Text (Parser Code))
  (all <>.either
       re_class^
       (re_var^ current_module)
       re_back_reference^
       re_char^
       ))

(def: (re_simple_quantified^ current_module)
  (-> Text (Parser Code))
  (do <>.monad
    [base (re_simple^ current_module)
     quantifier (<text>.one_of "?*+")]
    (case quantifier
      "?"
      (in (` ((~! <>.else) "" (~ base))))
      
      "*"
      (in (` ((~! together^) ((~! <>.some) (~ base)))))
      
      ... "+"
      _
      (in (` ((~! together^) ((~! <>.many) (~ base)))))
      )))

(exception: .public (incorrect_quantification [from Nat
                                               to Nat])
  (exception.report
   "Input" (format (%.nat from) "," (%.nat to))
   "Should be" (format (%.nat to) "," (%.nat from))))

(def: (re_counted_quantified^ current_module)
  (-> Text (Parser Code))
  (do [! <>.monad]
    [base (re_simple^ current_module)]
    (<| (<text>.enclosed ["{" "}"])
        (all <>.either
             (do !
               [[from to] (<>.and number^ (<>.after (<text>.this ",") number^))
                _ (<>.assertion (exception.error ..incorrect_quantification [from to])
                                (n.<= to from))]
               (in (` ((~! together^) ((~! <>.between)
                                       (~ (code.nat from))
                                       (~ (code.nat (n.- from to)))
                                       (~ base))))))
             (do !
               [limit (<>.after (<text>.this ",") number^)]
               (in (` ((~! together^) ((~! <>.at_most) (~ (code.nat limit)) (~ base))))))
             (do !
               [limit (<>.before (<text>.this ",") number^)]
               (in (` ((~! together^) ((~! <>.at_least) (~ (code.nat limit)) (~ base))))))
             (do !
               [limit number^]
               (in (` ((~! together^) ((~! <>.exactly) (~ (code.nat limit)) (~ base))))))))))

(def: (re_quantified^ current_module)
  (-> Text (Parser Code))
  (<>.either (re_simple_quantified^ current_module)
             (re_counted_quantified^ current_module)))

(def: (re_complex^ current_module)
  (-> Text (Parser Code))
  (all <>.either
       (re_quantified^ current_module)
       (re_simple^ current_module)))

(type: Re_Group
  (Variant
   {#Non_Capturing}
   {#Capturing [(Maybe Text) Nat]}))

(def: (re_sequential^ capturing? re_scoped^ current_module)
  (-> Bit
      (-> Text (Parser [Re_Group Code]))
      Text
      (Parser [Nat Code]))
  (do <>.monad
    [parts (<>.many (<>.or (re_complex^ current_module)
                           (re_scoped^ current_module)))
     .let [g!total (code.symbol ["" "0total"])
           g!temp (code.symbol ["" "0temp"])
           [_ names steps] (list#mix (is (-> (Either Code [Re_Group Code])
                                             [Nat (List Code) (List (List Code))]
                                             [Nat (List Code) (List (List Code))])
                                         (function (_ part [idx names steps])
                                           (case part
                                             (^.or {.#Left complex}
                                                   {.#Right [{#Non_Capturing} complex]})
                                             [idx
                                              names
                                              (partial_list (list g!temp complex
                                                                  (` .let) (` [(~ g!total) (# (~! //.monoid) (~' composite) (~ g!total) (~ g!temp))]))
                                                            steps)]
                                             
                                             {.#Right [{#Capturing [?name num_captures]} scoped]}
                                             (let [[idx! name!] (case ?name
                                                                  {.#Some _name}
                                                                  [idx (code.symbol ["" _name])]

                                                                  {.#None}
                                                                  [(++ idx) (code.symbol ["" (n#encoded idx)])])
                                                   access (if (n.> 0 num_captures)
                                                            (` ((~! product.left) (~ name!)))
                                                            name!)]
                                               [idx!
                                                (partial_list name! names)
                                                (partial_list (list name! scoped
                                                                    (` .let) (` [(~ g!total) (# (~! //.monoid) (~' composite) (~ g!total) (~ access))]))
                                                              steps)])
                                             )))
                                     [0
                                      (is (List Code) (list))
                                      (is (List (List Code)) (list))]
                                     parts)]]
    (in [(if capturing?
           (list.size names)
           0)
         (` ((~! do) (~! <>.monad)
             [.let [(~ g!total) ""]
              (~+ (|> steps list.reversed list#conjoint))]
             ((~ (' in)) [(~ g!total) (~+ (list.reversed names))])))])
    ))

(def: (unflatten^ lexer)
  (-> (Parser Text) (Parser [Text Any]))
  (<>.and lexer (# <>.monad in [])))

(def: (|||^ left right)
  (All (_ l r) (-> (Parser [Text l]) (Parser [Text r]) (Parser [Text (Or l r)])))
  (function (_ input)
    (case (left input)
      {try.#Success [input' [lt lv]]}
      {try.#Success [input' [lt {0 #0 lv}]]}

      {try.#Failure _}
      (case (right input)
        {try.#Success [input' [rt rv]]}
        {try.#Success [input' [rt {0 #1 rv}]]}

        {try.#Failure error}
        {try.#Failure error}))))

(def: (|||_^ left right)
  (All (_ l r) (-> (Parser [Text l]) (Parser [Text r]) (Parser Text)))
  (function (_ input)
    (case (left input)
      {try.#Success [input' [lt lv]]}
      {try.#Success [input' lt]}

      {try.#Failure _}
      (case (right input)
        {try.#Success [input' [rt rv]]}
        {try.#Success [input' rt]}

        {try.#Failure error}
        {try.#Failure error}))))

(def: (prep_alternative [num_captures alt])
  (-> [Nat Code] Code)
  (if (n.> 0 num_captures)
    alt
    (` ((~! unflatten^) (~ alt)))))

(def: (re_alternative^ capturing? re_scoped^ current_module)
  (-> Bit
      (-> Text (Parser [Re_Group Code]))
      Text
      (Parser [Nat Code]))
  (do <>.monad
    [.let [sub^ (re_sequential^ capturing? re_scoped^ current_module)]
     head sub^
     tail (<>.some (<>.after (<text>.this "|") sub^))]
    (if (list.empty? tail)
      (in head)
      (in [(list#mix n.max (product.left head) (list#each product.left tail))
           (` (all ((~ (if capturing?
                         (` (~! |||^))
                         (` (~! |||_^)))))
                   (~ (prep_alternative head))
                   (~+ (list#each prep_alternative tail))))]))))

(def: (re_scoped^ current_module)
  (-> Text (Parser [Re_Group Code]))
  (all <>.either
       (do <>.monad
         [_ (<text>.this "(?:")
          [_ scoped] (re_alternative^ #0 re_scoped^ current_module)
          _ (<text>.this ")")]
         (in [{#Non_Capturing} scoped]))
       (do <>.monad
         [complex (re_complex^ current_module)]
         (in [{#Non_Capturing} complex]))
       (do <>.monad
         [_ (<text>.this "(?<")
          captured_symbol symbol_part^
          _ (<text>.this ">")
          [num_captures pattern] (re_alternative^ #1 re_scoped^ current_module)
          _ (<text>.this ")")]
         (in [{#Capturing [{.#Some captured_symbol} num_captures]} pattern]))
       (do <>.monad
         [_ (<text>.this "(")
          [num_captures pattern] (re_alternative^ #1 re_scoped^ current_module)
          _ (<text>.this ")")]
         (in [{#Capturing [{.#None} num_captures]} pattern]))))

(def: (regex^ current_module)
  (-> Text (Parser Code))
  (# <>.monad each product.right (re_alternative^ #1 re_scoped^ current_module)))

(syntax: .public (regex [pattern <code>.text])
  (do meta.monad
    [current_module meta.current_module_name]
    (case (<text>.result (regex^ current_module)
                         pattern)
      {try.#Failure error}
      (meta.failure (format "Error while parsing regular-expression:" //.new_line
                            error))

      {try.#Success regex}
      (in (list regex)))))

(syntax: .public (pattern [[pattern bindings] (<code>.form (<>.and <code>.text (<>.maybe <code>.any)))
                           body <code>.any
                           branches (<>.many <code>.any)])
  (with_symbols [g!temp]
    (in (partial_list (` (^.multi (~ g!temp)
                                  [((~! <text>.result) (..regex (~ (code.text pattern))) (~ g!temp))
                                   {try.#Success (~ (maybe.else g!temp bindings))}]))
                      body
                      branches))))