authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-01 20:11:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-01 20:11:55-07:00
log272a0ab359ee504f147ccf1ce5adb78c676a8305
tree1f498413e27a2fc0bac5db5063c4371cfc6f7d17
parent20554d32c0a9e8adeb311645797e0d6873f4bbc0

zig fmt: implement "line comment followed by top-level comptime"


3 files changed, 37 insertions(+), 7 deletions(-)

lib/std/zig/parser_test.zig+11
...@@ -5,6 +5,17 @@...@@ -5,6 +5,17 @@
5// and substantial portions of the software.5// and substantial portions of the software.
6test "zig fmt: simple top level comptime block" {6test "zig fmt: simple top level comptime block" {
7 try testCanonical(7 try testCanonical(
8 \\// line comment
9 \\comptime {}
10 \\
11 );
12}
13
14test "zig fmt: two spaced line comments before decl" {
15 try testCanonical(
16 \\// line comment
17 \\
18 \\// another
8 \\comptime {}19 \\comptime {}
9 \\20 \\
10 );21 );
lib/std/zig/render.zig+6-5
...@@ -36,10 +36,11 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize, prefix: [...@@ -36,10 +36,11 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize, prefix: [
36 var index: usize = start;36 var index: usize = start;
37 var count: usize = 0;37 var count: usize = 0;
38 while (true) {38 while (true) {
39 // Scan forward to the next line comment, counting newlines.39 const comment_start = index +
40 const comment_start = mem.indexOf(u8, tree.source[index..end], "//") orelse return count;40 (mem.indexOf(u8, tree.source[index..end], "//") orelse return count);
41 const newline = mem.indexOfScalar(u8, tree.source[comment_start..end], '\n').?;41 const newline = comment_start +
42 const untrimmed_comment = tree.source[comment_start..][0..newline];42 mem.indexOfScalar(u8, tree.source[comment_start..end], '\n').?;
43 const untrimmed_comment = tree.source[comment_start..newline];
43 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, " \r\t");44 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, " \r\t");
44 if (count == 0) {45 if (count == 0) {
45 count += 1;46 count += 1;
...@@ -52,7 +53,7 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize, prefix: [...@@ -52,7 +53,7 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize, prefix: [
52 }53 }
53 }54 }
54 try ais.writer().print("{s}\n", .{trimmed_comment});55 try ais.writer().print("{s}\n", .{trimmed_comment});
55 index += comment_start + newline;56 index = newline + 1;
56 }57 }
57}58}
5859
lib/std/zig/tokenizer.zig+20-2
...@@ -1035,7 +1035,10 @@ pub const Tokenizer = struct {...@@ -1035,7 +1035,10 @@ pub const Tokenizer = struct {
1035 result.tag = .ContainerDocComment;1035 result.tag = .ContainerDocComment;
1036 state = .container_doc_comment;1036 state = .container_doc_comment;
1037 },1037 },
1038 '\n' => state = .start,1038 '\n' => {
1039 state = .start;
1040 result.loc.start = self.index + 1;
1041 },
1039 '\t', '\r' => state = .line_comment,1042 '\t', '\r' => state = .line_comment,
1040 else => {1043 else => {
1041 state = .line_comment;1044 state = .line_comment;
...@@ -1061,7 +1064,10 @@ pub const Tokenizer = struct {...@@ -1061,7 +1064,10 @@ pub const Tokenizer = struct {
1061 },1064 },
1062 },1065 },
1063 .line_comment => switch (c) {1066 .line_comment => switch (c) {
1064 '\n' => state = .start,1067 '\n' => {
1068 state = .start;
1069 result.loc.start = self.index + 1;
1070 },
1065 '\t', '\r' => {},1071 '\t', '\r' => {},
1066 else => self.checkLiteralCharacter(),1072 else => self.checkLiteralCharacter(),
1067 },1073 },
...@@ -1499,6 +1505,18 @@ test "tokenizer" {...@@ -1499,6 +1505,18 @@ test "tokenizer" {
1499 testTokenize("test", &[_]Token.Tag{.Keyword_test});1505 testTokenize("test", &[_]Token.Tag{.Keyword_test});
1500}1506}
15011507
1508test "line comment followed by top-level comptime" {
1509 testTokenize(
1510 \\// line comment
1511 \\comptime {}
1512 \\
1513 , &[_]Token.Tag{
1514 .Keyword_comptime,
1515 .LBrace,
1516 .RBrace,
1517 });
1518}
1519
1502test "tokenizer - unknown length pointer and then c pointer" {1520test "tokenizer - unknown length pointer and then c pointer" {
1503 testTokenize(1521 testTokenize(
1504 \\[*]u81522 \\[*]u8