Debian Codegraph @

texmf-dist/tex/luatex/luaotfload/luaotfload-parsers.lua

raw
1 -------------------------------------------------------------------------------
2 -- FILE: luaotfload-parsers.lua
3 -- DESCRIPTION: various lpeg-based parsers used in Luaotfload
4 -- REQUIREMENTS: Luaotfload
5 -- AUTHOR: Philipp Gesang (Phg), <[email protected]>, Marcel Krüger
6 -------------------------------------------------------------------------------
7
8 assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") {
9 name = "luaotfload-parsers",
10 version = "3.29", --TAGVERSION
11 date = "2024-12-03", --TAGDATE
12 description = "luaotfload submodule / parsers",
13 license = "GPL v2.0"
14 }
15
16 local traversal_maxdepth = 42 --- prevent stack overflows
17
18 local rawset = rawset
19
20 local lpeg = require "lpeg"
21 local patterns = lpeg.patterns
22 local P, R, S, V = lpeg.P, lpeg.R, lpeg.S, lpeg.V
23 local lpegmatch = lpeg.match
24 local C, Cc, Cf = lpeg.C, lpeg.Cc, lpeg.Cf
25 local Cg, Cmt, Cs, Ct = lpeg.Cg, lpeg.Cmt, lpeg.Cs, lpeg.Ct
26
27 local kpse = kpse
28 local kpseexpand_path = kpse.expand_path
29 local kpsereadable_file = kpse.readable_file
30
31 local file = file
32 local filejoin = file.join
33 local filedirname = file.dirname
34
35 local io = io
36 local ioopen = io.open
37
38 local logreport = print
39
40 local string = string
41 local stringsub = string.sub
42 local stringfind = string.find
43 local stringlower = string.lower
44
45 local mathceil = math.ceil
46
47 local lfs = lfs
48 local lfsisfile = lfs.isfile
49 local lfsisdir = lfs.isdir
50
51 -------------------------------------------------------------------------------
52 --- COMMON PATTERNS
53 -------------------------------------------------------------------------------
54
55 local asterisk = P"*"
56 local dot = P"."
57 local colon = P":"
58 local semicolon = P";"
59 local comma = P","
60 local noncomma = 1 - comma
61 local slash = P"/"
62 local backslash = P"\\"
63 local equals = P"="
64 local dash = P"-"
65 local gartenzaun = P"#"
66 local lbrk, rbrk = P"[", P"]"
67 local brackets = lbrk + rbrk
68 local squote = P"'"
69 local dquote = P"\""
70
71 local newline = P"\n"
72 local returnchar = P"\r"
73 local spacing = S" \t\v"
74 local linebreak = S"\n\r"
75 local whitespace = spacing + linebreak
76 local ws = spacing^0
77 local xmlws = whitespace^1
78 local eol = P"\n\r" + P"\r\n" + linebreak
79
80 local digit = R"09"
81 local alpha = R("az", "AZ")
82 local anum = alpha + digit
83 local decimal = digit^1 * (dot * digit^0)^-1
84 local hexdigit = R("09", "af", "AF")
85 local hex = P"0x" * hexdigit^1
86
87 -------------------------------------------------------------------------------
88 --- FONTCONFIG
89 -------------------------------------------------------------------------------
90
91 --[[doc--
92
93 For fonts installed on the operating system, there are several
94 options to make Luaotfload index them:
95
96 - If OSFONTDIR is set (which is the case under windows by default
97 but not on the other OSs), it scans it at the same time as the
98 texmf tree, in the function scan_texmf_fonts().
99
100 - Otherwise
101 - under Windows and Mac OSX, we take a look at some hardcoded
102 directories,
103 - under Unix, it reads /etc/fonts/fonts.conf and processes the
104 directories specified there.
105
106 This means that if you have fonts in fancy directories, you need to
107 set them in OSFONTDIR.
108
109 Beware: OSFONTDIR is a kpathsea variable, so fonts found in these
110 paths, though technically system fonts, are registered in the
111 category “texmf”, not “system”. This may have consequences for the
112 lookup order when a font file (or a font with the same name
113 information) is located in both the system and the texmf tree.
114
115 --doc]]--
116
117 local tag_name = C(alpha^1)
118 local comment = P"<!--" * (1 - P"--")^0 * P"-->"
119
120 ---> header specifica
121 local xml_declaration = P"<?xml" * (1 - P"?>")^0 * P"?>"
122 local xml_doctype = P"<!DOCTYPE" * xmlws
123 * "fontconfig" * (1 - P">")^0 * P">"
124 local header = xml_declaration^-1
125 * (xml_doctype + comment + xmlws)^0
126
127 ---> enforce root node
128 local root_start = P"<" * xmlws^-1 * P"fontconfig" * xmlws^-1 * P">"
129 local root_stop = P"</" * xmlws^-1 * P"fontconfig" * xmlws^-1 * P">"
130
131 local dquote, squote = P[["]], P"'"
132 local xml_namestartchar = S":_" + alpha --- ascii only, funk the rest
133 local xml_namechar = S":._" + alpha + digit
134 local xml_name = xmlws^-1
135 * C(xml_namestartchar * xml_namechar^0)
136 local xml_attvalue = dquote * C((1 - S[[%&"]])^1) * dquote * xmlws^-1
137 + squote * C((1 - S[[%&']])^1) * squote * xmlws^-1
138 local xml_attr = Cg(xml_name * P"=" * xml_attvalue)
139 local xml_attr_list = Cf(Ct"" * xml_attr^1, rawset)
140
141 --[[doc--
142 scan_node creates a parser for a given xml tag.
143 --doc]]--
144 --- string -> bool -> lpeg_t
145 local function scan_node (tag)
146 --- Node attributes go into a table with the index “attributes”
147 --- (relevant for “prefix="xdg"” and the likes).
148 local p_tag = P(tag)
149 local with_attributes = P"<" * p_tag
150 * Cg(xml_attr_list, "attributes")^-1
151 * xmlws^-1
152 * P">"
153 local plain = P"<" * p_tag * xmlws^-1 * P">"
154 local node_start = plain + with_attributes
155 local node_stop = P"</" * p_tag * xmlws^-1 * P">"
156 --- there is no nesting, the earth is flat ...
157 local node = node_start
158 * Cc(tag) * C(comment + (1 - node_stop)^1)
159 * node_stop
160 return Ct(node) -- returns {string, string [, attributes = { key = val }] }
161 end
162
163 --[[doc--
164 At the moment, the interesting tags are “dir” for
165 directory declarations, and “include” for including
166 further configuration files.
167
168 spec: http://freedesktop.org/software/fontconfig/fontconfig-user.html
169 --doc]]--
170 local include_node = scan_node"include"
171 local dir_node = scan_node"dir"
172
173 local element = dir_node
174 + include_node
175 + comment --> ignore
176 + P(1-root_stop) --> skip byte
177
178 local root = root_start * Ct(element^0) * root_stop
179 local p_cheapxml = header * root
180
181 --lpeg.print(p_cheapxml) ---> 757 rules with v0.10
182
183 --[[doc--
184 fonts_conf_scanner() handles configuration files.
185 It is called on an absolute path to a config file (e.g.
186 /home/luser/.config/fontconfig/fonts.conf) and returns a list
187 of the nodes it managed to extract from the file.
188 --doc]]--
189 --- string -> path list
190 local function fonts_conf_scanner (path)
191 logreport("both", 5, "db", "Read fontconfig file %s.", path)
192 local fh = ioopen(path, "r")
193 if not fh then
194 logreport("both", 3, "db", "Cannot open fontconfig file %s.", path)
195 return
196 end
197 local raw = fh:read"*all"
198 fh:close()
199 logreport("both", 7, "db",
200 "Reading fontconfig file %s completed (%d bytes).",
201 path, #raw)
202
203 logreport("both", 5, "db", "Scan fontconfig file %s.", path)
204 local confdata = lpegmatch(p_cheapxml, raw)
205 if not confdata then
206 logreport("both", 3, "db", "Cannot scan fontconfig file %s.", path)
207 return
208 end
209 logreport("both", 7, "db", "Scan of fontconfig file %s completed.", path)
210 return confdata
211 end
212
213 local p_conf = P".conf" * P(-1)
214 local p_filter = (1 - p_conf)^1 * p_conf
215
216 local function conf_filter (path)
217 if lpegmatch (p_filter, path) then
218 return true
219 end
220 return false
221 end
222
223 --[[doc--
224
225 read_fonts_conf_indeed() -- Scan paths included from fontconfig
226 configuration files recursively. Called with eight arguments.
227 The first five are
228
229 · the current recursion depth
230 · the path to the file
231 · the expanded $HOME
232 · the expanded $XDG_CONFIG_HOME
233 · the expanded $XDG_DATA_HOME
234
235 determining the path to be checked. Another three arguments are
236 tables that represent the state of the current job as lists of
237 strings; these are always returned. Finally a reference to the
238 find_files function is passed.
239
240 --doc]]--
241
242 --- size_t -> string -> string -> string -> string
243 --- -> string list -> string list -> string list
244 --- -> (string -> fun option -> string list)
245 --- -> tab * tab * tab
246 local function read_fonts_conf_indeed (depth,
247 start,
248 home,
249 xdg_config_home,
250 xdg_data_home,
251 acc,
252 done,
253 dirs_done,
254 find_files,
255 basedir)
256
257 logreport ("both", 4, "db",
258 "Fontconfig scanner processing path %s.",
259 start)
260 if depth >= traversal_maxdepth then
261 --- prevent overflow of Lua call stack
262 logreport ("both", 0, "db",
263 "Fontconfig scanner hit recursion limit (%d); "
264 .. "aborting directory traversal.",
265 traversal_maxdepth)
266 return acc, done, dirs_done
267 end
268
269 local paths = fonts_conf_scanner(start)
270 if not paths then --- nothing to do
271 return acc, done, dirs_done
272 end
273
274 for i=1, #paths do
275 local pathobj = paths[i]
276 local kind, path = pathobj[1], pathobj[2]
277 local attributes = pathobj.attributes
278
279 if kind == "dir" then
280 if attributes and attributes.prefix == "xdg" then
281 path = filejoin(xdg_data_home, path)
282 end
283 if stringsub(path, 1, 1) == "~" then
284 path = filejoin(home, stringsub(path, 2))
285 end
286 --- We exclude paths with texmf in them, as they should be
287 --- found anyway; also duplicates are ignored by checking
288 --- if they are elements of dirs_done.
289 ---
290 --- FIXME does this mean we cannot access paths from
291 --- distributions (e.g. Context minimals) installed
292 --- separately?
293 if not (stringfind(path, "texmf") or dirs_done[path]) then
294 logreport ("log", 5, "db",
295 "New fontconfig path at %s.",
296 path)
297 acc[#acc+1] = path
298 dirs_done[path] = true
299 end
300
301 elseif kind == "include" then
302 if attributes and attributes.prefix == "xdg" then
303 path = filejoin(xdg_config_home, path)
304 end
305 --- here the path can be four things: a directory or a file,
306 --- an absolute or relative path.
307 if stringsub(path, 1, 1) == "~" then
308 path = filejoin(home, stringsub(path, 2))
309 elseif --- if the path is relative, we make it absolute
310 not ( lfsisfile(path) or lfsisdir(path) )
311 then
312 path = filejoin(basedir, path)
313 end
314 if lfsisfile(path)
315 and kpsereadable_file(path)
316 then
317 if done[path] then
318 logreport("log", 3, "db",
319 "Skipping file at %s, already included.", path)
320 else
321 done[path] = true
322 acc = read_fonts_conf_indeed(depth + 1,
323 path,
324 home,
325 xdg_config_home,
326 xdg_data_home,
327 acc,
328 done,
329 dirs_done,
330 find_files,
331 basedir)
332 end
333 elseif lfsisdir(path) then --- arrow code ahead
334 if done[path] then
335 logreport ("log", 3, "db",
336 "Skipping directory at %s, already included.", path)
337 else
338 done[path] = true
339 local config_files = find_files (path, conf_filter)
340 for _, filename in next, config_files do
341 if done[filename] then
342 logreport ("log", 3, "db",
343 "Skipping file at %s, already included.", filename)
344 else
345 done[filename] = true
346 acc = read_fonts_conf_indeed(depth + 1,
347 filename,
348 home,
349 xdg_config_home,
350 xdg_data_home,
351 acc,
352 done,
353 dirs_done,
354 find_files,
355 basedir)
356 end
357 end
358 end
359 end --- match “kind”
360 end --- iterate paths
361 end
362
363 --inspect(acc)
364 --inspect(done)
365 return acc, done, dirs_done
366 end --- read_fonts_conf_indeed()
367
368 --[[doc--
369 read_fonts_conf() sets up an accumulator and two sets
370 for tracking what’s been done.
371
372 Also, the environment variables HOME, XDG_DATA_HOME and
373 XDG_CONFIG_HOME -- which are constants anyways -- are expanded
374 so we don’t have to repeat that over and over again as with the
375 old parser. Now they’re just passed on to every call of
376 read_fonts_conf_indeed().
377 --doc]]--
378
379 --- list -> (string -> function option -> string list) -> list
380
381 local function read_fonts_conf (path_list, find_files)
382 local home = kpseexpand_path"~" --- could be os.getenv"HOME"
383 local xdg_config_home = kpseexpand_path"$XDG_CONFIG_HOME"
384 if xdg_config_home == "" then xdg_config_home = filejoin(home, ".config") end
385 local xdg_data_home = kpseexpand_path"$XDG_DATA_HOME"
386 if xdg_data_home == "" then xdg_data_home = filejoin(home, ".local/share") end
387 local acc = { } ---> list: paths collected
388 local done = { } ---> set: files inspected
389 local dirs_done = { } ---> set: dirs in list
390 for i=1, #path_list do --- we keep the state between files
391 -- MK Pass the base directory
392 acc, done, dirs_done = read_fonts_conf_indeed(0,
393 path_list[i],
394 home,
395 xdg_config_home,
396 xdg_data_home,
397 acc,
398 done,
399 dirs_done,
400 find_files,
401 filedirname(path_list[i]))
402 -- /MK
403 end
404 return acc
405 end
406
407 -------------------------------------------------------------------------------
408 --- MISC PARSERS
409 -------------------------------------------------------------------------------
410
411
412 local trailingslashes = slash^1 * P(-1)
413 local stripslashes = C((1 - trailingslashes)^0)
414
415 local splitcomma = Ct((C(noncomma^1) + comma)^1)
416
417
418
419 -------------------------------------------------------------------------------
420 --- FONT REQUEST
421 -------------------------------------------------------------------------------
422
423
424 --[[doc------------------------------------------------------------------------
425
426 The luaotfload font request syntax (see manual)
427 has a canonical form:
428
429 \font<csname>=<prefix>:<identifier>:<features>
430
431 where
432 <csname> is the control sequence that activates the font
433 <prefix> is either “file” or “name”, determining the lookup
434 <identifier> is either a file name (no path) or a font
435 name, depending on the lookup
436 <features> is a list of switches or options, separated by
437 semicolons or commas; a switch is of the form “+” foo
438 or “-” foo, options are of the form lhs “=” rhs
439
440 however, to ensure backward compatibility we also have
441 support for Xetex-style requests.
442
443 for the Xetex emulation see:
444 · The XeTeX Reference Guide by Will Robertson, 2011
445 · The XeTeX Companion by Michel Goosens, 2010
446 · About XeTeX by Jonathan Kew, 2005
447
448
449 caueat emptor.
450
451 the request is parsed into one of **four** different lookup
452 categories: the regular ones, file and name, as well as the
453 Xetex compatibility ones, path and anon. (maybe a better choice
454 of identifier would be “ambig”.)
455
456 according to my reconstruction, the correct chaining of the
457 lookups for each category is as follows:
458
459 | File -> ( db/filename lookup )
460
461 | Name -> ( db/name lookup,
462 db/filename lookup )
463
464 | Path -> ( db/filename lookup,
465 fullpath lookup )
466
467 | Anon -> ( kpse.find_file(), // <- for tfm, ofm
468 db/name lookup,
469 db/filename lookup,
470 fullpath lookup )
471
472 caching of successful lookups is essential. we now as of v2.2
473 have a lookup cache that is stored in a separate file. it
474 pertains only to name: lookups, and is described in more detail
475 in luaotfload-database.lua.
476
477 -------------------------------------------------------------------------------
478
479 One further incompatibility between Xetex and Luatex-Fonts consists
480 in their option list syntax: apparently, Xetex requires key-value
481 options to be prefixed by a "+" (ascii “plus”) character. We
482 silently accept this as well, dropping the first byte if it is a
483 plus or minus character.
484
485 Reference: https://github.com/lualatex/luaotfload/issues/79#issuecomment-18104483
486
487 --doc]]------------------------------------------------------------------------
488
489
490 --[[doc--
491
492 Xetex style indexing begins at zero which we just increment before
493 passing it along to the font loader. Ymmv.
494
495 --doc]]--
496
497 local function handle_xetex_option (val)
498 return tostring(1 + tonumber(val))
499 end
500
501 local featuresep = comma + semicolon
502
503 --- modifiers ---------------------------------------------------------
504 --[[doc--
505 The slash notation: called “modifiers” (Kew) or “font options”
506 (Robertson, Goosens)
507 we only support the shorthands for italic / bold / bold italic
508 shapes, as well as setting optical size, the rest is ignored.
509 --doc]]--
510 local style_modifier = (S'bB' * S'iI'^-1 + S'iI' * S'bB'^-1 + S'rR')
511 / stringlower
512 local size_modifier = S"Ss" * P"=" --- optical size
513 * Cc"optsize" * C(decimal)
514 local other_modifier = P"AAT" + P"aat" --- apple stuff; unsupported
515 + P"ICU" + P"icu" --- not applicable
516 + P"GR" + P"gr" --- sil stuff; unsupported
517 local garbage_modifier = ((1 - colon - slash)^0 * Cc(false))
518 local modifier = slash * (other_modifier --> ignore
519 + Cs(style_modifier) --> collect
520 + Ct(size_modifier) --> collect
521 + garbage_modifier) --> warn
522 local modifier_list = Cg(Ct(modifier^0), "modifiers")
523
524 --- combining ---------------------------------------------------------
525 ---
526 --- \font \three = combo: 1->42; 2->99,0xdeadbeef; 3->(1,U+2a-U+1337*42)
527 --- v +~ v +~ +~~~~~~~~~ v v +~~~ +~~~~~ +~
528 --- | | | | | | | | | |
529 --- | | | | | | | | | |
530 --- idx : ---+--÷---+--÷--÷-----------+ | +----+ |
531 --- | | | | | |
532 --- id : ------+------+--÷---------------+ +-----------+
533 --- | |
534 --- chars : ----------------+-----------------+
535 ---
536 local comborowsep = ws * semicolon * ws
537 local combomapsep = ws * P"->" * ws
538 local combodefsep = ws * comma * ws
539 local comborangesep = ws * asterisk * ws
540
541 local combohex = hex / tonumber
542 local combouint = digit^1 / tonumber
543 local tonumber16 = function (s) return tonumber (s, 16) end
544 local combouni = P"U+" * (digit^1 / tonumber16)
545 local combonum = combohex + combouni + combouint
546 local comborange = Ct(combonum * dash * combonum) + combonum
547 local combochars = comborange * (comborangesep * comborange)^0
548 local parenthesized = function (p) return P"(" * ws * p * ws * P")" end
549
550 local comboidxpat = Cg(combouint, "idx")
551 local comboidpat = Cg(combouint, "id" )
552 local combocharspat = Cg(P"fallback" * Cc(true) + Ct(combochars^1), "chars")
553 local comboidcharspat = comboidpat * combodefsep * combocharspat
554
555 local comboidx = parenthesized (comboidxpat ) + comboidxpat
556 local comboid = parenthesized (comboidpat ) + comboidpat
557 local comboidchars = parenthesized (comboidcharspat) + comboidcharspat
558
559 local combodef1 = Ct(comboidx * combomapsep * comboid) --> no chars
560 local combodef = Ct(comboidx * combomapsep * comboidchars)
561 local combolist = Ct(combodef1 * (comborowsep * combodef)^1)
562
563 --- subfonts ----------------------------------------------------------
564 --- This rule is present in the original parser. It sets the “sub”
565 --- field of the specification which allows addressing a specific
566 --- font inside a TTC container. Neither in Luatex-Fonts nor in
567 --- Luaotfload is this documented, so we might as well silently drop
568 --- it. However, as backward compatibility is one of our prime goals we
569 --- just insert it here and leave it undocumented until someone cares
570 --- to ask. (Note: afair subfonts are numbered, but this rule matches a
571 --- string; I won’t mess with it though until someone reports a
572 --- problem.)
573 --- local subvalue = P("(") * (C(P(1-S("()"))^1)/issub) * P(")") -- for Kim
574 --- Note to self: subfonts apparently start at index 0. Tested with
575 --- Cambria.ttc that includes “Cambria Math” at 0 and “Cambria” at 1.
576 --- Other values cause luatex to segfault.
577 local subfont = P"(" * Cg(R'09'^1 / function (s)
578 return tonumber(s) + 1
579 end + (1 - S"()")^1, "sub") * P")"
580 -- An optional subfont shouldn't use subfont^-1 to ensure that parens
581 -- at the subfont location are never interpreted in different ways.
582 local maybe_subfont = subfont + #(1 - P"(" + -1)
583
584 --- lookups -----------------------------------------------------------
585 local fontname = C((1-S":(/")^1) --- like luatex-fonts
586 local combo = Cg(P"combo", "lookup") * colon * ws
587 * Cg(combolist, "name")
588 --- initially we intended file: to emulate the behavior of
589 --- luatex-fonts, i.e. no paths allowed. after all, we do have XeTeX
590 --- emulation with the path lookup and it interferes with db lookups.
591 --- turns out fontspec and other widely used packages rely on file:
592 --- with paths already, so we’ll add a less strict rule here. anyways,
593 --- we’ll emit a warning.
594 local prefixed = Cg(P"name" + "file" + "kpse" + "my", "lookup")
595 * colon * ws * Cg(fontname, "name")
596 + Cg(P"id", "lookup")
597 * colon * ws * Cg(R'09'^1 / tonumber, "id")
598 local unprefixed = Cg(Cc"anon", "lookup") * Cg(fontname, "name")
599 --- Bracketed “path” lookups: These may contain any character except
600 --- for unbalanced brackets. A backslash escapes any following
601 --- character. Everything inside the outermost brackets is treated as
602 --- part of the filename or path to look up. Subfonts may be specified
603 --- in parens *after* the closing bracket. Note that this differs from
604 --- Xetex whose syntax expects the subfont passed inside the brackets,
605 --- separated by a colon.
606 local path_escape = backslash / "" * patterns.utf8char
607 local path_content = path_escape + (1 - brackets)
608 local path_balanced = { (path_content + V(2))^1
609 , lbrk * V(1)^-1 * rbrk }
610 local path_lookup = Cg(Cc"path", "lookup")
611 * lbrk * Cg(Cs(path_balanced), "name") * rbrk
612
613 --- features ----------------------------------------------------------
614 local balanced_braces = P{((1 - S'{}') + '{' * V(1) * '}')^0}
615 local field_char = '{' * balanced_braces * '}' + (1 - S'={}' - featuresep)
616 local field = '{' * C(balanced_braces) * '}' + C(field_char^1)
617 --- assignments are “lhs=rhs”
618 --- or “+lhs=rhs” (Xetex-style)
619 --- switches are “+key” | “-key”
620 local normal_option = field * ws * equals * ws * field
621 local xetex_option = P"+" * ws * ( field * ws * equals * ws
622 * (digit^1/handle_xetex_option)
623 + normal_option)
624 local assignment = xetex_option
625 + normal_option
626 local switch = P"-" * ws * field * Cc(false)
627 + P"+"^-1 * ws * field * Cc(true)
628 local feature_expr = ws * Cg(assignment + switch) * ws
629 local option = feature_expr
630 local feature_list = Cf(Ct""
631 * option
632 * (featuresep * option^-1)^0
633 , rawset)
634 * featuresep^-1
635 --- top-level rules ---------------------------------------------------
636 --- \font\foo=<specification>:<features>
637 local features = Cg(feature_list, "features")
638 local specification = (path_lookup + prefixed + unprefixed)
639 * maybe_subfont
640 * modifier_list
641 local font_request = Ct(combo --> TODO: feature list needed?
642 + specification * (colon^-1 * features)^-1)
643
644 -- lpeg.print(font_request)
645 --- v2.5 parser: 1065 rules
646 --- v1.2 parser: 230 rules
647
648 -------------------------------------------------------------------------------
649 --- INI FILES
650 -------------------------------------------------------------------------------
651
652 --[[doc--
653
654 Luaotfload uses the pervasive flavor of the INI files that allows '#' in
655 addition to ';' to indicate comment lines (see git-config(1) for a
656 description of the syntax we’re targeting).
657
658 --doc]]--
659
660 local truth_ids = {
661 ["true"] = true,
662 ["1"] = true,
663 yes = true,
664 on = true,
665 ["false"] = false,
666 ["2"] = false,
667 no = false,
668 off = false,
669 }
670
671 local function maybe_cast (var)
672 local bool = truth_ids[var]
673 if bool ~= nil then
674 return bool
675 end
676 return tonumber (var) or var
677 end
678
679 local function escape (chr, repl)
680 return (backslash * P(chr) / (repl or chr))
681 end
682
683 local valid_escapes = escape "\""
684 + escape "\\"
685 + escape ("n", "\n")
686 + escape ("t", "\t")
687 + escape ("b", "\b")
688 local comment_char = semicolon + gartenzaun
689 local comment_line = ws * comment_char * (1 - eol)^0 * eol
690 local blank_line = ws * eol
691 local skip_line = comment_line + blank_line
692 local ini_id_char = alpha + (dash / "_")
693 local ini_id = Cs(alpha * ini_id_char^0) / stringlower
694 local ini_value_char = (valid_escapes + (1 - linebreak - backslash - comment_char))
695 local ini_value = (Cs (ini_value_char^0) / string.strip)
696 * (comment_char * (1 - eol)^0)^-1
697 local ini_string_char = (valid_escapes + (1 - linebreak - dquote - backslash))
698 local ini_string = dquote
699 * Cs (ini_string_char^0)
700 * dquote
701
702 local ini_heading_title = Ct (Cg (ini_id, "title")
703 * (ws * Cg (ini_string / stringlower, "subtitle"))^-1)
704 local ini_heading = lbrk * ws
705 * Cg (ini_heading_title, "section")
706 * ws * rbrk * ws * eol
707
708 local ini_variable_full = Cg (ws
709 * ini_id
710 * ws
711 * equals
712 * ws
713 * (ini_string + (ini_value / maybe_cast))
714 * ws
715 * eol)
716 local ini_variable_true = Cg (ws * ini_id * ws * eol * Cc (true))
717 local ini_variable = ini_variable_full
718 + ini_variable_true
719 + skip_line
720 local ini_variables = Cg (Cf (Ct "" * ini_variable^0, rawset), "variables")
721
722 local ini_section = Ct (ini_heading * ini_variables)
723 local ini_sections = skip_line^0 * ini_section^0
724 local parse_config = Ct (ini_sections)
725
726 --[=[doc--
727
728 The INI parser converts an input of the form
729
730 [==[
731 [foo]
732 bar = baz
733 xyzzy = no
734 buzz
735
736 [lavernica "brutalitops"]
737 # It’s a locomotive that runs on us.
738 laan-ev = zip zop zooey ; jib-jab
739 Crouton = "Fibrosis \"\\ # "
740
741 ]==]
742
743 to a Lua table of the form
744
745 { { section = { title = "foo" },
746 variables = { bar = "baz",
747 xyzzy = false,
748 buzz = true } },
749 { section = { title = "boing",
750 subtitle = "brutalitops" },
751 variables = { ["laan-ev"] = "zip zop zooey",
752 crouton = "Fibrosis \"\\ # " } } }
753
754 --doc]=]--
755
756 luaotfload.parsers = {
757 --- parameters
758 traversal_maxdepth = traversal_maxdepth,
759 --- main parsers
760 read_fonts_conf = read_fonts_conf,
761 font_request = font_request,
762 config = parse_config,
763 --- common patterns
764 stripslashes = stripslashes,
765 splitcomma = splitcomma,
766 }
767
768 return function ()
769 logreport = luaotfload.log.report
770 return true
771 end
772
773 -- vim:ft=lua:tw=71:et:sw=2:sts=4:ts=8