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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
if not modules then modules = { } end modules ['toks-scn'] = {
version = 1.001,
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- Writing this kind of code (and completing the newtoken code base) is fun. I did
-- so with the brilliant film music from The Girl with the Dragon Tattoo running in a
-- loop in the background (three cd's by Trent Reznor and Atticus Ross). An alien
-- feeling helps with alien code.
-- todo: more \let's at the tex end
local type, next, tostring, tonumber = type, next, tostring, tonumber
local formatters = string.formatters
local concat = table.concat
local scanners = tokens.scanners
local tokenbits = tokens.bits
local scanstring = scanners.string
local scaninteger = scanners.integer
local scannumber = scanners.number
local scankeyword = scanners.keyword
local scanword = scanners.word
local scancode = scanners.code
local scanboolean = scanners.boolean
local scandimen = scanners.dimen
local scancsname = scanners.csname
local todimen = number.todimen
local toboolean = toboolean
local lpegmatch = lpeg.match
local p_unquoted = lpeg.Cs(lpeg.patterns.unquoted)
local trace_compile = false trackers.register("tokens.compile", function(v) trace_compile = v end)
local report_compile = logs.reporter("tokens","compile")
local report_scan = logs.reporter("tokens","scan")
local open = tokenbits.open
local close = tokenbits.close
local function scanopen()
while true do
local c = scancode(open)
if c == 123 then
return true
-- elseif c ~= 32 then
elseif not c then
return
end
end
end
local function scanclose()
while true do
local c = scancode(close)
if c == 125 then
return true
-- elseif c ~= 32 then
elseif not c then
return
end
end
end
scanners.scanopen = scanopen
scanners.scanclose = scanclose
local function scanlist()
local wrapped = scanopen()
local list = { }
local size = 0
while true do
local entry = scanstring()
if entry then
size = size + 1
list[size] = entry
else
break
end
end
if wrapped then
scanclose()
end
return list
end
local function scanconditional()
local kw = scanword()
if kw == "true" then
return true
end
if kw == "false" then
return false
end
local c = scaninteger()
if c then
return c == 0 -- with a conditional 0=true
end
return nil
end
scanners.list = scanlist
scanners.conditional = scanconditional
local shortcuts = {
tokens = tokens,
bits = tokenbits,
open = open,
close = close,
scanners = scanners,
scanstring = scanstring,
scaninteger = scaninteger,
scannumber = scannumber,
scankeyword = scankeyword,
scanword = scanword,
scancode = scancode,
scanboolean = scanboolean,
scandimen = scandimen,
scandimension = scandimen,
scanconditional = scanconditional,
scanopen = scanopen,
scanclose = scanclose,
scanlist = scanlist,
scancsname = scancsname,
todimen = todimen,
tonumber = tonumber,
tostring = tostring,
toboolean = toboolean,
inspect = inspect,
report = report_scan,
}
tokens.shortcuts = shortcuts
local load = load
local dump = string.dump
local function loadstripped(code)
return load(code,nil,nil,shortcuts)
-- return load(dump(load(code),true),nil,nil,shortcuts)
end
tokens.converters = {
tonumber = "tonumber",
tostring = "tostring",
toboolean = "toboolean",
todimen = "todimen",
toglue = "todimen",
}
local f_if = formatters[ " if scankeyword('%s') then data['%s'] = scan%s()"]
local f_elseif = formatters[" elseif scankeyword('%s') then data['%s'] = scan%s()"]
local f_local = formatters["local scan%s = scanners.%s"]
local f_scan = formatters["scan%s()"]
local f_shortcut = formatters["local %s = scanners.converters.%s"]
local f_if_c = formatters[ " if scankeyword('%s') then data['%s'] = %s(scan%s())"]
local f_elseif_c = formatters[" elseif scankeyword('%s') then data['%s'] = %s(scan%s())"]
local f_scan_c = formatters["%s(scan%s())"]
local f_any = formatters[" else local key = scanword() if key then data[key] = scan%s() else break end end"]
local f_any_c = formatters[" else local key = scanword() if key then data[key] = %s(scan%s()) else break end end"]
local s_done = " else break end"
local f_any_all = formatters[" local key = scanword() if key then data[key] = scan%s() else break end"]
local f_any_all_c= formatters[" local key = scanword() if key then data[key] = %s(scan%s()) else break end"]
local f_table = formatters["%\nt\nreturn function()\n local data = { }\n%s\n return %s\nend\n"]
local f_sequence = formatters["%\nt\n%\nt\n%\nt\nreturn function()\n return %s\nend\n"]
local f_simple = formatters["%\nt\nreturn function()\n return %s\nend\n"]
local f_string = formatters["%q"]
local f_action_f = formatters["action%s(%s)"]
local f_action_s = formatters["local action%s = tokens._action[%s]"]
local f_nested = formatters["local function scan%s()\n local data = { }\n%s\n return data\nend\n"]
-- local f_check = formatters[ [[
-- local wrapped = false
-- while true do
-- local c = scancode(open)
-- if c == 123 then
-- wrapped = true
-- break
-- elseif c ~= 32 then
-- break
-- end
-- end
-- while true do
-- ]] .. "%\nt\n" .. [[
-- %s
-- end
-- if wrapped then
-- while true do
-- local c = scancode(close)
-- if c == 125 then
-- break
-- elseif c ~= 32 then
-- break
-- end
-- end
-- end
-- ]] ]
local f_check = formatters[ [[
local wrapped = scanopen()
while true do
]] .. "%\nt\n" .. [[
%s
end
if wrapped then
scanclose()
end
]] ]
function tokens.compile(specification)
local f = { }
local n = 0
local c = { }
local t = specification.arguments or specification
local a = specification.actions or nil
if type(a) == "function" then
a = { a }
end
local code
local function compile(t,nested)
local done = s_done
local r = { }
local m = 0
for i=1,#t do
local ti = t[i]
if ti == "*" and i == 1 then
done = f_any_all("string")
else
local t1 = ti[1]
local t2 = ti[2] or "string"
if type(t2) == "table" then
n = n + 1
f[n] = compile(t2,n)
t2 = n
end
local t3 = ti[3]
if type(t3) == "function" then
-- todo: also create shortcut
elseif t3 then
c[t3] = f_shortcut(t3,t3)
if t1 == "*" then
if i == 1 then
done = f_any_all_c(t3,t2)
break
else
done = f_any_c(t3,t2)
end
else
m = m + 1
r[m] = (m > 1 and f_elseif_c or f_if_c)(t1,t1,t3,t2)
end
else
if t1 == "*" then
if i == 1 then
done = f_any_all(t2)
break
else
done = f_any(t2)
end
else
m = m + 1
r[m] = (m > 1 and f_elseif or f_if )(t1,t1,t2)
end
end
end
end
local c = f_check(r,done)
if nested then
return f_nested(nested,c)
else
return c
end
end
local tt = type(t)
if tt == "string" then
if a then
local s = lpegmatch(p_unquoted,t)
if s and t ~= s then
code = t
else
code = f_scan(t)
end
tokens._action = a
for i=1,#a do
code = f_action_f(i,code)
f[#f+1] = f_action_s(i,i)
end
code = f_simple(f,code)
else
return scanners[t]
end
elseif tt ~= "table" then
return
elseif #t == 1 then
local ti = t[1]
if type(ti) == "table" then
ti = compile(ti)
code = "data"
if a then
tokens._action = a
for i=1,#a do
code = f_action_f(i,code)
f[#f+1] = f_action_s(i,i)
end
end
code = f_table(f,ti,code)
elseif a then
code = f_scan(ti)
tokens._action = a
for i=1,#a do
code = f_action_f(i,code)
f[#f+1] = f_action_s(i,i)
end
code = f_simple(f,code)
else
return scanners[ti]
end
else
local r = { }
local p = { }
local m = 0
for i=1,#t do
local ti = t[i]
local tt = type(ti)
if tt == "table" then
if ti[1] == "_constant_" then
local v = ti[2]
if type(v) == "string" then
r[i] = f_string(v)
else
r[i] = tostring(v)
end
else
m = m + 1
p[m] = compile(ti,100+m)
r[i] = f_scan(100+m)
end
elseif tt == "number" then
r[i] = tostring(ti)
elseif tt == "boolean" then
r[i] = tostring(ti)
else
local s = lpegmatch(p_unquoted,ti)
if s and ti ~= s then
r[i] = ti -- a string, given as "'foo'" or '"foo"'
elseif scanners[ti] then
r[i] = f_scan(ti)
else
report_compile("unknown scanner %a",ti)
r[i] = ti
end
end
end
code = concat(r,",")
if a then
tokens._action = a
for i=1,#a do
code = f_action_f(i,code)
f[#f+1] = f_action_s(i,i)
end
end
code = f_sequence(c,f,p,code)
end
if not code then
return
end
if trace_compile then
report_compile("code: %s",code)
end
local code, message = loadstripped(code)
if code then
code = code() -- sets action
else
report_compile("error in code: %s",code)
report_compile("error message: %s",message)
end
if a then
tokens._action = nil
end
if code then
return code
end
end
-- local fetch = tokens.compile {
-- "string",
-- "string",
-- {
-- { "data", "string" },
-- { "tab", "string" },
-- { "method", "string" },
-- { "foo", {
-- { "method", "integer" },
-- { "compact", "number" },
-- { "nature" },
-- { "*" }, -- any key
-- } },
-- { "compact", "string", "tonumber" },
-- { "nature", "boolean" },
-- { "escape", "string" },
-- { "escape" },
-- }
-- "boolean",
-- }
--
-- os.exit()
function tokens.scantable(t,data)
if not data then
data = { }
end
local wrapped = scanopen()
while true do
local key = scanword()
if key then
local get = t[key]
if get then
data[key] = get()
else
-- catch all we can get
end
else
break
end
end
if wrapped then
scanclose()
end
return data
end
function tokens.constant(s)
if type(s) == "string" then
return "'" .. s .. "'"
else
return s
end
end
|