This repository was archived by the owner on Nov 20, 2020. It is now read-only.
forked from strogo/lua-CodeGen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlpeg.lua
More file actions
282 lines (257 loc) · 9.32 KB
/
lpeg.lua
File metadata and controls
282 lines (257 loc) · 9.32 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
--
-- lua-CodeGen : <http://fperrad.github.com/lua-CodeGen>
--
local setmetatable = setmetatable
local tonumber = tonumber
local tostring = tostring
local type = type
local char = require 'string'.char
local tconcat = require 'table'.concat
local _G = _G
local lpeg = require 'lpeg'
_ENV = nil
local m = {}
local function gsub (s, patt, repl)
local p = lpeg.Cs((patt / repl + 1)^0)
return p:match(s)
end
local function split (s, sep, func)
local elem = lpeg.C((1 - sep)^0) / func
local p = elem * (sep * elem)^0
p:match(s)
end
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 digit = lpeg.R'09'
local escape_digit = lpeg.P[[\]]*lpeg.C(digit * digit^-2)
local escape_special = lpeg.P[[\]]*lpeg.C(lpeg.S[[abfnrtv\"']])
local function unescape(str)
str = gsub(str, escape_digit, function (s)
return char(tonumber(s) % 256)
end)
return gsub(str, escape_special, special)
end
local dot = lpeg.P'.'
local space = lpeg.S" \t"
local newline = lpeg.P"\n"
local newline_anywhere = lpeg.P{ newline + 1 * lpeg.V(1) }
local only_space = space^0 * -1
local newline_end = newline * -1
local indent_needed = newline * -newline
local vname_capture = lpeg.P'${' * lpeg.C(lpeg.R('AZ', 'az', '__') * lpeg.R('09', 'AZ', 'az', '__', '..')^0) * lpeg.Cp()
local separator_simple_quote_capture = lpeg.P"'" * lpeg.C((lpeg.P(1) - "'")^0) * lpeg.P"'"
local separator_double_quote_capture = lpeg.P'"' * lpeg.C((lpeg.P(1) - '"')^0) * lpeg.P'"'
local separator_capture = lpeg.P';' * space^1 * lpeg.P'separator' * space^0 * lpeg.P'=' * space^0 *
(separator_simple_quote_capture + separator_double_quote_capture) * space^0 * lpeg.Cp()
local identifier_capture = lpeg.C(lpeg.R('AZ', 'az', '__') * lpeg.R('09', 'AZ', 'az', '__')^0)
local format_capture = lpeg.P';' * space^1 * lpeg.P'format' * space^0 * lpeg.P'=' * space^0 *
identifier_capture * space^0 * lpeg.Cp()
local data_end = lpeg.P'}'
local include_end = lpeg.P'()}'
local if_capture = lpeg.P'?' * identifier_capture * lpeg.P'()}'
local if_else_capture = lpeg.P'?' * identifier_capture * lpeg.P'()!' * identifier_capture * lpeg.P'()}'
local map_capture = lpeg.P'/' * identifier_capture * lpeg.P'()' * lpeg.Cp()
local map_end = lpeg.P'}'
local subst = lpeg.P'$' * lpeg.P{ '{' * ((1 - lpeg.S'{}') + lpeg.V(1))^0 * '}' }
local indent_capture = lpeg.C(space^0) * subst * -1
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 t = self
split(vname, dot, function (w)
if type(t) == 'table' then
t = t[w]
else
add_message(vname, " is invalid")
t = nil
end
end)
return t
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 = vname_capture:match(capt, 1)
if not capt1 then
add_message(capt, " does not match")
return capt
end
local sep, pos_sep = separator_capture:match(capt, pos)
if sep then
sep = unescape(sep)
end
local fmt, pos_fmt = format_capture:match(capt, pos_sep or pos)
if data_end:match(capt, 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 include_end:match(capt, pos) then
return apply(self, capt1)
end
local capt2 = if_capture:match(capt, pos)
if capt2 then
if get_value(capt1) then
return apply(self, capt2)
else
return ''
end
end
local capt2, capt3 = if_else_capture:match(capt, 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 = map_capture:match(capt, pos)
if capt2 then
local sep, pos_sep = separator_capture:match(capt, pos)
if sep then
sep = unescape(sep)
end
if map_end:match(capt, 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 = indent_capture:match(line)
local result = gsub(line, subst, get_repl)
if indent then
result = gsub(result, newline_end, '')
if indent ~= '' then
result = gsub(result, indent_needed, "\n" .. indent)
end
end
return result
end -- interpolate_line
if newline_anywhere:match(template) then
local results = {}
split(template, newline, function (line)
local result = interpolate_line(line)
if result == line or not only_space:match(result) then
results[#results+1] = result
end
lineno = lineno + 1
end)
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 m._NAME 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.package.loaded['CodeGen'] = m
m._NAME = 'CodeGen'
m._VERSION = "0.2.3"
m._DESCRIPTION = "lua-CodeGen : a template engine"
m._COPYRIGHT = "Copyright (c) 2010-2011 Francois Perrad"
return m
--
-- This library is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--