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,66 +1,66 @@
1//test "zig fmt: same-line comment after a statement" {1test "zig fmt: same-line comment after a statement" {
2// try testCanonical(2 try testCanonical(
3// \\test "" {3 \\test "" {
4// \\ a = b;4 \\ a = b;
5// \\ debug.assert(H.digest_size <= H.block_size); // HMAC makes this assumption5 \\ debug.assert(H.digest_size <= H.block_size); // HMAC makes this assumption
6// \\ a = b;6 \\ a = b;
7// \\}7 \\}
8// \\8 \\
9// );9 );
10//}10}
11//11
12//test "zig fmt: same-line comment after var decl in struct" {12test "zig fmt: same-line comment after var decl in struct" {
13// try testCanonical(13 try testCanonical(
14// \\pub const vfs_cap_data = extern struct {14 \\pub const vfs_cap_data = extern struct {
15// \\ const Data = struct {}; // when on disk.15 \\ const Data = struct {}; // when on disk.
16// \\};16 \\};
17// \\17 \\
18// );18 );
19//}19}
20//20
21//test "zig fmt: same-line comment after field decl" {21test "zig fmt: same-line comment after field decl" {
22// try testCanonical(22 try testCanonical(
23// \\pub const dirent = extern struct {23 \\pub const dirent = extern struct {
24// \\ d_name: u8,24 \\ d_name: u8,
25// \\ d_name: u8, // comment 125 \\ d_name: u8, // comment 1
26// \\ d_name: u8,26 \\ d_name: u8,
27// \\ d_name: u8, // comment 227 \\ d_name: u8, // comment 2
28// \\ d_name: u8,28 \\ d_name: u8,
29// \\};29 \\};
30// \\30 \\
31// );31 );
32//}32}
33//33
34//test "zig fmt: same-line comment after switch prong" {34test "zig fmt: same-line comment after switch prong" {
35// try testCanonical(35 try testCanonical(
36// \\test "" {36 \\test "" {
37// \\ switch (err) {37 \\ switch (err) {
38// \\ error.PathAlreadyExists => {}, // comment 238 \\ error.PathAlreadyExists => {}, // comment 2
39// \\ else => return err, // comment 139 \\ else => return err, // comment 1
40// \\ }40 \\ }
41// \\}41 \\}
42// \\42 \\
43// );43 );
44//}44}
45//45
46//test "zig fmt: same-line comment after non-block if expression" {46test "zig fmt: same-line comment after non-block if expression" {
47// try testCanonical(47 try testCanonical(
48// \\comptime {48 \\comptime {
49// \\ if (sr > n_uword_bits - 1) // d > r49 \\ if (sr > n_uword_bits - 1) // d > r
50// \\ return 0;50 \\ return 0;
51// \\}51 \\}
52// \\52 \\
53// );53 );
54//}54}
55//55
56//test "zig fmt: same-line comment on comptime expression" {56test "zig fmt: same-line comment on comptime expression" {
57// try testCanonical(57 try testCanonical(
58// \\test "" {58 \\test "" {
59// \\ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt59 \\ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
60// \\}60 \\}
61// \\61 \\
62// );62 );
63//}63}
6464
65test "zig fmt: switch with empty body" {65test "zig fmt: switch with empty body" {
66 try testCanonical(66 try testCanonical(
std/zig/render.zig+43-13
...@@ -14,8 +14,13 @@ const RenderState = union(enum) {...@@ -14,8 +14,13 @@ const RenderState = union(enum) {
14 Statement: &ast.Node,14 Statement: &ast.Node,
15 PrintIndent,15 PrintIndent,
16 Indent: usize,16 Indent: usize,
17 MaybeSemiColon: &ast.Node,
18 Token: ast.TokenIndex,
19 NonBreakToken: ast.TokenIndex,
17};20};
1821
22const indent_delta = 4;
23
19pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {24pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
20 var stack = SegmentedList(RenderState, 32).init(allocator);25 var stack = SegmentedList(RenderState, 32).init(allocator);
21 defer stack.deinit();26 defer stack.deinit();
...@@ -44,7 +49,6 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {...@@ -44,7 +49,6 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
44 }49 }
45 }50 }
4651
47 const indent_delta = 4;
48 var indent: usize = 0;52 var indent: usize = 0;
49 while (stack.pop()) |state| {53 while (stack.pop()) |state| {
50 switch (state) {54 switch (state) {
...@@ -92,7 +96,7 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {...@@ -92,7 +96,7 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
92 try stream.print("{} ", tree.tokenSlice(visib_token));96 try stream.print("{} ", tree.tokenSlice(visib_token));
93 }97 }
94 try stream.print("{}: ", tree.tokenSlice(field.name_token));98 try stream.print("{}: ", tree.tokenSlice(field.name_token));
95 try stack.push(RenderState { .Text = "," });99 try stack.push(RenderState { .Token = field.lastToken() + 1 });
96 try stack.push(RenderState { .Expression = field.type_expr});100 try stack.push(RenderState { .Expression = field.type_expr});
97 },101 },
98 ast.Node.Id.UnionTag => {102 ast.Node.Id.UnionTag => {
...@@ -129,9 +133,7 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {...@@ -129,9 +133,7 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
129 try stream.print("{}", tree.tokenSlice(tag.name_token));133 try stream.print("{}", tree.tokenSlice(tag.name_token));
130 },134 },
131 ast.Node.Id.Comptime => {135 ast.Node.Id.Comptime => {
132 if (decl.requireSemiColon()) {136 try stack.push(RenderState { .MaybeSemiColon = decl });
133 try stack.push(RenderState { .Text = ";" });
134 }
135 try stack.push(RenderState { .Expression = decl });137 try stack.push(RenderState { .Expression = decl });
136 },138 },
137 ast.Node.Id.LineComment => {139 ast.Node.Id.LineComment => {
...@@ -143,7 +145,7 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {...@@ -143,7 +145,7 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
143 },145 },
144146
145 RenderState.VarDecl => |var_decl| {147 RenderState.VarDecl => |var_decl| {
146 try stack.push(RenderState { .Text = ";" });148 try stack.push(RenderState { .Token = var_decl.semicolon_token });
147 if (var_decl.init_node) |init_node| {149 if (var_decl.init_node) |init_node| {
148 try stack.push(RenderState { .Expression = init_node });150 try stack.push(RenderState { .Expression = init_node });
149 const text = if (init_node.id == ast.Node.Id.MultilineStringLiteral) " =" else " = ";151 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 {...@@ -895,7 +897,7 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
895 ast.Node.Id.SwitchCase => {897 ast.Node.Id.SwitchCase => {
896 const switch_case = @fieldParentPtr(ast.Node.SwitchCase, "base", base);898 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 });
899 try stack.push(RenderState { .Expression = switch_case.expr });901 try stack.push(RenderState { .Expression = switch_case.expr });
900 if (switch_case.payload) |payload| {902 if (switch_case.payload) |payload| {
901 try stack.push(RenderState { .Text = " " });903 try stack.push(RenderState { .Text = " " });
...@@ -1072,14 +1074,13 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {...@@ -1072,14 +1074,13 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
1072 }1074 }
10731075
1074 try stack.push(RenderState { .Expression = if_node.body });1076 try stack.push(RenderState { .Expression = if_node.body });
1075 try stack.push(RenderState { .Text = " " });
10761077
1077 if (if_node.payload) |payload| {1078 if (if_node.payload) |payload| {
1078 try stack.push(RenderState { .Expression = payload });
1079 try stack.push(RenderState { .Text = " " });1079 try stack.push(RenderState { .Text = " " });
1080 try stack.push(RenderState { .Expression = payload });
1080 }1081 }
10811082
1082 try stack.push(RenderState { .Text = ")" });1083 try stack.push(RenderState { .NonBreakToken = if_node.condition.lastToken() + 1 });
1083 try stack.push(RenderState { .Expression = if_node.condition });1084 try stack.push(RenderState { .Expression = if_node.condition });
1084 try stack.push(RenderState { .Text = "(" });1085 try stack.push(RenderState { .Text = "(" });
1085 },1086 },
...@@ -1217,17 +1218,46 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {...@@ -1217,17 +1218,46 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
1217 try stack.push(RenderState { .VarDecl = var_decl});1218 try stack.push(RenderState { .VarDecl = var_decl});
1218 },1219 },
1219 else => {1220 else => {
1220 if (base.requireSemiColon()) {1221 try stack.push(RenderState { .MaybeSemiColon = base });
1221 try stack.push(RenderState { .Text = ";" });
1222 }
1223 try stack.push(RenderState { .Expression = base });1222 try stack.push(RenderState { .Expression = base });
1224 },1223 },
1225 }1224 }
1226 },1225 },
1227 RenderState.Indent => |new_indent| indent = new_indent,1226 RenderState.Indent => |new_indent| indent = new_indent,
1228 RenderState.PrintIndent => try stream.writeByteNTimes(' ', indent),1227 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 }
1229 }1255 }
1230 }1256 }
1257
1258 if (!line_break) {
1259 try stream.writeByte(' ');
1260 }
1231}1261}
12321262
1233fn renderComments(tree: &ast.Tree, stream: var, node: var, indent: usize) !void {1263fn renderComments(tree: &ast.Tree, stream: var, node: var, indent: usize) !void {