Debian Codegraph @

texmf-dist/tex/latex/l3kernel/expl3.lua

raw
1 --
2 -- This is file `expl3.lua',
3 -- generated with the docstrip utility.
4 --
5 -- The original source files were:
6 --
7 -- l3luatex.dtx (with options: `lua')
8 -- l3names.dtx (with options: `lua')
9 -- l3basics.dtx (with options: `lua')
10 -- l3sys.dtx (with options: `lua')
11 -- l3token.dtx (with options: `lua')
12 -- l3intarray.dtx (with options: `lua')
13 -- l3pdf.dtx (with options: `lua')
14 --
15 -- Copyright (C) 1990-2026 The LaTeX Project
16 --
17 -- It may be distributed and/or modified under the conditions of
18 -- the LaTeX Project Public License (LPPL), either version 1.3c of
19 -- this license or (at your option) any later version. The latest
20 -- version of this license is in the file:
21 --
22 -- https://www.latex-project.org/lppl.txt
23 --
24 -- This file is part of the "l3kernel bundle" (The Work in LPPL)
25 -- and all files in that bundle must be distributed together.
26 --
27 -- File: l3luatex.dtx
28 ltx = ltx or {utils={}}
29 ltx.utils = ltx.utils or { }
30 local ltxutils = ltx.utils
31 local io = io
32 local kpse = kpse
33 local lfs = lfs
34 local math = math
35 local md5 = md5
36 local os = os
37 local string = string
38 local tex = tex
39 local texio = texio
40 local tonumber = tonumber
41 local token = token
42 local abs = math.abs
43 local byte = string.byte
44 local floor = math.floor
45 local format = string.format
46 local gsub = string.gsub
47 local lfs_attr = lfs.attributes
48 local open = io.open
49 local os_date = os.date
50 local setcatcode = tex.setcatcode
51 local sprint = tex.sprint
52 local cprint = tex.cprint
53 local write = tex.write
54 local write_nl = texio.write_nl
55 local utf8_char = utf8.char
56 local package_loaded = package.loaded
57 local package_searchers = package.searchers
58 local table_concat = table.concat
59
60 local scan_csname = token.scancsname or token.scan_csname
61 local scan_int = token.scan_int or token.scan_integer
62 local scan_string = token.scanstring or token.scan_string
63 local scan_keyword = token.scankeyword or token.scan_keyword
64 local scan_argument = token.scanargument or token.scan_argument
65 local get_next = token.scannext or token.get_next
66 local put_next = token.putnext or token.put_next
67 local token_create = token.create
68 local get_macro = token.getmacro or token.get_macro
69 local get_protected = token.getprotected or token.get_protected
70 local get_csname = token.getcsname or token.get_csname
71 local token_new = token.new
72 local set_macro = token.setmacro or token.set_macro
73
74 local active_prefix = status.luatex_engine == 'luametatex' and status.getconstants().active_character_namespace or utf8.char(0xFFFF)
75
76 local lbrace, rbrace = token_create(byte'{'), token_create(byte'}')
77 local token_create_safe
78 do
79 local is_defined = token.is_defined
80 local set_char = token.set_char or tex.chardef
81 local runtoks = tex.runtoks
82 local let_token = token_create'let'
83
84 function token_create_safe(s)
85 local orig_token = token_create(s)
86 if is_defined(s, true) then
87 return orig_token
88 end
89 set_char(s, 0)
90 local new_token = token_create(s)
91 runtoks(function()
92 put_next(let_token, new_token, orig_token)
93 end)
94 return new_token
95 end
96 end
97
98 local true_tok = token_create_safe'prg_return_true:'
99 local false_tok = token_create_safe'prg_return_false:'
100 local command_id = token.command_id
101 if not command_id and tokens and tokens.commands then
102 local id_map = tokens.commands
103 function command_id(name)
104 return id_map[name]
105 end
106 end
107 local kpse_find = (resolvers and resolvers.findfile) or kpse.find_file
108 local function escapehex(str)
109 return (gsub(str, ".",
110 function (ch) return format("%02X", byte(ch)) end))
111 end
112 local function filedump(name,offset,length)
113 local file = kpse_find(name,"tex",true)
114 if not file then return end
115 local f = open(file,"rb")
116 if not f then return end
117 if offset and offset > 0 then
118 f:seek("set", offset)
119 end
120 local data = f:read(length or 'a')
121 f:close()
122 return escapehex(data)
123 end
124 ltxutils.filedump = filedump
125 local md5_HEX = md5.HEX
126 if not md5_HEX then
127 local md5_sum = md5.sum
128 function md5_HEX(data)
129 return escapehex(md5_sum(data))
130 end
131 md5.HEX = md5_HEX
132 end
133 local function filemd5sum(name)
134 local file = kpse_find(name, "tex", true) if not file then return end
135 local f = open(file, "rb") if not f then return end
136
137 local data = f:read("*a")
138 f:close()
139 return md5_HEX(data)
140 end
141 ltxutils.filemd5sum = filemd5sum
142 local filemoddate
143 if os_date'%z':match'^[+-]%d%d%d%d$' then
144 local pattern = lpeg.Cs(16 *
145 (lpeg.Cg(lpeg.S'+-' * '0000' * lpeg.Cc'Z')
146 + 3 * lpeg.Cc"'" * 2 * lpeg.Cc"'"
147 + lpeg.Cc'Z')
148 * -1)
149 function filemoddate(name)
150 local file = kpse_find(name, "tex", true)
151 if not file then return end
152 local date = lfs_attr(file, "modification")
153 if not date then return end
154 return pattern:match(os_date("D:%Y%m%d%H%M%S%z", date))
155 end
156 else
157 local function filemoddate(name)
158 local file = kpse_find(name, "tex", true)
159 if not file then return end
160 local date = lfs_attr(file, "modification")
161 if not date then return end
162 local d = os_date("*t", date)
163 local u = os_date("!*t", date)
164 local off = 60 * (d.hour - u.hour) + d.min - u.min
165 if d.year ~= u.year then
166 if d.year > u.year then
167 off = off + 1440
168 else
169 off = off - 1440
170 end
171 elseif d.yday ~= u.yday then
172 if d.yday > u.yday then
173 off = off + 1440
174 else
175 off = off - 1440
176 end
177 end
178 local timezone
179 if off == 0 then
180 timezone = "Z"
181 else
182 if off < 0 then
183 timezone = "-"
184 off = -off
185 else
186 timezone = "+"
187 end
188 timezone = format("%s%02d'%02d'", timezone, hours // 60, hours % 60)
189 end
190 return format("D:%04d%02d%02d%02d%02d%02d%s",
191 d.year, d.month, d.day, d.hour, d.min, d.sec, timezone)
192 end
193 end
194 ltxutils.filemoddate = filemoddate
195 local function filesize(name)
196 local file = kpse_find(name, "tex", true)
197 if file then
198 local size = lfs_attr(file, "size")
199 if size then
200 return size
201 end
202 end
203 end
204 ltxutils.filesize = filesize
205 local luacmd do
206 local set_lua = token.setlua or token.set_lua
207 local undefined_cs = command_id'undefined_cs'
208
209 if not context and not luatexbase then require'ltluatex' end
210 if luatexbase then
211 local new_luafunction = luatexbase.new_luafunction
212 local functions = lua.get_functions_table()
213 function luacmd(name, func, ...)
214 local id
215 local tok = token_create(name)
216 if tok.command == undefined_cs then
217 id = new_luafunction(name)
218 set_lua(name, id, ...)
219 else
220 id = tok.index or tok.mode
221 end
222 functions[id] = func
223 end
224 elseif context then
225 local register = context.functions.register
226 local functions = context.functions.known
227 function luacmd(name, func, ...)
228 local tok = token_create(name)
229 if tok.command == undefined_cs then
230 set_lua(name, register(func), ...)
231 else
232 functions[tok.index or tok.mode] = func
233 end
234 end
235 end
236 end
237 local function try_require(name)
238 if package_loaded[name] then
239 return true, package_loaded[name]
240 end
241
242 local failure_details = {}
243 for _, searcher in ipairs(package_searchers) do
244 local loader, data = searcher(name)
245 if type(loader) == 'function' then
246 package_loaded[name] = loader(name, data) or true
247 return true, package_loaded[name]
248 elseif type(loader) == 'string' then
249 failure_details[#failure_details + 1] = loader
250 end
251 end
252
253 return false, table_concat(failure_details, '\n')
254 end
255 local char_given = command_id'char_given'
256 local c_true_bool = token_create(1, char_given)
257 local c_false_bool = token_create(0, char_given)
258 local c_str_cctab = token_create('c_str_cctab').mode
259
260 luacmd('__lua_load_module_p:n', function()
261 local success, result = try_require(scan_string())
262 if success then
263 set_macro(c_str_cctab, 'l__lua_err_msg_str', '')
264 put_next(c_true_bool)
265 else
266 set_macro(c_str_cctab, 'l__lua_err_msg_str', result)
267 put_next(c_false_bool)
268 end
269 end)
270 local register_luadata, get_luadata
271
272 if luatexbase then
273 local register = token_create'@expl@luadata@bytecode'.index
274 if status.ini_version then
275 local luadata, luadata_order = {}, {}
276
277 function register_luadata(name, func)
278 if luadata[name] then
279 error(format("LaTeX error: data name %q already in use", name))
280 end
281 luadata[name] = func
282 luadata_order[#luadata_order + 1] = func and name
283 end
284 luatexbase.add_to_callback("pre_dump", function()
285 if next(luadata) then
286 local str = "return {"
287 for i=1, #luadata_order do
288 local name = luadata_order[i]
289 str = format('%s[%q]=%s,', str, name, luadata[name]())
290 end
291 lua.bytecode[register] = assert(load(str .. "}"))
292 end
293 end, "ltx.luadata")
294 end
295 local luadata = lua.bytecode[register]
296 if luadata then
297 lua.bytecode[register] = nil
298 luadata = luadata()
299 function get_luadata(name)
300 if not luadata then return end
301 local data = luadata[name]
302 luadata[name] = nil
303 return data
304 end
305 end
306 end
307 do
308 if get_luadata then
309 local saved_unidata = get_luadata'lua-uni-data'
310 if saved_unidata then saved_unidata() end
311 end
312 if register_luadata then
313 register_luadata('lua-uni-data', function()
314 return string.format("load(%q, nil, 'b')", string.dump(require'lua-uni-data-preload'.generate_bytecode(), true))
315 end)
316 end
317
318 local uni_data = require'lua-uni-data'
319 local tables, decomposition_mapping = uni_data.tables, uni_data.misc.decomposition_mapping
320
321 luacmd('__kernel_codepoint_data:wn', function()
322 local cp = scan_int()
323 local table = scan_argument()
324 return sprint(-2, tostring(tables[table][cp]))
325 end)
326
327 luacmd('__codepoint_nfd:w', function()
328 local cp = scan_int()
329 local decomposed = decomposition_mapping[cp]
330 if decomposed then
331 for i=1,2 do
332 if decomposed[i] then
333 sprint(-2, lbrace, tostring(decomposed[i]), rbrace)
334 else
335 sprint(-2, lbrace, rbrace)
336 end
337 end
338 else
339 return sprint(-2, lbrace, tostring(cp), rbrace, lbrace, rbrace)
340 end
341 end)
342 end
343 -- File: l3names.dtx
344 local minus_tok = token_new(string.byte'-', 12)
345 local zero_tok = token_new(string.byte'0', 12)
346 local one_tok = token_new(string.byte'1', 12)
347 luacmd('tex_strcmp:D', function()
348 local first = scan_string()
349 local second = scan_string()
350 if first < second then
351 put_next(minus_tok, one_tok)
352 else
353 put_next(first == second and zero_tok or one_tok)
354 end
355 end, 'global')
356 local sprint = tex.sprint
357 local cprint = tex.cprint
358 luacmd('tex_Ucharcat:D', function()
359 local charcode = scan_int()
360 local catcode = scan_int()
361 if catcode == 10 then
362 sprint(token_new(charcode, 10))
363 else
364 cprint(catcode, utf8_char(charcode))
365 end
366 end, 'global')
367 luacmd('tex_filesize:D', function()
368 local size = filesize(scan_string())
369 if size then write(size) end
370 end, 'global')
371 luacmd('tex_mdfivesum:D', function()
372 local hash
373 if scan_keyword"file" then
374 hash = filemd5sum(scan_string())
375 else
376 hash = md5_HEX(scan_string())
377 end
378 if hash then write(hash) end
379 end, 'global')
380 luacmd('tex_filemoddate:D', function()
381 local date = filemoddate(scan_string())
382 if date then write(date) end
383 end, 'global')
384 luacmd('tex_filedump:D', function()
385 local offset = scan_keyword'offset' and scan_int() or nil
386 local length = scan_keyword'length' and scan_int()
387 or not scan_keyword'whole' and 0 or nil
388 local data = filedump(scan_string(), offset, length)
389 if data then write(data) end
390 end, 'global')
391 -- File: l3basics.dtx
392 if status.luatex_engine == 'luametatex' then
393 local function scan_full_csname()
394 local t = get_next()
395 local csname = get_csname(t)
396 return t.active and active_prefix .. csname or csname, t
397 end
398 luacmd('__cs_macro_prefix_spec:N', function()
399 local token = get_next()
400 if get_protected(token) then
401 sprint(-2, "\\protected ")
402 end
403 end, 'global')
404 luacmd('__cs_macro_parameter_spec:N', function()
405 local csname, token = scan_full_csname(true)
406 if token.parameters == 0 then return end
407 sprint(-2, get_macro(csname, false, true))
408 end, 'global')
409 luacmd('__cs_macro_replacement_spec:N', function()
410 local csname = scan_full_csname(true)
411 sprint(-2, get_macro(csname, false, false))
412 end, 'global')
413 end
414 -- File: l3sys.dtx
415 do
416 local os_exec = os.execute
417
418 local function shellescape(cmd)
419 local status,msg = os_exec(cmd)
420 if status == nil then
421 write_nl("log","runsystem(" .. cmd .. ")...(" .. msg .. ").\n")
422 elseif status == 0 then
423 write_nl("log","runsystem(" .. cmd .. ")...executed.\n")
424 else
425 write_nl("log","runsystem(" .. cmd .. ")...failed. " .. (msg or "") .. "\n")
426 end
427 end
428 luacmd("__sys_shell_now:e", function()
429 shellescape(scan_string())
430 end, "global", "protected")
431 local new_latelua = nodes and nodes.nuts and nodes.nuts.pool and nodes.nuts.pool.latelua or (function()
432 local whatsit_id = node.id'whatsit'
433 local latelua_sub = node.subtype'late_lua'
434 local node_new = node.direct.new
435 local setfield = node.direct.setwhatsitfield or node.direct.setfield
436 return function(f)
437 local n = node_new(whatsit_id, latelua_sub)
438 setfield(n, 'data', f)
439 return n
440 end
441 end)()
442 local node_write = node.direct.write
443
444 luacmd("__sys_shell_shipout:e", function()
445 local cmd = scan_string()
446 node_write(new_latelua(function() shellescape(cmd) end))
447 end, "global", "protected")
448 end
449 local gettimeofday = os.gettimeofday
450 local epoch = gettimeofday() - os.clock()
451 local write = tex.write
452 local tointeger = math.tointeger
453 luacmd('__sys_elapsedtime:', function()
454 write(tointeger((gettimeofday() - epoch)*65536 // 1))
455 end, 'global')
456 -- File: l3token.dtx
457 do
458 local get_command = token.get_command
459 local get_index = token.get_index
460 local get_mode = token.get_mode or token.get_index
461 local cmd = command_id
462 local set_font = cmd'get_font'
463 local biggest_char = token.biggest_char and token.biggest_char()
464 or status.getconstants().max_character_code
465
466 local mode_below_biggest_char = {}
467 local index_not_nil = {}
468 local mode_not_null = {}
469 local non_primitive = {
470 [cmd'left_brace'] = true,
471 [cmd'right_brace'] = true,
472 [cmd'math_shift'] = true,
473 [cmd'mac_param' or cmd'parameter'] = mode_below_biggest_char,
474 [cmd'sup_mark' or cmd'superscript'] = true,
475 [cmd'sub_mark' or cmd'subscript'] = true,
476 [cmd'endv' or cmd'ignore'] = true,
477 [cmd'spacer'] = true,
478 [cmd'letter'] = true,
479 [cmd'other_char'] = true,
480 [cmd'tab_mark' or cmd'alignment_tab'] = mode_below_biggest_char,
481 [cmd'char_given'] = true,
482 [cmd'math_given' or 'math_char_given'] = true,
483 [cmd'xmath_given' or 'math_char_xgiven'] = true,
484 [cmd'set_font'] = mode_not_null,
485 [cmd'undefined_cs'] = true,
486 [cmd'call'] = true,
487 [cmd'long_call' or cmd'protected_call'] = true,
488 [cmd'outer_call' or cmd'tolerant_call'] = true,
489 [cmd'long_outer_call' or cmd'tolerant_protected_call'] = true,
490 [cmd'assign_glue' or cmd'register_glue'] = index_not_nil,
491 [cmd'assign_mu_glue' or cmd'register_mu_glue' or cmd'register_muglue'] = index_not_nil,
492 [cmd'assign_toks' or cmd'register_toks'] = index_not_nil,
493 [cmd'assign_int' or cmd'register_int' or cmd'register_integer'] = index_not_nil,
494 [cmd'assign_attr' or cmd'register_attribute'] = true,
495 [cmd'assign_dimen' or cmd'register_dimen' or cmd'register_dimension'] = index_not_nil,
496 }
497
498 luacmd("__token_if_primitive_lua:N", function()
499 local tok = get_next()
500 local is_non_primitive = non_primitive[get_command(tok)]
501 return put_next(
502 is_non_primitive == true
503 and false_tok
504 or is_non_primitive == nil
505 and true_tok
506 or is_non_primitive == mode_not_null
507 and (get_mode(tok) == 0 and true_tok or false_tok)
508 or is_non_primitive == index_not_nil
509 and (get_index(tok) and false_tok or true_tok)
510 or is_non_primitive == mode_below_biggest_char
511 and (get_mode(tok) > biggest_char and true_tok or false_tok))
512 end, "global")
513 end
514 -- File: l3intarray.dtx
515 luacmd('__intarray:w', function()
516 scan_int()
517 tex.error'LaTeX Error: Isolated intarray ignored'
518 end, 'protected', 'global')
519
520 local scan_token = token.scan_token
521 local put_next = token.put_next
522 local intarray_marker = token_create_safe'__intarray:w'
523 local use_none = token_create_safe'use_none:n'
524 local use_i = token_create_safe'use:n'
525 local expand_after_scan_stop = {token_create_safe'exp_after:wN',
526 token_create_safe'scan_stop:'}
527 local comma = token_create(string.byte',')
528 local __intarray_table do
529 local tables = get_luadata and get_luadata'__intarray' or {[0] = {}}
530 function __intarray_table()
531 local t = scan_token()
532 if t ~= intarray_marker then
533 put_next(t)
534 tex.error'LaTeX Error: intarray expected'
535 return tables[0]
536 end
537 local i = scan_int()
538 local current_table = tables[i]
539 if current_table then return current_table end
540 current_table = {}
541 tables[i] = current_table
542 return current_table
543 end
544 if register_luadata then
545 register_luadata('__intarray', function()
546 local t = "{[0]={},"
547 for i=1, #tables do
548 t = string.format("%s{%s},", t, table.concat(tables[i], ','))
549 end
550 return t .. "}"
551 end)
552 end
553 end
554
555 local sprint = tex.sprint
556
557 luacmd('__intarray_gset_count:Nw', function()
558 local t = __intarray_table()
559 local n = scan_int()
560 for i=#t+1, n do t[i] = 0 end
561 end, 'protected', 'global')
562
563 luacmd('intarray_count:N', function()
564 sprint(-2, #__intarray_table())
565 end, 'global')
566 luacmd('__intarray_gset:wF', function()
567 local i = scan_int()
568 local t = __intarray_table()
569 if t[i] then
570 t[i] = scan_int()
571 put_next(use_none)
572 else
573 tex.count.l__intarray_bad_index_int = i
574 scan_int()
575 put_next(use_i)
576 end
577 end, 'protected', 'global')
578
579 luacmd('__intarray_gset:w', function()
580 local i = scan_int()
581 local t = __intarray_table()
582 t[i] = scan_int()
583 end, 'protected', 'global')
584 luacmd('intarray_gzero:N', function()
585 local t = __intarray_table()
586 for i=1, #t do
587 t[i] = 0
588 end
589 end, 'global', 'protected')
590 luacmd('__intarray_item:wF', function()
591 local i = scan_int()
592 local t = __intarray_table()
593 local item = t[i]
594 if item then
595 put_next(use_none)
596 else
597 tex.l__intarray_bad_index_int = i
598 put_next(use_i)
599 end
600 put_next(expand_after_scan_stop)
601 scan_token()
602 if item then
603 sprint(-2, item)
604 end
605 end, 'global')
606
607 luacmd('__intarray_item:w', function()
608 local i = scan_int()
609 local t = __intarray_table()
610 sprint(-2, t[i])
611 end, 'global')
612 local concat = table.concat
613 luacmd('__intarray_to_clist:Nn', function()
614 local t = __intarray_table()
615 local sep = token.scan_string()
616 sprint(-2, concat(t, sep))
617 end, 'global')
618 luacmd('__intarray_range_to_clist:w', function()
619 local t = __intarray_table()
620 local from = scan_int()
621 local to = scan_int()
622 sprint(-2, concat(t, ',', from, to))
623 end, 'global')
624 luacmd('__intarray_gset_range:w', function()
625 local from = scan_int()
626 local t = __intarray_table()
627 while true do
628 local tok = scan_token()
629 if tok == comma then
630 repeat
631 tok = scan_token()
632 until tok ~= comma
633 break
634 else
635 put_next(tok)
636 end
637 t[from] = scan_int()
638 scan_token()
639 from = from + 1
640 end
641 end, 'global', 'protected')
642 -- File: l3pdf.dtx
643
644 local scan_int = token.scan_int
645 local scan_string = token.scan_string
646 local cprint = tex.cprint
647
648 local __pdf_objects_named = {}
649 local __pdf_objects_indexed = {}
650
651 luacmd('__pdf_object_record:nN', function()
652 local name = scan_string()
653 local n = scan_int()
654 __pdf_objects_named[name] = n
655 end,'protected','global')
656
657 local function object_id(name,index)
658 if index then
659 return __pdf_objects_indexed[name][index] or 0
660 else
661 return __pdf_objects_named[name] or 0
662 end
663 end
664
665 luacmd('__pdf_object_retrieve:n', function()
666 local name = scan_string()
667 return cprint(12,tostring(object_id(name)))
668 end,'global')
669
670 ltx.pdf = ltx.pdf or {}
671 ltx.pdf.object_id = object_id
672
673
674 luacmd('__pdf_object_record:nnN', function()
675 local name = scan_string()
676 local index = tonumber(scan_string())
677 local n = scan_int()
678 __pdf_objects_indexed[name] = __pdf_objects_indexed[name] or {}
679 __pdf_objects_indexed[name][index] = n
680 end,'protected','global')
681
682 luacmd('__pdf_object_retrieve:nn', function()
683 local name = scan_string()
684 local index = tonumber(scan_string())
685 return cprint(12,tostring(object_id(name,index)))
686 end,'global')
687