authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-29 22:12:17-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-29 22:12:17-04:00
log4e23fb7f062322e604d74ebb7e62d707ecf7e7b6
tree151bb52d29a4f6666940d64152b1ebd7543860e8
parentf04015c080b8e4e97166d6ac9356f818e766fc48

zig fmt: comments before error set decl


2 files changed, 39 insertions(+), 10 deletions(-)

std/zig/parser.zig+26-10
......@@ -1517,8 +1517,15 @@ pub const Parser = struct {
15171517 continue;
15181518 }
15191519
1520 stack.append(State { .IdentifierListCommaOrEnd = list_state }) catch unreachable;
1521 try stack.append(State { .Identifier = OptionalCtx { .Required = try list_state.list.addOne() } });
1520 const comments = try self.eatComments(arena);
1521 const node_ptr = try list_state.list.addOne();
1522
1523 try stack.append(State { .AddComments = AddCommentsCtx {
1524 .node_ptr = node_ptr,
1525 .comments = comments,
1526 }});
1527 try stack.append(State { .IdentifierListCommaOrEnd = list_state });
1528 try stack.append(State { .Identifier = OptionalCtx { .Required = node_ptr } });
15221529 continue;
15231530 },
15241531 State.IdentifierListCommaOrEnd => |list_state| {
......@@ -2739,14 +2746,17 @@ pub const Parser = struct {
27392746 continue;
27402747 }
27412748
2742 const node = try self.createToCtxNode(arena, ctx.opt_ctx, ast.Node.ErrorSetDecl,
2743 ast.Node.ErrorSetDecl {
2744 .base = undefined,
2745 .error_token = ctx.error_token,
2746 .decls = ArrayList(&ast.Node).init(arena),
2747 .rbrace_token = undefined,
2748 }
2749 );
2749 const node = try arena.construct(ast.Node.ErrorSetDecl {
2750 .base = ast.Node {
2751 .id = ast.Node.Id.ErrorSetDecl,
2752 .before_comments = null,
2753 .same_line_comment = null,
2754 },
2755 .error_token = ctx.error_token,
2756 .decls = ArrayList(&ast.Node).init(arena),
2757 .rbrace_token = undefined,
2758 });
2759 ctx.opt_ctx.store(&node.base);
27502760
27512761 stack.append(State {
27522762 .IdentifierListItemOrEnd = ListSave(&ast.Node) {
......@@ -3337,6 +3347,7 @@ pub const Parser = struct {
33373347 PrintIndent,
33383348 Indent: usize,
33393349 PrintSameLineComment: ?&Token,
3350 PrintComments: &ast.Node,
33403351 };
33413352
33423353 pub fn renderSource(self: &Parser, stream: var, root_node: &ast.Node.Root) !void {
......@@ -3978,6 +3989,7 @@ pub const Parser = struct {
39783989 const node = decls[i];
39793990 try stack.append(RenderState { .Text = "," });
39803991 try stack.append(RenderState { .Expression = node });
3992 try stack.append(RenderState { .PrintComments = node });
39813993 try stack.append(RenderState.PrintIndent);
39823994 try stack.append(RenderState {
39833995 .Text = blk: {
......@@ -4466,6 +4478,10 @@ pub const Parser = struct {
44664478 const comment_token = maybe_comment ?? break :blk;
44674479 try stream.print(" {}", self.tokenizer.getTokenSlice(comment_token));
44684480 },
4481
4482 RenderState.PrintComments => |node| blk: {
4483 try self.renderComments(stream, node, indent);
4484 },
44694485 }
44704486 }
44714487 }
std/zig/parser_test.zig+13
......@@ -1,3 +1,16 @@
1test "zig fmt: comments before error set decl" {
2 try testCanonical(
3 \\const UnexpectedError = error {
4 \\ /// The Operating System returned an undocumented error code.
5 \\ Unexpected,
6 \\
7 \\ // another
8 \\ Another,
9 \\};
10 \\
11 );
12}
13
114test "zig fmt: comments before switch prong" {
215 try testCanonical(
316 \\test "" {