aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-02-17 22:14:45 +0200
committerVeikka Tuominen <git@vexu.eu>2022-02-17 22:16:26 +0200
commit6b65590715d0871c11635fc49cb1fc471a60ea59 (patch)
treef0de6e9c50c86a48df5656efb6ae963a8765d2ed /lib/std
parent92f276781417d7e710081470d97606e26cf764d6 (diff)
downloadzig-6b65590715d0871c11635fc49cb1fc471a60ea59.tar.gz
zig-6b65590715d0871c11635fc49cb1fc471a60ea59.zip
parser: add notes to decl_between_fields error
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/zig/Ast.zig11
-rw-r--r--lib/std/zig/parse.zig25
-rw-r--r--lib/std/zig/parser_test.zig2
3 files changed, 38 insertions, 0 deletions
diff --git a/lib/std/zig/Ast.zig b/lib/std/zig/Ast.zig
index 5501352084..a68959837a 100644
--- a/lib/std/zig/Ast.zig
+++ b/lib/std/zig/Ast.zig
@@ -329,6 +329,13 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void {
return stream.writeAll("expected field initializer");
},
+ .previous_field => {
+ return stream.writeAll("field before declarations here");
+ },
+ .next_field => {
+ return stream.writeAll("field after declarations here");
+ },
+
.expected_token => {
const found_tag = token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)];
const expected_symbol = parse_error.extra.expected_tag.symbol();
@@ -2470,6 +2477,7 @@ pub const full = struct {
pub const Error = struct {
tag: Tag,
+ is_note: bool = false,
/// True if `token` points to the token before the token causing an issue.
token_is_prev: bool = false,
token: TokenIndex,
@@ -2527,6 +2535,9 @@ pub const Error = struct {
expected_comma_after_switch_prong,
expected_initializer,
+ previous_field,
+ next_field,
+
/// `expected_tag` is populated.
expected_token,
};
diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig
index 46d2b0f49e..2578629af5 100644
--- a/lib/std/zig/parse.zig
+++ b/lib/std/zig/parse.zig
@@ -91,6 +91,9 @@ const Parser = struct {
extra_data: std.ArrayListUnmanaged(Node.Index),
scratch: std.ArrayListUnmanaged(Node.Index),
+ /// Used for the error note of decl_between_fields error.
+ last_field: TokenIndex = undefined,
+
const SmallSpan = union(enum) {
zero_or_one: Node.Index,
multi: Node.SubRange,
@@ -270,6 +273,8 @@ const Parser = struct {
.keyword_comptime => switch (p.token_tags[p.tok_i + 1]) {
.identifier => {
p.tok_i += 1;
+ const identifier = p.tok_i;
+ defer p.last_field = identifier;
const container_field = try p.expectContainerFieldRecoverable();
if (container_field != 0) {
switch (field_state) {
@@ -280,6 +285,16 @@ const Parser = struct {
.tag = .decl_between_fields,
.token = p.nodes.items(.main_token)[node],
});
+ try p.warnMsg(.{
+ .tag = .previous_field,
+ .is_note = true,
+ .token = p.last_field,
+ });
+ try p.warnMsg(.{
+ .tag = .next_field,
+ .is_note = true,
+ .token = identifier,
+ });
// Continue parsing; error will be reported later.
field_state = .err;
},
@@ -373,6 +388,8 @@ const Parser = struct {
trailing = p.token_tags[p.tok_i - 1] == .semicolon;
},
.identifier => {
+ const identifier = p.tok_i;
+ defer p.last_field = identifier;
const container_field = try p.expectContainerFieldRecoverable();
if (container_field != 0) {
switch (field_state) {
@@ -383,6 +400,14 @@ const Parser = struct {
.tag = .decl_between_fields,
.token = p.nodes.items(.main_token)[node],
});
+ try p.warnMsg(.{
+ .tag = .previous_field,
+ .token = p.last_field,
+ });
+ try p.warnMsg(.{
+ .tag = .next_field,
+ .token = identifier,
+ });
// Continue parsing; error will be reported later.
field_state = .err;
},
diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig
index 9bf7ae8da9..0c79b3b187 100644
--- a/lib/std/zig/parser_test.zig
+++ b/lib/std/zig/parser_test.zig
@@ -226,6 +226,8 @@ test "zig fmt: decl between fields" {
\\};
, &[_]Error{
.decl_between_fields,
+ .previous_field,
+ .next_field,
});
}