• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 11573237387

29 Oct 2024 11:50AM UTC coverage: 57.701% (-2.2%) from 59.882%
11573237387

push

github

web-flow
Merge f9757d6cf into 8390534e6

10284 of 17823 relevant lines covered (57.7%)

4153.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.38
/packages/math/texlike.lua
1
local syms = require("packages.math.unicode-symbols")
2✔
2
local bits = require("core.parserbits")
2✔
3

4
local epnf = require("epnf")
2✔
5
local lpeg = require("lpeg")
2✔
6

7
local atomType = syms.atomType
2✔
8
local symbolDefaults = syms.symbolDefaults
2✔
9
local symbols = syms.symbols
2✔
10

11
-- Grammar to parse TeX-like math
12
-- luacheck: push ignore
13
-- stylua: ignore start
14
---@diagnostic disable: undefined-global, unused-local, lowercase-global
15
local mathGrammar = function (_ENV)
16
   local _ = WS^0
2✔
17
   local eol = S"\r\n"
2✔
18
   local digit = R("09")
2✔
19
   local natural = digit^1 / tostring
2✔
20
   local pos_natural = R("19") * digit^0 / tonumber
2✔
21
   local ctrl_word = R("AZ", "az")^1
2✔
22
   local ctrl_symbol = P(1) - S"{}\\"
2✔
23
   local ctrl_sequence_name = C(ctrl_word + ctrl_symbol) / 1
2✔
24
   local comment = (
25
         P"%" *
2✔
26
         P(1-eol)^0 *
2✔
27
         eol^-1
2✔
28
      )
29
   local utf8cont = R("\128\191")
2✔
30
   local utf8code = lpeg.R("\0\127")
2✔
31
      + lpeg.R("\194\223") * utf8cont
2✔
32
      + lpeg.R("\224\239") * utf8cont * utf8cont
2✔
33
      + lpeg.R("\240\244") * utf8cont * utf8cont * utf8cont
2✔
34
   -- Identifiers inside \mo and \mi tags
35
   local sileID = C(bits.identifier + P(1)) / 1
2✔
36
   local mathMLID = (utf8code - S"\\{}%")^1 / function (...)
2✔
37
         local ret = ""
×
38
         local t = {...}
×
39
         for _,b in ipairs(t) do
×
40
         ret = ret .. b
×
41
         end
42
         return ret
×
43
      end
44
   local group = P"{" * V"mathlist" * (P"}" + E("`}` expected"))
4✔
45
   local element_no_infix =
46
      V"def" +
2✔
47
      V"command" +
2✔
48
      group +
2✔
49
      V"argument" +
2✔
50
      V"atom"
2✔
51
   local element =
52
      V"supsub" +
2✔
53
      V"subsup" +
2✔
54
      V"sup" +
2✔
55
      V"sub" +
2✔
56
      element_no_infix
2✔
57
   local sep = S",;" * _
2✔
58
   local quotedString = (P'"' * C((1-P'"')^1) * P'"')
2✔
59
   local value = ( quotedString + (1-S",;]")^1 )
2✔
60
   local pair = Cg(sileID * _ * "=" * _ * C(value)) * sep^-1 / function (...)
