std.zig.parser supports same-line comments on any token

This commit is contained in:
Andrew Kelley 2018-05-07 23:54:35 -04:00
parent 0cb65b266a
commit ca27ce3bee
3 changed files with 351 additions and 273 deletions

View File

@ -25,7 +25,10 @@ pub const Tree = struct {
}
pub fn tokenSlice(self: &Tree, token_index: TokenIndex) []const u8 {
const token = self.tokens.at(token_index);
return self.tokenSlicePtr(self.tokens.at(token_index));
}
pub fn tokenSlicePtr(self: &Tree, token: &const Token) []const u8 {
return self.source[token.start..token.end];
}
@ -36,14 +39,14 @@ pub const Tree = struct {
line_end: usize,
};
pub fn tokenLocation(self: &Tree, start_index: usize, token_index: TokenIndex) Location {
pub fn tokenLocationPtr(self: &Tree, start_index: usize, token: &const Token) Location {
var loc = Location {
.line = 0,
.column = 0,
.line_start = start_index,
.line_end = self.source.len,
};
const token_start = self.tokens.at(token_index).start;
const token_start = token.start;
for (self.source[start_index..]) |c, i| {
if (i + start_index == token_start) {
loc.line_end = i + start_index;
@ -61,6 +64,9 @@ pub const Tree = struct {
return loc;
}
pub fn tokenLocation(self: &Tree, start_index: usize, token_index: TokenIndex) Location {
return self.tokenLocationPtr(start_index, self.tokens.at(token_index));
}
};
pub const Error = union(enum) {

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,48 @@
//test "zig fmt: same-line comment after a statement" {
// try testCanonical(
// \\test "" {
// \\ a = b;
// \\ debug.assert(H.digest_size <= H.block_size); // HMAC makes this assumption
// \\ a = b;
// \\}
// \\
// );
//}
//
//test "zig fmt: same-line comment after var decl in struct" {
// try testCanonical(
// \\pub const vfs_cap_data = extern struct {
// \\ const Data = struct {}; // when on disk.
// \\};
// \\
// );
//}
//
//test "zig fmt: same-line comment after field decl" {
// try testCanonical(
// \\pub const dirent = extern struct {
// \\ d_name: u8,
// \\ d_name: u8, // comment 1
// \\ d_name: u8,
// \\ d_name: u8, // comment 2
// \\ d_name: u8,
// \\};
// \\
// );
//}
//
//test "zig fmt: same-line comment after switch prong" {
// try testCanonical(
// \\test "" {
// \\ switch (err) {
// \\ error.PathAlreadyExists => {}, // comment 2
// \\ else => return err, // comment 1
// \\ }
// \\}
// \\
// );
//}
//
//test "zig fmt: same-line comment after non-block if expression" {
// try testCanonical(
// \\comptime {
@ -7,6 +52,15 @@
// \\
// );
//}
//
//test "zig fmt: same-line comment on comptime expression" {
// try testCanonical(
// \\test "" {
// \\ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
// \\}
// \\
// );
//}
test "zig fmt: switch with empty body" {
try testCanonical(
@ -17,15 +71,6 @@ test "zig fmt: switch with empty body" {
);
}
//test "zig fmt: same-line comment on comptime expression" {
// try testCanonical(
// \\test "" {
// \\ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
// \\}
// \\
// );
//}
test "zig fmt: float literal with exponent" {
try testCanonical(
\\pub const f64_true_min = 4.94065645841246544177e-324;
@ -152,18 +197,6 @@ test "zig fmt: comments before switch prong" {
);
}
//test "zig fmt: same-line comment after switch prong" {
// try testCanonical(
// \\test "" {
// \\ switch (err) {
// \\ error.PathAlreadyExists => {}, // comment 2
// \\ else => return err, // comment 1
// \\ }
// \\}
// \\
// );
//}
test "zig fmt: comments before var decl in struct" {
try testCanonical(
\\pub const vfs_cap_data = extern struct {
@ -189,28 +222,6 @@ test "zig fmt: comments before var decl in struct" {
);
}
//test "zig fmt: same-line comment after var decl in struct" {
// try testCanonical(
// \\pub const vfs_cap_data = extern struct {
// \\ const Data = struct {}; // when on disk.
// \\};
// \\
// );
//}
//
//test "zig fmt: same-line comment after field decl" {
// try testCanonical(
// \\pub const dirent = extern struct {
// \\ d_name: u8,
// \\ d_name: u8, // comment 1
// \\ d_name: u8,
// \\ d_name: u8, // comment 2
// \\ d_name: u8,
// \\};
// \\
// );
//}
test "zig fmt: array literal with 1 item on 1 line" {
try testCanonical(
\\var s = []const u64{0} ** 25;
@ -218,17 +229,6 @@ test "zig fmt: array literal with 1 item on 1 line" {
);
}
//test "zig fmt: same-line comment after a statement" {
// try testCanonical(
// \\test "" {
// \\ a = b;
// \\ debug.assert(H.digest_size <= H.block_size); // HMAC makes this assumption
// \\ a = b;
// \\}
// \\
// );
//}
test "zig fmt: comments before global variables" {
try testCanonical(
\\/// Foo copies keys and values before they go into the map, and