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
|
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 format, gsub, concat, gmatch = string.format, string.gsub, table.concat, string.gmatch
local type, loadstring = type, loadstring
utilities = utilities or { }
local tables = utilities.tables
local allocate = utilities.storage.allocate
local sequencers = { }
utilities.sequencers = sequencers
local functions = allocate()
sequencers.functions = functions
local removevalue, insertaftervalue, insertbeforevalue = tables.removevalue, tables.insertaftervalue, tables.insertbeforevalue
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
function sequencers.reset(t)
local s = {
list = { },
order = { },
kind = { },
askip = { },
gskip = { },
dirty = true,
runner = nil,
}
if t then
s.arguments = t.arguments
s.returnvalues = t.returnvalues
s.results = t.results
end
return s
end
function sequencers.prependgroup(t,group,where)
if t then
local order = t.order
removevalue(order,group)
insertbeforevalue(order,where,group)
t.list[group], t.dirty, t.runner = { }, true, nil
end
end
function sequencers.appendgroup(t,group,where)
if t then
local order = t.order
removevalue(order,group)
insertaftervalue(order,where,group)
t.list[group], t.dirty, t.runner = { }, true, nil
end
end
function sequencers.prependaction(t,group,action,where,kind,force)
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], t.dirty, t.runner = kind, true, nil
end
end
end
function sequencers.appendaction(t,group,action,where,kind,force)
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], t.dirty, t.runner = kind, true, nil
end
end
end
function sequencers.enableaction (t,action)
if t then
t.askip[action], t.dirty, t.runner = false, true, nil
end
end
function sequencers.disableaction(t,action)
if t then
t.askip[action], t.dirty, t.runner = true, true, nil
end
end
function sequencers.enablegroup(t,group)
if t then
t.gskip[group], t.dirty, t.runner = false, true, nil
end
end
function sequencers.disablegroup(t,group)
if t then
t.gskip[group], t.dirty, t.runner = true, true, nil
end
end
function sequencers.setkind(t,action,kind)
if t then
t.kind[action], t.dirty, t.runner = kind, true, nil
end
end
function sequencers.removeaction(t,group,action,force)
local g = t and t.list[group]
if g and (force or validaction(action)) then
removevalue(g,action)
t.dirty, t.runner = true, nil
end
end
local function localize(str)
return (gsub(str,"[%.: ]+","_"))
end
local function construct(t,nodummy)
local list, order, kind, gskip, askip = t.list, t.order, t.kind, t.gskip, t.askip
local arguments, returnvalues, results = t.arguments or "...", t.returnvalues, t.results
local variables, calls, n = { }, { }, 0
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 type(action) == "function" then
local name = localize(tostring(action))
functions[name] = action
action = format("utilities.sequencers.functions.%s",name)
end
local localized = localize(action)
n = n + 1
variables[n] = format("local %s = %s",localized,action)
if not returnvalues then
calls[n] = format("%s(%s)",localized,arguments)
elseif n == 1 then
calls[n] = format("local %s = %s(%s)",returnvalues,localized,arguments)
else
calls[n] = format("%s = %s(%s)",returnvalues,localized,arguments)
end
end
end
end
end
t.dirty = false
if nodummy and #calls == 0 then
return nil
else
variables = concat(variables,"\n")
calls = concat(calls,"\n")
if results then
t.compiled = format("%s\nreturn function(%s)\n%s\nreturn %s\nend",variables,arguments,calls,results)
else
t.compiled = format("%s\nreturn function(%s)\n%s\nend",variables,arguments,calls)
end
return t.compiled -- also stored so that we can trace
end
end
sequencers.tostring = construct
sequencers.localize = localize
local function compile(t,compiler,n)
if not t or type(t) == "string" then
-- weird ... t.compiled = t .. so
return false
elseif compiler then
t.compiled = compiler(t,n)
else
t.compiled = construct(t)
end
local runner = loadstring(t.compiled)()
t.runner = runner
return runner -- faster
end
sequencers.compile = compile
function sequencers.autocompile(t,compiler,n) -- to be used in tasks
t.runner = compile(t,compiler,n)
local autorunner = function(...)
return (t.runner or compile(t,compiler,n))(...) -- ugly but less bytecode
end
t.autorunner = autorunner
return autorunner -- one more encapsulation
end
-- we used to deal with tail as well but now that the lists are always
-- double linked and the kernel function no longer expect tail as
-- argument we stick to head and done (done can probably also go
-- as luatex deals with return values efficiently now .. in the
-- past there was some copying involved, but no longer)
local template = [[
%s
return function(head%s)
local ok, done = false, false
%s
return head, done
end]]
function sequencers.nodeprocessor(t,nofarguments) -- todo: handle 'kind' in plug into tostring
local list, order, kind, gskip, askip = t.list, t.order, t.kind, t.gskip, t.askip
local vars, calls, args, n = { }, { }, nil, 0
if nofarguments == 0 then
args = ""
elseif nofarguments == 1 then
args = ",one"
elseif nofarguments == 2 then
args = ",one,two"
elseif nofarguments == 3 then
args = ",one,two,three"
elseif nofarguments == 4 then
args = ",one,two,three,four"
elseif nofarguments == 5 then
args = ",one,two,three,four,five"
else
args = ",..."
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
local localized = localize(action)
n = n + 1
vars[n] = format("local %s = %s",localized,action)
-- only difference with tostring is kind and rets (why no return)
if kind[action] == "nohead" then
calls[n] = format(" ok = %s(head%s) done = done or ok",localized,args)
else
calls[n] = format(" head, ok = %s(head%s) done = done or ok",localized,args)
end
end
end
end
end
local processor = format(template,concat(vars,"\n"),args,concat(calls,"\n"))
-- print(processor)
return processor
end
|