mult-chk.lua /size: 2483 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['mult-chk'] = {
2    version   = 1.001,
3    comment   = "companion to mult-chk.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local format = string.format
10local lpegmatch = lpeg.match
11local type = type
12
13local make_settings_to_hash_pattern = utilities.parsers.make_settings_to_hash_pattern
14local settings_to_set               = utilities.parsers.settings_to_set
15local allocate                      = utilities.storage.allocate
16
17local report_interface = logs.reporter("interface","checking")
18
19local interfaces = interfaces
20local implement  = interfaces.implement
21
22interfaces.syntax = allocate {
23    test = { keys = table.tohash { "a","b","c","d","e","f","g" } }
24}
25
26function interfaces.invalidkey(category,key)
27    report_interface("invalid key %a for %a in line %a",key,category,tex.inputlineno)
28end
29
30function interfaces.setvalidkeys(category,list)
31    local s = interfaces.syntax[category]
32    if not s then
33        interfaces.syntax[category] = {
34            keys = settings_to_set(list)
35        }
36    else
37        s.keys = settings_to_set(list)
38    end
39end
40
41function interfaces.addvalidkeys(category,list)
42    local s = interfaces.syntax[category]
43    if not s then
44        interfaces.syntax[category] = {
45            keys = settings_to_set(list)
46        }
47    else
48        settings_to_set(list,s.keys)
49    end
50end
51
52implement {
53    name      = "setvalidinterfacekeys",
54    actions   = interfaces.setvalidkeys,
55    arguments = "2 strings"
56}
57
58implement {
59    name      = "addvalidinterfacekeys",
60    actions   = interfaces.addvalidkeys,
61    arguments = "2 strings"
62}
63
64-- weird code, looks incomplete ... probably an experiment
65
66local prefix, category, keys
67
68local setsomevalue = context.setsomevalue
69local invalidkey   = interfaces.invalidkey
70
71local function set(key,value)
72    if keys and not keys[key] then
73        invalidkey(category,key)
74    else
75        setsomevalue(prefix,key,value)
76    end
77end
78
79local pattern = make_settings_to_hash_pattern(set,"tolerant")
80
81function interfaces.getcheckedparameters(k,p,s)
82    if s and s ~= "" then
83        prefix, category = p, k
84        keys = k and k ~= "" and interfaces.syntax[k].keys
85        lpegmatch(pattern,s)
86    end
87end
88
89implement {
90    name      = "getcheckedinterfaceparameters",
91    actions   = interfaces.getcheckedparameters,
92    arguments = "3 strings"
93}
94