Skip to content

Commit

Permalink
zig fmt: preserve same line doc comments on var decls
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed May 15, 2018
1 parent abcd418 commit 04bca58
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 28 deletions.
66 changes: 45 additions & 21 deletions std/zig/parse.zig
Expand Up @@ -640,12 +640,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
switch (token_ptr.id) {
Token.Id.Equal => {
var_decl.eq_token = token_index;
stack.append(State {
.ExpectTokenSave = ExpectTokenSave {
.id = Token.Id.Semicolon,
.ptr = &var_decl.semicolon_token,
},
}) catch unreachable;
stack.append(State { .VarDeclSemiColon = var_decl }) catch unreachable;
try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &var_decl.init_node } });
continue;
},
Expand All @@ -662,6 +657,30 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
}
},

State.VarDeclSemiColon => |var_decl| {
const semicolon_token = nextToken(&tok_it, &tree);

if (semicolon_token.ptr.id != Token.Id.Semicolon) {
*(try tree.errors.addOne()) = Error {
.ExpectedToken = Error.ExpectedToken {
.token = semicolon_token.index,
.expected_id = Token.Id.Semicolon,
},
};
return tree;
}

var_decl.semicolon_token = semicolon_token.index;

if (eatToken(&tok_it, &tree, Token.Id.DocComment)) |doc_comment_token| {
const loc = tree.tokenLocation(semicolon_token.ptr.end, doc_comment_token);
if (loc.line == 0) {
try pushDocComment(arena, doc_comment_token, &var_decl.doc_comments);
} else {
putBackToken(&tok_it, &tree);
}
}
},

State.FnDef => |fn_proto| {
const token = nextToken(&tok_it, &tree);
Expand Down Expand Up @@ -2938,6 +2957,7 @@ const State = union(enum) {
VarDecl: VarDeclCtx,
VarDeclAlign: &ast.Node.VarDecl,
VarDeclEq: &ast.Node.VarDecl,
VarDeclSemiColon: &ast.Node.VarDecl,

FnDef: &ast.Node.FnProto,
FnProto: &ast.Node.FnProto,
Expand Down Expand Up @@ -3042,25 +3062,29 @@ const State = union(enum) {
OptionalTokenSave: OptionalTokenSave,
};

fn pushDocComment(arena: &mem.Allocator, line_comment: TokenIndex, result: &?&ast.Node.DocComment) !void {
const node = blk: {
if (*result) |comment_node| {
break :blk comment_node;
} else {
const comment_node = try arena.construct(ast.Node.DocComment {
.base = ast.Node {
.id = ast.Node.Id.DocComment,
},
.lines = ast.Node.DocComment.LineList.init(arena),
});
*result = comment_node;
break :blk comment_node;
}
};
try node.lines.push(line_comment);
}

fn eatDocComments(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) !?&ast.Node.DocComment {
var result: ?&ast.Node.DocComment = null;
while (true) {
if (eatToken(tok_it, tree, Token.Id.DocComment)) |line_comment| {
const node = blk: {
if (result) |comment_node| {
break :blk comment_node;
} else {
const comment_node = try arena.construct(ast.Node.DocComment {
.base = ast.Node {
.id = ast.Node.Id.DocComment,
},
.lines = ast.Node.DocComment.LineList.init(arena),
});
result = comment_node;
break :blk comment_node;
}
};
try node.lines.push(line_comment);
try pushDocComment(arena, line_comment, &result);
continue;
}
break;
Expand Down
20 changes: 13 additions & 7 deletions std/zig/parser_test.zig
@@ -1,10 +1,16 @@
//test "zig fmt: same-line doc comment on variable declaration" {
// try testCanonical(
// \\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space
// \\pub const MAP_FILE = 0x0000; /// map from file (default)
// \\
// );
//}
test "zig fmt: same-line doc comment on variable declaration" {
try testTransform(
\\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space
\\pub const MAP_FILE = 0x0000; /// map from file (default)
\\
,
\\/// allocated from memory, swap space
\\pub const MAP_ANONYMOUS = 0x1000;
\\/// map from file (default)
\\pub const MAP_FILE = 0x0000;
\\
);
}

test "zig fmt: same-line comment after a statement" {
try testCanonical(
Expand Down

0 comments on commit 04bca58

Please sign in to comment.