2020-03-21 07:12:28 -07:00
|
|
|
const std = @import("std");
|
|
|
|
const mem = std.mem;
|
|
|
|
|
|
|
|
pub const list = @import("clang_options_data.zig").data;
|
|
|
|
|
|
|
|
pub const CliArg = struct {
|
|
|
|
name: []const u8,
|
|
|
|
syntax: Syntax,
|
|
|
|
|
|
|
|
/// TODO we're going to want to change this when we start shipping self-hosted because this causes
|
|
|
|
/// all the functions in stage2.zig to get exported.
|
|
|
|
zig_equivalent: @import("stage2.zig").ClangArgIterator.ZigEquivalent,
|
|
|
|
|
|
|
|
/// Prefixed by "-"
|
|
|
|
pd1: bool = false,
|
|
|
|
|
|
|
|
/// Prefixed by "--"
|
|
|
|
pd2: bool = false,
|
|
|
|
|
|
|
|
/// Prefixed by "/"
|
|
|
|
psl: bool = false,
|
|
|
|
|
2020-03-21 12:29:52 -07:00
|
|
|
pub const Syntax = union(enum) {
|
2020-03-21 07:12:28 -07:00
|
|
|
/// A flag with no values.
|
|
|
|
flag,
|
|
|
|
|
|
|
|
/// An option which prefixes its (single) value.
|
|
|
|
joined,
|
|
|
|
|
|
|
|
/// An option which is followed by its value.
|
|
|
|
separate,
|
|
|
|
|
|
|
|
/// An option which is either joined to its (non-empty) value, or followed by its value.
|
|
|
|
joined_or_separate,
|
|
|
|
|
|
|
|
/// An option which is both joined to its (first) value, and followed by its (second) value.
|
|
|
|
joined_and_separate,
|
|
|
|
|
|
|
|
/// An option followed by its values, which are separated by commas.
|
|
|
|
comma_joined,
|
|
|
|
|
|
|
|
/// An option which consumes an optional joined argument and any other remaining arguments.
|
|
|
|
remaining_args_joined,
|
|
|
|
|
|
|
|
/// An option which is which takes multiple (separate) arguments.
|
|
|
|
multi_arg: u8,
|
|
|
|
};
|
|
|
|
|
2020-03-21 19:30:46 -07:00
|
|
|
pub fn matchEql(self: CliArg, arg: []const u8) u2 {
|
2020-03-21 07:12:28 -07:00
|
|
|
if (self.pd1 and arg.len >= self.name.len + 1 and
|
|
|
|
mem.startsWith(u8, arg, "-") and mem.eql(u8, arg[1..], self.name))
|
|
|
|
{
|
2020-03-21 19:30:46 -07:00
|
|
|
return 1;
|
2020-03-21 07:12:28 -07:00
|
|
|
}
|
|
|
|
if (self.pd2 and arg.len >= self.name.len + 2 and
|
|
|
|
mem.startsWith(u8, arg, "--") and mem.eql(u8, arg[2..], self.name))
|
|
|
|
{
|
2020-03-21 19:30:46 -07:00
|
|
|
return 2;
|
2020-03-21 07:12:28 -07:00
|
|
|
}
|
|
|
|
if (self.psl and arg.len >= self.name.len + 1 and
|
|
|
|
mem.startsWith(u8, arg, "/") and mem.eql(u8, arg[1..], self.name))
|
|
|
|
{
|
2020-03-21 19:30:46 -07:00
|
|
|
return 1;
|
2020-03-21 07:12:28 -07:00
|
|
|
}
|
2020-03-21 19:30:46 -07:00
|
|
|
return 0;
|
2020-03-21 07:12:28 -07:00
|
|
|
}
|
|
|
|
|
2020-03-21 12:29:52 -07:00
|
|
|
pub fn matchStartsWith(self: CliArg, arg: []const u8) usize {
|
2020-03-21 07:12:28 -07:00
|
|
|
if (self.pd1 and arg.len >= self.name.len + 1 and
|
|
|
|
mem.startsWith(u8, arg, "-") and mem.startsWith(u8, arg[1..], self.name))
|
|
|
|
{
|
|
|
|
return self.name.len + 1;
|
|
|
|
}
|
|
|
|
if (self.pd2 and arg.len >= self.name.len + 2 and
|
|
|
|
mem.startsWith(u8, arg, "--") and mem.startsWith(u8, arg[2..], self.name))
|
|
|
|
{
|
|
|
|
return self.name.len + 2;
|
|
|
|
}
|
|
|
|
if (self.psl and arg.len >= self.name.len + 1 and
|
|
|
|
mem.startsWith(u8, arg, "/") and mem.startsWith(u8, arg[1..], self.name))
|
|
|
|
{
|
|
|
|
return self.name.len + 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Shortcut function for initializing a `CliArg`
|
|
|
|
pub fn flagpd1(name: []const u8) CliArg {
|
|
|
|
return .{
|
|
|
|
.name = name,
|
|
|
|
.syntax = .flag,
|
|
|
|
.zig_equivalent = .other,
|
|
|
|
.pd1 = true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-02 18:15:36 -07:00
|
|
|
/// Shortcut function for initializing a `CliArg`
|
|
|
|
pub fn flagpsl(name: []const u8) CliArg {
|
|
|
|
return .{
|
|
|
|
.name = name,
|
|
|
|
.syntax = .flag,
|
|
|
|
.zig_equivalent = .other,
|
|
|
|
.psl = true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-21 07:12:28 -07:00
|
|
|
/// Shortcut function for initializing a `CliArg`
|
|
|
|
pub fn joinpd1(name: []const u8) CliArg {
|
|
|
|
return .{
|
|
|
|
.name = name,
|
|
|
|
.syntax = .joined,
|
|
|
|
.zig_equivalent = .other,
|
|
|
|
.pd1 = true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shortcut function for initializing a `CliArg`
|
|
|
|
pub fn jspd1(name: []const u8) CliArg {
|
|
|
|
return .{
|
|
|
|
.name = name,
|
|
|
|
.syntax = .joined_or_separate,
|
|
|
|
.zig_equivalent = .other,
|
|
|
|
.pd1 = true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shortcut function for initializing a `CliArg`
|
|
|
|
pub fn sepd1(name: []const u8) CliArg {
|
|
|
|
return .{
|
|
|
|
.name = name,
|
|
|
|
.syntax = .separate,
|
|
|
|
.zig_equivalent = .other,
|
|
|
|
.pd1 = true,
|
|
|
|
};
|
|
|
|
}
|