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

sile-typesetter / sile / 6713098919

31 Oct 2023 10:21PM UTC coverage: 52.831% (-21.8%) from 74.636%
6713098919

push

github

web-flow
Merge d0a2a1ee9 into b185d4972

45 of 45 new or added lines in 3 files covered. (100.0%)

8173 of 15470 relevant lines covered (52.83%)

6562.28 hits per line

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

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

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

7
local atomType = syms.atomType
3✔
8
local symbolDefaults = syms.symbolDefaults
3✔
9
local symbols = syms.symbols
3✔
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
3✔
16
  local eol = S"\r\n"
3✔
17
  local digit = R("09")
3✔
18
  local natural = digit^1 / tostring
3✔
19
  local pos_natural = R("19") * digit^0 / tonumber
3✔
20
  local ctrl_word = R("AZ", "az")^1
3✔
21
  local ctrl_symbol = P(1) - S"{}\\"
3✔
22
  local ctrl_sequence_name = C(ctrl_word + ctrl_symbol) / 1
3✔
23
  local comment = (
24
      P"%" *
3✔
25
      P(1-eol)^0 *
3✔
26
      eol^-1
3✔
27
    )
28
  local utf8cont = R("\128\191")
3✔
29
  local utf8code = lpeg.R("\0\127")
3✔
30
    + lpeg.R("\194\223") * utf8cont
3✔
31
    + lpeg.R("\224\239") * utf8cont * utf8cont
3✔
32
    + lpeg.R("\240\244") * utf8cont * utf8cont * utf8cont
3✔
33
  -- Identifiers inside \mo and \mi tags
34
  local sileID = C(bits.identifier + P(1)) / 1
3✔
35
  local mathMLID = (utf8code - S"\\{}%")^1 / function (...)
3✔
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"))
6✔
44
  local element_no_infix =
45
    V"def" +
3✔
46
    V"command" +
3✔
47
    group +
3✔
48
    V"argument" +
3✔
49
    V"atom"
3✔
50
  local element =
51
    V"supsub" +
3✔
52
    V"subsup" +
3✔
53
    V"sup" +
3✔
54
    V"sub" +
3✔
55
    element_no_infix
3✔
56
  local sep = S",;" * _
3✔
57
  local quotedString = (P'"' * C((1-P'"')^1) * P'"')
3✔
58
  local value = ( quotedString + (1-S",;]")^1 )
3✔
59
  local pair = Cg(sileID * _ * "=" * _ * C(value)) * sep^-1 / function (...)
