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
|
const std = @import("std");
usingnamespace @import("strings.zig");
const fs = @import("fs.zig");
const unicode = std.unicode;
const expect = std.testing.expect;
const assert = std.debug.assert;
const ArrayList = std.ArrayList;
pub const Kind = enum {
err,
warn,
note,
debug,
pub fn string(self: Kind) string {
return switch (self) {
.err => "error",
.warn => "warn",
.note => "note",
.debug => "debug",
};
}
};
pub const Loc = struct {
start: i32 = -1,
pub const Empty = Loc{ .start = -1 };
pub fn eql(loc: *Loc, other: Loc) bool {
return loc.start == other.start;
}
};
pub const Location = struct {
file: string,
namespace: string = "file",
line: i32 = 1, // 1-based
column: i32 = 0, // 0-based, in bytes
length: usize = 0, // in bytes
line_text: ?string = null,
suggestion: ?string = null,
pub fn init(file: []u8, namespace: []u8, line: i32, column: i32, length: u32, line_text: ?[]u8, suggestion: ?[]u8) Location {
return Location{
.file = file,
.namespace = namespace,
.line = line,
.column = column,
.length = length,
.line_text = line_text,
.suggestion = suggestion,
};
}
pub fn init_or_nil(_source: ?Source, r: Range) ?Location {
if (_source) |source| {
var data = source.initErrorPosition(r.loc);
return Location{
.file = source.path.pretty,
.namespace = source.path.namespace,
.line = usize2Loc(data.line_count).start,
.column = usize2Loc(data.column_count).start,
.length = source.contents.len,
.line_text = source.contents[data.line_start..data.line_end],
};
} else {
return null;
}
}
pub fn init_file(file: string, line: i32, column: i32, length: u32, line_text: ?[]u8, suggestion: ?[]u8) Location {
var namespace = "file".*;
return Location{
.file = file,
.namespace = &namespace,
.line = line,
.column = column,
.length = length,
.line_text = line_text,
.suggestion = suggestion,
};
}
};
pub const Data = struct { text: []u8, location: ?Location = null };
pub const Msg = struct {
kind: Kind = Kind.err,
data: Data,
};
pub const Range = struct {
loc: Loc = Loc.Empty,
len: i32 = 0,
pub const None = Range{ .loc = Loc.Empty, .len = 0 };
};
pub const Log = struct {
debug: bool = false,
warnings: u8 = 0,
errors: u8 = 0,
msgs: ArrayList(Msg),
pub fn addVerbose(log: *Log, source: ?Source, loc: Loc, text: []u8) void {
log.addMsg(Msg{
.kind = .verbose,
.data = rangeData(source, Range{ .Loc = loc }, text),
});
}
pub fn addVerboseWithNotes(source: ?Source, loc: Loc, text: []u8, notes: []Data) void {
log.addMsg(Msg{
.kind = .verbose,
.data = rangeData(source, Range{ .loc = loc }, text),
.notes = notes,
});
}
pub fn addRangeError(log: *Log, source: ?Source, r: Range, text: []u8) void {
log.errors += 1;
log.addMsg(Msg{
.kind = .Error,
.data = rangeData(source, r, text),
});
}
pub fn addRangeWarning(log: *Log, source: ?Source, r: Range, text: []u8) void {
log.warnings += 1;
log.addMsg(Msg{
.kind = .warning,
.data = rangeData(source, r, text),
});
}
pub fn addRangeDebug(log: *Log, source: ?Source, r: Range, text: []u8) void {
log.addMsg(Msg{
.kind = .debug,
.data = rangeData(source, r, text),
});
}
pub fn addRangeErrorWithNotes(log: *Log, source: ?Source, r: Range, text: []u8, notes: []Data) void {
log.errors += 1;
log.addMsg(Msg{
.kind = Kind.err,
.data = rangeData(source, r, text),
.notes = notes,
});
}
pub fn addRangeWarningWithNotes(log: *Log, source: ?Source, r: Range, text: []u8, notes: []Data) void {
log.warnings += 1;
log.addMsg(Msg{
.kind = .warning,
.data = rangeData(source, r, text),
.notes = notes,
});
}
// TODO:
pub fn addMsg(self: *Log, msg: Msg) !void {
try self.msgs.append(msg);
}
// TODO:
pub fn addError(self: *Log, _source: ?Source, loc: Loc, text: []u8) !void {
self.errors += 1;
try self.addMsg(Msg{ .kind = .err, .data = rangeData(_source, Range{ .loc = loc }, text) });
}
// TODO:
pub fn print(self: *Log, to: anytype) !void {
for (self.msgs.items) |msg| {
try std.fmt.format(to, "\n\n{s}: {s}\n{s}\n{s}:{}:{}", .{ msg.kind.string(), msg.data.text, msg.data.location.?.line_text, msg.data.location.?.file, msg.data.location.?.line, msg.data.location.?.column });
}
}
};
pub fn usize2Loc(loc: usize) Loc {
if (loc > std.math.maxInt(i32)) {
return Loc.Empty;
} else {
return Loc{ .start = @intCast(i32, loc) };
}
}
pub const Source = struct {
path: fs.Path,
index: u32 = 0,
contents: string,
// An identifier that is mixed in to automatically-generated symbol names to
// improve readability. For example, if the identifier is "util" then the
// symbol for an "export default" statement will be called "util_default".
identifier_name: string,
pub const ErrorPosition = struct { line_start: usize, line_end: usize, column_count: usize, line_count: usize };
pub fn initFile(file: fs.File, allocator: *std.mem.Allocator) Source {
std.debug.assert(file.contents != null);
var name = file.path.name;
var identifier_name = name.nonUniqueNameString(allocator) catch unreachable;
if (file.contents) |contents| {
return Source{ .path = file.path, .identifier_name = identifier_name, .contents = contents };
} else {
std.debug.panic("Expected file.contents to not be null. {s}", .{file});
}
}
pub fn initPathString(pathString: string, contents: string) Source {
var path = fs.Path.init(pathString);
return Source{ .path = path, .identifier_name = path.name.base, .contents = contents };
}
pub fn initErrorPosition(self: *const Source, _offset: Loc) ErrorPosition {
var prev_code_point: u21 = 0;
var offset: usize = if (_offset.start < 0) 0 else @intCast(usize, _offset.start);
const contents = self.contents;
var iter = unicode.Utf8Iterator{
.bytes = self.contents[0..offset],
.i = std.math.min(offset, self.contents.len),
};
var line_start: usize = 0;
var line_count: usize = 0;
while (iter.nextCodepoint()) |code_point| {
switch (code_point) {
'\n' => {
line_start = iter.i + 1;
if (prev_code_point != '\r') {
line_count += 1;
}
},
'\r' => {
line_start = iter.i + 1;
line_count += 1;
},
0x2028, 0x2029 => {
line_start = iter.i + 3; // These take three bytes to encode in UTF-8
line_count += 1;
},
else => {},
}
prev_code_point = code_point;
}
iter = unicode.Utf8Iterator{
.bytes = self.contents[offset..],
.i = std.math.min(offset, self.contents.len),
};
// Scan to the end of the line (or end of file if this is the last line)
var line_end: usize = contents.len;
loop: while (iter.nextCodepoint()) |code_point| {
switch (code_point) {
'\r', '\n', 0x2028, 0x2029 => {
line_end = offset + iter.i;
break :loop;
},
else => {},
}
}
return ErrorPosition{
.line_start = line_start,
.line_end = line_end,
.line_count = line_count,
.column_count = offset - line_start,
};
}
};
fn rangeData(source: ?Source, r: Range, text: []u8) Data {
return Data{ .text = text, .location = Location.init_or_nil(source, r) };
}
test "print msg" {
var log = Log{ .msgs = ArrayList(Msg).init(std.testing.allocator) };
defer log.msgs.deinit();
var filename = "test.js".*;
var syntax = "for(i = 0;)".*;
var err = "invalid syntax".*;
var namespace = "file".*;
try log.addMsg(Msg{
.kind = .err,
.data = Data{ .location = Location.init_file(&filename, 1, 3, 0, &syntax, ""), .text = &err },
});
const stdout = std.io.getStdOut().writer();
try log.print(stdout);
}
|