authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-12 10:49:37+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-12 12:50:59+03:00
log2a3f3766a437faed13736c1ff505854b6737ae33
tree1c3756e7be1b39cb7d02083a1f29b7aa2a11cb42
parent5d22204d2dbf686d8b827eca15253914ef9543ab

stage2: handle parser notes in a more general way


3 files changed, 33 insertions(+), 60 deletions(-)

lib/std/zig/parse.zig+3-2
......@@ -178,7 +178,6 @@ const Parser = struct {
178178 .expected_block_or_assignment,
179179 .expected_block_or_expr,
180180 .expected_block_or_field,
181 .expected_container_members,
182181 .expected_expr,
183182 .expected_expr_or_assignment,
184183 .expected_fn,
......@@ -401,10 +400,12 @@ const Parser = struct {
401400 });
402401 try p.warnMsg(.{
403402 .tag = .previous_field,
403 .is_note = true,
404404 .token = last_field,
405405 });
406406 try p.warnMsg(.{
407407 .tag = .next_field,
408 .is_note = true,
408409 .token = identifier,
409410 });
410411 // Continue parsing; error will be reported later.
......@@ -985,7 +986,7 @@ const Parser = struct {
985986 .keyword_switch => return p.expectSwitchExpr(),
986987 .keyword_if => return p.expectIfStatement(),
987988 .keyword_enum, .keyword_struct, .keyword_union => {
988 const identifier = p.tok_i + 2;
989 const identifier = p.tok_i + 1;
989990 if (try p.parseCStyleContainer()) {
990991 // Return something so that `expectStatement` is happy.
991992 return p.addNode(.{
src/Module.zig+15-20
......@@ -3324,26 +3324,21 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
33243324 .parent_decl_node = 0,
33253325 .lazy = .{ .byte_abs = byte_abs },
33263326 }, err_msg, "invalid byte: '{'}'", .{std.zig.fmtEscapes(source[byte_abs..][0..1])});
3327 } else if (parse_err.tag == .decl_between_fields) {
3328 try mod.errNoteNonLazy(.{
3329 .file_scope = file,
3330 .parent_decl_node = 0,
3331 .lazy = .{ .byte_abs = token_starts[file.tree.errors[1].token] },
3332 }, err_msg, "field before declarations here", .{});
3333 try mod.errNoteNonLazy(.{
3334 .file_scope = file,
3335 .parent_decl_node = 0,
3336 .lazy = .{ .byte_abs = token_starts[file.tree.errors[2].token] },
3337 }, err_msg, "field after declarations here", .{});
3338 } else if (parse_err.tag == .c_style_container) {
3339 const note = file.tree.errors[1];
3340 try mod.errNoteNonLazy(.{
3341 .file_scope = file,
3342 .parent_decl_node = 0,
3343 .lazy = .{ .byte_abs = token_starts[note.token] },
3344 }, err_msg, "to declare a container do 'const {s} = {s}'", .{
3345 file.tree.tokenSlice(note.token), note.extra.expected_tag.symbol(),
3346 });
3327 }
3328
3329 for (file.tree.errors[1..]) |note| {
3330 if (!note.is_note) break;
3331
3332 try file.tree.renderError(note, msg.writer());
3333 err_msg.notes = try mod.gpa.realloc(err_msg.notes, err_msg.notes.len + 1);
3334 err_msg.notes[err_msg.notes.len - 1] = .{
3335 .src_loc = .{
3336 .file_scope = file,
3337 .parent_decl_node = 0,
3338 .lazy = .{ .byte_abs = token_starts[note.token] },
3339 },
3340 .msg = msg.toOwnedSlice(),
3341 };
33473342 }
33483343
33493344 {
src/main.zig+15-38
......@@ -4367,7 +4367,7 @@ fn printErrsMsgToStdErr(
43674367 defer text_buf.deinit();
43684368 const writer = text_buf.writer();
43694369 try tree.renderError(parse_error, writer);
4370 const text = text_buf.items;
4370 const text = try arena.dupe(u8, text_buf.items);
43714371
43724372 var notes_buffer: [2]Compilation.AllErrors.Message = undefined;
43734373 var notes_len: usize = 0;
......@@ -4388,49 +4388,26 @@ fn printErrsMsgToStdErr(
43884388 },
43894389 };
43904390 notes_len += 1;
4391 } else if (parse_error.tag == .decl_between_fields) {
4392 const prev_loc = tree.tokenLocation(0, parse_errors[i + 1].token);
4393 notes_buffer[0] = .{
4394 .src = .{
4395 .src_path = path,
4396 .msg = "field before declarations here",
4397 .byte_offset = @intCast(u32, prev_loc.line_start),
4398 .line = @intCast(u32, prev_loc.line),
4399 .column = @intCast(u32, prev_loc.column),
4400 .source_line = tree.source[prev_loc.line_start..prev_loc.line_end],
4401 },
4402 };
4403 const next_loc = tree.tokenLocation(0, parse_errors[i + 2].token);
4404 notes_buffer[1] = .{
4405 .src = .{
4406 .src_path = path,
4407 .msg = "field after declarations here",
4408 .byte_offset = @intCast(u32, next_loc.line_start),
4409 .line = @intCast(u32, next_loc.line),
4410 .column = @intCast(u32, next_loc.column),
4411 .source_line = tree.source[next_loc.line_start..next_loc.line_end],
4412 },
4413 };
4414 notes_len = 2;
4415 i += 2;
4416 } else if (parse_error.tag == .c_style_container) {
4417 const note = tree.errors[i + 1];
4391 }
4392
4393 for (parse_errors[i + 1 ..]) |note| {
4394 if (!note.is_note) break;
44184395
4419 const prev_loc = tree.tokenLocation(0, parse_errors[i + 1].token);
4420 notes_buffer[0] = .{
4396 text_buf.items.len = 0;
4397 try tree.renderError(note, writer);
4398 const note_loc = tree.tokenLocation(0, note.token);
4399 notes_buffer[notes_len] = .{
44214400 .src = .{
44224401 .src_path = path,
4423 .msg = try std.fmt.allocPrint(arena, "to declare a container do 'const {s} = {s}'", .{
4424 tree.tokenSlice(note.token), note.extra.expected_tag.symbol(),
4425 }),
4426 .byte_offset = @intCast(u32, prev_loc.line_start),
4427 .line = @intCast(u32, prev_loc.line),
4428 .column = @intCast(u32, prev_loc.column),
4429 .source_line = tree.source[prev_loc.line_start..prev_loc.line_end],
4402 .msg = try arena.dupe(u8, text_buf.items),
4403 .byte_offset = @intCast(u32, note_loc.line_start),
4404 .line = @intCast(u32, note_loc.line),
4405 .column = @intCast(u32, note_loc.column),
4406 .source_line = tree.source[note_loc.line_start..note_loc.line_end],
44304407 },
44314408 };
4432 notes_len = 1;
44334409 i += 1;
4410 notes_len += 1;
44344411 }
44354412
44364413 const extra_offset = tree.errorOffset(parse_error);