-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtamale.lua
344 lines (296 loc) · 11 KB
/
tamale.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
--[[
Copyright (c) 2010 Scott Vokes <vokes.s@gmail.com>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
--]]
-- Depenedencies
local assert, getmetatable, ipairs, pairs, pcall, setmetatable, type =
assert, getmetatable, ipairs, pairs, pcall, setmetatable, type
local concat, insert, sort = table.concat, table.insert, table.sort
local strmatch, tostring = string.match, tostring
local function trace(...) print(string.format(...)) end
---TAble-MAtching Lua Extension.
module("tamale")
VERSION = "1.2.1"
DEBUG = false --Set to true to enable traces.
local function sentinel(descr)
return setmetatable({}, { __tostring=function() return descr end })
end
local VAR, NIL = sentinel("[var]"), sentinel("[nil]")
local function is_var(t) return getmetatable(t) == VAR end
---Mark a string in a match pattern as a variable key.
-- (You probably want to alias this locally to something short.)
-- Any variables beginning with _ are ignored.
-- @usage { "extract", {var"_", var"_", var"third", var"_" } }
-- @usage A variable named "..." captures subsequent array-portion values.
function var(name)
assert(type(name) == "string", "Variable name must be string")
local ignore = (name:sub(1, 1) == "_")
local rest = (name == "...")
return setmetatable( { name=name, ignore=ignore, rest=rest }, VAR)
end
---Returns a function that tests a string with string:match, rather
-- than ==. Any captures from the string match are appended to the
-- capture table. Like var, this would probably be locally aliased,
-- and used like { P"num (%d+)", handler }.
function P(str)
return function(v)
if type(v) == "string" then return strmatch(v, str) end
end
end
---Default hook for match failure.
-- @param val The unmatched value.
function match_fail(val)
return nil, "Match failed", val
end
-- Key-weak cache for table counts, since #t only gives the
-- length of the array portion, and otherwise, values with extra
-- non-numeric keys can match rows that do not have them.
local counts = setmetatable({}, { __mode="k"})
local function get_count(t)
local v = counts[t]
if not v then
v = 0
for k in pairs(t) do v = v + 1 end
counts[t] = v
end
return v
end
-- Structurally match val against a pattern, setting variables in the
-- pattern to the corresponding values in val, and recursively
-- unifying table fields. Functions are treated as predicates - any
-- non-false result(s) are considered a success and are captured.
local function unify(pat, val, cs, ids, row)
local pt, vt, nil_captures = type(pat), type(val), 0
if pt == "table" then
if is_var(pat) then
local cur = cs[pat.name]
if cur and cur ~= val and not pat.ignore then return false end
cs[pat.name] = val
return cs
end
if vt ~= "table" then return false end
if ids[pat] and pat ~= val then --compare by pointer equality
return false
else
for k,v in pairs(pat) do
if not unify(v, val[k], cs, ids, row) then return false end
end
end
if not row.partial then --make sure val doesn't have extra fields
if get_count(pat) ~= get_count(val) then return false end
elseif row.rest then --save V"..." captures
local rest = {}
for i=row.rest,#val do rest[#rest+1] = val[i] end
cs['...'] = rest
end
return cs
elseif pt == "function" then
local fcs = { pat(val) } --function captures
if #fcs == 0 or not fcs[1] then return false end
for _,c in ipairs(fcs) do cs[#cs+1] = c end
return cs
else --just compare as literals
return pat == val and cs or false
end
end
-- Replace any variables in the result with their captures.
local function substituted(res, u)
local r = {}
if is_var(res) then return u[res.name] end
for k,v in pairs(res) do
if type(v) == "table" then
if is_var(v) then r[k] = u[v.name] else r[k] = substituted(v, u) end
else
r[k] = v
end
end
return r
end
-- Return (or execute) the result, substituting any vars present.
local function do_res(res, u, has_vars)
local t = type(res)
if t == "function" then
return res(u)
elseif t == "table" and has_vars then
return substituted(res, u), u
end
return res, u
end
local function append(t, key, val)
local arr = t[key] or {}
arr[#arr+1] = val; t[key] = arr
end
local function has_vars(res)
if type(res) ~= "table" then return false end
if is_var(res) then return true end
for k,v in pairs(res) do
if type(v) == "table" then
if is_var(v) or has_vars(v) then return true end
end
end
return false
end
-- If the list of row IDs didn't exist when the var row was
-- indexed (and thus didn't get added), add it here.
local function prepend_vars(vars, lists)
for i=#vars,1,-1 do
local vid = vars[i]
for k,l in pairs(lists) do
if l[1] > vid then insert(l, 1, vid) end
end
end
end
local function indexable(v)
return not is_var(v) and type(v) ~= "function"
end
-- Index each literal pattern and pattern table's first value (t[1]).
-- Also, add insert patterns with variables or functions in the
-- appropriate place(s).
local function index_spec(spec)
local ls, ts = {}, {} --literals and tables
local lni, tni = {}, {} --non-indexable fields for same
local vrs = {} --rows with vars in the result
local debug = spec.debug
-- field/value to index by, defaults to t[1].
local ispec, indexer
if spec.index == false then
ispec = false -- false -> don't index
else
ispec = spec.index or 1
end
if type(ispec) == "function" then indexer = ispec
elseif ispec == "false" then
indexer = function() end --put everything in the same index
else
indexer = function(t) return t[ispec] end
end
spec.indexer = indexer
for id, row in ipairs(spec) do
local pat, res = row[1], row[2]
local pt = type(pat)
if not indexable(pat) then --could match anything
if debug then trace(" * rule %d: not indexable, adding to all", id) end
lni[#lni+1] = id; tni[#tni+1] = id --for those that don't yet exist
for _,l in ipairs{ls, ts} do --and append to those that do
for k in pairs(l) do append(l, k, id) end
end
elseif pt == "table" then
local v = indexer(pat) or NIL
if not indexable(v) then --goes in every index
if debug then trace(" * rule %d: index(table) is not indexable", id) end
for k in pairs(ts) do append(ts, k, id) end
tni[#tni+1] = id
else
if debug then trace(" * rule %d: indexing on index(t)=%s",
id, tostring(v)) end
append(ts, v, id)
end
for i,v in ipairs(pat) do --check for special V"..." var
if is_var(v) and v.rest then
if debug then trace(" * rule %d: V'...' found in field %d",
id, i) end
row.partial = true; row.rest = i; break
end
end
else
if debug then trace(" * rule %d: indexing on %s",
id, tostring(pat)) end
append(ls, pat, id)
end
if has_vars(res) then
if debug then trace(" * rule %d: found var(s) in result", id) end
vrs[id] = true
end
end
prepend_vars(lni, ls)
prepend_vars(tni, ts)
ls[VAR] = lni; ts[VAR] = tni
return { ls=ls, ts=ts, vrs=vrs }
end
-- Get the appropriate list of rows to check (if any).
local function check_index(spec, t, idx)
local tt = type(t)
if tt == "table" then
local key = spec.indexer(t) or NIL
local ts = idx.ts
return ts[key] or ts[VAR]
else
local ls = idx.ls
return ls[t] or ls[VAR]
end
end
---Return a matcher function for a given specification. When the
-- function is called on one or more values, its first argument is
-- tested in order against every rule that could possibly match it,
-- selecting the relevant result (if any) or returning the values
-- (false, "Match failed", val).
-- If the result is a function, it is called with a table containing
-- any captures and any subsequent arguments passed to the matcher
-- function (in captures.args).
--@param spec A list of rows, where each row is of the form
-- { rule, result, [when=capture_predicate] }.
--@usage spec.ids: An optional list of table values that should be
-- compared by identity, not structure. If any empty tables are
-- being used as a sentinel value (e.g. "MAGIC_ID = {}"), list
-- them here.
--@usage spec.debug=true: Turn on debugging traces for the matcher.
function matcher(spec)
local debug = spec.debug or DEBUG
local ids = {}
if spec.ids then
for _,id in ipairs(spec.ids) do ids[id] = true end
end
local idx = index_spec(spec)
local vrs = idx.vrs --variable rows
return
function (t, ...)
local rows = check_index(spec, t, idx)
if debug then
trace(" -- Checking rules: %s", concat(rows, ", "))
end
for _,id in ipairs(rows) do
local row = spec[id]
local pat, res, when = row[1], row[2], row.when
if debug and res == nil then trace " -- Missing result" end
local args = { ... }
local u = unify(pat, t, { args=args }, ids, row)
if debug then
trace(" -- Trying rule %d...%s", id, u and "matched" or "failed")
end
if u then
u.input = t --whole matched value
if when then
local ok, val = pcall(when, u)
if debug then trace(" -- Running when(captures) check...%s",
(ok and val) and "matched" or "failed")
end
if ok and val then
return do_res(res, u, vrs[id])
end
else
return do_res(res, u, vrs[id])
end
end
end
if debug then trace(" -- Failed") end
local fail = spec.fail or match_fail
return fail(t)
end
end