authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-15 00:33:34-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-15 00:33:34-04:00
log04bca58a3a3a85eeaa36ab4c2cc0881347e123b0
tree38f8c506048be0df410e517e58367c3a015f613b
parentabcd418451051fe8c3b5c283d1701d101337dde8

zig fmt: preserve same line doc comments on var decls


2 files changed, 58 insertions(+), 28 deletions(-)

std/zig/parse.zig+45-21
......@@ -640,12 +640,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
640640 switch (token_ptr.id) {
641641 Token.Id.Equal => {
642642 var_decl.eq_token = token_index;
643 stack.append(State {
644 .ExpectTokenSave = ExpectTokenSave {
645 .id = Token.Id.Semicolon,
646 .ptr = &var_decl.semicolon_token,
647 },
648 }) catch unreachable;
643 stack.append(State { .VarDeclSemiColon = var_decl }) catch unreachable;
649644 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &var_decl.init_node } });
650645 continue;
651646 },
......@@ -662,6 +657,30 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
662657 }
663658 },
664659
660 State.VarDeclSemiColon => |var_decl| {
661 const semicolon_token = nextToken(&tok_it, &tree);
662
663 if (semicolon_token.ptr.id != Token.Id.Semicolon) {
664 *(try tree.errors.addOne()) = Error {
665 .ExpectedToken = Error.ExpectedToken {
666 .token = semicolon_token.index,
667 .expected_id = Token.Id.Semicolon,
668 },
669 };
670 return tree;
671 }
672
673 var_decl.semicolon_token = semicolon_token.index;
674
675 if (eatToken(&tok_it, &tree, Token.Id.DocComment)) |doc_comment_token| {
676 const loc = tree.tokenLocation(semicolon_token.ptr.end, doc_comment_token);
677 if (loc.line == 0) {
678 try pushDocComment(arena, doc_comment_token, &var_decl.doc_comments);
679 } else {
680 putBackToken(&tok_it, &tree);
681 }
682 }
683 },
665684
666685 State.FnDef => |fn_proto| {
667686 const token = nextToken(&tok_it, &tree);
......@@ -2938,6 +2957,7 @@ const State = union(enum) {
29382957 VarDecl: VarDeclCtx,
29392958 VarDeclAlign: &ast.Node.VarDecl,
29402959 VarDeclEq: &ast.Node.VarDecl,
2960 VarDeclSemiColon: &ast.Node.VarDecl,
29412961
29422962 FnDef: &ast.Node.FnProto,
29432963 FnProto: &ast.Node.FnProto,
......@@ -3042,25 +3062,29 @@ const State = union(enum) {
30423062 OptionalTokenSave: OptionalTokenSave,
30433063};
30443064
3065fn pushDocComment(arena: &mem.Allocator, line_comment: TokenIndex, result: &?&ast.Node.DocComment) !void {
3066 const node = blk: {
3067 if (*result) |comment_node| {
3068 break :blk comment_node;
3069 } else {
3070 const comment_node = try arena.construct(ast.Node.DocComment {
3071 .base = ast.Node {
3072 .id = ast.Node.Id.DocComment,
3073 },
3074 .lines = ast.Node.DocComment.LineList.init(arena),
3075 });
3076 *result = comment_node;
3077 break :blk comment_node;
3078 }
3079 };
3080 try node.lines.push(line_comment);
3081}
3082
30453083fn eatDocComments(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) !?&ast.Node.DocComment {
30463084 var result: ?&ast.Node.DocComment = null;
30473085 while (true) {
30483086 if (eatToken(tok_it, tree, Token.Id.DocComment)) |line_comment| {
3049 const node = blk: {
3050 if (result) |comment_node| {
3051 break :blk comment_node;
3052 } else {
3053 const comment_node = try arena.construct(ast.Node.DocComment {
3054 .base = ast.Node {
3055 .id = ast.Node.Id.DocComment,
3056 },
3057 .lines = ast.Node.DocComment.LineList.init(arena),
3058 });
3059 result = comment_node;
3060 break :blk comment_node;
3061 }
3062 };
3063 try node.lines.push(line_comment);
3087 try pushDocComment(arena, line_comment, &result);
30643088 continue;
30653089 }
30663090 break;
std/zig/parser_test.zig+13-7
......@@ -1,10 +1,16 @@
1//test "zig fmt: same-line doc comment on variable declaration" {
2// try testCanonical(
3// \\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space
4// \\pub const MAP_FILE = 0x0000; /// map from file (default)
5// \\
6// );
7//}
1test "zig fmt: same-line doc comment on variable declaration" {
2 try testTransform(
3 \\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space
4 \\pub const MAP_FILE = 0x0000; /// map from file (default)
5 \\
6 ,
7 \\/// allocated from memory, swap space
8 \\pub const MAP_ANONYMOUS = 0x1000;
9 \\/// map from file (default)
10 \\pub const MAP_FILE = 0x0000;
11 \\
12 );
13}
814
915test "zig fmt: same-line comment after a statement" {
1016 try testCanonical(