Now compiles on the new parser branch

This commit is contained in:
Alexandros Naskos 2020-05-24 01:21:02 +03:00
parent 30cb9e06e4
commit 11b5da3a91
2 changed files with 113 additions and 110 deletions

View File

@ -51,24 +51,31 @@ pub fn getDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, node: *ast
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);
}
},
// @TODO: ParamDecl is no longer a node.
// .ParamDecl => {
// const param = node.cast(ast.Node.ParamDecl).?;
// if (param.doc_comments) |doc_comments| {
// return try collectDocComments(allocator, tree, doc_comments);
// }
// },
else => {},
}
return null;
}
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);
defer lines.deinit();
while (doc_it.next()) |doc_comment| {
_ = try lines.append(std.fmt.trim(tree.tokenSlice(doc_comment.*)[3..]));
var curr_line_tok = doc_comments.first_line;
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);
@ -76,11 +83,11 @@ fn collectDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, doc_commen
/// Gets a function signature (keywords, name, return value)
pub fn getFunctionSignature(tree: *ast.Tree, func: *ast.Node.FnProto) []const u8 {
const start = tree.tokens.at(func.firstToken()).start;
const end = tree.tokens.at(switch (func.return_type) {
const start = tree.token_locs[func.firstToken()].start;
const end = tree.token_locs[switch (func.return_type) {
.Explicit, .InferErrorSet => |node| node.lastToken(),
.Invalid => |r_paren| r_paren,
}).end;
}].end;
return tree.source[start..end];
}
@ -96,38 +103,33 @@ pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func:
var buf_stream = buffer.outStream();
var param_num = @as(usize, 1);
var param_it = func.params.iterator(0);
while (param_it.next()) |param_ptr| : (param_num += 1) {
const param = param_ptr.*;
const param_decl = param.cast(ast.Node.ParamDecl).?;
for (func.paramsConst()) |param, param_num| {
if (param_num != 0) try buffer.appendSlice(", ${") else try buffer.appendSlice("${");
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_decl.comptime_token) |_| {
if (param.comptime_token) |_| {
try buffer.appendSlice("comptime ");
}
if (param_decl.noalias_token) |_| {
if (param.noalias_token) |_| {
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(": ");
}
switch (param_decl.param_type) {
switch (param.param_type) {
.var_args => try buffer.appendSlice("..."),
.var_type => try buffer.appendSlice("var"),
.type_expr => |type_expr| {
var curr_tok = type_expr.firstToken();
var end_tok = type_expr.lastToken();
while (curr_tok <= end_tok) : (curr_tok += 1) {
const id = tree.tokens.at(curr_tok).id;
const is_comma = tree.tokens.at(curr_tok).id == .Comma;
const id = tree.token_ids[curr_tok];
const is_comma = id == .Comma;
if (curr_tok == end_tok and is_comma) continue;
@ -146,8 +148,8 @@ pub fn getFunctionSnippet(allocator: *std.mem.Allocator, tree: *ast.Tree, func:
/// Gets a function signature (keywords, name, return value)
pub fn getVariableSignature(tree: *ast.Tree, var_decl: *ast.Node.VarDecl) []const u8 {
const start = tree.tokens.at(var_decl.firstToken()).start;
const end = tree.tokens.at(var_decl.semicolon_token).start;
const start = tree.token_locs[var_decl.firstToken()].start;
const end = tree.token_locs[var_decl.semicolon_token].start;
return tree.source[start..end];
}
@ -186,11 +188,12 @@ pub fn getDeclNameToken(tree: *ast.Tree, node: *ast.Node) ?ast.TokenIndex {
const vari = node.cast(ast.Node.VarDecl).?;
return vari.name_token;
},
.ParamDecl => {
const decl = node.cast(ast.Node.ParamDecl).?;
if (decl.name_token == null) return null;
return decl.name_token.?;
},
// @TODO Param decl is no longer a node
// .ParamDecl => {
// const decl = node.cast(ast.Node.ParamDecl).?;
// if (decl.name_token == null) return null;
// return decl.name_token.?;
// },
.FnProto => {
const func = node.cast(ast.Node.FnProto).?;
if (func.name_token == null) return null;
@ -217,8 +220,8 @@ fn getDeclName(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
/// Gets the child of node
pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node {
var index: usize = 0;
while (node.iterate(index)) |child| : (index += 1) {
var child_it = node.iterate();
while (child_it.next()) |child| {
const child_name = getDeclName(tree, child) orelse continue;
if (std.mem.eql(u8, child_name, name)) return child;
}
@ -236,8 +239,8 @@ pub fn getChildOfSlice(tree: *ast.Tree, nodes: []*ast.Node, name: []const u8) ?*
fn findReturnStatementInternal(base_node: *ast.Node, already_found: *bool) ?*ast.Node.ControlFlowExpression {
var result: ?*ast.Node.ControlFlowExpression = null;
var idx: usize = 0;
while (base_node.iterate(idx)) |child_node| : (idx += 1) {
var child_it = base_node.iterate();
while (child_it.next()) |child_node| {
switch (child_node.id) {
.ControlFlowExpression => {
const cfe = child_node.cast(ast.Node.ControlFlowExpression).?;
@ -293,15 +296,16 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
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 => {},
}
},
// @TODO Param decl is no longer a node type.
// .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 => {
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, analysis_ctx.tree.getNodeSource(node))) |child| {
return resolveTypeOfNode(analysis_ctx, child);
@ -311,18 +315,21 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
const field = node.cast(ast.Node.ContainerField).?;
return resolveTypeOfNode(analysis_ctx, field.type_expr orelse return null);
},
.SuffixOp => {
const suffix_op = node.cast(ast.Node.SuffixOp).?;
switch (suffix_op.op) {
.Call, .StructInitializer => {
const decl = resolveTypeOfNode(analysis_ctx, suffix_op.lhs.node) orelse return null;
return switch (decl.id) {
.FnProto => resolveReturnType(analysis_ctx, decl.cast(ast.Node.FnProto).?),
else => decl,
};
},
else => {},
}
.Call => {
const call = node.cast(ast.Node.Call).?;
const decl = resolveTypeOfNode(analysis_ctx, call.lhs) orelse return null;
return switch (decl.id) {
.FnProto => resolveReturnType(analysis_ctx, decl.cast(ast.Node.FnProto).?),
else => decl,
};
},
.StructInitializer => {
const struct_init = node.cast(ast.Node.StructInitializer).?;
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 => {
const infix_op = node.cast(ast.Node.InfixOp).?;
@ -344,8 +351,8 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
switch (prefix_op.op) {
.SliceType, .ArrayType => return node,
.PtrType => {
const op_token = analysis_ctx.tree.tokens.at(prefix_op.op_token);
switch (op_token.id) {
const op_token_id = analysis_ctx.tree.token_ids[prefix_op.op_token];
switch (op_token_id) {
.Asterisk => return resolveTypeOfNode(analysis_ctx, prefix_op.rhs),
.LBracket, .AsteriskAsterisk => return null,
else => unreachable,
@ -369,14 +376,14 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
const builtin_call = node.cast(ast.Node.BuiltinCall).?;
const call_name = analysis_ctx.tree.tokenSlice(builtin_call.builtin_token);
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.last_this_node;
}
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;
const import_str = analysis_ctx.tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token);
@ -397,9 +404,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 {
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;
const import_str = tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token);
@ -410,8 +417,7 @@ fn maybeCollectImport(tree: *ast.Tree, builtin_call: *ast.Node.BuiltinCall, arr:
/// The import paths are valid as long as the tree is.
pub fn collectImports(import_arr: *std.ArrayList([]const u8), tree: *ast.Tree) !void {
// TODO: Currently only detects `const smth = @import("string literal")<.SomeThing>;`
var idx: usize = 0;
while (tree.root_node.iterate(idx)) |decl| : (idx += 1) {
for (tree.root_node.decls()) |decl| {
if (decl.id != .VarDecl) continue;
const var_decl = decl.cast(ast.Node.VarDecl).?;
if (var_decl.init_node == null) continue;
@ -448,7 +454,7 @@ pub fn getFieldAccessTypeNode(
switch (next.id) {
.Eof => return current_node,
.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| {
current_node = node_type;
} else return null;
@ -460,9 +466,9 @@ pub fn getFieldAccessTypeNode(
return current_node;
} else if (after_period.id == .Identifier) {
// 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| {
current_node = child_type;
} else return null;
@ -521,8 +527,8 @@ pub fn nodeToString(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node, source_index: usize) error{OutOfMemory}!void {
switch (node.id) {
.Root, .ContainerDecl => {
var node_index: usize = 0;
while (node.iterate(node_index)) |child_node| : (node_index += 1) {
var node_it = node.iterate();
while (node_it.next()) |child_node| {
// Skip over container fields, we can only dot access those.
if (child_node.id == .ContainerField) continue;
@ -537,9 +543,10 @@ pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree,
.FnProto => {
const func = node.cast(ast.Node.FnProto).?;
var param_index: usize = 0;
while (param_index < func.params.len) : (param_index += 1)
try declsFromIndexInternal(decls, tree, func.params.at(param_index).*, source_index);
for (func.paramsConst()) |param| {
// try decls.append(node)
// @TODO We need to store something else than nodes from now on.
}
if (func.body_node) |body_node| {
if (!nodeContainsSourceIndex(tree, body_node, source_index)) return;
@ -552,8 +559,8 @@ pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree,
try declsFromIndexInternal(decls, tree, test_decl.body_node, source_index);
},
.Block => {
var index: usize = 0;
while (node.iterate(index)) |inode| : (index += 1) {
var node_it = node.iterate();
while (node_it.next()) |inode| {
if (nodeComesAfterSourceIndex(tree, inode, source_index)) return;
try declsFromIndexInternal(decls, tree, inode, source_index);
}
@ -617,8 +624,7 @@ pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree,
},
.Switch => {
const switch_node = node.cast(ast.Node.Switch).?;
var case_it = switch_node.cases.iterator(0);
while (case_it.next()) |case| {
for (switch_node.casesConst()) |case| {
const case_node = case.*.cast(ast.Node.SwitchCase).?;
if (nodeContainsSourceIndex(tree, case_node.expr, source_index)) {
if (case_node.payload) |payload| {
@ -646,14 +652,13 @@ pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree,
}
}
},
.ParamDecl => try decls.append(node),
else => {},
}
}
pub fn addChildrenNodes(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node) !void {
var index: usize = 0;
while (node.iterate(index)) |child_node| : (index += 1) {
var node_it = node.iterate();
while (node_it.next()) |child_node| {
try decls.append(child_node);
}
}
@ -664,38 +669,38 @@ pub fn declsFromIndex(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, source_
}
fn nodeContainsSourceIndex(tree: *ast.Tree, node: *ast.Node, source_index: usize) bool {
const first_token = tree.tokens.at(node.firstToken());
const last_token = tree.tokens.at(node.lastToken());
const first_token = tree.token_locs[node.firstToken()];
const last_token = tree.token_locs[node.lastToken()];
return source_index >= first_token.start and source_index <= last_token.end;
}
fn nodeComesAfterSourceIndex(tree: *ast.Tree, node: *ast.Node, source_index: usize) bool {
const first_token = tree.tokens.at(node.firstToken());
const last_token = tree.tokens.at(node.lastToken());
const first_token = tree.token_locs[node.firstToken()];
const last_token = tree.token_locs[node.lastToken()];
return source_index < first_token.start;
}
pub fn getImportStr(tree: *ast.Tree, source_index: usize) ?[]const u8 {
var node = &tree.root_node.base;
var index: usize = 0;
while (node.iterate(index)) |child| {
var child_it = node.iterate();
while (child_it.next()) |child| {
if (!nodeContainsSourceIndex(tree, child, source_index)) {
index += 1;
continue;
}
if (child.cast(ast.Node.BuiltinCall)) |builtin_call| blk: {
const call_name = tree.tokenSlice(builtin_call.builtin_token);
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 = tree.tokenSlice(import_str_node.token);
return import_str[1 .. import_str.len - 1];
}
node = child;
index = 0;
child_it = node.iterate();
}
return null;
}

View File

@ -100,8 +100,7 @@ fn publishDiagnostics(handle: DocumentStore.Handle, config: Config) !void {
var diagnostics = std.ArrayList(types.Diagnostic).init(&arena.allocator);
var error_it = tree.errors.iterator(0);
while (error_it.next()) |err| {
for (tree.errors) |*err| {
const loc = tree.tokenLocation(0, err.loc());
var mem_buffer: [256]u8 = undefined;
@ -119,9 +118,7 @@ fn publishDiagnostics(handle: DocumentStore.Handle, config: Config) !void {
}
if (tree.errors.len == 0) {
var decls = tree.root_node.decls.iterator(0);
while (decls.next()) |decl_ptr| {
var decl = decl_ptr.*;
for (tree.root_node.decls()) |decl| {
switch (decl.id) {
.FnProto => blk: {
const func = decl.cast(std.zig.ast.Node.FnProto).?;
@ -179,8 +176,8 @@ fn containerToCompletion(
container: *std.zig.ast.Node,
config: Config,
) !void {
var index: usize = 0;
while (container.iterate(index)) |child_node| : (index += 1) {
var child_it = container.iterate();
while (child_it.next()) |child_node| {
// 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)) {
try nodeToCompletion(list, analysis_ctx, orig_handle, child_node, config);
@ -229,7 +226,7 @@ fn nodeToCompletion(
},
.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();
defer child_analysis_context.deinit();
@ -259,16 +256,17 @@ fn nodeToCompletion(
.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),
});
},
// @TODO: No more ParamDecl node.
// .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 => {
try list.append(.{
.label = "len",