Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.zig.tokenizer: simplify line-based tokens #21371

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11721,16 +11721,14 @@ fn strLitNodeAsString(astgen: *AstGen, node: Ast.Node.Index) !IndexSlice {
var tok_i = start;
{
const slice = tree.tokenSlice(tok_i);
const carriage_return_ending: usize = if (slice[slice.len - 2] == '\r') 2 else 1;
const line_bytes = slice[2 .. slice.len - carriage_return_ending];
const line_bytes = slice[2..];
try string_bytes.appendSlice(gpa, line_bytes);
tok_i += 1;
}
// Following lines: each line prepends a newline.
while (tok_i <= end) : (tok_i += 1) {
const slice = tree.tokenSlice(tok_i);
const carriage_return_ending: usize = if (slice[slice.len - 2] == '\r') 2 else 1;
const line_bytes = slice[2 .. slice.len - carriage_return_ending];
const line_bytes = slice[2..];
try string_bytes.ensureUnusedCapacity(gpa, line_bytes.len + 1);
string_bytes.appendAssumeCapacity('\n');
string_bytes.appendSliceAssumeCapacity(line_bytes);
Expand Down
38 changes: 38 additions & 0 deletions lib/std/zig/parser_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3087,6 +3087,22 @@ test "zig fmt: multiline string" {
);
}

test "zig fmt: multiline string with CRLF line endings" {
try testTransform("" ++
"const s =\r\n" ++
" \\\\one\r\n" ++
" \\\\two)\r\n" ++
" \\\\three\r\n" ++
";\r\n",
\\const s =
\\ \\one
\\ \\two)
\\ \\three
\\;
\\
);
}

test "zig fmt: values" {
try testCanonical(
\\test "values" {
Expand Down Expand Up @@ -4404,6 +4420,28 @@ test "zig fmt: invalid doc comments on comptime and test blocks" {
});
}

test "zig fmt: comments with CRLF line endings" {
try testTransform("" ++
"//! Top-level doc comment\r\n" ++
"//! Continuing to another line\r\n" ++
"\r\n" ++
"/// Regular doc comment\r\n" ++
"const S = struct {\r\n" ++
" // Regular comment\r\n" ++
" // More content\r\n" ++
"};\r\n",
\\//! Top-level doc comment
\\//! Continuing to another line
\\
\\/// Regular doc comment
\\const S = struct {
\\ // Regular comment
\\ // More content
\\};
\\
);
}

test "zig fmt: else comptime expr" {
try testCanonical(
\\comptime {
Expand Down
3 changes: 0 additions & 3 deletions lib/std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3170,9 +3170,6 @@ fn discardAllParams(r: *Render, fn_proto_node: Ast.Node.Index) Error!void {
fn tokenSliceForRender(tree: Ast, token_index: Ast.TokenIndex) []const u8 {
var ret = tree.tokenSlice(token_index);
switch (tree.tokens.items(.tag)[token_index]) {
.multiline_string_literal_line => {
if (ret[ret.len - 1] == '\n') ret.len -= 1;
},
.container_doc_comment, .doc_comment => {
ret = mem.trimRight(u8, ret, &std.ascii.whitespace);
},
Expand Down
4 changes: 0 additions & 4 deletions lib/std/zig/tokenizer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -847,12 +847,10 @@ pub const Tokenizer = struct {
break;
},
'\n' => {
self.index += 1;
break;
},
'\r' => {
if (self.buffer[self.index + 1] == '\n') {
self.index += 2;
break;
} else {
state = .invalid;
Expand Down Expand Up @@ -1117,7 +1115,6 @@ pub const Tokenizer = struct {
},
'\r' => {
if (self.buffer[self.index + 1] == '\n') {
self.index += 1;
result.tag = .doc_comment;
break;
} else {
Expand Down Expand Up @@ -1167,7 +1164,6 @@ pub const Tokenizer = struct {
},
'\r' => {
if (self.buffer[self.index + 1] == '\n') {
self.index += 1;
break;
} else {
state = .invalid;
Expand Down