aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/configuration.lux
blob: be16a511007cabef7d54300c1c4f7999bd6b5517 (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
(.using
 [library
  [lux (.except for)
   ["[0]" meta]
   [abstract
    [equivalence (.only Equivalence)]
    [monoid (.only Monoid)]
    [monad (.only do)]]
   [control
    ["[0]" maybe (.open: "[1]#[0]" functor)]
    ["[0]" exception (.only exception:)]
    ["<>" parser (.only)
     ["<[0]>" text (.only Parser)]
     ["<[0]>" code]]]
   [data
    ["[0]" text (.open: "[1]#[0]" equivalence)
     ["%" format]]
    [collection
     ["[0]" list (.open: "[1]#[0]" functor mix)]
     [dictionary
      ["/" plist]]]]
   [macro
    [syntax (.only syntax:)]
    ["[0]" code]]
   [math
    [number (.only hex)]]]])

(type: .public Configuration
  (/.PList Text))

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

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

(def: .public empty
  Configuration
  /.empty)

(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: .public invalid)

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

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

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

(syntax: .public (for [specializations (<>.some (<>.and ..configuration <code>.any))
                       default (<>.maybe <code>.any)])
  (do meta.monad
    [actual meta.configuration]
    (case (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 [])))))