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

sile-typesetter / sile / 7092428209

04 Dec 2023 08:54PM UTC coverage: 63.585% (-10.7%) from 74.329%
7092428209

push

github

web-flow
chore(deps): Bump @commitlint/cli from 17.8.1 to 18.4.3 (#1917)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

9981 of 15697 relevant lines covered (63.59%)

4235.82 hits per line

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

95.14
/inputters/sil.lua
1
local base = require("inputters.base")
45✔
2

3
local epnf = require("epnf")
45✔
4

5
local inputter = pl.class(base)
45✔
6
inputter._name = "sil"
45✔
7

8
inputter.order = 50
45✔
9

10
inputter.appropriate = function (round, filename, doc)
11
  if round == 1 then
41✔
12
    return filename:match(".sil$")
38✔
13
  elseif round == 2 then
3✔
14
    local sniff = doc:sub(1, 100)
2✔
15
    local promising = sniff:match("\\begin") or sniff:match("\\document") or sniff:match("\\sile")
2✔
16
    return promising and inputter.appropriate(3, filename, doc) or false
3✔
17
  elseif round == 3 then
1✔
18
    local _parser = epnf.define(inputter._grammar)
1✔
19
    local status, _ = pcall(epnf.parsestring, _parser, doc)
1✔
20
    return status
1✔
21
  end
22
end
23

24
local bits = SILE.parserBits
45✔
25

26

27
inputter.passthroughCommands = {
45✔
28
  ftl = true,
29
  lua = true,
30
  math = true,
31
  raw = true,
32
  script = true,
33
  sil = true,
34
  use = true,
35
  xml = true
×
36
}
45✔
37

38
function inputter:_init ()
45✔
39
  -- Save time when parsing strings by only setting up the grammar once per
40
  -- instantiation then re-using it on every use.
41
  self._parser = self:rebuildParser()
72✔
42
  base._init(self)
36✔
43
end
44

45
-- luacheck: push ignore
46
---@diagnostic disable: undefined-global, unused-local, lowercase-global
47
function inputter._grammar (_ENV)
45✔
48
  local isPassthrough = function (_, _, command)
49
    return inputter.passthroughCommands[command] or false
854✔
50
  end
51
  local isNotPassthrough = function (...)
52
    return not isPassthrough(...)
814✔
53
  end
54
  local isMatchingEndEnv = function (a, b, thisCommand, lastCommand)
55
    return thisCommand == lastCommand
82✔
56
  end
57
  local _ = WS^0
37✔
58
  local eol = S"\r\n"
37✔
59
  local specials = S"{}%\\"
37✔
60
  local escaped_specials = P"\\" * specials
37✔
61
  local unescapeSpecials = function (str)
62
    return str:gsub('\\([{}%%\\])', '%1')
364✔
63
  end
64
  local myID = C(bits.silidentifier) / 1
37✔
65
  local cmdID = myID - P"beign" - P"end"
37✔
66
  local wrapper = function (a) return type(a)=="table" and a or {} end
404✔
67
  local parameters = (P"[" * bits.parameters * P"]")^-1 / wrapper
37✔
68
  local comment = (
69
      P"%" *
37✔
70
      P(1-eol)^0 *
37✔
71
      eol^-1
37✔
72
    ) / ""
37✔
73

74
  START "document"
37✔
75
  document = V"texlike_stuff" * EOF"Unexpected character at end of input"
74✔
76
  texlike_stuff = Cg(
74✔
77
      V"environment" +
37✔
78
      comment +
37✔
79
      V"texlike_text" +
37✔
80
      V"texlike_braced_stuff" +
37✔
81
      V"texlike_command"
37✔
82
    )^0
37✔
83
  passthrough_stuff = C(Cg(
111✔
84
      V"passthrough_text" +
37✔
85
      V"passthrough_debraced_stuff"
37✔
86
    )^0)
74✔
87
  passthrough_env_stuff = Cg(
74✔
88
      V"passthrough_env_text"
37✔
89
    )^0
37✔
90
  texlike_text = C((1 - specials + escaped_specials)^1) / unescapeSpecials
37✔
91
  passthrough_text = C((1-S("{}"))^1)
37✔
92
  passthrough_env_text = C((1 - (P"\\end{" * Cmt(cmdID * Cb"command", isMatchingEndEnv) * P"}"))^1)
37✔
93
  texlike_braced_stuff = P"{" * V"texlike_stuff" * ( P"}" + E("} expected") )
74✔
94
  passthrough_braced_stuff = P"{" * V"passthrough_stuff" * ( P"}" + E("} expected") )
74✔
95
  passthrough_debraced_stuff = C(V"passthrough_braced_stuff")
37✔
96
  texlike_command = (
×
97
      P"\\" *
37✔
98
      Cg(cmdID, "command") *
37✔
99
      Cg(parameters, "options") *
37✔
100
      (
×
101
        (Cmt(Cb"command", isPassthrough) * V"passthrough_braced_stuff") +
37✔
102
        (Cmt(Cb"command", isNotPassthrough) * V"texlike_braced_stuff")
37✔
103
      )^0
37✔
104
    )
37✔
105
  local notpass_end =
106
      P"\\end{" *
37✔
107
      ( Cmt(cmdID * Cb"command", isMatchingEndEnv) + E"Environment mismatch") *
74✔
108
      ( P"}" * _ ) + E"Environment begun but never ended"
74✔
109
  local pass_end =
110
      P"\\end{" *
37✔
111
      ( cmdID * Cb"command" ) *
37✔
112
      ( P"}" * _ ) + E"Environment begun but never ended"
74✔
113
  environment =
×
114
    P"\\begin" *
37✔
115
    Cg(parameters, "options") *
37✔
116
    P"{" *
37✔
117
    Cg(cmdID, "command") *
37✔
118
    P"}" *
37✔
119
    (
×
120
      (Cmt(Cb"command", isPassthrough) * V"passthrough_env_stuff" * pass_end) +
37✔
121
      (Cmt(Cb"command", isNotPassthrough) * V"texlike_stuff" * notpass_end)
37✔
122
    )
37✔
123
end
124
-- luacheck: pop
125
---@diagnostic enable: undefined-global, unused-local, lowercase-global
126

127
local linecache = {}
45✔
128
local lno, col, lastpos
129
local function resetCache ()
130
  lno = 1
36✔
131
  col = 1
36✔
132
  lastpos = 0
36✔
133
  linecache = { { lno = 1, pos = 1} }
36✔
134
end
135

136
local function getline (str, pos)
137
  local start = 1
1,440✔
138
  lno = 1
1,440✔
139
  if pos > lastpos then
1,440✔
140
    lno = linecache[#linecache].lno
963✔
141
    start = linecache[#linecache].pos + 1
963✔
142
    col = 1
963✔
143
  else
144
    for j = 1, #linecache-1 do
7,794✔
145
      if linecache[j+1].pos >= pos then
7,794✔
146
        lno = linecache[j].lno
477✔
147
        col = pos - linecache[j].pos
477✔
148
        return lno, col
477✔
149
      end
150
    end
151
  end
152
  for i = start, pos do
28,217✔
153
    if string.sub( str, i, i ) == "\n" then
54,508✔
154
      lno = lno + 1
606✔
155
      col = 1
606✔
156
      linecache[#linecache+1] = { pos = i, lno = lno }
606✔
157
      lastpos = i
606✔
158
    end
159
    col = col + 1
27,254✔
160
  end
161
  return lno, col
963✔
162
end
163

164
local function massage_ast (tree, doc)
165
  -- Sort out pos
166
  if type(tree) == "string" then return tree end
2,009✔
167
  if tree.pos then
1,440✔
168
    tree.lno, tree.col = getline(doc, tree.pos)
2,880✔
169
  end
170
  if tree.id == "document"
1,440✔
171
      or tree.id == "texlike_braced_stuff"
1,440✔
172
      or tree.id == "passthrough_stuff"
1,377✔
173
      or tree.id == "passthrough_braced_stuff"
1,356✔
174
      or tree.id == "passthrough_env_stuff"
1,335✔
175
    then
176
      return massage_ast(tree[1], doc)
124✔
177
  end
178
  if tree.id == "texlike_text"
1,316✔
179
    or tree.id == "passthrough_text"
953✔
180
    or tree.id == "passthrough_env_text"
953✔
181
    then
182
      return tree[1]
382✔
183
  end
184
  for key, val in ipairs(tree) do
2,783✔
185
    if val.id == "texlike_stuff" then
1,849✔
186
      SU.splice(tree, key, key, massage_ast(val, doc))
318✔
187
    else
188
      tree[key] = massage_ast(val, doc)
3,486✔
189
    end
190
  end
191
  return tree
934✔
192
end
193

194
function inputter:rebuildParser ()
45✔
195
  return epnf.define(self._grammar)
36✔
196
end
197

198
function inputter:parse (doc)
45✔
199
  local parsed = epnf.parsestring(self._parser, doc)[1]
72✔
200
  if not parsed then
36✔
201
    return SU.error("Unable to parse input document to an AST tree")
×
202
  end
203
  resetCache()
36✔
204
  local top = massage_ast(parsed, doc)
36✔
205
  local tree
206
  -- Content not part of a tagged command could either be part of a document
207
  -- fragment or junk (e.g. comments, whitespace) outside of a document tag. We
208
  -- need to either capture the document tag only or decide this is a fragment
209
  -- and wrap it in a document tag.
210
  for _, leaf in ipairs(top) do
36✔
211
    if leaf.command and (leaf.command == "document" or leaf.command == "sile") then
36✔
212
        tree = leaf
36✔
213
        break
36✔
214
    end
215
  end
216
  -- In the event we didn't isolate a top level document tag above, assume this
217
  -- is a fragment and wrap it in one.
218
  if not tree then
36✔
219
    tree = { top, command = "document" }
×
220
  end
221
  return { tree }
36✔
222
end
223

224
return inputter
45✔
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