aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/configuration.lux
blob: cddf4ab5997ecbdf4b1ff743dfa7fe829728f25a (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
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except for)
   [abstract
    [equivalence (.only Equivalence)]
    [monoid (.only Monoid)]
    [monad (.only do)]]
   [control
    ["<>" parser]
    ["[0]" maybe (.use "[1]#[0]" functor)]
    ["[0]" exception]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format]
     ["<[1]>" \\parser (.only Parser)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix)
      ["/" property]]]]
   [math
    [number (.only hex)]]
   ["[0]" meta (.only)
    ["[0]" code (.only)
     ["<[1]>" \\parser]]
    [macro
     [syntax (.only syntax)]]]]])

(type .public Configuration
  (/.List Text))

(def .public equivalence
  (Equivalence Configuration)
  (/.equivalence text.equivalence))

(def .public monoid
  (Monoid Configuration)
  /.monoid)

(def .public empty
  Configuration
  /.empty)

(with_template [<ascii> <name>]
  [(def <name>
     Text
     (text.of_char (hex <ascii>)))]

  ["02" start]
  ["03" end]
  )

(def format'
  (-> Text Text)
  (text.enclosed [..start ..end]))

(def .public format
  (%.Format Configuration)
  (|>> (list#each (function (_ [feature value])
                    (%.format (..format' feature) (..format' value))))
       text.together))

(def .public parser
  (Parser Configuration)
  (let [parser' (is (Parser Text)
                    (<| (<>.after (<text>.this ..start))
                        (<>.before (<text>.this ..end))
                        (<text>.slice (<text>.some! (<text>.none_of! ..end)))))]
    (<>.some (<>.and parser' parser'))))

(exception.def .public invalid)

(def configuration
  (<code>.Parser Configuration)
  (<code>.tuple (<>.some (<>.and <code>.text <code>.text))))

(def (subsumes? actual expected)
  (-> Configuration Configuration Bit)
  (when expected
    {.#End}
    true

    {.#Item [feature value] tail}
    (and (|> actual
             (/.value feature)
             (maybe#each (text#= value))
             (maybe.else false))
         (subsumes? expected tail))))

(def .public for
  (syntax (_ [specializations (<>.some (<>.and ..configuration <code>.any))
              default (<>.maybe <code>.any)])
    (do meta.monad
      [actual meta.configuration]
      (when (list#mix (function (_ [expected then] choice)
                        (if (subsumes? actual expected)
                          {.#Some then}
                          choice))
                      default
                      specializations)
        {.#Some it}
        (in (list it))
        
        {.#None}
        (meta.failure (exception.error ..invalid []))))))