Skip to content

Commit

Permalink
Compiler crash
Browse files Browse the repository at this point in the history
This code emits a compiler crash with zig-0.11
  • Loading branch information
voroskoi committed Nov 2, 2023
1 parent fa72bdb commit 62e713f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
43 changes: 43 additions & 0 deletions clap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@ test "parseParams" {
\\--str <str>
\\--str [str]
\\--str [str=foobar]
\\-s, --switch [bool]
\\-s <bool>
\\-s [bool=on]
\\-s, --str <str>
\\-s, --long <val> Help text
\\-s, --long <val>... Help text
Expand Down Expand Up @@ -529,6 +532,27 @@ test "parseParams" {
.takes_value = .one,
.required = false,
},
.{
//-s, --switch [bool]
.id = .{ .val = "bool" },
.names = .{ .short = 's', .long = "switch" },
.takes_value = .one,
.required = false,
},
.{
//-s <bool>
.id = .{ .val = "bool" },
.names = .{ .short = 's' },
.takes_value = .one,
.required = true,
},
.{
//-s [bool=on]
.id = .{ .val = "bool", .default = "on" },
.names = .{ .short = 's' },
.takes_value = .one,
.required = false,
},
.{
.id = .{ .val = "str" },
.names = .{ .short = 's', .long = "str" },
Expand Down Expand Up @@ -792,6 +816,7 @@ pub fn parseEx(
iter: anytype,
opt: ParseOptions,
) !ResultEx(Id, params, value_parsers) {
std.debug.print("parseEx called\n", .{});
const allocator = opt.allocator;
const Positional = FindPositionalType(Id, params, value_parsers);

Expand All @@ -804,6 +829,7 @@ pub fn parseEx(
.iter = iter,
.diagnostic = opt.diagnostic,
};
std.debug.print("state reached\n", .{});
while (try stream.next()) |arg| {
// TODO: We cannot use `try` inside the inline for because of a compiler bug that
// generates an infinite loop. For now, use a variable to store the error
Expand Down Expand Up @@ -1000,6 +1026,23 @@ fn Arguments(
} });
}

test "bool" {
const params = comptime parseParamsComptime(
\\--switch <bool>
\\--num <u64>
\\-s [bool=on]
\\[str]
);

var iter = args.SliceIterator{
.args = &.{ "--num", "10", "--switch", "1", "something" },
};
var res = try parseEx(Help, &params, parsers.default, &iter, .{
.allocator = testing.allocator,
});
defer res.deinit();
}

test "str and u64" {
const params = comptime parseParamsComptime(
\\--str <str>
Expand Down
19 changes: 19 additions & 0 deletions clap/parsers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fmt = std.fmt;
const testing = std.testing;

pub const default = .{
.bool = boolean,
.string = string,
.str = string,
.u8 = int(u8, 0),
Expand All @@ -29,6 +30,24 @@ test "string" {
try testing.expectEqualStrings("aa", try string("aa"));
}

/// Parse boolean value. False, off and 0 considered false, everything else is true.
pub fn boolean(in: []const u8) error{ParameterTooLong}!bool {
// check if input is not too long
const MAX_LEN = 16;
if (in.len >= MAX_LEN) return error.ParameterTooLong;

// convert to lowercase
var lower: [MAX_LEN]u8 = undefined;
for (in, 0..) |c, i| {
lower[i] = std.ascii.toLower(c);
}

if (std.mem.eql(u8, "0", lower[0..in.len]) or
std.mem.eql(u8, "off", lower[0..in.len]) or
std.mem.eql(u8, "false", lower[0..in.len])) return false;
return true;
}

/// A parser that uses `std.fmt.parseInt` to parse the string into an integer value.
/// See `std.fmt.parseInt` documentation for more information.
pub fn int(comptime T: type, comptime base: u8) fn ([]const u8) fmt.ParseIntError!T {
Expand Down

0 comments on commit 62e713f

Please sign in to comment.