From b5a838247bd7d66037ba48378c34ba4460747deb Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Thu, 14 Jul 2022 21:26:01 +0300 Subject: stage2: point to error location using spans --- src/Module.zig | 201 ++++++++++++++++++++++++--------------------------------- 1 file changed, 86 insertions(+), 115 deletions(-) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index 67fc0ca619..08625406ab 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -2082,60 +2082,62 @@ pub const SrcLoc = struct { return @bitCast(Ast.Node.Index, offset + @bitCast(i32, src_loc.parent_decl_node)); } - pub fn byteOffset(src_loc: SrcLoc, gpa: Allocator) !u32 { + pub const Span = struct { + start: u32, + end: u32, + }; + + pub fn span(src_loc: SrcLoc, gpa: Allocator) !Span { switch (src_loc.lazy) { .unneeded => unreachable, - .entire_file => return 0, + .entire_file => return Span{ .start = 0, .end = 1 }, - .byte_abs => |byte_index| return byte_index, + .byte_abs => |byte_index| return Span{ .start = byte_index, .end = byte_index + 1 }, .token_abs => |tok_index| { const tree = try src_loc.file_scope.getTree(gpa); - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + const start = tree.tokens.items(.start)[tok_index]; + const end = start + @intCast(u32, tree.tokenSlice(tok_index).len); + return Span{ .start = start, .end = end }; }, .node_abs => |node| { const tree = try src_loc.file_scope.getTree(gpa); - const token_starts = tree.tokens.items(.start); - const tok_index = tree.firstToken(node); - return token_starts[tok_index]; + return nodeToSpan(tree, node); }, .byte_offset => |byte_off| { const tree = try src_loc.file_scope.getTree(gpa); - const token_starts = tree.tokens.items(.start); - return token_starts[src_loc.declSrcToken()] + byte_off; + const tok_index = src_loc.declSrcToken(); + const start = tree.tokens.items(.start)[tok_index] + byte_off; + const end = start + @intCast(u32, tree.tokenSlice(tok_index).len); + return Span{ .start = start, .end = end }; }, .token_offset => |tok_off| { const tree = try src_loc.file_scope.getTree(gpa); const tok_index = src_loc.declSrcToken() + tok_off; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + const start = tree.tokens.items(.start)[tok_index]; + const end = start + @intCast(u32, tree.tokenSlice(tok_index).len); + return Span{ .start = start, .end = end }; }, .node_offset => |traced_off| { const node_off = traced_off.x; const tree = try src_loc.file_scope.getTree(gpa); const node = src_loc.declRelativeToNodeIndex(node_off); assert(src_loc.file_scope.tree_loaded); - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, node); }, .node_offset_bin_op => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); const node = src_loc.declRelativeToNodeIndex(node_off); assert(src_loc.file_scope.tree_loaded); - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, node); }, .node_offset_back2tok => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); const node = src_loc.declRelativeToNodeIndex(node_off); const tok_index = tree.firstToken(node) - 2; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + const start = tree.tokens.items(.start)[tok_index]; + const end = start + @intCast(u32, tree.tokenSlice(tok_index).len); + return Span{ .start = start, .end = end }; }, .node_offset_var_decl_ty => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); @@ -2154,8 +2156,9 @@ pub const SrcLoc = struct { } else blk: { break :blk full.ast.mut_token + 1; // the name token }; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + const start = tree.tokens.items(.start)[tok_index]; + const end = start + @intCast(u32, tree.tokenSlice(tok_index).len); + return Span{ .start = start, .end = end }; }, .node_offset_builtin_call_arg0 => |n| return src_loc.byteOffsetBuiltinCallArg(gpa, n, 0), .node_offset_builtin_call_arg1 => |n| return src_loc.byteOffsetBuiltinCallArg(gpa, n, 1), @@ -2167,10 +2170,7 @@ pub const SrcLoc = struct { const tree = try src_loc.file_scope.getTree(gpa); const node_datas = tree.nodes.items(.data); const node = src_loc.declRelativeToNodeIndex(node_off); - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[node_datas[node].rhs]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, node_datas[node].rhs); }, .node_offset_slice_ptr, .node_offset_slice_start, @@ -2187,7 +2187,7 @@ pub const SrcLoc = struct { else => unreachable, }; const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[ + const part_node = main_tokens[ switch (src_loc.lazy) { .node_offset_slice_ptr => full.ast.sliced, .node_offset_slice_start => full.ast.start, @@ -2196,8 +2196,7 @@ pub const SrcLoc = struct { else => unreachable, } ]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, part_node); }, .node_offset_call_func => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); @@ -2219,10 +2218,7 @@ pub const SrcLoc = struct { else => unreachable, }; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[full.ast.fn_expr]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, full.ast.fn_expr); }, .node_offset_field_name => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); @@ -2233,16 +2229,14 @@ pub const SrcLoc = struct { .field_access => node_datas[node].rhs, else => tree.firstToken(node) - 2, }; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + const start = tree.tokens.items(.start)[tok_index]; + const end = start + @intCast(u32, tree.tokenSlice(tok_index).len); + return Span{ .start = start, .end = end }; }, .node_offset_deref_ptr => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); - const node_datas = tree.nodes.items(.data); const node = src_loc.declRelativeToNodeIndex(node_off); - const tok_index = node_datas[node].lhs; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, node); }, .node_offset_asm_source => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); @@ -2253,10 +2247,7 @@ pub const SrcLoc = struct { .@"asm" => tree.asmFull(node), else => unreachable, }; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[full.ast.template]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, full.ast.template); }, .node_offset_asm_ret_ty => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); @@ -2269,11 +2260,7 @@ pub const SrcLoc = struct { }; const asm_output = full.outputs[0]; const node_datas = tree.nodes.items(.data); - const ret_ty_node = node_datas[asm_output].lhs; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[ret_ty_node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, node_datas[asm_output].lhs); }, .node_offset_for_cond, .node_offset_if_cond => |node_off| { @@ -2290,41 +2277,26 @@ pub const SrcLoc = struct { .@"for" => tree.forFull(node).ast.cond_expr, else => unreachable, }; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[src_node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, src_node); }, .node_offset_bin_lhs => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); const node = src_loc.declRelativeToNodeIndex(node_off); const node_datas = tree.nodes.items(.data); - const src_node = node_datas[node].lhs; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[src_node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, node_datas[node].lhs); }, .node_offset_bin_rhs => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); const node = src_loc.declRelativeToNodeIndex(node_off); const node_datas = tree.nodes.items(.data); - const src_node = node_datas[node].rhs; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[src_node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, node_datas[node].rhs); }, .node_offset_switch_operand => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); const node = src_loc.declRelativeToNodeIndex(node_off); const node_datas = tree.nodes.items(.data); - const src_node = node_datas[node].lhs; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[src_node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, node_datas[node].lhs); }, .node_offset_switch_special_prong => |node_off| { @@ -2347,9 +2319,7 @@ pub const SrcLoc = struct { mem.eql(u8, tree.tokenSlice(main_tokens[case.ast.values[0]]), "_")); if (!is_special) continue; - const tok_index = main_tokens[case_node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, case_node); } else unreachable; }, @@ -2375,9 +2345,7 @@ pub const SrcLoc = struct { for (case.ast.values) |item_node| { if (node_tags[item_node] == .switch_range) { - const tok_index = main_tokens[item_node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, item_node); } } } else unreachable; @@ -2403,10 +2371,7 @@ pub const SrcLoc = struct { }, else => unreachable, }; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[full.ast.callconv_expr]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, full.ast.callconv_expr); }, .node_offset_fn_type_ret_ty => |node_off| { @@ -2421,21 +2386,14 @@ pub const SrcLoc = struct { .fn_proto => tree.fnProto(node), else => unreachable, }; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[full.ast.return_type]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, full.ast.return_type); }, .node_offset_anyframe_type => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); const node_datas = tree.nodes.items(.data); const parent_node = src_loc.declRelativeToNodeIndex(node_off); - const node = node_datas[parent_node].rhs; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, node_datas[parent_node].rhs); }, .node_offset_lib_name => |node_off| { @@ -2462,8 +2420,9 @@ pub const SrcLoc = struct { else => unreachable, }; const tok_index = full.lib_name.?; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + const start = tree.tokens.items(.start)[tok_index]; + const end = start + @intCast(u32, tree.tokenSlice(tok_index).len); + return Span{ .start = start, .end = end }; }, .node_offset_array_type_len => |node_off| { @@ -2476,11 +2435,7 @@ pub const SrcLoc = struct { .array_type_sentinel => tree.arrayTypeSentinel(parent_node), else => unreachable, }; - const node = full.ast.elem_count; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, full.ast.elem_count); }, .node_offset_array_type_sentinel => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); @@ -2492,11 +2447,7 @@ pub const SrcLoc = struct { .array_type_sentinel => tree.arrayTypeSentinel(parent_node), else => unreachable, }; - const node = full.ast.sentinel; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, full.ast.sentinel); }, .node_offset_array_type_elem => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); @@ -2508,21 +2459,14 @@ pub const SrcLoc = struct { .array_type_sentinel => tree.arrayTypeSentinel(parent_node), else => unreachable, }; - const node = full.ast.elem_type; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[node]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, full.ast.elem_type); }, .node_offset_un_op => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); const node_datas = tree.nodes.items(.data); const node = src_loc.declRelativeToNodeIndex(node_off); - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[node_datas[node].lhs]; - const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + return nodeToSpan(tree, node_datas[node].lhs); }, } } @@ -2532,7 +2476,7 @@ pub const SrcLoc = struct { gpa: Allocator, node_off: i32, arg_index: u32, - ) !u32 { + ) !Span { const tree = try src_loc.file_scope.getTree(gpa); const node_datas = tree.nodes.items(.data); const node_tags = tree.nodes.items(.tag); @@ -2546,10 +2490,33 @@ pub const SrcLoc = struct { .builtin_call, .builtin_call_comma => tree.extra_data[node_datas[node].lhs + arg_index], else => unreachable, }; - const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[param]; + return nodeToSpan(tree, param); + } + + pub fn nodeToSpan(tree: *const Ast, node: u32) Span { const token_starts = tree.tokens.items(.start); - return token_starts[tok_index]; + const start = tree.firstToken(node); + const end = tree.lastToken(node); + if (tree.tokensOnSameLine(start, end)) { + const start_off = token_starts[start]; + const end_off = token_starts[end] + @intCast(u32, tree.tokenSlice(end).len); + return Span{ .start = start_off, .end = end_off }; + } + + const main_token = tree.nodes.items(.main_token)[node]; + if (tree.tokensOnSameLine(start, main_token)) { + const start_off = token_starts[start]; + const end_off = token_starts[main_token] + @intCast(u32, tree.tokenSlice(main_token).len); + return Span{ .start = start_off, .end = end_off }; + } + if (tree.tokensOnSameLine(main_token, end)) { + const start_off = token_starts[main_token]; + const end_off = token_starts[end] + @intCast(u32, tree.tokenSlice(end).len); + return Span{ .start = start_off, .end = end_off }; + } + const start_off = token_starts[main_token]; + const end_off = token_starts[main_token] + @intCast(u32, tree.tokenSlice(main_token).len); + return Span{ .start = start_off, .end = end_off }; } }; @@ -3313,7 +3280,11 @@ pub fn astGenFile(mod: *Module, file: *File) !void { .src_loc = .{ .file_scope = file, .parent_decl_node = 0, - .lazy = .{ .byte_abs = token_starts[parse_err.token] + extra_offset }, + .lazy = if (extra_offset == 0) .{ + .token_abs = parse_err.token, + } else .{ + .byte_abs = token_starts[parse_err.token], + }, }, .msg = msg.toOwnedSlice(), }; @@ -3336,7 +3307,7 @@ pub fn astGenFile(mod: *Module, file: *File) !void { .src_loc = .{ .file_scope = file, .parent_decl_node = 0, - .lazy = .{ .byte_abs = token_starts[note.token] }, + .lazy = .{ .token_abs = note.token }, }, .msg = msg.toOwnedSlice(), }; -- cgit v1.2.3