authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-09 21:15:34-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-09 21:15:34-04:00
log403e5239e3668f626ac105fbfbb08456b859963a
tree3407270ca94f37ed670c63259da433dbfc410aee
parentca27ce3bee16ebb611621f15830dd6bf74d65f9f

all tests passing again


2 files changed, 106 insertions(+), 76 deletions(-)

std/zig/parser_test.zig+63-63
......@@ -1,66 +1,66 @@
1//test "zig fmt: same-line comment after a statement" {
2// try testCanonical(
3// \\test "" {
4// \\ a = b;
5// \\ debug.assert(H.digest_size <= H.block_size); // HMAC makes this assumption
6// \\ a = b;
7// \\}
8// \\
9// );
10//}
11//
12//test "zig fmt: same-line comment after var decl in struct" {
13// try testCanonical(
14// \\pub const vfs_cap_data = extern struct {
15// \\ const Data = struct {}; // when on disk.
16// \\};
17// \\
18// );
19//}
20//
21//test "zig fmt: same-line comment after field decl" {
22// try testCanonical(
23// \\pub const dirent = extern struct {
24// \\ d_name: u8,
25// \\ d_name: u8, // comment 1
26// \\ d_name: u8,
27// \\ d_name: u8, // comment 2
28// \\ d_name: u8,
29// \\};
30// \\
31// );
32//}
33//
34//test "zig fmt: same-line comment after switch prong" {
35// try testCanonical(
36// \\test "" {
37// \\ switch (err) {
38// \\ error.PathAlreadyExists => {}, // comment 2
39// \\ else => return err, // comment 1
40// \\ }
41// \\}
42// \\
43// );
44//}
45//
46//test "zig fmt: same-line comment after non-block if expression" {
47// try testCanonical(
48// \\comptime {
49// \\ if (sr > n_uword_bits - 1) // d > r
50// \\ return 0;
51// \\}
52// \\
53// );
54//}
55//
56//test "zig fmt: same-line comment on comptime expression" {
57// try testCanonical(
58// \\test "" {
59// \\ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
60// \\}
61// \\
62// );
63//}
1test "zig fmt: same-line comment after a statement" {
2 try testCanonical(
3 \\test "" {
4 \\ a = b;
5 \\ debug.assert(H.digest_size <= H.block_size); // HMAC makes this assumption
6 \\ a = b;
7 \\}
8 \\
9 );
10}
11
12test "zig fmt: same-line comment after var decl in struct" {
13 try testCanonical(
14 \\pub const vfs_cap_data = extern struct {
15 \\ const Data = struct {}; // when on disk.
16 \\};
17 \\
18 );
19}
20
21test "zig fmt: same-line comment after field decl" {
22 try testCanonical(
23 \\pub const dirent = extern struct {
24 \\ d_name: u8,
25 \\ d_name: u8, // comment 1
26 \\ d_name: u8,
27 \\ d_name: u8, // comment 2
28 \\ d_name: u8,
29 \\};
30 \\
31 );
32}
33
34test "zig fmt: same-line comment after switch prong" {
35 try testCanonical(
36 \\test "" {
37 \\ switch (err) {
38 \\ error.PathAlreadyExists => {}, // comment 2
39 \\ else => return err, // comment 1
40 \\ }
41 \\}
42 \\
43 );
44}
45
46test "zig fmt: same-line comment after non-block if expression" {
47 try testCanonical(
48 \\comptime {
49 \\ if (sr > n_uword_bits - 1) // d > r
50 \\ return 0;
51 \\}
52 \\
53 );
54}
55
56test "zig fmt: same-line comment on comptime expression" {
57 try testCanonical(
58 \\test "" {
59 \\ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
60 \\}
61 \\
62 );
63}
6464
6565test "zig fmt: switch with empty body" {
6666 try testCanonical(
std/zig/render.zig+43-13
......@@ -14,8 +14,13 @@ const RenderState = union(enum) {
1414 Statement: &ast.Node,
1515 PrintIndent,
1616 Indent: usize,
17 MaybeSemiColon: &ast.Node,
18 Token: ast.TokenIndex,
19 NonBreakToken: ast.TokenIndex,
1720};
1821
22const indent_delta = 4;
23
1924pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
2025 var stack = SegmentedList(RenderState, 32).init(allocator);
2126 defer stack.deinit();
......@@ -44,7 +49,6 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
4449 }
4550 }
4651
47 const indent_delta = 4;
4852 var indent: usize = 0;
4953 while (stack.pop()) |state| {
5054 switch (state) {
......@@ -92,7 +96,7 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
9296 try stream.print("{} ", tree.tokenSlice(visib_token));
9397 }
9498 try stream.print("{}: ", tree.tokenSlice(field.name_token));
95 try stack.push(RenderState { .Text = "," });
99 try stack.push(RenderState { .Token = field.lastToken() + 1 });
96100 try stack.push(RenderState { .Expression = field.type_expr});
97101 },
98102 ast.Node.Id.UnionTag => {
......@@ -129,9 +133,7 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
129133 try stream.print("{}", tree.tokenSlice(tag.name_token));
130134 },
131135 ast.Node.Id.Comptime => {
132 if (decl.requireSemiColon()) {
133 try stack.push(RenderState { .Text = ";" });
134 }
136 try stack.push(RenderState { .MaybeSemiColon = decl });
135137 try stack.push(RenderState { .Expression = decl });
136138 },
137139 ast.Node.Id.LineComment => {
......@@ -143,7 +145,7 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
143145 },
144146
145147 RenderState.VarDecl => |var_decl| {
146 try stack.push(RenderState { .Text = ";" });
148 try stack.push(RenderState { .Token = var_decl.semicolon_token });
147149 if (var_decl.init_node) |init_node| {
148150 try stack.push(RenderState { .Expression = init_node });
149151 const text = if (init_node.id == ast.Node.Id.MultilineStringLiteral) " =" else " = ";
......@@ -895,7 +897,7 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
895897 ast.Node.Id.SwitchCase => {
896898 const switch_case = @fieldParentPtr(ast.Node.SwitchCase, "base", base);
897899
898 try stack.push(RenderState { .Text = "," });
900 try stack.push(RenderState { .Token = switch_case.lastToken() + 1 });
899901 try stack.push(RenderState { .Expression = switch_case.expr });
900902 if (switch_case.payload) |payload| {
901903 try stack.push(RenderState { .Text = " " });
......@@ -1072,14 +1074,13 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
10721074 }
10731075
10741076 try stack.push(RenderState { .Expression = if_node.body });
1075 try stack.push(RenderState { .Text = " " });
10761077
10771078 if (if_node.payload) |payload| {
1078 try stack.push(RenderState { .Expression = payload });
10791079 try stack.push(RenderState { .Text = " " });
1080 try stack.push(RenderState { .Expression = payload });
10801081 }
10811082
1082 try stack.push(RenderState { .Text = ")" });
1083 try stack.push(RenderState { .NonBreakToken = if_node.condition.lastToken() + 1 });
10831084 try stack.push(RenderState { .Expression = if_node.condition });
10841085 try stack.push(RenderState { .Text = "(" });
10851086 },
......@@ -1217,17 +1218,46 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
12171218 try stack.push(RenderState { .VarDecl = var_decl});
12181219 },
12191220 else => {
1220 if (base.requireSemiColon()) {
1221 try stack.push(RenderState { .Text = ";" });
1222 }
1221 try stack.push(RenderState { .MaybeSemiColon = base });
12231222 try stack.push(RenderState { .Expression = base });
12241223 },
12251224 }
12261225 },
12271226 RenderState.Indent => |new_indent| indent = new_indent,
12281227 RenderState.PrintIndent => try stream.writeByteNTimes(' ', indent),
1228 RenderState.Token => |token_index| try renderToken(tree, stream, token_index, indent, true),
1229 RenderState.NonBreakToken => |token_index| try renderToken(tree, stream, token_index, indent, false),
1230 RenderState.MaybeSemiColon => |base| {
1231 if (base.requireSemiColon()) {
1232 const semicolon_index = base.lastToken() + 1;
1233 assert(tree.tokens.at(semicolon_index).id == Token.Id.Semicolon);
1234 try renderToken(tree, stream, semicolon_index, indent, true);
1235 }
1236 },
1237 }
1238 }
1239}
1240
1241fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent: usize, line_break: bool) !void {
1242 const token = tree.tokens.at(token_index);
1243 try stream.write(tree.tokenSlicePtr(token));
1244
1245 const next_token = tree.tokens.at(token_index + 1);
1246 if (next_token.id == Token.Id.LineComment) {
1247 const loc = tree.tokenLocationPtr(token.end, next_token);
1248 if (loc.line == 0) {
1249 try stream.print(" {}", tree.tokenSlicePtr(next_token));
1250 if (!line_break) {
1251 try stream.write("\n");
1252 try stream.writeByteNTimes(' ', indent + indent_delta);
1253 return;
1254 }
12291255 }
12301256 }
1257
1258 if (!line_break) {
1259 try stream.writeByte(' ');
1260 }
12311261}
12321262
12331263fn renderComments(tree: &ast.Tree, stream: var, node: var, indent: usize) !void {