Commit 2f236e6efb

Shritesh Bhattarai <shritesh@shritesh.com>
2019-04-05 18:25:32
fmt: support trailing comma after var_args
1 parent 6cc2d39
Changed files (2)
std/zig/parse.zig
@@ -903,8 +903,20 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
             State.ParamDeclEnd => |ctx| {
                 if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| {
                     ctx.param_decl.var_args_token = ellipsis3;
-                    stack.append(State{ .ExpectToken = Token.Id.RParen }) catch unreachable;
-                    continue;
+
+                    switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RParen)) {
+                        ExpectCommaOrEndResult.end_token => |t| {
+                            if (t == null) {
+                                stack.append(State{ .ExpectToken = Token.Id.RParen }) catch unreachable;
+                                continue;
+                            }
+                            continue;
+                        },
+                        ExpectCommaOrEndResult.parse_error => |e| {
+                            try tree.errors.push(e);
+                            return tree;
+                        },
+                    }
                 }
 
                 try stack.append(State{ .ParamDeclComma = ctx.fn_proto });
std/zig/parser_test.zig
@@ -354,6 +354,15 @@ test "zig fmt: fn decl with trailing comma" {
     );
 }
 
+test "zig fmt: var_args with trailing comma" {
+    try testCanonical(
+        \\pub fn add(
+        \\    a: ...,
+        \\) void {}
+        \\
+    );
+}
+
 test "zig fmt: enum decl with no trailing comma" {
     try testTransform(
         \\const StrLitKind = enum {Normal, C};