3✔
60
      local t = {...}; return t[1], t[#t]
43✔
61
    end
62
  local list = Cf(Ct"" * pair^0, rawset)
3✔
63
  local parameters = (
64
      P"[" *
3✔
65
      list *
3✔
66
      P"]"
3✔
67
    )^-1 / function (a)
3✔
68
        return type(a)=="table" and a or {}
80✔
69
      end
70

71
  local dim2_arg_inner = Ct(V"mathlist" * (P"&" * V"mathlist")^0) /
3✔
72
    function (t)
73
      t.id = "mathlist"
×
74
      return t
×
75
    end
76
  local dim2_arg =
77
    Cg(P"{" *
6✔
78
       dim2_arg_inner *
3✔
79
       (P"\\\\" * dim2_arg_inner)^1 *
3✔
80
       (P"}" + E("`}` expected"))
6✔
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"
3✔
90
  texlike_math = V"mathlist" * EOF"Unexpected character at end of math code"
6✔
91
  mathlist = (comment + (WS * _) + element)^0
3✔
92
  supsub = element_no_infix * _ * P"^" * _ * element_no_infix * _ *
3✔
93
    P"_" * _ * element_no_infix
3✔
94
  subsup = element_no_infix * _ * P"_" * _ * element_no_infix * _ *
3✔
95
    P"^" * _ * element_no_infix
3✔
96
  sup = element_no_infix * _ * P"^" * _ * element_no_infix
3✔
97
  sub = element_no_infix * _ * P"_" * _ * element_no_infix
3✔
98
  atom = natural + C(utf8code - S"\\{}%^_&") +
3✔
99
    (P"\\{" + P"\\}") / function (s) return string.sub(s, -1) end
3✔
100
  command = (
×
101
      P"\\" *
3✔
102
      Cg(ctrl_sequence_name, "command") *
3✔
103
      Cg(parameters, "options") *
3✔
104
      (dim2_arg + group^0)
3✔
105
    )
3✔
106
  def = P"\\def" * _ * P"{" *
3✔
107
    Cg(ctrl_sequence_name, "command-name") * P"}" * _ *
3✔
108
    --P"[" * Cg(digit^1, "arity") * P"]" * _ *
109
    P"{" * V"mathlist" * P"}"
3✔
110
  argument = P"#" * Cg(pos_natural, "index")
3✔
111
end
112
-- luacheck: pop
113
---@diagnostic enable: undefined-global, unused-local, lowercase-global
114

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

117
local commands = {}
3✔
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 = {
3✔
125
  tree = 1,
126
  str = 2
×
127
}
128

129
local function inferArgTypes_aux (accumulator, typeRequired, body)
130
  if type(body) == "table" then
174✔
131
    if body.id == "argument" then
174✔
132
      local ret = accumulator
15✔
133
      table.insert(ret, body.index, typeRequired)
15✔
134
      return ret
15✔
135
    elseif body.id == "command" then
159✔
136
      if commands[body.command] then
60✔
137
        local cmdArgTypes = commands[body.command][1]
30✔
138
        if #cmdArgTypes ~= #body then
30✔
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
42✔
144
            accumulator = inferArgTypes_aux(accumulator, cmdArgTypes[i], body[i])
24✔
145
          end
146
        end
147
        return accumulator
30✔
148
      elseif body.command == "mi" or body.command == "mo" or body.command == "mn" then
30✔
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
36✔
159
          accumulator = inferArgTypes_aux(accumulator, objType.tree, child)
12✔
160
        end
161
        return accumulator
30✔
162
      end
163
    elseif body.id == "atom" then
99✔
164
      return accumulator
24✔
165
    else
166
      -- Simply recurse on children
167
      for _, child in ipairs(body) do
174✔
168
        accumulator = inferArgTypes_aux(accumulator, typeRequired, child)
198✔
169
      end
170
      return accumulator
75✔
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)
57✔
177
end
178

179
local function registerCommand (name, argTypes, func)
180
  commands[name] = { argTypes, func }
69✔
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 = {}
169✔
188
  for k, v in pl.utils.kpairs(table) do
1,357✔
189
    accumulator = func(v, k, accumulator)
850✔
190
  end
191
  for i, v in ipairs(table) do
446✔
192
    accumulator = func(v, i, accumulator)
554✔
193
  end
194
  return accumulator
169✔
195
end
196

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

204
local compileToStr = function (argEnv, mathlist)
205
  if #mathlist == 1 and mathlist.id == "atom" then
10✔
206
    -- List is a single atom
207
    return mathlist[1]
×
208
  elseif #mathlist == 1 and mathlist[1].id == "argument" then
10✔
209
    return argEnv[mathlist[1].index]
×
210
  elseif mathlist.id == "argument" then
10✔
211
    return argEnv[mathlist.index]
×
212
  else
213
    local ret = ""
10✔
214
    for _,elt in ipairs(mathlist) do
24✔
215
      if elt.id == "atom" then
14✔
216
        ret = ret .. elt[1]
14✔
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
10✔
224
  end
225
end
226

227
local function compileToMathML_aux (_, arg_env, tree)
228
  if type(tree) == "string" then return tree end
227✔
229
  local function compile_and_insert (child, key, accumulator)
230
    if type(key) ~= "number" then
702✔
231
      accumulator[key] = child
425✔
232
      return accumulator
425✔
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
277✔
238
      -- Conserve unevaluated child
239
      table.insert(accumulator, child)
67✔
240
    else
241
      -- Compile next child
242
      local comp = compileToMathML_aux(nil, arg_env, child)
210✔
243
      if comp then
210✔
244
        if comp.id == "wrapper" then
153✔
245
          -- Insert all children of the wrapper node
246
          for _, inner_child in ipairs(comp) do
×
247
            table.insert(accumulator, inner_child)
×
248
          end
249
        else
250
          table.insert(accumulator, comp)
153✔
251
        end
252
      end
253
    end
254
    return accumulator
277✔
255
  end
256
  tree = fold_pairs(compile_and_insert, tree)
338✔
257
  if tree.id == "texlike_math" then
169✔
258
    tree.command = "math"
7✔
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
18✔
262
      tree[1].command = "math"
3✔
263
      return tree[1]
3✔
264
    end
265
  elseif tree.id == "mathlist" then
162✔
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
15✔
270
      return tree[1]
×
271
    else tree.command = "mrow" end
15✔
272
    tree.command = "mrow"
15✔
273
  elseif tree.id == "atom" then
147✔
274
    local codepoints = {}
58✔
275
    for _, cp in luautf8.codes(tree[1]) do
120✔
276
      table.insert(codepoints, cp)
62✔
277
    end
278
    local cp = codepoints[1]
58✔
279
    if #codepoints == 1 and ( -- If length of UTF-8 string is 1
58✔
280
       cp >= SU.codepoint("A") and cp <= SU.codepoint("Z") or
134✔
281
       cp >= SU.codepoint("a") and cp <= SU.codepoint("z") or
128✔
282
       cp >= SU.codepoint("Α") and cp <= SU.codepoint("Ω") or
92✔
283
       cp >= SU.codepoint("α") and cp <= SU.codepoint("ω")
92✔
284
    ) then
