1//! Minimal formatter CLI via `zig build format -- filepath`
2//! Use this only for working on the formatter.
3
4pub fn main() !u8 {
5 try core.unicode_props.init();
6
7 const use_gpa = @import("builtin").mode == .Debug;
8 var gpa = if (use_gpa)
9 std.heap.GeneralPurposeAllocator(.{
10 .stack_trace_frames = 100,
11 }){}
12 else
13 std.heap.ArenaAllocator.init(std.heap.page_allocator);
14 defer _ = if (use_gpa) gpa.deinit();
15 const alloc = gpa.allocator();
16
17 var args = try std.process.argsWithAllocator(alloc);
18 defer args.deinit();
19
20 _ = args.skip();
21 const file_path = args.next() orelse {
22 std.debug.print("Missing filepath argument\n", .{});
23 return 1;
24 };
25
26 const code = try std.fs.cwd()
27 .readFileAllocOptions(alloc, file_path, std.math.maxInt(u32), null, @alignOf(u8), 0);
28 defer alloc.free(code);
29
30 var err_data: core.Parse.AstError.WithTrace = undefined;
31 var ast = core.Parse.parse(
32 alloc,
33 code,
34 &err_data,
35 ) catch |err| switch (err) {
36 error.OutOfMemory => return err,
37 error.ParseFail => {
38 var report = try err_data.toReport(alloc);
39 defer report.deinit(alloc);
40
41 report.dump(
42 .{ .single_file = .{
43 .allocator = alloc,
44 .url = file_path,
45 .file = .{ .utf8_source = code },
46 } },
47 .getStderr(),
48 ) catch {};
49
50 return err;
51 },
52 };
53 defer ast.deinit(alloc);
54
55 const stdout = std.io.getStdOut();
56 try Format.format(ast, stdout.writer().any());
57
58 return 0;
59}
60
61const std = @import("std");
62const core = @import("core");
63const Format = @import("format");