Debian Codegraph @

texmf-dist/doc/support/cluttex/src/config-file.sml

raw
1 structure ConfigFile:
2 sig
3 type config =
4 { temporary_directory: string option
5 , color: { type_: ANSIStyle.style option
6 , execute: ANSIStyle.style option
7 , error: ANSIStyle.style option
8 , warning: ANSIStyle.style option
9 , diagnostic: ANSIStyle.style option
10 , information: ANSIStyle.style option
11 }
12 }
13 val defaultConfig: config
14 val loadConfig: string -> config
15 end =
16 struct
17
18 type config =
19 { temporary_directory: string option
20 , color: { type_: ANSIStyle.style option
21 , execute: ANSIStyle.style option
22 , error: ANSIStyle.style option
23 , warning: ANSIStyle.style option
24 , diagnostic: ANSIStyle.style option
25 , information: ANSIStyle.style option
26 }
27 }
28
29 val defaultConfig: config =
30 { temporary_directory = NONE
31 , color =
32 { type_ = NONE
33 , execute = NONE
34 , error = NONE
35 , warning = NONE
36 , diagnostic = NONE
37 , information = NONE
38 }
39 }
40
41 fun get (table: TomlValue.table, key) =
42 case List.find (fn (key', _) => key' = key) table of
43 NONE => NONE
44 | SOME (_, value) => SOME value
45
46 fun weakGet (SOME table : TomlValue.table option, key) =
47 (case List.find (fn (key', _) => key' = key) table of
48 NONE => NONE
49 | SOME (_, value) => SOME value)
50 | weakGet (NONE, _) = NONE
51
52 infix ?|> ??
53 (* Option.mapPartial, flipped *)
54 fun (SOME x) ?|> f = f x
55 | NONE ?|> _ = NONE
56
57 val op?? = Option.getOpt
58
59 (*: val checkBool : string -> TomlValue.value -> bool option *)
60 fun checkBool _ (TomlValue.BOOL x) = SOME x
61 | checkBool path _ =
62 (Message.warn ("Config entry " ^ path ^ " should be a boolean."); NONE)
63
64 (*: val checkString : string -> TomlValue.value -> string option *)
65 fun checkString _ (TomlValue.STRING x) = SOME x
66 | checkString path _ =
67 (Message.warn ("Config entry " ^ path ^ " should be a string."); NONE)
68
69 (*: val checkTable : string -> TomlValue.value -> TomlValue.table option *)
70 fun checkTable _ (TomlValue.TABLE x) = SOME x
71 | checkTable path _ =
72 (Message.warn ("Config entry " ^ path ^ " should be a table."); NONE)
73
74 (*: val checkColor : string -> TomlValue.value -> ANSIColor.color option *)
75 fun checkColor path (TomlValue.STRING x) =
76 (case ANSIColor.fromString x of
77 SOME c => SOME c
78 | NONE =>
79 ( Message.warn
80 ("Config entry " ^ path ^ " should be a valid color.")
81 ; NONE
82 ))
83 | checkColor path _ =
84 (Message.warn ("Config entry " ^ path ^ " should be a string."); NONE)
85
86 (*: val parseStyle : string -> TomlValue.value -> ANSIStyle.style option *)
87 fun parseStyle path (TomlValue.TABLE t) =
88 SOME
89 { foreground = get (t, "fore") ?|> checkColor (path ^ ".fore")
90 , background = get (t, "back") ?|> checkColor (path ^ ".back")
91 , bold = get (t, "bold") ?|> checkBool (path ^ ".bold") ?? false
92 , dim = get (t, "dim") ?|> checkBool (path ^ ".dim") ?? false
93 , underline =
94 get (t, "underline") ?|> checkBool (path ^ ".underline") ?? false
95 , blink = get (t, "blink") ?|> checkBool (path ^ ".blink") ?? false
96 , reverse =
97 get (t, "reverse") ?|> checkBool (path ^ ".reverse") ?? false
98 , italic = get (t, "italic") ?|> checkBool (path ^ ".italic") ?? false
99 , strike = get (t, "strike") ?|> checkBool (path ^ ".strike") ?? false
100 }
101 | parseStyle path _ =
102 (Message.warn ("Config entry " ^ path ^ " should be a table."); NONE)
103
104 fun loadConfig path =
105 let
106 val ins = TextIO.openIn path
107 val ins' = ValidateUtf8.mkValidatingStream (TextIO.getInstream ins)
108 val table =
109 ParseToml.parse (ValidateUtf8.validatingReader TextIO.StreamIO.input1)
110 ins'
111 in
112 { temporary_directory =
113 get (table, "temporary-directory")
114 ?|> checkString "temporary-directory"
115 , color =
116 let
117 val color = get (table, "color") ?|> checkTable "color"
118 in
119 { type_ = weakGet (color, "type") ?|> parseStyle "color.type"
120 , execute =
121 weakGet (color, "execute") ?|> parseStyle "color.execute"
122 , error = weakGet (color, "error") ?|> parseStyle "color.error"
123 , warning =
124 weakGet (color, "warning") ?|> parseStyle "color.warning"
125 , diagnostic =
126 weakGet (color, "diagnostic") ?|> parseStyle "color.diagnostic"
127 , information =
128 weakGet (color, "information")
129 ?|> parseStyle "color.information"
130 }
131 end
132 }
133 end
134 end;