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 {...@@ -1517,8 +1517,15 @@ pub const Parser = struct {
1517 continue;1517 continue;
1518 }1518 }
15191519
1520 stack.append(State { .IdentifierListCommaOrEnd = list_state }) catch unreachable;1520 const comments = try self.eatComments(arena);
1521 try stack.append(State { .Identifier = OptionalCtx { .Required = try list_state.list.addOne() } });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 } });
1522 continue;1529 continue;
1523 },1530 },
1524 State.IdentifierListCommaOrEnd => |list_state| {1531 State.IdentifierListCommaOrEnd => |list_state| {
...@@ -2739,14 +2746,17 @@ pub const Parser = struct {...@@ -2739,14 +2746,17 @@ pub const Parser = struct {
2739 continue;2746 continue;
2740 }2747 }
27412748
2742 const node = try self.createToCtxNode(arena, ctx.opt_ctx, ast.Node.ErrorSetDecl,2749 const node = try arena.construct(ast.Node.ErrorSetDecl {
2743 ast.Node.ErrorSetDecl {2750 .base = ast.Node {
2744 .base = undefined,2751 .id = ast.Node.Id.ErrorSetDecl,
2745 .error_token = ctx.error_token,2752 .before_comments = null,
2746 .decls = ArrayList(&ast.Node).init(arena),2753 .same_line_comment = null,
2747 .rbrace_token = undefined,2754 },
2748 }2755 .error_token = ctx.error_token,
2749 );2756 .decls = ArrayList(&ast.Node).init(arena),
2757 .rbrace_token = undefined,
2758 });
2759 ctx.opt_ctx.store(&node.base);
27502760
2751 stack.append(State {2761 stack.append(State {
2752 .IdentifierListItemOrEnd = ListSave(&ast.Node) {2762 .IdentifierListItemOrEnd = ListSave(&ast.Node) {
...@@ -3337,6 +3347,7 @@ pub const Parser = struct {...@@ -3337,6 +3347,7 @@ pub const Parser = struct {
3337 PrintIndent,3347 PrintIndent,
3338 Indent: usize,3348 Indent: usize,
3339 PrintSameLineComment: ?&Token,3349 PrintSameLineComment: ?&Token,
3350 PrintComments: &ast.Node,
3340 };3351 };
33413352
3342 pub fn renderSource(self: &Parser, stream: var, root_node: &ast.Node.Root) !void {3353 pub fn renderSource(self: &Parser, stream: var, root_node: &ast.Node.Root) !void {
...@@ -3978,6 +3989,7 @@ pub const Parser = struct {...@@ -3978,6 +3989,7 @@ pub const Parser = struct {
3978 const node = decls[i];3989 const node = decls[i];
3979 try stack.append(RenderState { .Text = "," });3990 try stack.append(RenderState { .Text = "," });
3980 try stack.append(RenderState { .Expression = node });3991 try stack.append(RenderState { .Expression = node });
3992 try stack.append(RenderState { .PrintComments = node });
3981 try stack.append(RenderState.PrintIndent);3993 try stack.append(RenderState.PrintIndent);
3982 try stack.append(RenderState {3994 try stack.append(RenderState {
3983 .Text = blk: {3995 .Text = blk: {
...@@ -4466,6 +4478,10 @@ pub const Parser = struct {...@@ -4466,6 +4478,10 @@ pub const Parser = struct {
4466 const comment_token = maybe_comment ?? break :blk;4478 const comment_token = maybe_comment ?? break :blk;
4467 try stream.print(" {}", self.tokenizer.getTokenSlice(comment_token));4479 try stream.print(" {}", self.tokenizer.getTokenSlice(comment_token));
4468 },4480 },
4481
4482 RenderState.PrintComments => |node| blk: {
4483 try self.renderComments(stream, node, indent);
4484 },
4469 }4485 }
4470 }4486 }
4471 }4487 }
std/zig/parser_test.zig+13
...@@ -1,3 +1,16 @@...@@ -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
1test "zig fmt: comments before switch prong" {14test "zig fmt: comments before switch prong" {
2 try testCanonical(15 try testCanonical(
3 \\test "" {16 \\test "" {