Make semantic_tokens use @asyncCall instead of a stack again.

This commit is contained in:
Alexandros Naskos 2021-04-19 17:17:46 +03:00
parent 8f868dfec6
commit 23454e111c
No known key found for this signature in database
GPG Key ID: 02BF2E72B0EA32D2

View File

@ -327,7 +327,7 @@ fn writeNodeTokens(
store: *DocumentStore,
maybe_node: ?ast.Node.Index,
) error{OutOfMemory}!void {
const start_node = maybe_node orelse return;
const node = maybe_node orelse return;
const handle = builder.handle;
const tree = handle.tree;
@ -335,24 +335,21 @@ fn writeNodeTokens(
const token_tags = tree.tokens.items(.tag);
const node_data = tree.nodes.items(.data);
const main_tokens = tree.nodes.items(.main_token);
if (start_node > node_data.len) return;
if (node == 0 or node > node_data.len) return;
var stack = std.ArrayList(ast.Node.Index).init(arena.child_allocator);
defer stack.deinit();
try stack.append(start_node);
while (stack.popOrNull()) |node| {
if (node == 0 or node > node_data.len) continue;
const FrameSize = @sizeOf(@Frame(writeNodeTokens));
var child_frame = try arena.child_allocator.alignedAlloc(u8, std.Target.stack_align, FrameSize);
defer arena.child_allocator.free(child_frame);
const tag = node_tags[node];
const main_token = main_tokens[node];
switch (tag) {
.root => unreachable,
.container_field,
.container_field_align,
.container_field_init,
=> try writeContainerField(builder, arena, store, node, .field, &stack),
=> try writeContainerField(builder, arena, store, node, .field, child_frame),
.@"errdefer" => {
try writeToken(builder, main_token, .keyword);
@ -363,7 +360,7 @@ fn writeNodeTokens(
try writeToken(builder, payload_tok + 1, .operator);
}
try stack.append(node_data[node].rhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].rhs });
},
.block,
.block_semicolon,
@ -394,9 +391,9 @@ fn writeNodeTokens(
for (statements) |child| {
try gap_highlighter.next(child);
if (node_tags[child].isContainerField()) {
try writeContainerField(builder, arena, store, child, .field, &stack);
try writeContainerField(builder, arena, store, child, .field, child_frame);
} else {
try stack.append(child);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, child });
}
}
@ -424,14 +421,14 @@ fn writeNodeTokens(
}
if (var_decl.ast.type_node != 0)
try stack.append(var_decl.ast.type_node);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, var_decl.ast.type_node });
if (var_decl.ast.align_node != 0)
try stack.append(var_decl.ast.align_node);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, var_decl.ast.align_node });
if (var_decl.ast.section_node != 0)
try stack.append(var_decl.ast.section_node);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, var_decl.ast.section_node });
try writeToken(builder, var_decl.ast.mut_token + 2, .operator);
try stack.append(var_decl.ast.init_node);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, var_decl.ast.init_node });
},
.@"usingnamespace" => {
const first_tok = tree.firstToken(node);
@ -439,7 +436,7 @@ fn writeNodeTokens(
try writeDocComments(builder, tree, first_tok - 1);
try writeToken(builder, if (token_tags[first_tok] == .keyword_pub) first_tok else null, .keyword);
try writeToken(builder, main_token, .keyword);
try stack.append(node_data[node].lhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].lhs });
},
.container_decl,
.container_decl_trailing,
@ -469,19 +466,19 @@ fn writeNodeTokens(
try writeToken(builder, decl.ast.main_token, .keyword);
if (decl.ast.enum_token) |enum_token| {
if (decl.ast.arg != 0)
try stack.append(decl.ast.arg)
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, decl.ast.arg })
else
try writeToken(builder, enum_token, .keyword);
} else if (decl.ast.arg != 0) try stack.append(decl.ast.arg);
} else if (decl.ast.arg != 0) try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, decl.ast.arg });
var gap_highlighter = GapHighlighter.init(builder, main_token + 1);
const field_token_type = fieldTokenType(node, handle);
for (decl.ast.members) |child| {
try gap_highlighter.next(child);
if (node_tags[child].isContainerField()) {
try writeContainerField(builder, arena, store, child, field_token_type, &stack);
try writeContainerField(builder, arena, store, child, field_token_type, child_frame);
} else {
try stack.append(child);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, child });
}
}
try gap_highlighter.end(lastToken(tree, node));
@ -551,32 +548,32 @@ fn writeNodeTokens(
try writeTokenMod(builder, param_decl.name_token, .parameter, .{ .declaration = true });
if (param_decl.anytype_ellipsis3) |any_token| {
try writeToken(builder, any_token, .type);
} else if (param_decl.type_expr != 0) try stack.append(param_decl.type_expr);
} else if (param_decl.type_expr != 0) try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, param_decl.type_expr });
}
if (fn_proto.ast.align_expr != 0)
try stack.append(fn_proto.ast.align_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.ast.align_expr });
if (fn_proto.ast.section_expr != 0)
try stack.append(fn_proto.ast.section_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.ast.section_expr });
if (fn_proto.ast.callconv_expr != 0)
try stack.append(fn_proto.ast.callconv_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.ast.callconv_expr });
if (fn_proto.ast.return_type != 0)
try stack.append(fn_proto.ast.return_type);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, fn_proto.ast.return_type });
if (tag == .fn_decl)
try stack.append(node_data[node].rhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].rhs });
},
.anyframe_type => {
try writeToken(builder, main_token, .type);
if (node_data[node].rhs != 0) {
try writeToken(builder, node_data[node].lhs, .type);
try stack.append(node_data[node].rhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].rhs });
}
},
.@"defer" => {
try writeToken(builder, main_token, .keyword);
try stack.append(node_data[node].rhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].rhs });
},
.@"comptime",
.@"nosuspend",
@ -584,20 +581,20 @@ fn writeNodeTokens(
if (analysis.getDocCommentTokenIndex(token_tags, main_token)) |doc|
try writeDocComments(builder, tree, doc);
try writeToken(builder, main_token, .keyword);
try stack.append(node_data[node].lhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].lhs });
},
.@"switch",
.switch_comma,
=> {
try writeToken(builder, main_token, .keyword);
try stack.append(node_data[node].lhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].lhs });
const extra = tree.extraData(node_data[node].rhs, ast.Node.SubRange);
const cases = tree.extra_data[extra.start..extra.end];
var gap_highlighter = GapHighlighter.init(builder, lastToken(tree, node_data[node].lhs) + 1);
for (cases) |case_node| {
try gap_highlighter.next(case_node);
try stack.append(case_node);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, case_node });
}
try gap_highlighter.end(lastToken(tree, node));
},
@ -605,7 +602,7 @@ fn writeNodeTokens(
.switch_case,
=> {
const switch_case = if (tag == .switch_case) tree.switchCase(node) else tree.switchCaseOne(node);
for (switch_case.ast.values) |item_node| try stack.append(item_node);
for (switch_case.ast.values) |item_node| try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, item_node });
// check it it's 'else'
if (switch_case.ast.values.len == 0) try writeToken(builder, switch_case.ast.arrow_token - 1, .keyword);
try writeToken(builder, switch_case.ast.arrow_token, .operator);
@ -613,7 +610,7 @@ fn writeNodeTokens(
const p_token = @boolToInt(token_tags[payload_token] == .asterisk);
try writeToken(builder, p_token, .variable);
}
try stack.append(switch_case.ast.target_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, switch_case.ast.target_expr });
},
.@"while",
.while_simple,
@ -625,7 +622,7 @@ fn writeNodeTokens(
try writeToken(builder, while_node.label_token, .label);
try writeToken(builder, while_node.inline_token, .keyword);
try writeToken(builder, while_node.ast.while_token, .keyword);
try stack.append(while_node.ast.cond_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, while_node.ast.cond_expr });
if (while_node.payload_token) |payload| {
try writeToken(builder, payload - 1, .operator);
try writeToken(builder, payload, .variable);
@ -638,9 +635,9 @@ fn writeNodeTokens(
try writeToken(builder, r_pipe, .operator);
}
if (while_node.ast.cont_expr != 0)
try stack.append(while_node.ast.cont_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, while_node.ast.cont_expr });
try stack.append(while_node.ast.then_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, while_node.ast.then_expr });
if (while_node.ast.else_expr != 0) {
try writeToken(builder, while_node.else_token, .keyword);
@ -650,7 +647,7 @@ fn writeNodeTokens(
try writeToken(builder, err_token, .variable);
try writeToken(builder, err_token + 1, .operator);
}
try stack.append(while_node.ast.else_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, while_node.ast.else_expr });
}
},
.@"if",
@ -659,7 +656,7 @@ fn writeNodeTokens(
const if_node = ifFull(tree, node);
try writeToken(builder, if_node.ast.if_token, .keyword);
try stack.append(if_node.ast.cond_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, if_node.ast.cond_expr });
if (if_node.payload_token) |payload| {
// if (?x) |x|
@ -667,7 +664,7 @@ fn writeNodeTokens(
try writeToken(builder, payload, .variable); // x
try writeToken(builder, payload + 1, .operator); // |
}
try stack.append(if_node.ast.then_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, if_node.ast.then_expr });
if (if_node.ast.else_expr != 0) {
try writeToken(builder, if_node.else_token, .keyword);
@ -677,7 +674,7 @@ fn writeNodeTokens(
try writeToken(builder, err_token, .variable); // err
try writeToken(builder, err_token + 1, .operator); // |
}
try stack.append(if_node.ast.else_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, if_node.ast.else_expr });
}
},
.array_init,
@ -699,8 +696,8 @@ fn writeNodeTokens(
};
if (array_init.ast.type_expr != 0)
try stack.append(array_init.ast.type_expr);
for (array_init.ast.elements) |elem| try stack.append(elem);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, array_init.ast.type_expr });
for (array_init.ast.elements) |elem| try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, elem });
},
.struct_init,
.struct_init_comma,
@ -723,7 +720,7 @@ fn writeNodeTokens(
var field_token_type: ?TokenType = null;
if (struct_init.ast.type_expr != 0) {
try stack.append(struct_init.ast.type_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, struct_init.ast.type_expr });
field_token_type = if (try analysis.resolveTypeOfNode(store, arena, .{
.node = struct_init.ast.type_expr,
@ -745,7 +742,7 @@ fn writeNodeTokens(
try writeToken(builder, init_token - 3, field_token_type orelse .field); // '.'
try writeToken(builder, init_token - 2, field_token_type orelse .field); // name
try writeToken(builder, init_token - 1, .operator); // '='
try stack.append(field_init);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, field_init });
}
try gap_highlighter.end(lastToken(tree, node));
},
@ -766,14 +763,14 @@ fn writeNodeTokens(
};
try writeToken(builder, call.async_token, .keyword);
try stack.append(call.ast.fn_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, call.ast.fn_expr });
if (builder.current_token) |curr_tok| {
if (curr_tok != lastToken(tree, call.ast.fn_expr) and token_tags[lastToken(tree, call.ast.fn_expr)] == .identifier) {
try writeToken(builder, lastToken(tree, call.ast.fn_expr), .function);
}
}
for (call.ast.params) |param| try stack.append(param);
for (call.ast.params) |param| try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, param });
},
.slice,
.slice_open,
@ -786,29 +783,29 @@ fn writeNodeTokens(
else => unreachable,
};
try stack.append(slice.ast.sliced);
try stack.append(slice.ast.start);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, slice.ast.sliced });
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, slice.ast.start });
try writeToken(builder, lastToken(tree, slice.ast.start) + 1, .operator);
if (slice.ast.end != 0)
try stack.append(slice.ast.end);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, slice.ast.end });
if (slice.ast.sentinel != 0)
try stack.append(slice.ast.sentinel);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, slice.ast.sentinel });
},
.array_access => {
try stack.append(node_data[node].lhs);
try stack.append(node_data[node].rhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].lhs });
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].rhs });
},
.deref => {
try stack.append(node_data[node].lhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].lhs });
try writeToken(builder, main_token, .operator);
},
.unwrap_optional => {
try stack.append(node_data[node].lhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].lhs });
try writeToken(builder, main_token + 1, .operator);
},
.grouped_expression => {
try stack.append(node_data[node].lhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].lhs });
},
.@"break",
.@"continue",
@ -817,12 +814,12 @@ fn writeNodeTokens(
if (node_data[node].lhs != 0)
try writeToken(builder, node_data[node].lhs, .label);
if (node_data[node].rhs != 0)
try stack.append(node_data[node].rhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].rhs });
},
.@"suspend", .@"return" => {
try writeToken(builder, main_token, .keyword);
if (node_data[node].lhs != 0)
try stack.append(node_data[node].lhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].lhs });
},
.integer_literal,
.float_literal,
@ -852,7 +849,7 @@ fn writeNodeTokens(
try writeToken(builder, main_token, .builtin);
for (params) |param|
try stack.append(param);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, param });
},
.string_literal,
.char_literal,
@ -889,7 +886,7 @@ fn writeNodeTokens(
try writeToken(builder, main_token, .keyword);
try writeToken(builder, asm_node.volatile_token, .keyword);
try stack.append(asm_node.ast.template);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, asm_node.ast.template });
// TODO Inputs, outputs.
},
.@"anytype" => {
@ -903,14 +900,14 @@ fn writeNodeTokens(
if (token_tags[main_token + 1] == .string_literal)
try writeToken(builder, main_token + 1, .string);
try stack.append(node_data[node].rhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].rhs });
},
.@"catch" => {
try stack.append(node_data[node].lhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].lhs });
try writeToken(builder, main_token, .keyword);
if (token_tags[main_token + 1] == .pipe)
try writeToken(builder, main_token + 1, .variable);
try stack.append(node_data[node].rhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].rhs });
},
.add,
.add_wrap,
@ -954,7 +951,7 @@ fn writeNodeTokens(
.sub_wrap,
.@"orelse",
=> {
try stack.append(node_data[node].lhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].lhs });
const token_type: TokenType = switch (tag) {
.bool_and, .bool_or => .keyword,
else => .operator,
@ -962,14 +959,14 @@ fn writeNodeTokens(
try writeToken(builder, main_token, token_type);
if (node_data[node].rhs != 0)
try stack.append(node_data[node].rhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].rhs });
},
.field_access => {
const data = node_data[node];
if (data.rhs == 0) return;
const rhs_str = tree.tokenSlice(data.rhs);
try stack.append(data.lhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, data.lhs });
// TODO This is basically exactly the same as what is done in analysis.resolveTypeOfNode, with the added
// writeToken code.
@ -1026,12 +1023,12 @@ fn writeNodeTokens(
if (ptr_type.size == .One and token_tags[main_token] == .asterisk_asterisk and
main_token == main_tokens[ptr_type.ast.child_type])
{
return try stack.append(ptr_type.ast.child_type);
return try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, ptr_type.ast.child_type });
}
if (ptr_type.size == .One) try writeToken(builder, main_token, .operator);
if (ptr_type.ast.sentinel != 0) {
return try stack.append(ptr_type.ast.sentinel);
return try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, ptr_type.ast.sentinel });
}
try writeToken(builder, ptr_type.allowzero_token, .keyword);
@ -1039,19 +1036,19 @@ fn writeNodeTokens(
if (ptr_type.ast.align_node != 0) {
const first_tok = tree.firstToken(ptr_type.ast.align_node);
try writeToken(builder, first_tok - 2, .keyword);
try stack.append(ptr_type.ast.align_node);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, ptr_type.ast.align_node });
if (ptr_type.ast.bit_range_start != 0) {
try stack.append(ptr_type.ast.bit_range_start);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, ptr_type.ast.bit_range_start });
try writeToken(builder, tree.firstToken(ptr_type.ast.bit_range_end - 1), .operator);
try stack.append(ptr_type.ast.bit_range_end);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, ptr_type.ast.bit_range_end });
}
}
try writeToken(builder, ptr_type.const_token, .keyword);
try writeToken(builder, ptr_type.volatile_token, .keyword);
try stack.append(ptr_type.ast.child_type);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, ptr_type.ast.child_type });
},
.array_type,
.array_type_sentinel,
@ -1061,11 +1058,11 @@ fn writeNodeTokens(
else
tree.arrayTypeSentinel(node);
try stack.append(array_type.ast.elem_count);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, array_type.ast.elem_count });
if (array_type.ast.sentinel != 0)
try stack.append(array_type.ast.sentinel);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, array_type.ast.sentinel });
try stack.append(array_type.ast.elem_type);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, array_type.ast.elem_type });
},
.address_of,
.bit_not,
@ -1075,18 +1072,17 @@ fn writeNodeTokens(
.negation_wrap,
=> {
try writeToken(builder, main_token, .operator);
try stack.append(node_data[node].lhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].lhs });
},
.@"try",
.@"resume",
.@"await",
=> {
try writeToken(builder, main_token, .keyword);
try stack.append(node_data[node].lhs);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, node_data[node].lhs });
},
.anyframe_literal => try writeToken(builder, main_token, .keyword),
}
}
}
fn writeContainerField(
@ -1095,7 +1091,7 @@ fn writeContainerField(
store: *DocumentStore,
node: ast.Node.Index,
field_token_type: ?TokenType,
stack: *std.ArrayList(ast.Node.Index),
child_frame: anytype,
) !void {
const tree = builder.handle.tree;
const container_field = containerField(tree, node).?;
@ -1111,9 +1107,9 @@ fn writeContainerField(
if (container_field.ast.type_expr != 0) {
if (container_field.ast.align_expr != 0) {
try writeToken(builder, tree.firstToken(container_field.ast.align_expr) - 2, .keyword);
try stack.append(container_field.ast.align_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, container_field.ast.align_expr });
}
try stack.append(container_field.ast.type_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, container_field.ast.type_expr });
}
if (container_field.ast.value_expr != 0) block: {
@ -1125,7 +1121,7 @@ fn writeContainerField(
break :block; // Check this, I believe it is correct.
try writeToken(builder, eq_tok, .operator);
try stack.append(container_field.ast.value_expr);
try await @asyncCall(child_frame, {}, writeNodeTokens, .{ builder, arena, store, container_field.ast.value_expr });
}
}