-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
325 lines (284 loc) · 8.29 KB
/
Copy pathinit.lua
File metadata and controls
325 lines (284 loc) · 8.29 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
-- (c) twiswist 2022
-- Permission to use, copy, modify, and/or distribute this software for
-- any purpose with or without fee is hereby granted. This software is
-- offered as-is, without any warranty.
-- local function trace(...)
-- return print(...)
-- end
local function err(where, why)
return error("syntax error: " .. why .. " at " .. where, 2)
end
local function ass(there, where, why)
return there or error("syntax error: " .. why .. " at " .. where, 2)
end
----------------------------------------
local
parseJson,
parseWhitespace,
parseValue,
parseObject,
parseArray,
parseString,
parseNumber,
parseOther,
parseMembers,
parseElements,
parseMember,
parseElement
----------------------------------------
function parseJson(str)
coroutine.yield() -- take str argument from jsons.parser() and wait for the actual user
-- trace("parseJson")
local where = 1
if string.sub(str, 1, 3) == "\xef\xbb\xbf" then where = 4 end -- UTF-8 BOM
where = parseElement(str, where)
ass(where == #str + 1, where, "trailing data")
return where
end
----------------------------------------
function parseWhitespace(str, where)
-- trace("parseWhitespace", where)
-- see also: empty element detection in parseArray and parseObject
return string.match(str, "^[ \r\n\t]*()", where)
end
function parseValue(str, where)
-- trace("parseValue", where)
return parseObject(str, where)
or parseArray(str, where)
or parseString(str, where)
or parseNumber(str, where)
or parseOther(str, where)
end
----------------------------------------
function parseObject(str, where)
-- trace("parseObject", where)
if string.sub(str, where, where) ~= "{" then return end
coroutine.yield("object", where)
local there = string.match(str, "^[ \r\n\t]*}()", where + 1)
if there then
coroutine.yield()
return there
else
where = parseMembers(str, where + 1)
ass(string.sub(str, where, where) == "}", where, "unterminated object")
coroutine.yield()
return where + 1
end
end
function parseArray(str, where)
-- trace("parseArray", where)
if string.sub(str, where, where) ~= "[" then return end
coroutine.yield("array", where)
local there = string.match(str, "^[ \r\n\t]*%]()", where + 1)
if there then
coroutine.yield()
return there
else
where = parseElements(str, where + 1)
ass(string.sub(str, where, where) == "]", where, "unterminated array")
coroutine.yield()
return where + 1
end
end
local escapes = {
['"'] = '"',
["\\"] = "\\",
["/"] = "/",
["b"] = "\b",
["f"] = "\f",
["n"] = "\n",
["r"] = "\r",
["t"] = "\t",
}
function parseString(str, where)
-- note: this does not validate unicode
-- it also probably gets \uxxxx escapes completely wrong
-- trace("parseString", where)
if string.sub(str, where, where) ~= '"' then return end
local there = where + 1
local after, what = there
local rope = {}
while true do
after = string.match(str, '^[^%z\1-\31"\\]*()', after)
if (there ~= after) then table.insert(rope, string.sub(str, there, after - 1)) end
what = string.sub(str, after, after)
after = after + 1 -- after the "what" character
if what == '"' then
coroutine.yield("string", where, table.concat(rope), string.sub(str, where, after - 1))
return after
elseif what == "\\" then
what = string.sub(str, after, after)
if what == "u" then
-- kind of stupid to check for "u" first...
what = ass(string.match(str, "^%x%x%x%x", after + 1), after, "incomplete unicode escape sequence")
if string.sub(str, after + 1, after + 2) ~= "00" then
table.insert(rope, string.char(tonumber(string.sub(str, after + 1, after + 2), 16)))
end
table.insert(rope, string.char(tonumber(string.sub(str, after + 3, after + 4), 16)))
after = after + 5
else
what = ass(escapes[what], after, "unrecognized escape sequence")
table.insert(rope, what)
after = after + 1
end
else
err(after, "malformed string")
end
there = after
end
end
function parseNumber(str, where)
-- trace("parseNumber", where)
local there = where
if string.sub(str, where, where) == "-" then where = where + 1 end
where = string.match(str, "^0()", where) or string.match(str, "^%d+()", where)
if where == nil then return end
where = string.match(str, "^%.%d+()", where) or where
where = string.match(str, "^[eE][-+]?%d+()", where) or where
local result = string.sub(str, there, where - 1)
coroutine.yield("number", there, tonumber(result), result) -- yeah just tonumber it
return where
end
function parseOther(str, where)
if string.sub(str, where, where + 3) == "true" then coroutine.yield("boolean", where, true , "true" ) return where + 4 end
if string.sub(str, where, where + 4) == "false" then coroutine.yield("boolean", where, false, "false") return where + 5 end
if string.sub(str, where, where + 3) == "null" then coroutine.yield("nil" , where, nil , "null" ) return where + 4 end
end
----------------------------------------
function parseMembers(str, where)
-- trace("parseMembers", where)
where = parseMember(str, where)
while string.sub(str, where, where) == "," do
where = parseMember(str, where + 1)
end
return where
end
function parseElements(str, where)
-- trace("parseElements", where)
where = parseElement(str, where)
while string.sub(str, where, where) == "," do
where = parseElement(str, where + 1)
end
return where
end
----------------------------------------
function parseMember(str, where)
-- trace("parseMember", where)
where = parseWhitespace(str, where)
where = ass(parseString(str, where), where, "expected string")
where = parseWhitespace(str, where)
ass(string.sub(str, where, where) == ":", where, "expected :")
return parseElement(str, where + 1)
end
function parseElement(str, where)
-- trace("parseElement", where)
where = parseWhitespace(str, where)
where = ass(parseValue(str, where), where, "unrecognizable value")
return parseWhitespace(str, where)
end
----------------------------------------
local function line(level) return "\n" .. string.rep("\t", level) end
-- the quick rundown on this function:
-- print() is output, get() is input,
-- level is indentation, type and value act as one token of lookahead
local function pretty(print, level, get, type, value)
if type == "array" then
print("[")
type, value = get()
if type ~= nil then
print(line(level + 1))
pretty(print, level + 1, get, type, value)
for type, value in get do
print("," .. line(level + 1))
pretty(print, level + 1, get, type, value)
end
print(line(level))
end
print("]")
elseif type == "object" then
print("{")
local key, key = get()
if key ~= nil then
type, value = get()
print(line(level + 1) .. key .. ": ")
pretty(print, level + 1, get, type, value)
for key, key in get do
type, value = get()
print("," .. line(level + 1) .. key .. ": ")
pretty(print, level + 1, get, type, value)
end
print(line(level))
end
print("}")
else
print(value)
end
end
local function minify(print, get, type, value)
if type == "array" then
print("[")
type, value = get()
if type ~= nil then
minify(print, get, type, value)
for type, value in get do
print(",")
minify(print, get, type, value)
end
end
print("]")
elseif type == "object" then
print("{")
local key, key = get()
if key ~= nil then
type, value = get()
print(key .. ":")
minify(print, get, type, value)
for key, key in get do
type, value = get()
print("," .. key .. ":")
minify(print, get, type, value)
end
end
print("}")
else
print(value)
end
end
----------------------------------------
local jsons = {}
function jsons.parser(str)
local parser = coroutine.wrap(parseJson)
parser(str)
return parser
end
-- warning: parser() will just skip any UTF-8 BOM, and pretty() does not preserve it
function jsons.pretty(parser)
local rope = {}
local function parser2()
local type, value, value, value = parser()
return type, value
end
pretty(
function(x) return table.insert(rope, x) end,
0,
parser2,
parser2()
)
parser() -- check for trailing data
return table.concat(rope)
end
function jsons.minify(parser)
local rope = {}
local function parser2()
local type, value, value, value = parser()
return type, value
end
minify(
function(x) return table.insert(rope, x) end,
parser2,
parser2()
)
parser() -- check for trailing data
return table.concat(rope)
end
return jsons