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

sile-typesetter / sile / 9446096271

10 Jun 2024 09:39AM UTC coverage: 62.76% (-0.3%) from 63.078%
9446096271

push

github

alerque
chore(tooling): Deconflict release tooling config keys per fork

10818 of 17237 relevant lines covered (62.76%)

4590.53 hits per line

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

91.84
/inputters/sil.lua
1
local base = require("inputters.base")
144✔
2

3
local _variant = "epnf"
144✔
4
local parser
5
local function load_parser ()
6
   parser = require("inputters.sil-" .. _variant)
137✔
7
end
8

9
local inputter = pl.class(base)
144✔
10
inputter._name = "sil"
144✔
11

12
inputter.order = 50
144✔
13

14
inputter.appropriate = function (round, filename, doc)
15
   if not parser then
141✔
16
      load_parser()
137✔
17
   end
18
   if round == 1 then
141✔
19
      return filename:match(".sil$")
138✔
20
   elseif round == 2 then
3✔
21
      local sniff = doc:sub(1, 100)
2✔
22
      local promising = sniff:match("\\begin") or sniff:match("\\document") or sniff:match("\\sile")
2✔
23
      return promising and inputter.appropriate(3, filename, doc) or false
3✔
24
   elseif round == 3 then
1✔
25
      local status, _ = pcall(parser, doc)
1✔
26
      return status
1✔
27
   end
28
end
29

30
function inputter:_init (options)
144✔
31
   options = options or {}
136✔
32
   if options.variant then
136✔
33
      _variant = options.variant
×
34
      load_parser()
×
35
   else
36
      if not parser then
136✔
37
         load_parser()
×
38
      end
39
   end
40
   -- Save time when parsing strings by only setting up the grammar once per
41
   -- instantiation then re-using it on every use.
42
   self._parser = parser
136✔
43
   base._init(self)
136✔
44
end
45

46
local linecache = {}
144✔
47
local lno, col, lastpos
48
local function resetCache ()
49
   lno = 1
136✔
50
   col = 1
136✔
51
   lastpos = 0
136✔
52
   linecache = { { lno = 1, pos = 1 } }
136✔
53
end
54

55
local function getline (str, pos)
56
   local start = 1
4,637✔
57
   lno = 1
4,637✔
58
   if pos > lastpos then
4,637✔
59
      lno = linecache[#linecache].lno
4,387✔
60
      start = linecache[#linecache].pos + 1
4,387✔
61
      col = 1
4,387✔
62
   else
63
      for j = 1, #linecache - 1 do
2,317✔
64
         if linecache[j + 1].pos >= pos then
2,317✔
65
            lno = linecache[j].lno
250✔
66
            col = pos - linecache[j].pos
250✔
67
            return lno, col
250✔
68
         end
69
      end
70
   end
71
   for i = start, pos do
126,026✔
72
      if string.sub(str, i, i) == "\n" then
243,278✔
73
         lno = lno + 1
2,571✔
74
         col = 1
2,571✔
75
         linecache[#linecache + 1] = { pos = i, lno = lno }
2,571✔
76
         lastpos = i
2,571✔
77
      end
78
      col = col + 1
121,639✔
79
   end
80
   return lno, col
4,387✔
81
end
82

83
local function massage_ast (tree, doc)
84
   if type(tree) == "string" then
6,635✔
85
      return tree
34✔
86
   end
87
   if tree.pos then
6,601✔
88
      tree.lno, tree.col = getline(doc, tree.pos)
9,274✔
89
      tree.pos = nil
4,637✔
90
   end
91
   SU.debug("inputter", "Processing ID:", tree.id)
6,601✔
92
   if false or tree.id == "comment" then
6,601✔
93
      SU.debug("inputter", "Discarding comment:", pl.stringx.strip(tree[1]))
308✔
94
      return {}
154✔
95
   elseif
×
96
      false
97
      or tree.id == "document"
6,447✔
98
      or tree.id == "braced_content"
6,447✔
99
      or tree.id == "passthrough_content"
6,100✔
100
      or tree.id == "braced_passthrough_content"
6,066✔
101
      or tree.id == "env_passthrough_content"
6,066✔
102
   then
103
      SU.debug("inputter", "Re-massage subtree", tree.id)
412✔
104
      return massage_ast(tree[1], doc)
412✔
105
   elseif
×
106
      false
107
      or tree.id == "text"
6,035✔
108
      or tree.id == "passthrough_text"
4,193✔
109
      or tree.id == "braced_passthrough_text"
4,193✔
110
      or tree.id == "env_passthrough_text"
4,159✔
111
   then
112
      SU.debug("inputter", "  - Collapse subtree")
1,907✔
113
      return tree[1]
1,907✔
114
   elseif false or tree.id == "content" or tree.id == "environment" or tree.id == "command" then
4,128✔
115
      SU.debug("inputter", "  - Massage in place", tree.id)
4,128✔
116
      for key, val in ipairs(tree) do
12,488✔
117
         SU.debug("inputter", "    -", val.id)
8,360✔
118
         if val.id == "content" then
8,360✔
119
            SU.splice(tree, key, key, massage_ast(val, doc))
1,578✔
120
         elseif val.id then -- requiring an id discards nodes with no content such as comments
7,834✔
121
            tree[key] = massage_ast(val, doc)
11,122✔
122
         end
123
      end
124
      return tree
4,128✔
125
   end
126
end
127

128
function inputter:parse (doc)
144✔
129
   local status, result = pcall(self._parser, doc)
136✔
130
   if not status then
136✔
131
      return SU.error(([[Unable to parse input document to an AST tree. Parser error:
×
132

133
%s  thrown from document beginning]]):format(pl.stringx.indent(result, 6)))
×
134
   end
135
   resetCache()
136✔
136
   local top = massage_ast(result[1], doc)
136✔
137
   local tree
138
   -- Content not part of a tagged command could either be part of a document
139
   -- fragment or junk (e.g. comments, whitespace) outside of a document tag. We
140
   -- need to either capture the document tag only or decide this is a fragment
141
   -- and wrap it in a document tag.
142
   for _, leaf in ipairs(top) do
139✔
143
      if leaf.command and (leaf.command == "document" or leaf.command == "sile") then
139✔
144
         tree = leaf
136✔
145
         break
136✔
146
      end
147
   end
148
   -- In the event we didn't isolate a top level document tag above, assume this
149
   -- is a fragment and wrap it in one.
150
   if not tree then
136✔
151
      tree = { top, command = "document" }
×
152
   end
153
   -- SU.dump(tree)
154
   return { tree }
136✔
155
end
156

157
return inputter
144✔
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