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

sile-typesetter / sile / 6915845768

18 Nov 2023 07:23PM UTC coverage: 63.161% (-5.6%) from 68.751%
6915845768

push

github

web-flow
Merge 0f5c09a66 into f64e235fa

9802 of 15519 relevant lines covered (63.16%)

2045.54 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")
49✔
2

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

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

8
inputter.order = 50
49✔
9

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

26

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

38
function inputter:_init ()
49✔
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()
80✔
42
  base._init(self)
40✔
43
end
44

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

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

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

136
local function getline (str, pos)
137
  local start = 1
1,483✔
138
  lno = 1
1,483✔
139
  if pos > lastpos then
1,483✔
140
    lno = linecache[#linecache].lno
994✔
141
    start = linecache[#linecache].pos + 1
994✔
142
    col = 1
994✔
143
  else
144
    for j = 1, #linecache-1 do
7,820✔
145
      if linecache[j+1].pos >= pos then
7,820✔
146
        lno = linecache[j].lno
489✔
147
        col = pos - linecache[j].pos
489✔
148
        return lno, col
489✔
149
      end
150
    end
151
  end
152
  for i = start, pos do
28,873✔
153
    if string.sub( str, i, i ) == "\n" then
55,758✔
154
      lno = lno + 1
628✔
155
      col = 1
628✔
156
      linecache[#linecache+1] = { pos = i, lno = lno }
628✔
157
      lastpos = i
628✔
158
    end
159
    col = col + 1
27,879✔
160
  end
161
  return lno, col
994✔
162
end
163

164
local function massage_ast (tree, doc)
165
  -- Sort out pos
166
  if type(tree) == "string" then return tree end
2,065✔
167
  if tree.pos then
1,483✔
168
    tree.lno, tree.col = getline(doc, tree.pos)
2,966✔
169
  end
170
  if tree.id == "document"
1,483✔
171
      or tree.id == "texlike_braced_stuff"
1,483✔
172
      or tree.id == "passthrough_stuff"
1,419✔
173
      or tree.id == "passthrough_braced_stuff"
1,398✔
174
      or tree.id == "passthrough_env_stuff"
1,377✔
175
    then
176
      return massage_ast(tree[1], doc)
126✔
177
  end
178
  if tree.id == "texlike_text"
1,357✔
179
    or tree.id == "passthrough_text"
982✔
180
    or tree.id == "passthrough_env_text"
982✔
181
    then
182
      return tree[1]
395✔
183
  end
184
  for key, val in ipairs(tree) do
2,861✔
185
    if val.id == "texlike_stuff" then
1,899✔
186
      SU.splice(tree, key, key, massage_ast(val, doc))
333✔
187
    else
188
      tree[key] = massage_ast(val, doc)
3,576✔
189
    end
190
  end
191
  return tree
962✔
192
end
193

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

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

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