-
Notifications
You must be signed in to change notification settings - Fork 18
/
etlua.moon
286 lines (220 loc) · 6.12 KB
/
etlua.moon
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
VERSION = "1.3.0"
import insert, concat from table
import load, setfenv, assert, type, error, tostring, tonumber, setmetatable from _G
setfenv = setfenv or (fn, env) ->
local name
i = 1
while true
name = debug.getupvalue fn, i
break if not name or name == "_ENV"
i += 1
if name
debug.upvaluejoin fn, i, (-> env), 1
fn
html_escape_entities = {
['&']: '&'
['<']: '<'
['>']: '>'
['"']: '"'
["'"]: '''
}
html_escape = (str) ->
(str\gsub [=[["><'&]]=], html_escape_entities)
get_line = (str, line_num) ->
-- todo: this returns an extra blank line at the end
for line in str\gmatch "([^\n]*)\n?"
return line if line_num == 1
line_num -= 1
pos_to_line = (str, pos) ->
line = 1
for _ in str\sub(1, pos)\gmatch("\n")
line += 1
line
class Compiler
html_escape: true
new: =>
@buffer = {}
@i = 0
render: =>
table.concat @buffer
push: (str, ...) =>
i = @i + 1
@buffer[i] = str
@i = i
@push ... if ...
header: =>
@push "local _tostring, _escape, _b, _b_i = ...\n"
footer: =>
@push "return _b"
increment: =>
@push "_b_i = _b_i + 1\n"
mark: (pos) =>
@push "--[[", tostring(pos), "]] "
assign: (...) =>
@push "_b[_b_i] = ", ...
@push "\n" if ...
compile_chunks: (chunks) =>
@header!
for chunk in *chunks
t = type chunk
t = chunk[1] if t == "table"
switch t
when "string"
@increment!
@assign ("%q")\format(chunk)
when "code"
@mark chunk[3]
@push chunk[2], "\n"
when "=", "-"
@increment!
@mark chunk[3]
@assign!
if t == "=" and @html_escape
@push "_escape(_tostring(", chunk[2], "))\n"
else
@push "_tostring(", chunk[2], ")\n"
else
error "unknown type #{t}"
@footer!
@render!
class Parser
open_tag: "<%"
close_tag: "%>"
modifiers: "^[=-]"
next_tag: =>
start, stop = @str\find @open_tag, @pos, true
-- no more tags, all done
unless start
@push_raw @pos, #@str
return false
-- add text before
unless start == @pos
@push_raw @pos, start - 1
@pos = stop + 1
modifier = if @str\match @modifiers, @pos
with @str\sub @pos, @pos
@pos += 1
close_start, close_stop = @str\find @close_tag, @pos, true
unless close_start
return nil, @error_for_pos start, "failed to find closing tag"
while @in_string @pos, close_start
close_start, close_stop = @str\find @close_tag, close_stop, true
unless close_start
return nil, @error_for_pos start, "failed to find string close"
trim_newline = if "-" == @str\sub close_start - 1, close_start - 1
close_start -= 1
true
@push_code modifier or "code", @pos, close_start - 1
@pos = close_stop + 1
if trim_newline
if match = @str\match "^\n", @pos
@pos += #match
true
-- see if stop leaves us in the middle of a string
in_string: (start, stop) =>
in_string = false
end_delim = nil
escape = false
pos = 0
skip_until = nil
chunk = @str\sub start, stop
for char in chunk\gmatch "."
pos += 1
if skip_until
continue if pos <= skip_until
skip_until = nil
if end_delim
if end_delim == char and not escape
in_string = false
end_delim = nil
else
if char == "'" or char == '"'
end_delim = char
in_string = true
if char == "["
if lstring = chunk\match "^%[=*%[", pos
lstring_end = lstring\gsub "%[", "]"
lstring_p1, lstring_p2 = chunk\find lstring_end, pos, true
-- no closing lstring, must be inside string
return true unless lstring_p1
skip_until = lstring_p2
escape = char == "\\"
in_string
push_raw: (start, stop) =>
insert @chunks, @str\sub start, stop
push_code: (kind, start, stop) =>
insert @chunks, {
kind, @str\sub(start, stop), start
}
compile: (str) =>
success, err = @parse str
return nil, err unless success
fn, err = @load @chunks_to_lua!
return nil, err unless fn
(...) ->
buffer, err = @run fn, ...
if buffer
concat buffer
else
nil, err
parse: (@str) =>
assert type(@str) == "string", "expecting string for parse"
@pos = 1
@chunks = {}
while true
found, err = @next_tag!
return nil, err if err
break unless found
true
parse_error: (err, code) =>
line_no, err_msg = err\match "%[.-%]:(%d+): (.*)$"
line_no = tonumber line_no
return unless line_no
line = get_line code, line_no
source_pos = tonumber line\match "^%-%-%[%[(%d+)%]%]"
return unless source_pos
@error_for_pos source_pos, err_msg
error_for_pos: (source_pos, err_msg) =>
source_line_no = pos_to_line @str, source_pos
source_line = get_line @str, source_line_no
"#{err_msg} [#{source_line_no}]: #{source_line}"
-- converts lua string into template function
load: (code, name="etlua") =>
code_fn = do
code_ref = code
->
with ret = code_ref
code_ref = nil
fn, err = load code_fn, name
unless fn
-- try to extract meaningful error message
if err_msg = @parse_error err, code
return nil, err_msg
return nil, err
fn
-- takes a function from @load and executes it with correct parameters
run: (fn, env={}, buffer, i, ...) =>
combined_env = setmetatable {}, __index: (name) =>
val = env[name]
val = _G[name] if val == nil
val
unless buffer
buffer = {}
i = 0
setfenv fn, combined_env
fn tostring, html_escape, buffer, i, ...
compile_to_lua: (str, ...) =>
success, err = @parse str
return nil, err unless success
@chunks_to_lua ...
-- generates the code of the template
chunks_to_lua: (compiler_cls=Compiler) =>
compiler_cls!\compile_chunks @chunks
compile = Parser!\compile
render = (str, ...) ->
fn, err = compile(str)
if fn
fn ...
else
nil, err
{ :compile, :render, :Parser, :Compiler, _version: VERSION }