| ... | @@ -1,260 +1,656 @@ | ... | @@ -1,260 +1,656 @@ |
| 1 | //! This is the `lang` command line interface. It combines all components | 1 | //! Configuration for an command line interface, which can be executed against |
| 2 | //! of the software development kit into one. Multiple versions of this | 2 | //! an argument slice using `.parse`. None of the parsing strategies require an |
| 3 | //! file are built with different subsets of features, allowing you to, | 3 | //! allocator, however it expects incoming arguments to be UTF-8 formatted which |
| 4 | //! for example, test the `core` module's compilation features without | 4 | //! on Windows will require an allocator. |
| 5 | //! needing to compile the formatter. | 5 | //! |
| 6 | | 6 | //! See `fn main` for a basic usage example. |
| 7 | const have_debug_features = builtin.mode == .Debug; | 7 | options: []const Option = &.{}, |
| 8 | const config = @import("config"); | 8 | positionals: Positionals = .none, |
| 9 | | 9 | help: HelpConfig = .{}, |
| 10 | pub fn main() !u8 { | 10 | |
| 11 | try tracer.init(); | 11 | const Option = struct { |
| 12 | defer tracer.deinit(); | 12 | /// Should be formatted as the flag name: '--dump-ir' |
| 13 | try tracer.init_thread(null); | 13 | name: []const u8, |
| 14 | defer tracer.deinit_thread(); | 14 | /// Displayed in generated help menu |
| 15 | | 15 | desc: []const u8, |
| 16 | // unicode props is asserted to be initialized before core.Tokenizer is used | 16 | kind: Kind = .boolean, |
| 17 | // this will result in a static page allocation, which is never cleaned up. | 17 | /// A short code allows single dash argument parsing. |
| 18 | try core.unicode_props.init(); | 18 | short: ?u8 = null, |
| 19 | | 19 | |
| 20 | const use_gpa = builtin.mode == .Debug; | 20 | const Kind = union(enum) { |
| 21 | var gpa = if (use_gpa) | 21 | /// UTF-8 validated |
| 22 | std.heap.GeneralPurposeAllocator(.{ | 22 | string, |
| 23 | .stack_trace_frames = 100, | 23 | boolean, |
| 24 | }){} | 24 | @"enum": type, |
| 25 | else | 25 | |
| 26 | std.heap.ArenaAllocator.init(std.heap.page_allocator); | 26 | pub fn Type(k: Kind) type { |
| 27 | var no_leak_checks = false; | 27 | return switch (k) { |
| 28 | defer _ = if (use_gpa and !no_leak_checks) gpa.deinit(); | 28 | .string => []const u8, |
| 29 | const alloc = gpa.allocator(); | 29 | .boolean => bool, |
| | 30 | .@"enum" => |t| t, |
| | 31 | }; |
| | 32 | } |
| | 33 | }; |
| | 34 | |
| | 35 | const help: Option = .{ |
| | 36 | .name = "--help", |
| | 37 | .short = 'h', |
| | 38 | .desc = "Print this help and exit", |
| | 39 | }; |
| | 40 | }; |
| | 41 | |
| | 42 | pub const Positionals = union(enum) { |
| | 43 | none, |
| | 44 | /// Store a single positional: ?[]const u8 |
| | 45 | single, |
| | 46 | /// Store the start and count of positionals. Call |
| | 47 | /// 'parsed.positionals.iterator(args)' with the |
| | 48 | /// original argument array to read positionals. |
| | 49 | unlimited, |
| | 50 | /// Same strategy as .unlimited, with a limit |
| | 51 | limited: usize, |
| | 52 | /// Use a bounded array to store positionals, using |
| | 53 | /// a stack allocated buffer that can hold enough |
| | 54 | /// '[]const u8's as allowed by the bound. |
| | 55 | bounded: usize, |
| | 56 | |
| | 57 | pub fn Type(p: Positionals) type { |
| | 58 | return switch (p) { |
| | 59 | .none => void, |
| | 60 | .single => ?[]const u8, |
| | 61 | .limited => |limit| switch (limit) { |
| | 62 | 0 => void, |
| | 63 | else => Multi, |
| | 64 | }, |
| | 65 | .unlimited => Multi, |
| | 66 | .bounded => |n| std.BoundedArray([]const u8, n), |
| | 67 | }; |
| | 68 | } |
| 30 | | 69 | |
| 31 | // Current working directory is required for CLI resolution. | 70 | pub inline fn maxCount(p: Positionals) usize { |
| 32 | var cwd_buf: [std.fs.max_path_bytes]u8 = undefined; | 71 | return switch (p) { |
| 33 | const cwd = try std.posix.getcwd(&cwd_buf); | 72 | .none => 0, |
| 34 | | 73 | .single => 1, |
| 35 | // Command line argument parsing. | 74 | inline .limited, .bounded => |limit| limit, |
| 36 | var maybe_file_path: ?[]const u8 = null; | 75 | .unlimited => std.math.maxInt(usize), |
| 37 | defer if (maybe_file_path) |path| alloc.free(path); | 76 | }; |
| 38 | var exe_name: ?[]const u8 = null; | 77 | } |
| 39 | defer if (exe_name) |path| alloc.free(path); | 78 | |
| 40 | var dump_ir = false; | 79 | pub const Multi = struct { |
| 41 | var dump_state: ?[]const u8 = null; | 80 | /// The index of the first positional |
| 42 | var run = false; | 81 | first: usize, |
| 43 | var has_errors = false; | 82 | /// The number of total positionals. This can |
| 44 | { | 83 | /// be used to pre-allocate resources. |
| 45 | var args = try std.process.argsWithAllocator(alloc); | 84 | len: usize, |
| 46 | defer args.deinit(); | 85 | |
| 47 | | 86 | first_is_after_double_dash: bool, |
| 48 | exe_name = try alloc.dupe(u8, std.fs.path.basename(args.next() orelse "cli")); | | |
| 49 | | | |
| 50 | while (args.next()) |arg| { | | |
| 51 | if (std.mem.startsWith(u8, arg, "-")) { | | |
| 52 | if (have_debug_features) { | | |
| 53 | if (std.mem.eql(u8, arg, "--report-trace")) { | | |
| 54 | core.Report.debug_show_report_trace = true; | | |
| 55 | continue; | | |
| 56 | } | | |
| 57 | if (std.mem.eql(u8, arg, "--no-leak-checks")) { | | |
| 58 | no_leak_checks = true; | | |
| 59 | continue; | | |
| 60 | } | | |
| 61 | } | | |
| 62 | | 87 | |
| 63 | if (std.mem.eql(u8, arg, "--dump-ir")) { | 88 | pub const empty = .{ |
| 64 | dump_ir = true; | 89 | .first = 0, |
| 65 | continue; | 90 | .len = 0, |
| | 91 | .first_is_after_double_dash = false, |
| | 92 | }; |
| | 93 | |
| | 94 | pub fn iterator(pos: @This(), all_args: []const []const u8) PositionalIterator { |
| | 95 | return .{ |
| | 96 | .args = all_args, |
| | 97 | .i = pos.first, |
| | 98 | .left = pos.len, |
| | 99 | .after_double_dash = pos.first_is_after_double_dash, |
| | 100 | }; |
| | 101 | } |
| | 102 | }; |
| | 103 | }; |
| | 104 | |
| | 105 | pub const HelpConfig = struct { |
| | 106 | /// If set true, registers an addition '--help' / '-h' flag |
| | 107 | /// These will show the help menu and exit the program. |
| | 108 | /// |
| | 109 | /// Help menu can always be displayed manually with .showHelp() |
| | 110 | option: bool = true, |
| | 111 | |
| | 112 | /// Override the usage string in the first line. |
| | 113 | usage: ?[]const u8 = null, |
| | 114 | |
| | 115 | /// Hide a list of flag names. |
| | 116 | hide_options: []const []const u8 = &.{}, |
| | 117 | }; |
| | 118 | |
| | 119 | pub const PositionalIterator = struct { |
| | 120 | args: []const []const u8, |
| | 121 | i: usize, |
| | 122 | left: usize, |
| | 123 | after_double_dash: bool, |
| | 124 | |
| | 125 | pub fn peek(it: PositionalIterator) ?[]const u8 { |
| | 126 | return if (it.left > 0) it.args[it.i] else null; |
| | 127 | } |
| | 128 | |
| | 129 | pub fn next(it: *PositionalIterator) ?[]const u8 { |
| | 130 | if (it.left > 0) { |
| | 131 | defer it.advance(); |
| | 132 | return it.args[it.i]; |
| | 133 | } else { |
| | 134 | return null; |
| | 135 | } |
| | 136 | } |
| | 137 | |
| | 138 | pub fn advance(it: *PositionalIterator) void { |
| | 139 | it.left -= 1; |
| | 140 | if (it.left == 0) return; |
| | 141 | if (it.after_double_dash) { |
| | 142 | it.i += 1; |
| | 143 | } else while (mem.startsWith(u8, it.args[it.i], "-")) { |
| | 144 | defer it.i += 1; |
| | 145 | if (it.args[it.i].len == 2 and it.args[it.i][1] == '-') { |
| | 146 | it.after_double_dash = true; |
| | 147 | return; |
| | 148 | } |
| | 149 | } |
| | 150 | } |
| | 151 | }; |
| | 152 | |
| | 153 | inline fn fieldName(comptime name: []const u8) [:0]const u8 { |
| | 154 | std.debug.assert(name[0] == '-' and name[1] == '-'); |
| | 155 | for (name[2..]) |char| switch (char) { |
| | 156 | 'a'...'z', '-' => {}, |
| | 157 | else => |ch| @compileError(std.fmt.comptimePrint("Unexpected char '{c}' in option name '{s}'", .{ ch, name })), |
| | 158 | }; |
| | 159 | |
| | 160 | const replacement_size = mem.replacementSize(u8, name[2..], "-", "_"); |
| | 161 | const replaced = brk: { |
| | 162 | var buf: [replacement_size:0]u8 = undefined; |
| | 163 | _ = mem.replace(u8, name[2..], "-", "_", &buf); |
| | 164 | break :brk buf; |
| | 165 | }; |
| | 166 | return &replaced; |
| | 167 | } |
| | 168 | |
| | 169 | /// Every option name is converted into snake case, then |
| | 170 | /// an additional field name 'positionals' is added. |
| | 171 | pub fn Parsed(p: Cli) type { |
| | 172 | const fields = brk: { |
| | 173 | var fields: [p.options.len + 1]Type.StructField = undefined; |
| | 174 | |
| | 175 | for (p.options, fields[0..p.options.len]) |def, *field| { |
| | 176 | const Wrapped = ?def.kind.Type(); |
| | 177 | field.* = .{ |
| | 178 | .name = fieldName(def.name), |
| | 179 | .alignment = @alignOf(Wrapped), |
| | 180 | .default_value = &@as(Wrapped, null), |
| | 181 | .is_comptime = false, |
| | 182 | .type = Wrapped, |
| | 183 | }; |
| | 184 | } |
| | 185 | |
| | 186 | const Pos = p.positionals.Type(); |
| | 187 | fields[fields.len - 1] = .{ |
| | 188 | .name = "positionals", |
| | 189 | .alignment = @alignOf(Pos), |
| | 190 | .type = Pos, |
| | 191 | .default_value = &@as(Pos, switch (p.positionals) { |
| | 192 | .none => {}, |
| | 193 | .single => null, |
| | 194 | .limited, .unlimited => .empty, |
| | 195 | .bounded => .{}, |
| | 196 | }), |
| | 197 | .is_comptime = false, |
| | 198 | }; |
| | 199 | |
| | 200 | break :brk fields; |
| | 201 | }; |
| | 202 | |
| | 203 | return @Type(.{ .@"struct" = .{ |
| | 204 | .layout = .auto, |
| | 205 | .fields = &fields, |
| | 206 | .decls = &.{}, |
| | 207 | .is_tuple = false, |
| | 208 | } }); |
| | 209 | } |
| | 210 | |
| | 211 | const StdErrWriter = struct { |
| | 212 | file: std.fs.File, |
| | 213 | buffered_writer: std.io.BufferedWriter(4096, std.fs.File.Writer), |
| | 214 | tty: std.io.tty.Config, |
| | 215 | writer_ptr: std.io.BufferedWriter(4096, std.fs.File.Writer).Writer, |
| | 216 | |
| | 217 | pub fn init() StdErrWriter { |
| | 218 | const stderr = std.io.getStdErr(); |
| | 219 | const tty = std.io.tty.detectConfig(stderr); |
| | 220 | return .{ |
| | 221 | .file = stderr, |
| | 222 | .tty = tty, |
| | 223 | .buffered_writer = std.io.bufferedWriter(stderr.writer()), |
| | 224 | .writer_ptr = undefined, |
| | 225 | }; |
| | 226 | } |
| | 227 | |
| | 228 | pub fn get(w: *StdErrWriter) std.io.AnyWriter { |
| | 229 | if (@import("builtin").os.tag == .windows and w.tty == .windows_api) { |
| | 230 | // all data must be flushed before changing colors using |
| | 231 | // the Windows API. thankfully, modern windows terminals |
| | 232 | // will not hit this path. |
| | 233 | return w.buffered_writer.unbuffered_writer.any(); |
| | 234 | } else { |
| | 235 | w.writer_ptr = w.buffered_writer.writer(); // uaf unless stored |
| | 236 | return w.writer_ptr.any(); |
| | 237 | } |
| | 238 | } |
| | 239 | }; |
| | 240 | |
| | 241 | const ParseError = error{ InvalidArguments, DisplayedHelpMenu }; |
| | 242 | |
| | 243 | /// This will quit the program with usage information if invalid arguments are |
| | 244 | /// received and the `--help` option is enabled. This is the most common use |
| | 245 | /// case. If manual error handling is desired, `.parseWithErrorWriter` can |
| | 246 | /// be used instead. |
| | 247 | pub fn parse( |
| | 248 | comptime p: Cli, |
| | 249 | program_name: []const u8, |
| | 250 | args: []const []const u8, |
| | 251 | ) p.Parsed() { |
| | 252 | var stderr = StdErrWriter.init(); |
| | 253 | defer stderr.buffered_writer.flush() catch {}; |
| | 254 | return parseWithErrorWriter( |
| | 255 | p, |
| | 256 | program_name, |
| | 257 | args, |
| | 258 | stderr.get(), |
| | 259 | stderr.tty, |
| | 260 | true, |
| | 261 | true, |
| | 262 | ) catch |e| switch (e) { |
| | 263 | error.InvalidArguments, |
| | 264 | error.DisplayedHelpMenu, |
| | 265 | => { |
| | 266 | stderr.buffered_writer.flush() catch {}; |
| | 267 | std.process.exit(1); |
| | 268 | }, |
| | 269 | }; |
| | 270 | } |
| | 271 | |
| | 272 | pub fn parseWithErrorWriter( |
| | 273 | comptime p: Cli, |
| | 274 | program_name: []const u8, |
| | 275 | args: []const []const u8, |
| | 276 | err: std.io.AnyWriter, |
| | 277 | tty: std.io.tty.Config, |
| | 278 | comptime show_help_on_error: bool, |
| | 279 | comptime exit_on_error: bool, |
| | 280 | ) ParseError!p.Parsed() { |
| | 281 | var out: p.Parsed() = .{}; |
| | 282 | var had_errors = false; |
| | 283 | var positionals_counted: usize = 0; |
| | 284 | var first_positional: usize = 0; |
| | 285 | var double_dash: ?usize = null; |
| | 286 | |
| | 287 | var show_help = false; |
| | 288 | |
| | 289 | const max_positionals = p.positionals.maxCount(); |
| | 290 | |
| | 291 | outer: for (args, 0..) |arg, i| { |
| | 292 | if (mem.startsWith(u8, arg, "--")) { |
| | 293 | if (arg.len == 2) { |
| | 294 | if (max_positionals == 0) { |
| | 295 | writeError(err, tty); |
| | 296 | err.writeAll("unexpected end-of-parameter mark: --\n") catch {}; |
| 66 | } | 297 | } |
| 67 | | 298 | |
| 68 | if (std.mem.startsWith(u8, arg, "--dump-state=")) { | 299 | double_dash = i; |
| 69 | dump_state = arg["--dump-state=".len..]; | 300 | const remain = args[i + 1 ..]; |
| 70 | continue; | 301 | if (positionals_counted == 0 and remain.len > 0) |
| | 302 | first_positional = i + 1; |
| | 303 | |
| | 304 | if (p.positionals == .bounded) { |
| | 305 | out.positionals.appendSliceAssumeCapacity(remain[0..@min(remain.len, max_positionals -| positionals_counted)]); |
| 71 | } | 306 | } |
| 72 | | 307 | |
| 73 | if (config.have_embedded_quickjs and std.mem.eql(u8, arg, "--run")) { | 308 | positionals_counted += remain.len; |
| 74 | run = true; | 309 | |
| 75 | continue; | 310 | if (positionals_counted > max_positionals) { |
| | 311 | had_errors = true; |
| | 312 | const diff = @min(remain.len, positionals_counted - max_positionals); |
| | 313 | for (remain[remain.len - diff ..]) |pos| { |
| | 314 | writeError(err, tty); |
| | 315 | err.print("unrecognized argument '{s}'\n", .{pos}) catch {}; |
| | 316 | } |
| 76 | } | 317 | } |
| 77 | | 318 | |
| 78 | std.debug.print("error: unknown flag '{s}'\n", .{arg}); | | |
| 79 | has_errors = true; | | |
| 80 | break; | 319 | break; |
| 81 | } | 320 | } |
| 82 | | 321 | |
| 83 | if (maybe_file_path == null) { | 322 | inline for (p.options) |def| try_this_def: { |
| 84 | maybe_file_path = try std.fs.path.resolve(alloc, &.{ cwd, arg }); | 323 | const prefix = def.name; |
| 85 | } else { | 324 | const kind = def.kind; |
| 86 | std.debug.print("error: multiple files specified\n", .{}); | 325 | const dest = &@field(out, fieldName(def.name)); |
| 87 | maybe_file_path = null; | 326 | |
| 88 | break; | 327 | const is_blank = arg.len == prefix.len; |
| | 328 | if (!(mem.startsWith(u8, arg[2..], prefix[2..]) and (is_blank or arg[prefix.len] == '='))) |
| | 329 | break :try_this_def; |
| | 330 | |
| | 331 | if (kind != .boolean and is_blank) { |
| | 332 | had_errors = true; |
| | 333 | writeError(err, tty); |
| | 334 | err.print(prefix ++ " requires a value\n", .{}) catch {}; |
| | 335 | continue :outer; |
| | 336 | } |
| | 337 | |
| | 338 | const value = if (is_blank) "" else arg[prefix.len + 1 ..]; |
| | 339 | switch (kind) { |
| | 340 | .boolean => if (value.len == 0) { |
| | 341 | dest.* = true; |
| | 342 | } else if (mem.eql(u8, value, "false")) { |
| | 343 | dest.* = false; |
| | 344 | } else if (mem.eql(u8, value, "true")) { |
| | 345 | dest.* = true; |
| | 346 | } else { |
| | 347 | had_errors = true; |
| | 348 | writeError(err, tty); |
| | 349 | err.print(prefix ++ " only accepts true or false, got '{s}'\n", .{ |
| | 350 | arg[prefix.len + 1 ..], |
| | 351 | }) catch {}; |
| | 352 | }, |
| | 353 | |
| | 354 | .string => if (std.unicode.utf8ValidateSlice(value)) { |
| | 355 | dest.* = value; |
| | 356 | } else { |
| | 357 | had_errors = true; |
| | 358 | writeError(err, tty); |
| | 359 | err.print(prefix ++ " requires valid UTF-8 input\n", .{}) catch {}; |
| | 360 | }, |
| | 361 | |
| | 362 | .@"enum" => |E| { |
| | 363 | dest.* = std.meta.stringToEnum(E, value) orelse { |
| | 364 | had_errors = true; |
| | 365 | const values_list = comptime brk: { |
| | 366 | var str: []const u8 = ""; |
| | 367 | for (std.enums.values(E)) |val| { |
| | 368 | str = str ++ " " ++ @tagName(val) ++ "\n"; |
| | 369 | } |
| | 370 | break :brk str; |
| | 371 | }; |
| | 372 | writeError(err, tty); |
| | 373 | err.print(prefix ++ " must be one of the following values, got {s}:\n{s}", .{ |
| | 374 | arg[prefix.len + 1 ..], |
| | 375 | values_list, |
| | 376 | }) catch {}; |
| | 377 | continue :outer; |
| | 378 | }; |
| | 379 | }, |
| | 380 | } |
| | 381 | |
| | 382 | continue :outer; |
| | 383 | } |
| | 384 | |
| | 385 | if (p.help.option) { |
| | 386 | if (mem.startsWith(u8, arg[2..], "help") and (arg.len == "--help".len or arg["--help".len] == '=')) { |
| | 387 | show_help = true; |
| | 388 | if (arg.len > "--help".len) { |
| | 389 | had_errors = true; |
| | 390 | err.print("--help does not take a value\n", .{}) catch {}; |
| | 391 | } |
| | 392 | continue :outer; |
| | 393 | } |
| | 394 | } |
| | 395 | |
| | 396 | had_errors = true; |
| | 397 | writeError(err, tty); |
| | 398 | err.print("unrecognized argument '{s}'\n", .{arg}) catch {}; |
| | 399 | } else if (arg.len > 1 and arg[0] == '-') { |
| | 400 | per_char_in_short: for (arg[1..]) |ch| { |
| | 401 | inline for (p.options) |def| try_this_def: { |
| | 402 | const short = def.short orelse break :try_this_def; |
| | 403 | if (def.kind != .boolean) { |
| | 404 | @compileError("TODO: non-boolean shorthand arguments"); |
| | 405 | } |
| | 406 | if (ch == short) { |
| | 407 | const dest = &@field(out, fieldName(def.name)); |
| | 408 | dest.* = true; |
| | 409 | continue :per_char_in_short; |
| | 410 | } |
| | 411 | if (p.help.option and ch == 'h') { |
| | 412 | show_help = true; |
| | 413 | } |
| | 414 | } |
| | 415 | } |
| | 416 | } else { |
| | 417 | if (positionals_counted == 0) first_positional = i; |
| | 418 | positionals_counted += 1; |
| | 419 | if (positionals_counted > max_positionals) { |
| | 420 | had_errors = true; |
| | 421 | writeError(err, tty); |
| | 422 | err.print("unrecognized argument '{s}'\n", .{arg}) catch {}; |
| | 423 | } else if (p.positionals == .bounded) { |
| | 424 | out.positionals.appendAssumeCapacity(arg); |
| 89 | } | 425 | } |
| 90 | } | 426 | } |
| 91 | } | 427 | } |
| 92 | | 428 | |
| 93 | if (maybe_file_path == null) has_errors = true; | 429 | if (had_errors) { |
| 94 | if (has_errors) { | 430 | if (positionals_counted > max_positionals) { |
| 95 | std.debug.print("Usage: {s} [flags] <input file>\n", .{exe_name.?}); | 431 | writeNote(err, tty); |
| 96 | { | 432 | err.writeAll(switch (max_positionals) { |
| 97 | std.debug.print( | 433 | 0 => "this program does not take positional arguments\n", |
| 98 | // | 60 chars wrap point| | 434 | 1 => "this program accepts one positional argument\n", |
| 99 | \\ | 435 | else => |n| std.fmt.comptimePrint( |
| 100 | \\Flags: | 436 | "this program accepts up to {d} positional arguments\n", |
| 101 | \\ --dump-ir Dump intermediate representations | 437 | .{n}, |
| 102 | \\ --dump-state=<path> Dump compiler state to JSON | 438 | ), |
| 103 | \\ | 439 | }) catch {}; |
| 104 | , .{}); | | |
| 105 | } | 440 | } |
| 106 | if (config.have_embedded_quickjs) { | 441 | if (show_help or show_help_on_error) { |
| 107 | std.debug.print( | 442 | p.showHelpWithWriter(program_name, err, tty); |
| 108 | \\ --run Run program using QuickJS | | |
| 109 | \\ | | |
| 110 | , .{}); | | |
| 111 | } | 443 | } |
| 112 | if (have_debug_features) { | 444 | if (exit_on_error) { |
| 113 | std.debug.print( | 445 | return error.DisplayedHelpMenu; |
| 114 | \\ | | |
| 115 | \\Debug Mode Flags: | | |
| 116 | \\ --report-trace Print stack traces per error | | |
| 117 | \\ --no-leak-checks | | |
| 118 | \\ | | |
| 119 | , .{}); | | |
| 120 | } | 446 | } |
| 121 | std.debug.print("\nLanguage SDK v" ++ config.version_string ++ "\n", .{}); | 447 | return error.InvalidArguments; |
| 122 | return 1; | | |
| 123 | } | 448 | } |
| 124 | const file_path = maybe_file_path.?; | | |
| 125 | | 449 | |
| 126 | // Execute compiler | 450 | if (show_help) { |
| 127 | { | 451 | p.showHelpWithWriter(program_name, err, tty); |
| 128 | var js_backend = core.Js{}; | 452 | return error.DisplayedHelpMenu; |
| 129 | | 453 | } |
| 130 | var comp = try core.Comp.init(.{ | | |
| 131 | .gpa = tracer.tracingAllocator(&alloc), | | |
| 132 | .options = .{ | | |
| 133 | .dump_ir = dump_ir, | | |
| 134 | }, | | |
| 135 | .backend = .{ .js = &js_backend }, | | |
| 136 | }); | | |
| 137 | defer comp.deinit(); | | |
| 138 | | 454 | |
| 139 | defer if (dump_state) |path| { | 455 | out.positionals = switch (p.positionals) { |
| 140 | dumpState(&comp, path) catch |err| { | 456 | .none => {}, |
| 141 | std.debug.print("Failed to dump compilation state: {}", .{err}); | 457 | .single => if (positionals_counted > 0) |
| 142 | }; | 458 | args[first_positional] |
| 143 | }; | 459 | else |
| | 460 | null, |
| | 461 | .bounded => out.positionals, // already correct |
| | 462 | .unlimited, .limited => if (max_positionals > 0) .{ |
| | 463 | .first = first_positional, |
| | 464 | .len = positionals_counted, |
| | 465 | .first_is_after_double_dash = if (double_dash) |dd| |
| | 466 | dd < first_positional |
| | 467 | else |
| | 468 | false, |
| | 469 | }, |
| | 470 | }; |
| | 471 | |
| | 472 | return out; |
| | 473 | } |
| 144 | | 474 | |
| 145 | const main_file = try comp.getFile(file_path); | 475 | pub fn showHelp(comptime p: Cli, program_name: []const u8) void { |
| | 476 | var stderr = StdErrWriter.init(); |
| | 477 | defer stderr.buffered_writer.flush() catch {}; |
| | 478 | return showHelpWithWriter( |
| | 479 | p, |
| | 480 | program_name, |
| | 481 | stderr.get(), |
| | 482 | stderr.tty, |
| | 483 | ); |
| | 484 | } |
| 146 | | 485 | |
| 147 | var fail_report: Report = undefined; | 486 | pub fn showHelpWithWriter( |
| 148 | const teg = main_file.getTeg(&comp, &fail_report) catch |err| switch (err) { | 487 | comptime p: Cli, |
| 149 | error.TransformFail => { | 488 | program_name: []const u8, |
| 150 | std.debug.print("Transform failed:\n", .{}); | 489 | w: std.io.AnyWriter, |
| 151 | try fail_report.dump(.initComp(&comp, main_file), comp.out); | 490 | tty: std.io.tty.Config, |
| 152 | fail_report.deinit(comp.gpa); | 491 | ) void { |
| 153 | return 1; | 492 | const usage = p.help.usage orelse default: { |
| 154 | }, | 493 | const opts_usage = if (p.options.len > 0 or p.help.option) "[options...]" else ""; |
| 155 | error.ParseFail => { | 494 | break :default switch (p.positionals.maxCount()) { |
| 156 | // Already printed. | 495 | 0 => opts_usage, |
| 157 | return 1; | 496 | 1 => mem.trimLeft(u8, opts_usage ++ " [argument]", " "), |
| 158 | }, | 497 | else => mem.trimLeft(u8, opts_usage ++ " [args...]", " "), |
| 159 | else => return err, | | |
| 160 | }; | 498 | }; |
| | 499 | }; |
| | 500 | |
| | 501 | w.print("usage: {s} {s}\n", .{ program_name, usage }) catch return; |
| | 502 | |
| | 503 | if (p.options.len > 0 or p.help.option) { |
| | 504 | w.writeAll("\n") catch return; |
| | 505 | tty.setColor(w, .bold) catch return; |
| | 506 | w.writeAll("options:") catch return; |
| | 507 | tty.setColor(w, .reset) catch return; |
| | 508 | w.writeAll(comptime brk: { |
| | 509 | var width = 0; |
| | 510 | var any_short_options = false; |
| | 511 | |
| | 512 | const all_opts = if (p.help.option) |
| | 513 | p.options ++ [_]Option{.help} |
| | 514 | else |
| | 515 | p.options; |
| | 516 | for (all_opts) |opt| { |
| | 517 | width = @max(width, opt.name.len); |
| | 518 | any_short_options = any_short_options or opt.short != null; |
| | 519 | } |
| 161 | | 520 | |
| 162 | if (comp.options.dump_ir) { | 521 | width += " ".len; |
| 163 | const stderr = std.io.getStdErr().writer(); | 522 | if (any_short_options) width += "-h ".len; |
| 164 | try teg.dumpToWriter(stderr, null); | 523 | const sub = if (any_short_options) 4 else 1; |
| 165 | } | | |
| 166 | | | |
| 167 | const entry_point = try comp.enqueueEntryModule(main_file); | | |
| 168 | comp.runAllJobs() catch |err| switch (err) { | | |
| 169 | error.AnalysisFail => { | | |
| 170 | // error is already printed | | |
| 171 | return 1; | | |
| 172 | }, | | |
| 173 | else => return err, | | |
| 174 | }; | | |
| 175 | | 524 | |
| 176 | if (config.have_embedded_quickjs and run) { | 525 | var out: []const u8 = "\n"; |
| 177 | // Execute with QuickJS | 526 | for (all_opts) |opt| { |
| 178 | var out: std.ArrayListUnmanaged(u8) = .{}; | 527 | out = out ++ " "; |
| 179 | defer out.deinit(alloc); | 528 | if (any_short_options) { |
| 180 | try js_backend.link(&comp, entry_point, out.writer(alloc).any()); | 529 | out = out ++ if (opt.short) |s| [_]u8{ '-', s, ' ' } else " "; |
| 181 | try out.append(alloc, 0); | 530 | } |
| 182 | try runWithQuickJs(out.items[0 .. out.items.len - 1 :0]); | 531 | out = out ++ opt.name ++ (" " ** (width - opt.name.len - sub)); |
| 183 | } else { | 532 | // TODO: wrapping at 72 columns |
| 184 | // Output to standard out | 533 | out = out ++ opt.desc ++ "\n"; |
| 185 | const output = std.io.getStdOut().writer(); | 534 | } |
| 186 | var bufw = std.io.bufferedWriter(output); | 535 | break :brk out ++ "\n"; |
| 187 | const writer = bufw.writer(); | 536 | }) catch return; |
| 188 | try js_backend.link(&comp, entry_point, writer.any()); | | |
| 189 | try bufw.flush(); | | |
| 190 | } | | |
| 191 | } | 537 | } |
| 192 | | | |
| 193 | std.process.cleanExit(); | | |
| 194 | return 0; | | |
| 195 | } | 538 | } |
| 196 | | 539 | |
| 197 | fn runWithQuickJs(js: [:0]const u8) !void { | 540 | pub fn showHelpAndExit(comptime p: Cli, program_name: []const u8) noreturn { |
| 198 | const qjs = @import("quickjs"); | 541 | p.showHelp(program_name); |
| 199 | | 542 | std.process.exit(1); |
| 200 | const runtime = qjs.JS_NewRuntime() orelse | 543 | } |
| 201 | return error.UnexpectedError; | | |
| 202 | defer qjs.JS_FreeRuntime(runtime); | | |
| 203 | const js_ctx = qjs.JS_NewContext(runtime) orelse | | |
| 204 | return error.UnexpectedError; | | |
| 205 | defer qjs.JS_FreeContext(js_ctx); | | |
| 206 | | | |
| 207 | // QuickJS provides a completely blank global context. It is a non-goal to | | |
| 208 | // be a full runtime, but adding simple globals to align with expectations | | |
| 209 | // is OK. | | |
| 210 | | | |
| 211 | // 0 and null is zero process args | | |
| 212 | qjs.js_std_add_helpers(js_ctx, 0, null); | | |
| 213 | | | |
| 214 | const result = qjs.JS_Eval( | | |
| 215 | js_ctx, | | |
| 216 | js.ptr, | | |
| 217 | js.len, | | |
| 218 | "test.js", | | |
| 219 | qjs.JS_EVAL_TYPE_GLOBAL, | | |
| 220 | ); | | |
| 221 | defer qjs.JS_FreeValue(js_ctx, result); | | |
| 222 | | 544 | |
| 223 | if (qjs.JS_IsException(result) != 0) { | 545 | fn writeError(err: std.io.AnyWriter, tty: std.io.tty.Config) void { |
| 224 | const exception = qjs.JS_GetException(js_ctx); | 546 | tty.setColor(err, .red) catch return; |
| 225 | defer qjs.JS_FreeValue(js_ctx, exception); | 547 | tty.setColor(err, .bold) catch return; |
| | 548 | err.writeAll("error") catch return; |
| | 549 | tty.setColor(err, .reset) catch return; |
| | 550 | tty.setColor(err, .dim) catch return; |
| | 551 | err.writeAll(": ") catch return; |
| | 552 | tty.setColor(err, .reset) catch return; |
| | 553 | } |
| 226 | | 554 | |
| 227 | const exception_str = qjs.JS_ToCString(js_ctx, exception); | 555 | fn writeNote(err: std.io.AnyWriter, tty: std.io.tty.Config) void { |
| 228 | defer qjs.JS_FreeCString(js_ctx, exception_str); | 556 | tty.setColor(err, .blue) catch return; |
| | 557 | tty.setColor(err, .bold) catch return; |
| | 558 | err.writeAll("note") catch return; |
| | 559 | tty.setColor(err, .reset) catch return; |
| | 560 | tty.setColor(err, .dim) catch return; |
| | 561 | err.writeAll(": ") catch return; |
| | 562 | tty.setColor(err, .reset) catch return; |
| | 563 | } |
| 229 | | 564 | |
| 230 | const slice = std.mem.span(exception_str); | 565 | /// Splits args[0] containing the program name |
| | 566 | /// |
| | 567 | /// const program_name, const args = programArgsAlloc(); |
| | 568 | pub fn programArgsAlloc(allocator: std.mem.Allocator, fallback_name: []const u8) !struct { []const u8, [][:0]u8 } { |
| | 569 | const magic_number = @alignOf([*][:0]u8); |
| | 570 | var raw_args = try std.process.argsAlloc(allocator); |
| | 571 | switch (raw_args.len) { |
| | 572 | 0 => { |
| | 573 | raw_args.ptr = @ptrFromInt(magic_number); |
| | 574 | return .{ fallback_name, raw_args }; |
| | 575 | }, |
| | 576 | else => return .{ |
| | 577 | std.fs.path.basename(raw_args[0]), |
| | 578 | raw_args[1..], |
| | 579 | }, |
| | 580 | } |
| | 581 | } |
| 231 | | 582 | |
| 232 | std.debug.print("Exception: {s}\n", .{slice}); | 583 | pub fn programArgsFree(allocator: std.mem.Allocator, args: [][:0]u8) void { |
| | 584 | const magic_number = @alignOf([*][:0]u8); |
| | 585 | if (args.len == 0 and @intFromPtr(args.ptr) == magic_number) return; |
| | 586 | const raw_args = (args.ptr - 1)[0 .. args.len + 1]; |
| | 587 | std.process.argsFree(allocator, raw_args); |
| | 588 | } |
| 233 | | 589 | |
| 234 | std.process.exit(1); | 590 | pub fn fatalWithHelp(p: Cli, program_name: []const u8, comptime fmt: []const u8, args: anytype) noreturn { |
| | 591 | print: { |
| | 592 | const stderr = std.io.getStdErr(); |
| | 593 | const tty = std.io.tty.detectConfig(stderr); |
| | 594 | var bufw = std.io.bufferedWriter(stderr.writer()); |
| | 595 | writeError(bufw.writer().any(), tty); |
| | 596 | bufw.writer().print(fmt ++ "\n", args) catch break :print; |
| | 597 | p.showHelpWithWriter(program_name, bufw.writer().any(), tty); |
| | 598 | bufw.flush() catch break :print; |
| 235 | } | 599 | } |
| | 600 | std.process.exit(1); |
| 236 | } | 601 | } |
| 237 | | 602 | |
| 238 | const core = @import("core"); | 603 | pub fn fatal(comptime fmt: []const u8, args: anytype) noreturn { |
| 239 | const Comp = core.Comp; | 604 | const stderr = std.io.getStdErr(); |
| 240 | const Report = core.Report; | 605 | const tty = std.io.tty.detectConfig(stderr); |
| | 606 | var bufw = std.io.bufferedWriter(stderr.writer()); |
| | 607 | writeError(bufw.writer().any(), tty); |
| | 608 | bufw.writer().print(fmt ++ "\n", args); |
| | 609 | bufw.flush() catch {}; // ignoring I/O errors |
| | 610 | std.process.exit(1); |
| | 611 | } |
| 241 | | 612 | |
| 242 | const tracer = @import("tracer"); | 613 | pub fn main() !void { |
| 243 | pub const tracer_impl = @field(tracer, @tagName(@import("config").tracing_mode)); | 614 | var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init; |
| | 615 | defer _ = gpa.deinit(); |
| | 616 | const alloc = gpa.allocator(); |
| 244 | | 617 | |
| 245 | const std = @import("std"); | 618 | const config: Cli = .{ |
| 246 | const builtin = @import("builtin"); | 619 | .options = &.{ |
| | 620 | .{ .name = "--flag", .desc = "check for something" }, |
| | 621 | .{ .name = "--str", .desc = "pass a string", .kind = .string }, |
| | 622 | .{ |
| | 623 | .name = "--enum", |
| | 624 | .desc = "enum", |
| | 625 | .kind = .{ .@"enum" = std.builtin.OptimizeMode }, |
| | 626 | }, |
| | 627 | }, |
| | 628 | .positionals = .{ .bounded = 2 }, |
| | 629 | }; |
| | 630 | |
| | 631 | const program_name, const args = try Cli.programArgsAlloc(alloc); |
| | 632 | defer Cli.programArgsFree(alloc, args); |
| | 633 | |
| | 634 | const parsed = config.parse(args[1..]) catch |
| | 635 | config.showHelpAndExit(program_name); |
| 247 | | 636 | |
| 248 | fn dumpState(comp: *const Comp, dump_path: []const u8) !void { | 637 | std.debug.print("--flag: {any}\n", .{parsed.flag}); |
| 249 | const file = try std.fs.cwd().createFile(dump_path, .{}); | 638 | std.debug.print("--str: {?s}\n", .{parsed.str}); |
| 250 | defer file.close(); | 639 | std.debug.print("--enum: {any}\n", .{parsed.@"enum"}); |
| | 640 | std.debug.print("positionals({d}):\n", .{parsed.positionals.len}); |
| 251 | | 641 | |
| 252 | var bufw = std.io.bufferedWriter(file.writer()); | 642 | // if using '.positionals = .unlimited' |
| 253 | var jw = std.json.writeStream(bufw.writer(), .{ | 643 | // var it = parsed.positionals.iterator(args[1..]); |
| 254 | .whitespace = .indent_2, | 644 | // while (it.next()) |item| { |
| 255 | }); | 645 | // std.debug.print(" - {s}\n", .{item}); |
| | 646 | // } |
| 256 | | 647 | |
| 257 | try comp.jsonStringify(&jw); | 648 | for (parsed.positionals.slice()) |item| { |
| 258 | try bufw.writer().writeAll("\n"); | 649 | std.debug.print(" - {s}\n", .{item}); |
| 259 | try bufw.flush(); | 650 | } |
| 260 | } | 651 | } |
| | 652 | |
| | 653 | const Cli = @This(); |
| | 654 | const std = @import("std"); |
| | 655 | const mem = std.mem; |
| | 656 | const Type = std.builtin.Type; |