Debian Codegraph @

texmf-dist/tex/luatex/lua-uni-algos/lua-uni-parse.lua

raw
1 -- lua-uni-parse.lua
2 -- Copyright 2020--2025 Marcel Krüger
3 --
4 -- This work may be distributed and/or modified under the
5 -- conditions of the LaTeX Project Public License, either version 1.3
6 -- of this license or (at your option) any later version.
7 -- The latest version of this license is in
8 -- http://www.latex-project.org/lppl.txt
9 -- and version 1.3 or later is part of all distributions of LaTeX
10 -- version 2005/12/01 or later.
11 --
12 -- This work has the LPPL maintenance status `maintained'.
13 --
14 -- The Current Maintainer of this work is Marcel Krüger
15
16 -- Just a simple helper module to make UCD parsing more readable
17
18 -- The rawget is needed here because of Context idiosyncrasies.
19 local find_file = assert(kpse and rawget(kpse, 'find_file') or resolvers and resolvers.find_file, 'No file searching library found')
20
21 local lpeg = lpeg or require'lpeg'
22 local R = lpeg.R
23 local tonumber = tonumber
24
25 local codepoint = lpeg.R('09', 'AF')^4 / function(c) return tonumber(c, 16) end
26 local sep = lpeg.P' '^0 * ';' * lpeg.P' '^0
27 local codepoint_range = codepoint * ('..' * codepoint + lpeg.Cc(false))
28 local ignore_line = (1-lpeg.P'\n')^0 * '\n'
29 local eol = lpeg.S' \t'^0 * ('#' * ignore_line + '\n')
30 local ignored = (1-lpeg.S';#\n')^0
31 local number = lpeg.R'09'^1 / tonumber
32
33 local function fields(first, ...)
34 if first == ignore_line then
35 assert(select('#', ...) == 0)
36 return ignore_line
37 end
38 local tail = select('#', ...) == 0 and eol or sep * fields(...)
39 return first * tail
40 end
41
42 local function multiset(table, key1, key2, value)
43 for key = key1,(key2 or key1) do
44 table[key] = value
45 end
46 return table
47 end
48
49 local function parse_uni_file(filename, patt, func, ...)
50 if func then
51 return parse_uni_file(filename, lpeg.Cf(lpeg.Ct'' * patt^0 * -1, func), nil, ...)
52 end
53 local resolved = find_file(filename .. '.txt')
54 if not resolved then
55 error(string.format("Unable to find Unicode datafile %q", filename))
56 end
57 local f = assert(io.open(resolved))
58 local data = f:read'*a'
59 f:close()
60 return lpeg.match(patt, data, 1, ...)
61 end
62
63 return {
64 codepoint = codepoint,
65 codepoint_range = codepoint_range,
66 ignore_line = ignore_line,
67 ignore_field = ignored,
68 eol = eol,
69 sep = sep,
70 number = number,
71 fields = fields,
72 multiset = multiset,
73 parse_file = parse_uni_file,
74 }