2018-05-30 15:26:09 -07:00
|
|
|
const std = @import("std");
|
|
|
|
const mem = std.mem;
|
2019-05-26 10:17:34 -07:00
|
|
|
const fs = std.fs;
|
|
|
|
const process = std.process;
|
2018-05-30 15:26:09 -07:00
|
|
|
const Token = std.zig.Token;
|
|
|
|
const ast = std.zig.ast;
|
|
|
|
const TokenIndex = std.zig.ast.TokenIndex;
|
2018-07-23 11:28:14 -07:00
|
|
|
const Compilation = @import("compilation.zig").Compilation;
|
|
|
|
const Scope = @import("scope.zig").Scope;
|
2018-05-30 15:26:09 -07:00
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const Color = enum {
|
2018-05-30 15:26:09 -07:00
|
|
|
Auto,
|
|
|
|
Off,
|
|
|
|
On,
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const Span = struct {
|
2018-07-10 17:18:43 -07:00
|
|
|
first: ast.TokenIndex,
|
|
|
|
last: ast.TokenIndex,
|
2018-07-13 18:56:38 -07:00
|
|
|
|
|
|
|
pub fn token(i: TokenIndex) Span {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Span{
|
2018-07-13 18:56:38 -07:00
|
|
|
.first = i,
|
|
|
|
.last = i,
|
|
|
|
};
|
|
|
|
}
|
2018-07-22 20:27:58 -07:00
|
|
|
|
|
|
|
pub fn node(n: *ast.Node) Span {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Span{
|
2018-07-22 20:27:58 -07:00
|
|
|
.first = n.firstToken(),
|
|
|
|
.last = n.lastToken(),
|
|
|
|
};
|
|
|
|
}
|
2018-07-10 17:18:43 -07:00
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const Msg = struct {
|
2018-07-23 11:28:14 -07:00
|
|
|
text: []u8,
|
2018-08-03 14:22:17 -07:00
|
|
|
realpath: []u8,
|
2018-07-23 11:28:14 -07:00
|
|
|
data: Data,
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const Data = union(enum) {
|
2018-08-03 14:22:17 -07:00
|
|
|
Cli: Cli,
|
2018-07-23 11:28:14 -07:00
|
|
|
PathAndTree: PathAndTree,
|
|
|
|
ScopeAndComp: ScopeAndComp,
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const PathAndTree = struct {
|
2018-08-03 14:22:17 -07:00
|
|
|
span: Span,
|
2018-07-23 11:28:14 -07:00
|
|
|
tree: *ast.Tree,
|
|
|
|
allocator: *mem.Allocator,
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const ScopeAndComp = struct {
|
2018-08-03 14:22:17 -07:00
|
|
|
span: Span,
|
2018-08-02 14:04:17 -07:00
|
|
|
tree_scope: *Scope.AstTree,
|
2018-07-23 11:28:14 -07:00
|
|
|
compilation: *Compilation,
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const Cli = struct {
|
2018-08-03 14:22:17 -07:00
|
|
|
allocator: *mem.Allocator,
|
|
|
|
};
|
|
|
|
|
2018-07-23 11:28:14 -07:00
|
|
|
pub fn destroy(self: *Msg) void {
|
|
|
|
switch (self.data) {
|
2019-11-06 22:31:00 -08:00
|
|
|
.Cli => |cli| {
|
2018-08-03 14:22:17 -07:00
|
|
|
cli.allocator.free(self.text);
|
|
|
|
cli.allocator.free(self.realpath);
|
|
|
|
cli.allocator.destroy(self);
|
|
|
|
},
|
2019-11-06 22:31:00 -08:00
|
|
|
.PathAndTree => |path_and_tree| {
|
2018-07-23 11:28:14 -07:00
|
|
|
path_and_tree.allocator.free(self.text);
|
2018-08-03 14:22:17 -07:00
|
|
|
path_and_tree.allocator.free(self.realpath);
|
2018-07-23 11:28:14 -07:00
|
|
|
path_and_tree.allocator.destroy(self);
|
|
|
|
},
|
2019-11-06 22:31:00 -08:00
|
|
|
.ScopeAndComp => |scope_and_comp| {
|
2018-08-02 14:04:17 -07:00
|
|
|
scope_and_comp.tree_scope.base.deref(scope_and_comp.compilation);
|
2018-07-23 11:28:14 -07:00
|
|
|
scope_and_comp.compilation.gpa().free(self.text);
|
2018-08-03 14:22:17 -07:00
|
|
|
scope_and_comp.compilation.gpa().free(self.realpath);
|
2018-07-23 11:28:14 -07:00
|
|
|
scope_and_comp.compilation.gpa().destroy(self);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn getAllocator(self: *const Msg) *mem.Allocator {
|
|
|
|
switch (self.data) {
|
2019-11-06 22:31:00 -08:00
|
|
|
.Cli => |cli| return cli.allocator,
|
|
|
|
.PathAndTree => |path_and_tree| {
|
2018-07-23 11:28:14 -07:00
|
|
|
return path_and_tree.allocator;
|
|
|
|
},
|
2019-11-06 22:31:00 -08:00
|
|
|
.ScopeAndComp => |scope_and_comp| {
|
2018-07-23 11:28:14 -07:00
|
|
|
return scope_and_comp.compilation.gpa();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getTree(self: *const Msg) *ast.Tree {
|
|
|
|
switch (self.data) {
|
2019-11-06 22:31:00 -08:00
|
|
|
.Cli => unreachable,
|
|
|
|
.PathAndTree => |path_and_tree| {
|
2018-07-23 11:28:14 -07:00
|
|
|
return path_and_tree.tree;
|
|
|
|
},
|
2019-11-06 22:31:00 -08:00
|
|
|
.ScopeAndComp => |scope_and_comp| {
|
2018-08-02 14:04:17 -07:00
|
|
|
return scope_and_comp.tree_scope.tree;
|
2018-07-23 11:28:14 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
pub fn getSpan(self: *const Msg) Span {
|
|
|
|
return switch (self.data) {
|
2019-11-06 22:31:00 -08:00
|
|
|
.Cli => unreachable,
|
|
|
|
.PathAndTree => |path_and_tree| path_and_tree.span,
|
|
|
|
.ScopeAndComp => |scope_and_comp| scope_and_comp.span,
|
2018-08-03 14:22:17 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-23 11:28:14 -07:00
|
|
|
/// Takes ownership of text
|
2018-08-02 14:04:17 -07:00
|
|
|
/// References tree_scope, and derefs when the msg is freed
|
|
|
|
pub fn createFromScope(comp: *Compilation, tree_scope: *Scope.AstTree, span: Span, text: []u8) !*Msg {
|
2018-08-03 14:22:17 -07:00
|
|
|
const realpath = try mem.dupe(comp.gpa(), u8, tree_scope.root().realpath);
|
|
|
|
errdefer comp.gpa().free(realpath);
|
|
|
|
|
2019-02-03 13:13:28 -08:00
|
|
|
const msg = try comp.gpa().create(Msg);
|
|
|
|
msg.* = Msg{
|
2018-07-23 11:28:14 -07:00
|
|
|
.text = text,
|
2018-08-03 14:22:17 -07:00
|
|
|
.realpath = realpath,
|
2018-11-13 05:08:37 -08:00
|
|
|
.data = Data{
|
|
|
|
.ScopeAndComp = ScopeAndComp{
|
2018-08-02 14:04:17 -07:00
|
|
|
.tree_scope = tree_scope,
|
2018-07-23 11:28:14 -07:00
|
|
|
.compilation = comp,
|
2018-08-03 14:22:17 -07:00
|
|
|
.span = span,
|
2018-07-23 11:28:14 -07:00
|
|
|
},
|
|
|
|
},
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-08-02 14:04:17 -07:00
|
|
|
tree_scope.base.ref();
|
2018-07-23 11:28:14 -07:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
/// Caller owns returned Msg and must free with `allocator`
|
|
|
|
/// allocator will additionally be used for printing messages later.
|
|
|
|
pub fn createFromCli(comp: *Compilation, realpath: []const u8, text: []u8) !*Msg {
|
|
|
|
const realpath_copy = try mem.dupe(comp.gpa(), u8, realpath);
|
|
|
|
errdefer comp.gpa().free(realpath_copy);
|
|
|
|
|
2019-02-03 13:13:28 -08:00
|
|
|
const msg = try comp.gpa().create(Msg);
|
|
|
|
msg.* = Msg{
|
2018-08-03 14:22:17 -07:00
|
|
|
.text = text,
|
|
|
|
.realpath = realpath_copy,
|
2018-11-13 05:08:37 -08:00
|
|
|
.data = Data{
|
|
|
|
.Cli = Cli{ .allocator = comp.gpa() },
|
2018-08-03 14:22:17 -07:00
|
|
|
},
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-08-03 14:22:17 -07:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2018-07-23 11:28:14 -07:00
|
|
|
pub fn createFromParseErrorAndScope(
|
|
|
|
comp: *Compilation,
|
2018-08-02 14:04:17 -07:00
|
|
|
tree_scope: *Scope.AstTree,
|
2018-07-23 11:28:14 -07:00
|
|
|
parse_error: *const ast.Error,
|
|
|
|
) !*Msg {
|
|
|
|
const loc_token = parse_error.loc();
|
|
|
|
var text_buf = try std.Buffer.initSize(comp.gpa(), 0);
|
|
|
|
defer text_buf.deinit();
|
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
const realpath_copy = try mem.dupe(comp.gpa(), u8, tree_scope.root().realpath);
|
|
|
|
errdefer comp.gpa().free(realpath_copy);
|
|
|
|
|
2018-07-23 11:28:14 -07:00
|
|
|
var out_stream = &std.io.BufferOutStream.init(&text_buf).stream;
|
2018-08-02 14:04:17 -07:00
|
|
|
try parse_error.render(&tree_scope.tree.tokens, out_stream);
|
2018-07-23 11:28:14 -07:00
|
|
|
|
2019-02-03 13:13:28 -08:00
|
|
|
const msg = try comp.gpa().create(Msg);
|
|
|
|
msg.* = Msg{
|
2018-07-23 11:28:14 -07:00
|
|
|
.text = undefined,
|
2018-08-03 14:22:17 -07:00
|
|
|
.realpath = realpath_copy,
|
2018-11-13 05:08:37 -08:00
|
|
|
.data = Data{
|
|
|
|
.ScopeAndComp = ScopeAndComp{
|
2018-08-02 14:04:17 -07:00
|
|
|
.tree_scope = tree_scope,
|
2018-07-23 11:28:14 -07:00
|
|
|
.compilation = comp,
|
2018-11-13 05:08:37 -08:00
|
|
|
.span = Span{
|
2018-08-03 14:22:17 -07:00
|
|
|
.first = loc_token,
|
|
|
|
.last = loc_token,
|
|
|
|
},
|
2018-07-23 11:28:14 -07:00
|
|
|
},
|
|
|
|
},
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-08-02 14:04:17 -07:00
|
|
|
tree_scope.base.ref();
|
2018-07-23 11:28:14 -07:00
|
|
|
msg.text = text_buf.toOwnedSlice();
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `realpath` must outlive the returned Msg
|
|
|
|
/// `tree` must outlive the returned Msg
|
|
|
|
/// Caller owns returned Msg and must free with `allocator`
|
|
|
|
/// allocator will additionally be used for printing messages later.
|
|
|
|
pub fn createFromParseError(
|
|
|
|
allocator: *mem.Allocator,
|
|
|
|
parse_error: *const ast.Error,
|
|
|
|
tree: *ast.Tree,
|
|
|
|
realpath: []const u8,
|
|
|
|
) !*Msg {
|
|
|
|
const loc_token = parse_error.loc();
|
|
|
|
var text_buf = try std.Buffer.initSize(allocator, 0);
|
|
|
|
defer text_buf.deinit();
|
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
const realpath_copy = try mem.dupe(allocator, u8, realpath);
|
|
|
|
errdefer allocator.free(realpath_copy);
|
|
|
|
|
2018-07-23 11:28:14 -07:00
|
|
|
var out_stream = &std.io.BufferOutStream.init(&text_buf).stream;
|
|
|
|
try parse_error.render(&tree.tokens, out_stream);
|
|
|
|
|
2019-02-03 13:13:28 -08:00
|
|
|
const msg = try allocator.create(Msg);
|
|
|
|
msg.* = Msg{
|
2018-07-23 11:28:14 -07:00
|
|
|
.text = undefined,
|
2018-08-03 14:22:17 -07:00
|
|
|
.realpath = realpath_copy,
|
2018-11-13 05:08:37 -08:00
|
|
|
.data = Data{
|
|
|
|
.PathAndTree = PathAndTree{
|
2018-07-23 11:28:14 -07:00
|
|
|
.allocator = allocator,
|
|
|
|
.tree = tree,
|
2018-11-13 05:08:37 -08:00
|
|
|
.span = Span{
|
2018-08-03 14:22:17 -07:00
|
|
|
.first = loc_token,
|
|
|
|
.last = loc_token,
|
|
|
|
},
|
2018-07-23 11:28:14 -07:00
|
|
|
},
|
|
|
|
},
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-07-23 11:28:14 -07:00
|
|
|
msg.text = text_buf.toOwnedSlice();
|
|
|
|
errdefer allocator.destroy(msg);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn printToStream(msg: *const Msg, stream: var, color_on: bool) !void {
|
2018-08-03 14:22:17 -07:00
|
|
|
switch (msg.data) {
|
2019-11-06 22:31:00 -08:00
|
|
|
.Cli => {
|
2019-12-08 20:17:03 -08:00
|
|
|
try stream.print("{}:-:-: error: {}\n", .{ msg.realpath, msg.text });
|
2018-08-03 14:22:17 -07:00
|
|
|
return;
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
|
2018-07-23 11:28:14 -07:00
|
|
|
const allocator = msg.getAllocator();
|
|
|
|
const tree = msg.getTree();
|
|
|
|
|
2019-05-26 10:17:34 -07:00
|
|
|
const cwd = try process.getCwdAlloc(allocator);
|
2018-07-23 11:28:14 -07:00
|
|
|
defer allocator.free(cwd);
|
|
|
|
|
2019-05-26 10:17:34 -07:00
|
|
|
const relpath = try fs.path.relative(allocator, cwd, msg.realpath);
|
2018-07-23 11:28:14 -07:00
|
|
|
defer allocator.free(relpath);
|
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
const path = if (relpath.len < msg.realpath.len) relpath else msg.realpath;
|
|
|
|
const span = msg.getSpan();
|
2018-07-23 11:28:14 -07:00
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
const first_token = tree.tokens.at(span.first);
|
|
|
|
const last_token = tree.tokens.at(span.last);
|
2018-07-23 11:28:14 -07:00
|
|
|
const start_loc = tree.tokenLocationPtr(0, first_token);
|
|
|
|
const end_loc = tree.tokenLocationPtr(first_token.end, last_token);
|
|
|
|
if (!color_on) {
|
2019-12-08 20:17:03 -08:00
|
|
|
try stream.print("{}:{}:{}: error: {}\n", .{
|
2018-07-23 11:28:14 -07:00
|
|
|
path,
|
|
|
|
start_loc.line + 1,
|
|
|
|
start_loc.column + 1,
|
|
|
|
msg.text,
|
2019-12-08 20:17:03 -08:00
|
|
|
});
|
2018-07-23 11:28:14 -07:00
|
|
|
return;
|
|
|
|
}
|
2018-05-30 15:26:09 -07:00
|
|
|
|
2019-12-08 20:17:03 -08:00
|
|
|
try stream.print("{}:{}:{}: error: {}\n{}\n", .{
|
2018-07-23 11:28:14 -07:00
|
|
|
path,
|
2018-05-30 15:26:09 -07:00
|
|
|
start_loc.line + 1,
|
|
|
|
start_loc.column + 1,
|
|
|
|
msg.text,
|
2018-07-23 11:28:14 -07:00
|
|
|
tree.source[start_loc.line_start..start_loc.line_end],
|
2019-12-08 20:17:03 -08:00
|
|
|
});
|
2018-07-23 11:28:14 -07:00
|
|
|
try stream.writeByteNTimes(' ', start_loc.column);
|
|
|
|
try stream.writeByteNTimes('~', last_token.end - first_token.start);
|
|
|
|
try stream.write("\n");
|
2018-05-30 15:26:09 -07:00
|
|
|
}
|
|
|
|
|
2019-05-26 10:17:34 -07:00
|
|
|
pub fn printToFile(msg: *const Msg, file: fs.File, color: Color) !void {
|
2018-07-23 11:28:14 -07:00
|
|
|
const color_on = switch (color) {
|
2019-11-06 22:31:00 -08:00
|
|
|
.Auto => file.isTty(),
|
|
|
|
.On => true,
|
|
|
|
.Off => false,
|
2018-07-23 11:28:14 -07:00
|
|
|
};
|
2018-09-30 14:23:42 -07:00
|
|
|
var stream = &file.outStream().stream;
|
2018-07-23 11:28:14 -07:00
|
|
|
return msg.printToStream(stream, color_on);
|
|
|
|
}
|
|
|
|
};
|