forked from qwreey/parser-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
572 lines (512 loc) · 14.8 KB
/
init.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
--!strict
--[[lit-meta
name = "qwreey/parser"
version = "2.1.5"
dependencies = {}
description = "Parsing support lib"
tags = { "lua", "parsing" }
license = "MIT"
author = { name = "qwreey", email = "[email protected]" }
homepage = "https://github.com/qwreey/parser.lua"
]]
--[[
todo
{"'", "[["}, func
style handler
should impl
- cache
]]
local concat = table.concat
local insert = table.insert
local remove = table.remove
-- flatten templates
local function flatten(comps: any)
local cur,tmp = 1,comps[1]
while tmp do
if type(tmp) == "table" and rawget(tmp,"__template") then
flatten(tmp)
remove(comps,cur)
for i,v in ipairs(tmp) do
insert(comps,cur+i-1,v)
end
end
-- next
cur = cur + 1
tmp = comps[cur]
end
end
local function isCacheable(regex)
if regex:sub(1,1) == "^" then return false end
return true
end
local function createParser(comps: { [any]: any })
comps.__index = comps
comps.__parser = true
-- flatten templates
flatten(comps)
-- Update super for children
for key,v in pairs(comps) do
if key ~= "__index" and key ~= "super" and type(v) == "table" and rawget(v,"__parser") then
if v.super then error("this parser already has super") end
if v.super ~= true then
v.super = comps
end
end
end
-- unpacking
local regexs = {}
local funcs = {}
local cacheable = {}
local len do
local index = 1
local cur = 1
while comps[cur*2-1] do
local regex = comps[cur*2-1]
local func = comps[cur*2]
if func == nil then
error("Regular expressions and handler functions are not paired. (length: "..tostring(#comps)..")")
end
if type(regex) == "table" then -- unpack table
for _,childRegex in ipairs(regex :: any) do
cacheable[index] = isCacheable(childRegex)
regexs[index] = childRegex
funcs[index] = func
index = index + 1
end
else
cacheable[index] = isCacheable(regex)
regexs[index] = regex
funcs[index] = func
index = index + 1
end
cur = cur + 1
end
len = index-1
end
-- main parser function
local function parse(self,str,start,userdata,verbose)
local state = setmetatable({parent = self, __state = true},comps)
local findCache = {}
local strlen = #str
local pos = start or 1
local lastpos
local init = comps.init
local stop = comps.stop
local orphans = comps.orphans
local tailing = comps.tailing
local noEOF = comps.noEOF
-- local ignoreIndexs = {}
-- Run init function
if init then
local initPos,initEndup = init(state,str,pos,userdata)
if initPos then pos = initPos end -- move position if updated
if initEndup then -- check endup
local result = state
if stop then
result = stop(state)
end
return result,pos
end
end
-- Loop find
while true do
if verbose then print("LOOP: pos "..pos) end
-- Check infinity loop
if pos == lastpos then
error("Infinite loop detected")
end
lastpos = pos
-- Check buffer exhaust
if pos > strlen then
if noEOF then -- EOF is not required
-- Make result
local result = state
if stop then
result = stop(state)
end
return result,strlen
end
error("Buffer exhausted") -- EOF is required
end
-- Loop matchers
local minStartAt = math.huge
local minFind
local minIndex
for index=1,len do
local find
-- Check old cache by position (use memoization for performance)
local oldFind = findCache[index]
if cacheable[index] and oldFind and oldFind[1] and oldFind[1] >= pos then
find = oldFind
elseif (not cacheable[index]) or (not oldFind) or (oldFind[1] and oldFind[1] < pos) then
local regex = regexs[index]
find = table.pack(string.find(str,regex,pos))
findCache[index] = find
end
-- Check min startat
local startAt = find and find[1]
if startAt and minStartAt > startAt then
minStartAt = startAt
minFind = find
minIndex = index
end
end
if minFind then
-- If token found
if verbose then
print("From "..pos..": "..regexs[minIndex].." ("..minStartAt.."~"..minFind[2]..")")
end
-- Push orphans
if minStartAt-1 >= pos then
local orphanString = string.sub(str,pos,minStartAt-1)
if orphanString ~= "" then
if orphans then
if verbose then print(" | Push orphans : "..orphanString) end
orphans(state,orphanString,false)
else
-- if no orphan handler. orphan is not allowed. end parsing
local result = state
if stop then
result = stop(state)
end
return result,pos - 1
end
end
end
-- Call function and update position
local func = funcs[minIndex]
local nextPos, endup = func(state,str,pos,table.unpack(minFind,1,minFind.n))
pos = nextPos or (minFind[2] + 1)
if verbose then
print(" | Next : "..pos..", EndUp : "..tostring(endup))
end
-- If parse ended
if endup then
local result = state
if stop then
result = stop(state)
end
return result,pos - 1
end
else
-- no token found
if verbose then print("No more token") end
-- if EOF is required
if not noEOF then
error("No token found")
end
-- Process tailing
if tailing and strlen >= pos then
local tailingString = string.sub(str,pos,strlen)
if tailingString ~= "" then
if verbose then print(" | Push tailing : "..tailingString) end
tailing(state,tailingString)
end
pos = strlen + 1
end
-- Make result
local result = state
if stop then
result = stop(state)
end
return result,pos-1
end
end
end
setmetatable(comps,{
__call = function (_,self,...)
if type(self) == "table" and rawget(self,"__state") then
return parse(self,...)
end
return parse(nil,self::any,...)
end
})
return comps
end
local whitespaces = "%s+"
local function ignore(_,_,_,_,endAt: any)
return endAt+1
end
local function eof(_,_,_,_,endAt: any)
return endAt+1,true
end
local stringEscapes = setmetatable({
["n"] = "\n";
["r"] = "\r";
["f"] = "\f";
["v"] = "\v";
["b"] = "\b";
["\\"] = "\\";
["t"] = "\t";
["\""] = "\"";
["\'"] = "\'";
["a"] = "\a";
["z"] = "\z";
},{ __index = function(self,key) return key end })
local function luaStringEscapes(func)
return {
__template = true,
"\\u{(%x+)}", function (self,str,pos,startAt,endAt,hex) return func(self,str,pos,startAt,endAt,utf8.char(tonumber(hex,16) :: any)) end,
"\\(%d%d?%d?)", function (self,str,pos,startAt,endAt,num) return func(self,str,pos,startAt,endAt,string.char(num)) end,
"\\x(%x%x?)", function (self,str,pos,startAt,endAt,hex) return func(self,str,pos,startAt,endAt,string.char(tonumber(hex,16) :: any)) end,
"\\(.)", function (self,str,pos,startAt,endAt,char) return func(self,str,pos,startAt,endAt,stringEscapes[char]) end,
} :: {
[number]: any
}
end
local function luaNumbers(func)
return {
__template = true,
"(%d*%.?%d+[eE][%+%-]?%d+)", function (self,str,pos,startAt,endAt,expo)
return func(self,str,pos,startAt,endAt,tonumber(expo))
end,
"(0x%x*%.?%x+)", function (self,str,pos,startAt,endAt,hex)
return func(self,str,pos,startAt,endAt,tonumber(hex))
end,
"(0b[10]*%.?[10]+)", function (self,str,pos,startAt,endAt,bin)
return func(self,str,pos,startAt,endAt,tonumber(bin))
end,
"(%d*%.?%d+)", function (self,str,pos,startAt,endAt,num)
return func(self,str,pos,startAt,endAt,tonumber(num))
end,
} :: {
[number]: any
}
end
local luaStringParser = createParser {
-- Block
blockParser = createParser {
"%]%]", eof;
orphans = function (self,str) insert(self.parent.buffer,str) end;
tailing = function (self,str) insert(self.parent.buffer,str) end;
};
"^%[%[", function (self,str,pos,startAt,endAt: any)
local _,parseEndAt: any = self:blockParser(str,endAt+1)
return parseEndAt+1,true
end;
-- Double Quote
doubleQuoteParser = createParser {
"\"", eof;
orphans = function (self,str) insert(self.parent.buffer,str) end;
tailing = function (self,str) insert(self.parent.buffer,str) end;
luaStringEscapes(function (self,str,pos,startAt,endAt,char)
insert((self :: any).parent.buffer,char)
end);
};
"^\"", function (self,str,pos,startAt,endAt: any)
local _,parseEndAt: any = self:doubleQuoteParser(str,endAt+1)
return parseEndAt+1,true
end;
-- Single Quote
singleQuoteParser = createParser {
"\'", eof;
orphans = function (self,str) insert(self.parent.buffer,str) end;
tailing = function (self,str) insert(self.parent.buffer,str) end;
luaStringEscapes(function (self,str,pos,startAt,endAt,char)
insert((self :: any).parent.buffer,char)
end);
};
"^'", function (self,str,pos,startAt,endAt: any)
local _,parseEndAt: any = self:singleQuoteParser(str,endAt+1)
return parseEndAt+1,true
end;
init = function (self,str,pos) self.buffer = {} end;
stop = function (self) return concat(self.buffer) end;
}
local function luaStrings(func)
return {
__template = true,
{"\'","\"","%[%["}, function (self,str,pos,startAt,endAt)
local result, parseEndAt = luaStringParser(str,startAt)
local funcPos,funcStop = func(self,str,pos,startAt,parseEndAt,result)
return funcPos or (parseEndAt + 1), funcStop
end,
} :: {
[any]: any
}
end
local function luaValues(func)
return {
__template = true,
-- Boolean parsing
"true", function (self: any, str: any, pos: any, startAt: any, endAt: any)
local funcPos,funcStop = func(self,str,pos,startAt,endAt,true)
return funcPos or (endAt + 1), funcStop
end;
"false", function (self, str, pos, startAt, endAt)
local funcPos,funcStop = func(self,str,pos,startAt,endAt,false)
return funcPos or (endAt + 1), funcStop
end;
-- Nil parsing
"nil", function (self, str, pos, startAt, endAt)
local funcPos,funcStop = func(self,str,pos,startAt,endAt,nil :: any)
return funcPos or (endAt + 1), funcStop
end;
-- Number parsing
luaNumbers(function(self, str, pos, startAt, endAt: any, num: any)
local funcPos,funcStop = func(self,str,pos,startAt,endAt,num)
return funcPos or (endAt + 1), funcStop
end);
-- String parsing
luaStrings(function(self, str, pos, startAt, endAt, string: any): (any, any)
local funcPos,funcStop = func(self,str,pos,startAt,endAt,string)
return funcPos or (endAt + 1), funcStop
end);
} :: {
[any]: any
}
end
local luaValueParser = createParser {
stop = function (self)
return self.value
end;
-- common lua values
luaValues(function(self, str, pos, startAt, endAt: any, value): (any, any)
self.value = value
return endAt+1,true
end);
-- table parsing
objectParser = createParser {
init = function (self,str,pos)
self.terminatorRequired = false
self.orphanIndex = 1
end;
-- index by string
"^%s*([_%w][%w_]*)%s*=%s*", function (self, str, pos, startAt, endAt: any, key)
if self.terminatorRequired then
error("token [;,] is require on position "..tostring(startAt))
end
self.terminatorRequired = true
local value, valueEndAt: any = self.super(str,endAt+1)
self.parent.value[key] = value
self.terminatorRequired = true
return valueEndAt+1
end;
-- index by lua value
indexParser = createParser {
"^%s*]%s*=%s*", function (self, str, pos, startAt, endAt: any)
return endAt+1,true
end;
};
"^%s*%[", function (self, str, pos, startAt, endAt: any)
if self.terminatorRequired then
error("token [;,] is require on position "..tostring(startAt))
end
self.terminatorRequired = true
local key,keyEndAt: any = self.super(str,endAt+1)
local _,indexEndAt: any = self.indexParser(str,keyEndAt+1)
local value, valueEndAt = self.super(str,indexEndAt+1)
self.parent.value[key] = value
return valueEndAt+1
end;
-- next key
"^%s*[;,]", function (self, str, pos, startAt, endAt: any, key)
if not self.terminatorRequired then
error("Unexpected token "..str:sub(endAt,endAt) :: any .." at position "..tostring(endAt))
end
self.terminatorRequired = false
return endAt+1
end;
-- EOF
"^%s*}", function (self, str, pos, startAt, endAt: any)
return endAt+1,true
end;
-- any (array style items)
"^%s*().+", function (self, str, pos, startAt, endAt, valueStartAt)
if self.terminatorRequired then
error("token [;,] is require on position "..tostring(startAt))
end
self.terminatorRequired = true
local index: any = self.orphanIndex
local value, valueEndAt: any = self.super(str,valueStartAt)
self.parent.value[index] = value
self.orphanIndex = index+1
return valueEndAt+1
end;
whitespaces, ignore;
};
"{", function (self, str, pos, startAt, endAt: any)
-- pass self to object parser
self.value = {}
local _,parseEndAt: any = self:objectParser(str,endAt+1)
return parseEndAt+1,true
end;
whitespaces, ignore;
}
-- Allow : instead of =
-- Allow no ; or , (eg: { true true true { true } true } )
local luaNonstrictValueParser = createParser {
stop = function (self)
return self.value
end;
-- common lua values
luaValues(function(self, str, pos, startAt, endAt: any, value): (any, any)
self.value = value
return endAt+1,true
end);
-- table parsing
objectParser = createParser {
init = function (self,str,pos)
self.orphanIndex = 1
end;
-- index by string
"^%s*([_%w][%w_]*)%s*[=:]%s*", function (self, str, pos, startAt, endAt: any, key)
local value, valueEndAt: any = self.super(str,endAt+1)
self.parent.value[key] = value
return valueEndAt+1
end;
-- index by lua value
indexParser = createParser {
"^%s*]%s*[=:]%s*", function (self, str, pos, startAt, endAt: any)
return endAt+1,true
end;
};
"^%s*%[", function (self, str, pos, startAt, endAt: any, key)
local key,keyEndAt: any = self.super(str,endAt+1)
local _,indexEndAt: any = self.indexParser(str,keyEndAt+1)
local value, valueEndAt = self.super(str,indexEndAt+1)
self.parent.value[key] = value
return valueEndAt+1
end;
-- next key
"^%s*[;,]", function (self, str, pos, startAt, endAt: any, key)
return endAt+1
end;
-- EOF
"^%s*}", function (self, str, pos, startAt, endAt: any)
return endAt+1,true
end;
-- any (array style items)
"^%s*().+", function (self, str, pos, startAt, endAt, valueStartAt)
local index: any = self.orphanIndex
local value, valueEndAt: any = self.super(str,valueStartAt)
self.parent.value[index] = value
self.orphanIndex = index+1
return valueEndAt+1
end;
whitespaces, ignore;
};
"{", function (self, str, pos, startAt, endAt: any)
-- pass self to object parser
self.value = {}
local _,parseEndAt: any = self:objectParser(str,endAt+1)
return parseEndAt+1,true
end;
whitespaces, ignore;
}
return {
version = "2.1.5";
createParser = createParser;
-- parsers
luaNonstrictValueParser = luaNonstrictValueParser;
luaValueParser = luaValueParser;
luaStringEscapes = luaStringEscapes;
luaStringParser = luaStringParser;
-- utility
ignore = ignore;
whitespaces = whitespaces;
eof = eof;
-- templates
luaNumbers = luaNumbers;
luaStrings = luaStrings;
}