- Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathetlua.lua
More file actions
Latest commit
409 lines (409 loc) · 10.4 KB
/
Copy pathetlua.lua
File metadata and controls
409 lines (409 loc) · 10.4 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
localVERSION="1.3.0"
localinsert, concat
do
local_obj_0=table
insert, concat=_obj_0.insert, _obj_0.concat
end
localload, setfenv, assert, type, error, tostring, tonumber, setmetatable
do
local_obj_0=_G
load, setfenv, assert, type, error, tostring, tonumber, setmetatable=_obj_0.load, _obj_0.setfenv, _obj_0.assert, _obj_0.type, _obj_0.error, _obj_0.tostring, _obj_0.tonumber, _obj_0.setmetatable
end
setfenv=setfenvorfunction(fn, env)
localname
locali=1
whiletruedo
name=debug.getupvalue(fn, i)
ifnotnameorname=="_ENV" then
break
end
i=i+1
end
ifnamethen
debug.upvaluejoin(fn, i, (function()
returnenv
end), 1)
end
returnfn
end
localhtml_escape_entities= {
['&'] ='&',
['<'] ='<',
['>'] ='>',
['"'] ='"',
["'"] ='''
}
localhtml_escape
html_escape=function(str)
return (str:gsub([=[["><'&]]=], html_escape_entities))
end
localget_line
get_line=function(str, line_num)
forlineinstr:gmatch("([^\n]*)\n?") do
ifline_num==1then
returnline
end
line_num=line_num-1
end
end
localpos_to_line
pos_to_line=function(str, pos)
localline=1
for_instr:sub(1, pos):gmatch("\n") do
line=line+1
end
returnline
end
localCompiler
do
local_class_0
local_base_0= {
html_escape=true,
render=function(self)
returntable.concat(self.buffer)
end,
push=function(self, str, ...)
locali=self.i+1
self.buffer[i] =str
self.i=i
if...then
returnself:push(...)
end
end,
header=function(self)
returnself:push("local _tostring, _escape, _b, _b_i = ...\n")
end,
footer=function(self)
returnself:push("return _b")
end,
increment=function(self)
returnself:push("_b_i = _b_i + 1\n")
end,
mark=function(self, pos)
returnself:push("--[[", tostring(pos), "]] ")
end,
assign=function(self, ...)
self:push("_b[_b_i] = ", ...)
if...then
returnself:push("\n")
end
end,
compile_chunks=function(self, chunks)
self:header()
for_index_0=1, #chunksdo
localchunk=chunks[_index_0]
localt=type(chunk)
ift=="table" then
t=chunk[1]
end
local_exp_0=t
if"string" ==_exp_0then
self:increment()
self:assign(("%q"):format(chunk))
elseif"code" ==_exp_0then
self:mark(chunk[3])
self:push(chunk[2], "\n")
elseif"=" ==_exp_0or"-" ==_exp_0then
self:increment()
self:mark(chunk[3])
self:assign()
ift=="=" andself.html_escapethen
self:push("_escape(_tostring(", chunk[2], "))\n")
else
self:push("_tostring(", chunk[2], ")\n")
end
else
error("unknown type " ..tostring(t))
end
end
self:footer()
returnself:render()
end
}
_base_0.__index=_base_0
_class_0=setmetatable({
__init=function(self)
self.buffer= { }
self.i=0
end,
__base=_base_0,
__name="Compiler"
}, {
__index=_base_0,
__call=function(cls, ...)
local_self_0=setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return_self_0
end
})
_base_0.__class=_class_0
Compiler=_class_0
end
localParser
do
local_class_0
local_base_0= {
open_tag="<%",
close_tag="%>",
modifiers="^[=-]",
next_tag=function(self)
localstart, stop=self.str:find(self.open_tag, self.pos, true)
ifnot (start) then
self:push_raw(self.pos, #self.str)
returnfalse
end
ifnot (start==self.pos) then
self:push_raw(self.pos, start-1)
end
self.pos=stop+1
localmodifier
ifself.str:match(self.modifiers, self.pos) then
do
local_with_0=self.str:sub(self.pos, self.pos)
self.pos=self.pos+1
modifier=_with_0
end
end
localclose_start, close_stop=self.str:find(self.close_tag, self.pos, true)
ifnot (close_start) then
returnnil, self:error_for_pos(start, "failed to find closing tag")
end
whileself:in_string(self.pos, close_start) do
close_start, close_stop=self.str:find(self.close_tag, close_stop, true)
ifnot (close_start) then
returnnil, self:error_for_pos(start, "failed to find string close")
end
end
localtrim_newline
if"-" ==self.str:sub(close_start-1, close_start-1) then
close_start=close_start-1
trim_newline=true
end
self:push_code(modifieror"code", self.pos, close_start-1)
self.pos=close_stop+1
iftrim_newlinethen
do
localmatch=self.str:match("^\n", self.pos)
ifmatchthen
self.pos=self.pos+#match
end
end
end
returntrue
end,
in_string=function(self, start, stop)
localin_string=false
localend_delim=nil
localescape=false
localpos=0
localskip_until=nil
localchunk=self.str:sub(start, stop)
forcharinchunk:gmatch(".") do
local_continue_0=false
repeat
pos=pos+1
ifskip_untilthen
ifpos<=skip_untilthen
_continue_0=true
break
end
skip_until=nil
end
ifend_delimthen
ifend_delim==charandnotescapethen
in_string=false
end_delim=nil
end
else
ifchar=="'" orchar=='"' then
end_delim=char
in_string=true
end
ifchar=="[" then
do
locallstring=chunk:match("^%[=*%[", pos)
iflstringthen
locallstring_end=lstring:gsub("%[", "]")
locallstring_p1, lstring_p2=chunk:find(lstring_end, pos, true)
ifnot (lstring_p1) then
returntrue
end
skip_until=lstring_p2
end
end
end
end
escape=char=="\\"
_continue_0=true
untiltrue
ifnot_continue_0then
break
end
end
returnin_string
end,
push_raw=function(self, start, stop)
returninsert(self.chunks, self.str:sub(start, stop))
end,
push_code=function(self, kind, start, stop)
returninsert(self.chunks, {
kind,
self.str:sub(start, stop),
start
})
end,
compile=function(self, str)
localsuccess, err=self:parse(str)
ifnot (success) then
returnnil, err
end
localfn
fn, err=self:load(self:chunks_to_lua())
ifnot (fn) then
returnnil, err
end
returnfunction(...)
localbuffer
buffer, err=self:run(fn, ...)
ifbufferthen
returnconcat(buffer)
else
returnnil, err
end
end
end,
parse=function(self, str)
self.str=str
assert(type(self.str) =="string", "expecting string for parse")
self.pos=1
self.chunks= { }
whiletruedo
localfound, err=self:next_tag()
iferrthen
returnnil, err
end
ifnot (found) then
break
end
end
returntrue
end,
parse_error=function(self, err, code)
localline_no, err_msg=err:match("%[.-%]:(%d+): (.*)$")
line_no=tonumber(line_no)
ifnot (line_no) then
return
end
localline=get_line(code, line_no)
localsource_pos=tonumber(line:match("^%-%-%[%[(%d+)%]%]"))
ifnot (source_pos) then
return
end
returnself:error_for_pos(source_pos, err_msg)
end,
error_for_pos=function(self, source_pos, err_msg)
localsource_line_no=pos_to_line(self.str, source_pos)
localsource_line=get_line(self.str, source_line_no)
returntostring(err_msg) .." [" ..tostring(source_line_no) .."]: " ..tostring(source_line)
end,
load=function(self, code, name)
ifname==nilthen
name="etlua"
end
localcode_fn
do
localcode_ref=code
code_fn=function()
do
localret=code_ref
code_ref=nil
returnret
end
end
end
localfn, err=load(code_fn, name)
ifnot (fn) then
do
localerr_msg=self:parse_error(err, code)
iferr_msgthen
returnnil, err_msg
end
end
returnnil, err
end
returnfn
end,
run=function(self, fn, env, buffer, i, ...)
ifenv==nilthen
env= { }
end
localcombined_env=setmetatable({ }, {
__index=function(self, name)
localval=env[name]
ifval==nilthen
val=_G[name]
end
returnval
end
})
ifnot (buffer) then
buffer= { }
i=0
end
setfenv(fn, combined_env)
returnfn(tostring, html_escape, buffer, i, ...)
end,
compile_to_lua=function(self, str, ...)
localsuccess, err=self:parse(str)
ifnot (success) then
returnnil, err
end
returnself:chunks_to_lua(...)
end,
chunks_to_lua=function(self, compiler_cls)
ifcompiler_cls==nilthen
compiler_cls=Compiler
end
returncompiler_cls():compile_chunks(self.chunks)
end
}
_base_0.__index=_base_0
_class_0=setmetatable({
__init=function() end,
__base=_base_0,
__name="Parser"
}, {
__index=_base_0,
__call=function(cls, ...)
local_self_0=setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return_self_0
end
})
_base_0.__class=_class_0
Parser=_class_0
end
localcompile
do
local_base_0=Parser()
local_fn_0=_base_0.compile
compile=function(...)
return_fn_0(_base_0, ...)
end
end
localrender
render=function(str, ...)
localfn, err=compile(str)
iffnthen
returnfn(...)
else
returnnil, err
end
end
return {
compile=compile,
render=render,
Parser=Parser,
Compiler=Compiler,
_version=VERSION
}