-
Notifications
You must be signed in to change notification settings - Fork 0
/
tampl.lua
443 lines (362 loc) · 12.3 KB
/
tampl.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
local TEMPLATE_TYPE = {}
local function lookupify(tbl)
local lookup = {}
for k,v in pairs(tbl) do lookup[v] = k end
return lookup
end
local function combined_lookup(a,b)
local lookup = {}
for k,v in pairs(b) do lookup[a[k]] = v end
return lookup
end
local keywords = {
"and", "break", "do", "else", "elseif",
"end", "false", "for", "function","if",
"in", "local", "nil", "not", "or",
"repeat","return","then","true", "until","while"
}
local lua_tokens = {
"+", "-", "*", "/", "%", "^","#",
"==","~=","<=",">=","<", ">","=",
"(", ")", "{", "}", "[", "]",
";", ":", ",", ".", "..","..."
}
local keyword_value_proccessor = {
value={
["true"] =function() return true end,
["false"]=function() return false end,
["nil"] =function() return nil end
},
}
local expansible_tokens = {
["="] ="=",
["=="] ="=",
["~"] ="=",
["~="] ="=",
["<"] ="=",
["<="] ="=",
[">"] ="=",
[">="] ="=",
["."] =".",
[".."] =".",
["..."] ="",
["-"] ="-",
["--"] ="[",
["["] ="[",
["[["] ="[",
["]"] ="]",
["]]"] ="]",
["--["] ="[",
["--[["]="["
}
local scope = {
open_begin = lookupify{
"if","elseif","while","for"
},
open = lookupify{
"do","else","then","function","repeat"
},
close = lookupify{
"end","elseif","else","until"
}
}
local positioning_types = {
"HEAD","TAIL","THIS"
}
local positioning_handles = {
function(tree,position,type,simple_override,tail_handle)
if not simple_override then
local tree_size = #tree
for i=tree_size,position,-1 do
local val = tree[i]
local hook = val.hook_info
if hook then hook.index = hook.index + 1 end
tree[i+1] = val
end
end
tree[position] = type
end,
function(tree,position,type,simple_override,tail_handle)
if not simple_override then
local tree_size = #tree
for i=tree_size,position,-1 do
local val = tree[i]
local hook = val.hook_info
if hook then hook.index = hook.index + 1 end
tree[i+1] = val
end
end
tree[position+(tail_handle or 0)] = type
end,
function(tree,position,type,tail_handle)
tree[position] = type
end
}
local positioning_offsets = {
0,1,1
}
local hook_types = {
"#","$","="
}
local hook_type_handles = {
function(tree,position,type,positioning)
positioning(tree,position,type)
end,
function(tree,position,type,positioning)
positioning(tree,position-1,type,true)
end,
function(tree,position,type,positioning)
positioning(tree,position-1,type,true,1)
end
}
local hooks = combined_lookup(hook_types,hook_type_handles)
local relative_positions = combined_lookup(positioning_types,positioning_handles)
local relative_offsets = combined_lookup(positioning_types,positioning_offsets)
local keyword_lookup = lookupify(keywords)
local token_lookup = lookupify(lua_tokens)
local function generate_tokens(str)
str = str .. "\0"
local tokens = {}
local token = ""
local is_string = false
local is_number = false
local is_comment = false
local is_mulline = false
local escape_next = false
local can_expand = ""
for i=1,#str-1 do
local char = str:sub(i,i)
local next_char = str:sub(i+1,i+1)
if (char == "\'" or char == "\"") and not is_comment then
if not escape_next then is_string = not is_string end
end
if char == "\\" then
escape_next = true
end
if char == "\n" and not is_mulline then is_string,is_comment,is_number = false,false,false end
if not is_string and (char:match("%d") or (char == "." and next_char:match("%d"))) then
is_number = true
elseif char ~= "." then
is_number = false
end
if char:match("[%s\t]") and not is_string and not is_comment and not is_number then
if token ~= "" then tokens[#tokens+1] = token end
token = ""
elseif token_lookup[char] and not is_string and not is_number and not is_comment then
if token ~= "" then tokens[#tokens+1] = token end
if not expansible_tokens[char] then
tokens[#tokens+1] = char
end
token = ""
elseif not expansible_tokens[char] or is_string or is_number or is_comment then
token = token .. char
escape_next = false
end
if (((expansible_tokens[can_expand] == char) or (can_expand == "" and expansible_tokens[char]))) and not is_number then
can_expand = can_expand .. char
if not expansible_tokens[can_expand .. next_char] then
if can_expand == "--" and not is_string and not is_number and not is_comment then
token = token .. "--"
is_comment = true
elseif (can_expand == "[[" or can_expand == "--[[") and not is_string and not is_number and not is_comment then
token = token .. can_expand
if can_expand == "--[[" and not escape_next then
is_comment = true
elseif can_expand == "[[" and not escape_next then
is_string = true
end
is_mulline = true
elseif can_expand == "]]" and is_mulline and not escape_next then
tokens[#tokens+1] = token
token = ""
is_mulline,is_string,is_comment,is_number = false,false,false,false
elseif not is_string and not is_number and not is_comment then
tokens[#tokens+1] = can_expand
end
can_expand = ""
end
end
end
if token ~= "" then tokens[#tokens+1] = token end
return tokens
end
local function make_value(token,token_buffer,token_index)
local out
if keyword_lookup[token] then
local keyword_type = keyword_lookup[token]
if keyword_type and keyword_value_proccessor[keyword_type] then
out = keyword_value_proccessor[keyword_type][token](token_buffer,token_index)
end
elseif token_lookup[token] then
out = "lua_token"
elseif token:match("^%-%-") or token:match("^%-%-%[%[.+%]%]$") then
out = token:match("^%-%-%[%[(.+)%]%]$") or token:match("^%-%-(.+)")
elseif token:match("^\".+\"$") or token:match("^%[%[.+%]%]$") then
out = token:match("^%[%[(.+)%]%]$") or token:match("^\"(.+)\"$")
elseif token:match("(%d*%.?%d+)") then
out = tonumber(token)
else out = token end
return out
end
local function make_type(token)
local out
local keyword_type
if keyword_lookup[token] then
out = "lua_keyword"
keyword_type = keyword_lookup[token]
elseif token_lookup[token] then
out = "lua_token"
elseif token:match("^%-%-") or token:match("^%-%-%[%[.+%]%]$") then
out = "comment"
elseif token:match("^\".+\"$") or token:match("^%[%[.+%]%]$") then
out = "string"
elseif token:match("(%d*%.?%d+)") then
out = "number"
else out = "name" end
return out,keyword_type
end
local TOKEN_MT = {__tostring=function(self) return "TOKEN: " .. self.type end}
local SCOPE_MT = {__tostring=function(self) return "SCOPE: " .. self.index end}
local function parse_token(out,token,token_buffer,token_index)
out.entry = "token"
out.name = token
out.type,out.keyword_type = make_type (token)
out.value = make_value(token,token_buffer,token_index)
setmetatable(out,TOKEN_MT)
return out
end
local function generate_extra_hook_info(t)
local hook = t.scope[t.index]
hook.hook_info = t
t.hook_comment = hook
t.name = hook.value:match("^.(.+)")
t.type = hook.value:match("^.")
return t
end
local function find_hooks(tree,lst)
lst = lst or {}
for k,v in ipairs(tree) do
if v.entry == "scope" then
find_hooks(v,lst)
elseif v.type == "comment" then
local new_index = #lst+1
local new = generate_extra_hook_info{scope=tree,index=k,hook_index=new_index}
if hooks[new.type] then
lst[new_index] = new
end
end
end
return lst
end
local function generate_code_tree(tokens)
local current_scope = {}
local token_buffer = {}
local buffer_open = false
local scope_index = 0
for i=1,#tokens do
local current_token = tokens[i]
if scope.open_begin[current_token] then
buffer_open = true
end
if buffer_open then
token_buffer[#token_buffer+1] = current_token
end
if scope.close[current_token] then
current_scope = current_scope.parent
end
current_scope[#current_scope+1] = parse_token({},current_token,t,i)
if scope.open[current_token] then
scope_index = scope_index + 1
local new_scope = setmetatable({
index = scope_index,
parent = current_scope,
keyword = current_token,
entry = "scope"
},SCOPE_MT)
token_buffer = {}
buffer_open = false
current_scope[#current_scope+1] = new_scope
current_scope = new_scope
end
end
return current_scope
end
local function load_template_tree(tree)
local template_hooks = find_hooks(tree)
local object;object = {
inject=function(hook_list,position,code)
local code_tree = code
local rebuild_on_end = false
if type(hook_list) ~= "table" then
error("Invalid hook",2)
end
if type(code_tree) == "table" and code_tree.type == TEMPLATE_TYPE then
code_tree = code.tree
rebuild_on_end = true
end
for k,hook in ipairs(hook_list) do
local scope = hook.scope
for k,v in ipairs(code_tree) do
hooks[hook.type](scope,hook.index+k*position.offset,v,position.pos)
end
end
if rebuild_on_end then object.rebuild() end
return object
end,
construct=function(input)
local code = ""
for k,v in ipairs(input or tree) do
if v.entry == "scope" then
code = code .. object.construct(v) .. "\n"
else
code = code .. v.name .. (v.type == "comment" and "\n" or " ")
end
end
return code
end,
rebuild = function()
local reconstructed = load_template_tree(tree)
for k,v in pairs(reconstructed) do
object[k] = v
end
return reconstructed
end,
tree = tree,
type = TEMPLATE_TYPE
}
for k,v in pairs(template_hooks) do
local index_name = ("_%s"):format(v.name)
if not object[index_name] then object[index_name] = {} end
local hook_named = object[index_name]
hook_named[#hook_named+1] = v
end
return object
end
local function load_template_tokens(tokens)
return load_template_tree(generate_code_tree(tokens))
end
local function load_template(data)
return load_template_tokens(generate_tokens(data))
end
local function load_template_file(path)
local file = fs.open(path,"r")
local data = file.readAll()
file.close()
return load_template_tokens(generate_tokens(data))
end
local function parse_code_block(data)
local tokens = generate_tokens (data)
local tree = generate_code_tree(tokens)
return tree
end
local function inject_table_position(tp)
return {pos=relative_positions[tp],offset=relative_offsets[tp]}
end
return {
new = load_template,
from_file = load_template_file,
from_tokens = load_template_tokens,
from_tree = load_template_tree,
parse = parse_code_block,
At = inject_table_position
}