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 {...@@ -640,12 +640,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
640 switch (token_ptr.id) {640 switch (token_ptr.id) {
641 Token.Id.Equal => {641 Token.Id.Equal => {
642 var_decl.eq_token = token_index;642 var_decl.eq_token = token_index;
643 stack.append(State {643 stack.append(State { .VarDeclSemiColon = var_decl }) catch unreachable;
644 .ExpectTokenSave = ExpectTokenSave {
645 .id = Token.Id.Semicolon,
646 .ptr = &var_decl.semicolon_token,
647 },
648 }) catch unreachable;
649 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &var_decl.init_node } });644 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &var_decl.init_node } });
650 continue;645 continue;
651 },646 },
...@@ -662,6 +657,30 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -662,6 +657,30 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
662 }657 }
663 },658 },
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
666 State.FnDef => |fn_proto| {685 State.FnDef => |fn_proto| {
667 const token = nextToken(&tok_it, &tree);686 const token = nextToken(&tok_it, &tree);
...@@ -2938,6 +2957,7 @@ const State = union(enum) {...@@ -2938,6 +2957,7 @@ const State = union(enum) {
2938 VarDecl: VarDeclCtx,2957 VarDecl: VarDeclCtx,
2939 VarDeclAlign: &ast.Node.VarDecl,2958 VarDeclAlign: &ast.Node.VarDecl,
2940 VarDeclEq: &ast.Node.VarDecl,2959 VarDeclEq: &ast.Node.VarDecl,
2960 VarDeclSemiColon: &ast.Node.VarDecl,
29412961
2942 FnDef: &ast.Node.FnProto,2962 FnDef: &ast.Node.FnProto,
2943 FnProto: &ast.Node.FnProto,2963 FnProto: &ast.Node.FnProto,
...@@ -3042,25 +3062,29 @@ const State = union(enum) {...@@ -3042,25 +3062,29 @@ const State = union(enum) {
3042 OptionalTokenSave: OptionalTokenSave,3062 OptionalTokenSave: OptionalTokenSave,
3043};3063};
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
3045fn eatDocComments(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) !?&ast.Node.DocComment {3083fn eatDocComments(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) !?&ast.Node.DocComment {
3046 var result: ?&ast.Node.DocComment = null;3084 var result: ?&ast.Node.DocComment = null;
3047 while (true) {3085 while (true) {
3048 if (eatToken(tok_it, tree, Token.Id.DocComment)) |line_comment| {3086 if (eatToken(tok_it, tree, Token.Id.DocComment)) |line_comment| {
3049 const node = blk: {3087 try pushDocComment(arena, line_comment, &result);
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);
3064 continue;3088 continue;
3065 }3089 }
3066 break;3090 break;
std/zig/parser_test.zig+13-7
...@@ -1,10 +1,16 @@...@@ -1,10 +1,16 @@
1//test "zig fmt: same-line doc comment on variable declaration" {1test "zig fmt: same-line doc comment on variable declaration" {
2// try testCanonical(2 try testTransform(
3// \\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space3 \\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space
4// \\pub const MAP_FILE = 0x0000; /// map from file (default)4 \\pub const MAP_FILE = 0x0000; /// map from file (default)
5// \\5 \\
6// );6 ,
7//}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
9test "zig fmt: same-line comment after a statement" {15test "zig fmt: same-line comment after a statement" {
10 try testCanonical(16 try testCanonical(