diff options
| author | 2018-06-08 10:41:37 +0200 | |
|---|---|---|
| committer | 2018-06-08 10:41:37 +0200 | |
| commit | 765c82671a733991c46f3d3e14297bc94d6b56c3 (patch) | |
| tree | 77d5db9a7bf5f5e96ec4b84024aeeda5061acf5a /src | |
| 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
Diffstat (limited to 'src')
| -rw-r--r-- | src/core.zig | 26 | ||||
| -rw-r--r-- | src/extended.zig | 37 |
2 files changed, 30 insertions, 33 deletions
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; |