Merge pull request #77 from alexnask/new_parser

We now compile on zig master with the new parser changes
This commit is contained in:
Alexandros Naskos 2020-05-24 17:33:26 +03:00 committed by GitHub
commit 66fdbc0b7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 131 additions and 147 deletions

View File

@ -51,24 +51,24 @@ pub fn getDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, node: *ast
return try collectDocComments(allocator, tree, doc_comments); return try collectDocComments(allocator, tree, doc_comments);
} }
}, },
.ParamDecl => {
const param = node.cast(ast.Node.ParamDecl).?;
if (param.doc_comments) |doc_comments| {
return try collectDocComments(allocator, tree, doc_comments);
}
},
else => {}, else => {},
} }
return null; return null;
} }
fn collectDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, doc_comments: *ast.Node.DocComment) ![]const u8 { fn collectDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, doc_comments: *ast.Node.DocComment) ![]const u8 {
var doc_it = doc_comments.lines.iterator(0);
var lines = std.ArrayList([]const u8).init(allocator); var lines = std.ArrayList([]const u8).init(allocator);
defer lines.deinit(); defer lines.deinit();
while (doc_it.next()) |doc_comment| { var curr_line_tok = doc_comments.first_line;
_ = try lines.append(std.fmt.trim(tree.tokenSlice(doc_comment.*)[3..])); while (true) : (curr_line_tok += 1) {
switch (tree.token_ids[curr_line_tok]) {
.LineComment => continue,
.DocComment, .ContainerDocComment => {
try lines.append(std.fmt.trim(tree.tokenSlice(curr_line_tok)[3..]));
},
else => break,
}
} }
return try std.mem.join(allocator, "\n", lines.items); return try std.mem.join(allocator, "\n", lines.items);
@ -76,11 +76,11 @@ fn collectDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, doc_commen
/// Gets a function signature (keywords, name, return value) /// Gets a function signature (keywords, name, return value)
pub fn getFunctionSignature(tree: *ast.Tree, func: *ast.Node.FnProto) []const u8 { pub fn getFunctionSignature(tree: *ast.Tree, func: *ast.Node.FnProto) []const u8 {
const start = tree.tokens.at(func.firstToken()).start; const start = tree.token_locs[func.firstToken()].start;
const end = tree.tokens.at(switch (func.return_type) { const end = tree.token_locs[switch (func.return_type) {
.Explicit, .InferErrorSet => |node| node.lastToken(), .Explicit, .InferErrorSet => |node| node.lastToken(),
.Invalid => |r_paren| r_paren, .Invalid => |r_paren| r_paren,
}).end; }].end;
return tree.source[start..end]; return tree.source[start..end];
} }
@ -96,38 +96,33 @@ pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func:
var buf_stream = buffer.outStream(); var buf_stream = buffer.outStream();
var param_num = @as(usize, 1); for (func.paramsConst()) |param, param_num| {
var param_it = func.params.iterator(0); if (param_num != 0) try buffer.appendSlice(", ${") else try buffer.appendSlice("${");
while (param_it.next()) |param_ptr| : (param_num += 1) {
const param = param_ptr.*;
const param_decl = param.cast(ast.Node.ParamDecl).?;
if (param_num != 1) try buffer.appendSlice(", ${") else try buffer.appendSlice("${"); try buf_stream.print("{}:", .{param_num + 1});
try buf_stream.print("{}:", .{param_num}); if (param.comptime_token) |_| {
if (param_decl.comptime_token) |_| {
try buffer.appendSlice("comptime "); try buffer.appendSlice("comptime ");
} }
if (param_decl.noalias_token) |_| { if (param.noalias_token) |_| {
try buffer.appendSlice("noalias "); try buffer.appendSlice("noalias ");
} }
if (param_decl.name_token) |name_token| { if (param.name_token) |name_token| {
try buffer.appendSlice(tree.tokenSlice(name_token)); try buffer.appendSlice(tree.tokenSlice(name_token));
try buffer.appendSlice(": "); try buffer.appendSlice(": ");
} }
switch (param_decl.param_type) { switch (param.param_type) {
.var_args => try buffer.appendSlice("..."), .var_args => try buffer.appendSlice("..."),
.var_type => try buffer.appendSlice("var"), .var_type => try buffer.appendSlice("var"),
.type_expr => |type_expr| { .type_expr => |type_expr| {
var curr_tok = type_expr.firstToken(); var curr_tok = type_expr.firstToken();
var end_tok = type_expr.lastToken(); var end_tok = type_expr.lastToken();
while (curr_tok <= end_tok) : (curr_tok += 1) { while (curr_tok <= end_tok) : (curr_tok += 1) {
const id = tree.tokens.at(curr_tok).id; const id = tree.token_ids[curr_tok];
const is_comma = tree.tokens.at(curr_tok).id == .Comma; const is_comma = id == .Comma;
if (curr_tok == end_tok and is_comma) continue; if (curr_tok == end_tok and is_comma) continue;
@ -146,15 +141,8 @@ pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func:
/// Gets a function signature (keywords, name, return value) /// Gets a function signature (keywords, name, return value)
pub fn getVariableSignature(tree: *ast.Tree, var_decl: *ast.Node.VarDecl) []const u8 { pub fn getVariableSignature(tree: *ast.Tree, var_decl: *ast.Node.VarDecl) []const u8 {
const start = tree.tokens.at(var_decl.firstToken()).start; const start = tree.token_locs[var_decl.firstToken()].start;
const end = tree.tokens.at(var_decl.semicolon_token).start; const end = tree.token_locs[var_decl.semicolon_token].start;
return tree.source[start..end];
}
/// Gets a param signature
pub fn getParamSignature(tree: *ast.Tree, param: *ast.Node.ParamDecl) []const u8 {
const start = tree.tokens.at(param.firstToken()).start;
const end = tree.tokens.at(param.lastToken()).end;
return tree.source[start..end]; return tree.source[start..end];
} }
@ -186,11 +174,6 @@ pub fn getDeclNameToken(tree: *ast.Tree, node: *ast.Node) ?ast.TokenIndex {
const vari = node.cast(ast.Node.VarDecl).?; const vari = node.cast(ast.Node.VarDecl).?;
return vari.name_token; return vari.name_token;
}, },
.ParamDecl => {
const decl = node.cast(ast.Node.ParamDecl).?;
if (decl.name_token == null) return null;
return decl.name_token.?;
},
.FnProto => { .FnProto => {
const func = node.cast(ast.Node.FnProto).?; const func = node.cast(ast.Node.FnProto).?;
if (func.name_token == null) return null; if (func.name_token == null) return null;
@ -217,8 +200,8 @@ fn getDeclName(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
/// Gets the child of node /// Gets the child of node
pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node { pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node {
var index: usize = 0; var child_idx: usize = 0;
while (node.iterate(index)) |child| : (index += 1) { while (node.iterate(child_idx)) |child| : (child_idx += 1) {
const child_name = getDeclName(tree, child) orelse continue; const child_name = getDeclName(tree, child) orelse continue;
if (std.mem.eql(u8, child_name, name)) return child; if (std.mem.eql(u8, child_name, name)) return child;
} }
@ -241,20 +224,18 @@ fn findReturnStatementInternal(
already_found: *bool, already_found: *bool,
) ?*ast.Node.ControlFlowExpression { ) ?*ast.Node.ControlFlowExpression {
var result: ?*ast.Node.ControlFlowExpression = null; var result: ?*ast.Node.ControlFlowExpression = null;
var idx: usize = 0; var child_idx: usize = 0;
while (base_node.iterate(idx)) |child_node| : (idx += 1) { while (base_node.iterate(child_idx)) |child_node| : (child_idx += 1) {
switch (child_node.id) { switch (child_node.id) {
.ControlFlowExpression => { .ControlFlowExpression => {
const cfe = child_node.cast(ast.Node.ControlFlowExpression).?; const cfe = child_node.cast(ast.Node.ControlFlowExpression).?;
if (cfe.kind == .Return) { if (cfe.kind == .Return) {
// If we are calling ourselves recursively, ignore this return. // If we are calling ourselves recursively, ignore this return.
if (cfe.rhs) |rhs| { if (cfe.rhs) |rhs| {
if (rhs.cast(ast.Node.SuffixOp)) |suffix_op| { if (rhs.cast(ast.Node.Call)) |call_node| {
if (suffix_op.op == .Call) { if (call_node.lhs.id == .Identifier) {
if (suffix_op.lhs.node.id == .Identifier) { if (std.mem.eql(u8, getDeclName(tree, call_node.lhs).?, getDeclName(tree, &fn_decl.base).?)) {
if (std.mem.eql(u8, getDeclName(tree, suffix_op.lhs.node).?, getDeclName(tree, &fn_decl.base).?)) { continue;
continue;
}
} }
} }
} }
@ -311,15 +292,6 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?) orelse null; return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?) orelse null;
}, },
.ParamDecl => {
const decl = node.cast(ast.Node.ParamDecl).?;
switch (decl.param_type) {
.var_type, .type_expr => |var_type| {
return resolveTypeOfNode(analysis_ctx, var_type) orelse null;
},
else => {},
}
},
.Identifier => { .Identifier => {
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, analysis_ctx.tree.getNodeSource(node))) |child| { if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, analysis_ctx.tree.getNodeSource(node))) |child| {
return resolveTypeOfNode(analysis_ctx, child); return resolveTypeOfNode(analysis_ctx, child);
@ -329,18 +301,21 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
const field = node.cast(ast.Node.ContainerField).?; const field = node.cast(ast.Node.ContainerField).?;
return resolveTypeOfNode(analysis_ctx, field.type_expr orelse return null); return resolveTypeOfNode(analysis_ctx, field.type_expr orelse return null);
}, },
.SuffixOp => { .Call => {
const suffix_op = node.cast(ast.Node.SuffixOp).?; const call = node.cast(ast.Node.Call).?;
switch (suffix_op.op) { const decl = resolveTypeOfNode(analysis_ctx, call.lhs) orelse return null;
.Call, .StructInitializer => { return switch (decl.id) {
const decl = resolveTypeOfNode(analysis_ctx, suffix_op.lhs.node) orelse return null; .FnProto => resolveReturnType(analysis_ctx, decl.cast(ast.Node.FnProto).?),
return switch (decl.id) { else => decl,
.FnProto => resolveReturnType(analysis_ctx, decl.cast(ast.Node.FnProto).?), };
else => decl, },
}; .StructInitializer => {
}, const struct_init = node.cast(ast.Node.StructInitializer).?;
else => {}, const decl = resolveTypeOfNode(analysis_ctx, struct_init.lhs) orelse return null;
} return switch (decl.id) {
.FnProto => resolveReturnType(analysis_ctx, decl.cast(ast.Node.FnProto).?),
else => decl,
};
}, },
.InfixOp => { .InfixOp => {
const infix_op = node.cast(ast.Node.InfixOp).?; const infix_op = node.cast(ast.Node.InfixOp).?;
@ -363,8 +338,8 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
switch (prefix_op.op) { switch (prefix_op.op) {
.SliceType, .ArrayType => return node, .SliceType, .ArrayType => return node,
.PtrType => { .PtrType => {
const op_token = analysis_ctx.tree.tokens.at(prefix_op.op_token); const op_token_id = analysis_ctx.tree.token_ids[prefix_op.op_token];
switch (op_token.id) { switch (op_token_id) {
.Asterisk => return resolveTypeOfNode(analysis_ctx, prefix_op.rhs), .Asterisk => return resolveTypeOfNode(analysis_ctx, prefix_op.rhs),
.LBracket, .AsteriskAsterisk => return null, .LBracket, .AsteriskAsterisk => return null,
else => unreachable, else => unreachable,
@ -388,14 +363,14 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
const builtin_call = node.cast(ast.Node.BuiltinCall).?; const builtin_call = node.cast(ast.Node.BuiltinCall).?;
const call_name = analysis_ctx.tree.tokenSlice(builtin_call.builtin_token); const call_name = analysis_ctx.tree.tokenSlice(builtin_call.builtin_token);
if (std.mem.eql(u8, call_name, "@This")) { if (std.mem.eql(u8, call_name, "@This")) {
if (builtin_call.params.len != 0) return null; if (builtin_call.params_len != 0) return null;
return analysis_ctx.in_container; return analysis_ctx.in_container;
} }
if (!std.mem.eql(u8, call_name, "@import")) return null; if (!std.mem.eql(u8, call_name, "@import")) return null;
if (builtin_call.params.len > 1) return null; if (builtin_call.params_len > 1) return null;
const import_param = builtin_call.params.at(0).*; const import_param = builtin_call.paramsConst()[0];
if (import_param.id != .StringLiteral) return null; if (import_param.id != .StringLiteral) return null;
const import_str = analysis_ctx.tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token); const import_str = analysis_ctx.tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token);
@ -416,9 +391,9 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
fn maybeCollectImport(tree: *ast.Tree, builtin_call: *ast.Node.BuiltinCall, arr: *std.ArrayList([]const u8)) !void { fn maybeCollectImport(tree: *ast.Tree, builtin_call: *ast.Node.BuiltinCall, arr: *std.ArrayList([]const u8)) !void {
if (!std.mem.eql(u8, tree.tokenSlice(builtin_call.builtin_token), "@import")) return; if (!std.mem.eql(u8, tree.tokenSlice(builtin_call.builtin_token), "@import")) return;
if (builtin_call.params.len > 1) return; if (builtin_call.params_len > 1) return;
const import_param = builtin_call.params.at(0).*; const import_param = builtin_call.paramsConst()[0];
if (import_param.id != .StringLiteral) return; if (import_param.id != .StringLiteral) return;
const import_str = tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token); const import_str = tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token);
@ -429,8 +404,7 @@ fn maybeCollectImport(tree: *ast.Tree, builtin_call: *ast.Node.BuiltinCall, arr:
/// The import paths are valid as long as the tree is. /// The import paths are valid as long as the tree is.
pub fn collectImports(import_arr: *std.ArrayList([]const u8), tree: *ast.Tree) !void { pub fn collectImports(import_arr: *std.ArrayList([]const u8), tree: *ast.Tree) !void {
// TODO: Currently only detects `const smth = @import("string literal")<.SomeThing>;` // TODO: Currently only detects `const smth = @import("string literal")<.SomeThing>;`
var idx: usize = 0; for (tree.root_node.decls()) |decl| {
while (tree.root_node.iterate(idx)) |decl| : (idx += 1) {
if (decl.id != .VarDecl) continue; if (decl.id != .VarDecl) continue;
const var_decl = decl.cast(ast.Node.VarDecl).?; const var_decl = decl.cast(ast.Node.VarDecl).?;
if (var_decl.init_node == null) continue; if (var_decl.init_node == null) continue;
@ -468,7 +442,7 @@ pub fn getFieldAccessTypeNode(
switch (next.id) { switch (next.id) {
.Eof => return current_node, .Eof => return current_node,
.Identifier => { .Identifier => {
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, tokenizer.buffer[next.start..next.end])) |child| { if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, tokenizer.buffer[next.loc.start..next.loc.end])) |child| {
if (resolveTypeOfNode(analysis_ctx, child)) |node_type| { if (resolveTypeOfNode(analysis_ctx, child)) |node_type| {
current_node = node_type; current_node = node_type;
} else return null; } else return null;
@ -480,9 +454,9 @@ pub fn getFieldAccessTypeNode(
return current_node; return current_node;
} else if (after_period.id == .Identifier) { } else if (after_period.id == .Identifier) {
// TODO: This works for now, maybe we should filter based on the partial identifier ourselves? // TODO: This works for now, maybe we should filter based on the partial identifier ourselves?
if (after_period.end == line_length) return current_node; if (after_period.loc.end == line_length) return current_node;
if (getChild(analysis_ctx.tree, current_node, tokenizer.buffer[after_period.start..after_period.end])) |child| { if (getChild(analysis_ctx.tree, current_node, tokenizer.buffer[after_period.loc.start..after_period.loc.end])) |child| {
if (resolveTypeOfNode(analysis_ctx, child)) |child_type| { if (resolveTypeOfNode(analysis_ctx, child)) |child_type| {
current_node = child_type; current_node = child_type;
} else return null; } else return null;
@ -575,6 +549,7 @@ pub fn nodeToString(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
} }
pub fn declsFromIndexInternal( pub fn declsFromIndexInternal(
arena: *std.heap.ArenaAllocator,
decls: *std.ArrayList(*ast.Node), decls: *std.ArrayList(*ast.Node),
tree: *ast.Tree, tree: *ast.Tree,
node: *ast.Node, node: *ast.Node,
@ -584,8 +559,8 @@ pub fn declsFromIndexInternal(
switch (node.id) { switch (node.id) {
.Root, .ContainerDecl => { .Root, .ContainerDecl => {
container.* = node; container.* = node;
var node_index: usize = 0; var node_idx: usize = 0;
while (node.iterate(node_index)) |child_node| : (node_index += 1) { while (node.iterate(node_idx)) |child_node| : (node_idx += 1) {
// Skip over container fields, we can only dot access those. // Skip over container fields, we can only dot access those.
if (child_node.id == .ContainerField) continue; if (child_node.id == .ContainerField) continue;
@ -593,54 +568,78 @@ pub fn declsFromIndexInternal(
// If the cursor is in a variable decls it will insert itself anyway, we don't need to take care of it. // If the cursor is in a variable decls it will insert itself anyway, we don't need to take care of it.
if ((is_contained and child_node.id != .VarDecl) or !is_contained) try decls.append(child_node); if ((is_contained and child_node.id != .VarDecl) or !is_contained) try decls.append(child_node);
if (is_contained) { if (is_contained) {
try declsFromIndexInternal(decls, tree, child_node, container, source_index); try declsFromIndexInternal(arena, decls, tree, child_node, container, source_index);
} }
} }
}, },
.FnProto => { .FnProto => {
const func = node.cast(ast.Node.FnProto).?; const func = node.cast(ast.Node.FnProto).?;
var param_index: usize = 0; // TODO: This is a hack to enable param decls with the new parser
while (param_index < func.params.len) : (param_index += 1) for (func.paramsConst()) |param| {
try declsFromIndexInternal(decls, tree, func.params.at(param_index).*, container, source_index); if (param.name_token) |name_token| {
const var_decl_node = try arena.allocator.create(ast.Node.VarDecl);
var_decl_node.* = .{
.doc_comments = param.doc_comments,
.comptime_token = param.comptime_token,
.visib_token = null,
.thread_local_token = null,
.name_token = name_token,
.eq_token = null,
.mut_token = name_token, // TODO: better tokens for mut_token. semicolon_token?
.extern_export_token = null,
.lib_name = null,
.type_node = switch (param.param_type) {
.type_expr => |t| t,
else => null,
},
.align_node = null,
.section_node = null,
.init_node = null,
.semicolon_token = name_token,
};
try decls.append(&var_decl_node.base);
}
}
if (func.body_node) |body_node| { if (func.body_node) |body_node| {
if (!nodeContainsSourceIndex(tree, body_node, source_index)) return; if (!nodeContainsSourceIndex(tree, body_node, source_index)) return;
try declsFromIndexInternal(decls, tree, body_node, container, source_index); try declsFromIndexInternal(arena, decls, tree, body_node, container, source_index);
} }
}, },
.TestDecl => { .TestDecl => {
const test_decl = node.cast(ast.Node.TestDecl).?; const test_decl = node.cast(ast.Node.TestDecl).?;
if (!nodeContainsSourceIndex(tree, test_decl.body_node, source_index)) return; if (!nodeContainsSourceIndex(tree, test_decl.body_node, source_index)) return;
try declsFromIndexInternal(decls, tree, test_decl.body_node, container, source_index); try declsFromIndexInternal(arena, decls, tree, test_decl.body_node, container, source_index);
}, },
.Block => { .Block => {
var index: usize = 0; var inode_idx: usize = 0;
while (node.iterate(index)) |inode| : (index += 1) { while (node.iterate(inode_idx)) |inode| : (inode_idx += 1) {
if (nodeComesAfterSourceIndex(tree, inode, source_index)) return; if (nodeComesAfterSourceIndex(tree, inode, source_index)) return;
try declsFromIndexInternal(decls, tree, inode, container, source_index); try declsFromIndexInternal(arena, decls, tree, inode, container, source_index);
} }
}, },
.Comptime => { .Comptime => {
const comptime_stmt = node.cast(ast.Node.Comptime).?; const comptime_stmt = node.cast(ast.Node.Comptime).?;
if (nodeComesAfterSourceIndex(tree, comptime_stmt.expr, source_index)) return; if (nodeComesAfterSourceIndex(tree, comptime_stmt.expr, source_index)) return;
try declsFromIndexInternal(decls, tree, comptime_stmt.expr, container, source_index); try declsFromIndexInternal(arena, decls, tree, comptime_stmt.expr, container, source_index);
}, },
.If => { .If => {
const if_node = node.cast(ast.Node.If).?; const if_node = node.cast(ast.Node.If).?;
if (nodeContainsSourceIndex(tree, if_node.body, source_index)) { if (nodeContainsSourceIndex(tree, if_node.body, source_index)) {
if (if_node.payload) |payload| { if (if_node.payload) |payload| {
try declsFromIndexInternal(decls, tree, payload, container, source_index); try declsFromIndexInternal(arena, decls, tree, payload, container, source_index);
} }
return try declsFromIndexInternal(decls, tree, if_node.body, container, source_index); return try declsFromIndexInternal(arena, decls, tree, if_node.body, container, source_index);
} }
if (if_node.@"else") |else_node| { if (if_node.@"else") |else_node| {
if (nodeContainsSourceIndex(tree, else_node.body, source_index)) { if (nodeContainsSourceIndex(tree, else_node.body, source_index)) {
if (else_node.payload) |payload| { if (else_node.payload) |payload| {
try declsFromIndexInternal(decls, tree, payload, container, source_index); try declsFromIndexInternal(arena, decls, tree, payload, container, source_index);
} }
return try declsFromIndexInternal(decls, tree, else_node.body, container, source_index); return try declsFromIndexInternal(arena, decls, tree, else_node.body, container, source_index);
} }
} }
}, },
@ -648,46 +647,45 @@ pub fn declsFromIndexInternal(
const while_node = node.cast(ast.Node.While).?; const while_node = node.cast(ast.Node.While).?;
if (nodeContainsSourceIndex(tree, while_node.body, source_index)) { if (nodeContainsSourceIndex(tree, while_node.body, source_index)) {
if (while_node.payload) |payload| { if (while_node.payload) |payload| {
try declsFromIndexInternal(decls, tree, payload, container, source_index); try declsFromIndexInternal(arena, decls, tree, payload, container, source_index);
} }
return try declsFromIndexInternal(decls, tree, while_node.body, container, source_index); return try declsFromIndexInternal(arena, decls, tree, while_node.body, container, source_index);
} }
if (while_node.@"else") |else_node| { if (while_node.@"else") |else_node| {
if (nodeContainsSourceIndex(tree, else_node.body, source_index)) { if (nodeContainsSourceIndex(tree, else_node.body, source_index)) {
if (else_node.payload) |payload| { if (else_node.payload) |payload| {
try declsFromIndexInternal(decls, tree, payload, container, source_index); try declsFromIndexInternal(arena, decls, tree, payload, container, source_index);
} }
return try declsFromIndexInternal(decls, tree, else_node.body, container, source_index); return try declsFromIndexInternal(arena, decls, tree, else_node.body, container, source_index);
} }
} }
}, },
.For => { .For => {
const for_node = node.cast(ast.Node.For).?; const for_node = node.cast(ast.Node.For).?;
if (nodeContainsSourceIndex(tree, for_node.body, source_index)) { if (nodeContainsSourceIndex(tree, for_node.body, source_index)) {
try declsFromIndexInternal(decls, tree, for_node.payload, container, source_index); try declsFromIndexInternal(arena, decls, tree, for_node.payload, container, source_index);
return try declsFromIndexInternal(decls, tree, for_node.body, container, source_index); return try declsFromIndexInternal(arena, decls, tree, for_node.body, container, source_index);
} }
if (for_node.@"else") |else_node| { if (for_node.@"else") |else_node| {
if (nodeContainsSourceIndex(tree, else_node.body, source_index)) { if (nodeContainsSourceIndex(tree, else_node.body, source_index)) {
if (else_node.payload) |payload| { if (else_node.payload) |payload| {
try declsFromIndexInternal(decls, tree, payload, container, source_index); try declsFromIndexInternal(arena, decls, tree, payload, container, source_index);
} }
return try declsFromIndexInternal(decls, tree, else_node.body, container, source_index); return try declsFromIndexInternal(arena, decls, tree, else_node.body, container, source_index);
} }
} }
}, },
.Switch => { .Switch => {
const switch_node = node.cast(ast.Node.Switch).?; const switch_node = node.cast(ast.Node.Switch).?;
var case_it = switch_node.cases.iterator(0); for (switch_node.casesConst()) |case| {
while (case_it.next()) |case| {
const case_node = case.*.cast(ast.Node.SwitchCase).?; const case_node = case.*.cast(ast.Node.SwitchCase).?;
if (nodeContainsSourceIndex(tree, case_node.expr, source_index)) { if (nodeContainsSourceIndex(tree, case_node.expr, source_index)) {
if (case_node.payload) |payload| { if (case_node.payload) |payload| {
try declsFromIndexInternal(decls, tree, payload, container, source_index); try declsFromIndexInternal(arena, decls, tree, payload, container, source_index);
} }
return try declsFromIndexInternal(decls, tree, case_node.expr, container, source_index); return try declsFromIndexInternal(arena, decls, tree, case_node.expr, container, source_index);
} }
} }
}, },
@ -705,61 +703,60 @@ pub fn declsFromIndexInternal(
try decls.append(node); try decls.append(node);
if (node.cast(ast.Node.VarDecl).?.init_node) |child| { if (node.cast(ast.Node.VarDecl).?.init_node) |child| {
if (nodeContainsSourceIndex(tree, child, source_index)) { if (nodeContainsSourceIndex(tree, child, source_index)) {
try declsFromIndexInternal(decls, tree, child, container, source_index); try declsFromIndexInternal(arena, decls, tree, child, container, source_index);
} }
} }
}, },
.ParamDecl => try decls.append(node),
else => {}, else => {},
} }
} }
pub fn addChildrenNodes(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node) !void { pub fn addChildrenNodes(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node) !void {
var index: usize = 0; var node_idx: usize = 0;
while (node.iterate(index)) |child_node| : (index += 1) { while (node.iterate(node_idx)) |child_node| : (node_idx += 1) {
try decls.append(child_node); try decls.append(child_node);
} }
} }
pub fn declsFromIndex(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, source_index: usize) !*ast.Node { pub fn declsFromIndex(arena: *std.heap.ArenaAllocator, decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, source_index: usize) !*ast.Node {
var result = &tree.root_node.base; var result = &tree.root_node.base;
try declsFromIndexInternal(decls, tree, &tree.root_node.base, &result, source_index); try declsFromIndexInternal(arena, decls, tree, &tree.root_node.base, &result, source_index);
return result; return result;
} }
fn nodeContainsSourceIndex(tree: *ast.Tree, node: *ast.Node, source_index: usize) bool { fn nodeContainsSourceIndex(tree: *ast.Tree, node: *ast.Node, source_index: usize) bool {
const first_token = tree.tokens.at(node.firstToken()); const first_token = tree.token_locs[node.firstToken()];
const last_token = tree.tokens.at(node.lastToken()); const last_token = tree.token_locs[node.lastToken()];
return source_index >= first_token.start and source_index <= last_token.end; return source_index >= first_token.start and source_index <= last_token.end;
} }
fn nodeComesAfterSourceIndex(tree: *ast.Tree, node: *ast.Node, source_index: usize) bool { fn nodeComesAfterSourceIndex(tree: *ast.Tree, node: *ast.Node, source_index: usize) bool {
const first_token = tree.tokens.at(node.firstToken()); const first_token = tree.token_locs[node.firstToken()];
const last_token = tree.tokens.at(node.lastToken()); const last_token = tree.token_locs[node.lastToken()];
return source_index < first_token.start; return source_index < first_token.start;
} }
pub fn getImportStr(tree: *ast.Tree, source_index: usize) ?[]const u8 { pub fn getImportStr(tree: *ast.Tree, source_index: usize) ?[]const u8 {
var node = &tree.root_node.base; var node = &tree.root_node.base;
var index: usize = 0;
while (node.iterate(index)) |child| { var child_idx: usize = 0;
while (node.iterate(child_idx)) |child| : (child_idx += 1) {
if (!nodeContainsSourceIndex(tree, child, source_index)) { if (!nodeContainsSourceIndex(tree, child, source_index)) {
index += 1;
continue; continue;
} }
if (child.cast(ast.Node.BuiltinCall)) |builtin_call| blk: { if (child.cast(ast.Node.BuiltinCall)) |builtin_call| blk: {
const call_name = tree.tokenSlice(builtin_call.builtin_token); const call_name = tree.tokenSlice(builtin_call.builtin_token);
if (!std.mem.eql(u8, call_name, "@import")) break :blk; if (!std.mem.eql(u8, call_name, "@import")) break :blk;
if (builtin_call.params.len != 1) break :blk; if (builtin_call.params_len != 1) break :blk;
const import_param = builtin_call.params.at(0).*; const import_param = builtin_call.paramsConst()[0];
const import_str_node = import_param.cast(ast.Node.StringLiteral) orelse break :blk; const import_str_node = import_param.cast(ast.Node.StringLiteral) orelse break :blk;
const import_str = tree.tokenSlice(import_str_node.token); const import_str = tree.tokenSlice(import_str_node.token);
return import_str[1 .. import_str.len - 1]; return import_str[1 .. import_str.len - 1];
} }
node = child; node = child;
index = 0; child_idx = 0;
} }
return null; return null;
} }

View File

@ -402,7 +402,7 @@ pub fn analysisContext(
const tree = try handle.tree(self.allocator); const tree = try handle.tree(self.allocator);
var scope_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator); var scope_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator);
const in_container = try analysis.declsFromIndex(&scope_nodes, tree, position); const in_container = try analysis.declsFromIndex(arena, &scope_nodes, tree, position);
const std_uri = try stdUriFromLibPath(&arena.allocator, zig_lib_path); const std_uri = try stdUriFromLibPath(&arena.allocator, zig_lib_path);
return AnalysisContext{ return AnalysisContext{

View File

@ -100,8 +100,7 @@ fn publishDiagnostics(handle: DocumentStore.Handle, config: Config) !void {
var diagnostics = std.ArrayList(types.Diagnostic).init(&arena.allocator); var diagnostics = std.ArrayList(types.Diagnostic).init(&arena.allocator);
var error_it = tree.errors.iterator(0); for (tree.errors) |*err| {
while (error_it.next()) |err| {
const loc = tree.tokenLocation(0, err.loc()); const loc = tree.tokenLocation(0, err.loc());
var mem_buffer: [256]u8 = undefined; var mem_buffer: [256]u8 = undefined;
@ -119,9 +118,7 @@ fn publishDiagnostics(handle: DocumentStore.Handle, config: Config) !void {
} }
if (tree.errors.len == 0) { if (tree.errors.len == 0) {
var decls = tree.root_node.decls.iterator(0); for (tree.root_node.decls()) |decl| {
while (decls.next()) |decl_ptr| {
var decl = decl_ptr.*;
switch (decl.id) { switch (decl.id) {
.FnProto => blk: { .FnProto => blk: {
const func = decl.cast(std.zig.ast.Node.FnProto).?; const func = decl.cast(std.zig.ast.Node.FnProto).?;
@ -179,8 +176,8 @@ fn containerToCompletion(
container: *std.zig.ast.Node, container: *std.zig.ast.Node,
config: Config, config: Config,
) !void { ) !void {
var index: usize = 0; var child_idx: usize = 0;
while (container.iterate(index)) |child_node| : (index += 1) { while (container.iterate(child_idx)) |child_node| : (child_idx += 1) {
// Declarations in the same file do not need to be public. // Declarations in the same file do not need to be public.
if (orig_handle == analysis_ctx.handle or analysis.isNodePublic(analysis_ctx.tree, child_node)) { if (orig_handle == analysis_ctx.handle or analysis.isNodePublic(analysis_ctx.tree, child_node)) {
try nodeToCompletion(list, analysis_ctx, orig_handle, child_node, config); try nodeToCompletion(list, analysis_ctx, orig_handle, child_node, config);
@ -229,7 +226,7 @@ fn nodeToCompletion(
}, },
.VarDecl => { .VarDecl => {
const var_decl = node.cast(std.zig.ast.Node.VarDecl).?; const var_decl = node.cast(std.zig.ast.Node.VarDecl).?;
const is_const = analysis_ctx.tree.tokens.at(var_decl.mut_token).id == .Keyword_const; const is_const = analysis_ctx.tree.token_ids[var_decl.mut_token] == .Keyword_const;
var child_analysis_context = try analysis_ctx.clone(); var child_analysis_context = try analysis_ctx.clone();
defer child_analysis_context.deinit(); defer child_analysis_context.deinit();
@ -259,16 +256,6 @@ fn nodeToCompletion(
.detail = analysis.getVariableSignature(analysis_ctx.tree, var_decl), .detail = analysis.getVariableSignature(analysis_ctx.tree, var_decl),
}); });
}, },
.ParamDecl => {
const param = node.cast(std.zig.ast.Node.ParamDecl).?;
if (param.name_token) |name_token|
try list.append(.{
.label = analysis_ctx.tree.tokenSlice(name_token),
.kind = .Constant,
.documentation = doc,
.detail = analysis.getParamSignature(analysis_ctx.tree, param),
});
},
.PrefixOp => { .PrefixOp => {
try list.append(.{ try list.append(.{
.label = "len", .label = "len",
@ -325,7 +312,7 @@ fn gotoDefinitionGlobal(id: i64, pos_index: usize, handle: DocumentStore.Handle)
defer arena.deinit(); defer arena.deinit();
var decl_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator); var decl_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator);
_ = try analysis.declsFromIndex(&decl_nodes, tree, pos_index); _ = try analysis.declsFromIndex(&arena, &decl_nodes, tree, pos_index);
const decl = analysis.getChildOfSlice(tree, decl_nodes.items, name) orelse return try respondGeneric(id, null_result_response); const decl = analysis.getChildOfSlice(tree, decl_nodes.items, name) orelse return try respondGeneric(id, null_result_response);
const name_token = analysis.getDeclNameToken(tree, decl) orelse unreachable; const name_token = analysis.getDeclNameToken(tree, decl) orelse unreachable;