-
Notifications
You must be signed in to change notification settings - Fork 2
/
luatoml.lua
287 lines (266 loc) · 6.51 KB
/
luatoml.lua
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
require "util"
pprint = require "pl.pretty"
debug = false
function strip(str)
return string.format( "%s", str:match( "^%s*(.-)%s*$" ) )
end
function replace_char(pos, str, r)
return ("%s%s%s"):format(str:sub(1,pos-1), r, str:sub(pos+1))
end
function splitLineToGroupArray(line)
local retVal = {}
local i = 1
for group in string.gmatch(line, "[^.]+") do
retVal[i] = group
i = i + 1
end
return retVal
end
function tableSize(table)
local size = 0
for i, v in pairs(table) do
size = size + 1
end
return size
end
function splitLine(str)
local openArr = 0
local openString = false
local beginLine = true
local keyGroup = false
local delNum = 1
for i = 1, #str do
local char = string.sub(str, i, i)
if char == '"' and string.sub(str, i-1, i-1) ~= '\\' then
openString = not openString
end
if keyGroup and (char == ' ' or char == '\t') then
keyGroup = false
end
if char == '#' and not openString and not keyGroup then
local j = i
while string.sub(str, j, j) ~= '\n' do
str = replace_char(j, str, ' ')
j = j + 1
end
end
if char == '[' and not openString and not keyGroup then
if beginLine then
keyGroup = true
else
openArr = openArr + 1
end
end
if char == ']' and not openString and not keyGroup then
if keyGroup then
keyGroup = false
else
openArr = openArr - 1
end
end
if char == '\n' then
if openString then error("Unbalanced Quotes") end
if openArr ~= 0 then
str = replace_char(i, str, ' ')
else
beginLine = true
end
elseif beginLine and char ~= ' ' and char ~= '\t' then
beginLine = false
keyGroup = true
else
end
end -- end of for loop
return str
end
function splitAssignment(str)
local retVal = {}
local i = 1
for group in string.gmatch(str, "[^=]+") do
retVal[i] = group
i = i + 1
end
return retVal
end
function load(str)
--[[--
Load a TOML string into Lua object (deserialization)
@Parameter: str
The str which is parsed in as TOML
@Returns: A table of nested components of TOML string
--]]--
local implicitGroups = {}
local retVal = {}
local currentLevel = retVal
str = splitLine(str)
for line in string.gmatch(str, "[^\n]+") do
line = strip(line)
if string.sub(line, 1, 1) == '[' then
line = string.sub(line, 2, #line-1) -- key group name
currentLevel = retVal
groups = splitLineToGroupArray(line)
for i, group in ipairs(groups) do
if group == "" then error("Can't have keygroup with empty name") end
if currentLevel[group] ~= nil then
if i == tableSize(groups) then
if implicitGroups[group] ~= nil then
implicitGroups[group] = nil
else
error("Group existed")
end
end
else
if i ~= tableSize(groups) then
implicitGroups[group] = true
end
currentLevel[group] = {}
end
currentLevel = currentLevel[group]
end -- end of for loop
elseif line:match("=") ~= nil then
assignment = splitAssignment(line)
variable = strip(assignment[1])
value = strip(assignment[2])
loadedVal = load_value(value)
value = loadedVal["value"]
if currentLevel[variable] ~= nil then
error("Duplicated key")
else
currentLevel[variable] = value
end
end
end
return retVal
end
function load_value(v)
-- boolean
if v == "true" then
return { value=true, type="boolean" }
-- boolean
elseif v == "false" then
return { value=false, type="boolean" }
-- string, handle simple string for now
elseif string.sub(v, 1, 1) == '"' then
local lengthV = string.len(v)
local str = string.sub(v, 2, lengthV-1)
return { value=str, type="string" }
-- array
elseif string.sub(v, 1, 1) == '[' then
return { value=load_array(v), type="table" }
-- datetime, handle as string for now
elseif string.len(v) == 20 and string.sub(v, 20, 20) == 'Z' then
local lengthV = string.len(v)
local str = string.sub(v, 1, lengthV-1)
return { value=str, type="string" }
-- number
else
local negative = false
if string.sub(v, 1, 1) == '-' then
negative = true
local lengthV = string.len(v)
v = string.sub(v, 2, lengthV)
end
v = tonumber(v)
if negative then v = 0 - v end
return { value=v, type="number" }
end
end
function load_array(a)
local retVal = {}
a = string.format( "%s", a:match( "^%s*(.-)%s*$" ) )
a = string.sub(a, 2, #a - 1)
local newArr = {}
local openArr = 0
local j = 1
local index = 1
for i = 1, #a do
local char = string.sub(a, i, i)
if char == "[" then
openArr = openArr + 1
elseif char == "]" then
openArr = openArr - 1
elseif char == "," and openArr == 0 then
local subStr = string.sub(a, j, i-1)
subStr = string.format( "%s", subStr:match( "^%s*(.-)%s*$" ) )
newArr[index] = subStr
j = i + 1
index = index + 1
end
end
local subStr = string.sub(a, j, #a)
subStr = string.format( "%s", subStr:match( "^%s*(.-)%s*$" ) )
newArr[index] = subStr
local aType = nil
local index = 1
for i, v in ipairs(newArr) do
if v ~= '' then
local loadedVal = load_value(v)
local nVal = loadedVal["value"]
local nType = loadedVal["type"]
if aType then
if aType ~= nType then
error("Not homogeneous array")
end
else
aType = nType
end
retVal[index] = nVal
index = index + 1
end
end
return retVal
end
function dump(obj)
--[[--
Dump Lua object into TOML string (serialization)
@Parameter: obj
The Lua table
@Returns: A TOML string
--]]--
local addToSections
local retVal = ""
local dumpedSection = dump_section(obj)
local addToRetVal = dumpedSection["retStr"]
local sections = dumpedSection["retDict"]
retVal = retVal .. addToRetVal
while tableSize(sections) ~= 0 do
local newSections = {}
for section, value in pairs(sections) do
dumpedSection = dump_section(sections[section])
addToRetVal = dumpedSection["retStr"]
addToSections = dumpedSection["retDict"]
if addToRetVal ~= nil then
retVal = retVal .. "[" .. section .. "]\n"
retVal = retVal .. addToRetVal
end
for s, value in pairs(addToSections) do
newSections[section .. "." .. s] = addToSections[s]
end
end
sections = newSections
end
return retVal
end
function dump_section(obj)
local retStr = ""
local retDict = {}
for i, section in pairs(obj) do
if type(obj[i]) ~= "table" then
retStr = retStr .. i .. " = "
retStr = retStr .. dump_value(obj[i]) .. '\n'
else
retDict[i] = obj[i]
end
end
return { retStr=retStr, retDict=retDict }
end
function dump_value(v)
if type(v) == "string" then
local retStr = '"' .. v .. '"'
return retStr
elseif type(v) == "boolean" then
return tostring(v)
else
return v
end
end