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 @@
55// and substantial portions of the software.
66test "zig fmt: simple top level comptime block" {
77 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
819 \\comptime {}
920 \\
1021 );
lib/std/zig/render.zig+6-5
......@@ -36,10 +36,11 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize, prefix: [
3636 var index: usize = start;
3737 var count: usize = 0;
3838 while (true) {
39 // Scan forward to the next line comment, counting newlines.
40 const comment_start = mem.indexOf(u8, tree.source[index..end], "//") orelse return count;
41 const newline = mem.indexOfScalar(u8, tree.source[comment_start..end], '\n').?;
42 const untrimmed_comment = tree.source[comment_start..][0..newline];
39 const comment_start = index +
40 (mem.indexOf(u8, tree.source[index..end], "//") orelse return count);
41 const newline = comment_start +
42 mem.indexOfScalar(u8, tree.source[comment_start..end], '\n').?;
43 const untrimmed_comment = tree.source[comment_start..newline];
4344 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, " \r\t");
4445 if (count == 0) {
4546 count += 1;
......@@ -52,7 +53,7 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize, prefix: [
5253 }
5354 }
5455 try ais.writer().print("{s}\n", .{trimmed_comment});
55 index += comment_start + newline;
56 index = newline + 1;
5657 }
5758}
5859
lib/std/zig/tokenizer.zig+20-2
......@@ -1035,7 +1035,10 @@ pub const Tokenizer = struct {
10351035 result.tag = .ContainerDocComment;
10361036 state = .container_doc_comment;
10371037 },
1038 '\n' => state = .start,
1038 '\n' => {
1039 state = .start;
1040 result.loc.start = self.index + 1;
1041 },
10391042 '\t', '\r' => state = .line_comment,
10401043 else => {
10411044 state = .line_comment;
......@@ -1061,7 +1064,10 @@ pub const Tokenizer = struct {
10611064 },
10621065 },
10631066 .line_comment => switch (c) {
1064 '\n' => state = .start,
1067 '\n' => {
1068 state = .start;
1069 result.loc.start = self.index + 1;
1070 },
10651071 '\t', '\r' => {},
10661072 else => self.checkLiteralCharacter(),
10671073 },
......@@ -1499,6 +1505,18 @@ test "tokenizer" {
14991505 testTokenize("test", &[_]Token.Tag{.Keyword_test});
15001506}
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
15021520test "tokenizer - unknown length pointer and then c pointer" {
15031521 testTokenize(
15041522 \\[*]u8