Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
forked from Neopallium/LuaNativeObjects
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord.lua
More file actions
Latest commit
605 lines (575 loc) · 14.5 KB
/
Copy pathrecord.lua
File metadata and controls
605 lines (575 loc) · 14.5 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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
-- Copyright (c) 2010 by Robert G. Jakabosky <bobby@neoawareness.com>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
localtinsert,tremove=table.insert,table.remove
localtappend=function(dst,src) for_,vinpairs(src) dodst[#dst+1] =vendend
-- records that are not contained in other records
localroot_records={}
localrecord_type_groups={}
localclear_funcs={}
localglobal_scope={}
functionclear_all_records()
root_records={}
record_type_groups={}
-- run clear functions
fori=1,#clear_funcsdo
localfunc=clear_funcs[i]
func()
end
global_scope={}
end
functionreg_clear_func(func)
clear_funcs[#clear_funcs+1] =func
end
-- get a named record with rec_type
functionget_named_record(rec_type, name)
localrecs=record_type_groups[rec_type]
ifrecs~=nilthen
returnrecs[name]
end
end
localfunctiongroup_add_record(rec, rec_type)
-- add this record to the list of records with the same type.
localtype_group=record_type_groups[rec_type]
iftype_group==nilthen
type_group= {}
record_type_groups[rec_type] =type_group
end
type_group[#type_group+1] =rec
ifname~=nilthen
type_group[name] =rec
end
end
localvariable_format="%s_idx%d"
functionset_variable_format(format)
variable_format=format
end
functionformat_variable(name, idx)
returnvariable_format:format(name, idx)
end
-- Meta-table used by all records.
localignore_record={_rec_type="ignore"}
localrec_meta
rec_meta={
clear=function(self)
self._vars= {}
self._data_parts= {}
self._rec_counts= {}
end,
--
-- add data output functions to record.
--
-- replace variables in "part"
vars_part=function(self, part)
localtmpl=self:dump_parts({part})
tmpl=tmpl:gsub("%${(.-)}", self._vars)
self._data_parts[part] = {tmpl}
returntmpl
end,
-- replace variables in "parts"
vars_parts=function(self, parts)
localout={}
parts=self:parts(parts)
-- apply variables to all "parts".
fori=1,#partsdo
localpart=parts[i]
locald=self:vars_part(part)
out[#out+1] =d
end
returnout
end,
-- append data to "part"
write_part=function(self, part, data)
iftype(data) ~="table" then
ifdata==nilthenreturnend
data= { tostring(data) }
end
localout=self._data_parts[part]
ifout==nilthen
out= {}
self._data_parts[part] =out
end
-- append data.
tappend(out, data)
end,
parts=function(self, parts)
-- make sure "parts" is a table.
ifparts==nilthen
parts= {}
forpartinpairs(self._data_parts) doparts[#parts+1] =partend
elseiftype(parts) ~="table" then
parts= { parts }
end
returnparts
end,
dump_parts=function(self, parts, sep)
localout={}
parts=self:parts(parts)
-- return all parts listed in "parts".
localdata=self._data_parts
fori=1,#partsdo
localpart=parts[i]
locald_part=data[part]
ifd_partthen
tappend(out, d_part)
end
ifsep~=nilthen
out[#out+1] =sep
end
end
returntable.concat(out)
end,
-- copy parts from "src" record.
copy_parts=function(self, src, parts)
parts=src:parts(parts)
fori=1,#partsdo
localpart=parts[i]
self:write_part(part, src:dump_parts(part))
end
end,
--
-- functions for counting sub-records
--
get_sub_record_count=function(self, _rec_type)
localcount=self._rec_counts[_rec_type]
ifcount==nilthencount=0end
returncount
end,
count_sub_record=function(self, rec)
localcount=self:get_sub_record_count(rec._rec_type)
count=count+1
self._rec_counts[rec._rec_type] =count
rec._rec_idx=count
end,
--
-- functions for adding named variables
--
add_var=function(self, key, value)
self._vars[key] =value
end,
add_rec_var=function(self, rec, name, vname, idx)
localname=nameorrec.name
localidx=idxorrec._rec_idx
self._vars[name] =vnameorformat_variable(name, idx)
self._vars[name.."::idx"] =idx
end,
--
-- sub-records management functions
--
make_sub_record=function(self, parent)
localroot_idx
-- find record in roots list
foridx=1,#root_recordsdo
ifroot_records[idx] ==selfthen
root_idx=idx
break
end
end
-- remove it from the roots list
ifroot_idx~=nilandroot_records[root_idx] ==selfthen
tremove(root_records, root_idx)
end
rawset(self, "_parent", parent)
end,
insert_record=function(self, rec, pos)
rec:make_sub_record(self)
ifpos~=nilthen
tinsert(self, pos, rec)
else
self[#self+1] =rec
end
end,
add_record=function(self, rec)
self:insert_record(rec)
end,
find_record=function(self, rec)
fori=1,#selfdo
localsub=self[i]
ifsub==recthen
returni
end
end
end,
replace_record=function(self, old_rec, new_rec)
fori=1,#selfdo
localsub=self[i]
ifsub==old_recthen
self[i] =new_rec
returni
end
end
end,
remove_record=function(self, rec)
fori=1,#selfdo
localsub=self[i]
ifsub==recthen
rawset(self, i, ignore_record) -- have to insert an empty table in it's place.
rawset(sub, "_parent", nil)
return
end
end
end,
--
-- delete a record and all it's sub-records
--
delete_record=function(self)
-- remove from parent.
ifself._parent~=nilthen
self._parent:remove_record(self)
end
-- delete sub-records
fori=1,#selfdo
localsub=self[i]
ifis_record(sub) andsub._parent==selfthen
self[i] =nil
sub:delete_record()
rawset(sub, "_parent", nil)
end
end
-- ignore this record and it sub-records
self._rec_type="ignore"
end,
--
-- Copy record and all it's sub-records.
--
copy_record=function(self)
localcopy= {}
-- copy values from current record.
fork,vinpairs(self) do
copy[k] =v
end
rawset(copy, "_parent", nil) -- unlink from old parent
-- copy sub-records
fori=1,#copydo
localsub=copy[i]
ifis_record(sub) then
localsub_copy=sub:copy_record()
rawset(copy, i, sub_copy)
rawset(sub_copy, "_parent", copy)
end
end
setmetatable(copy, rec_meta)
group_add_record(copy, copy._rec_type)
returncopy
end,
--
-- Symbol resolver
--
add_symbol=function(self, name, obj, scope)
-- default scope 'local'
ifscope==nilthenscope="local" end
-- if scope is global then skip local maps.
ifscope=='global' then
global_scope[name] =obj
return
end
-- add symbol to local map
self._symbol_map[name] =obj
-- if scope is doesn't equal our scope
ifscope~=self.scopeandself._parent~=nilthen
self._parent:add_symbol(name, obj, scope)
end
end,
get_symbol=function(self, name)
-- check our mappings
localobj=self._symbol_map[name]
-- check parent if we don't have a mapping for the symbol
ifobj==nilandself._parent~=nilthen
obj=self._parent:get_symbol(name)
end
-- next check the imports for the symbol
ifobj==nilthen
localimports=self._imports
fori=1,#importsdo
localimport=imports[i]
obj=import:get_symbol(name)
ifobj~=nilthen
break
end
end
end
-- next check the globals for the symbol
ifobj==nilthen
obj=global_scope[name]
end
returnobj
end,
-- import symbols from a "file" record
add_import=function(self, import_rec)
localimports=self._imports
-- if already imported then skip
ifimports[import_rec] thenreturnend
imports[import_rec] =true
-- append to head of imports list so that the last import overrides symbols
-- from the previous imports
table.insert(imports, 1, import_rec)
end,
}
rec_meta.__index=rec_meta
functionis_record(rec)
-- use a metatable to identify records
return (getmetatable(rec) ==rec_metaandrec._rec_type~=nil)
end
setmetatable(ignore_record, rec_meta)
localfunctionremove_child_records_from_roots(rec, seen)
-- make sure we don't get in a reference loop.
ifseen==nilthenseen= {} end
ifseen[rec] thenreturnend
seen[rec] =true
-- remove from root list.
fori=1,#recdo
localval=rec[i]
ifis_record(val) then
val:make_sub_record(rec)
end
end
end
localfunctionend_record(rec)
iftype(rec) ~='function' thenreturnrecend
localrc, result=pcall(rec, nil)
ifnotrcthen
print("Error processing new record: " ..result)
returnrec
end
returnend_record(result)
end
functionmake_record(rec, rec_type, name, scope)
ifrec==nilthenrec= {} end
iftype(rec) ~="table" thenrec= { rec } end
-- set record's name.
ifname==nilthenname=rec_typeend
rec.name=name
-- record's symbol scope
rec.scope=scope
rec._symbol_map= {}
rec._imports= {}
-- make "rec" into a record.
rec._rec_type=rec_type
setmetatable(rec, rec_meta)
-- complete partial child records.
fori=1,#recdo
localval=rec[i]
iftype(val) =='function' then
val=end_record(val)
rec[i] =val
end
end
-- remove this record's child records from the root list.
remove_child_records_from_roots(rec)
-- add this record to the root list.
root_records[#root_records+1] =rec
group_add_record(rec, rec_type)
returnrec
end
--
-- Record parser
--
localfunctionrecord_parser(callbacks, name)
name=nameor"parse"
localfunctioncall_meth(self, rec_type, post, rec, parent)
localfunc=self[rec_type..post]
iffunc==nilthen
func=self["unknown" ..post]
iffunc==nilthenreturnend
end
returnfunc(self, rec, parent)
end
localseen={}
callbacks=setmetatable(callbacks, {
__call=function(self, rec, parent)
-- make sure it is a valid record.
ifnotis_record(rec) orseen[rec] orrec._rec_type=="ignore" thenreturnend
ifparentthen
parent:count_sub_record(rec) -- count sub-records.
end
-- keep track of records we have already processed
seen[rec] =true
localrec_type=rec._rec_type
-- clear record's data output & sub-record counts.
rec:clear()
-- start record.
call_meth(self, rec_type, "", rec, parent)
-- transverse into sub-records
for_,vinipairs(rec) do
self(v, rec)
end
-- end record
call_meth(self, rec_type, "_end", rec, parent)
-- update "last_type"
self.last_type=rec_type
end
})
returncallbacks
end
functionprocess_records(parser)
record_parser(parser)
-- process each root record
fori=1,#root_recordsdo
parser(root_records[i])
end
returnparser
end
localstages= {}
functionreg_stage_parser(stage, parser)
localparsers=stages[stage]
ifparsers==nilthen
-- new stage add it to the end of the stage list.
stages[#stages+1] =stage
parsers= {}
stages[stage] =parsers
end
parsers[#parsers+1] =parser
end
-- setup default stages
localdefault_stages= { "symbol_map", "imports" }
-- run all parser stages.
functionrun_stage_parsers()
fori=1,#stagesdo
localstage=stages[i]
localparsers=stages[stage]
forx=1,#parsersdo
process_records(parsers[x])
end
end
end
functionmove_recs(dst, src, idx)
-- move records from "rec" to it's parent
fori=1,#srcdo
localrec=src[i]
ifis_record(rec) andrec._rec_type~="ignore" then
src:remove_record(rec) -- remove from src
ifidxthen
dst:insert_record(rec, idx) -- insert into dst
idx=idx+1
else
dst:add_record(rec) -- add to dst
end
end
end
-- now delete this empty container record
src:delete_record()
end
--
-- Record functions -- Used to create new records.
--
functionmake_generic_rec_func(rec_type, no_name)
if_G[rec_type] ~=nilthenerror("global already exists with that name: " ..rec_type) end
ifnotno_namethen
_G[rec_type] =function(name)
returnfunction(rec)
rec=make_record(rec, rec_type, name)
returnrec
end
end
else
_G[rec_type] =function(rec)
rec=make_record(rec, rec_type, rec_type)
returnrec
end
end
end
localpath_char=package.config:sub(1,1)
localpath_match='(.*)' ..path_char
localpath_stack= {''}
functionsubfile_path(filename)
locallevel=#path_stack
localcur_path=path_stack[level] or''
returncur_path..filename
end
functionsubfiles(files)
locallevel=#path_stack
localcur_path=path_stack[level]
localrc
level=level+1
-- use a new roots list to catch records from subfiles
localprev_roots=root_records
root_records={}
-- process subfiles
fori=1,#filesdo
localfile=files[i]
-- add current path to file
file=cur_path..file
-- seperate file's path from the filename.
localfile_path=file:match(path_match) or''
if#file_path>0then
file_path=file_path..path_char
end
-- push the file's path onto the path_stack only if it is different.
ifcur_path~=file_paththen
path_stack[level] =file_path
end
-- check file path
print("Parsing records from file: " ..file)
rc= {dofile(file)}
-- pop path
ifcur_path~=file_paththen
path_stack[level] =nil
end
end
-- move sub-records into new array
localrec={}
fori=1,#root_recordsdo
rec[i] =root_records[i]
end
-- switch back to previous roots list
root_records=prev_roots
-- make this into a record holding the sub-records from each of the sub-files
rec=make_record(rec, "subfiles")
if#rc>0then
returnrec, rc
end
returnrec
end
-- process some container records
reg_stage_parser("containers", {
subfiles=function(self, rec, parent)
localidx=parent:find_record(rec)
move_recs(parent, rec, idx)
end,
})
localsubfolders={}
functionsubfolder(folder)
returnfunction(...)
localfiles=select(1, ...)
iftype(files) ~='table' then
files= {...}
end
-- push subfolder
subfolders[#subfolders+1] =folder
-- build full path
folder=table.concat(subfolders, "/") .."/"
fori=1,#filesdo
files[i] =folder..files[i]
end
-- use subfile record.
localrec=subfiles(files)
-- pop subfolder
subfolders[#subfolders] =nil
returnrec
end
end
functionimport(name)
rec=make_record({}, "import", name)
returnrec
end
-- resolve imports
reg_stage_parser("imports", {
import=function(self, rec, parent)
end,
})