Debian Codegraph @

texmf-dist/tex/luatex/luakeyval/luakeyval.lua

raw
1 -- luakeyval Version: 0.1, 2025-12-01
2
3 local put_next = token.unchecked_put_next
4 local get_next = token.get_next
5 local scan_toks = token.scan_toks
6 local scan_keyword = token.scan_keyword_cs
7
8 local texerror, utfchar = tex.error, utf8.char
9 local format = string.format
10
11 -- local relax = token.new(token.biggest_char() + 1)
12 local relax
13 do
14 -- initialization of the new primitives.
15 local prefix = '@lua^key&val_' -- unlikely prefix...
16 while token.is_defined(prefix .. 'relax') do
17 prefix = prefix .. '@lua^key&val_'
18 end
19 tex.enableprimitives(prefix,{'relax'})
20 -- Now we create new tokens with the meaning of
21 -- the primitives.
22 local tok = token.create(prefix .. 'relax')
23 relax = token.new(tok.mode, tok.command)
24 end
25
26 local function check_delimiter(error1, error2, key)
27 local tok = get_next()
28 if tok.tok ~= relax.tok then
29 local tok_name = tok.csname or utfchar(tok.mode)
30 texerror(format(error1, key, tok_name),{format(error2, key, tok_name)})
31 put_next({tok})
32 end
33 end
34
35 local unpack = table.unpack
36 local function process_keys(keys, messages, order)
37 assert(type(keys) == 'table')
38 local matched, vals, curr_key = true, { }
39 messages = messages or { }
40 local value_forbidden = messages.value_forbidden
41 or 'luakeyval: The key "%s" does not accept a value'
42 local value_required = messages.value_required
43 or 'luakeyval: The key "%s" requires a value'
44 local error1 = messages.error1
45 or 'luakeyval: Wrong syntax when processing keys'
46 local error2 = messages.error2
47 or 'luakeyval: The last scanned key was "%s".\nUnexpected token "%s" encountered.'
48 local key_list = { }
49 if order then
50 for _, k in ipairs(order) do
51 key_list[#key_list+1] = k
52 end
53 else
54 for k in pairs(keys) do
55 key_list[#key_list+1] = k
56 end
57 end
58 local toks = scan_toks()
59 toks[#toks+1] = relax
60 put_next(toks)
61 while matched do
62 matched = false
63 for _, key in ipairs(key_list) do
64 local param = keys[key]
65 if scan_keyword(key) then
66 matched = true
67 curr_key = key
68 local args = param.args or { }
69 local scanner = param.scanner
70 local val
71 if scan_keyword('=') then
72 if scanner then
73 val = scanner(unpack(args))
74 else
75 texerror(format(value_forbidden, key))
76 end
77 else
78 val = param.default
79 if val == nil then
80 texerror(format(value_required, key))
81 end
82 end
83 local func = param.func
84 if func then func(key,val) end
85 vals[key] = val
86 break
87 end
88 end
89 end
90 check_delimiter(error1, error2, curr_key or '<none>')
91 return vals
92 end
93
94 local function scan_choice(...)
95 local choices = {...}
96 for _, choice in ipairs(choices) do
97 if scan_keyword(choice) then
98 return choice
99 end
100 end
101 return nil
102 end
103
104 local function scan_bool()
105 if scan_keyword('true') then
106 return true
107 elseif scan_keyword('false') then
108 return false
109 end
110 return nil
111 end
112
113 return {
114 process = process_keys,
115 choices = scan_choice,
116 bool = scan_bool,
117 }