-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c6cf2b
commit b60c1d8
Showing
2 changed files
with
12 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,14 @@ | ||
const std = @import("std"); | ||
|
||
// Although this function looks imperative, note that its job is to | ||
// declaratively construct a build graph that will be executed by an external | ||
// runner. | ||
pub fn build(b: *std.Build) void { | ||
// Standard target options allows the person running `zig build` to choose | ||
// what target to build for. Here we do not override the defaults, which | ||
// means any target is allowed, and the default is native. Other options | ||
// for restricting supported target set are available. | ||
const target = b.standardTargetOptions(.{}); | ||
|
||
// Standard optimization options allow the person running `zig build` to select | ||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not | ||
// set a preferred release mode, allowing the user to decide how to optimize. | ||
const optimize = b.standardOptimizeOption(.{}); | ||
|
||
const exe = b.addExecutable(.{ | ||
.name = "zig_fmt", | ||
// In this case the main source file is merely a path, however, in more | ||
// complicated build scripts, this could be a generated file. | ||
.root_source_file = b.path("src/main.zig"), | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
|
||
// This declares intent for the executable to be installed into the | ||
// standard location when the user invokes the "install" step (the default | ||
// step when running `zig build`). | ||
b.installArtifact(exe); | ||
|
||
// This *creates* a Run step in the build graph, to be executed when another | ||
// step is evaluated that depends on it. The next line below will establish | ||
// such a dependency. | ||
const run_cmd = b.addRunArtifact(exe); | ||
|
||
// By making the run step depend on the install step, it will be run from the | ||
// installation directory rather than directly from within the cache directory. | ||
// This is not necessary, however, if the application depends on other installed | ||
// files, this ensures they will be present and in the expected location. | ||
run_cmd.step.dependOn(b.getInstallStep()); | ||
|
||
// This allows the user to pass arguments to the application in the build | ||
// command itself, like this: `zig build run -- arg1 arg2 etc` | ||
if (b.args) |args| { | ||
run_cmd.addArgs(args); | ||
} | ||
|
||
// This creates a build step. It will be visible in the `zig build --help` menu, | ||
// and can be selected like this: `zig build run` | ||
// This will evaluate the `run` step rather than the default, which is "install". | ||
const run_step = b.step("run", "Run the app"); | ||
run_step.dependOn(&run_cmd.step); | ||
|
||
// Creates a step for unit testing. This only builds the test executable | ||
// but does not run it. | ||
const unit_tests = b.addTest(.{ | ||
.root_source_file = b.path("src/main.zig"), | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
|
||
const run_unit_tests = b.addRunArtifact(unit_tests); | ||
|
||
// Similar to creating the run step earlier, this exposes a `test` step to | ||
// the `zig build --help` menu, providing a way for the user to request | ||
// running the unit tests. | ||
const test_step = b.step("test", "Run unit tests"); | ||
test_step.dependOn(&run_unit_tests.step); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,19 @@ | ||
const std = @import("std"); | ||
const builtin = @import("builtin"); | ||
|
||
const io = std.io; | ||
const fs = std.fs; | ||
const mem = std.mem; | ||
const Allocator = mem.Allocator; | ||
const process = std.process; | ||
|
||
pub fn main() !void { | ||
const stdin = io.getStdIn(); | ||
const stdout = io.getStdOut().writer(); | ||
|
||
const allocator = gpa: { | ||
if (builtin.os.tag == .wasi) { | ||
break :gpa std.heap.wasm_allocator; | ||
} | ||
// We would prefer to use raw libc allocator here, but cannot | ||
// use it if it won't support the alignment we need. | ||
if (@alignOf(std.c.max_align_t) < @alignOf(i128)) { | ||
break :gpa std.heap.c_allocator; | ||
} | ||
break :gpa std.heap.raw_c_allocator; | ||
}; | ||
|
||
const source_code = try readSourceFileToEndAlloc(allocator, &stdin, null); | ||
|
||
var tree = try std.zig.Ast.parse(allocator, source_code, .zig); | ||
defer tree.deinit(allocator); | ||
|
||
const formatted = try tree.render(allocator); | ||
defer allocator.free(formatted); | ||
try stdout.writeAll(formatted); | ||
} | ||
|
||
const max_src_size = std.math.maxInt(u32); | ||
|
||
fn readSourceFileToEndAlloc( | ||
allocator: Allocator, | ||
input: *const fs.File, | ||
size_hint: ?usize, | ||
) ![:0]u8 { | ||
const source_code = input.readToEndAllocOptions( | ||
allocator, | ||
max_src_size, | ||
size_hint, | ||
@alignOf(u16), | ||
0, | ||
) catch |err| switch (err) { | ||
error.ConnectionResetByPeer => unreachable, | ||
error.ConnectionTimedOut => unreachable, | ||
error.NotOpenForReading => unreachable, | ||
else => |e| return e, | ||
}; | ||
errdefer allocator.free(source_code); | ||
|
||
// Detect unsupported file types with their Byte Order Mark | ||
const unsupported_boms = [_][]const u8{ | ||
"\xff\xfe\x00\x00", // UTF-32 little endian | ||
"\xfe\xff\x00\x00", // UTF-32 big endian | ||
"\xfe\xff", // UTF-16 big endian | ||
}; | ||
for (unsupported_boms) |bom| { | ||
if (mem.startsWith(u8, source_code, bom)) { | ||
return error.UnsupportedEncoding; | ||
} | ||
} | ||
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
defer arena_instance.deinit(); | ||
const arena = arena_instance.allocator(); | ||
const gpa = arena; | ||
|
||
// If the file starts with a UTF-16 little endian BOM, translate it to UTF-8 | ||
if (mem.startsWith(u8, source_code, "\xff\xfe")) { | ||
const source_code_utf16_le = mem.bytesAsSlice(u16, source_code); | ||
const source_code_utf8 = std.unicode.utf16leToUtf8AllocZ(allocator, source_code_utf16_le) catch |err| switch (err) { | ||
error.DanglingSurrogateHalf => error.UnsupportedEncoding, | ||
error.ExpectedSecondSurrogateHalf => error.UnsupportedEncoding, | ||
error.UnexpectedSecondSurrogateHalf => error.UnsupportedEncoding, | ||
else => |e| return e, | ||
}; | ||
const stdin = std.io.getStdIn(); | ||
const source_code = try std.zig.readSourceFileToEndAlloc(gpa, stdin, null); | ||
defer gpa.free(source_code); | ||
|
||
allocator.free(source_code); | ||
return source_code_utf8; | ||
} | ||
var tree = try std.zig.Ast.parse(gpa, source_code, .zig); | ||
defer tree.deinit(gpa); | ||
|
||
return source_code; | ||
const formatted = try tree.render(gpa); | ||
defer gpa.free(formatted); | ||
return std.io.getStdOut().writeAll(formatted); | ||
} |