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

sile-typesetter / sile / 11170735472

03 Oct 2024 10:32PM UTC coverage: 58.612% (-4.5%) from 63.103%
11170735472

push

github

web-flow
Merge bcab25790 into 783083345

15 of 64 new or added lines in 5 files covered. (23.44%)

828 existing lines in 41 files now uncovered.

10478 of 17877 relevant lines covered (58.61%)

2029.7 hits per line

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

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

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

7
local atomType = syms.atomType
9✔
8
local symbolDefaults = syms.symbolDefaults
9✔
9
local symbols = syms.symbols
9✔
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
9✔
17
   local eol = S"\r\n"
9✔
18
   local digit = R("09")
9✔
19
   local natural = digit^1 / tostring
9✔
20
   local pos_natural = R("19") * digit^0 / tonumber
9✔
21
   local ctrl_word = R("AZ", "az")^1
9✔
22
   local ctrl_symbol = P(1) - S"{}\\"
9✔
23
   local ctrl_sequence_name = C(ctrl_word + ctrl_symbol) / 1
9✔
24
   local comment = (
25
         P"%" *
9✔
26
         P(1-eol)^0 *
9✔
27
         eol^-1
9✔
28
      )
29
   local utf8cont = R("\128\191")
9✔
30
   local utf8code = lpeg.R("\0\127")
9✔
31
      + lpeg.R("\194\223") * utf8cont
9✔
32
      + lpeg.R("\224\239") * utf8cont * utf8cont
9✔
33
      + lpeg.R("\240\244") * utf8cont * utf8cont * utf8cont
9✔
34
   -- Identifiers inside \mo and \mi tags
35
   local sileID = C(bits.identifier + P(1)) / 1
9✔
36
   local mathMLID = (utf8code - S"\\{}%")^1 / function (...)
9✔
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"))
18✔
45
   local element_no_infix =
46
      V"def" +
9✔
47
      V"command" +
9✔
48
      group +
9✔
49
      V"argument" +
9✔
50
      V"atom"
9✔
51
   local element =
52
      V"supsub" +
9✔
53
      V"subsup" +
9✔
54
      V"sup" +
9✔
55
      V"sub" +
9✔
56
      element_no_infix
9✔
57
   local sep = S",;" * _
9✔
58
   local quotedString = (P'"' * C((1-P'"')^1) * P'"')
9✔
59
   local value = ( quotedString + (1-S",;]")^1 )
9✔
60
   local pair = Cg(sileID * _ * "=" * _ * C(value)) * sep^-1 / function (...)
9✔
61
      local t = {...}; return t[1], t[#t]
125✔
62
   end
63
   local list = Cf(Ct"" * pair^0, rawset)
9✔
64
   local parameters = (
65
         P"[" *
9✔
66
         list *
9✔
67
         P"]"
9✔
68
      )^-1 / function (a)
9✔
69
            return type(a)=="table" and a or {}
413✔
70
         end
71
   local dim2_arg_inner = Ct(V"mathlist" * (P"&" * V"mathlist")^0) /
9✔
72
      function (t)
73
         t.id = "mathlist"
×
74
         return t
×
75
      end
76
   local dim2_arg =
77
      Cg(P"{" *
18✔
78
         dim2_arg_inner *
9✔
79
         (P"\\\\" * dim2_arg_inner)^1 *
9✔
80
         (P"}" + E("`}` expected"))
18✔
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) /
9✔
90
      function (t)
91
         t.id = "mathlist"
16✔
92
         return t
16✔
93
      end
94
   local dim2_arg =
95
      Cg(P"{" *
18✔
96
         dim2_arg_inner *
9✔
97
         (P"\\\\" * dim2_arg_inner)^1 *
9✔
98
         (P"}" + E("`}` expected"))
18✔
99
         ) / function (...)
×
100
         local t = {...}
4✔
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
4✔
104
         return pl.utils.unpack(t)
4✔
105
         end
106

107
   START "math"
9✔
108
   math = V"mathlist" * EOF"Unexpected character at end of math code"
18✔
109
   mathlist = (comment + (WS * _) + element)^0
9✔
110
   supsub = element_no_infix * _ * P"^" * _ * element_no_infix * _ *
9✔
111
      P"_" * _ * element_no_infix
9✔
112
   subsup = element_no_infix * _ * P"_" * _ * element_no_infix * _ *
9✔
113
      P"^" * _ * element_no_infix
9✔
114
   sup = element_no_infix * _ * P"^" * _ * element_no_infix
9✔
115
   sub = element_no_infix * _ * P"_" * _ * element_no_infix
9✔
116
   atom = natural + C(utf8code - S"\\{}%^_&") +
9✔
117
      (P"\\{" + P"\\}") / function (s) return string.sub(s, -1) end
9✔
118
   command = (
×
119
         P"\\" *
9✔
120
         Cg(ctrl_sequence_name, "command") *
9✔
121
         Cg(parameters, "options") *
9✔
122
         (dim2_arg + group^0)
9✔
123
      )
9✔
124
   def = P"\\def" * _ * P"{" *
9✔
125
      Cg(ctrl_sequence_name, "command-name") * P"}" * _ *
9✔
126
      --P"[" * Cg(digit^1, "arity") * P"]" * _ *
127
      P"{" * V"mathlist" * P"}"
9✔
128
   argument = P"#" * Cg(pos_natural, "index")
9✔
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)
9✔
135

