-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiagnostics_util.lua
324 lines (247 loc) · 8.82 KB
/
diagnostics_util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
local string = require("string")
local table = require("table")
local format = string.format
local assert = assert
local ipairs = ipairs
local type = type
local unpack = unpack
local class = require("class").class
local error_util = require("error_util")
local check = error_util.check
local checktype = error_util.checktype
local Col = require("terminal_colors")
local encode_color = Col.encode
----------
local api = {}
local function GetColorizeTripleFunc(severityTextColor, colorizeMainText)
return function(pre, tag, post)
return
encode_color(pre, Col.Bold..Col.White)..
encode_color(tag, Col.Bold..severityTextColor)..
(colorizeMainText and encode_color(post, Col.Bold..Col.White) or post)
end
end
local ColorizeErrorFunc = GetColorizeTripleFunc(Col.Red, true)
local ColorizeWarningFunc = GetColorizeTripleFunc(Col.Purple, true)
local ColorizeNoteFunc = GetColorizeTripleFunc(Col.Black, false)
local ColorSubstitutions = {
{ "(.*)(fatal error: )(.*)", ColorizeErrorFunc },
{ "(.*)(error: )(.*)", ColorizeErrorFunc },
{ "(.*)(warning: )(.*)", ColorizeWarningFunc },
{ "(.*)(note: )(.*)", ColorizeNoteFunc },
}
-----===== Diagnostic formatting =====-----
local function getIndented(indentation, str)
return format("%s%s", string.rep(" ", indentation), str)
end
local FormattedDiag = class
{
function(useColors, severity)
checktype(useColors, 1, "boolean", 2)
checktype(severity, 2, "string", 2)
-- self: sequence table of lines constituting the diagnostic
return {
usingColors = useColors,
severity = severity,
}
end,
addIndentedLine = function(self, indentation, str)
self[#self + 1] = getIndented(indentation, str)
end,
getSeverity = function(self)
return self.severity
end,
getString = function(self, keepColorsIfPresent)
checktype(keepColorsIfPresent, 1, "boolean", 2)
local str = table.concat(self, '\n')
return
(not self.usingColors) and str or
keepColorsIfPresent and Col.colorize(str) or
Col.strip(str)
end,
}
-- Special separation characters that are disjoint with our encoding of color codes.
-- (See terminal_colors.lua)
local Sep = {
Diag = {
Value = '\0',
Pattern = '%z',
},
-- Octet values that cannot be part of a well-formed UTF-8-encoded string.
-- (See ISO/IEC 10646:2017 section 9.2)
Line = '\xFE',
EmptyInfo = '\xFD',
}
local SpecialCharsPattern = "[%z\xFE\xFD]"
local DiagInfoSeverity = "ignored"
local function patternFor(sep)
return "([^"..sep.."]+)"..sep
end
local function FormattedDiagSet_Serialize(self)
local tab = {}
for _, diag in ipairs(self.diags) do
local innerTab = { diag.severity }
for _, line in ipairs(diag) do
assert(not line:find(SpecialCharsPattern))
innerTab[#innerTab + 1] = line
end
tab[#tab + 1] = table.concat(innerTab, Sep.Line)..Sep.Line
end
local L = Sep.Line
local info = self.info ~= nil and self.info[1] or Sep.EmptyInfo
if (self.info ~= nil) then
assert(#self.info == 1)
assert(not self.info[1]:find(SpecialCharsPattern))
end
tab[#tab + 1] = DiagInfoSeverity..Sep.Line..info..Sep.Line
return table.concat(tab, Sep.Diag.Value)..Sep.Diag.Value
end
local FormattedDiagSet -- "forward-declare"
local InvalidStringMsg = "passed string that is not a formatted diagnostic serialization"
function api.FormattedDiagSet_Deserialize(diagsStr, useColors)
checktype(diagsStr, 1, "string", 2)
checktype(useColors, 2, "boolean", 2)
local fDiagSet = FormattedDiagSet(useColors)
for diagStr in diagsStr:gmatch(patternFor(Sep.Diag.Pattern)) do
local fDiag
for line in diagStr:gmatch(patternFor(Sep.Line)) do
if (fDiag == nil) then
fDiag = FormattedDiag(useColors, line)
else
fDiag[#fDiag + 1] = line
end
end
assert(fDiag ~= nil)
fDiagSet.diags[#fDiagSet.diags + 1] = fDiag
end
local lastDiag = fDiagSet.diags[#fDiagSet.diags]
fDiagSet.info = (lastDiag[1] ~= Sep.EmptyInfo) and lastDiag or nil
fDiagSet.diags[#fDiagSet.diags] = nil
if (fDiagSet.info ~= nil) then
local good = (#fDiagSet.info == 1 and not fDiagSet.info[1]:find(Sep.EmptyInfo))
check(good, InvalidStringMsg..", or INTERNAL ERROR", 2)
else
-- TODO: also have a check?
end
return fDiagSet
end
FormattedDiagSet = class
{
function(useColors)
return {
diags = {}, -- list of FormattedDiag objects
info = nil, -- nil or a FormattedDiag with one line
usingColors = useColors,
}
end,
isEmpty = function(self)
return (#self:getDiags() == 0 and self:getInfo() == nil)
end,
getDiags = function(self)
return self.diags
end,
getInfo = function(self)
return self.info
end,
newDiag = function(self, severity)
return FormattedDiag(self.usingColors, severity)
end,
appendDiag = function(self, fDiag)
self.diags[#self.diags + 1] = fDiag
end,
setInfo = function(self, info)
checktype(info, 1, "string", 2)
self.info = self:newDiag(DiagInfoSeverity)
self.info:addIndentedLine(0, info)
end,
getString = function(self, keepColorsIfPresent)
checktype(keepColorsIfPresent, 1, "boolean", 2)
local fDiags = {}
for _, fDiag in ipairs(self.diags) do
fDiags[#fDiags + 1] = fDiag:getString(keepColorsIfPresent)
end
return table.concat(fDiags, '\n\n') ..
(self.info ~= nil and '\n'..self.info:getString(keepColorsIfPresent) or "")
end,
serialize = FormattedDiagSet_Serialize,
}
local function FormatDiagnostic(diag, useColors, indentation,
--[[out--]] fDiag)
local text = diag:format()
local printCategory = (indentation == 0)
if (useColors) then
for i = 1, #ColorSubstitutions do
local matchCount
local subst = ColorSubstitutions[i]
text, matchCount = text:gsub(subst[1], subst[2])
if (matchCount > 0) then
break
end
end
end
local category = diag:category()
local textWithMaybeCategory =
text .. ((printCategory and #category > 0) and " ["..category.."]" or "")
fDiag:addIndentedLine(indentation, textWithMaybeCategory)
end
-----
local function PrintPrefixDiagnostics(diags, indentation,
--[[out--]] fDiag)
for i = 1, #diags do
local text = diags[i]:spelling()
if (text:match("^in file included from ")) then
fDiag:addIndentedLine(indentation, "In"..text:sub(3))
else
return i
end
end
return #diags + 1
end
local function PrintDiagsImpl(diags, useColors, allDiags,
startIndex, indentation, currentFDiag)
if (startIndex == nil) then
startIndex = 1
end
if (indentation == nil) then
indentation = 0
end
local formattedDiags = FormattedDiagSet(useColors)
for i = startIndex, #diags do
local diag = diags[i]
local fDiag = (currentFDiag ~= nil) and
currentFDiag or
formattedDiags:newDiag(diag:severity())
local childDiags = diag:childDiagnostics()
local innerStartIndex = PrintPrefixDiagnostics(childDiags, indentation, fDiag)
FormatDiagnostic(diag, useColors, indentation, fDiag)
-- Recurse. We expect only at most two levels in total (but do not check for that).
PrintDiagsImpl(diag:childDiagnostics(), useColors, allDiags,
innerStartIndex, indentation + 2, fDiag)
local isFatal = (diag:severity() == "fatal")
local isError = isFatal or diag:severity() == "error"
local omitFollowing = not allDiags and (isFatal or (isError and diag:category() == "Parse Issue"))
if (omitFollowing) then
assert(indentation == 0)
if (i < #diags) then
local info = format("%s: omitting %d following diagnostics.",
useColors and encode_color("NOTE", Col.Bold..Col.Blue) or "NOTE",
#diags - i)
formattedDiags:setInfo(info)
end
end
formattedDiags:appendDiag(fDiag)
if (omitFollowing) then
break
end
end
return formattedDiags
end
api.FormattedDiagSet = FormattedDiagSet
function api.GetDiags(diags, useColors, allDiags)
checktype(diags, 1, "table", 2)
checktype(useColors, 2, "boolean", 2)
checktype(allDiags, 3, "boolean", 2)
return PrintDiagsImpl(diags, useColors, allDiags)
end
-- Done!
return api