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

sile-typesetter / sile / 6932773445

20 Nov 2023 04:11PM UTC coverage: 60.703% (-1.6%) from 62.266%
6932773445

Pull #1904

github

alerque
feat(utilities): Add Greek alphabetical (non-arithmetic) numbering

Useful in some context such as biblical annotations etc. where greek
characters are used orderly for numbering.
Pull Request #1904: Merge develop into master (commit to next release being breaking)

66 of 193 new or added lines in 19 files covered. (34.2%)

321 existing lines in 26 files now uncovered.

9452 of 15571 relevant lines covered (60.7%)

2104.43 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")
28✔
2

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

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

8
inputter.order = 50
28✔
9

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

26

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

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

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

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

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

136
local function getline (str, pos)
137
  local start = 1
682✔
138
  lno = 1
682✔
139
  if pos > lastpos then
682✔
140
    lno = linecache[#linecache].lno
449✔
141
    start = linecache[#linecache].pos + 1
449✔
142
    col = 1
449✔
143
  else
144
    for j = 1, #linecache-1 do
3,874✔
145
      if linecache[j+1].pos >= pos then
3,874✔
146
        lno = linecache[j].lno
233✔
147
        col = pos - linecache[j].pos
233✔
148
        return lno, col
233✔
149
      end
150
    end
151
  end
152
  for i = start, pos do
12,932✔
153
    if string.sub( str, i, i ) == "\n" then
24,966✔
154
      lno = lno + 1
338✔
155
      col = 1
338✔
156
      linecache[#linecache+1] = { pos = i, lno = lno }
338✔
157
      lastpos = i
338✔
158
    end
159
    col = col + 1
12,483✔
160
  end
161
  return lno, col
449✔
162
end
163

164
local function massage_ast (tree, doc)
165
  -- Sort out pos
166
  if type(tree) == "string" then return tree end
958✔
167
  if tree.pos then
682✔
168
    tree.lno, tree.col = getline(doc, tree.pos)
1,364✔
169
  end
170
  if tree.id == "document"
682✔
171
      or tree.id == "texlike_braced_stuff"
682✔
172
      or tree.id == "passthrough_stuff"
649✔
173
      or tree.id == "passthrough_braced_stuff"
645✔
174
      or tree.id == "passthrough_env_stuff"
641✔
175
    then
176
      return massage_ast(tree[1], doc)
55✔
177
  end
178
  if tree.id == "texlike_text"
627✔
179
    or tree.id == "passthrough_text"
465✔
180
    or tree.id == "passthrough_env_text"
465✔
181
    then
182
      return tree[1]
176✔
183
  end
184
  for key, val in ipairs(tree) do
1,335✔
185
    if val.id == "texlike_stuff" then
884✔
186
      SU.splice(tree, key, key, massage_ast(val, doc))
171✔
187
    else
188
      tree[key] = massage_ast(val, doc)
1,654✔
189
    end
190
  end
191
  return tree
451✔
192
end
193

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

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

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