136
local commands = {}
9✔
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 = {
9✔
144
   tree = 1,
145
   str = 2,
146
}
147

148
local function inferArgTypes_aux (accumulator, typeRequired, body)
149
   if type(body) == "table" then
571✔
150
      if body.id == "argument" then
571✔
151
         local ret = accumulator
54✔
152
         table.insert(ret, body.index, typeRequired)
54✔
153
         return ret
54✔
154
      elseif body.id == "command" then
517✔
155
         if commands[body.command] then
192✔
156
            local cmdArgTypes = commands[body.command][1]
93✔
157
            if #cmdArgTypes ~= #body then
93✔
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
132✔
169
                  accumulator = inferArgTypes_aux(accumulator, cmdArgTypes[i], body[i])
78✔
170
               end
171
            end
172
            return accumulator
93✔
173
         elseif body.command == "mi" or body.command == "mo" or body.command == "mn" then
99✔
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
126✔
183
               accumulator = inferArgTypes_aux(accumulator, objType.tree, child)
54✔
184
            end
185
            return accumulator
99✔
186
         end
187
      elseif body.id == "atom" then
325✔
188
         return accumulator
76✔
189
      else
190
         -- Simply recurse on children
191
         for _, child in ipairs(body) do
571✔
192
            accumulator = inferArgTypes_aux(accumulator, typeRequired, child)
644✔
193
         end
194
         return accumulator
249✔
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)
183✔
203
end
204

205
local function registerCommand (name, argTypes, func)
206
   commands[name] = { argTypes, func }
219✔
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 = {}
1,242✔
214
   for k, v in pl.utils.kpairs(table) do
10,234✔
215
      accumulator = func(v, k, accumulator)
6,508✔
216
   end
217
   for i, v in ipairs(table) do
2,939✔
218
      accumulator = func(v, i, accumulator)
3,394✔
219
   end
220
   return accumulator
1,242✔
221
end
222

223
local function forall (pred, list)
224
   for _, x in ipairs(list) do
46✔
225
      if not pred(x) then
74✔
226
         return false
37✔
227
      end
228
   end
229
   return true
9✔
230
end
231

232
local compileToStr = function (argEnv, mathlist)
233
   if #mathlist == 1 and mathlist.id == "atom" then
100✔
234
      -- List is a single atom
235
      return mathlist[1]
×
236
   elseif #mathlist == 1 and mathlist[1].id == "argument" then
100✔
UNCOV
237
      return argEnv[mathlist[1].index]
×
238
   elseif mathlist.id == "argument" then
100✔
239
      return argEnv[mathlist.index]
×
240
   else
241
      local ret = ""
100✔
242
      for _, elt in ipairs(mathlist) do
410✔
243
         if elt.id == "atom" then
310✔
244
            ret = ret .. elt[1]
310✔
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
100✔
252
   end
253
end
254

255
local function compileToMathML_aux (_, arg_env, tree)
256
   if type(tree) == "string" then
1,587✔
257
      return tree
345✔
258
   end
259
   local function compile_and_insert (child, key, accumulator)
260
      if type(key) ~= "number" then
4,951✔
261
         accumulator[key] = child
3,254✔
262
         return accumulator
3,254✔
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
1,697✔
268
         -- Conserve unevaluated child
269
         table.insert(accumulator, child)
316✔
270
      else
271
         -- Compile next child
272
         local comp = compileToMathML_aux(nil, arg_env, child)
1,381✔
273
         if comp then
1,381✔
274
            if comp.id == "wrapper" then
1,198✔
275
               -- Insert all children of the wrapper node
276
               for _, inner_child in ipairs(comp) do
170✔
277
                  table.insert(accumulator, inner_child)
87✔
278
               end
279
            else
280
               table.insert(accumulator, comp)
1,115✔
281
            end
282
         end
283
      end
284
      return accumulator
1,697✔
285
   end
286
   tree = fold_pairs(compile_and_insert, tree)
2,484✔
287
   if tree.id == "math" then
1,242✔
288
      tree.command = "math"
46✔
289
      -- If the outermost `mrow` contains only other `mrow`s, remove it
290
      -- (allowing vertical stacking).
