cli: add deterministic source formatter
This commit is contained in:
1 parent
d901d5227b
commit
462ddd2c5c
14 files changed
+787
-13
No files matched your search
@@ -33,6 +33,9 @@ const usage =
|
||||
\\ print the canonical AIR a source module lowers to;
|
||||
\\ --annotate adds source spans as comments (not canonical);
|
||||
\\ --stats prints source/AIR size and expansion ratio
|
||||
\\ fmt [--check | --write] <file.aji>
|
||||
\\ print formatted source; --check checks without writing,
|
||||
\\ --write replaces a regular file; both print a JSON record
|
||||
\\ explore [--max-runs N] [--grant P=KIND[:SCOPE]]... <file.air | file.aji | --manifest aji.toml>
|
||||
\\ run @main under every schedule and weak-memory choice
|
||||
\\ (depth first, up to N runs, default 100000) and print the
|
||||
@@ -71,6 +74,7 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
|
||||
return .usage;
|
||||
}
|
||||
const cmd = args[1];
|
||||
if (std.mem.eql(u8, cmd, "fmt")) return formatCommand(io, arena, args[2..], out);
|
||||
if (std.mem.eql(u8, cmd, "version")) {
|
||||
try out.print("aji {s} (air format {d})\n", .{ aji.version, aji.air.format_version });
|
||||
return .accepted;
|
||||
@@ -485,6 +489,85 @@ test "standard library trap record identifies the bundled source" {
|
||||
try std.testing.expectEqual(@as(i64, 20), location.get("line").?.integer);
|
||||
}
|
||||
|
||||
fn formatCommand(io: std.Io, arena: std.mem.Allocator, arguments: []const [:0]const u8, out: *std.Io.Writer) !ExitCode {
|
||||
var rest = arguments;
|
||||
const Mode = enum { stdout, check, write };
|
||||
var mode: Mode = .stdout;
|
||||
if (rest.len != 0 and (std.mem.eql(u8, rest[0], "--check") or std.mem.eql(u8, rest[0], "--write"))) {
|
||||
mode = if (std.mem.eql(u8, rest[0], "--check")) .check else .write;
|
||||
rest = rest[1..];
|
||||
}
|
||||
if (rest.len != 1 or std.mem.startsWith(u8, rest[0], "--") or !std.mem.endsWith(u8, rest[0], ".aji")) return usageError("fmt takes [--check | --write] and one .aji file");
|
||||
const path = rest[0];
|
||||
var permissions: ?std.Io.File.Permissions = null;
|
||||
if (mode == .write) {
|
||||
const stat = std.Io.Dir.cwd().statFile(io, path, .{ .follow_symlinks = false }) catch |err| {
|
||||
std.debug.print("aji: {s}: {t}\n", .{ path, err });
|
||||
return .usage;
|
||||
};
|
||||
if (stat.kind != .file) {
|
||||
std.debug.print("aji: fmt --write requires a regular file\n", .{});
|
||||
return .usage;
|
||||
}
|
||||
permissions = stat.permissions;
|
||||
}
|
||||
const input = try readInput(arena, io, path, false) orelse return .usage;
|
||||
const formatted = aji.frontend.format.format(arena, input.source) catch |err| switch (err) {
|
||||
error.Invariant => {
|
||||
std.debug.print("aji: formatter token preservation invariant failed\n", .{});
|
||||
return .usage;
|
||||
},
|
||||
error.OutOfMemory => return error.OutOfMemory,
|
||||
};
|
||||
const text = switch (formatted) {
|
||||
.err => |f| {
|
||||
const d = try aji.frontend.syntaxDiagnostic(arena, path, f);
|
||||
try d.writeJson(out);
|
||||
try out.writeByte('\n');
|
||||
return .rejected;
|
||||
},
|
||||
.ok => |text| text,
|
||||
};
|
||||
if (mode == .stdout) {
|
||||
try out.writeAll(text);
|
||||
return .accepted;
|
||||
}
|
||||
const changed = !std.mem.eql(u8, input.source, text);
|
||||
if (mode == .write) {
|
||||
const dir = std.Io.Dir.cwd();
|
||||
if (changed) {
|
||||
var file = dir.createFileAtomic(io, path, .{ .replace = true, .permissions = permissions.? }) catch |err| {
|
||||
std.debug.print("aji: {s}: {t}\n", .{ path, err });
|
||||
return .usage;
|
||||
};
|
||||
defer file.deinit(io);
|
||||
// Creation may apply umask; restore the complete original mode.
|
||||
file.file.setPermissions(io, permissions.?) catch |err| {
|
||||
std.debug.print("aji: {s}: {t}\n", .{ path, err });
|
||||
return .usage;
|
||||
};
|
||||
var buffer: [4096]u8 = undefined;
|
||||
var writer = file.file.writer(io, &buffer);
|
||||
writer.interface.writeAll(text) catch |err| {
|
||||
std.debug.print("aji: {s}: {t}\n", .{ path, writer.err orelse err });
|
||||
return .usage;
|
||||
};
|
||||
writer.flush() catch |err| {
|
||||
std.debug.print("aji: {s}: {t}\n", .{ path, err });
|
||||
return .usage;
|
||||
};
|
||||
file.replace(io) catch |err| {
|
||||
std.debug.print("aji: {s}: {t}\n", .{ path, err });
|
||||
return .usage;
|
||||
};
|
||||
}
|
||||
}
|
||||
var json: std.json.Stringify = .{ .writer = out };
|
||||
try json.write(.{ .file = path, .status = if (mode == .check and changed) "needs_format" else "formatted", .changed = changed });
|
||||
try out.writeByte('\n');
|
||||
return if (mode == .check and changed) .rejected else .accepted;
|
||||
}
|
||||
|
||||
fn readInput(arena: std.mem.Allocator, io: std.Io, path: []const u8, from_manifest: bool) !?aji.source_package.Input {
|
||||
return switch (if (from_manifest) try aji.source_package.load(arena, io, path) else try aji.source_package.single(arena, io, path)) {
|
||||
.ok => |input| input,
|
||||
|
||||
Reference in new issue
Block a user