forked from miking-lang/miking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoml-ext.mc
261 lines (202 loc) · 9.01 KB
/
toml-ext.mc
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
include "map.mc"
/-
Implements functionality for reading and writing TOML data.
A TOML table is a sequence of (key, value) pairs, where each key is a string,
and each value is one of:
- integer
- float
- string
- TOML table
- a sequence of values
Boolean and date data types are currently not supported.
-/
type TomlTable
type TomlValue
------------------
-- READING TOML --
------------------
-- 'tomlFromString str' parses 'str' into a toml table.
external externalTomlFromStringExn ! : String -> TomlTable
let tomlFromStringExn = lam str. externalTomlFromStringExn str
utest tomlFromStringExn "key=1"; () with ()
-- 'tomlFindExn key table' returns the value bound to 'key' in 'table'.
external externalTomlFindExn ! : String -> TomlTable -> TomlValue
let tomlFindExn = lam str. lam table. externalTomlFindExn str table
utest tomlFindExn "key" (tomlFromStringExn "key=1"); () with ()
-- 'tomlBindings table' returns the bindings of 'table'.
external externalTomlBindings ! : TomlTable -> [(String, TomlValue)]
let tomlBindings = lam table. externalTomlBindings table
utest
let binds = tomlBindings (tomlFromStringExn "intval=1\nstringval=\"abc\"") in
let keys = map (lam b : (String, TomlValue). b.0) binds in
keys
with ["intval", "stringval"]
-- 'tomlBindings table' converts 'table' into a map.
let tomlTableToMap : TomlTable -> Map String TomlValue = lam table.
mapFromSeq cmpString (tomlBindings table)
-- 'tomlValueToString v' converts a toml value to a string, regardless of the
-- type of the value.
external externalTomlValueToString ! : TomlValue -> String
let tomlValueToString = lam v. externalTomlValueToString v
utest
let t = tomlFromStringExn
"
key1=1
key2=3.14
key3=[3.14,4.1]
key4=\"value\"
key5=[[]]
"
in
map (lam b : (String, TomlValue). tomlValueToString b.1) (tomlBindings t)
with ["1", "3.14", "[3.14, 4.1]", "\"value\"","[[]]"]
-- 'tomlValueToIntExn v' converts a toml value to an integer.
external externalTomlValueToIntExn ! : TomlValue -> Int
let tomlValueToIntExn = lam v. externalTomlValueToIntExn v
utest tomlValueToIntExn (tomlFindExn "key" (tomlFromStringExn "key=1")) with 1
-- 'tomlValueToStringExn v' converts a toml value to a string.
external externalTomlValueToStringExn ! : TomlValue -> String
let tomlValueToStringExn = lam v. externalTomlValueToStringExn v
utest tomlValueToStringExn (tomlFindExn "key" (tomlFromStringExn "key=\"value\"")) with "value"
-- 'tomlValueToFloatExn v' converts a toml value to a float.
external externalTomlValueToFloatExn ! : TomlValue -> Float
let tomlValueToFloatExn = lam v. externalTomlValueToFloatExn v
utest tomlValueToFloatExn (tomlFindExn "key" (tomlFromStringExn "key=3.14")) with 3.14
-- 'tomlValueToTableExn v' converts a toml value to a toml table.
external externalTomlValueToTableExn ! : TomlValue -> TomlTable
let tomlValueToTableExn = lam v. externalTomlValueToTableExn v
utest
let t1 = tomlFromStringExn
"
[key]
subkey=1
"
in
let t2 = tomlValueToTableExn (tomlFindExn "key" t1) in
tomlValueToIntExn (tomlFindExn "subkey" t2)
with 1
-- 'tomlValueToIntSeqExn v' converts a toml value to an integer sequence.
external externalTomlValueToIntSeqExn ! : TomlValue -> [Int]
let tomlValueToIntSeqExn = lam v. externalTomlValueToIntSeqExn v
utest tomlValueToIntSeqExn (tomlFindExn "key" (tomlFromStringExn "key=[1,2,3]")) with [1,2,3]
utest tomlValueToIntSeqExn (tomlFindExn "key" (tomlFromStringExn "key=[]")) with []
-- 'tomlValueToStringSeqExn v' converts a toml value to a string sequence.
external externalTomlValueToStringSeqExn ! : TomlValue -> [String]
let tomlValueToStringSeqExn = lam v. externalTomlValueToStringSeqExn v
utest tomlValueToStringSeqExn (tomlFindExn "key" (tomlFromStringExn "key=[\"foo\", \"bar\"]")) with ["foo", "bar"]
utest tomlValueToStringSeqExn (tomlFindExn "key" (tomlFromStringExn "key=[]")) with []
-- 'tomlValueToFloatSeqExn v' converts a toml value to a float sequence.
external externalTomlValueToFloatSeqExn ! : TomlValue -> [Float]
let tomlValueToFloatSeqExn = lam v. externalTomlValueToFloatSeqExn v
utest tomlValueToFloatSeqExn (tomlFindExn "key" (tomlFromStringExn "key=[3.14,1e1]")) with [3.14,1e1]
utest tomlValueToFloatSeqExn (tomlFindExn "key" (tomlFromStringExn "key=[]")) with []
-- 'tomlValueToTableSeqExn v' converts a toml value to a sequence of toml
-- tables.
external externalTomlValueToTableSeqExn ! : TomlValue -> [TomlTable]
let tomlValueToTableSeqExn = lam v. externalTomlValueToTableSeqExn v
utest
let t1 = tomlFromStringExn
"
[[fruit]]
name = \"apple\"
[[fruit]]
name = \"orange\"
"
in
let t2 : [TomlTable] = tomlValueToTableSeqExn (tomlFindExn "fruit" t1) in
let vals = map (lam t. tomlFindExn "name" t) t2 in
map tomlValueToStringExn vals
with ["apple", "orange"]
utest
let t = tomlFromStringExn "[[fruit]]" in
tomlValueToTableSeqExn (tomlFindExn "fruit" t)
with []
-- 'tomlValueToSeqSeqExn v' converts a toml value to a sequence of sequence of
-- toml values.
external externalTomlValueToSeqSeqExn ! : TomlValue -> [TomlValue]
let tomlValueToSeqSeqExn = lam v. externalTomlValueToSeqSeqExn v
utest
let t1 = tomlFromStringExn
"
seq_of_seq = [[1,2],[3]]
"
in
let t2 : [TomlValue] = tomlValueToSeqSeqExn (tomlFindExn "seq_of_seq" t1) in
map tomlValueToIntSeqExn t2
with [[1,2],[3]]
------------------
-- WRITING TOML --
------------------
let _strEqNoWhitespace = lam s1. lam s2.
let s1 = filter (lam c. not (isWhitespace c)) s1 in
let s2 = filter (lam c. not (isWhitespace c)) s2 in
eqString s1 s2
-- 'tomlToString table' converts 'table' into a string.
external externalTomlToString ! : TomlTable -> String
let tomlToString = lam table. externalTomlToString table
utest tomlToString (tomlFromStringExn "key=1") with "key=1" using _strEqNoWhitespace
-- 'tomlFromBindings binds' converts 'binds' to a table.
external externalTomlFromBindings ! : [(String, TomlValue)] -> TomlTable
let tomlFromBindings = lam binds. externalTomlFromBindings binds
-- 'tomlIntToValue v' converts an integer to a toml value.
external externalTomlIntToValue ! : Int -> TomlValue
let tomlIntToValue = lam v. externalTomlIntToValue v
utest tomlToString (tomlFromBindings [("key", tomlIntToValue 1)]) with "key=1" using _strEqNoWhitespace
-- 'tomlStringToValue v' converts a string to a toml value.
external externalTomlStringToValue ! : String -> TomlValue
let tomlStringToValue = lam s. externalTomlStringToValue s
utest tomlToString (tomlFromBindings [("key", tomlStringToValue "42")]) with "key=\"42\"" using _strEqNoWhitespace
-- 'tomlFloatToValue v' converts a float to a toml value.
external externalTomlFloatToValue ! : Float -> TomlValue
let tomlFloatToValue = lam v. externalTomlFloatToValue v
utest tomlToString (tomlFromBindings [("key", tomlFloatToValue 3.14)]) with "key=3.14" using _strEqNoWhitespace
-- 'tomlTableToValue v' converts a toml table to a toml value.
external externalTomlTableToValue ! : TomlTable -> TomlValue
let tomlTableToValue = lam v. externalTomlTableToValue v
utest
let t1 = tomlFromBindings [("subkey", tomlIntToValue 1)] in
let t2 = tomlFromBindings [("key", tomlTableToValue t1)] in
tomlToString t2
with
"
[key]
subkey=1
"
using _strEqNoWhitespace
-- 'tomlIntSeqToValue v' converts an integer to a toml value.
external externalTomlIntSeqToValue ! : [Int] -> TomlValue
let tomlIntSeqToValue = lam v. externalTomlIntSeqToValue v
utest tomlToString (tomlFromBindings [("key", tomlIntSeqToValue [1,2,3])]) with "key=[1,2,3]" using _strEqNoWhitespace
utest tomlToString (tomlFromBindings [("key", tomlIntSeqToValue [])]) with "key=[]" using _strEqNoWhitespace
-- 'tomlStringSeqToValue v' converts a string to a toml value.
external externalTomlStringSeqToValue ! : [String] -> TomlValue
let tomlStringSeqToValue = lam s. externalTomlStringSeqToValue s
utest tomlToString (tomlFromBindings [("key", tomlStringSeqToValue ["42","43"])]) with "key=[\"42\", \"43\"]" using _strEqNoWhitespace
-- 'tomlFloatSeqToValue v' converts a float to a toml value.
external externalTomlFloatSeqToValue ! : [Float] -> TomlValue
let tomlFloatSeqToValue = lam v. externalTomlFloatSeqToValue v
utest tomlToString (tomlFromBindings [("key", tomlFloatSeqToValue [3.14])]) with "key=[3.14]" using _strEqNoWhitespace
-- 'tomlTableSeqToValue v' converts a sequence of toml tables to a toml value.
external externalTomlTableSeqToValue ! : [TomlTable] -> TomlValue
let tomlTableSeqToValue = lam v. externalTomlTableSeqToValue v
utest
let t1 = tomlFromBindings [("name", tomlStringToValue "apple")] in
let t2 = tomlFromBindings [("name", tomlStringToValue "orange")] in
let t3 = tomlFromBindings [("fruit", tomlTableSeqToValue [t1, t2])] in
tomlToString t3
with
"
[[fruit]]
name = \"apple\"
[[fruit]]
name = \"orange\"
"
using _strEqNoWhitespace
-- 'tomlSeqSeqToValue v' converts a sequence of toml values to a toml value
external externalTomlSeqSeqToValue ! : [TomlValue] -> TomlValue
let tomlSeqSeqToValue = lam v. externalTomlSeqSeqToValue v
utest
let t = tomlFromBindings
[("key", tomlSeqSeqToValue [tomlIntSeqToValue [1,2], tomlIntSeqToValue [3]])] in
tomlToString t
with "key=[[1,2],[3]]" using _strEqNoWhitespace