2017-11-07 08:22:27 +00:00
|
|
|
const std = @import("std");
|
2020-02-27 18:32:49 +00:00
|
|
|
const builtin = std.builtin;
|
2017-11-07 08:22:27 +00:00
|
|
|
const io = std.io;
|
2019-05-26 17:17:34 +00:00
|
|
|
const fs = std.fs;
|
|
|
|
const process = std.process;
|
|
|
|
const ChildProcess = std.ChildProcess;
|
2020-09-27 04:03:38 +00:00
|
|
|
const print = std.debug.print;
|
2018-01-17 04:19:05 +00:00
|
|
|
const mem = std.mem;
|
2019-02-08 23:18:47 +00:00
|
|
|
const testing = std.testing;
|
2018-01-17 04:19:05 +00:00
|
|
|
|
2018-01-17 06:50:35 +00:00
|
|
|
const max_doc_file_size = 10 * 1024 * 1024;
|
|
|
|
|
2020-02-26 06:18:23 +00:00
|
|
|
const exe_ext = @as(std.zig.CrossTarget, .{}).exeFileExt();
|
|
|
|
const obj_ext = @as(std.zig.CrossTarget, .{}).oFileExt();
|
2018-01-19 08:03:20 +00:00
|
|
|
const tmp_dir_name = "docgen_tmp";
|
2019-05-26 17:17:34 +00:00
|
|
|
const test_out_path = tmp_dir_name ++ fs.path.sep_str ++ "test" ++ exe_ext;
|
2017-11-07 08:22:27 +00:00
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn main() !void {
|
2019-11-25 22:25:06 +00:00
|
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
2018-02-12 07:14:44 +00:00
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
const allocator = &arena.allocator;
|
2017-11-07 08:22:27 +00:00
|
|
|
|
2019-05-26 17:17:34 +00:00
|
|
|
var args_it = process.args();
|
2017-11-07 08:22:27 +00:00
|
|
|
|
|
|
|
if (!args_it.skip()) @panic("expected self arg");
|
|
|
|
|
2018-06-10 05:13:51 +00:00
|
|
|
const zig_exe = try (args_it.next(allocator) orelse @panic("expected zig exe arg"));
|
2018-01-17 06:50:35 +00:00
|
|
|
defer allocator.free(zig_exe);
|
|
|
|
|
2018-06-10 05:13:51 +00:00
|
|
|
const in_file_name = try (args_it.next(allocator) orelse @panic("expected input arg"));
|
2017-11-07 08:22:27 +00:00
|
|
|
defer allocator.free(in_file_name);
|
|
|
|
|
2018-06-10 05:13:51 +00:00
|
|
|
const out_file_name = try (args_it.next(allocator) orelse @panic("expected output arg"));
|
2017-11-07 08:22:27 +00:00
|
|
|
defer allocator.free(out_file_name);
|
|
|
|
|
2020-02-08 05:03:06 +00:00
|
|
|
var in_file = try fs.cwd().openFile(in_file_name, .{ .read = true });
|
2017-11-07 08:22:27 +00:00
|
|
|
defer in_file.close();
|
|
|
|
|
2020-02-08 05:03:06 +00:00
|
|
|
var out_file = try fs.cwd().createFile(out_file_name, .{});
|
2017-11-07 08:22:27 +00:00
|
|
|
defer out_file.close();
|
|
|
|
|
2020-03-11 00:22:30 +00:00
|
|
|
const input_file_bytes = try in_file.inStream().readAllAlloc(allocator, max_doc_file_size);
|
2018-01-17 04:19:05 +00:00
|
|
|
|
2020-12-23 11:01:29 +00:00
|
|
|
var buffered_out_stream = io.bufferedOutStream(out_file.writer());
|
2017-11-07 08:22:27 +00:00
|
|
|
|
2018-01-17 05:22:33 +00:00
|
|
|
var tokenizer = Tokenizer.init(in_file_name, input_file_bytes);
|
|
|
|
var toc = try genToc(allocator, &tokenizer);
|
2017-11-07 08:22:27 +00:00
|
|
|
|
2020-03-03 21:01:09 +00:00
|
|
|
try fs.cwd().makePath(tmp_dir_name);
|
2020-03-18 20:09:06 +00:00
|
|
|
defer fs.cwd().deleteTree(tmp_dir_name) catch {};
|
2018-06-12 05:55:08 +00:00
|
|
|
|
2020-12-23 11:01:29 +00:00
|
|
|
try genHtml(allocator, &tokenizer, &toc, buffered_out_stream.writer(), zig_exe);
|
2018-01-17 04:19:05 +00:00
|
|
|
try buffered_out_stream.flush();
|
2017-11-07 08:22:27 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const Token = struct {
|
2018-01-17 04:19:05 +00:00
|
|
|
id: Id,
|
|
|
|
start: usize,
|
|
|
|
end: usize,
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const Id = enum {
|
2018-01-17 04:19:05 +00:00
|
|
|
Invalid,
|
|
|
|
Content,
|
|
|
|
BracketOpen,
|
|
|
|
TagContent,
|
|
|
|
Separator,
|
|
|
|
BracketClose,
|
|
|
|
Eof,
|
|
|
|
};
|
2017-11-07 08:22:27 +00:00
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const Tokenizer = struct {
|
2018-01-17 04:19:05 +00:00
|
|
|
buffer: []const u8,
|
|
|
|
index: usize,
|
|
|
|
state: State,
|
|
|
|
source_file_name: []const u8,
|
2018-01-19 08:03:20 +00:00
|
|
|
code_node_count: usize,
|
2017-11-07 08:22:27 +00:00
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const State = enum {
|
2018-01-17 04:19:05 +00:00
|
|
|
Start,
|
|
|
|
LBracket,
|
|
|
|
Hash,
|
|
|
|
TagName,
|
|
|
|
Eof,
|
|
|
|
};
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn init(source_file_name: []const u8, buffer: []const u8) Tokenizer {
|
2018-11-13 13:08:37 +00:00
|
|
|
return Tokenizer{
|
2018-01-17 04:19:05 +00:00
|
|
|
.buffer = buffer,
|
|
|
|
.index = 0,
|
|
|
|
.state = State.Start,
|
|
|
|
.source_file_name = source_file_name,
|
2018-01-19 08:03:20 +00:00
|
|
|
.code_node_count = 0,
|
2018-01-17 04:19:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
fn next(self: *Tokenizer) Token {
|
2018-11-13 13:08:37 +00:00
|
|
|
var result = Token{
|
2018-01-17 04:19:05 +00:00
|
|
|
.id = Token.Id.Eof,
|
|
|
|
.start = self.index,
|
|
|
|
.end = undefined,
|
2017-11-07 08:22:27 +00:00
|
|
|
};
|
2018-01-17 04:19:05 +00:00
|
|
|
while (self.index < self.buffer.len) : (self.index += 1) {
|
|
|
|
const c = self.buffer[self.index];
|
|
|
|
switch (self.state) {
|
|
|
|
State.Start => switch (c) {
|
|
|
|
'{' => {
|
|
|
|
self.state = State.LBracket;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
result.id = Token.Id.Content;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
State.LBracket => switch (c) {
|
|
|
|
'#' => {
|
|
|
|
if (result.id != Token.Id.Eof) {
|
|
|
|
self.index -= 1;
|
|
|
|
self.state = State.Start;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
result.id = Token.Id.BracketOpen;
|
|
|
|
self.index += 1;
|
|
|
|
self.state = State.TagName;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
result.id = Token.Id.Content;
|
|
|
|
self.state = State.Start;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
State.TagName => switch (c) {
|
|
|
|
'|' => {
|
|
|
|
if (result.id != Token.Id.Eof) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
result.id = Token.Id.Separator;
|
|
|
|
self.index += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'#' => {
|
|
|
|
self.state = State.Hash;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
result.id = Token.Id.TagContent;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
State.Hash => switch (c) {
|
|
|
|
'}' => {
|
|
|
|
if (result.id != Token.Id.Eof) {
|
|
|
|
self.index -= 1;
|
|
|
|
self.state = State.TagName;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
result.id = Token.Id.BracketClose;
|
|
|
|
self.index += 1;
|
|
|
|
self.state = State.Start;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
result.id = Token.Id.TagContent;
|
|
|
|
self.state = State.TagName;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
State.Eof => unreachable,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (self.state) {
|
|
|
|
State.Start, State.LBracket, State.Eof => {},
|
2017-11-07 08:22:27 +00:00
|
|
|
else => {
|
2018-01-17 04:19:05 +00:00
|
|
|
result.id = Token.Id.Invalid;
|
2017-11-07 08:22:27 +00:00
|
|
|
},
|
2018-01-17 04:19:05 +00:00
|
|
|
}
|
|
|
|
self.state = State.Eof;
|
|
|
|
}
|
|
|
|
result.end = self.index;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const Location = struct {
|
2018-01-17 04:19:05 +00:00
|
|
|
line: usize,
|
|
|
|
column: usize,
|
|
|
|
line_start: usize,
|
|
|
|
line_end: usize,
|
|
|
|
};
|
|
|
|
|
2018-10-15 22:23:47 +00:00
|
|
|
fn getTokenLocation(self: *Tokenizer, token: Token) Location {
|
2018-11-13 13:08:37 +00:00
|
|
|
var loc = Location{
|
2018-01-17 04:19:05 +00:00
|
|
|
.line = 0,
|
|
|
|
.column = 0,
|
|
|
|
.line_start = 0,
|
|
|
|
.line_end = 0,
|
|
|
|
};
|
|
|
|
for (self.buffer) |c, i| {
|
|
|
|
if (i == token.start) {
|
|
|
|
loc.line_end = i;
|
|
|
|
while (loc.line_end < self.buffer.len and self.buffer[loc.line_end] != '\n') : (loc.line_end += 1) {}
|
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
if (c == '\n') {
|
|
|
|
loc.line += 1;
|
|
|
|
loc.column = 0;
|
|
|
|
loc.line_start = i + 1;
|
|
|
|
} else {
|
|
|
|
loc.column += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-11 19:04:38 +00:00
|
|
|
fn parseError(tokenizer: *Tokenizer, token: Token, comptime fmt: []const u8, args: anytype) anyerror {
|
2018-01-17 04:19:05 +00:00
|
|
|
const loc = tokenizer.getTokenLocation(token);
|
2019-12-09 05:18:01 +00:00
|
|
|
const args_prefix = .{ tokenizer.source_file_name, loc.line + 1, loc.column + 1 };
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}:{}:{}: error: " ++ fmt ++ "\n", args_prefix ++ args);
|
2018-01-17 04:19:05 +00:00
|
|
|
if (loc.line_start <= loc.line_end) {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}\n", .{tokenizer.buffer[loc.line_start..loc.line_end]});
|
2018-01-17 04:19:05 +00:00
|
|
|
{
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < loc.column) : (i += 1) {
|
2020-09-27 04:03:38 +00:00
|
|
|
print(" ", .{});
|
2018-01-17 04:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const caret_count = token.end - token.start;
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < caret_count) : (i += 1) {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("~", .{});
|
2018-01-17 04:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-27 04:03:38 +00:00
|
|
|
print("\n", .{});
|
2018-01-17 04:19:05 +00:00
|
|
|
}
|
|
|
|
return error.ParseError;
|
|
|
|
}
|
|
|
|
|
2018-10-15 22:23:47 +00:00
|
|
|
fn assertToken(tokenizer: *Tokenizer, token: Token, id: Token.Id) !void {
|
2018-01-17 04:19:05 +00:00
|
|
|
if (token.id != id) {
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, token, "expected {}, found {}", .{ @tagName(id), @tagName(token.id) });
|
2018-01-17 04:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
fn eatToken(tokenizer: *Tokenizer, id: Token.Id) !Token {
|
2018-01-17 04:19:05 +00:00
|
|
|
const token = tokenizer.next();
|
|
|
|
try assertToken(tokenizer, token, id);
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const HeaderOpen = struct {
|
2018-01-17 04:19:05 +00:00
|
|
|
name: []const u8,
|
|
|
|
url: []const u8,
|
|
|
|
n: usize,
|
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const SeeAlsoItem = struct {
|
2018-01-17 05:22:33 +00:00
|
|
|
name: []const u8,
|
|
|
|
token: Token,
|
2018-01-17 04:19:05 +00:00
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const ExpectedOutcome = enum {
|
2018-01-19 08:03:20 +00:00
|
|
|
Succeed,
|
|
|
|
Fail,
|
2019-05-01 19:34:17 +00:00
|
|
|
BuildFail,
|
2018-01-19 08:03:20 +00:00
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const Code = struct {
|
2018-01-17 06:50:35 +00:00
|
|
|
id: Id,
|
|
|
|
name: []const u8,
|
|
|
|
source_token: Token,
|
2018-01-19 08:03:20 +00:00
|
|
|
is_inline: bool,
|
|
|
|
mode: builtin.Mode,
|
|
|
|
link_objects: []const []const u8,
|
2019-03-20 23:00:23 +00:00
|
|
|
target_str: ?[]const u8,
|
2018-01-19 08:03:20 +00:00
|
|
|
link_libc: bool,
|
2020-09-27 04:03:38 +00:00
|
|
|
disable_cache: bool,
|
2018-01-17 06:50:35 +00:00
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const Id = union(enum) {
|
2018-01-17 06:50:35 +00:00
|
|
|
Test,
|
2018-01-19 08:03:20 +00:00
|
|
|
TestError: []const u8,
|
|
|
|
TestSafety: []const u8,
|
|
|
|
Exe: ExpectedOutcome,
|
2018-01-23 03:24:07 +00:00
|
|
|
Obj: ?[]const u8,
|
2019-05-16 18:54:05 +00:00
|
|
|
Lib,
|
2018-01-17 06:50:35 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const Link = struct {
|
2018-01-23 04:06:07 +00:00
|
|
|
url: []const u8,
|
|
|
|
name: []const u8,
|
|
|
|
token: Token,
|
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const Node = union(enum) {
|
2018-01-17 04:19:05 +00:00
|
|
|
Content: []const u8,
|
|
|
|
Nav,
|
2018-09-25 16:03:09 +00:00
|
|
|
Builtin: Token,
|
2018-01-17 04:19:05 +00:00
|
|
|
HeaderOpen: HeaderOpen,
|
2018-01-17 05:22:33 +00:00
|
|
|
SeeAlso: []const SeeAlsoItem,
|
2018-01-17 06:50:35 +00:00
|
|
|
Code: Code,
|
2018-01-23 04:06:07 +00:00
|
|
|
Link: Link,
|
2018-09-14 14:35:03 +00:00
|
|
|
Syntax: Token,
|
2018-01-17 04:19:05 +00:00
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const Toc = struct {
|
2018-01-17 04:19:05 +00:00
|
|
|
nodes: []Node,
|
|
|
|
toc: []u8,
|
2019-09-03 21:53:05 +00:00
|
|
|
urls: std.StringHashMap(Token),
|
2018-01-17 04:19:05 +00:00
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const Action = enum {
|
2018-01-17 04:19:05 +00:00
|
|
|
Open,
|
|
|
|
Close,
|
|
|
|
};
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
|
2019-09-03 21:53:05 +00:00
|
|
|
var urls = std.StringHashMap(Token).init(allocator);
|
2018-01-24 04:08:09 +00:00
|
|
|
errdefer urls.deinit();
|
2018-01-17 04:19:05 +00:00
|
|
|
|
|
|
|
var header_stack_size: usize = 0;
|
|
|
|
var last_action = Action.Open;
|
2019-09-04 15:08:28 +00:00
|
|
|
var last_columns: ?u8 = null;
|
2018-01-17 04:19:05 +00:00
|
|
|
|
2020-03-08 07:16:10 +00:00
|
|
|
var toc_buf = std.ArrayList(u8).init(allocator);
|
2018-01-17 04:19:05 +00:00
|
|
|
defer toc_buf.deinit();
|
|
|
|
|
2020-12-23 11:01:29 +00:00
|
|
|
var toc = toc_buf.writer();
|
2018-01-17 04:19:05 +00:00
|
|
|
|
|
|
|
var nodes = std.ArrayList(Node).init(allocator);
|
|
|
|
defer nodes.deinit();
|
|
|
|
|
|
|
|
try toc.writeByte('\n');
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const token = tokenizer.next();
|
|
|
|
switch (token.id) {
|
|
|
|
Token.Id.Eof => {
|
|
|
|
if (header_stack_size != 0) {
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, token, "unbalanced headers", .{});
|
2018-01-17 04:19:05 +00:00
|
|
|
}
|
2020-03-11 00:22:30 +00:00
|
|
|
try toc.writeAll(" </ul>\n");
|
2018-01-17 04:19:05 +00:00
|
|
|
break;
|
|
|
|
},
|
|
|
|
Token.Id.Content => {
|
2018-11-13 13:08:37 +00:00
|
|
|
try nodes.append(Node{ .Content = tokenizer.buffer[token.start..token.end] });
|
2018-01-17 04:19:05 +00:00
|
|
|
},
|
|
|
|
Token.Id.BracketOpen => {
|
2018-01-17 05:22:33 +00:00
|
|
|
const tag_token = try eatToken(tokenizer, Token.Id.TagContent);
|
|
|
|
const tag_name = tokenizer.buffer[tag_token.start..tag_token.end];
|
2018-01-17 04:19:05 +00:00
|
|
|
|
|
|
|
if (mem.eql(u8, tag_name, "nav")) {
|
2018-01-17 06:50:35 +00:00
|
|
|
_ = try eatToken(tokenizer, Token.Id.BracketClose);
|
2018-01-17 05:22:33 +00:00
|
|
|
|
|
|
|
try nodes.append(Node.Nav);
|
2018-06-07 23:10:45 +00:00
|
|
|
} else if (mem.eql(u8, tag_name, "builtin")) {
|
|
|
|
_ = try eatToken(tokenizer, Token.Id.BracketClose);
|
2018-11-13 13:08:37 +00:00
|
|
|
try nodes.append(Node{ .Builtin = tag_token });
|
2018-01-17 04:19:05 +00:00
|
|
|
} else if (mem.eql(u8, tag_name, "header_open")) {
|
2018-01-17 06:50:35 +00:00
|
|
|
_ = try eatToken(tokenizer, Token.Id.Separator);
|
2018-01-17 05:22:33 +00:00
|
|
|
const content_token = try eatToken(tokenizer, Token.Id.TagContent);
|
|
|
|
const content = tokenizer.buffer[content_token.start..content_token.end];
|
2019-09-04 15:08:28 +00:00
|
|
|
var columns: ?u8 = null;
|
|
|
|
while (true) {
|
|
|
|
const bracket_tok = tokenizer.next();
|
|
|
|
switch (bracket_tok.id) {
|
|
|
|
.BracketClose => break,
|
|
|
|
.Separator => continue,
|
|
|
|
.TagContent => {
|
|
|
|
const param = tokenizer.buffer[bracket_tok.start..bracket_tok.end];
|
2019-10-23 10:55:20 +00:00
|
|
|
if (mem.eql(u8, param, "2col")) {
|
|
|
|
columns = 2;
|
2019-09-04 15:08:28 +00:00
|
|
|
} else {
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(
|
|
|
|
tokenizer,
|
|
|
|
bracket_tok,
|
|
|
|
"unrecognized header_open param: {}",
|
|
|
|
.{param},
|
|
|
|
);
|
2019-09-04 15:08:28 +00:00
|
|
|
}
|
|
|
|
},
|
2019-12-09 05:18:01 +00:00
|
|
|
else => return parseError(tokenizer, bracket_tok, "invalid header_open token", .{}),
|
2019-09-04 15:08:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-17 05:22:33 +00:00
|
|
|
|
2018-01-17 04:19:05 +00:00
|
|
|
header_stack_size += 1;
|
2018-01-17 05:22:33 +00:00
|
|
|
|
|
|
|
const urlized = try urlize(allocator, content);
|
2018-11-13 13:08:37 +00:00
|
|
|
try nodes.append(Node{
|
|
|
|
.HeaderOpen = HeaderOpen{
|
2018-05-26 22:16:39 +00:00
|
|
|
.name = content,
|
|
|
|
.url = urlized,
|
|
|
|
.n = header_stack_size,
|
|
|
|
},
|
|
|
|
});
|
2020-07-04 22:25:49 +00:00
|
|
|
if (try urls.fetchPut(urlized, tag_token)) |entry| {
|
2019-12-09 05:18:01 +00:00
|
|
|
parseError(tokenizer, tag_token, "duplicate header url: #{}", .{urlized}) catch {};
|
|
|
|
parseError(tokenizer, entry.value, "other tag here", .{}) catch {};
|
2018-01-17 05:22:33 +00:00
|
|
|
return error.ParseError;
|
|
|
|
}
|
|
|
|
if (last_action == Action.Open) {
|
|
|
|
try toc.writeByte('\n');
|
|
|
|
try toc.writeByteNTimes(' ', header_stack_size * 4);
|
2019-09-04 15:08:28 +00:00
|
|
|
if (last_columns) |n| {
|
2019-12-09 05:18:01 +00:00
|
|
|
try toc.print("<ul style=\"columns: {}\">\n", .{n});
|
2019-09-04 15:08:28 +00:00
|
|
|
} else {
|
2020-03-11 00:22:30 +00:00
|
|
|
try toc.writeAll("<ul>\n");
|
2019-09-04 15:08:28 +00:00
|
|
|
}
|
2018-01-17 05:22:33 +00:00
|
|
|
} else {
|
|
|
|
last_action = Action.Open;
|
|
|
|
}
|
2019-09-04 15:08:28 +00:00
|
|
|
last_columns = columns;
|
2018-01-17 05:22:33 +00:00
|
|
|
try toc.writeByteNTimes(' ', 4 + header_stack_size * 4);
|
2019-12-09 05:18:01 +00:00
|
|
|
try toc.print("<li><a id=\"toc-{}\" href=\"#{}\">{}</a>", .{ urlized, urlized, content });
|
2018-01-17 04:19:05 +00:00
|
|
|
} else if (mem.eql(u8, tag_name, "header_close")) {
|
|
|
|
if (header_stack_size == 0) {
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, tag_token, "unbalanced close header", .{});
|
2018-01-17 04:19:05 +00:00
|
|
|
}
|
|
|
|
header_stack_size -= 1;
|
2018-01-17 06:50:35 +00:00
|
|
|
_ = try eatToken(tokenizer, Token.Id.BracketClose);
|
2018-01-17 05:22:33 +00:00
|
|
|
|
|
|
|
if (last_action == Action.Close) {
|
|
|
|
try toc.writeByteNTimes(' ', 8 + header_stack_size * 4);
|
2020-03-11 00:22:30 +00:00
|
|
|
try toc.writeAll("</ul></li>\n");
|
2018-01-17 05:22:33 +00:00
|
|
|
} else {
|
2020-03-11 00:22:30 +00:00
|
|
|
try toc.writeAll("</li>\n");
|
2018-01-17 05:22:33 +00:00
|
|
|
last_action = Action.Close;
|
|
|
|
}
|
|
|
|
} else if (mem.eql(u8, tag_name, "see_also")) {
|
|
|
|
var list = std.ArrayList(SeeAlsoItem).init(allocator);
|
2018-01-24 04:08:09 +00:00
|
|
|
errdefer list.deinit();
|
2018-01-17 05:22:33 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const see_also_tok = tokenizer.next();
|
|
|
|
switch (see_also_tok.id) {
|
|
|
|
Token.Id.TagContent => {
|
|
|
|
const content = tokenizer.buffer[see_also_tok.start..see_also_tok.end];
|
2018-11-13 13:08:37 +00:00
|
|
|
try list.append(SeeAlsoItem{
|
2018-01-17 05:22:33 +00:00
|
|
|
.name = content,
|
|
|
|
.token = see_also_tok,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
Token.Id.Separator => {},
|
|
|
|
Token.Id.BracketClose => {
|
2018-11-13 13:08:37 +00:00
|
|
|
try nodes.append(Node{ .SeeAlso = list.toOwnedSlice() });
|
2018-01-17 05:22:33 +00:00
|
|
|
break;
|
|
|
|
},
|
2019-12-09 05:18:01 +00:00
|
|
|
else => return parseError(tokenizer, see_also_tok, "invalid see_also token", .{}),
|
2018-01-17 04:19:05 +00:00
|
|
|
}
|
2018-01-17 05:22:33 +00:00
|
|
|
}
|
2018-01-23 04:06:07 +00:00
|
|
|
} else if (mem.eql(u8, tag_name, "link")) {
|
|
|
|
_ = try eatToken(tokenizer, Token.Id.Separator);
|
|
|
|
const name_tok = try eatToken(tokenizer, Token.Id.TagContent);
|
|
|
|
const name = tokenizer.buffer[name_tok.start..name_tok.end];
|
|
|
|
|
|
|
|
const url_name = blk: {
|
|
|
|
const tok = tokenizer.next();
|
|
|
|
switch (tok.id) {
|
|
|
|
Token.Id.BracketClose => break :blk name,
|
|
|
|
Token.Id.Separator => {
|
|
|
|
const explicit_text = try eatToken(tokenizer, Token.Id.TagContent);
|
|
|
|
_ = try eatToken(tokenizer, Token.Id.BracketClose);
|
|
|
|
break :blk tokenizer.buffer[explicit_text.start..explicit_text.end];
|
|
|
|
},
|
2019-12-09 05:18:01 +00:00
|
|
|
else => return parseError(tokenizer, tok, "invalid link token", .{}),
|
2018-01-23 04:06:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
try nodes.append(Node{
|
|
|
|
.Link = Link{
|
2018-01-23 04:06:07 +00:00
|
|
|
.url = try urlize(allocator, url_name),
|
|
|
|
.name = name,
|
|
|
|
.token = name_tok,
|
|
|
|
},
|
|
|
|
});
|
2018-01-17 06:50:35 +00:00
|
|
|
} else if (mem.eql(u8, tag_name, "code_begin")) {
|
|
|
|
_ = try eatToken(tokenizer, Token.Id.Separator);
|
|
|
|
const code_kind_tok = try eatToken(tokenizer, Token.Id.TagContent);
|
|
|
|
var name: []const u8 = "test";
|
|
|
|
const maybe_sep = tokenizer.next();
|
|
|
|
switch (maybe_sep.id) {
|
|
|
|
Token.Id.Separator => {
|
|
|
|
const name_tok = try eatToken(tokenizer, Token.Id.TagContent);
|
|
|
|
name = tokenizer.buffer[name_tok.start..name_tok.end];
|
|
|
|
_ = try eatToken(tokenizer, Token.Id.BracketClose);
|
|
|
|
},
|
|
|
|
Token.Id.BracketClose => {},
|
2019-12-09 05:18:01 +00:00
|
|
|
else => return parseError(tokenizer, token, "invalid token", .{}),
|
2018-01-17 06:50:35 +00:00
|
|
|
}
|
|
|
|
const code_kind_str = tokenizer.buffer[code_kind_tok.start..code_kind_tok.end];
|
|
|
|
var code_kind_id: Code.Id = undefined;
|
2018-01-19 08:03:20 +00:00
|
|
|
var is_inline = false;
|
2018-01-17 06:50:35 +00:00
|
|
|
if (mem.eql(u8, code_kind_str, "exe")) {
|
2018-11-13 13:08:37 +00:00
|
|
|
code_kind_id = Code.Id{ .Exe = ExpectedOutcome.Succeed };
|
2018-01-19 08:03:20 +00:00
|
|
|
} else if (mem.eql(u8, code_kind_str, "exe_err")) {
|
2018-11-13 13:08:37 +00:00
|
|
|
code_kind_id = Code.Id{ .Exe = ExpectedOutcome.Fail };
|
2019-05-01 19:34:17 +00:00
|
|
|
} else if (mem.eql(u8, code_kind_str, "exe_build_err")) {
|
|
|
|
code_kind_id = Code.Id{ .Exe = ExpectedOutcome.BuildFail };
|
2018-01-17 06:50:35 +00:00
|
|
|
} else if (mem.eql(u8, code_kind_str, "test")) {
|
|
|
|
code_kind_id = Code.Id.Test;
|
2018-01-19 08:03:20 +00:00
|
|
|
} else if (mem.eql(u8, code_kind_str, "test_err")) {
|
2018-11-13 13:08:37 +00:00
|
|
|
code_kind_id = Code.Id{ .TestError = name };
|
2018-01-19 08:03:20 +00:00
|
|
|
name = "test";
|
|
|
|
} else if (mem.eql(u8, code_kind_str, "test_safety")) {
|
2018-11-13 13:08:37 +00:00
|
|
|
code_kind_id = Code.Id{ .TestSafety = name };
|
2018-01-19 08:03:20 +00:00
|
|
|
name = "test";
|
|
|
|
} else if (mem.eql(u8, code_kind_str, "obj")) {
|
2018-11-13 13:08:37 +00:00
|
|
|
code_kind_id = Code.Id{ .Obj = null };
|
2018-01-23 03:24:07 +00:00
|
|
|
} else if (mem.eql(u8, code_kind_str, "obj_err")) {
|
2018-11-13 13:08:37 +00:00
|
|
|
code_kind_id = Code.Id{ .Obj = name };
|
2018-01-23 03:24:07 +00:00
|
|
|
name = "test";
|
2019-05-16 18:54:05 +00:00
|
|
|
} else if (mem.eql(u8, code_kind_str, "lib")) {
|
|
|
|
code_kind_id = Code.Id.Lib;
|
2018-01-19 08:03:20 +00:00
|
|
|
} else if (mem.eql(u8, code_kind_str, "syntax")) {
|
2018-11-13 13:08:37 +00:00
|
|
|
code_kind_id = Code.Id{ .Obj = null };
|
2018-01-19 08:03:20 +00:00
|
|
|
is_inline = true;
|
2018-01-17 06:50:35 +00:00
|
|
|
} else {
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code_kind_tok, "unrecognized code kind: {}", .{code_kind_str});
|
2018-01-17 06:50:35 +00:00
|
|
|
}
|
2018-01-19 08:03:20 +00:00
|
|
|
|
2020-02-27 18:32:49 +00:00
|
|
|
var mode: builtin.Mode = .Debug;
|
2018-01-19 08:03:20 +00:00
|
|
|
var link_objects = std.ArrayList([]const u8).init(allocator);
|
|
|
|
defer link_objects.deinit();
|
2019-03-20 23:00:23 +00:00
|
|
|
var target_str: ?[]const u8 = null;
|
2018-01-19 08:03:20 +00:00
|
|
|
var link_libc = false;
|
2020-09-27 04:03:38 +00:00
|
|
|
var disable_cache = false;
|
2018-01-19 08:03:20 +00:00
|
|
|
|
|
|
|
const source_token = while (true) {
|
|
|
|
const content_tok = try eatToken(tokenizer, Token.Id.Content);
|
|
|
|
_ = try eatToken(tokenizer, Token.Id.BracketOpen);
|
|
|
|
const end_code_tag = try eatToken(tokenizer, Token.Id.TagContent);
|
|
|
|
const end_tag_name = tokenizer.buffer[end_code_tag.start..end_code_tag.end];
|
|
|
|
if (mem.eql(u8, end_tag_name, "code_release_fast")) {
|
2020-02-27 18:32:49 +00:00
|
|
|
mode = .ReleaseFast;
|
2019-09-29 21:28:30 +00:00
|
|
|
} else if (mem.eql(u8, end_tag_name, "code_release_safe")) {
|
2020-02-27 18:32:49 +00:00
|
|
|
mode = .ReleaseSafe;
|
2020-09-27 04:03:38 +00:00
|
|
|
} else if (mem.eql(u8, end_tag_name, "code_disable_cache")) {
|
|
|
|
disable_cache = true;
|
2018-01-19 08:03:20 +00:00
|
|
|
} else if (mem.eql(u8, end_tag_name, "code_link_object")) {
|
|
|
|
_ = try eatToken(tokenizer, Token.Id.Separator);
|
|
|
|
const obj_tok = try eatToken(tokenizer, Token.Id.TagContent);
|
|
|
|
try link_objects.append(tokenizer.buffer[obj_tok.start..obj_tok.end]);
|
|
|
|
} else if (mem.eql(u8, end_tag_name, "target_windows")) {
|
2019-03-20 23:00:23 +00:00
|
|
|
target_str = "x86_64-windows";
|
|
|
|
} else if (mem.eql(u8, end_tag_name, "target_linux_x86_64")) {
|
|
|
|
target_str = "x86_64-linux";
|
2019-09-29 18:00:05 +00:00
|
|
|
} else if (mem.eql(u8, end_tag_name, "target_linux_riscv64")) {
|
|
|
|
target_str = "riscv64-linux";
|
2019-05-01 22:48:26 +00:00
|
|
|
} else if (mem.eql(u8, end_tag_name, "target_wasm")) {
|
|
|
|
target_str = "wasm32-freestanding";
|
|
|
|
} else if (mem.eql(u8, end_tag_name, "target_wasi")) {
|
|
|
|
target_str = "wasm32-wasi";
|
2018-01-19 08:03:20 +00:00
|
|
|
} else if (mem.eql(u8, end_tag_name, "link_libc")) {
|
|
|
|
link_libc = true;
|
|
|
|
} else if (mem.eql(u8, end_tag_name, "code_end")) {
|
|
|
|
_ = try eatToken(tokenizer, Token.Id.BracketClose);
|
|
|
|
break content_tok;
|
|
|
|
} else {
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(
|
|
|
|
tokenizer,
|
|
|
|
end_code_tag,
|
|
|
|
"invalid token inside code_begin: {}",
|
|
|
|
.{end_tag_name},
|
|
|
|
);
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
|
|
|
_ = try eatToken(tokenizer, Token.Id.BracketClose);
|
2018-05-26 22:16:39 +00:00
|
|
|
} else
|
|
|
|
unreachable; // TODO issue #707
|
2018-11-13 13:08:37 +00:00
|
|
|
try nodes.append(Node{
|
|
|
|
.Code = Code{
|
2018-05-26 22:16:39 +00:00
|
|
|
.id = code_kind_id,
|
|
|
|
.name = name,
|
|
|
|
.source_token = source_token,
|
|
|
|
.is_inline = is_inline,
|
|
|
|
.mode = mode,
|
|
|
|
.link_objects = link_objects.toOwnedSlice(),
|
2019-03-20 23:00:23 +00:00
|
|
|
.target_str = target_str,
|
2018-05-26 22:16:39 +00:00
|
|
|
.link_libc = link_libc,
|
2020-09-27 04:03:38 +00:00
|
|
|
.disable_cache = disable_cache,
|
2018-05-26 22:16:39 +00:00
|
|
|
},
|
|
|
|
});
|
2018-01-19 08:03:20 +00:00
|
|
|
tokenizer.code_node_count += 1;
|
2018-09-14 14:35:03 +00:00
|
|
|
} else if (mem.eql(u8, tag_name, "syntax")) {
|
|
|
|
_ = try eatToken(tokenizer, Token.Id.BracketClose);
|
|
|
|
const content_tok = try eatToken(tokenizer, Token.Id.Content);
|
|
|
|
_ = try eatToken(tokenizer, Token.Id.BracketOpen);
|
|
|
|
const end_syntax_tag = try eatToken(tokenizer, Token.Id.TagContent);
|
|
|
|
const end_tag_name = tokenizer.buffer[end_syntax_tag.start..end_syntax_tag.end];
|
|
|
|
if (!mem.eql(u8, end_tag_name, "endsyntax")) {
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(
|
|
|
|
tokenizer,
|
|
|
|
end_syntax_tag,
|
|
|
|
"invalid token inside syntax: {}",
|
|
|
|
.{end_tag_name},
|
|
|
|
);
|
2018-09-14 14:35:03 +00:00
|
|
|
}
|
|
|
|
_ = try eatToken(tokenizer, Token.Id.BracketClose);
|
2018-11-13 13:08:37 +00:00
|
|
|
try nodes.append(Node{ .Syntax = content_tok });
|
2018-01-17 05:22:33 +00:00
|
|
|
} else {
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, tag_token, "unrecognized tag name: {}", .{tag_name});
|
2018-01-17 04:19:05 +00:00
|
|
|
}
|
|
|
|
},
|
2019-12-09 05:18:01 +00:00
|
|
|
else => return parseError(tokenizer, token, "invalid token", .{}),
|
2018-01-17 04:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
return Toc{
|
2018-01-17 04:19:05 +00:00
|
|
|
.nodes = nodes.toOwnedSlice(),
|
|
|
|
.toc = toc_buf.toOwnedSlice(),
|
2018-01-17 05:22:33 +00:00
|
|
|
.urls = urls,
|
2018-01-17 04:19:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
fn urlize(allocator: *mem.Allocator, input: []const u8) ![]u8 {
|
2020-03-08 07:16:10 +00:00
|
|
|
var buf = std.ArrayList(u8).init(allocator);
|
2018-01-17 04:19:05 +00:00
|
|
|
defer buf.deinit();
|
|
|
|
|
2020-12-23 11:01:29 +00:00
|
|
|
const out = buf.writer();
|
2018-01-17 04:19:05 +00:00
|
|
|
for (input) |c| {
|
|
|
|
switch (c) {
|
2019-04-05 21:32:19 +00:00
|
|
|
'a'...'z', 'A'...'Z', '_', '-', '0'...'9' => {
|
2018-01-17 04:19:05 +00:00
|
|
|
try out.writeByte(c);
|
|
|
|
},
|
|
|
|
' ' => {
|
|
|
|
try out.writeByte('-');
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.toOwnedSlice();
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
fn escapeHtml(allocator: *mem.Allocator, input: []const u8) ![]u8 {
|
2020-03-08 07:16:10 +00:00
|
|
|
var buf = std.ArrayList(u8).init(allocator);
|
2018-01-17 06:50:35 +00:00
|
|
|
defer buf.deinit();
|
|
|
|
|
2020-12-23 11:01:29 +00:00
|
|
|
const out = buf.writer();
|
2018-09-12 21:26:51 +00:00
|
|
|
try writeEscaped(out, input);
|
|
|
|
return buf.toOwnedSlice();
|
|
|
|
}
|
|
|
|
|
2020-07-11 19:04:38 +00:00
|
|
|
fn writeEscaped(out: anytype, input: []const u8) !void {
|
2018-01-17 06:50:35 +00:00
|
|
|
for (input) |c| {
|
|
|
|
try switch (c) {
|
2020-03-11 00:22:30 +00:00
|
|
|
'&' => out.writeAll("&"),
|
|
|
|
'<' => out.writeAll("<"),
|
|
|
|
'>' => out.writeAll(">"),
|
|
|
|
'"' => out.writeAll("""),
|
2018-01-17 06:50:35 +00:00
|
|
|
else => out.writeByte(c),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 08:03:20 +00:00
|
|
|
//#define VT_RED "\x1b[31;1m"
|
|
|
|
//#define VT_GREEN "\x1b[32;1m"
|
|
|
|
//#define VT_CYAN "\x1b[36;1m"
|
|
|
|
//#define VT_WHITE "\x1b[37;1m"
|
|
|
|
//#define VT_BOLD "\x1b[0;1m"
|
|
|
|
//#define VT_RESET "\x1b[0m"
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const TermState = enum {
|
2018-01-19 08:03:20 +00:00
|
|
|
Start,
|
|
|
|
Escape,
|
|
|
|
LBracket,
|
|
|
|
Number,
|
|
|
|
AfterNumber,
|
|
|
|
Arg,
|
|
|
|
ArgNumber,
|
|
|
|
ExpectEnd,
|
|
|
|
};
|
|
|
|
|
|
|
|
test "term color" {
|
|
|
|
const input_bytes = "A\x1b[32;1mgreen\x1b[0mB";
|
2020-01-30 03:22:01 +00:00
|
|
|
const result = try termColor(std.testing.allocator, input_bytes);
|
|
|
|
defer std.testing.allocator.free(result);
|
2019-02-09 00:37:59 +00:00
|
|
|
testing.expectEqualSlices(u8, "A<span class=\"t32\">green</span>B", result);
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
fn termColor(allocator: *mem.Allocator, input: []const u8) ![]u8 {
|
2020-03-08 07:16:10 +00:00
|
|
|
var buf = std.ArrayList(u8).init(allocator);
|
2018-01-19 08:03:20 +00:00
|
|
|
defer buf.deinit();
|
|
|
|
|
2020-12-23 11:01:29 +00:00
|
|
|
var out = buf.writer();
|
2018-01-19 08:03:20 +00:00
|
|
|
var number_start_index: usize = undefined;
|
|
|
|
var first_number: usize = undefined;
|
|
|
|
var second_number: usize = undefined;
|
|
|
|
var i: usize = 0;
|
|
|
|
var state = TermState.Start;
|
|
|
|
var open_span_count: usize = 0;
|
|
|
|
while (i < input.len) : (i += 1) {
|
|
|
|
const c = input[i];
|
|
|
|
switch (state) {
|
|
|
|
TermState.Start => switch (c) {
|
|
|
|
'\x1b' => state = TermState.Escape,
|
|
|
|
else => try out.writeByte(c),
|
|
|
|
},
|
|
|
|
TermState.Escape => switch (c) {
|
|
|
|
'[' => state = TermState.LBracket,
|
|
|
|
else => return error.UnsupportedEscape,
|
|
|
|
},
|
|
|
|
TermState.LBracket => switch (c) {
|
|
|
|
'0'...'9' => {
|
|
|
|
number_start_index = i;
|
|
|
|
state = TermState.Number;
|
|
|
|
},
|
|
|
|
else => return error.UnsupportedEscape,
|
|
|
|
},
|
|
|
|
TermState.Number => switch (c) {
|
|
|
|
'0'...'9' => {},
|
|
|
|
else => {
|
|
|
|
first_number = std.fmt.parseInt(usize, input[number_start_index..i], 10) catch unreachable;
|
|
|
|
second_number = 0;
|
|
|
|
state = TermState.AfterNumber;
|
|
|
|
i -= 1;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
TermState.AfterNumber => switch (c) {
|
|
|
|
';' => state = TermState.Arg,
|
|
|
|
else => {
|
|
|
|
state = TermState.ExpectEnd;
|
|
|
|
i -= 1;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TermState.Arg => switch (c) {
|
|
|
|
'0'...'9' => {
|
|
|
|
number_start_index = i;
|
|
|
|
state = TermState.ArgNumber;
|
|
|
|
},
|
|
|
|
else => return error.UnsupportedEscape,
|
|
|
|
},
|
|
|
|
TermState.ArgNumber => switch (c) {
|
|
|
|
'0'...'9' => {},
|
|
|
|
else => {
|
|
|
|
second_number = std.fmt.parseInt(usize, input[number_start_index..i], 10) catch unreachable;
|
|
|
|
state = TermState.ExpectEnd;
|
|
|
|
i -= 1;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TermState.ExpectEnd => switch (c) {
|
|
|
|
'm' => {
|
|
|
|
state = TermState.Start;
|
|
|
|
while (open_span_count != 0) : (open_span_count -= 1) {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</span>");
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
|
|
|
if (first_number != 0 or second_number != 0) {
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("<span class=\"t{}_{}\">", .{ first_number, second_number });
|
2018-01-19 08:03:20 +00:00
|
|
|
open_span_count += 1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => return error.UnsupportedEscape,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.toOwnedSlice();
|
|
|
|
}
|
|
|
|
|
2019-06-09 23:24:24 +00:00
|
|
|
const builtin_types = [_][]const u8{
|
2019-04-03 21:39:07 +00:00
|
|
|
"f16", "f32", "f64", "f128", "c_longdouble", "c_short",
|
|
|
|
"c_ushort", "c_int", "c_uint", "c_long", "c_ulong", "c_longlong",
|
|
|
|
"c_ulonglong", "c_char", "c_void", "void", "bool", "isize",
|
|
|
|
"usize", "noreturn", "type", "anyerror", "comptime_int", "comptime_float",
|
2018-09-12 21:26:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fn isType(name: []const u8) bool {
|
|
|
|
for (builtin_types) |t| {
|
|
|
|
if (mem.eql(u8, t, name))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-11 19:04:38 +00:00
|
|
|
fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: anytype, source_token: Token, raw_src: []const u8) !void {
|
2018-09-14 14:35:03 +00:00
|
|
|
const src = mem.trim(u8, raw_src, " \n");
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<code class=\"zig\">");
|
2018-09-12 21:26:51 +00:00
|
|
|
var tokenizer = std.zig.Tokenizer.init(src);
|
|
|
|
var index: usize = 0;
|
|
|
|
var next_tok_is_fn = false;
|
|
|
|
while (true) {
|
|
|
|
const prev_tok_was_fn = next_tok_is_fn;
|
|
|
|
next_tok_is_fn = false;
|
|
|
|
|
|
|
|
const token = tokenizer.next();
|
2020-05-24 00:40:09 +00:00
|
|
|
try writeEscaped(out, src[index..token.loc.start]);
|
2018-09-12 21:26:51 +00:00
|
|
|
switch (token.id) {
|
2019-07-19 21:54:06 +00:00
|
|
|
.Eof => break,
|
|
|
|
|
|
|
|
.Keyword_align,
|
|
|
|
.Keyword_and,
|
|
|
|
.Keyword_asm,
|
|
|
|
.Keyword_async,
|
|
|
|
.Keyword_await,
|
|
|
|
.Keyword_break,
|
|
|
|
.Keyword_catch,
|
|
|
|
.Keyword_comptime,
|
|
|
|
.Keyword_const,
|
|
|
|
.Keyword_continue,
|
|
|
|
.Keyword_defer,
|
|
|
|
.Keyword_else,
|
|
|
|
.Keyword_enum,
|
|
|
|
.Keyword_errdefer,
|
|
|
|
.Keyword_error,
|
|
|
|
.Keyword_export,
|
|
|
|
.Keyword_extern,
|
|
|
|
.Keyword_for,
|
|
|
|
.Keyword_if,
|
|
|
|
.Keyword_inline,
|
|
|
|
.Keyword_noalias,
|
2019-09-06 01:55:07 +00:00
|
|
|
.Keyword_noinline,
|
2020-05-05 11:08:46 +00:00
|
|
|
.Keyword_nosuspend,
|
2020-09-26 15:05:11 +00:00
|
|
|
.Keyword_opaque,
|
2019-07-19 21:54:06 +00:00
|
|
|
.Keyword_or,
|
|
|
|
.Keyword_orelse,
|
|
|
|
.Keyword_packed,
|
2019-08-11 23:53:10 +00:00
|
|
|
.Keyword_anyframe,
|
2019-07-19 21:54:06 +00:00
|
|
|
.Keyword_pub,
|
|
|
|
.Keyword_resume,
|
|
|
|
.Keyword_return,
|
|
|
|
.Keyword_linksection,
|
2019-12-23 20:52:06 +00:00
|
|
|
.Keyword_callconv,
|
2019-07-19 21:54:06 +00:00
|
|
|
.Keyword_struct,
|
|
|
|
.Keyword_suspend,
|
|
|
|
.Keyword_switch,
|
|
|
|
.Keyword_test,
|
|
|
|
.Keyword_threadlocal,
|
|
|
|
.Keyword_try,
|
|
|
|
.Keyword_union,
|
|
|
|
.Keyword_unreachable,
|
|
|
|
.Keyword_usingnamespace,
|
|
|
|
.Keyword_var,
|
|
|
|
.Keyword_volatile,
|
|
|
|
.Keyword_allowzero,
|
|
|
|
.Keyword_while,
|
2020-07-11 19:04:38 +00:00
|
|
|
.Keyword_anytype,
|
2018-09-12 21:26:51 +00:00
|
|
|
=> {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<span class=\"tok-kw\">");
|
2020-05-24 00:40:09 +00:00
|
|
|
try writeEscaped(out, src[token.loc.start..token.loc.end]);
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</span>");
|
2018-09-12 21:26:51 +00:00
|
|
|
},
|
|
|
|
|
2019-07-19 21:54:06 +00:00
|
|
|
.Keyword_fn => {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<span class=\"tok-kw\">");
|
2020-05-24 00:40:09 +00:00
|
|
|
try writeEscaped(out, src[token.loc.start..token.loc.end]);
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</span>");
|
2018-09-12 21:26:51 +00:00
|
|
|
next_tok_is_fn = true;
|
|
|
|
},
|
|
|
|
|
2019-07-19 21:54:06 +00:00
|
|
|
.Keyword_undefined,
|
|
|
|
.Keyword_null,
|
|
|
|
.Keyword_true,
|
|
|
|
.Keyword_false,
|
2018-09-12 21:26:51 +00:00
|
|
|
=> {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<span class=\"tok-null\">");
|
2020-05-24 00:40:09 +00:00
|
|
|
try writeEscaped(out, src[token.loc.start..token.loc.end]);
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</span>");
|
2018-09-12 21:26:51 +00:00
|
|
|
},
|
|
|
|
|
2019-07-19 21:54:06 +00:00
|
|
|
.StringLiteral,
|
|
|
|
.MultilineStringLiteralLine,
|
|
|
|
.CharLiteral,
|
2018-09-12 21:26:51 +00:00
|
|
|
=> {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<span class=\"tok-str\">");
|
2020-05-24 00:40:09 +00:00
|
|
|
try writeEscaped(out, src[token.loc.start..token.loc.end]);
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</span>");
|
2018-09-12 21:26:51 +00:00
|
|
|
},
|
|
|
|
|
2019-07-19 21:54:06 +00:00
|
|
|
.Builtin => {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<span class=\"tok-builtin\">");
|
2020-05-24 00:40:09 +00:00
|
|
|
try writeEscaped(out, src[token.loc.start..token.loc.end]);
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</span>");
|
2018-09-12 21:26:51 +00:00
|
|
|
},
|
|
|
|
|
2019-07-19 21:54:06 +00:00
|
|
|
.LineComment,
|
|
|
|
.DocComment,
|
2019-11-15 16:44:29 +00:00
|
|
|
.ContainerDocComment,
|
2019-07-19 21:54:06 +00:00
|
|
|
.ShebangLine,
|
2018-09-12 21:26:51 +00:00
|
|
|
=> {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<span class=\"tok-comment\">");
|
2020-05-24 00:40:09 +00:00
|
|
|
try writeEscaped(out, src[token.loc.start..token.loc.end]);
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</span>");
|
2018-09-12 21:26:51 +00:00
|
|
|
},
|
|
|
|
|
2019-07-19 21:54:06 +00:00
|
|
|
.Identifier => {
|
2018-09-12 21:26:51 +00:00
|
|
|
if (prev_tok_was_fn) {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<span class=\"tok-fn\">");
|
2020-05-24 00:40:09 +00:00
|
|
|
try writeEscaped(out, src[token.loc.start..token.loc.end]);
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</span>");
|
2018-09-12 21:26:51 +00:00
|
|
|
} else {
|
|
|
|
const is_int = blk: {
|
2020-05-24 00:40:09 +00:00
|
|
|
if (src[token.loc.start] != 'i' and src[token.loc.start] != 'u')
|
2018-09-12 21:26:51 +00:00
|
|
|
break :blk false;
|
2020-05-24 00:40:09 +00:00
|
|
|
var i = token.loc.start + 1;
|
|
|
|
if (i == token.loc.end)
|
2018-09-12 21:26:51 +00:00
|
|
|
break :blk false;
|
2020-05-24 00:40:09 +00:00
|
|
|
while (i != token.loc.end) : (i += 1) {
|
2018-09-12 21:26:51 +00:00
|
|
|
if (src[i] < '0' or src[i] > '9')
|
|
|
|
break :blk false;
|
|
|
|
}
|
|
|
|
break :blk true;
|
|
|
|
};
|
2020-05-24 00:40:09 +00:00
|
|
|
if (is_int or isType(src[token.loc.start..token.loc.end])) {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<span class=\"tok-type\">");
|
2020-05-24 00:40:09 +00:00
|
|
|
try writeEscaped(out, src[token.loc.start..token.loc.end]);
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</span>");
|
2018-09-12 21:26:51 +00:00
|
|
|
} else {
|
2020-05-24 00:40:09 +00:00
|
|
|
try writeEscaped(out, src[token.loc.start..token.loc.end]);
|
2018-09-12 21:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-07-19 21:54:06 +00:00
|
|
|
.IntegerLiteral,
|
|
|
|
.FloatLiteral,
|
2018-09-12 21:26:51 +00:00
|
|
|
=> {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<span class=\"tok-number\">");
|
2020-05-24 00:40:09 +00:00
|
|
|
try writeEscaped(out, src[token.loc.start..token.loc.end]);
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</span>");
|
2018-09-12 21:26:51 +00:00
|
|
|
},
|
|
|
|
|
2019-07-19 21:54:06 +00:00
|
|
|
.Bang,
|
|
|
|
.Pipe,
|
|
|
|
.PipePipe,
|
|
|
|
.PipeEqual,
|
|
|
|
.Equal,
|
|
|
|
.EqualEqual,
|
|
|
|
.EqualAngleBracketRight,
|
|
|
|
.BangEqual,
|
|
|
|
.LParen,
|
|
|
|
.RParen,
|
|
|
|
.Semicolon,
|
|
|
|
.Percent,
|
|
|
|
.PercentEqual,
|
|
|
|
.LBrace,
|
|
|
|
.RBrace,
|
|
|
|
.LBracket,
|
|
|
|
.RBracket,
|
|
|
|
.Period,
|
2019-11-01 04:53:46 +00:00
|
|
|
.PeriodAsterisk,
|
2019-07-19 21:54:06 +00:00
|
|
|
.Ellipsis2,
|
|
|
|
.Ellipsis3,
|
|
|
|
.Caret,
|
|
|
|
.CaretEqual,
|
|
|
|
.Plus,
|
|
|
|
.PlusPlus,
|
|
|
|
.PlusEqual,
|
|
|
|
.PlusPercent,
|
|
|
|
.PlusPercentEqual,
|
|
|
|
.Minus,
|
|
|
|
.MinusEqual,
|
|
|
|
.MinusPercent,
|
|
|
|
.MinusPercentEqual,
|
|
|
|
.Asterisk,
|
|
|
|
.AsteriskEqual,
|
|
|
|
.AsteriskAsterisk,
|
|
|
|
.AsteriskPercent,
|
|
|
|
.AsteriskPercentEqual,
|
|
|
|
.Arrow,
|
|
|
|
.Colon,
|
|
|
|
.Slash,
|
|
|
|
.SlashEqual,
|
|
|
|
.Comma,
|
|
|
|
.Ampersand,
|
|
|
|
.AmpersandEqual,
|
|
|
|
.QuestionMark,
|
|
|
|
.AngleBracketLeft,
|
|
|
|
.AngleBracketLeftEqual,
|
|
|
|
.AngleBracketAngleBracketLeft,
|
|
|
|
.AngleBracketAngleBracketLeftEqual,
|
|
|
|
.AngleBracketRight,
|
|
|
|
.AngleBracketRightEqual,
|
|
|
|
.AngleBracketAngleBracketRight,
|
|
|
|
.AngleBracketAngleBracketRightEqual,
|
|
|
|
.Tilde,
|
2020-05-24 00:40:09 +00:00
|
|
|
=> try writeEscaped(out, src[token.loc.start..token.loc.end]),
|
2018-09-14 14:35:03 +00:00
|
|
|
|
2020-10-29 18:59:58 +00:00
|
|
|
.Invalid, .Invalid_ampersands, .Invalid_periodasterisks => return parseError(
|
2018-09-14 14:35:03 +00:00
|
|
|
docgen_tokenizer,
|
|
|
|
source_token,
|
|
|
|
"syntax error",
|
2019-12-09 05:18:01 +00:00
|
|
|
.{},
|
2018-09-14 14:35:03 +00:00
|
|
|
),
|
2018-09-12 21:26:51 +00:00
|
|
|
}
|
2020-05-24 00:40:09 +00:00
|
|
|
index = token.loc.end;
|
2018-09-12 21:26:51 +00:00
|
|
|
}
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</code>");
|
2018-09-12 21:26:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 19:04:38 +00:00
|
|
|
fn tokenizeAndPrint(docgen_tokenizer: *Tokenizer, out: anytype, source_token: Token) !void {
|
2018-09-25 16:03:09 +00:00
|
|
|
const raw_src = docgen_tokenizer.buffer[source_token.start..source_token.end];
|
|
|
|
return tokenizeAndPrintRaw(docgen_tokenizer, out, source_token, raw_src);
|
|
|
|
}
|
|
|
|
|
2020-07-11 19:04:38 +00:00
|
|
|
fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: anytype, zig_exe: []const u8) !void {
|
2018-01-19 08:03:20 +00:00
|
|
|
var code_progress_index: usize = 0;
|
2018-06-07 23:10:45 +00:00
|
|
|
|
2019-05-26 17:17:34 +00:00
|
|
|
var env_map = try process.getEnvMap(allocator);
|
2018-07-09 16:12:37 +00:00
|
|
|
try env_map.set("ZIG_DEBUG_COLOR", "1");
|
|
|
|
|
2018-09-25 16:03:09 +00:00
|
|
|
const builtin_code = try getBuiltinCode(allocator, &env_map, zig_exe);
|
2018-06-07 23:10:45 +00:00
|
|
|
|
2018-01-17 04:19:05 +00:00
|
|
|
for (toc.nodes) |node| {
|
|
|
|
switch (node) {
|
2020-02-27 18:32:49 +00:00
|
|
|
.Content => |data| {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll(data);
|
2018-01-17 04:19:05 +00:00
|
|
|
},
|
2020-02-27 18:32:49 +00:00
|
|
|
.Link => |info| {
|
2018-01-23 04:06:07 +00:00
|
|
|
if (!toc.urls.contains(info.url)) {
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, info.token, "url not found: {}", .{info.url});
|
2018-01-23 04:06:07 +00:00
|
|
|
}
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("<a href=\"#{}\">{}</a>", .{ info.url, info.name });
|
2018-01-23 04:06:07 +00:00
|
|
|
},
|
2020-02-27 18:32:49 +00:00
|
|
|
.Nav => {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll(toc.toc);
|
2018-01-17 04:19:05 +00:00
|
|
|
},
|
2020-02-27 18:32:49 +00:00
|
|
|
.Builtin => |tok| {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<pre>");
|
2018-09-25 16:03:09 +00:00
|
|
|
try tokenizeAndPrintRaw(tokenizer, out, tok, builtin_code);
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</pre>");
|
2018-06-07 23:10:45 +00:00
|
|
|
},
|
2020-02-27 18:32:49 +00:00
|
|
|
.HeaderOpen => |info| {
|
2019-04-03 21:39:07 +00:00
|
|
|
try out.print(
|
2019-04-06 03:12:25 +00:00
|
|
|
"<h{} id=\"{}\"><a href=\"#toc-{}\">{}</a> <a class=\"hdr\" href=\"#{}\">§</a></h{}>\n",
|
2019-12-09 05:18:01 +00:00
|
|
|
.{ info.n, info.url, info.url, info.name, info.url, info.n },
|
2019-04-03 21:39:07 +00:00
|
|
|
);
|
2017-11-07 08:22:27 +00:00
|
|
|
},
|
2020-02-27 18:32:49 +00:00
|
|
|
.SeeAlso => |items| {
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<p>See also:</p><ul>\n");
|
2018-01-17 05:22:33 +00:00
|
|
|
for (items) |item| {
|
|
|
|
const url = try urlize(allocator, item.name);
|
|
|
|
if (!toc.urls.contains(url)) {
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, item.token, "url not found: {}", .{url});
|
2018-01-17 05:22:33 +00:00
|
|
|
}
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("<li><a href=\"#{}\">{}</a></li>\n", .{ url, item.name });
|
2018-01-17 05:22:33 +00:00
|
|
|
}
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</ul>\n");
|
2018-01-17 05:22:33 +00:00
|
|
|
},
|
2020-02-27 18:32:49 +00:00
|
|
|
.Syntax => |content_tok| {
|
2018-09-25 16:03:09 +00:00
|
|
|
try tokenizeAndPrint(tokenizer, out, content_tok);
|
2018-09-14 14:35:03 +00:00
|
|
|
},
|
2020-02-27 18:32:49 +00:00
|
|
|
.Code => |code| {
|
2018-01-19 08:03:20 +00:00
|
|
|
code_progress_index += 1;
|
2020-09-27 04:03:38 +00:00
|
|
|
print("docgen example code {}/{}...", .{ code_progress_index, tokenizer.code_node_count });
|
2018-01-19 08:03:20 +00:00
|
|
|
|
2018-01-17 06:50:35 +00:00
|
|
|
const raw_source = tokenizer.buffer[code.source_token.start..code.source_token.end];
|
|
|
|
const trimmed_raw_source = mem.trim(u8, raw_source, " \n");
|
2018-01-19 08:03:20 +00:00
|
|
|
if (!code.is_inline) {
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("<p class=\"file\">{}.zig</p>", .{code.name});
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("<pre>");
|
2018-09-25 16:03:09 +00:00
|
|
|
try tokenizeAndPrint(tokenizer, out, code.source_token);
|
2020-03-11 00:22:30 +00:00
|
|
|
try out.writeAll("</pre>");
|
2019-12-09 05:18:01 +00:00
|
|
|
const name_plus_ext = try std.fmt.allocPrint(allocator, "{}.zig", .{code.name});
|
2019-05-26 17:17:34 +00:00
|
|
|
const tmp_source_file_name = try fs.path.join(
|
2019-02-07 05:42:41 +00:00
|
|
|
allocator,
|
2019-12-02 02:27:55 +00:00
|
|
|
&[_][]const u8{ tmp_dir_name, name_plus_ext },
|
2019-02-07 05:42:41 +00:00
|
|
|
);
|
2020-03-30 18:23:22 +00:00
|
|
|
try fs.cwd().writeFile(tmp_source_file_name, trimmed_raw_source);
|
2018-05-26 22:16:39 +00:00
|
|
|
|
2018-01-17 06:50:35 +00:00
|
|
|
switch (code.id) {
|
2019-03-20 23:00:23 +00:00
|
|
|
Code.Id.Exe => |expected_outcome| code_block: {
|
2019-12-09 05:18:01 +00:00
|
|
|
const name_plus_bin_ext = try std.fmt.allocPrint(allocator, "{}{}", .{ code.name, exe_ext });
|
2018-01-19 08:03:20 +00:00
|
|
|
var build_args = std.ArrayList([]const u8).init(allocator);
|
|
|
|
defer build_args.deinit();
|
2019-12-02 02:27:55 +00:00
|
|
|
try build_args.appendSlice(&[_][]const u8{
|
2020-09-27 04:03:38 +00:00
|
|
|
zig_exe, "build-exe",
|
|
|
|
"--name", code.name,
|
|
|
|
"--color", "on",
|
|
|
|
"--enable-cache", tmp_source_file_name,
|
2018-01-19 08:03:20 +00:00
|
|
|
});
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("<pre><code class=\"shell\">$ zig build-exe {}.zig", .{code.name});
|
2018-01-19 08:03:20 +00:00
|
|
|
switch (code.mode) {
|
2020-02-27 18:32:49 +00:00
|
|
|
.Debug => {},
|
2020-09-27 04:03:38 +00:00
|
|
|
else => {
|
|
|
|
try build_args.appendSlice(&[_][]const u8{ "-O", @tagName(code.mode) });
|
|
|
|
try out.print(" -O {s}", .{@tagName(code.mode)});
|
2018-04-16 01:18:52 +00:00
|
|
|
},
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
|
|
|
for (code.link_objects) |link_object| {
|
2019-12-09 05:18:01 +00:00
|
|
|
const name_with_ext = try std.fmt.allocPrint(allocator, "{}{}", .{ link_object, obj_ext });
|
2019-05-26 17:17:34 +00:00
|
|
|
const full_path_object = try fs.path.join(
|
2019-02-07 05:42:41 +00:00
|
|
|
allocator,
|
2019-12-02 02:27:55 +00:00
|
|
|
&[_][]const u8{ tmp_dir_name, name_with_ext },
|
2019-02-07 05:42:41 +00:00
|
|
|
);
|
2018-01-19 08:03:20 +00:00
|
|
|
try build_args.append(full_path_object);
|
2020-09-27 04:03:38 +00:00
|
|
|
try out.print(" {s}", .{name_with_ext});
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
|
|
|
if (code.link_libc) {
|
2019-09-24 15:36:59 +00:00
|
|
|
try build_args.append("-lc");
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print(" -lc", .{});
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
2020-03-14 04:46:40 +00:00
|
|
|
const target = try std.zig.CrossTarget.parse(.{
|
|
|
|
.arch_os_abi = code.target_str orelse "native",
|
|
|
|
});
|
2019-03-20 23:00:23 +00:00
|
|
|
if (code.target_str) |triple| {
|
2019-12-02 02:27:55 +00:00
|
|
|
try build_args.appendSlice(&[_][]const u8{ "-target", triple });
|
2019-05-02 14:48:33 +00:00
|
|
|
if (!code.is_inline) {
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print(" -target {}", .{triple});
|
2019-05-02 14:48:33 +00:00
|
|
|
}
|
2019-03-20 23:00:23 +00:00
|
|
|
}
|
2019-05-01 19:34:17 +00:00
|
|
|
if (expected_outcome == .BuildFail) {
|
2020-03-30 18:23:22 +00:00
|
|
|
const result = try ChildProcess.exec(.{
|
|
|
|
.allocator = allocator,
|
2020-04-09 12:46:13 +00:00
|
|
|
.argv = build_args.items,
|
2020-03-30 18:23:22 +00:00
|
|
|
.env_map = &env_map,
|
|
|
|
.max_output_bytes = max_doc_file_size,
|
|
|
|
});
|
2019-05-01 19:34:17 +00:00
|
|
|
switch (result.term) {
|
2019-05-26 17:17:34 +00:00
|
|
|
.Exited => |exit_code| {
|
2019-05-01 19:34:17 +00:00
|
|
|
if (exit_code == 0) {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}\nThe following command incorrectly succeeded:\n", .{result.stderr});
|
|
|
|
dumpArgs(build_args.items);
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example incorrectly compiled", .{});
|
2019-05-01 19:34:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}\nThe following command crashed:\n", .{result.stderr});
|
|
|
|
dumpArgs(build_args.items);
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example compile crashed", .{});
|
2019-05-01 19:34:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
const escaped_stderr = try escapeHtml(allocator, result.stderr);
|
|
|
|
const colored_stderr = try termColor(allocator, escaped_stderr);
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("\n{}</code></pre>\n", .{colored_stderr});
|
2019-05-01 19:34:17 +00:00
|
|
|
break :code_block;
|
|
|
|
}
|
2020-04-09 12:46:13 +00:00
|
|
|
const exec_result = exec(allocator, &env_map, build_args.items) catch
|
2020-02-27 18:32:49 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example failed to compile", .{});
|
2018-01-19 08:03:20 +00:00
|
|
|
|
2019-03-20 23:00:23 +00:00
|
|
|
if (code.target_str) |triple| {
|
2020-02-27 19:41:44 +00:00
|
|
|
if (mem.startsWith(u8, triple, "wasm32") or
|
2019-09-29 18:00:05 +00:00
|
|
|
mem.startsWith(u8, triple, "riscv64-linux") or
|
2020-02-27 19:41:44 +00:00
|
|
|
(mem.startsWith(u8, triple, "x86_64-linux") and
|
|
|
|
std.Target.current.os.tag != .linux or std.Target.current.cpu.arch != .x86_64))
|
2019-03-20 23:00:23 +00:00
|
|
|
{
|
|
|
|
// skip execution
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("</code></pre>\n", .{});
|
2019-03-20 23:00:23 +00:00
|
|
|
break :code_block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 04:46:40 +00:00
|
|
|
const path_to_exe_dir = mem.trim(u8, exec_result.stdout, " \r\n");
|
|
|
|
const path_to_exe_basename = try std.fmt.allocPrint(allocator, "{}{}", .{
|
|
|
|
code.name,
|
|
|
|
target.exeFileExt(),
|
|
|
|
});
|
|
|
|
const path_to_exe = try fs.path.join(allocator, &[_][]const u8{
|
|
|
|
path_to_exe_dir,
|
|
|
|
path_to_exe_basename,
|
|
|
|
});
|
2019-12-02 02:27:55 +00:00
|
|
|
const run_args = &[_][]const u8{path_to_exe};
|
2018-01-19 08:03:20 +00:00
|
|
|
|
2019-09-26 17:11:07 +00:00
|
|
|
var exited_with_signal = false;
|
|
|
|
|
2018-01-19 08:03:20 +00:00
|
|
|
const result = if (expected_outcome == ExpectedOutcome.Fail) blk: {
|
2020-03-30 18:23:22 +00:00
|
|
|
const result = try ChildProcess.exec(.{
|
|
|
|
.allocator = allocator,
|
|
|
|
.argv = run_args,
|
|
|
|
.env_map = &env_map,
|
|
|
|
.max_output_bytes = max_doc_file_size,
|
|
|
|
});
|
2018-01-17 06:50:35 +00:00
|
|
|
switch (result.term) {
|
2019-05-26 17:17:34 +00:00
|
|
|
.Exited => |exit_code| {
|
2018-01-19 08:03:20 +00:00
|
|
|
if (exit_code == 0) {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}\nThe following command incorrectly succeeded:\n", .{result.stderr});
|
|
|
|
dumpArgs(run_args);
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example incorrectly compiled", .{});
|
2018-01-17 06:50:35 +00:00
|
|
|
}
|
|
|
|
},
|
2019-09-26 17:11:07 +00:00
|
|
|
.Signal => exited_with_signal = true,
|
2018-01-19 08:03:20 +00:00
|
|
|
else => {},
|
2018-01-17 06:50:35 +00:00
|
|
|
}
|
2018-01-19 08:03:20 +00:00
|
|
|
break :blk result;
|
|
|
|
} else blk: {
|
2019-12-09 05:18:01 +00:00
|
|
|
break :blk exec(allocator, &env_map, run_args) catch return parseError(tokenizer, code.source_token, "example crashed", .{});
|
2018-01-19 08:03:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const escaped_stderr = try escapeHtml(allocator, result.stderr);
|
|
|
|
const escaped_stdout = try escapeHtml(allocator, result.stdout);
|
|
|
|
|
|
|
|
const colored_stderr = try termColor(allocator, escaped_stderr);
|
|
|
|
const colored_stdout = try termColor(allocator, escaped_stdout);
|
|
|
|
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("\n$ ./{}\n{}{}", .{ code.name, colored_stdout, colored_stderr });
|
2019-09-26 17:11:07 +00:00
|
|
|
if (exited_with_signal) {
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("(process terminated by signal)", .{});
|
2019-09-26 17:11:07 +00:00
|
|
|
}
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("</code></pre>\n", .{});
|
2018-01-19 08:03:20 +00:00
|
|
|
},
|
|
|
|
Code.Id.Test => {
|
|
|
|
var test_args = std.ArrayList([]const u8).init(allocator);
|
|
|
|
defer test_args.deinit();
|
|
|
|
|
2020-09-27 04:03:38 +00:00
|
|
|
try test_args.appendSlice(&[_][]const u8{ zig_exe, "test", tmp_source_file_name });
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("<pre><code class=\"shell\">$ zig test {}.zig", .{code.name});
|
2018-01-19 08:03:20 +00:00
|
|
|
switch (code.mode) {
|
2020-02-27 18:32:49 +00:00
|
|
|
.Debug => {},
|
2020-09-27 04:03:38 +00:00
|
|
|
else => {
|
|
|
|
try test_args.appendSlice(&[_][]const u8{ "-O", @tagName(code.mode) });
|
|
|
|
try out.print(" -O {s}", .{@tagName(code.mode)});
|
2018-04-16 01:18:52 +00:00
|
|
|
},
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
2019-09-24 15:41:19 +00:00
|
|
|
if (code.link_libc) {
|
|
|
|
try test_args.append("-lc");
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print(" -lc", .{});
|
2019-09-24 15:41:19 +00:00
|
|
|
}
|
2019-03-20 23:00:23 +00:00
|
|
|
if (code.target_str) |triple| {
|
2019-12-02 02:27:55 +00:00
|
|
|
try test_args.appendSlice(&[_][]const u8{ "-target", triple });
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print(" -target {}", .{triple});
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
2020-04-09 12:46:13 +00:00
|
|
|
const result = exec(allocator, &env_map, test_args.items) catch return parseError(tokenizer, code.source_token, "test failed", .{});
|
2018-01-19 08:03:20 +00:00
|
|
|
const escaped_stderr = try escapeHtml(allocator, result.stderr);
|
|
|
|
const escaped_stdout = try escapeHtml(allocator, result.stdout);
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("\n{}{}</code></pre>\n", .{ escaped_stderr, escaped_stdout });
|
2018-01-19 08:03:20 +00:00
|
|
|
},
|
|
|
|
Code.Id.TestError => |error_match| {
|
|
|
|
var test_args = std.ArrayList([]const u8).init(allocator);
|
|
|
|
defer test_args.deinit();
|
|
|
|
|
2019-12-02 02:27:55 +00:00
|
|
|
try test_args.appendSlice(&[_][]const u8{
|
2018-05-26 22:16:39 +00:00
|
|
|
zig_exe,
|
|
|
|
"test",
|
|
|
|
"--color",
|
|
|
|
"on",
|
|
|
|
tmp_source_file_name,
|
|
|
|
});
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("<pre><code class=\"shell\">$ zig test {}.zig", .{code.name});
|
2018-01-19 08:03:20 +00:00
|
|
|
switch (code.mode) {
|
2020-02-27 18:32:49 +00:00
|
|
|
.Debug => {},
|
2020-09-27 04:03:38 +00:00
|
|
|
else => {
|
|
|
|
try test_args.appendSlice(&[_][]const u8{ "-O", @tagName(code.mode) });
|
|
|
|
try out.print(" -O {s}", .{@tagName(code.mode)});
|
2018-04-16 01:18:52 +00:00
|
|
|
},
|
2018-01-17 06:50:35 +00:00
|
|
|
}
|
2020-03-30 18:23:22 +00:00
|
|
|
const result = try ChildProcess.exec(.{
|
|
|
|
.allocator = allocator,
|
2020-04-09 12:46:13 +00:00
|
|
|
.argv = test_args.items,
|
2020-03-30 18:23:22 +00:00
|
|
|
.env_map = &env_map,
|
|
|
|
.max_output_bytes = max_doc_file_size,
|
|
|
|
});
|
2018-01-17 06:50:35 +00:00
|
|
|
switch (result.term) {
|
2019-05-26 17:17:34 +00:00
|
|
|
.Exited => |exit_code| {
|
2018-01-19 08:03:20 +00:00
|
|
|
if (exit_code == 0) {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}\nThe following command incorrectly succeeded:\n", .{result.stderr});
|
|
|
|
dumpArgs(test_args.items);
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example incorrectly compiled", .{});
|
2018-01-17 06:50:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}\nThe following command crashed:\n", .{result.stderr});
|
|
|
|
dumpArgs(test_args.items);
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example compile crashed", .{});
|
2018-01-17 06:50:35 +00:00
|
|
|
},
|
|
|
|
}
|
2018-01-19 08:03:20 +00:00
|
|
|
if (mem.indexOf(u8, result.stderr, error_match) == null) {
|
2020-10-20 08:05:44 +00:00
|
|
|
print("{}\nExpected to find '{}' in stderr\n", .{ result.stderr, error_match });
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example did not have expected compile error", .{});
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
|
|
|
const escaped_stderr = try escapeHtml(allocator, result.stderr);
|
|
|
|
const colored_stderr = try termColor(allocator, escaped_stderr);
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("\n{}</code></pre>\n", .{colored_stderr});
|
2018-01-17 06:50:35 +00:00
|
|
|
},
|
2018-01-19 08:03:20 +00:00
|
|
|
|
|
|
|
Code.Id.TestSafety => |error_match| {
|
|
|
|
var test_args = std.ArrayList([]const u8).init(allocator);
|
|
|
|
defer test_args.deinit();
|
|
|
|
|
2019-12-02 02:27:55 +00:00
|
|
|
try test_args.appendSlice(&[_][]const u8{
|
2018-05-26 22:16:39 +00:00
|
|
|
zig_exe,
|
|
|
|
"test",
|
|
|
|
tmp_source_file_name,
|
|
|
|
});
|
2019-03-13 18:46:53 +00:00
|
|
|
var mode_arg: []const u8 = "";
|
2018-01-19 08:03:20 +00:00
|
|
|
switch (code.mode) {
|
2020-02-27 18:32:49 +00:00
|
|
|
.Debug => {},
|
|
|
|
.ReleaseSafe => {
|
2020-09-27 04:03:38 +00:00
|
|
|
try test_args.append("-OReleaseSafe");
|
|
|
|
mode_arg = "-OReleaseSafe";
|
2019-03-13 18:46:53 +00:00
|
|
|
},
|
2020-02-27 18:32:49 +00:00
|
|
|
.ReleaseFast => {
|
2020-09-27 04:03:38 +00:00
|
|
|
try test_args.append("-OReleaseFast");
|
|
|
|
mode_arg = "-OReleaseFast";
|
2019-03-13 18:46:53 +00:00
|
|
|
},
|
2020-02-27 18:32:49 +00:00
|
|
|
.ReleaseSmall => {
|
2020-09-27 04:03:38 +00:00
|
|
|
try test_args.append("-OReleaseSmall");
|
|
|
|
mode_arg = "-OReleaseSmall";
|
2019-03-13 18:46:53 +00:00
|
|
|
},
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 18:23:22 +00:00
|
|
|
const result = try ChildProcess.exec(.{
|
|
|
|
.allocator = allocator,
|
2020-04-09 12:46:13 +00:00
|
|
|
.argv = test_args.items,
|
2020-03-30 18:23:22 +00:00
|
|
|
.env_map = &env_map,
|
|
|
|
.max_output_bytes = max_doc_file_size,
|
|
|
|
});
|
2018-01-19 08:03:20 +00:00
|
|
|
switch (result.term) {
|
2019-05-26 17:17:34 +00:00
|
|
|
.Exited => |exit_code| {
|
2018-01-19 08:03:20 +00:00
|
|
|
if (exit_code == 0) {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}\nThe following command incorrectly succeeded:\n", .{result.stderr});
|
|
|
|
dumpArgs(test_args.items);
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example test incorrectly succeeded", .{});
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}\nThe following command crashed:\n", .{result.stderr});
|
|
|
|
dumpArgs(test_args.items);
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example compile crashed", .{});
|
2018-01-19 08:03:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if (mem.indexOf(u8, result.stderr, error_match) == null) {
|
2020-10-20 08:05:44 +00:00
|
|
|
print("{}\nExpected to find '{}' in stderr\n", .{ result.stderr, error_match });
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example did not have expected runtime safety error message", .{});
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
|
|
|
const escaped_stderr = try escapeHtml(allocator, result.stderr);
|
|
|
|
const colored_stderr = try termColor(allocator, escaped_stderr);
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("<pre><code class=\"shell\">$ zig test {}.zig{}\n{}</code></pre>\n", .{
|
2019-03-13 18:46:53 +00:00
|
|
|
code.name,
|
|
|
|
mode_arg,
|
|
|
|
colored_stderr,
|
2019-12-09 05:18:01 +00:00
|
|
|
});
|
2018-01-17 06:50:35 +00:00
|
|
|
},
|
2018-01-23 03:24:07 +00:00
|
|
|
Code.Id.Obj => |maybe_error_match| {
|
2019-12-09 05:18:01 +00:00
|
|
|
const name_plus_obj_ext = try std.fmt.allocPrint(allocator, "{}{}", .{ code.name, obj_ext });
|
2019-05-26 17:17:34 +00:00
|
|
|
const tmp_obj_file_name = try fs.path.join(
|
2019-02-07 05:42:41 +00:00
|
|
|
allocator,
|
2019-12-02 02:27:55 +00:00
|
|
|
&[_][]const u8{ tmp_dir_name, name_plus_obj_ext },
|
2019-02-07 05:42:41 +00:00
|
|
|
);
|
2018-01-19 08:03:20 +00:00
|
|
|
var build_args = std.ArrayList([]const u8).init(allocator);
|
|
|
|
defer build_args.deinit();
|
|
|
|
|
2019-12-09 05:18:01 +00:00
|
|
|
const name_plus_h_ext = try std.fmt.allocPrint(allocator, "{}.h", .{code.name});
|
2019-05-26 17:17:34 +00:00
|
|
|
const output_h_file_name = try fs.path.join(
|
2019-02-07 05:42:41 +00:00
|
|
|
allocator,
|
2019-12-02 02:27:55 +00:00
|
|
|
&[_][]const u8{ tmp_dir_name, name_plus_h_ext },
|
2019-02-07 05:42:41 +00:00
|
|
|
);
|
2018-06-12 19:21:14 +00:00
|
|
|
|
2019-12-02 02:27:55 +00:00
|
|
|
try build_args.appendSlice(&[_][]const u8{
|
2018-05-26 22:16:39 +00:00
|
|
|
zig_exe,
|
|
|
|
"build-obj",
|
|
|
|
tmp_source_file_name,
|
|
|
|
"--color",
|
|
|
|
"on",
|
2019-03-09 06:00:45 +00:00
|
|
|
"--name",
|
|
|
|
code.name,
|
2020-09-27 04:03:38 +00:00
|
|
|
try std.fmt.allocPrint(allocator, "-femit-bin={s}{c}{s}", .{
|
|
|
|
tmp_dir_name, fs.path.sep, name_plus_obj_ext,
|
|
|
|
}),
|
2018-05-26 22:16:39 +00:00
|
|
|
});
|
2018-01-19 08:03:20 +00:00
|
|
|
if (!code.is_inline) {
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("<pre><code class=\"shell\">$ zig build-obj {}.zig", .{code.name});
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (code.mode) {
|
2020-02-27 18:32:49 +00:00
|
|
|
.Debug => {},
|
2020-09-27 04:03:38 +00:00
|
|
|
else => {
|
|
|
|
try build_args.appendSlice(&[_][]const u8{ "-O", @tagName(code.mode) });
|
2018-04-16 01:18:52 +00:00
|
|
|
if (!code.is_inline) {
|
2020-09-27 04:03:38 +00:00
|
|
|
try out.print(" -O {s}", .{@tagName(code.mode)});
|
2018-04-16 01:18:52 +00:00
|
|
|
}
|
|
|
|
},
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 14:48:33 +00:00
|
|
|
if (code.target_str) |triple| {
|
2019-12-02 02:27:55 +00:00
|
|
|
try build_args.appendSlice(&[_][]const u8{ "-target", triple });
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print(" -target {}", .{triple});
|
2019-05-02 14:48:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 03:24:07 +00:00
|
|
|
if (maybe_error_match) |error_match| {
|
2020-03-30 18:23:22 +00:00
|
|
|
const result = try ChildProcess.exec(.{
|
|
|
|
.allocator = allocator,
|
2020-04-09 12:46:13 +00:00
|
|
|
.argv = build_args.items,
|
2020-03-30 18:23:22 +00:00
|
|
|
.env_map = &env_map,
|
|
|
|
.max_output_bytes = max_doc_file_size,
|
|
|
|
});
|
2018-01-23 03:24:07 +00:00
|
|
|
switch (result.term) {
|
2019-05-26 17:17:34 +00:00
|
|
|
.Exited => |exit_code| {
|
2018-01-23 03:24:07 +00:00
|
|
|
if (exit_code == 0) {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}\nThe following command incorrectly succeeded:\n", .{result.stderr});
|
|
|
|
dumpArgs(build_args.items);
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example build incorrectly succeeded", .{});
|
2018-01-23 03:24:07 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}\nThe following command crashed:\n", .{result.stderr});
|
|
|
|
dumpArgs(build_args.items);
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example compile crashed", .{});
|
2018-01-23 03:24:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if (mem.indexOf(u8, result.stderr, error_match) == null) {
|
2020-10-20 08:05:44 +00:00
|
|
|
print("{}\nExpected to find '{}' in stderr\n", .{ result.stderr, error_match });
|
2019-12-09 05:18:01 +00:00
|
|
|
return parseError(tokenizer, code.source_token, "example did not have expected compile error message", .{});
|
2018-01-23 03:24:07 +00:00
|
|
|
}
|
|
|
|
const escaped_stderr = try escapeHtml(allocator, result.stderr);
|
|
|
|
const colored_stderr = try termColor(allocator, escaped_stderr);
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("\n{}", .{colored_stderr});
|
2018-01-23 03:24:07 +00:00
|
|
|
} else {
|
2020-04-09 12:46:13 +00:00
|
|
|
_ = exec(allocator, &env_map, build_args.items) catch return parseError(tokenizer, code.source_token, "example failed to compile", .{});
|
2018-01-23 03:24:07 +00:00
|
|
|
}
|
2018-01-19 08:03:20 +00:00
|
|
|
if (!code.is_inline) {
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("</code></pre>\n", .{});
|
2018-01-19 08:03:20 +00:00
|
|
|
}
|
2018-01-17 06:50:35 +00:00
|
|
|
},
|
2019-05-16 18:54:05 +00:00
|
|
|
Code.Id.Lib => {
|
2020-09-27 04:03:38 +00:00
|
|
|
const bin_basename = try std.zig.binNameAlloc(allocator, .{
|
|
|
|
.root_name = code.name,
|
|
|
|
.target = std.Target.current,
|
|
|
|
.output_mode = .Lib,
|
|
|
|
});
|
|
|
|
|
2019-05-16 18:54:05 +00:00
|
|
|
var test_args = std.ArrayList([]const u8).init(allocator);
|
|
|
|
defer test_args.deinit();
|
|
|
|
|
2019-12-02 02:27:55 +00:00
|
|
|
try test_args.appendSlice(&[_][]const u8{
|
2019-05-16 18:54:05 +00:00
|
|
|
zig_exe,
|
|
|
|
"build-lib",
|
|
|
|
tmp_source_file_name,
|
2020-09-27 04:03:38 +00:00
|
|
|
try std.fmt.allocPrint(allocator, "-femit-bin={s}{s}{s}", .{
|
|
|
|
tmp_dir_name, fs.path.sep_str, bin_basename,
|
|
|
|
}),
|
2019-05-16 18:54:05 +00:00
|
|
|
});
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("<pre><code class=\"shell\">$ zig build-lib {}.zig", .{code.name});
|
2019-05-16 18:54:05 +00:00
|
|
|
switch (code.mode) {
|
2020-02-27 18:32:49 +00:00
|
|
|
.Debug => {},
|
2020-09-27 04:03:38 +00:00
|
|
|
else => {
|
|
|
|
try test_args.appendSlice(&[_][]const u8{ "-O", @tagName(code.mode) });
|
|
|
|
try out.print(" -O {s}", .{@tagName(code.mode)});
|
2019-05-16 18:54:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if (code.target_str) |triple| {
|
2019-12-02 02:27:55 +00:00
|
|
|
try test_args.appendSlice(&[_][]const u8{ "-target", triple });
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print(" -target {}", .{triple});
|
2019-05-16 18:54:05 +00:00
|
|
|
}
|
2020-04-09 12:46:13 +00:00
|
|
|
const result = exec(allocator, &env_map, test_args.items) catch return parseError(tokenizer, code.source_token, "test failed", .{});
|
2019-05-16 18:54:05 +00:00
|
|
|
const escaped_stderr = try escapeHtml(allocator, result.stderr);
|
|
|
|
const escaped_stdout = try escapeHtml(allocator, result.stdout);
|
2019-12-09 05:18:01 +00:00
|
|
|
try out.print("\n{}{}</code></pre>\n", .{ escaped_stderr, escaped_stdout });
|
2019-05-16 18:54:05 +00:00
|
|
|
},
|
2018-01-17 06:50:35 +00:00
|
|
|
}
|
2020-09-27 04:03:38 +00:00
|
|
|
print("OK\n", .{});
|
2018-01-17 06:50:35 +00:00
|
|
|
},
|
2017-11-07 08:22:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-19 08:03:20 +00:00
|
|
|
|
2019-05-26 17:17:34 +00:00
|
|
|
fn exec(allocator: *mem.Allocator, env_map: *std.BufMap, args: []const []const u8) !ChildProcess.ExecResult {
|
2020-03-30 18:23:22 +00:00
|
|
|
const result = try ChildProcess.exec(.{
|
2020-03-14 04:46:40 +00:00
|
|
|
.allocator = allocator,
|
|
|
|
.argv = args,
|
|
|
|
.env_map = env_map,
|
|
|
|
.max_output_bytes = max_doc_file_size,
|
|
|
|
});
|
2018-01-19 08:03:20 +00:00
|
|
|
switch (result.term) {
|
2019-05-26 17:17:34 +00:00
|
|
|
.Exited => |exit_code| {
|
2018-01-19 08:03:20 +00:00
|
|
|
if (exit_code != 0) {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}\nThe following command exited with code {}:\n", .{ result.stderr, exit_code });
|
|
|
|
dumpArgs(args);
|
2018-01-19 08:03:20 +00:00
|
|
|
return error.ChildExitError;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
2020-09-27 04:03:38 +00:00
|
|
|
print("{}\nThe following command crashed:\n", .{result.stderr});
|
|
|
|
dumpArgs(args);
|
2018-01-19 08:03:20 +00:00
|
|
|
return error.ChildCrashed;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-06-07 23:10:45 +00:00
|
|
|
|
2018-07-09 16:12:37 +00:00
|
|
|
fn getBuiltinCode(allocator: *mem.Allocator, env_map: *std.BufMap, zig_exe: []const u8) ![]const u8 {
|
2020-09-27 04:03:38 +00:00
|
|
|
const result = try exec(allocator, env_map, &[_][]const u8{ zig_exe, "build-obj", "--show-builtin" });
|
2018-06-07 23:10:45 +00:00
|
|
|
return result.stdout;
|
|
|
|
}
|
2020-09-27 04:03:38 +00:00
|
|
|
|
|
|
|
fn dumpArgs(args: []const []const u8) void {
|
|
|
|
for (args) |arg|
|
|
|
|
print("{} ", .{arg})
|
|
|
|
else
|
|
|
|
print("\n", .{});
|
|
|
|
}
|