|
| 1 | +const std = @import("std"); |
| 2 | +const mem = std.mem; |
| 3 | + |
| 4 | +pub const list = @import("clang_options_data.zig").data; |
| 5 | + |
| 6 | +pub const CliArg = struct { |
| 7 | + name: []const u8, |
| 8 | + syntax: Syntax, |
| 9 | + |
| 10 | + /// TODO we're going to want to change this when we start shipping self-hosted because this causes |
| 11 | + /// all the functions in stage2.zig to get exported. |
| 12 | + zig_equivalent: @import("stage2.zig").ClangArgIterator.ZigEquivalent, |
| 13 | + |
| 14 | + /// Prefixed by "-" |
| 15 | + pd1: bool = false, |
| 16 | + |
| 17 | + /// Prefixed by "--" |
| 18 | + pd2: bool = false, |
| 19 | + |
| 20 | + /// Prefixed by "/" |
| 21 | + psl: bool = false, |
| 22 | + |
| 23 | + pub const Syntax = union(enum) { |
| 24 | + /// A flag with no values. |
| 25 | + flag, |
| 26 | + |
| 27 | + /// An option which prefixes its (single) value. |
| 28 | + joined, |
| 29 | + |
| 30 | + /// An option which is followed by its value. |
| 31 | + separate, |
| 32 | + |
| 33 | + /// An option which is either joined to its (non-empty) value, or followed by its value. |
| 34 | + joined_or_separate, |
| 35 | + |
| 36 | + /// An option which is both joined to its (first) value, and followed by its (second) value. |
| 37 | + joined_and_separate, |
| 38 | + |
| 39 | + /// An option followed by its values, which are separated by commas. |
| 40 | + comma_joined, |
| 41 | + |
| 42 | + /// An option which consumes an optional joined argument and any other remaining arguments. |
| 43 | + remaining_args_joined, |
| 44 | + |
| 45 | + /// An option which is which takes multiple (separate) arguments. |
| 46 | + multi_arg: u8, |
| 47 | + }; |
| 48 | + |
| 49 | + pub fn matchEql(self: CliArg, arg: []const u8) u2 { |
| 50 | + if (self.pd1 and arg.len >= self.name.len + 1 and |
| 51 | + mem.startsWith(u8, arg, "-") and mem.eql(u8, arg[1..], self.name)) |
| 52 | + { |
| 53 | + return 1; |
| 54 | + } |
| 55 | + if (self.pd2 and arg.len >= self.name.len + 2 and |
| 56 | + mem.startsWith(u8, arg, "--") and mem.eql(u8, arg[2..], self.name)) |
| 57 | + { |
| 58 | + return 2; |
| 59 | + } |
| 60 | + if (self.psl and arg.len >= self.name.len + 1 and |
| 61 | + mem.startsWith(u8, arg, "/") and mem.eql(u8, arg[1..], self.name)) |
| 62 | + { |
| 63 | + return 1; |
| 64 | + } |
| 65 | + return 0; |
| 66 | + } |
| 67 | + |
| 68 | + pub fn matchStartsWith(self: CliArg, arg: []const u8) usize { |
| 69 | + if (self.pd1 and arg.len >= self.name.len + 1 and |
| 70 | + mem.startsWith(u8, arg, "-") and mem.startsWith(u8, arg[1..], self.name)) |
| 71 | + { |
| 72 | + return self.name.len + 1; |
| 73 | + } |
| 74 | + if (self.pd2 and arg.len >= self.name.len + 2 and |
| 75 | + mem.startsWith(u8, arg, "--") and mem.startsWith(u8, arg[2..], self.name)) |
| 76 | + { |
| 77 | + return self.name.len + 2; |
| 78 | + } |
| 79 | + if (self.psl and arg.len >= self.name.len + 1 and |
| 80 | + mem.startsWith(u8, arg, "/") and mem.startsWith(u8, arg[1..], self.name)) |
| 81 | + { |
| 82 | + return self.name.len + 1; |
| 83 | + } |
| 84 | + return 0; |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +/// Shortcut function for initializing a `CliArg` |
| 89 | +pub fn flagpd1(name: []const u8) CliArg { |
| 90 | + return .{ |
| 91 | + .name = name, |
| 92 | + .syntax = .flag, |
| 93 | + .zig_equivalent = .other, |
| 94 | + .pd1 = true, |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +/// Shortcut function for initializing a `CliArg` |
| 99 | +pub fn joinpd1(name: []const u8) CliArg { |
| 100 | + return .{ |
| 101 | + .name = name, |
| 102 | + .syntax = .joined, |
| 103 | + .zig_equivalent = .other, |
| 104 | + .pd1 = true, |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +/// Shortcut function for initializing a `CliArg` |
| 109 | +pub fn jspd1(name: []const u8) CliArg { |
| 110 | + return .{ |
| 111 | + .name = name, |
| 112 | + .syntax = .joined_or_separate, |
| 113 | + .zig_equivalent = .other, |
| 114 | + .pd1 = true, |
| 115 | + }; |
| 116 | +} |
| 117 | + |
| 118 | +/// Shortcut function for initializing a `CliArg` |
| 119 | +pub fn sepd1(name: []const u8) CliArg { |
| 120 | + return .{ |
| 121 | + .name = name, |
| 122 | + .syntax = .separate, |
| 123 | + .zig_equivalent = .other, |
| 124 | + .pd1 = true, |
| 125 | + }; |
| 126 | +} |
0 commit comments