-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCodeGen.lua
More file actions
253 lines (235 loc) · 8.28 KB
/
CodeGen.lua
File metadata and controls
253 lines (235 loc) · 8.28 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
--
-- lua-CodeGen : <http://fperrad.github.com/lua-CodeGen>
--
local setmetatable = setmetatable
local tonumber = tonumber
local tostring = tostring
local type = type
local tconcat = require 'table'.concat
local _G = _G
local string = require 'string'
_ENV = nil
local m = {}
local function render (val, sep, formatter)
formatter = formatter or tostring
if val == nil then
return ''
end
if type(val) == 'table' then
local t = {}
for i = 1, #val do
t[i] = formatter(val[i])
end
return tconcat(t, sep)
else
return formatter(val)
end
end
local special = {
['a'] = "\a",
['b'] = "\b",
['f'] = "\f",
['n'] = "\n",
['r'] = "\r",
['t'] = "\t",
['v'] = "\v",
['\\'] = '\\',
['"'] = '"',
["'"] = "'",
}
local function unescape(str)
str = str:gsub([[\(%d+)]], function (s)
local n = tonumber(s:sub(1, 3))
return string.char(n % 256) .. s:sub(4)
end
)
return str:gsub([[\([abfnrtv\"'])]], special)
end
local new
local function eval (self, name)
local cyclic = {}
local msg = {}
local function interpolate (self, template, tname)
if type(template) ~= 'string' then
return nil
end
local lineno = 1
local function add_message (...)
msg[#msg+1] = tname .. ':' .. lineno .. ': ' .. tconcat{...}
end -- add_message
local function get_value (vname)
local i = 1
local t = self
for w in vname:gmatch "(%w+)%." do
i = i + w:len() + 1
t = t[w]
if type(t) ~= 'table' then
add_message(vname, " is invalid")
return nil
end
end
return t[vname:sub(i)]
end -- get_value
local function interpolate_line (line)
local function get_repl (capt)
local function apply (self, tmpl)
if cyclic[tmpl] then
add_message("cyclic call of ", tmpl)
return capt
end
cyclic[tmpl] = true
local result = interpolate(self, self[tmpl], tmpl)
cyclic[tmpl] = nil
if result == nil then
add_message(tmpl, " is not a template")
return capt
end
return result
end -- apply
local capt1, pos = capt:match("^%${([%a_][%w%._]*)()", 1)
if not capt1 then
add_message(capt, " does not match")
return capt
end
local sep, pos_sep = capt:match("^;%s+separator%s*=%s*'([^']+)'%s*()", pos)
if not sep then
sep, pos_sep = capt:match("^;%s+separator%s*=%s*\"([^\"]+)\"%s*()", pos)
end
if sep then
sep = unescape(sep)
end
local fmt, pos_fmt = capt:match("^;%s+format%s*=%s*([%a_][%w_]*)%s*()", pos_sep or pos)
if capt:match("^}", pos_fmt or pos_sep or pos) then
if fmt then
local formatter = self[fmt]
if type(formatter) ~= 'function' then
add_message(fmt, " is not a formatter")
return capt
end
return render(get_value(capt1), sep, formatter)
else
return render(get_value(capt1), sep)
end
end
if capt:match("^%(%)}", pos) then
return apply(self, capt1)
end
local capt2 = capt:match("^?([%a_][%w_]*)%(%)}", pos)
if capt2 then
if get_value(capt1) then
return apply(self, capt2)
else
return ''
end
end
local capt2, capt3 = capt:match("^?([%a_][%w_]*)%(%)!([%a_][%w_]*)%(%)}", pos)
if capt2 and capt3 then
if get_value(capt1) then
return apply(self, capt2)
else
return apply(self, capt3)
end
end
local capt2, pos = capt:match("^/([%a_][%w_]*)%(%)()", pos)
if capt2 then
local sep, pos_sep = capt:match("^;%s+separator%s*=%s*'([^']+)'%s*()", pos)
if not sep then
sep, pos_sep = capt:match("^;%s+separator%s*=%s*\"([^\"]+)\"%s*()", pos)
end
if sep then
sep = unescape(sep)
end
if capt:match("^}", pos_sep or pos) then
local array = get_value(capt1)
if array == nil then
return ''
end
if type(array) ~= 'table' then
add_message(capt1, " is not a table")
return capt
end
local results = {}
for i = 1, #array do
local item = array[i]
if type(item) ~= 'table' then
item = { it = item }
end
local result = apply(new(item, self), capt2)
results[#results+1] = result
if result == capt then
break
end
end
return tconcat(results, sep)
end
end
add_message(capt, " does not match")
return capt
end -- get_repl
local indent = line:match "^(%s*)%$%b{}$"
local result = line:gsub("(%$%b{})", get_repl)
if indent == '' then
result = result:gsub("\n$", '')
elseif indent then
result = result:gsub("\n", "\n" .. indent)
result = result:gsub("^" .. indent .. "\n", "\n")
repeat
local nb
result, nb = result:gsub("\n" .. indent .. "\n", "\n\n")
until nb == 0
result = result:gsub("\n" .. indent .. "$", '')
end
return result
end -- interpolate_line
if template:find "\n" then
local results = {}
for line in template:gmatch "([^\n]*)\n" do
local result = interpolate_line(line)
if result == line or not result:match'^%s*$' then
results[#results+1] = result
end
lineno = lineno + 1
end
results[#results+1] = ''
return tconcat(results, "\n")
else
return interpolate_line(template)
end
end -- interpolate
local val = self[name]
if type(val) == 'string' then
return interpolate(self, val, name),
(#msg > 0 and tconcat(msg, "\n")) or nil
else
return render(val)
end
end
function new (env, ...)
local obj = { env or {}, ... }
setmetatable(obj, {
__tostring = function () return 'CodeGen' end,
__call = function (...) return eval(...) end,
__index = function (t, k)
for i = 1, #t do
local v = t[i][k]
if v ~= nil then
return v
end
end
end,
})
return obj
end
m.new = new
setmetatable(m, {
__call = function (func, ...) return new(...) end
})
_G.CodeGen = m
m._VERSION = "0.2.1"
m._DESCRIPTION = "lua-CodeGen : a template engine"
m._COPYRIGHT = "Copyright (c) 2010 Francois Perrad"
return m
--
-- This library is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--