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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
if not modules then modules = { } end modules ['util-seq'] = {
version = 1.001,
comment = "companion to luat-lib.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
--[[ldx--
<p>Here we implement a mechanism for chaining the special functions
that we use in <l n="context"> to deal with mode list processing. We
assume that namespaces for the functions are used, but for speed we
use locals to refer to them when compiling the chain.</p>
--ldx]]--
-- todo: delayed: i.e. we register them in the right order already but delay usage
-- todo: protect groups (as in tasks)
local gsub, gmatch = string.gsub, string.gmatch
local concat, sortedkeys = table.concat, table.sortedkeys
local type, load, next, tostring = type, load, next, tostring
utilities = utilities or { }
local tables = utilities.tables
local allocate = utilities.storage.allocate
local formatters = string.formatters
local replacer = utilities.templates.replacer
local trace_used = false
local trace_detail = false
local report = logs.reporter("sequencer")
local usedcount = 0
local usednames = { }
trackers.register("sequencers.used", function(v) trace_used = true end)
trackers.register("sequencers.detail",function(v) trace_detail = true end)
local sequencers = { }
utilities.sequencers = sequencers
local functions = allocate()
sequencers.functions = functions
local removevalue = tables.removevalue
local replacevalue = tables.replacevalue
local insertaftervalue = tables.insertaftervalue
local insertbeforevalue = tables.insertbeforevalue
local usedsequences = { }
local function validaction(action)
if type(action) == "string" then
local g = _G
for str in gmatch(action,"[^%.]+") do
g = g[str]
if not g then
return false
end
end
end
return true
end
local compile
local known = { } -- just a convenience, in case we want public access (only to a few methods)
function sequencers.new(t) -- was reset
local s = {
list = { },
order = { },
kind = { },
askip = { },
gskip = { },
dirty = true,
runner = nil,
steps = 0,
}
if t then
s.arguments = t.arguments
s.templates = t.templates
s.returnvalues = t.returnvalues
s.results = t.results
local name = t.name
if name and name ~= "" then
s.name = name
known[name] = s
end
end
table.setmetatableindex(s,function(t,k)
-- this will automake a dirty runner
if k == "runner" then
local v = compile(t,t.compiler)
return v
end
end)
known[s] = s -- saves test for string later on
return s
end
function sequencers.prependgroup(t,group,where)
if t and group then
t = known[t]
if t then
local order = t.order
removevalue(order,group)
insertbeforevalue(order,where,group)
t.list[group] = { }
t.dirty = true
t.runner = nil
end
end
end
function sequencers.appendgroup(t,group,where)
if t and group then
t = known[t]
if t then
local order = t.order
removevalue(order,group)
insertaftervalue(order,where,group)
t.list[group] = { }
t.dirty = true
t.runner = nil
end
end
end
function sequencers.prependaction(t,group,action,where,kind,force)
if t and group and action then
t = known[t]
if t then
local g = t.list[group]
if g and (force or validaction(action)) then
removevalue(g,action)
insertbeforevalue(g,where,action)
t.kind[action] = kind
t.dirty = true
t.runner = nil
end
end
end
end
function sequencers.appendaction(t,group,action,where,kind,force)
if t and group and action then
t = known[t]
if t then
local g = t.list[group]
if g and (force or validaction(action)) then
removevalue(g,action)
insertaftervalue(g,where,action)
t.kind[action] = kind
t.dirty = true
t.runner = nil
end
end
end
end
function sequencers.enableaction(t,action)
if t and action then
t = known[t]
if t then
t.askip[action] = false
t.dirty = true
t.runner = nil
end
end
end
function sequencers.disableaction(t,action)
if t and action then
t = known[t]
if t then
t.askip[action] = true
t.dirty = true
t.runner = nil
end
end
end
function sequencers.enablegroup(t,group)
if t and group then
t = known[t]
if t then
t.gskip[group] = false
t.dirty = true
t.runner = nil
end
end
end
function sequencers.disablegroup(t,group)
if t and group then
t = known[t]
if t then
t.gskip[group] = true
t.dirty = true
t.runner = nil
end
end
end
function sequencers.setkind(t,action,kind)
if t and action then
t = known[t]
if t then
t.kind[action] = kind
t.dirty = true
t.runner = nil
end
end
end
function sequencers.removeaction(t,group,action,force)
if t and group and action then
t = known[t]
local g = t and t.list[group]
if g and (force or validaction(action)) then
removevalue(g,action)
t.dirty = true
t.runner = nil
end
end
end
function sequencers.replaceaction(t,group,oldaction,newaction,force)
if t and group and oldaction and newaction then
t = known[t]
if t then
local g = t.list[group]
if g and (force or validaction(oldaction)) then
replacevalue(g,oldaction,newaction)
t.dirty = true
t.runner = nil
end
end
end
end
local function localize(str)
return (gsub(str,"[%.: ]+","_"))
end
local function construct(t)
local list = t.list
local order = t.order
local kind = t.kind
local gskip = t.gskip
local askip = t.askip
local name = t.name or "?"
local arguments = t.arguments or "..."
local returnvalues = t.returnvalues
local results = t.results
local variables = { }
local calls = { }
local n = 0
usedcount = usedcount + 1
for i=1,#order do
local group = order[i]
if not gskip[group] then
local actions = list[group]
for i=1,#actions do
local action = actions[i]
if not askip[action] then
if trace_used then
local action = tostring(action)
report("%02i: category %a, group %a, action %a",usedcount,name,group,action)
usednames[action] = true
end
local localized
if type(action) == "function" then
local name = localize(tostring(action))
functions[name] = action
action = formatters["utilities.sequencers.functions.%s"](name)
localized = localize(name) -- shorter than action
else
localized = localize(action)
end
n = n + 1
variables[n] = formatters["local %s = %s"](localized,action)
if not returnvalues then
calls[n] = formatters["%s(%s)"](localized,arguments)
elseif n == 1 then
calls[n] = formatters["local %s = %s(%s)"](returnvalues,localized,arguments)
else
calls[n] = formatters["%s = %s(%s)"](returnvalues,localized,arguments)
end
end
end
end
end
t.dirty = false
t.steps = n
if n == 0 then
t.compiled = ""
else
variables = concat(variables,"\n")
calls = concat(calls,"\n")
if results then
t.compiled = formatters["%s\nreturn function(%s)\n%s\nreturn %s\nend"](variables,arguments,calls,results)
else
t.compiled = formatters["%s\nreturn function(%s)\n%s\nend"](variables,arguments,calls)
end
end
return t.compiled -- also stored so that we can trace
end
sequencers.tostring = construct
sequencers.localize = localize
compile = function(t,compiler,...) -- already referred to in sequencers.new
local compiled
if not t or type(t) == "string" then
return false
end
if compiler then
compiled = compiler(t,...)
t.compiled = compiled
else
compiled = construct(t,...)
end
local runner
if compiled == "" then
runner = false
else
runner = compiled and load(compiled)() -- we can use loadstripped here
end
t.runner = runner
return runner
end
sequencers.compile = compile
function sequencers.nodeprocessor(t,nofarguments)
--
local templates = nofarguments
--
if type(templates) ~= "table" then
return ""
end
--
local replacers = { }
for k, v in next, templates do
replacers[k] = replacer(v)
end
--
local construct = replacers.process
local step = replacers.step
if not construct or not step then
return ""
end
--
local calls = { }
local aliases = { }
local ncalls = 0
local naliases = 0
local f_alias = formatters["local %s = %s"]
--
local list = t.list
local order = t.order
local kind = t.kind
local gskip = t.gskip
local askip = t.askip
local name = t.name or "?"
local steps = 0
usedcount = usedcount + 1
--
if trace_detail then
naliases = naliases + 1
aliases[naliases] = formatters["local report = logs.reporter('sequencer',%q)"](name)
ncalls = ncalls + 1
calls[ncalls] = [[report("start")]]
end
for i=1,#order do
local group = order[i]
if not gskip[group] then
local actions = list[group]
for i=1,#actions do
local action = actions[i]
if not askip[action] then
steps = steps + 1
if trace_used or trace_detail then
local action = tostring(action)
report("%02i: category %a, group %a, action %a",usedcount,name,group,action)
usednames[action] = true
end
if trace_detail then
ncalls = ncalls + 1
calls[ncalls] = formatters[ [[report(" step %a, action %a")]] ](steps,tostring(action))
end
local localized = localize(action)
local onestep = replacers[kind[action]] or step
naliases = naliases + 1
ncalls = ncalls + 1
aliases[naliases] = f_alias(localized,action)
calls [ncalls] = onestep { action = localized }
end
end
end
end
t.steps = steps
local processor
if steps == 0 then
processor = templates.default or construct { }
else
if trace_detail then
ncalls = ncalls + 1
calls[ncalls] = [[report("stop")]]
end
processor = construct {
localize = concat(aliases,"\n"),
actions = concat(calls,"\n"),
}
end
-- processor = "print('running : " .. (t.name or "?") .. "')\n" .. processor
-- print(processor)
return processor
end
statistics.register("used sequences",function()
if next(usednames) then
return concat(sortedkeys(usednames)," ")
end
end)
|