-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosmo.lua
317 lines (291 loc) · 7.69 KB
/
cosmo.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
local require = require
local grammar = require "cosmo.grammar"
local interpreter = require "cosmo.fill"
local loadstring = loadstring
module(..., package.seeall)
yield = coroutine.yield
local preamble = [[
local is_callable, insert, concat, setmetatable, getmetatable, type, wrap, tostring, check_selector = ...
local function prepare_env(env, parent)
local __index = function (t, k)
local v = env[k]
if not v then
v = parent[k]
end
return v
end
local __newindex = function (t, k, v)
env[k] = v
end
return setmetatable({ self = env }, { __index = __index, __newindex = __newindex })
end
local id = function () end
local template_func = %s
return function (env, opts)
opts = opts or {}
local out = opts.out or {}
template_func(out, env)
return concat(out, opts.delim)
end
]]
local compiled_template = [[
function (out, env)
if type(env) == "string" then env = { it = env } end
$parts[=[
insert(out, $quoted_text)
]=],
[=[
local selector_name = $selector
local selector = $parsed_selector
$if_subtemplate[==[
local subtemplates = {}
$subtemplates[===[
subtemplates[$i] = $subtemplate
]===]
$if_args[===[
check_selector(selector_name, selector)
for e, literal in wrap(selector), $args, true do
if literal then
insert(out, tostring(e))
else
if type(e) ~= "table" then
e = prepare_env({ it = tostring(e) }, env)
else
e = prepare_env(e, env)
end
(subtemplates[e.self._template or 1] or id)(out, e)
end
end
]===],
[===[
if type(selector) == 'table' then
for _, e in ipairs(selector) do
if type(e) ~= "table" then
e = prepare_env({ it = tostring(e) }, env)
else
e = prepare_env(e, env)
end
(subtemplates[e.self._template or 1] or id)(out, e)
end
else
check_selector(selector_name, selector)
for e, literal in wrap(selector), nil, true do
if literal then
insert(out, tostring(e))
else
if type(e) ~= "table" then
e = prepare_env({ it = tostring(e) }, env)
else
e = prepare_env(e, env)
end
(subtemplates[e.self._template or 1] or id)(out, e)
end
end
end
]===]
]==],
[==[
$if_args[===[
check_selector(selector_name, selector)
selector = selector($args, false)
insert(out, tostring(selector))
]===],
[===[
if is_callable(selector) then
insert(out, tostring(selector()))
else
insert(out, tostring(selector or ""))
end
]===]
]==]
]=]
end
]]
local function is_callable(f)
if type(f) == "function" then return true end
local meta = getmetatable(f)
if meta and meta.__call then return true end
return false
end
local function check_selector(name, selector)
if not is_callable(selector) then
error("selector " .. name .. " is not callable but is " .. type(selector))
end
end
local function compile_template(chunkname, template_code)
local template_func, err = loadstring(string.format(preamble, template_code), chunkname)
if not template_func then
error("syntax error when compiling template: " .. err)
else
return template_func(is_callable, table.insert, table.concat, setmetatable, getmetatable, type,
coroutine.wrap, tostring, check_selector)
end
end
local compiler = {}
function compiler.template(template)
assert(template.tag == "template")
local parts = {}
for _, part in ipairs(template.parts) do
parts[#parts+1] = compiler[part.tag](part)
end
return interpreter.fill(compiled_template, { parts = parts })
end
function compiler.text(text)
assert(text.tag == "text")
return { _template = 1, quoted_text = string.format("%q", text.text) }
end
function compiler.appl(appl)
assert(appl.tag == "appl")
local selector, args, subtemplates = appl.selector, appl.args, appl.subtemplates
local ta = { _template = 2, selector = string.format("%q", selector),
parsed_selector = selector }
local do_subtemplates = function ()
for i, subtemplate in ipairs(subtemplates) do
yield{ i = i, subtemplate = compiler.template(subtemplate) }
end
end
if #subtemplates == 0 then
if args and args ~= "" and args ~= "{}" then
ta.if_subtemplate = { { _template = 2, if_args = { { _template = 1, args = args } } } }
else
ta.if_subtemplate = { { _template = 2, if_args = { { _template = 2 } } } }
end
else
if args and args ~= "" and args ~= "{}" then
ta.if_subtemplate = { { _template = 1, subtemplates = do_subtemplates,
if_args = { { _template = 1, args = args } } } }
else
ta.if_subtemplate = { { _template = 1, subtemplates = do_subtemplates,
if_args = { { _template = 2 } } } }
end
end
return ta
end
local cache = {}
setmetatable(cache, { __index = function (tab, key)
local new = {}
tab[key] = new
return new
end,
__mode = "v" })
function compile(template, chunkname)
template = template or ""
chunkname = chunkname or template
local compiled_template = cache[template][chunkname]
if not compiled_template then
compiled_template = compile_template(chunkname, compiler.template(grammar.ast:match(template)))
cache[template][chunkname] = compiled_template
end
return compiled_template
end
local filled_templates = {}
function fill(template, env)
template = template or ""
local start = template:match("^(%[=*%[)")
if start then template = template:sub(#start + 1, #template - #start) end
if filled_templates[template] then
return compile(template)(env)
else
filled_templates[template] = true
return interpreter.fill(template, env)
end
end
local nop = function () end
function cond(bool, table)
if bool then
return function () yield(table) end
else
return nop
end
end
f = compile
function c(bool)
if bool then
return function (table)
return function () yield(table) end
end
else
return function (table) return nop end
end
end
function map(arg, has_block)
if has_block then
for _, item in ipairs(arg) do
cosmo.yield(item)
end
else
return table.concat(arg)
end
end
function inject(arg)
cosmo.yield(arg)
end
function cif(arg, has_block)
if not has_block then error("this selector needs a block") end
if arg[1] then
arg._template = 1
else
arg._template = 2
end
cosmo.yield(arg)
end
function concat(arg)
local list, sep = arg[1], arg[2] or ", "
local size = #list
for i, e in ipairs(list) do
if type(e) == "table" then
if i ~= size then
cosmo.yield(e)
cosmo.yield(sep, true)
else
cosmo.yield(e)
end
else
if i ~= size then
cosmo.yield{ it = e }
cosmo.yield(sep, true)
else
cosmo.yield{ it = e }
end
end
end
end
function make_concat(list)
return function (arg)
local sep = (arg and arg[1]) or ", "
local size = #list
for i, e in ipairs(list) do
if type(e) == "table" then
if i ~= size then
cosmo.yield(e)
cosmo.yield(sep, true)
else
cosmo.yield(e)
end
else
if i ~= size then
cosmo.yield{ it = e }
cosmo.yield(sep, true)
else
cosmo.yield{ it = e }
end
end
end
end
end
function cfor(args)
local name, list, args = args[1], args[2], args[3]
if type(list) == "table" then
for i, item in ipairs(list) do
cosmo.yield({ [name] = item, i = i })
end
else
for item, literal in coroutine.wrap(list), args, true do
if literal then
cosmo.yield(item, true)
else
cosmo.yield({ [name] = item })
end
end
end
end