Debian Codegraph @

texmf-dist/tex/luatex/lua-uni-algos/lua-uni-data-parser.lua

raw
1 -- lua-uni-data-parser.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 -- This is mainly an internal module used in lua-uni-data to efficiently store
17 -- the data entries.
18 --
19 -- This is an internal module which should only be loaded by other parts of lua-uni-algos.
20 -- If you want to access the data parsed here, please use lua-uni-data.
21
22 local bits = 6
23
24 local parse, lpeg = require'lua-uni-parse', lpeg or require'lpeg'
25 local lookup_table = require'lua-uni-stage-tables'
26 local to_three_stage_lookup = lookup_table.to_three_stage_lookup
27
28 local function dynamic_invertible_table(default)
29 local forward, count = {}, 0
30 local backward = setmetatable({}, {__index = function(t, key)
31 local index = count + 1
32 count = index
33 forward[index], t[key] = key, index
34 return index
35 end})
36 if default ~= nil then
37 forward[0], backward[default] = default, 0
38 end
39 return forward, backward
40 end
41
42 local read_properties do
43 local l, p = lpeg, parse
44 local any_property = l.R('az', 'AZ', '__')^1
45
46 function read_properties(filename, default, properties_pattern)
47 local forward_mapping, inverted_mapping = dynamic_invertible_table(default)
48 properties_pattern = l.P(properties_pattern or any_property)
49 local result = p.parse_file(filename,
50 l.Cg(p.fields(p.codepoint_range, properties_pattern / inverted_mapping)) + p.ignore_line,
51 p.multiset
52 )
53 if not result then
54 return nil, string.format("Failed to parse %s", filename)
55 end
56 return result, forward_mapping
57 end
58 end
59
60 local category_mapping
61 local general_category, ccc, decomposition_mapping, compatibility_mapping, uppercase, lowercase, titlecase do
62 local reverse_category_mapping
63 category_mapping, reverse_category_mapping = dynamic_invertible_table'Cn'
64 local function multiset(ts, key, key_range, general_category, ccc, decomp_kind, decomp_mapping, upper, lower, title)
65 key_range = key_range or key
66 for codepoint=key, key_range do
67 ts[1][codepoint], ts[2][codepoint], ts[4][codepoint], ts[5][codepoint], ts[6][codepoint], ts[7][codepoint] = general_category, ccc, decomp_mapping, upper and upper - codepoint, lower and lower - codepoint, title and title - codepoint or upper and upper - codepoint
68 if not decomp_kind then
69 ts[3][codepoint] = decomp_mapping
70 end
71 end
72 return ts
73 end
74 local l, p = lpeg, parse
75 local Cnil = l.Cc(nil)
76 local letter = l.R('AZ', 'az')
77 local codepoint_or_ud_range = p.codepoint * ( -- When scanning a range in UnicodeData.txt, we use the data from the entry of the end of the range.
78 ';<' * p.ignore_field * lpeg.B', First>' * p.ignore_line * p.codepoint
79 + l.Cc(nil)
80 )
81 local parsed = assert(p.parse_file('UnicodeData', l.Cf(
82 l.Ct(l.Ct'' * l.Ct'' * l.Ct'' * l.Ct'' * l.Ct'' * l.Ct'' * l.Ct'') * (
83 l.Cg(p.fields(codepoint_or_ud_range,
84 p.ignore_field, -- Name (ignored)
85 l.R'AZ' * l.R'az' / reverse_category_mapping, -- General_Category
86 '0' * Cnil + p.number, -- Canonical_Combining_Class
87 p.ignore_field, -- Bidi_Class (ignored)
88 ('<' * l.C(letter^1) * '> ' + Cnil) -- Decomposition_Type
89 * (l.Ct(p.codepoint * (' ' * p.codepoint)^0) + Cnil), -- Decomposition_Mapping
90 p.ignore_field, -- Numeric_Type / Numeric_Value (ignored)
91 p.ignore_field, -- Numeric_Type / Numeric_Value (ignored)
92 p.ignore_field, -- Numeric_Type / Numeric_Value (ignored)
93 p.ignore_field, -- Bidi_Mirrored (ignored)
94 p.ignore_field, -- obsolete
95 p.ignore_field, -- obsolete
96 (p.codepoint + Cnil), -- Simple_Uppercase_Mapping
97 (p.codepoint + Cnil), -- Simple_Lowercase_Mapping
98 (p.codepoint + Cnil)) -- Simple_Titlecase_Mapping
99 ) + p.eol
100 )^0, multiset) * -1))
101 general_category, ccc, decomposition_mapping, compatibility_mapping, uppercase, lowercase, titlecase = unpack(parsed)
102 end
103
104 local grapheme_break_property, grapheme_break_mapping = assert(read_properties('GraphemeBreakProperty', 'Other'))
105 local word_break_property, word_break_mapping = assert(read_properties('WordBreakProperty', 'Other'))
106
107 general_category = to_three_stage_lookup(general_category, category_mapping, bits, bits, 1)
108 ccc = to_three_stage_lookup(ccc, nil, bits, bits, 1)
109 grapheme_break_property = to_three_stage_lookup(grapheme_break_property, grapheme_break_mapping, bits, bits, 1)
110 uppercase = to_three_stage_lookup(uppercase, 'offset', bits, bits, 3)
111 lowercase = to_three_stage_lookup(lowercase, 'offset', bits, bits, 3)
112 titlecase = to_three_stage_lookup(titlecase, 'offset', bits, bits, 3)
113 word_break_property = to_three_stage_lookup(word_break_property, word_break_mapping, bits, bits, 1)
114
115 return {
116 tables = {
117 category = general_category,
118 ccc = ccc,
119 grapheme = grapheme_break_property,
120 uppercase = uppercase,
121 lowercase = lowercase,
122 titlecase = titlecase,
123 wordbreak = word_break_property,
124 },
125 misc = {
126 decomposition_mapping = decomposition_mapping,
127 compatibility_mapping = compatibility_mapping,
128 },
129 }