291
      if forall(function (c)
92✔
292
         return c.command == "mrow"
37✔
293
      end, tree[1]) then
92✔
294
         tree[1].command = "math"
9✔
295
         return tree[1]
9✔
296
      end
297
   elseif tree.id == "mathlist" then
1,196✔
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
278✔
302
         return tree[1]
×
303
      else
304
         tree.command = "mrow"
278✔
305
      end
306
      tree.command = "mrow"
278✔
307
   elseif tree.id == "atom" then
918✔
308
      local codepoints = {}
345✔
309
      for _, cp in luautf8.codes(tree[1]) do
702✔
310
         table.insert(codepoints, cp)
357✔
311
      end
312
      local cp = codepoints[1]
345✔
313
      if
314
         #codepoints == 1
345✔
315
         and ( -- If length of UTF-8 string is 1
×
316
            cp >= SU.codepoint("A") and cp <= SU.codepoint("Z")
811✔
317
            or cp >= SU.codepoint("a") and cp <= SU.codepoint("z")
793✔
318
            or cp >= SU.codepoint("Α") and cp <= SU.codepoint("Ω")
523✔
319
            or cp >= SU.codepoint("α") and cp <= SU.codepoint("ω")
517✔
320
         )
321
      then
322
         tree.command = "mi"
108✔
323
      elseif lpeg.match(lpeg.R("09") ^ 1, tree[1]) then
237✔
324
         tree.command = "mn"
100✔
325
      else
326
         tree.command = "mo"
137✔
327
      end
328
      tree.options = {}
345✔
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
573✔
332
      tree.command = "mover"
×
333
   elseif tree.id == "sub" and tree[1].command == "mo" and symbolDefaults[tree[1][1]].atom == atomType.bigOperator then
573✔
UNCOV
334
      tree.command = "munder"
×
335
   elseif
×
336
      tree.id == "subsup"
573✔
337
      and tree[1].command == "mo"
14✔
338
      and symbolDefaults[tree[1][1]].atom == atomType.bigOperator
10✔
339
   then
340
      tree.command = "munderover"
8✔
341
   elseif
×
342
      tree.id == "supsub"
565✔
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
565✔
351
      tree.command = "msup"
10✔
352
   elseif tree.id == "sub" then
555✔
353
      tree.command = "msub"
28✔
354
   elseif tree.id == "subsup" then
527✔
355
      tree.command = "msubsup"
6✔
356
   elseif tree.id == "supsub" then
521✔
357
      tree.command = "msubsup"
×
358
      local tmp = tree[2]
×
359
      tree[2] = tree[3]
×
360
      tree[3] = tmp
×
361
   elseif tree.id == "def" then
521✔
362
      local commandName = tree["command-name"]
183✔
363
      local argTypes = inferArgTypes(tree[1])
183✔
364
      registerCommand(commandName, argTypes, function (compiledArgs)
366✔
365
         return compileToMathML_aux(nil, compiledArgs, tree[1])
83✔
366
      end)
367
      return nil
183✔
368
   elseif tree.id == "command" and commands[tree.command] then
338✔
369
      local argTypes = commands[tree.command][1]
184✔
370
      local cmdFun = commands[tree.command][2]
184✔
371
      local applicationTree = tree
184✔
372
      local cmdName = tree.command
184✔
373
      if #applicationTree ~= #argTypes then
184✔
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 = {}
184✔
386
      for i, arg in pairs(applicationTree) do
1,053✔
387
         if type(i) == "number" then
869✔
388
            if argTypes[i] == objType.tree then
133✔
389
               table.insert(compiledArgs, compileToMathML_aux(nil, arg_env, arg))
66✔
390
            else
391
               local x = compileToStr(arg_env, arg)
100✔
392
               table.insert(compiledArgs, x)
100✔
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]
736✔
398
         end
399
      end
400
      local res = cmdFun(compiledArgs)
184✔
401
      if res.command == "mrow" then
184✔
402
         -- Mark the outer mrow to be unwrapped in the parent
403
         res.id = "wrapper"
83✔
404
      end
405
      return res
184✔
406
   elseif tree.id == "command" and symbols[tree.command] then
154✔
407
      local atom = { id = "atom", [1] = symbols[tree.command] }
44✔
408
      tree = compileToMathML_aux(nil, arg_env, atom)
88✔
409
   elseif tree.id == "argument" then
110✔
410
      if arg_env[tree.index] then
33✔
411
         return arg_env[tree.index]
33✔
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
833✔
417
   return tree
833✔
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)
46✔
446
   SU.debug("texmath", function ()
92✔
447
      return "Resulting MathML: " .. printMathML(result)
×
448
   end)
449
   return result
46✔
450
end
451

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

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

473
compileToMathML(
18✔
474
   nil,
9✔
475
   {},
476
   convertTexlike(nil, {
9✔
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 }
9✔
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