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

sile-typesetter / sile / 8736205729

18 Apr 2024 10:16AM UTC coverage: 63.747% (-11.0%) from 74.718%
8736205729

push

github

alerque
chore(classes): Correct typos in user facing deprecation message

10118 of 15872 relevant lines covered (63.75%)

6751.63 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")
180✔
2

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

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

8
inputter.order = 50
180✔
9

10
inputter.appropriate = function (round, filename, doc)
11
  if round == 1 then
177✔
12
    return filename:match(".sil$")
174✔
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
180✔
25

26

27
inputter.passthroughCommands = {
180✔
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
}
180✔
37

38
function inputter:_init ()
180✔
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()
346✔
42
  base._init(self)
173✔
43
end
44

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

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

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

136
local function getline (str, pos)
137
  local start = 1
1,516✔
138
  lno = 1
1,516✔
139
  if pos > lastpos then
1,516✔
140
    lno = linecache[#linecache].lno
1,016✔
141
    start = linecache[#linecache].pos + 1
1,016✔
142
    col = 1
1,016✔
143
  else
144
    for j = 1, #linecache-1 do
7,967✔
145
      if linecache[j+1].pos >= pos then
7,967✔
146
        lno = linecache[j].lno
500✔
147
        col = pos - linecache[j].pos
500✔
148
        return lno, col
500✔
149
      end
150
    end
151
  end
152
  for i = start, pos do
29,997✔
153
    if string.sub( str, i, i ) == "\n" then
57,962✔
154
      lno = lno + 1
660✔
155
      col = 1
660✔
156
      linecache[#linecache+1] = { pos = i, lno = lno }
660✔
157
      lastpos = i
660✔
158
    end
159
    col = col + 1
28,981✔
160
  end
161
  return lno, col
1,016✔
162
end
163

164
local function massage_ast (tree, doc)
165
  -- Sort out pos
166
  if type(tree) == "string" then return tree end
2,118✔
167
  if tree.pos then
1,516✔
168
    tree.lno, tree.col = getline(doc, tree.pos)
3,032✔
169
  end
170
  if tree.id == "document"
1,516✔
171
      or tree.id == "texlike_braced_stuff"
1,516✔
172
      or tree.id == "passthrough_stuff"
1,452✔
173
      or tree.id == "passthrough_braced_stuff"
1,431✔
174
      or tree.id == "passthrough_env_stuff"
1,410✔
175
    then
176
      return massage_ast(tree[1], doc)
127✔
177
  end
178
  if tree.id == "texlike_text"
1,389✔
179
    or tree.id == "passthrough_text"
1,004✔
180
    or tree.id == "passthrough_env_text"
1,004✔
181
    then
182
      return tree[1]
406✔
183
  end
184
  for key, val in ipairs(tree) do
2,933✔
185
    if val.id == "texlike_stuff" then
1,950✔
186
      SU.splice(tree, key, key, massage_ast(val, doc))
336✔
187
    else
188
      tree[key] = massage_ast(val, doc)
3,676✔
189
    end
190
  end
191
  return tree
983✔
192
end
193

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

198
function inputter:parse (doc)
50✔
199
  local parsed = epnf.parsestring(self._parser, doc)[1]
82✔
200
  if not parsed then
41✔
201
    return SU.error("Unable to parse input document to an AST tree")
×
202
  end
203
  resetCache()
41✔
204
  local top = massage_ast(parsed, doc)
41✔
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
41✔
211
    if leaf.command and (leaf.command == "document" or leaf.command == "sile") then
41✔
212
        tree = leaf
41✔
213
        break
41✔
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
41✔
219
    tree = { top, command = "document" }
×
220
  end
221
  return { tree }
41✔
222
end
223

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