diff options
| author | 2018-06-08 10:41:37 +0200 | |
|---|---|---|
| committer | 2018-06-08 10:41:37 +0200 | |
| commit | 765c82671a733991c46f3d3e14297bc94d6b56c3 (patch) | |
| tree | 77d5db9a7bf5f5e96ec4b84024aeeda5061acf5a | |
| parent | Reworked extended.zig again! (diff) | |
| download | zig-clap-765c82671a733991c46f3d3e14297bc94d6b56c3.tar.gz zig-clap-765c82671a733991c46f3d3e14297bc94d6b56c3.tar.xz zig-clap-765c82671a733991c46f3d3e14297bc94d6b56c3.zip | |
Updated to newest pointer syntax
| -rw-r--r-- | build.zig | 2 | ||||
| -rw-r--r-- | examples/core.zig | 26 | ||||
| -rw-r--r-- | src/core.zig | 26 | ||||
| -rw-r--r-- | src/extended.zig | 37 | ||||
| -rw-r--r-- | tests/extended.zig | 4 |
5 files changed, 46 insertions, 49 deletions
| @@ -1,6 +1,6 @@ | |||
| 1 | const Builder = @import("std").build.Builder; | 1 | const Builder = @import("std").build.Builder; |
| 2 | 2 | ||
| 3 | pub fn build(b: &Builder) void { | 3 | pub fn build(b: *Builder) void { |
| 4 | const mode = b.standardReleaseOptions(); | 4 | const mode = b.standardReleaseOptions(); |
| 5 | 5 | ||
| 6 | { | 6 | { |
diff --git a/examples/core.zig b/examples/core.zig index eb512ef..4335ff2 100644 --- a/examples/core.zig +++ b/examples/core.zig | |||
| @@ -10,7 +10,7 @@ const Param = clap.Param; | |||
| 10 | const ArgError = clap.OsArgIterator.Error; | 10 | const ArgError = clap.OsArgIterator.Error; |
| 11 | 11 | ||
| 12 | // TODO: More specific error in this func type. | 12 | // TODO: More specific error in this func type. |
| 13 | const Command = fn(&mem.Allocator, &clap.ArgIterator(ArgError)) error!void; | 13 | const Command = fn(*mem.Allocator, *clap.ArgIterator(ArgError)) error!void; |
| 14 | 14 | ||
| 15 | const params = []Param(Command){ | 15 | const params = []Param(Command){ |
| 16 | Param(Command).init(help, false, Names.prefix("help")), | 16 | Param(Command).init(help, false, Names.prefix("help")), |
| @@ -74,7 +74,7 @@ pub fn main() !void { | |||
| 74 | try arg.param.id(allocator, parser.iter); | 74 | try arg.param.id(allocator, parser.iter); |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | pub fn help(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { | 77 | pub fn help(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { |
| 78 | // debug.warn(usage); TODO: error: evaluation exceeded 1000 backwards branches | 78 | // debug.warn(usage); TODO: error: evaluation exceeded 1000 backwards branches |
| 79 | } | 79 | } |
| 80 | 80 | ||
| @@ -147,7 +147,7 @@ const missing_build_file = | |||
| 147 | \\ | 147 | \\ |
| 148 | ; | 148 | ; |
| 149 | 149 | ||
| 150 | fn cmdBuild(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) !void { | 150 | fn cmdBuild(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) !void { |
| 151 | var init = false; | 151 | var init = false; |
| 152 | var build_file: []const u8 = "build.zig"; | 152 | var build_file: []const u8 = "build.zig"; |
| 153 | var cache_dir: []const u8 = "zig-cache"; | 153 | var cache_dir: []const u8 = "zig-cache"; |
| @@ -323,17 +323,17 @@ const build_generic_usage = | |||
| 323 | ; | 323 | ; |
| 324 | 324 | ||
| 325 | 325 | ||
| 326 | fn cmdBuildExe(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { | 326 | fn cmdBuildExe(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { |
| 327 | } | 327 | } |
| 328 | 328 | ||
| 329 | // cmd:build-lib /////////////////////////////////////////////////////////////////////////////////// | 329 | // cmd:build-lib /////////////////////////////////////////////////////////////////////////////////// |
| 330 | 330 | ||
| 331 | fn cmdBuildLib(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { | 331 | fn cmdBuildLib(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { |
| 332 | } | 332 | } |
| 333 | 333 | ||
| 334 | // cmd:build-obj /////////////////////////////////////////////////////////////////////////////////// | 334 | // cmd:build-obj /////////////////////////////////////////////////////////////////////////////////// |
| 335 | 335 | ||
| 336 | fn cmdBuildObj(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { | 336 | fn cmdBuildObj(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { |
| 337 | } | 337 | } |
| 338 | 338 | ||
| 339 | // cmd:fmt ///////////////////////////////////////////////////////////////////////////////////////// | 339 | // cmd:fmt ///////////////////////////////////////////////////////////////////////////////////////// |
| @@ -350,17 +350,17 @@ const usage_fmt = | |||
| 350 | \\ | 350 | \\ |
| 351 | ; | 351 | ; |
| 352 | 352 | ||
| 353 | fn cmdFmt(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { | 353 | fn cmdFmt(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { |
| 354 | } | 354 | } |
| 355 | 355 | ||
| 356 | // cmd:targets ///////////////////////////////////////////////////////////////////////////////////// | 356 | // cmd:targets ///////////////////////////////////////////////////////////////////////////////////// |
| 357 | 357 | ||
| 358 | fn cmdTargets(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { | 358 | fn cmdTargets(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { |
| 359 | } | 359 | } |
| 360 | 360 | ||
| 361 | // cmd:version ///////////////////////////////////////////////////////////////////////////////////// | 361 | // cmd:version ///////////////////////////////////////////////////////////////////////////////////// |
| 362 | 362 | ||
| 363 | fn cmdVersion(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { | 363 | fn cmdVersion(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { |
| 364 | } | 364 | } |
| 365 | 365 | ||
| 366 | // cmd:test //////////////////////////////////////////////////////////////////////////////////////// | 366 | // cmd:test //////////////////////////////////////////////////////////////////////////////////////// |
| @@ -374,7 +374,7 @@ const usage_test = | |||
| 374 | \\ | 374 | \\ |
| 375 | ; | 375 | ; |
| 376 | 376 | ||
| 377 | fn cmdTest(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { | 377 | fn cmdTest(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { |
| 378 | } | 378 | } |
| 379 | 379 | ||
| 380 | // cmd:run ///////////////////////////////////////////////////////////////////////////////////////// | 380 | // cmd:run ///////////////////////////////////////////////////////////////////////////////////////// |
| @@ -390,7 +390,7 @@ const usage_run = | |||
| 390 | \\ | 390 | \\ |
| 391 | ; | 391 | ; |
| 392 | 392 | ||
| 393 | fn cmdRun(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { | 393 | fn cmdRun(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { |
| 394 | } | 394 | } |
| 395 | 395 | ||
| 396 | // cmd:translate-c ///////////////////////////////////////////////////////////////////////////////// | 396 | // cmd:translate-c ///////////////////////////////////////////////////////////////////////////////// |
| @@ -428,7 +428,7 @@ const info_zen = | |||
| 428 | \\ | 428 | \\ |
| 429 | ; | 429 | ; |
| 430 | 430 | ||
| 431 | fn cmdZen(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { | 431 | fn cmdZen(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { |
| 432 | } | 432 | } |
| 433 | 433 | ||
| 434 | // cmd:internal //////////////////////////////////////////////////////////////////////////////////// | 434 | // cmd:internal //////////////////////////////////////////////////////////////////////////////////// |
| @@ -442,5 +442,5 @@ const usage_internal = | |||
| 442 | \\ | 442 | \\ |
| 443 | ; | 443 | ; |
| 444 | 444 | ||
| 445 | fn cmdInternal(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { | 445 | fn cmdInternal(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { |
| 446 | } | 446 | } |
diff --git a/src/core.zig b/src/core.zig index f2e1fe0..d870e8c 100644 --- a/src/core.zig +++ b/src/core.zig | |||
| @@ -99,7 +99,7 @@ pub fn Param(comptime Id: type) type { | |||
| 99 | takes_value: bool, | 99 | takes_value: bool, |
| 100 | names: Names, | 100 | names: Names, |
| 101 | 101 | ||
| 102 | pub fn init(id: Id, takes_value: bool, names: &const Names) Self { | 102 | pub fn init(id: Id, takes_value: bool, names: *const Names) Self { |
| 103 | // Assert, that if the param have no name, then it has to take | 103 | // Assert, that if the param have no name, then it has to take |
| 104 | // a value. | 104 | // a value. |
| 105 | debug.assert( | 105 | debug.assert( |
| @@ -123,10 +123,10 @@ pub fn Arg(comptime Id: type) type { | |||
| 123 | return struct { | 123 | return struct { |
| 124 | const Self = this; | 124 | const Self = this; |
| 125 | 125 | ||
| 126 | param: &const Param(Id), | 126 | param: *const Param(Id), |
| 127 | value: ?[]const u8, | 127 | value: ?[]const u8, |
| 128 | 128 | ||
| 129 | pub fn init(param: &const Param(Id), value: ?[]const u8) Self { | 129 | pub fn init(param: *const Param(Id), value: ?[]const u8) Self { |
| 130 | return Self { | 130 | return Self { |
| 131 | .param = param, | 131 | .param = param, |
| 132 | .value = value, | 132 | .value = value, |
| @@ -141,9 +141,9 @@ pub fn ArgIterator(comptime E: type) type { | |||
| 141 | const Self = this; | 141 | const Self = this; |
| 142 | const Error = E; | 142 | const Error = E; |
| 143 | 143 | ||
| 144 | nextFn: fn(iter: &Self) Error!?[]const u8, | 144 | nextFn: fn(iter: *Self) Error!?[]const u8, |
| 145 | 145 | ||
| 146 | pub fn next(iter: &Self) Error!?[]const u8 { | 146 | pub fn next(iter: *Self) Error!?[]const u8 { |
| 147 | return iter.nextFn(iter); | 147 | return iter.nextFn(iter); |
| 148 | } | 148 | } |
| 149 | }; | 149 | }; |
| @@ -168,7 +168,7 @@ pub const ArgSliceIterator = struct { | |||
| 168 | }; | 168 | }; |
| 169 | } | 169 | } |
| 170 | 170 | ||
| 171 | fn nextFn(iter: &ArgIterator(Error)) Error!?[]const u8 { | 171 | fn nextFn(iter: *ArgIterator(Error)) Error!?[]const u8 { |
| 172 | const self = @fieldParentPtr(ArgSliceIterator, "iter", iter); | 172 | const self = @fieldParentPtr(ArgSliceIterator, "iter", iter); |
| 173 | if (self.args.len <= self.index) | 173 | if (self.args.len <= self.index) |
| 174 | return null; | 174 | return null; |
| @@ -187,7 +187,7 @@ pub const OsArgIterator = struct { | |||
| 187 | args: os.ArgIterator, | 187 | args: os.ArgIterator, |
| 188 | iter: ArgIterator(Error), | 188 | iter: ArgIterator(Error), |
| 189 | 189 | ||
| 190 | pub fn init(allocator: &mem.Allocator) OsArgIterator { | 190 | pub fn init(allocator: *mem.Allocator) OsArgIterator { |
| 191 | return OsArgIterator { | 191 | return OsArgIterator { |
| 192 | .arena = heap.ArenaAllocator.init(allocator), | 192 | .arena = heap.ArenaAllocator.init(allocator), |
| 193 | .args = os.args(), | 193 | .args = os.args(), |
| @@ -197,11 +197,11 @@ pub const OsArgIterator = struct { | |||
| 197 | }; | 197 | }; |
| 198 | } | 198 | } |
| 199 | 199 | ||
| 200 | pub fn deinit(iter: &OsArgIterator) void { | 200 | pub fn deinit(iter: *OsArgIterator) void { |
| 201 | iter.arena.deinit(); | 201 | iter.arena.deinit(); |
| 202 | } | 202 | } |
| 203 | 203 | ||
| 204 | fn nextFn(iter: &ArgIterator(Error)) Error!?[]const u8 { | 204 | fn nextFn(iter: *ArgIterator(Error)) Error!?[]const u8 { |
| 205 | const self = @fieldParentPtr(OsArgIterator, "iter", iter); | 205 | const self = @fieldParentPtr(OsArgIterator, "iter", iter); |
| 206 | if (builtin.os == builtin.Os.windows) { | 206 | if (builtin.os == builtin.Os.windows) { |
| 207 | return try self.args.next(self.allocator) ?? return null; | 207 | return try self.args.next(self.allocator) ?? return null; |
| @@ -229,10 +229,10 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { | |||
| 229 | }; | 229 | }; |
| 230 | 230 | ||
| 231 | params: []const Param(Id), | 231 | params: []const Param(Id), |
| 232 | iter: &ArgIterator(ArgError), | 232 | iter: *ArgIterator(ArgError), |
| 233 | state: State, | 233 | state: State, |
| 234 | 234 | ||
| 235 | pub fn init(params: []const Param(Id), iter: &ArgIterator(ArgError)) Self { | 235 | pub fn init(params: []const Param(Id), iter: *ArgIterator(ArgError)) Self { |
| 236 | var res = Self { | 236 | var res = Self { |
| 237 | .params = params, | 237 | .params = params, |
| 238 | .iter = iter, | 238 | .iter = iter, |
| @@ -243,7 +243,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { | |||
| 243 | } | 243 | } |
| 244 | 244 | ||
| 245 | /// Get the next ::Arg that matches a ::Param. | 245 | /// Get the next ::Arg that matches a ::Param. |
| 246 | pub fn next(clap: &Self) !?Arg(Id) { | 246 | pub fn next(clap: *Self) !?Arg(Id) { |
| 247 | const ArgInfo = struct { | 247 | const ArgInfo = struct { |
| 248 | const Kind = enum { Long, Short, Bare }; | 248 | const Kind = enum { Long, Short, Bare }; |
| 249 | 249 | ||
| @@ -334,7 +334,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { | |||
| 334 | } | 334 | } |
| 335 | } | 335 | } |
| 336 | 336 | ||
| 337 | fn chainging(clap: &Self, state: &const State.Chaining) !?Arg(Id) { | 337 | fn chainging(clap: *Self, state: *const State.Chaining) !?Arg(Id) { |
| 338 | const arg = state.arg; | 338 | const arg = state.arg; |
| 339 | const index = state.index; | 339 | const index = state.index; |
| 340 | const next_index = index + 1; | 340 | const next_index = index + 1; |
diff --git a/src/extended.zig b/src/extended.zig index ffcce5b..8fb017f 100644 --- a/src/extended.zig +++ b/src/extended.zig | |||
| @@ -29,25 +29,25 @@ pub const Command = struct { | |||
| 29 | params: []const Param, | 29 | params: []const Param, |
| 30 | 30 | ||
| 31 | Result: type, | 31 | Result: type, |
| 32 | default: &const Opaque, | 32 | default: *const Opaque, |
| 33 | 33 | ||
| 34 | pub fn init(comptime Result: type, default: &const Result, params: []const Param) Command { | 34 | pub fn init(comptime Result: type, default: *const Result, params: []const Param) Command { |
| 35 | return Command{ | 35 | return Command{ |
| 36 | .params = params, | 36 | .params = params, |
| 37 | .Result = Result, | 37 | .Result = Result, |
| 38 | .default = @ptrCast(&const Opaque, default), | 38 | .default = @ptrCast(*const Opaque, default), |
| 39 | }; | 39 | }; |
| 40 | } | 40 | } |
| 41 | }; | 41 | }; |
| 42 | 42 | ||
| 43 | pub const Parser = struct { | 43 | pub const Parser = struct { |
| 44 | const UnsafeFunction = &const void; | 44 | const UnsafeFunction = *const void; |
| 45 | 45 | ||
| 46 | FieldType: type, | 46 | FieldType: type, |
| 47 | Errors: type, | 47 | Errors: type, |
| 48 | func: UnsafeFunction, | 48 | func: UnsafeFunction, |
| 49 | 49 | ||
| 50 | pub fn init(comptime FieldType: type, comptime Errors: type, func: parseFunc(FieldType, Errors)) Parser { | 50 | pub fn init(comptime FieldType: type, comptime Errors: type, func: ParseFunc(FieldType, Errors)) Parser { |
| 51 | return Parser { | 51 | return Parser { |
| 52 | .FieldType = FieldType, | 52 | .FieldType = FieldType, |
| 53 | .Errors = Errors, | 53 | .Errors = Errors, |
| @@ -55,20 +55,17 @@ pub const Parser = struct { | |||
| 55 | }; | 55 | }; |
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | fn parse(comptime parser: Parser, field_ptr: TakePtr(parser.FieldType), arg: []const u8) parser.Errors!void { | 58 | fn parse(comptime parser: Parser, field_ptr: *parser.FieldType, arg: []const u8) parser.Errors!void { |
| 59 | return @ptrCast(parseFunc(parser.FieldType, parser.Errors), parser.func)(field_ptr, arg); | 59 | return @ptrCast(ParseFunc(parser.FieldType, parser.Errors), parser.func)(field_ptr, arg); |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | // TODO: This is a workaround, since we don't have pointer reform yet. | 62 | fn ParseFunc(comptime FieldType: type, comptime Errors: type) type { |
| 63 | fn TakePtr(comptime T: type) type { return &T; } | 63 | return fn(*FieldType, []const u8) Errors!void; |
| 64 | |||
| 65 | fn parseFunc(comptime FieldType: type, comptime Errors: type) type { | ||
| 66 | return fn(&FieldType, []const u8) Errors!void; | ||
| 67 | } | 64 | } |
| 68 | 65 | ||
| 69 | pub fn int(comptime Int: type, comptime radix: u8) Parser { | 66 | pub fn int(comptime Int: type, comptime radix: u8) Parser { |
| 70 | const func = struct { | 67 | const func = struct { |
| 71 | fn i(field_ptr: &Int, arg: []const u8) !void { | 68 | fn i(field_ptr: *Int, arg: []const u8) !void { |
| 72 | field_ptr.* = try fmt.parseInt(Int, arg, radix); | 69 | field_ptr.* = try fmt.parseInt(Int, arg, radix); |
| 73 | } | 70 | } |
| 74 | }.i; | 71 | }.i; |
| @@ -83,7 +80,7 @@ pub const Parser = struct { | |||
| 83 | []const u8, | 80 | []const u8, |
| 84 | error{}, | 81 | error{}, |
| 85 | struct { | 82 | struct { |
| 86 | fn s(field_ptr: &[]const u8, arg: []const u8) (error{}!void) { | 83 | fn s(field_ptr: *[]const u8, arg: []const u8) (error{}!void) { |
| 87 | field_ptr.* = arg; | 84 | field_ptr.* = arg; |
| 88 | } | 85 | } |
| 89 | }.s | 86 | }.s |
| @@ -98,23 +95,23 @@ pub fn Clap(comptime Result: type) type { | |||
| 98 | params: []const Param, | 95 | params: []const Param, |
| 99 | 96 | ||
| 100 | pub fn parse( | 97 | pub fn parse( |
| 101 | comptime clap: &const Self, | 98 | comptime clap: *const Self, |
| 102 | comptime Error: type, | 99 | comptime Error: type, |
| 103 | iter: &core.ArgIterator(Error), | 100 | iter: *core.ArgIterator(Error), |
| 104 | ) !Result { | 101 | ) !Result { |
| 105 | // We initialize the core.Clap without any params, and fill them out in parseHelper. | 102 | // We initialize the core.Clap without any params, and fill them out in parseHelper. |
| 106 | var c = core.Clap(usize, Error).init([]core.Param(usize){}, iter); | 103 | var c = core.Clap(usize, Error).init([]core.Param(usize){}, iter); |
| 107 | 104 | ||
| 108 | const top_level_command = comptime Command.init(Result, &clap.default, clap.params); | 105 | const top_level_command = comptime Command.init(Result, &clap.default, clap.params); |
| 109 | return try parseHelper(top_level_command, Error, &c); | 106 | return try parseHelper(&top_level_command, Error, &c); |
| 110 | } | 107 | } |
| 111 | 108 | ||
| 112 | fn parseHelper( | 109 | fn parseHelper( |
| 113 | comptime command: &const Command, | 110 | comptime command: *const Command, |
| 114 | comptime Error: type, | 111 | comptime Error: type, |
| 115 | clap: &core.Clap(usize, Error), | 112 | clap: *core.Clap(usize, Error), |
| 116 | ) !command.Result { | 113 | ) !command.Result { |
| 117 | var result = @ptrCast(&const command.Result, command.default).*; | 114 | var result = @ptrCast(*const command.Result, command.default).*; |
| 118 | 115 | ||
| 119 | var handled = comptime blk: { | 116 | var handled = comptime blk: { |
| 120 | var res: [command.params.len]bool = undefined; | 117 | var res: [command.params.len]bool = undefined; |
diff --git a/tests/extended.zig b/tests/extended.zig index 140c822..00cc84b 100644 --- a/tests/extended.zig +++ b/tests/extended.zig | |||
| @@ -45,7 +45,7 @@ pub fn Test(comptime Expect: type) type { | |||
| 45 | Fail: error, | 45 | Fail: error, |
| 46 | }; | 46 | }; |
| 47 | 47 | ||
| 48 | pub fn success(args: []const []const u8, expected: &const Expect) Self { | 48 | pub fn success(args: []const []const u8, expected: *const Expect) Self { |
| 49 | return Self{ | 49 | return Self{ |
| 50 | .args = args, | 50 | .args = args, |
| 51 | .kind = Kind{ | 51 | .kind = Kind{ |
| @@ -63,7 +63,7 @@ pub fn Test(comptime Expect: type) type { | |||
| 63 | }; | 63 | }; |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | pub fn run(t: &const Self, comptime parser: var) void { | 66 | pub fn run(t: *const Self, comptime parser: var) void { |
| 67 | var iter = ArgSliceIterator.init(t.args); | 67 | var iter = ArgSliceIterator.init(t.args); |
| 68 | const actual = parser.parse(ArgSliceIterator.Error, &iter.iter); | 68 | const actual = parser.parse(ArgSliceIterator.Error, &iter.iter); |
| 69 | 69 | ||