×
285
        tree.command = "mi"
16✔
286
    elseif lpeg.match(lpeg.R("09")^1, tree[1]) then
42✔
287
      tree.command = "mn"
12✔
288
    else
289
      tree.command = "mo"
30✔
290
    end
291
    tree.options = {}
58✔
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"
89✔
295
      and tree[1].atom == atomType.bigOperator then
×
296
    tree.command = "mover"
×
297
  elseif tree.id == "sub" and tree[1].command == "mo"
89✔
298
      and symbolDefaults[tree[1][1]].atom == atomType.bigOperator then
×
299
      tree.command = "munder"
×
300
  elseif tree.id == "subsup" and tree[1].command == "mo"
89✔
301
      and symbolDefaults[tree[1][1]].atom == atomType.bigOperator then
×
302
    tree.command = "munderover"
×
303
  elseif tree.id == "supsub" and tree[1].command == "mo"
89✔
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
89✔
310
    tree.command = "msup"
6✔
311
  elseif tree.id == "sub" then
83✔
312
      tree.command = "msub"
2✔
313
  elseif tree.id == "subsup" then
81✔
314
    tree.command = "msubsup"
4✔
315
  elseif tree.id == "supsub" then
77✔
316
    tree.command = "msubsup"
×
317
    local tmp = tree[2]
×
318
    tree[2] = tree[3]
×
319
    tree[3] = tmp
×
320
  elseif tree.id == "def" then
77✔
321
    local commandName = tree["command-name"]
57✔
322
    local argTypes = inferArgTypes(tree[1])
57✔
323
    registerCommand(commandName, argTypes, function (compiledArgs)
114✔
324
      return compileToMathML_aux(nil, compiledArgs, tree[1])
×
325
    end)
326
    return nil
57✔
327
  elseif tree.id == "command" and commands[tree.command] then
20✔
328
    local argTypes = commands[tree.command][1]
10✔
329
    local cmdFun = commands[tree.command][2]
10✔
330
    local applicationTree = tree
10✔
331
    local cmdName = tree.command
10✔
332
    if #applicationTree ~= #argTypes then
10✔
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 = {}
10✔
339
    for i,arg in pairs(applicationTree) do
60✔
340
      if type(i) == "number" then
50✔
341
        if argTypes[i] == objType.tree then
10✔
342
          table.insert(compiledArgs, compileToMathML_aux(nil, arg_env, arg))
×
343
        else
344
          local x = compileToStr(arg_env, arg)
10✔
345
          table.insert(compiledArgs, x)
10✔
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]
40✔
351
      end
352
    end
353
    local res = cmdFun(compiledArgs)
10✔
354
    if res.command == "mrow" then
10✔
355
      -- Mark the outer mrow to be unwrapped in the parent
356
      res.id = "wrapper"
×
357
    end
358
    return res
10✔
359
  elseif tree.id == "command" and symbols[tree.command] then
10✔
360
    local atom = {id = "atom", [1] = symbols[tree.command]}
10✔
361
    tree = compileToMathML_aux(nil, arg_env, atom)
20✔
362
  elseif tree.id == "argument" then
×
363
    if arg_env[tree.index] then
×
364
      return arg_env[tree.index]
×
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
99✔
370
  return tree
99✔
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)
7✔
399
  SU.debug("texmath", function ()
14✔
400
    return "Resulting MathML: " .. printMathML(result)
×
401
  end)
402
  return result
7✔
403
end
404

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

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

420
compileToMathML(nil, {}, convertTexlike(nil, {[==[
6✔
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 }
3✔
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

© 2025 Coveralls, Inc