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 {...@@ -178,7 +178,6 @@ const Parser = struct {
178 .expected_block_or_assignment,178 .expected_block_or_assignment,
179 .expected_block_or_expr,179 .expected_block_or_expr,
180 .expected_block_or_field,180 .expected_block_or_field,
181 .expected_container_members,
182 .expected_expr,181 .expected_expr,
183 .expected_expr_or_assignment,182 .expected_expr_or_assignment,
184 .expected_fn,183 .expected_fn,
...@@ -401,10 +400,12 @@ const Parser = struct {...@@ -401,10 +400,12 @@ const Parser = struct {
401 });400 });
402 try p.warnMsg(.{401 try p.warnMsg(.{
403 .tag = .previous_field,402 .tag = .previous_field,
403 .is_note = true,
404 .token = last_field,404 .token = last_field,
405 });405 });
406 try p.warnMsg(.{406 try p.warnMsg(.{
407 .tag = .next_field,407 .tag = .next_field,
408 .is_note = true,
408 .token = identifier,409 .token = identifier,
409 });410 });
410 // Continue parsing; error will be reported later.411 // Continue parsing; error will be reported later.
...@@ -985,7 +986,7 @@ const Parser = struct {...@@ -985,7 +986,7 @@ const Parser = struct {
985 .keyword_switch => return p.expectSwitchExpr(),986 .keyword_switch => return p.expectSwitchExpr(),
986 .keyword_if => return p.expectIfStatement(),987 .keyword_if => return p.expectIfStatement(),
987 .keyword_enum, .keyword_struct, .keyword_union => {988 .keyword_enum, .keyword_struct, .keyword_union => {
988 const identifier = p.tok_i + 2;989 const identifier = p.tok_i + 1;
989 if (try p.parseCStyleContainer()) {990 if (try p.parseCStyleContainer()) {
990 // Return something so that `expectStatement` is happy.991 // Return something so that `expectStatement` is happy.
991 return p.addNode(.{992 return p.addNode(.{
src/Module.zig+15-20
...@@ -3324,26 +3324,21 @@ pub fn astGenFile(mod: *Module, file: *File) !void {...@@ -3324,26 +3324,21 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
3324 .parent_decl_node = 0,3324 .parent_decl_node = 0,
3325 .lazy = .{ .byte_abs = byte_abs },3325 .lazy = .{ .byte_abs = byte_abs },
3326 }, err_msg, "invalid byte: '{'}'", .{std.zig.fmtEscapes(source[byte_abs..][0..1])});3326 }, err_msg, "invalid byte: '{'}'", .{std.zig.fmtEscapes(source[byte_abs..][0..1])});
3327 } else if (parse_err.tag == .decl_between_fields) {3327 }
3328 try mod.errNoteNonLazy(.{3328
3329 .file_scope = file,3329 for (file.tree.errors[1..]) |note| {
3330 .parent_decl_node = 0,3330 if (!note.is_note) break;
3331 .lazy = .{ .byte_abs = token_starts[file.tree.errors[1].token] },3331
3332 }, err_msg, "field before declarations here", .{});3332 try file.tree.renderError(note, msg.writer());
3333 try mod.errNoteNonLazy(.{3333 err_msg.notes = try mod.gpa.realloc(err_msg.notes, err_msg.notes.len + 1);
3334 .file_scope = file,3334 err_msg.notes[err_msg.notes.len - 1] = .{
3335 .parent_decl_node = 0,3335 .src_loc = .{
3336 .lazy = .{ .byte_abs = token_starts[file.tree.errors[2].token] },3336 .file_scope = file,
3337 }, err_msg, "field after declarations here", .{});3337 .parent_decl_node = 0,
3338 } else if (parse_err.tag == .c_style_container) {3338 .lazy = .{ .byte_abs = token_starts[note.token] },
3339 const note = file.tree.errors[1];3339 },
3340 try mod.errNoteNonLazy(.{3340 .msg = msg.toOwnedSlice(),
3341 .file_scope = file,3341 };
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 });
3347 }3342 }
33483343
3349 {3344 {
src/main.zig+15-38
...@@ -4367,7 +4367,7 @@ fn printErrsMsgToStdErr(...@@ -4367,7 +4367,7 @@ fn printErrsMsgToStdErr(
4367 defer text_buf.deinit();4367 defer text_buf.deinit();
4368 const writer = text_buf.writer();4368 const writer = text_buf.writer();
4369 try tree.renderError(parse_error, writer);4369 try tree.renderError(parse_error, writer);
4370 const text = text_buf.items;4370 const text = try arena.dupe(u8, text_buf.items);
43714371
4372 var notes_buffer: [2]Compilation.AllErrors.Message = undefined;4372 var notes_buffer: [2]Compilation.AllErrors.Message = undefined;
4373 var notes_len: usize = 0;4373 var notes_len: usize = 0;
...@@ -4388,49 +4388,26 @@ fn printErrsMsgToStdErr(...@@ -4388,49 +4388,26 @@ fn printErrsMsgToStdErr(
4388 },4388 },
4389 };4389 };
4390 notes_len += 1;4390 notes_len += 1;
4391 } else if (parse_error.tag == .decl_between_fields) {4391 }
4392 const prev_loc = tree.tokenLocation(0, parse_errors[i + 1].token);4392
4393 notes_buffer[0] = .{4393 for (parse_errors[i + 1 ..]) |note| {
4394 .src = .{4394 if (!note.is_note) break;
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];
44184395
4419 const prev_loc = tree.tokenLocation(0, parse_errors[i + 1].token);4396 text_buf.items.len = 0;
4420 notes_buffer[0] = .{4397 try tree.renderError(note, writer);
4398 const note_loc = tree.tokenLocation(0, note.token);
4399 notes_buffer[notes_len] = .{
4421 .src = .{4400 .src = .{
4422 .src_path = path,4401 .src_path = path,
4423 .msg = try std.fmt.allocPrint(arena, "to declare a container do 'const {s} = {s}'", .{4402 .msg = try arena.dupe(u8, text_buf.items),
4424 tree.tokenSlice(note.token), note.extra.expected_tag.symbol(),4403 .byte_offset = @intCast(u32, note_loc.line_start),
4425 }),4404 .line = @intCast(u32, note_loc.line),
4426 .byte_offset = @intCast(u32, prev_loc.line_start),4405 .column = @intCast(u32, note_loc.column),
4427 .line = @intCast(u32, prev_loc.line),4406 .source_line = tree.source[note_loc.line_start..note_loc.line_end],
4428 .column = @intCast(u32, prev_loc.column),
4429 .source_line = tree.source[prev_loc.line_start..prev_loc.line_end],
4430 },4407 },
4431 };4408 };
4432 notes_len = 1;
4433 i += 1;4409 i += 1;
4410 notes_len += 1;
4434 }4411 }
44354412
4436 const extra_offset = tree.errorOffset(parse_error);4413 const extra_offset = tree.errorOffset(parse_error);