Debian Codegraph @

texmf-dist/tex/lualatex/pyluatex/pyluatex-parsers.lua

raw
1 --[[
2 MIT License
3
4 Copyright (c) 2021-2026 Tobias Enderle
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 IN THE SOFTWARE.
23 --]]
24
25 local P, S, C, Cc, Cs, Ct = lpeg.P, lpeg.S, lpeg.C, lpeg.Cc, lpeg.Cs, lpeg.Ct
26
27 local pyluatex_parsers = {}
28
29 local function rstrip(str)
30 return (str:gsub("%s*$", ""))
31 end
32
33 local function unescape(str)
34 return (str:gsub('\\"', '"'))
35 end
36
37 local sp = S" \t\r\n"^0
38 local key = (1 - S",=")^1 / rstrip
39 local escaped_allowed = P'\\"' + 1 - '"'
40 local quoted = '"' * C(escaped_allowed^0) * '"'
41 local value = quoted + (escaped_allowed - ",")^0 / rstrip
42 local pair = sp * key * "=" * sp * Cs(value / unescape) * sp
43 local flag = sp * key * Cc(true)
44 local option = (pair + flag + sp) * ","
45 local options_patt = Ct((sp * option * sp)^0)
46
47 -- Returns a table containing the key-value pairs or nil on error.
48 -- Valueless options (e.g. "verbose" (as opposed to "session=default"))
49 -- receive a value of true (boolean type).
50 function pyluatex_parsers.parse_options(str)
51 local result = lpeg.match(options_patt, str .. ",")
52 if next(result) == nil and str:find("[^%s,]") ~= nil then
53 return nil -- matching error
54 end
55
56 local t = {}
57 for i = 1, #result, 2 do
58 t[result[i]] = result[i + 1]
59 end
60 return t
61 end
62
63 return pyluatex_parsers