authorgravatar for shritesh@shritesh.comShritesh Bhattarai <shritesh@shritesh.com> 2019-04-05 11:25:32-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-05 12:27:13-04:00
log2f236e6efb2c95d086b916334db492b948b48e44
tree4bc323bb56201ea179b5827bce7679324a595582
parent6cc2d3938e81f1c5db9cf94db90fda11b026422a

fmt: support trailing comma after var_args


2 files changed, 23 insertions(+), 2 deletions(-)

std/zig/parse.zig+14-2
...@@ -903,8 +903,20 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {...@@ -903,8 +903,20 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
903 State.ParamDeclEnd => |ctx| {903 State.ParamDeclEnd => |ctx| {
904 if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| {904 if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| {
905 ctx.param_decl.var_args_token = ellipsis3;905 ctx.param_decl.var_args_token = ellipsis3;
906 stack.append(State{ .ExpectToken = Token.Id.RParen }) catch unreachable;906
907 continue;907 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RParen)) {
908 ExpectCommaOrEndResult.end_token => |t| {
909 if (t == null) {
910 stack.append(State{ .ExpectToken = Token.Id.RParen }) catch unreachable;
911 continue;
912 }
913 continue;
914 },
915 ExpectCommaOrEndResult.parse_error => |e| {
916 try tree.errors.push(e);
917 return tree;
918 },
919 }
908 }920 }
909921
910 try stack.append(State{ .ParamDeclComma = ctx.fn_proto });922 try stack.append(State{ .ParamDeclComma = ctx.fn_proto });
std/zig/parser_test.zig+9
...@@ -354,6 +354,15 @@ test "zig fmt: fn decl with trailing comma" {...@@ -354,6 +354,15 @@ test "zig fmt: fn decl with trailing comma" {
354 );354 );
355}355}
356356
357test "zig fmt: var_args with trailing comma" {
358 try testCanonical(
359 \\pub fn add(
360 \\ a: ...,
361 \\) void {}
362 \\
363 );
364}
365
357test "zig fmt: enum decl with no trailing comma" {366test "zig fmt: enum decl with no trailing comma" {
358 try testTransform(367 try testTransform(
359 \\const StrLitKind = enum {Normal, C};368 \\const StrLitKind = enum {Normal, C};