@@ -2085,20 +2085,21 @@ pub const SrcLoc = struct {
2085
2085
pub const Span = struct {
2086
2086
start : u32 ,
2087
2087
end : u32 ,
2088
+ main : u32 ,
2088
2089
};
2089
2090
2090
2091
pub fn span (src_loc : SrcLoc , gpa : Allocator ) ! Span {
2091
2092
switch (src_loc .lazy ) {
2092
2093
.unneeded = > unreachable ,
2093
- .entire_file = > return Span { .start = 0 , .end = 1 },
2094
+ .entire_file = > return Span { .start = 0 , .end = 1 , . main = 0 },
2094
2095
2095
- .byte_abs = > | byte_index | return Span { .start = byte_index , .end = byte_index + 1 },
2096
+ .byte_abs = > | byte_index | return Span { .start = byte_index , .end = byte_index + 1 , . main = byte_index },
2096
2097
2097
2098
.token_abs = > | tok_index | {
2098
2099
const tree = try src_loc .file_scope .getTree (gpa );
2099
2100
const start = tree .tokens .items (.start )[tok_index ];
2100
2101
const end = start + @intCast (u32 , tree .tokenSlice (tok_index ).len );
2101
- return Span { .start = start , .end = end };
2102
+ return Span { .start = start , .end = end , . main = start };
2102
2103
},
2103
2104
.node_abs = > | node | {
2104
2105
const tree = try src_loc .file_scope .getTree (gpa );
@@ -2109,14 +2110,14 @@ pub const SrcLoc = struct {
2109
2110
const tok_index = src_loc .declSrcToken ();
2110
2111
const start = tree .tokens .items (.start )[tok_index ] + byte_off ;
2111
2112
const end = start + @intCast (u32 , tree .tokenSlice (tok_index ).len );
2112
- return Span { .start = start , .end = end };
2113
+ return Span { .start = start , .end = end , . main = start };
2113
2114
},
2114
2115
.token_offset = > | tok_off | {
2115
2116
const tree = try src_loc .file_scope .getTree (gpa );
2116
2117
const tok_index = src_loc .declSrcToken () + tok_off ;
2117
2118
const start = tree .tokens .items (.start )[tok_index ];
2118
2119
const end = start + @intCast (u32 , tree .tokenSlice (tok_index ).len );
2119
- return Span { .start = start , .end = end };
2120
+ return Span { .start = start , .end = end , . main = start };
2120
2121
},
2121
2122
.node_offset = > | traced_off | {
2122
2123
const node_off = traced_off .x ;
@@ -2137,7 +2138,7 @@ pub const SrcLoc = struct {
2137
2138
const tok_index = tree .firstToken (node ) - 2 ;
2138
2139
const start = tree .tokens .items (.start )[tok_index ];
2139
2140
const end = start + @intCast (u32 , tree .tokenSlice (tok_index ).len );
2140
- return Span { .start = start , .end = end };
2141
+ return Span { .start = start , .end = end , . main = start };
2141
2142
},
2142
2143
.node_offset_var_decl_ty = > | node_off | {
2143
2144
const tree = try src_loc .file_scope .getTree (gpa );
@@ -2158,7 +2159,7 @@ pub const SrcLoc = struct {
2158
2159
};
2159
2160
const start = tree .tokens .items (.start )[tok_index ];
2160
2161
const end = start + @intCast (u32 , tree .tokenSlice (tok_index ).len );
2161
- return Span { .start = start , .end = end };
2162
+ return Span { .start = start , .end = end , . main = start };
2162
2163
},
2163
2164
.node_offset_builtin_call_arg0 = > | n | return src_loc .byteOffsetBuiltinCallArg (gpa , n , 0 ),
2164
2165
.node_offset_builtin_call_arg1 = > | n | return src_loc .byteOffsetBuiltinCallArg (gpa , n , 1 ),
@@ -2186,16 +2187,13 @@ pub const SrcLoc = struct {
2186
2187
.slice_sentinel = > tree .sliceSentinel (node ),
2187
2188
else = > unreachable ,
2188
2189
};
2189
- const main_tokens = tree .nodes .items (.main_token );
2190
- const part_node = main_tokens [
2191
- switch (src_loc .lazy ) {
2192
- .node_offset_slice_ptr = > full .ast .sliced ,
2193
- .node_offset_slice_start = > full .ast .start ,
2194
- .node_offset_slice_end = > full .ast .end ,
2195
- .node_offset_slice_sentinel = > full .ast .sentinel ,
2196
- else = > unreachable ,
2197
- }
2198
- ];
2190
+ const part_node = switch (src_loc .lazy ) {
2191
+ .node_offset_slice_ptr = > full .ast .sliced ,
2192
+ .node_offset_slice_start = > full .ast .start ,
2193
+ .node_offset_slice_end = > full .ast .end ,
2194
+ .node_offset_slice_sentinel = > full .ast .sentinel ,
2195
+ else = > unreachable ,
2196
+ };
2199
2197
return nodeToSpan (tree , part_node );
2200
2198
},
2201
2199
.node_offset_call_func = > | node_off | {
@@ -2231,7 +2229,7 @@ pub const SrcLoc = struct {
2231
2229
};
2232
2230
const start = tree .tokens .items (.start )[tok_index ];
2233
2231
const end = start + @intCast (u32 , tree .tokenSlice (tok_index ).len );
2234
- return Span { .start = start , .end = end };
2232
+ return Span { .start = start , .end = end , . main = start };
2235
2233
},
2236
2234
.node_offset_deref_ptr = > | node_off | {
2237
2235
const tree = try src_loc .file_scope .getTree (gpa );
@@ -2422,7 +2420,7 @@ pub const SrcLoc = struct {
2422
2420
const tok_index = full .lib_name .? ;
2423
2421
const start = tree .tokens .items (.start )[tok_index ];
2424
2422
const end = start + @intCast (u32 , tree .tokenSlice (tok_index ).len );
2425
- return Span { .start = start , .end = end };
2423
+ return Span { .start = start , .end = end , . main = start };
2426
2424
},
2427
2425
2428
2426
.node_offset_array_type_len = > | node_off | {
@@ -2495,28 +2493,25 @@ pub const SrcLoc = struct {
2495
2493
2496
2494
pub fn nodeToSpan (tree : * const Ast , node : u32 ) Span {
2497
2495
const token_starts = tree .tokens .items (.start );
2496
+ const main_token = tree .nodes .items (.main_token )[node ];
2498
2497
const start = tree .firstToken (node );
2499
2498
const end = tree .lastToken (node );
2500
- if (tree .tokensOnSameLine (start , end )) {
2501
- const start_off = token_starts [start ];
2502
- const end_off = token_starts [end ] + @intCast (u32 , tree .tokenSlice (end ).len );
2503
- return Span { .start = start_off , .end = end_off };
2504
- }
2499
+ var start_tok = start ;
2500
+ var end_tok = end ;
2505
2501
2506
- const main_token = tree .nodes .items (.main_token )[node ];
2507
- if (tree .tokensOnSameLine (start , main_token )) {
2508
- const start_off = token_starts [start ];
2509
- const end_off = token_starts [main_token ] + @intCast (u32 , tree .tokenSlice (main_token ).len );
2510
- return Span { .start = start_off , .end = end_off };
2511
- }
2512
- if (tree .tokensOnSameLine (main_token , end )) {
2513
- const start_off = token_starts [main_token ];
2514
- const end_off = token_starts [end ] + @intCast (u32 , tree .tokenSlice (end ).len );
2515
- return Span { .start = start_off , .end = end_off };
2502
+ if (tree .tokensOnSameLine (start , end )) {
2503
+ // do nothing
2504
+ } else if (tree .tokensOnSameLine (start , main_token )) {
2505
+ end_tok = main_token ;
2506
+ } else if (tree .tokensOnSameLine (main_token , end )) {
2507
+ start_tok = main_token ;
2508
+ } else {
2509
+ start_tok = main_token ;
2510
+ end_tok = main_token ;
2516
2511
}
2517
- const start_off = token_starts [main_token ];
2518
- const end_off = token_starts [main_token ] + @intCast (u32 , tree .tokenSlice (main_token ).len );
2519
- return Span { .start = start_off , .end = end_off };
2512
+ const start_off = token_starts [start_tok ];
2513
+ const end_off = token_starts [end_tok ] + @intCast (u32 , tree .tokenSlice (end_tok ).len );
2514
+ return Span { .start = start_off , .end = end_off , . main = token_starts [ main_token ] };
2520
2515
}
2521
2516
};
2522
2517
@@ -3283,7 +3278,7 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
3283
3278
.lazy = if (extra_offset == 0 ) .{
3284
3279
.token_abs = parse_err .token ,
3285
3280
} else .{
3286
- .byte_abs = token_starts [parse_err .token ],
3281
+ .byte_abs = token_starts [parse_err .token ] + extra_offset ,
3287
3282
},
3288
3283
},
3289
3284
.msg = msg .toOwnedSlice (),
0 commit comments