|
5 | 5 | local put_next = token.unchecked_put_next |
6 | 6 | local get_next = token.get_next |
7 | 7 | local scan_toks = token.scan_toks |
8 | | -local scan_keyword = token.scan_keyword |
| 8 | +local scan_keyword = token.scan_keyword_cs |
9 | 9 |
|
10 | 10 | local relax = token.new(token.biggest_char() + 1) |
11 | | -local texerror, uni_char = tex.error, utf8.char |
| 11 | +local texerror, utfchar = tex.error, utf8.char |
12 | 12 | local format = string.format |
13 | | -local function check_delimiter(str1, str2, key) |
| 13 | + |
| 14 | +local function check_delimiter(error1, error2, key) |
14 | 15 | local tok = get_next() |
15 | 16 | if tok.tok ~= relax.tok then |
16 | | - local tok_name = tok.csname or uni_char(tok.mode) |
17 | | - texerror(format(str1, key, tok_name),{format(str2, key, tok_name)}) |
| 17 | + local tok_name = tok.csname or utfchar(tok.mode) |
| 18 | + texerror(format(error1, key, tok_name),{format(error2, key, tok_name)}) |
18 | 19 | put_next({tok}) |
19 | 20 | end |
20 | 21 | end |
21 | 22 |
|
22 | 23 | local unpack, insert = table.unpack, table.insert |
23 | | -local function process_keys(tbl, str1, str2) |
| 24 | +local function process_keys(keys, messages) |
| 25 | + local matched, vals, curr_key = true, { } |
| 26 | + local value_forbidden = messages.value_forbidden |
| 27 | + or "luakeyval: the %s key does not accept a value" |
| 28 | + local value_required = messages.value_required |
| 29 | + or "luakeyval: the %s key require a value" |
| 30 | + local error1 = messages.error1 |
| 31 | + or "wrong syntax when processing keys" |
| 32 | + local error2 = messages.error2 |
| 33 | + or 'the last scanned key was "%s".\nthere is a "%s" in the way.' |
24 | 34 | local toks = scan_toks() |
25 | | - insert(toks, relax) |
| 35 | + insert(toks, relax) |
26 | 36 | put_next(toks) |
27 | | - local matched, vals = true, { } |
28 | | - local curr_key |
29 | 37 | while matched do |
30 | | - matched = false |
31 | | - for key, param in pairs(tbl) do |
32 | | - if scan_keyword(key) then |
33 | | - matched = true |
34 | | - curr_key = key |
35 | | - local args = param.args or {} |
36 | | - local scanner = param.scanner |
37 | | - local val = scan_keyword('=') and |
38 | | - (scanner and scanner(unpack(args)) or true) or |
39 | | - (param.default or true) |
40 | | - local func = param.func |
41 | | - if func then func(key,val) end |
42 | | - vals[key] = val |
43 | | - break |
44 | | - end |
45 | | - end |
| 38 | + matched = false |
| 39 | + for key, param in pairs(keys) do |
| 40 | + if scan_keyword(key) then |
| 41 | + matched = true |
| 42 | + curr_key = key |
| 43 | + local args = param.args or { } |
| 44 | + local scanner = param.scanner |
| 45 | + local val = scan_keyword('=') and |
| 46 | + (scanner and scanner(unpack(args)) or texerror(format(value_forbidden, key))) |
| 47 | + or (param.default or texerror(format(value_required, key))) |
| 48 | + local func = param.func |
| 49 | + if func then func(key,val) end |
| 50 | + vals[key] = val |
| 51 | + break |
| 52 | + end |
| 53 | + end |
46 | 54 | end |
47 | | - check_delimiter(str1, str2, curr_key) |
48 | | - return vals |
| 55 | + check_delimiter(error1, error2, curr_key) |
| 56 | + return vals |
49 | 57 | end |
50 | 58 |
|
51 | 59 | local function scan_choice(...) |
|
59 | 67 |
|
60 | 68 | local function scan_bool() |
61 | 69 | if scan_keyword('true') then |
62 | | - return true |
| 70 | + return true |
63 | 71 | elseif scan_keyword('flase') then |
64 | 72 | return false |
65 | | - end |
| 73 | + end |
66 | 74 | end |
67 | 75 |
|
68 | 76 | return { |
69 | 77 | process = process_keys, |
70 | | - choices = scan_choice, |
71 | | - bool = scan_bool, |
| 78 | + choices = scan_choice, |
| 79 | + bool = scan_bool, |
72 | 80 | } |
0 commit comments