-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.lua
More file actions
548 lines (507 loc) · 15.2 KB
/
parser.lua
File metadata and controls
548 lines (507 loc) · 15.2 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
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
function readonlytable(table)
return setmetatable({}, {
__index = table,
__newindex = function(table, key, value)
error('Attempt to modify read-only table')
end,
__metatable = false,
__len = function(o)
local i = 1
while o[i] ~= nil do
i = i+1
end
return i-1
end
});
end
local errorupdate = function (parserstate, errmsg)
assert(parserstate,'something fishy in errorupdate')
assert(errmsg, 'errmsg is nil in errorupdate')
assert(type(errmsg) == 'string', string.format('errmsg is not a string in errorupdate type: <%s>', type(errmsg)))
local newstate = {}
newstate[1] = parserstate[1]
newstate.iserror = true
newstate.errormsg = errmsg
newstate.result = nil
newstate.index = parserstate.index
return readonlytable(newstate)
end
local update = function (parserstate, index, result)
assert(index,'index should not be nil in update')
assert(index > 0, index)
assert(result,'result should not be nil in update')
local newstate = {}
newstate[1] = parserstate[1]
newstate.index = index
newstate.result = result
newstate.iserror = false
return readonlytable(newstate)
end
local Parser = {}
Parser.__index = Parser
setmetatable(Parser, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function Parser.new(f)
assert(f, 'f is nil in Parser.new')
assert(type(f) == 'function', 'f is not a function in Parser.new')
local self = setmetatable({}, Parser)
self.f = f
return self
end
function Parser:run(targetstring)
assert(targetstring, 'run got nil targetstring')
local initialstate = readonlytable{
targetstring,
index = 1,
result = {},
error = false,
}
return self.f(initialstate)
end
function Parser:map(fn)
return self.new(function (parserstate)
local nextstate = self.f(parserstate)
if nextstate.iserror then return nextstate end
return update(nextstate,nextstate.index , fn(nextstate.result))
end)
end
function Parser:errormap(fn)
return self.new(function (parserstate)
local nextstate = self.f(parserstate)
if not(nextstate.iserror) then
return nextstate
end
return errorupdate(nextstate, fn(nextstate.errormsg, nextstate.index))
end)
end
function Parser:chain(fn)
return self.new(function (parserstate)
local nextstate = self.f(parserstate)
if(nextstate.iserror) then return nextstate end
local parser = fn(nextstate.result)
return parser.f(nextstate)
end)
end
Parser.string_parser = function (s)
return Parser(function (parserstate)
local targetstring = parserstate[1]
local index = parserstate.index
local iserror = parserstate.iserror
if iserror then return parserstate end
assert(targetstring, 'targetstring should not be nil in string parser')
local sliced =targetstring:sub(index)
if #sliced == 0 then return errorupdate(parserstate, string.format("unexpected eof at %d", index)) end
if sliced:sub(1,s:len()) == s then
return update(parserstate, index + #s, s)
end
local emsg =string.format('cannot match "%s" got "%s" at %d', s ,targetstring:sub(index) , index)
return errorupdate ( parserstate, emsg )
end
)
end
Parser.letters =
Parser(function (parserstate)
local targetstring = parserstate[1]
local index = parserstate.index
local iserror = parserstate.iserror
if iserror then return parserstate end
assert(targetstring, 'targetstring should not be nil in numbers parser')
local sliced =targetstring:sub(index)
if #sliced == 0 then return errorupdate(parserstate, string.format("letters: unexpected eof at %d", index)) end
local match =sliced:match('^%a+')
if match then
return update(parserstate, index + #match, match)
end
local emsg =string.format('cannot match "%s" got "%s" at %d', 'any letter' ,targetstring:sub(index) , index)
return errorupdate ( parserstate, emsg )
end
)
Parser.numbers = Parser(function (parserstate)
local targetstring = parserstate[1]
local index = parserstate.index
local iserror = parserstate.iserror
assert(targetstring, 'targetstring should not be nil in numbers parser')
if iserror then
return parserstate
end
local sliced =targetstring:sub(index)
if #sliced == 0 then return errorupdate(parserstate, string.format("numbers: unexpected eof at %d", index)) end
local match =sliced:match('^%d+')
if match then
return update(parserstate, index + #match, match)
end
local emsg =string.format('cannot match "%s" got "%s" at %d', 'any numbers' ,targetstring:sub(index) , index)
return errorupdate ( parserstate, emsg )
end
)
Parser.sequenceof = function (parsers)
assert(#parsers >= 1, 'sequenceof should have at least one parser')
return Parser(function (parserstate)
if parserstate.iserror then return parserstate end
local results = {}
local nextstate = parserstate
for i, p in ipairs(parsers) do
assert(p~=nil, string.format('xth parser in sequence of is nil x = %d',i))
nextstate = p.f(nextstate)
table.insert(results,nextstate.result)
end
return {
nextstate[1],
index = nextstate.index,
iserror = nextstate.iserror,
errormsg = nextstate.errormsg,
result = results,
}
end
)
end
Parser.many = function (parser)
return Parser(function (parserstate)
if parserstate.iserror then return parserstate end
local results = {}
local nextstate = parserstate
local done = false
local it = 0
while not(done) do
local teststate = parser.f(nextstate)
if not(teststate.iserror) then
table.insert(results,teststate.result)
nextstate = teststate
else
done = true
end
end
return update(nextstate, nextstate.index, results)
end
)
end
Parser.many1 = function (parser)
return Parser(function (parserstate)
if parserstate.iserror then return parserstate end
local results = {}
local nextstate = parserstate
local done = false
while not(done) do
local teststate = parser.f(nextstate)
if not(teststate.iserror) then
table.insert(results,teststate.result)
nextstate = teststate
else
done = true
end
end
if #results == 0 then
local emsg = string.format('many1: Unable to match any input using parser @ index= %d',nextstate.index)
return errorupdate(nextstate,emsg)
end
return update( nextstate, nextstate.index, results)
end
)
end
Parser.choice = function (parsers)
return Parser(function (parserstate)
assert(parserstate, 'something ifshy in choice')
if parserstate.iserror then
return parserstate
end
for i, p in ipairs(parsers) do
assert(p, 'parser nil in choice')
local nextstate = p.f(parserstate)
if not(nextstate.iserror) then
return nextstate
end
end
local emsg =string.format(
'choice: Unable to match with any parser at %d',
parserstate.index
)
return errorupdate(parserstate,emsg)
end
)
end
local gettableresult
gettableresult = function(t)
local res = "{"
for k,v in pairs(t) do
if type(v) == "table" then
res = res .. k .. ' : ' .. gettableresult(v) .. ', '
else
res = res .. k .. ' : ' .. tostring(v) .. ', '
end
end
res = res .. '}'
return res
end
Parser.print_parser_state = function (state)
local f;
assert(state[1])
if state.iserror then
f = string.format('{targetstring "%s", errormsg: %s}',state[1],state.errormsg)
else
local r
if type(state.result) == "table" then
r = gettableresult(state.result)
else
r = state.result
end
f = string.format('{targetstring "%s" result:%s, index: %d } ',state[1] , r , state.index)
end
print(f)
end
Parser.between = function (left, right)
return function (content)
local s = Parser.sequenceof ( { left, content , right} )
return s:map(function (x) return x[2] end )
end
end
Parser.sepby = function (seperatorparser)
return function(valueparser)
return Parser(function (parserstate)
if parserstate.iserror then return parserstate end
local results = {}
local nextstate = parserstate
while true do
local thethingwewant = valueparser.f(nextstate)
if thethingwewant.iserror then
break
end
table.insert(results,thethingwewant.result)
nextstate = thethingwewant
local sepstate = seperatorparser.f(nextstate)
if sepstate.iserror then
break
end
nextstate = sepstate
end
return update(nextstate,nextstate.index, results)
end)
end
end
Parser.sepby1 = function (seperatorparser)
return function(valueparser)
return Parser(function (parserstate)
if parserstate.iserror then return parserstate end
local results = {}
local nextstate = parserstate
while true do
local thethingwewant = valueparser.f(nextstate)
if thethingwewant.iserror then
break
end
table.insert(result,thethingwewant.result)
nextstate = thethingwewant
local sepstate = seperatorparser.f(nextstate)
if sepstate.iserror then
break
end
nextstate = sepstate
end
if #results == 0 then
local emsg = string.format('sepby1: Unable to match any input using parser @ index= %d',nextstate.index)
return errorupdate(nextstate,emsg)
end
return update(nextstate,nextstate.index, results)
end)
end
end
Parser.lazy = function (thunk)
local f = function()
-- TODO: consider one time evaluation for parser
while true do coroutine.yield( thunk()) end
end
local thread = coroutine.create(f)
return Parser(
function (parserstate)
local s,v = coroutine.resume(thread)
return v.f(parserstate)
end
)
end
Parser.bitparser_lsb = Parser (function(parserstate)
if parserstate.iserror then return parserstate end
local byteoffset= math.floor((parserstate.index - 1) / 8)
local bitoffset = (parserstate.index - 1) % 8
assert(bitoffset,"bitoffset should be non nil")
local byte = string.byte(parserstate[1],byteoffset + 1)
assert(byte,"byte should be non nil")
local result = bit32.rshift(
bit32.band(byte,
bit32.lshift(1, bitoffset)
)
, bitoffset
)
return update(parserstate,parserstate.index + 1, result)
end)
Parser.one = Parser (function(parserstate)
if parserstate.iserror then return parserstate end
local byteoffset= math.floor( (parserstate.index - 1) / 8)
local bitoffset = (parserstate.index - 1) % 8
assert(bitoffset,"bitoffset should be non nil")
local byte = string.byte(parserstate[1],byteoffset + 1)
assert(byte,"byte should be non nil")
local result = bit32.rshift(
bit32.band(byte,
bit32.lshift(1, bitoffset)
)
, bitoffset
)
if result ~= 1 then
local emsg = string.format("expected one but got 0 @ index %d",parserstate.index)
return errorupdate(parserstate,emsg)
end
return update(parserstate,parserstate.index + 1, result)
end)
Parser.zero = Parser (function(parserstate)
if parserstate.iserror then return parserstate end
local byteoffset= math.floor( (parserstate.index - 1) / 8)
local bitoffset = (parserstate.index - 1) % 8
assert(bitoffset,"bitoffset should be non nil")
local byte = string.byte(parserstate[1],byteoffset + 1)
assert(byte,"byte should be non nil")
local result = bit32.rshift(
bit32.band(byte,
bit32.lshift(1, bitoffset)
)
, bitoffset
)
if result ~= 0 then
local emsg = string.format("expected zero but got 1 @ index %d",parserstate.index)
return errorupdate(parserstate,emsg)
end
return update(parserstate,parserstate.index + 1, result)
end)
Parser.bitparser= Parser (function(parserstate)
if parserstate.iserror then return parserstate end
local byteoffset= math.floor((parserstate.index - 1) / 8)
local bitoffset = 7 - (parserstate.index - 1) % 8
assert(bitoffset,"bitoffset should be non nil")
local byte = string.byte(parserstate[1],byteoffset + 1)
assert(byte,"byte should be non nil")
local result = bit32.rshift(
bit32.band(byte,
bit32.lshift(1, bitoffset)
)
, bitoffset
)
return update(parserstate,parserstate.index + 1, result)
end)
Parser.uint_le = function (n)
local bitparsers = {}
for i=1,n do
table.insert(bitparsers, Parser.bitparser_lsb)
end
local bp = Parser.sequenceof(bitparsers)
bp = bp:map( function (x)
local res = 0
for i,v in pairs(x) do
res = res + 2^(i-1) * v
end
return res
end
)
return bp
end
Parser.byte = Parser(function (parserstate)
if parserstate.iserror then return parserstate end
local byte = string.byte(parserstate[1],parserstate.index)
assert(byte,"byte should be non nil")
return update(parserstate, parserstate.index+1, byte)
end
)
Parser.uint= function (nbytes, endiann)
assert(endiann == "<" or endiann == ">", "incorrect endiannness in Parser.uint")
local byteparsers = {}
for i=1,nbytes do
table.insert(byteparsers, Parser.byte)
end
local bp = Parser.sequenceof(byteparsers)
bp = bp:map( function (x)
local res = 0
for i,v in pairs(x) do
if endiann == "<" then
res = res + (2^8)^(i - 1) * v -- little endiann
else
res = res + (2^8)^(nbytes-1 -(i -1)) * v -- bigendiann
end
end
return res
end
)
return bp
end
Parser.int= function (nbytes, endiann)
assert(nil, "Parser.int is not implemented yet")
assert(endiann == "<" or endiann == ">", "incorrect endiannness in Parser.int")
local byteparsers = {}
for i=1,nbytes do
table.insert(byteparsers, Parser.byte)
end
local bp = Parser.sequenceof(byteparsers)
bp = bp:map( function (x)
local res = 0
for i,v in pairs(x) do
if endiann == "<" then
res = res + (2^8)^(i - 1) * v -- little endiann
else
res = res + (2^8)^(nbytes-1 -(i -1)) * v -- bigendiann
end
end
return res
end
)
return bp
end
Parser.int_le = function (n)
local bitparsers = {}
for i=1,n do
table.insert(bitparsers, Parser.bitparser_lsb)
end
local bp = Parser.sequenceof(bitparsers)
bp = bp:map( function (x)
local res = 0
for i,v in pairs(x) do
if (i == n) then
res = res - 2^(i-1) * v
else
res = res + 2^(i-1) * v
end
end
return res
end
)
return bp
end
Parser.fail = function(emsg)
return Parser(function (parserstate)
return errorupdate(parserstate,emsg)
end
)
end
Parser.success = function(result)
return Parser(function (parserstate)
return update(parserstate,parserstate.index,result)
end
)
end
Parser.binary_str = function (s)
local byteparsers = {}
for i=1,#s do
local p = Parser.byte
p = p:chain(function(r)
local expected = string.byte(s, i)
if r == expected then
return Parser.success(r)
else
return Parser.fail(string.format("expected char '%c' but got '%c' @ %d", expected, r, i))
end
end
)
table.insert(byteparsers, p)
end
local sp = Parser.sequenceof (byteparsers)
return sp
end
return Parser