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

sile-typesetter / sile / 7054606668

01 Dec 2023 01:43AM UTC coverage: 70.141% (-4.2%) from 74.329%
7054606668

push

github

web-flow
Merge 14837a0c3 into a6c229613

11050 of 15754 relevant lines covered (70.14%)

3938.65 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")
90✔
2

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

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

8
inputter.order = 50
90✔
9

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

26

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

38
function inputter:_init ()
90✔
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()
162✔
42
  base._init(self)
81✔
43
end
44

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

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

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

136
local function getline (str, pos)
137
  local start = 1
3,848✔
138
  lno = 1
3,848✔
139
  if pos > lastpos then
3,848✔
140
    lno = linecache[#linecache].lno
2,459✔
141
    start = linecache[#linecache].pos + 1
2,459✔
142
    col = 1
2,459✔
143
  else
144
    for j = 1, #linecache-1 do
20,412✔
145
      if linecache[j+1].pos >= pos then
20,412✔
146
        lno = linecache[j].lno
1,389✔
147
        col = pos - linecache[j].pos
1,389✔
148
        return lno, col
1,389✔
149
      end
150
    end
151
  end
152
  for i = start, pos do
64,775✔
153
    if string.sub( str, i, i ) == "\n" then
124,632✔
154
      lno = lno + 1
1,305✔
155
      col = 1
1,305✔
156
      linecache[#linecache+1] = { pos = i, lno = lno }
1,305✔
157
      lastpos = i
1,305✔
158
    end
159
    col = col + 1
62,316✔
160
  end
161
  return lno, col
2,459✔
162
end
163

164
local function massage_ast (tree, doc)
165
  -- Sort out pos
166
  if type(tree) == "string" then return tree end
5,365✔
167
  if tree.pos then
3,848✔
168
    tree.lno, tree.col = getline(doc, tree.pos)
7,696✔
169
  end
170
  if tree.id == "document"
3,848✔
171
      or tree.id == "texlike_braced_stuff"
3,848✔
172
      or tree.id == "passthrough_stuff"
3,635✔
173
      or tree.id == "passthrough_braced_stuff"
3,605✔
174
      or tree.id == "passthrough_env_stuff"
3,575✔
175
    then
176
      return massage_ast(tree[1], doc)
297✔
177
  end
178
  if tree.id == "texlike_text"
3,551✔
179
    or tree.id == "passthrough_text"
2,610✔
180
    or tree.id == "passthrough_env_text"
2,610✔
181
    then
182
      return tree[1]
965✔
183
  end
184
  for key, val in ipairs(tree) do
7,573✔
185
    if val.id == "texlike_stuff" then
4,987✔
186
      SU.splice(tree, key, key, massage_ast(val, doc))
990✔
187
    else
188
      tree[key] = massage_ast(val, doc)
9,314✔
189
    end
190
  end
191
  return tree
2,586✔
192
end
193

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

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

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