2✔
61
      local t = {...}; return t[1], t[#t]
32✔
62
   end
63
   local list = Cf(Ct"" * pair^0, rawset)
2✔
64
   local parameters = (
65
         P"[" *
2✔
66
         list *
2✔
67
         P"]"
2✔
68
      )^-1 / function (a)
2✔
69
            return type(a)=="table" and a or {}
184✔
70
         end
71
   local dim2_arg_inner = Ct(V"mathlist" * (P"&" * V"mathlist")^0) /
2✔
72
      function (t)
73
         t.id = "mathlist"
×
74
         return t
×
75
      end
76
   local dim2_arg =
77
      Cg(P"{" *
4✔
78
         dim2_arg_inner *
2✔
79
         (P"\\\\" * dim2_arg_inner)^1 *
2✔
80
         (P"}" + E("`}` expected"))
4✔
81
         ) / function (...)
×
82
            local t = {...}
×
83
            -- Remove the last mathlist if empty. This way,
84
            -- `inner1 \\ inner2 \\` is the same as `inner1 \\ inner2`.
85
            if not t[#t][1] or not t[#t][1][1] then table.remove(t) end
×
86
            return pl.utils.unpack(t)
×
87
         end
88

89
   local dim2_arg_inner = Ct(V"mathlist" * (P"&" * V"mathlist")^0) /
2✔
90
      function (t)
91
         t.id = "mathlist"
×
92
         return t
×
93
      end
94
   local dim2_arg =
95
      Cg(P"{" *
4✔
96
         dim2_arg_inner *
2✔
97
         (P"\\\\" * dim2_arg_inner)^1 *
2✔
98
         (P"}" + E("`}` expected"))
4✔
99
         ) / function (...)
×
100
         local t = {...}
×
101
         -- Remove the last mathlist if empty. This way,
102
         -- `inner1 \\ inner2 \\` is the same as `inner1 \\ inner2`.
103
         if not t[#t][1] or not t[#t][1][1] then table.remove(t) end
×
104
         return pl.utils.unpack(t)
×
105
         end
106

107
   START "math"
2✔
108
   math = V"mathlist" * EOF"Unexpected character at end of math code"
4✔
109
   mathlist = (comment + (WS * _) + element)^0
2✔
110
   supsub = element_no_infix * _ * P"^" * _ * element_no_infix * _ *
2✔
111
      P"_" * _ * element_no_infix
2✔
112
   subsup = element_no_infix * _ * P"_" * _ * element_no_infix * _ *
2✔
113
      P"^" * _ * element_no_infix
2✔
114
   sup = element_no_infix * _ * P"^" * _ * element_no_infix
2✔
115
   sub = element_no_infix * _ * P"_" * _ * element_no_infix
2✔
116
   atom = natural + C(utf8code - S"\\{}%^_&") +
2✔
117
      (P"\\{" + P"\\}") / function (s) return string.sub(s, -1) end
2✔
118
   command = (
×
119
         P"\\" *
2✔
120
         Cg(ctrl_sequence_name, "command") *
2✔
121
         Cg(parameters, "options") *
2✔
122
         (dim2_arg + group^0)
2✔
123
      )
2✔
124
   def = P"\\def" * _ * P"{" *
2✔
125
      Cg(ctrl_sequence_name, "command-name") * P"}" * _ *
2✔
126
      --P"[" * Cg(digit^1, "arity") * P"]" * _ *
127
      P"{" * V"mathlist" * P"}"
2✔
128
   argument = P"#" * Cg(pos_natural, "index")
2✔
129
end
130
-- luacheck: pop
131
-- stylua: ignore end
132
---@diagnostic enable: undefined-global, unused-local, lowercase-global
133

134
local mathParser = epnf.define(mathGrammar)
2✔
135

136
local commands = {}
2✔
137

138
-- A command type is a type for each argument it takes: either string or MathML
139
-- tree. If a command has no type, it is assumed to take only trees.
140
-- Tags like <mi>, <mo>, <mn> take a string, and this needs to be propagated in
141
-- commands that use them.
142

143
local objType = {
2✔
144
   tree = 1,
145
   str = 2,
146
}
147

148
local function inferArgTypes_aux (accumulator, typeRequired, body)
149
   if type(body) == "table" then
124✔
150
      if body.id == "argument" then
124✔
151
         local ret = accumulator
12✔
152
         table.insert(ret, body.index, typeRequired)
12✔
153
         return ret
12✔
154
      elseif body.id == "command" then
112✔
155
         if commands[body.command] then
42✔
156
            local cmdArgTypes = commands[body.command][1]
20✔
157
            if #cmdArgTypes ~= #body then
20✔
158
               SU.error(
×
159
                  "Wrong number of arguments ("
160
                     .. #body
×
161
                     .. ") for command "
×
162
                     .. body.command
×
163
                     .. " (should be "
×
164
                     .. #cmdArgTypes
×
165
                     .. ")"
×
166
               )
167
            else
168
               for i = 1, #cmdArgTypes do
28✔
169
                  accumulator = inferArgTypes_aux(accumulator, cmdArgTypes[i], body[i])
16✔
170
               end
171
            end
172
            return accumulator
20✔
173
         elseif body.command == "mi" or body.command == "mo" or body.command == "mn" then
22✔
174
            if #body ~= 1 then
×
175
               SU.error("Wrong number of arguments (" .. #body .. ") for command " .. body.command .. " (should be 1)")
×
176
            end
177
            accumulator = inferArgTypes_aux(accumulator, objType.str, body[1])
×
178
            return accumulator
×
179
         else
180
            -- Not a macro, recurse on children assuming tree type for all
181
            -- arguments
182
            for _, child in ipairs(body) do
28✔
183
               accumulator = inferArgTypes_aux(accumulator, objType.tree, child)
12✔
184
            end
185
            return accumulator
22✔
186
         end
187
      elseif body.id == "atom" then
70✔
188
         return accumulator
16✔
189
      else
190
         -- Simply recurse on children
191
         for _, child in ipairs(body) do
124✔
192
            accumulator = inferArgTypes_aux(accumulator, typeRequired, child)
140✔
193
         end
194
         return accumulator
54✔
195
      end
196
   else
197
      SU.error("invalid argument to inferArgTypes_aux")
×
198
   end
199
end
200

201
local inferArgTypes = function (body)
202
   return inferArgTypes_aux({}, objType.tree, body)
40✔
203
end
204

205
local function registerCommand (name, argTypes, func)
206
   commands[name] = { argTypes, func }
48✔
207
end
208

209
-- Computes func(func(... func(init, k1, v1), k2, v2)..., k_n, v_n), i.e. applies
210
-- func on every key-value pair in the table. Keys with numeric indices are
211
-- processed in order. This is an important property for MathML compilation below.
212
local function fold_pairs (func, table)
213
   local accumulator = {}
386✔
214
   for k, v in pl.utils.kpairs(table) do
3,578✔
215
      accumulator = func(v, k, accumulator)
2,420✔
216
   end
217
   for i, v in ipairs(table) do
845✔
218
      accumulator = func(v, i, accumulator)
918✔
219
   end
220
   return accumulator
386✔
221
end
222

223
local function forall (pred, list)
224
   for _, x in ipairs(list) do
12✔
225
      if not pred(x) then
20✔
226
         return false
10✔
227
      end
228
   end
229
   return true
2✔
230
end
231

232
local compileToStr = function (argEnv, mathlist)
233
   if #mathlist == 1 and mathlist.id == "atom" then
78✔
234
      -- List is a single atom
235
      return mathlist[1]
×
236
   elseif #mathlist == 1 and mathlist[1].id == "argument" then
78✔
237
      return argEnv[mathlist[1].index]
×
238
   elseif mathlist.id == "argument" then
78✔
239
      return argEnv[mathlist.index]
×
240
   else
241
      local ret = ""
78✔
242
      for _, elt in ipairs(mathlist) do
353✔
243
         if elt.id == "atom" then
275✔
244
            ret = ret .. elt[1]
275✔
245
         elseif elt.id == "command" and symbols[elt.command] then
×
246
            ret = ret .. symbols[elt.command]
×
247
         else
248
            SU.error("Encountered non-character token in command that takes a string")
×
249
         end
250
      end
251
      return ret
78✔
252
   end
253
end
254

255
local function compileToMathML_aux (_, arg_env, tree)
256
   if type(tree) == "string" then
422✔
257
      return tree
36✔
258
   end
259
   local function compile_and_insert (child, key, accumulator)
260
      if type(key) ~= "number" then
1,669✔
261
         accumulator[key] = child
1,210✔
262
         return accumulator
1,210✔
263
      -- Compile all children, except if this node is a macro definition (no
264
      -- evaluation "under lambda") or the application of a registered macro
265
      -- (since evaluating the nodes depends on the macro's signature, it is more
266
      -- complex and done below)..
267
      elseif tree.id == "def" or (tree.id == "command" and commands[tree.command]) then
459✔
268
         -- Conserve unevaluated child
269
         table.insert(accumulator, child)
118✔
270
      else
271
         -- Compile next child
272
         local comp = compileToMathML_aux(nil, arg_env, child)
341✔
273
         if comp then
341✔
274
            if comp.id == "wrapper" then
301✔
275
               -- Insert all children of the wrapper node
276
               for _, inner_child in ipairs(comp) do
122✔
277
                  table.insert(accumulator, inner_child)
61✔
278
               end
279
            else
280
               table.insert(accumulator, comp)
240✔
281
            end
282
         end
283
      end
284
      return accumulator
459✔
285
   end
286
   tree = fold_pairs(compile_and_insert, tree)
772✔
287
   if tree.id == "math" then
386✔
288
      tree.command = "math"
12✔
289
      -- If the outermost `mrow` contains only other `mrow`s, remove it
290
      -- (allowing vertical stacking).
291
      if forall(function (c)
24✔
292
         return c.command == "mrow"
10✔
293
      end, tree[1]) then
24✔
294
         tree[1].command = "math"
2✔
295
         return tree[1]
2✔
296
      end
297
   elseif tree.id == "mathlist" then
374✔
298
      -- Turn mathlist into `mrow` except if it has exactly one `mtr` or `mtd`
299
      -- child.
300
      -- Note that `def`s have already been compiled away at this point.
301
      if #tree == 1 and (tree[1].command == "mtr" or tree[1].command == "mtd") then
83✔
302
         return tree[1]
×
303
      else
304
         tree.command = "mrow"
83✔
305
      end
306
      tree.command = "mrow"
83✔
307
   elseif tree.id == "atom" then
291✔
308
      local codepoints = {}
36✔
309
      for _, cp in luautf8.codes(tree[1]) do
72✔
310
         table.insert(codepoints, cp)
36✔
311
      end
312
      local cp = codepoints[1]
36✔
313
      if
314
         #codepoints == 1
36✔
315
         and ( -- If length of UTF-8 string is 1
×
316
            cp >= SU.codepoint("A") and cp <= SU.codepoint("Z")
90✔
317
            or cp >= SU.codepoint("a") and cp <= SU.codepoint("z")
90✔
318
            or cp >= SU.codepoint("Α") and cp <= SU.codepoint("Ω")
60✔
319
            or cp >= SU.codepoint("α") and cp <= SU.codepoint("ω")
60✔
320
         )
321
      then
322
         tree.command = "mi"
10✔
323
      elseif lpeg.match(lpeg.R("09") ^ 1, tree[1]) then
26✔
324
         tree.command = "mn"
2✔
325
      else
326
         tree.command = "mo"
24✔
327
      end
328
      tree.options = {}
36✔
329
   -- Translate TeX-like sub/superscripts to `munderover` or `msubsup`,
330
   -- depending on whether the base is a big operator
331
   elseif tree.id == "sup" and tree[1].command == "mo" and tree[1].atom == atomType.bigOperator then
255✔
332
      tree.command = "mover"
×
333
   elseif tree.id == "sub" and tree[1].command == "mo" and symbolDefaults[tree[1][1]].atom == atomType.bigOperator then
255✔
334
      tree.command = "munder"
×
335
   elseif
×
336
      tree.id == "subsup"
255✔
337
      and tree[1].command == "mo"
×
338
      and symbolDefaults[tree[1][1]].atom == atomType.bigOperator
×
339
   then
340
      tree.command = "munderover"
×
341
   elseif
×
342
      tree.id == "supsub"
255✔
343
      and tree[1].command == "mo"
×
344
      and symbolDefaults[tree[1][1]].atom == atomType.bigOperator
×
345
   then
346
      tree.command = "munderover"
×
347
      local tmp = tree[2]
×
348
      tree[2] = tree[3]
×
349
      tree[3] = tmp
×
350
   elseif tree.id == "sup" then
255✔
351
      tree.command = "msup"
4✔
352
   elseif tree.id == "sub" then
251✔
353
      tree.command = "msub"
8✔
354
   elseif tree.id == "subsup" then
243✔
355
      tree.command = "msubsup"
×
356
   elseif tree.id == "supsub" then
243✔
357
      tree.command = "msubsup"
×
358
      local tmp = tree[2]
×
359
      tree[2] = tree[3]
×
360
      tree[3] = tmp
×
361
   elseif tree.id == "def" then
243✔
362
      local commandName = tree["command-name"]
40✔
363
      local argTypes = inferArgTypes(tree[1])
40✔
364
      registerCommand(commandName, argTypes, function (compiledArgs)
80✔
365
         return compileToMathML_aux(nil, compiledArgs, tree[1])
61✔
366
      end)
367
      return nil
40✔
368
   elseif tree.id == "command" and commands[tree.command] then
203✔
369
      local argTypes = commands[tree.command][1]
139✔
370
      local cmdFun = commands[tree.command][2]
139✔
371
      local applicationTree = tree
139✔
372
      local cmdName = tree.command
139✔
373
      if #applicationTree ~= #argTypes then
139✔
374
         SU.error(
×
375
            "Wrong number of arguments ("
376
               .. #applicationTree
×
377
               .. ") for command "
×
378
               .. cmdName
×
379
               .. " (should be "
×
380
               .. #argTypes
×
381
               .. ")"
×
382
         )
383
      end
384
      -- Compile every argument
385
      local compiledArgs = {}
139✔
386
      for i, arg in pairs(applicationTree) do
773✔
387
         if type(i) == "number" then
634✔
388
            if argTypes[i] == objType.tree then
78✔
389
               table.insert(compiledArgs, compileToMathML_aux(nil, arg_env, arg))
×
390
            else
391
               local x = compileToStr(arg_env, arg)
78✔
392
               table.insert(compiledArgs, x)
78✔
393
            end
394
         else
395
            -- Not an argument but an attribute. Add it to the compiled
396
            -- argument tree as-is
397
            compiledArgs[i] = applicationTree[i]
556✔
398
         end
399
      end
400
      local res = cmdFun(compiledArgs)
139✔
401
      if res.command == "mrow" then
139✔
402
         -- Mark the outer mrow to be unwrapped in the parent
403
         res.id = "wrapper"
61✔
404
      end
405
      return res
139✔
406
   elseif tree.id == "command" and symbols[tree.command] then
64✔
407
      local atom = { id = "atom", [1] = symbols[tree.command] }
8✔
408
      tree = compileToMathML_aux(nil, arg_env, atom)
16✔
409
   elseif tree.id == "argument" then
56✔
410
      if arg_env[tree.index] then
×
411
         return arg_env[tree.index]
×
412
      else
413
         SU.error("Argument #" .. tree.index .. " has escaped its scope (probably not fully applied command).")
×
414
      end
415
   end
416
   tree.id = nil
205✔
417
   return tree
205✔
418
end
419

420
local function printMathML (tree)
421
   if type(tree) == "string" then
×
422
      return tree
×
423
   end
424
   local result = "\\" .. tree.command
×
425
   if tree.options then
×
426
      local options = {}
×
427
      for k, v in pairs(tree.options) do
×
428
         table.insert(options, k .. "=" .. v)
×
429
      end
430
      if #options > 0 then
×
431
         result = result .. "[" .. table.concat(options, ", ") .. "]"
×
432
      end
433
   end
434
   if #tree > 0 then
×
435
      result = result .. "{"
×
436
      for _, child in ipairs(tree) do
×
437
         result = result .. printMathML(child)
×
438
      end
439
      result = result .. "}"
×
440
   end
441
   return result
×
442
end
443

444
local function compileToMathML (_, arg_env, tree)
445
   local result = compileToMathML_aux(_, arg_env, tree)
12✔
446
   SU.debug("texmath", function ()
24✔
447
      return "Resulting MathML: " .. printMathML(result)
×
448
   end)
449
   return result
12✔
450
end
451

452
local function convertTexlike (_, content)
453
   local ret = epnf.parsestring(mathParser, content[1])
12✔
454
   SU.debug("texmath", function ()
24✔
455
      return "Parsed TeX math: " .. pl.pretty.write(ret)
×
456
   end)
457
   return ret
12✔
458
end
459

460
registerCommand("%", {}, function ()
4✔
461
   return { "%", command = "mo", options = {} }
×
462
end)
463
registerCommand("mi", { [1] = objType.str }, function (x)
4✔
464
   return x
76✔
465
end)
466
registerCommand("mo", { [1] = objType.str }, function (x)
4✔
467
   return x
2✔
468
end)
469
registerCommand("mn", { [1] = objType.str }, function (x)
4✔
470
   return x
×
471
end)
472

473
compileToMathML(
4✔
474
   nil,
2✔
475
   {},
476
   convertTexlike(nil, {
2✔
477
      [==[
×
478
  \def{frac}{\mfrac{#1}{#2}}
479
  \def{sqrt}{\msqrt{#1}}
480
  \def{bi}{\mi[mathvariant=bold-italic]{#1}}
481
  \def{dsi}{\mi[mathvariant=double-struck]{#1}}
482

483
  % Standard spaces gleaned from plain TeX
484
  \def{thinspace}{\mspace[width=thin]}
485
  \def{negthinspace}{\mspace[width=-thin]}
486
  \def{,}{\thinspace}
487
  \def{!}{\negthinspace}
488
  \def{medspace}{\mspace[width=med]}
489
  \def{negmedspace}{\mspace[width=-med]}
490
  \def{>}{\medspace}
491
  \def{thickspace}{\mspace[width=thick]}
492
  \def{negthickspace}{\mspace[width=-thick]}
493
  \def{;}{\thickspace}
494
  \def{enspace}{\mspace[width=1en]}
495
  \def{enskip}{\enspace}
496
  \def{quad}{\mspace[width=1em]}
497
  \def{qquad}{\mspace[width=2em]}
498

499
  % Modulus operator forms
500
  \def{bmod}{\mo{mod}}
501
  \def{pmod}{\quad(\mo{mod} #1)}
502
]==],
503
   })
504
)
505

506
return { convertTexlike, compileToMathML }
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc