authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-29 19:55:57-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-29 19:55:57-04:00
logc53209a8a8cee014382a735b3baa7cd439106c6f
tree1109201249a486bde629356928ba6fed283fee54
parent3235eb03f979cbcc38cbe8b69d55761079e6d864

zig fmt: comments before var decl in struct


2 files changed, 45 insertions(+), 14 deletions(-)

std/zig/parser.zig+17-9
...@@ -230,6 +230,7 @@ pub const Parser = struct {...@@ -230,6 +230,7 @@ pub const Parser = struct {
230 Semicolon: &&ast.Node,230 Semicolon: &&ast.Node,
231 AddComments: AddCommentsCtx,231 AddComments: AddCommentsCtx,
232 LookForSameLineComment: &&ast.Node,232 LookForSameLineComment: &&ast.Node,
233 LookForSameLineCommentDirect: &ast.Node,
233234
234 AsmOutputItems: &ArrayList(&ast.Node.AsmOutput),235 AsmOutputItems: &ArrayList(&ast.Node.AsmOutput),
235 AsmOutputReturnOrType: &ast.Node.AsmOutput,236 AsmOutputReturnOrType: &ast.Node.AsmOutput,
...@@ -532,7 +533,7 @@ pub const Parser = struct {...@@ -532,7 +533,7 @@ pub const Parser = struct {
532 }533 }
533 }534 }
534535
535 stack.append(State {536 try stack.append(State {
536 .VarDecl = VarDeclCtx {537 .VarDecl = VarDeclCtx {
537 .comments = ctx.comments,538 .comments = ctx.comments,
538 .visib_token = ctx.visib_token,539 .visib_token = ctx.visib_token,
...@@ -542,7 +543,7 @@ pub const Parser = struct {...@@ -542,7 +543,7 @@ pub const Parser = struct {
542 .mut_token = token,543 .mut_token = token,
543 .list = ctx.decls544 .list = ctx.decls
544 }545 }
545 }) catch unreachable;546 });
546 continue;547 continue;
547 },548 },
548 Token.Id.Keyword_fn, Token.Id.Keyword_nakedcc,549 Token.Id.Keyword_fn, Token.Id.Keyword_nakedcc,
...@@ -705,6 +706,7 @@ pub const Parser = struct {...@@ -705,6 +706,7 @@ pub const Parser = struct {
705 continue;706 continue;
706 },707 },
707 State.ContainerDecl => |container_decl| {708 State.ContainerDecl => |container_decl| {
709 const comments = try self.eatComments(arena);
708 const token = self.getNextToken();710 const token = self.getNextToken();
709 switch (token.id) {711 switch (token.id) {
710 Token.Id.Identifier => {712 Token.Id.Identifier => {
...@@ -713,7 +715,7 @@ pub const Parser = struct {...@@ -713,7 +715,7 @@ pub const Parser = struct {
713 const node = try arena.construct(ast.Node.StructField {715 const node = try arena.construct(ast.Node.StructField {
714 .base = ast.Node {716 .base = ast.Node {
715 .id = ast.Node.Id.StructField,717 .id = ast.Node.Id.StructField,
716 .before_comments = null,718 .before_comments = comments,
717 .same_line_comment = null,719 .same_line_comment = null,
718 },720 },
719 .visib_token = null,721 .visib_token = null,
...@@ -765,7 +767,7 @@ pub const Parser = struct {...@@ -765,7 +767,7 @@ pub const Parser = struct {
765 .TopLevelExternOrField = TopLevelExternOrFieldCtx {767 .TopLevelExternOrField = TopLevelExternOrFieldCtx {
766 .visib_token = token,768 .visib_token = token,
767 .container_decl = container_decl,769 .container_decl = container_decl,
768 .comments = null,770 .comments = comments,
769 }771 }
770 });772 });
771 continue;773 continue;
...@@ -778,7 +780,7 @@ pub const Parser = struct {...@@ -778,7 +780,7 @@ pub const Parser = struct {
778 .visib_token = token,780 .visib_token = token,
779 .extern_export_inline_token = null,781 .extern_export_inline_token = null,
780 .lib_name = null,782 .lib_name = null,
781 .comments = null,783 .comments = comments,
782 }784 }
783 });785 });
784 continue;786 continue;
...@@ -793,7 +795,7 @@ pub const Parser = struct {...@@ -793,7 +795,7 @@ pub const Parser = struct {
793 .visib_token = token,795 .visib_token = token,
794 .extern_export_inline_token = null,796 .extern_export_inline_token = null,
795 .lib_name = null,797 .lib_name = null,
796 .comments = null,798 .comments = comments,
797 }799 }
798 });800 });
799 continue;801 continue;
...@@ -811,7 +813,7 @@ pub const Parser = struct {...@@ -811,7 +813,7 @@ pub const Parser = struct {
811 .visib_token = null,813 .visib_token = null,
812 .extern_export_inline_token = null,814 .extern_export_inline_token = null,
813 .lib_name = null,815 .lib_name = null,
814 .comments = null,816 .comments = comments,
815 }817 }
816 });818 });
817 continue;819 continue;
...@@ -842,7 +844,8 @@ pub const Parser = struct {...@@ -842,7 +844,8 @@ pub const Parser = struct {
842 });844 });
843 try ctx.list.append(&var_decl.base);845 try ctx.list.append(&var_decl.base);
844846
845 stack.append(State { .VarDeclAlign = var_decl }) catch unreachable;847 try stack.append(State { .LookForSameLineCommentDirect = &var_decl.base });
848 try stack.append(State { .VarDeclAlign = var_decl });
846 try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &var_decl.type_node} });849 try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &var_decl.type_node} });
847 try stack.append(State { .IfToken = Token.Id.Colon });850 try stack.append(State { .IfToken = Token.Id.Colon });
848 try stack.append(State {851 try stack.append(State {
...@@ -854,7 +857,7 @@ pub const Parser = struct {...@@ -854,7 +857,7 @@ pub const Parser = struct {
854 continue;857 continue;
855 },858 },
856 State.VarDeclAlign => |var_decl| {859 State.VarDeclAlign => |var_decl| {
857 stack.append(State { .VarDeclEq = var_decl }) catch unreachable;860 try stack.append(State { .VarDeclEq = var_decl });
858861
859 const next_token = self.getNextToken();862 const next_token = self.getNextToken();
860 if (next_token.id == Token.Id.Keyword_align) {863 if (next_token.id == Token.Id.Keyword_align) {
...@@ -1348,6 +1351,11 @@ pub const Parser = struct {...@@ -1348,6 +1351,11 @@ pub const Parser = struct {
1348 continue;1351 continue;
1349 },1352 },
13501353
1354 State.LookForSameLineCommentDirect => |node| {
1355 try self.lookForSameLineComment(arena, node);
1356 continue;
1357 },
1358
13511359
1352 State.AsmOutputItems => |items| {1360 State.AsmOutputItems => |items| {
1353 const lbracket = self.getNextToken();1361 const lbracket = self.getNextToken();
std/zig/parser_test.zig+28-5
...@@ -1,4 +1,27 @@...@@ -1,4 +1,27 @@
1test "zig fmt: line comment after field decl" {1test "zig fmt: comments before var decl in struct" {
2 try testCanonical(
3 \\pub const vfs_cap_data = extern struct {
4 \\ // All of these are mandated as little endian
5 \\ // when on disk.
6 \\ const Data = struct {
7 \\ permitted: u32,
8 \\ inheritable: u32,
9 \\ };
10 \\};
11 \\
12 );
13}
14
15test "zig fmt: same-line comment after var decl in struct" {
16 try testCanonical(
17 \\pub const vfs_cap_data = extern struct {
18 \\ const Data = struct {}; // when on disk.
19 \\};
20 \\
21 );
22}
23
24test "zig fmt: same-line comment after field decl" {
2 try testCanonical(25 try testCanonical(
3 \\pub const dirent = extern struct {26 \\pub const dirent = extern struct {
4 \\ d_name: u8,27 \\ d_name: u8,
...@@ -18,7 +41,7 @@ test "zig fmt: array literal with 1 item on 1 line" {...@@ -18,7 +41,7 @@ test "zig fmt: array literal with 1 item on 1 line" {
18 );41 );
19}42}
2043
21test "zig fmt: preserve same-line comment after a statement" {44test "zig fmt: same-line comment after a statement" {
22 try testCanonical(45 try testCanonical(
23 \\test "" {46 \\test "" {
24 \\ a = b;47 \\ a = b;
...@@ -29,7 +52,7 @@ test "zig fmt: preserve same-line comment after a statement" {...@@ -29,7 +52,7 @@ test "zig fmt: preserve same-line comment after a statement" {
29 );52 );
30}53}
3154
32test "zig fmt: preserve comments before global variables" {55test "zig fmt: comments before global variables" {
33 try testCanonical(56 try testCanonical(
34 \\/// Foo copies keys and values before they go into the map, and57 \\/// Foo copies keys and values before they go into the map, and
35 \\/// frees them when they get removed.58 \\/// frees them when they get removed.
...@@ -38,7 +61,7 @@ test "zig fmt: preserve comments before global variables" {...@@ -38,7 +61,7 @@ test "zig fmt: preserve comments before global variables" {
38 );61 );
39}62}
4063
41test "zig fmt: preserve comments before statements" {64test "zig fmt: comments before statements" {
42 try testCanonical(65 try testCanonical(
43 \\test "std" {66 \\test "std" {
44 \\ // statement comment67 \\ // statement comment
...@@ -48,7 +71,7 @@ test "zig fmt: preserve comments before statements" {...@@ -48,7 +71,7 @@ test "zig fmt: preserve comments before statements" {
48 );71 );
49}72}
5073
51test "zig fmt: preserve top level comments" {74test "zig fmt: comments before test decl" {
52 try testCanonical(75 try testCanonical(
53 \\// top level comment76 \\// top level comment
54 \\test "hi" {}77 \\test "hi" {}