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

sile-typesetter / sile / 11135083927

01 Oct 2024 11:26PM UTC coverage: 62.071% (-7.3%) from 69.333%
11135083927

push

github

alerque
style: Reformat Lua with stylua

0 of 9 new or added lines in 2 files covered. (0.0%)

1678 existing lines in 57 files now uncovered.

11071 of 17836 relevant lines covered (62.07%)

5220.48 hits per line

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

92.86
/inputters/sil.lua
1
local base = require("inputters.base")
188✔
2

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

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

12
inputter.order = 50
188✔
13

14
inputter.appropriate = function (round, filename, doc)
15
   if not parser then
185✔
16
      load_parser()
181✔
17
   end
18
   if round == 1 then
185✔
19
      return filename:match(".sil$")
182✔
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)
188✔
31
   options = options or {}
181✔
32
   if options.variant then
181✔
33
      _variant = options.variant
×
34
      load_parser()
×
35
   else
36
      if not parser then
181✔
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
181✔
43
   base._init(self)
181✔
44
end
45

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

55
local function getline (str, pos)
56
   local start = 1
5,822✔
57
   lno = 1
5,822✔
58
   if pos > lastpos then
5,822✔
59
      lno = linecache[#linecache].lno
5,508✔
60
      start = linecache[#linecache].pos + 1
5,508✔
61
      col = 1
5,508✔
62
   else
63
      for j = 1, #linecache - 1 do
2,416✔
64
         if linecache[j + 1].pos >= pos then
2,416✔
65
            lno = linecache[j].lno
314✔
66
            col = pos - linecache[j].pos
314✔
67
            return lno, col
314✔
68
         end
69
      end
70
   end
71
   for i = start, pos do
161,781✔
72
      if string.sub(str, i, i) == "\n" then
312,546✔
73
         lno = lno + 1
3,285✔
74
         col = 1
3,285✔
75
         linecache[#linecache + 1] = { pos = i, lno = lno }
3,285✔
76
         lastpos = i
3,285✔
77
      end
78
      col = col + 1
156,273✔
79
   end
80
   return lno, col
5,508✔
81
end
82

83
local function massage_ast (tree, doc)
84
   if type(tree) == "string" then
8,230✔
85
      return tree
46✔
86
   end
87
   if tree.pos then
8,184✔
88
      tree.lno, tree.col = getline(doc, tree.pos)
11,644✔
89
      tree.pos = nil
5,822✔
90
   end
91
   SU.debug("inputter", "Processing ID:", tree.id)
8,184✔
92
   if false or tree.id == "comment" then
8,184✔
93
      SU.debug("inputter", "Discarding comment:", pl.stringx.strip(tree[1]))
384✔
94
      return {}
192✔
95
   elseif
×
96
      false
97
      or tree.id == "document"
7,992✔
98
      or tree.id == "braced_content"
7,992✔
99
      or tree.id == "passthrough_content"
7,587✔
100
      or tree.id == "braced_passthrough_content"
7,541✔
101
      or tree.id == "env_passthrough_content"
7,541✔
102
   then
103
      SU.debug("inputter", "Re-massage subtree", tree.id)
490✔
104
      return massage_ast(tree[1], doc)
490✔
105
   elseif
×
106
      false
107
      or tree.id == "text"
7,502✔
108
      or tree.id == "passthrough_text"
5,163✔
109
      or tree.id == "braced_passthrough_text"
5,163✔
110
      or tree.id == "env_passthrough_text"
5,117✔
111
   then
112
      SU.debug("inputter", "  - Collapse subtree")
2,424✔
113
      return tree[1]
2,424✔
114
   elseif false or tree.id == "content" or tree.id == "environment" or tree.id == "command" then
5,078✔
115
      SU.debug("inputter", "  - Massage in place", tree.id)
5,078✔
116
      for key, val in ipairs(tree) do
15,356✔
117
         SU.debug("inputter", "    -", val.id)
10,278✔
118
         if val.id == "content" then
10,278✔
119
            SU.splice(tree, key, key, massage_ast(val, doc))
1,875✔
120
         elseif val.id then -- requiring an id discards nodes with no content such as comments
9,653✔
121
            tree[key] = massage_ast(val, doc)
13,868✔
122
         end
123
      end
124
      return tree
5,078✔
125
   end
126
end
127

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

134
         Parser error:
135

136
           %s
137

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

162
return inputter
188✔
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