This repository was archived by the owner on Nov 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExpand.lua
More file actions
251 lines (224 loc) · 6.24 KB
/
Expand.lua
File metadata and controls
251 lines (224 loc) · 6.24 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
------------------------------------------------------------------------------
-- Expand.lua
------------------------------------------------------------------------------
local strfind = string.find
local strsub = string.sub
local push = table.insert
local pop = table.remove
local concat = table.concat
local statements = {}
for w in string.gfind('do if for while repeat', '%a+') do
statements[w] = true
end
local function expand(str, ...)
assert(type(str)=='string', 'expecting string')
local searchlist = arg
local estring,evar
function estring(str)
local b,e,i
b,i = strfind(str, '%$.')
if not b then return str end
local R, pos = {}, 1
repeat
b,e = strfind(str, '^%b{}', i)
if b then
push(R, strsub(str, pos, b-2))
push(R, evar(strsub(str, b+1, e-1)))
i = e+1
pos = i
else
b,e = strfind(str, '^%b()', i)
if b then
push(R, strsub(str, pos, b-2))
push(R, evar(strsub(str, b+1, e-1)))
i = e+1
pos = i
elseif strfind(str, '^%a', i) then
push(R, strsub(str, pos, i-2))
push(R, evar(strsub(str, i, i)))
i = i+1
pos = i
elseif strfind(str, '^%$', i) then
push(R, strsub(str, pos, i))
i = i+1
pos = i
end
end
b,i = strfind(str, '%$.', i)
until not b
push(R, strsub(str, pos))
return concat(R)
end
local function search(index)
for _,symt in ipairs(searchlist) do
local ts = type(symt)
local value
if ts == 'function' then value = symt(index)
elseif ts == 'table'
or ts == 'userdata' then value = symt[index]
if type(value)=='function' then value = value(symt) end
else error'search item must be a function, table or userdata' end
if value ~= nil then return value end
end
error('unknown variable: '.. index)
end
local function elist(var, v, str, sep)
local tab = search(v)
if tab then
assert(type(tab)=='table', 'expecting table from: '.. var)
local R = {}
push(searchlist, 1, tab)
push(searchlist, 1, false)
for _,elem in ipairs(tab) do
searchlist[1] = elem
push(R, estring(str))
end
pop(searchlist, 1)
pop(searchlist, 1)
return concat(R, sep)
else
return ''
end
end
local function get(tab,index)
for _,symt in ipairs(searchlist) do
local ts = type(symt)
local value
if ts == 'function' then value = symt(index)
elseif ts == 'table'
or ts == 'userdata' then value = symt[index]
else error'search item must be a function, table or userdata' end
if value ~= nil then
tab[index] = value -- caches value and prevents changing elements
return value
end
end
end
function evar(var)
if strfind(var, '^[_%a][_%w]*$') then -- ${vn}
return estring(tostring(search(var)))
end
local b,e,cmd = strfind(var, '^(%a+)%s.')
if cmd == 'foreach' then -- ${foreach vn xxx} or ${foreach vn/sep/xxx}
local vn,s
b,e,vn,s = strfind(var, '^([_%a][_%w]*)([%s%p]).', e)
if vn then
if strfind(s, '%s') then
return elist(var, vn, strsub(var, e), '')
end
b = strfind(var, s, e, true)
if b then
return elist(var, vn, strsub(var, b+1), strsub(var,e,b-1))
end
end
error('syntax error in: '.. var, 2)
elseif cmd == 'when' then -- $(when vn xxx)
local vn
b,e,vn = strfind(var, '^([_%a][_%w]*)%s.', e)
if vn then
local t = search(vn)
if not t then
return ''
end
local s = strsub(var,e)
if type(t)=='table' or type(t)=='userdata' then
push(searchlist, 1, t)
s = estring(s)
pop(searchlist, 1)
return s
else
return estring(s)
end
end
error('syntax error in: '.. var, 2)
else
if statements[cmd] then -- do if for while repeat
var = 'local OUT="" '.. var ..' return OUT'
else -- expression
var = 'return '.. var
end
local f = assert(loadstring(var))
local t = searchlist[1]
assert(type(t)=='table' or type(t)=='userdata', 'expecting table')
setfenv(f, setmetatable({}, {__index=get, __newindex=t}))
return estring(tostring(f()))
end
end
return estring(str)
end
--[[
template = [[
you can access variables: $v
or environment variables: ${HOME}
you can call functions: ${table.concat(list, ', ')}
this list has ${list.n} elements
${string.rep('=', list.n)}
${table.concat(list)}
${string.rep('=', list.n)}
or evaluate code inline
${for i=1,list.n do
OUT = table.concat{ OUT, ' list[', i, '] = ', list[i], '\n'}
end}
you can access global variables:
This example is from ${mjd} at $(mjdweb)
The Lord High Chamberlain has gotten ${L.n}
things for me this year.
${do diff = L.n - 5
more = 'more'
if diff == 0 then
diff = 'no'
elseif diff < 0 then
diff = -diff
more = 'fewer'
end
end}
That is $(diff) $(more) than he gave me last year.
values can have other variables: $(ref)
]]
mjd = "Mark J. Dominus"
mjdweb = 'http://perl.plover.com/'
L = {n=0}
for i = 1,4 do table.insert(L, string.char(64+i)) end
local x = {
v = 'this is v',
list = L,
ref = "$(mjd) made Text::Template.pm"
}
setmetatable(x, {__index=_G})
-- fill in the template with values in table x
io.write(expand(template, x, os.getenv))
------------------------------------------------------------------------------
fun_temp = [[
==============================================================================
$(foreach funcs
${type} x = ${name}( ${table.concat(args, ', ')} ) {
$(code)
$(when stuff
x = $x;
y = $y;
) reutrn $(exit);
}
)
==============================================================================
]]
fun_list = {
exit = 1;
stuff = false;
funcs = {
{ type = 'int';
name = 'bill';
args = { 'a', 'b', 'c' };
code = 'something';
stuff = { x=99, y=34 };
};
{ type = 'char *';
name = 'bert';
args = { 'one', 'two', 'three' };
code = 'something else';
exit = 2
};
};
}
io.write(expand(fun_temp, fun_list, _G))
--]]
return expand