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

sile-typesetter / sile / 6691426968

30 Oct 2023 09:11AM UTC coverage: 62.266% (-12.4%) from 74.636%
6691426968

push

github

alerque
chore(release): 0.14.13

9777 of 15702 relevant lines covered (62.27%)

6531.11 hits per line

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

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

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

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

11
-- Grammar to parse TeX-like math
12
-- luacheck: push ignore
13
---@diagnostic disable: undefined-global, unused-local, lowercase-global
14
local mathGrammar = function (_ENV)
15
  local _ = WS^0
7✔
16
  local eol = S"\r\n"
7✔
17
  local digit = R("09")
7✔
18
  local natural = digit^1 / tostring
7✔
19
  local pos_natural = R("19") * digit^0 / tonumber
7✔
20
  local ctrl_word = R("AZ", "az")^1
7✔
21
  local ctrl_symbol = P(1) - S"{}\\"
7✔
22
  local ctrl_sequence_name = C(ctrl_word + ctrl_symbol) / 1
7✔
23
  local comment = (
24
      P"%" *
7✔
25
      P(1-eol)^0 *
7✔
26
      eol^-1
7✔
27
    )
28
  local utf8cont = R("\128\191")
7✔
29
  local utf8code = lpeg.R("\0\127")
7✔
30
    + lpeg.R("\194\223") * utf8cont
7✔
31
    + lpeg.R("\224\239") * utf8cont * utf8cont
7✔
32
    + lpeg.R("\240\244") * utf8cont * utf8cont * utf8cont
7✔
33
  -- Identifiers inside \mo and \mi tags
34
  local sileID = C(bits.identifier + P(1)) / 1
7✔
35
  local mathMLID = (utf8code - S"\\{}%")^1 / function (...)
7✔
36
      local ret = ""
×
37
      local t = {...}
×
38
      for _,b in ipairs(t) do
×
39
        ret = ret .. b
×
40
      end
41
      return ret
×
42
    end
43
  local group = P"{" * V"mathlist" * (P"}" + E("`}` expected"))
14✔
44
  local element_no_infix =
45
    V"def" +
7✔
46
    V"command" +
7✔
47
    group +
7✔
48
    V"argument" +
7✔
49
    V"atom"
7✔
50
  local element =
51
    V"supsub" +
7✔
52
    V"subsup" +
7✔
53
    V"sup" +
7✔
54
    V"sub" +
7✔
55
    element_no_infix
7✔
56
  local sep = S",;" * _
7✔
57
  local quotedString = (P'"' * C((1-P'"')^1) * P'"')
7✔
58
  local value = ( quotedString + (1-S",;]")^1 )
7✔
59
  local pair = Cg(sileID * _ * "=" * _ * C(value)) * sep^-1 / function (...)
7✔
60
      local t = {...}; return t[1], t[#t]
95✔
61
    end
62
  local list = Cf(Ct"" * pair^0, rawset)
7✔
63
  local parameters = (
64
      P"[" *
7✔
65
      list *
7✔
66
      P"]"
7✔
67
    )^-1 / function (a)
7✔
68
        return type(a)=="table" and a or {}
342✔
69
      end
70

71
  local dim2_arg_inner = Ct(V"mathlist" * (P"&" * V"mathlist")^0) /
7✔
72
    function (t)
73
      t.id = "mathlist"
×
74
      return t
×
75
    end
76
  local dim2_arg =
77
    Cg(P"{" *
14✔
78
       dim2_arg_inner *
7✔
79
       (P"\\\\" * dim2_arg_inner)^1 *
7✔
80
       (P"}" + E("`}` expected"))
14✔
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 table.unpack(t)
×
87
      end
88

89
  START "texlike_math"
7✔
90
  texlike_math = V"mathlist" * EOF"Unexpected character at end of math code"
14✔
91
  mathlist = (comment + (WS * _) + element)^0
7✔
92
  supsub = element_no_infix * _ * P"^" * _ * element_no_infix * _ *
7✔
93
    P"_" * _ * element_no_infix
7✔
94
  subsup = element_no_infix * _ * P"_" * _ * element_no_infix * _ *
7✔
95
    P"^" * _ * element_no_infix
7✔
96
  sup = element_no_infix * _ * P"^" * _ * element_no_infix
7✔
97
  sub = element_no_infix * _ * P"_" * _ * element_no_infix
7✔
98
  atom = natural + C(utf8code - S"\\{}%^_&") +
7✔
99
    (P"\\{" + P"\\}") / function (s) return string.sub(s, -1) end
7✔
100
  command = (
×
101
      P"\\" *
7✔
102
      Cg(ctrl_sequence_name, "command") *
7✔
103
      Cg(parameters, "options") *
7✔
104
      (dim2_arg + group^0)
7✔
105
    )
7✔
106
  def = P"\\def" * _ * P"{" *
7✔
107
    Cg(ctrl_sequence_name, "command-name") * P"}" * _ *
7✔
108
    --P"[" * Cg(digit^1, "arity") * P"]" * _ *
109
    P"{" * V"mathlist" * P"}"
7✔
110
  argument = P"#" * Cg(pos_natural, "index")
7✔
111
end
112
-- luacheck: pop
113
---@diagnostic enable: undefined-global, unused-local, lowercase-global
114

115
local mathParser = epnf.define(mathGrammar)
7✔
116

117
local commands = {}
7✔
118

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

124
local objType = {
7✔
125
  tree = 1,
126
  str = 2
×
127
}
128

129
local function inferArgTypes_aux (accumulator, typeRequired, body)
130
  if type(body) == "table" then
406✔
131
    if body.id == "argument" then
406✔
132
      local ret = accumulator
35✔
133
      table.insert(ret, body.index, typeRequired)
35✔
134
      return ret
35✔
135
    elseif body.id == "command" then
371✔
136
      if commands[body.command] then
140✔
137
        local cmdArgTypes = commands[body.command][1]
70✔
138
        if #cmdArgTypes ~= #body then
70✔
139
          SU.error("Wrong number of arguments (" .. #body ..
×
140
            ") for command " .. body.command .. " (should be " ..
×
141
            #cmdArgTypes .. ")")
×
142
        else
143
          for i = 1, #cmdArgTypes do
98✔
144
            accumulator = inferArgTypes_aux(accumulator, cmdArgTypes[i], body[i])
56✔
145
          end
146
        end
147
        return accumulator
70✔
148
      elseif body.command == "mi" or body.command == "mo" or body.command == "mn" then
70✔
149
        if #body ~= 1 then
×
150
          SU.error("Wrong number of arguments ("..#body..") for command "..
×
151
            body.command.." (should be 1)")
×
152
        end
153
        accumulator = inferArgTypes_aux(accumulator, objType.str, body[1])
×
154
        return accumulator
×
155
      else
156
        -- Not a macro, recurse on children assuming tree type for all
157
        -- arguments
158
        for _, child in ipairs(body) do
84✔
159
          accumulator = inferArgTypes_aux(accumulator, objType.tree, child)
28✔
160
        end
161
        return accumulator
70✔
162
      end
163
    elseif body.id == "atom" then
231✔
164
      return accumulator
56✔
165
    else
166
      -- Simply recurse on children
167
      for _, child in ipairs(body) do
406✔
168
        accumulator = inferArgTypes_aux(accumulator, typeRequired, child)
462✔
169
      end
170
      return accumulator
175✔
171
    end
172
  else SU.error("invalid argument to inferArgTypes_aux") end
×
173
end
174

175
local inferArgTypes = function (body)
176
  return inferArgTypes_aux({}, objType.tree, body)
133✔
177
end
178

179
local function registerCommand (name, argTypes, func)
180
  commands[name] = { argTypes, func }
161✔
181
end
182

183
-- Computes func(func(... func(init, k1, v1), k2, v2)..., k_n, v_n), i.e. applies
184
-- func on every key-value pair in the table. Keys with numeric indices are
185
-- processed in order. This is an important property for MathML compilation below.
186
local function fold_pairs (func, table)
187
  local accumulator = {}
931✔
188
  for k, v in pl.utils.kpairs(table) do
7,879✔
189
    accumulator = func(v, k, accumulator)
5,086✔
190
  end
191
  for i, v in ipairs(table) do
2,152✔
192
    accumulator = func(v, i, accumulator)
2,442✔
193
  end
194
  return accumulator
931✔
195
end
196

197
local function forall (pred, list)
198
  for _,x in ipairs(list) do
28✔
199
    if not pred(x) then return false end
42✔
200
  end
201
  return true
7✔
202
end
203

204
local compileToStr = function (argEnv, mathlist)
205
  if #mathlist == 1 and mathlist.id == "atom" then
88✔
206
    -- List is a single atom
207
    return mathlist[1]
×
208
  elseif #mathlist == 1 and mathlist[1].id == "argument" then
88✔
209
    return argEnv[mathlist[1].index]
×
210
  elseif mathlist.id == "argument" then
88✔
211
    return argEnv[mathlist.index]
×
212
  else
213
    local ret = ""
88✔
214
    for _,elt in ipairs(mathlist) do
377✔
215
      if elt.id == "atom" then
289✔
216
        ret = ret .. elt[1]
289✔
217
      elseif elt.id == "command" and symbols[elt.command] then
×
218
        ret = ret .. symbols[elt.command]
×
219
      else
220
        SU.error("Encountered non-character token in command that takes a string")
×
221
      end
222
    end
223
    return ret
88✔
224
  end
225
end
226

227
local function compileToMathML_aux (_, arg_env, tree)
228
  if type(tree) == "string" then return tree end
1,147✔
229
  local function compile_and_insert (child, key, accumulator)
230
    if type(key) ~= "number" then
3,764✔
231
      accumulator[key] = child
2,543✔
232
      return accumulator
2,543✔
233
    -- Compile all children, except if this node is a macro definition (no
234
    -- evaluation "under lambda") or the application of a registered macro
235
    -- (since evaluating the nodes depends on the macro's signature, it is more
236
    -- complex and done below)..
237
    elseif tree.id == "def" or (tree.id == "command" and commands[tree.command]) then
1,221✔
238
      -- Conserve unevaluated child
239
      table.insert(accumulator, child)
253✔
240
    else
241
      -- Compile next child
242
      local comp = compileToMathML_aux(nil, arg_env, child)
968✔
243
      if comp then
968✔
244
        if comp.id == "wrapper" then
835✔
245
          -- Insert all children of the wrapper node
246
          for _, inner_child in ipairs(comp) do
154✔
247
            table.insert(accumulator, inner_child)
77✔
248
          end
249
        else
250
          table.insert(accumulator, comp)
758✔
251
        end
252
      end
253
    end
254
    return accumulator
1,221✔
255
  end
256
  tree = fold_pairs(compile_and_insert, tree)
1,862✔
257
  if tree.id == "texlike_math" then
931✔
258
    tree.command = "math"
28✔
259
    -- If the outermost `mrow` contains only other `mrow`s, remove it
260
    -- (allowing vertical stacking).
261
    if forall(function (c) return c.command == "mrow" end, tree[1]) then
77✔
262
      tree[1].command = "math"
7✔
263
      return tree[1]
7✔
264
    end
265
  elseif tree.id == "mathlist" then
903✔
266
    -- Turn mathlist into `mrow` except if it has exactly one `mtr` or `mtd`
267
    -- child.
268
    -- Note that `def`s have already been compiled away at this point.
269
    if #tree == 1 and (tree[1].command == "mtr" or tree[1].command == "mtd") then
201✔
270
      return tree[1]
×
271
    else tree.command = "mrow" end
201✔
272
    tree.command = "mrow"
201✔
273
  elseif tree.id == "atom" then
702✔
274
    local codepoints = {}
216✔
275
    for _, cp in luautf8.codes(tree[1]) do
436✔
276
      table.insert(codepoints, cp)
220✔
277
    end
278
    local cp = codepoints[1]
216✔
279
    if #codepoints == 1 and ( -- If length of UTF-8 string is 1
216✔
280
       cp >= SU.codepoint("A") and cp <= SU.codepoint("Z") or
520✔
281
       cp >= SU.codepoint("a") and cp <= SU.codepoint("z") or
502✔
282
       cp >= SU.codepoint("Α") and cp <= SU.codepoint("Ω") or
364✔
283
       cp >= SU.codepoint("α") and cp <= SU.codepoint("ω")
358✔
284
    ) then
×
285
        tree.command = "mi"
64✔
286
    elseif lpeg.match(lpeg.R("09")^1, tree[1]) then
152✔
287
      tree.command = "mn"
56✔
288
    else
289
      tree.command = "mo"
96✔
290
    end
291
    tree.options = {}
216✔
292
  -- Translate TeX-like sub/superscripts to `munderover` or `msubsup`,
293
  -- depending on whether the base is a big operator
294
  elseif tree.id == "sup" and tree[1].command == "mo"
486✔
295
      and tree[1].atom == atomType.bigOperator then
×
296
    tree.command = "mover"
×
297
  elseif tree.id == "sub" and tree[1].command == "mo"
486✔
298
      and symbolDefaults[tree[1][1]].atom == atomType.bigOperator then
4✔
299
      tree.command = "munder"
×
300
  elseif tree.id == "subsup" and tree[1].command == "mo"
486✔
301
      and symbolDefaults[tree[1][1]].atom == atomType.bigOperator then
10✔
302
    tree.command = "munderover"
8✔
303
  elseif tree.id == "supsub" and tree[1].command == "mo"
478✔
304
      and symbolDefaults[tree[1][1]].atom == atomType.bigOperator then
×
305
    tree.command = "munderover"
×
306
    local tmp = tree[2]
×
307
    tree[2] = tree[3]
×
308
    tree[3] = tmp
×
309
  elseif tree.id == "sup" then
478✔
310
    tree.command = "msup"
10✔
311
  elseif tree.id == "sub" then
468✔
312
      tree.command = "msub"
18✔
313
  elseif tree.id == "subsup" then
450✔
314
    tree.command = "msubsup"
6✔
315
  elseif tree.id == "supsub" then
444✔
316
    tree.command = "msubsup"
×
317
    local tmp = tree[2]
×
318
    tree[2] = tree[3]
×
319
    tree[3] = tmp
×
320
  elseif tree.id == "def" then
444✔
321
    local commandName = tree["command-name"]
133✔
322
    local argTypes = inferArgTypes(tree[1])
133✔
323
    registerCommand(commandName, argTypes, function (compiledArgs)
266✔
324
      return compileToMathML_aux(nil, compiledArgs, tree[1])
77✔
325
    end)
326
    return nil
133✔
327
  elseif tree.id == "command" and commands[tree.command] then
311✔
328
    local argTypes = commands[tree.command][1]
165✔
329
    local cmdFun = commands[tree.command][2]
165✔
330
    local applicationTree = tree
165✔
331
    local cmdName = tree.command
165✔
332
    if #applicationTree ~= #argTypes then
165✔
333
      SU.error("Wrong number of arguments (" .. #applicationTree ..
×
334
        ") for command " .. cmdName .. " (should be " ..
×
335
        #argTypes .. ")")
×
336
    end
337
    -- Compile every argument
338
    local compiledArgs = {}
165✔
339
    for i,arg in pairs(applicationTree) do
945✔
340
      if type(i) == "number" then
780✔
341
        if argTypes[i] == objType.tree then
120✔
342
          table.insert(compiledArgs, compileToMathML_aux(nil, arg_env, arg))
64✔
343
        else
344
          local x = compileToStr(arg_env, arg)
88✔
345
          table.insert(compiledArgs, x)
88✔
346
        end
347
      else
348
        -- Not an argument but an attribute. Add it to the compiled
349
        -- argument tree as-is
350
        compiledArgs[i] = applicationTree[i]
660✔
351
      end
352
    end
353
    local res = cmdFun(compiledArgs)
165✔
354
    if res.command == "mrow" then
165✔
355
      -- Mark the outer mrow to be unwrapped in the parent
356
      res.id = "wrapper"
77✔
357
    end
358
    return res
165✔
359
  elseif tree.id == "command" and symbols[tree.command] then
146✔
360
    local atom = {id = "atom", [1] = symbols[tree.command]}
42✔
361
    tree = compileToMathML_aux(nil, arg_env, atom)
84✔
362
  elseif tree.id == "argument" then
104✔
363
    if arg_env[tree.index] then
32✔
364
      return arg_env[tree.index]
32✔
365
    else
366
      SU.error("Argument #"..tree.index.." has escaped its scope (probably not fully applied command).")
×
367
    end
368
  end
369
  tree.id = nil
594✔
370
  return tree
594✔
371
end
372

373
local function printMathML (tree)
374
  if type(tree) == "string" then
×
375
    return tree
×
376
  end
377
  local result = "\\" .. tree.command
×
378
  if tree.options then
×
379
    local options = {}
×
380
    for k,v in pairs(tree.options) do
×
381
      table.insert(options, k .. "=" .. v)
×
382
    end
383
    if #options > 0 then
×
384
      result = result .. "[" .. table.concat(options, ", ") .. "]"
×
385
    end
386
  end
387
  if #tree > 0 then
×
388
    result = result .. "{"
×
389
    for _, child in ipairs(tree) do
×
390
      result = result .. printMathML(child)
×
391
    end
392
    result = result .. "}"
×
393
  end
394
  return result
×
395
end
396

397
local function compileToMathML (_, arg_env, tree)
398
  local result = compileToMathML_aux(_, arg_env, tree)
28✔
399
  SU.debug("texmath", function ()
56✔
400
    return "Resulting MathML: " .. printMathML(result)
×
401
  end)
402
  return result
28✔
403
end
404

405
local function convertTexlike (_, content)
406
  local ret = epnf.parsestring(mathParser, content[1])
28✔
407
  SU.debug("texmath", function ()
56✔
408
    return "Parsed TeX math: " .. pl.pretty.write(ret)
×
409
  end)
410
  return ret
28✔
411
end
412

413
registerCommand("%", {}, function ()
14✔
414
  return { "%", command = "mo", options = {} }
×
415
end)
416
registerCommand("mi", { [1] = objType.str }, function (x) return x end)
91✔
417
registerCommand("mo", { [1] = objType.str }, function (x) return x end)
11✔
418
registerCommand("mn", { [1] = objType.str }, function (x) return x end)
7✔
419

420
compileToMathML(nil, {}, convertTexlike(nil, {[==[
14✔
421
  \def{frac}{\mfrac{#1}{#2}}
422
  \def{bi}{\mi[mathvariant=bold-italic]{#1}}
423
  \def{dsi}{\mi[mathvariant=double-struck]{#1}}
424

425
  % Standard spaces gleaned from plain TeX
426
  \def{thinspace}{\mspace[width=thin]}
427
  \def{negthinspace}{\mspace[width=-thin]}
428
  \def{,}{\thinspace}
429
  \def{!}{\negthinspace}
430
  \def{medspace}{\mspace[width=med]}
431
  \def{negmedspace}{\mspace[width=-med]}
432
  \def{>}{\medspace}
433
  \def{thickspace}{\mspace[width=thick]}
434
  \def{negthickspace}{\mspace[width=-thick]}
435
  \def{;}{\thickspace}
436
  \def{enspace}{\mspace[width=1en]}
437
  \def{enskip}{\enspace}
438
  \def{quad}{\mspace[width=1em]}
439
  \def{qquad}{\mspace[width=2em]}
440

441
  % Modulus operator forms
442
  \def{bmod}{\mo{mod}}
443
  \def{pmod}{\quad(\mo{mod} #1)}
444
]==]}))
445

446
return { convertTexlike, compileToMathML }
7✔
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