Preparation for imports, abstracted document hashmap into a document storage type
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
const std = @import("std");
|
||||
|
||||
const types = @import("types.zig");
|
||||
const ast = std.zig.ast;
|
||||
|
||||
/// REALLY BAD CODE, PLEASE DON'T USE THIS!!!!!!! (only for testing)
|
||||
@@ -154,20 +154,20 @@ pub fn isPascalCase(name: []const u8) bool {
|
||||
// ANALYSIS ENGINE
|
||||
|
||||
/// Gets the child of node
|
||||
pub fn getChild(tree: *std.zig.ast.Tree, node: *std.zig.ast.Node, name: []const u8) ?*std.zig.ast.Node {
|
||||
pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node {
|
||||
var index: usize = 0;
|
||||
while (node.iterate(index)) |child| {
|
||||
switch (child.id) {
|
||||
.VarDecl => {
|
||||
const vari = child.cast(std.zig.ast.Node.VarDecl).?;
|
||||
const vari = child.cast(ast.Node.VarDecl).?;
|
||||
if (std.mem.eql(u8, tree.tokenSlice(vari.name_token), name)) return child;
|
||||
},
|
||||
.FnProto => {
|
||||
const func = child.cast(std.zig.ast.Node.FnProto).?;
|
||||
const func = child.cast(ast.Node.FnProto).?;
|
||||
if (func.name_token != null and std.mem.eql(u8, tree.tokenSlice(func.name_token.?), name)) return child;
|
||||
},
|
||||
.ContainerField => {
|
||||
const field = child.cast(std.zig.ast.Node.ContainerField).?;
|
||||
const field = child.cast(ast.Node.ContainerField).?;
|
||||
if (std.mem.eql(u8, tree.tokenSlice(field.name_token), name)) return child;
|
||||
},
|
||||
else => {}
|
||||
@@ -178,44 +178,44 @@ pub fn getChild(tree: *std.zig.ast.Tree, node: *std.zig.ast.Node, name: []const
|
||||
}
|
||||
|
||||
/// Resolves the type of a node
|
||||
pub fn resolveTypeOfNode(tree: *std.zig.ast.Tree, node: *std.zig.ast.Node) ?*std.zig.ast.Node {
|
||||
pub fn resolveTypeOfNode(tree: *ast.Tree, node: *ast.Node, import_ctx: *types.ImportCtx) ?*ast.Node {
|
||||
switch (node.id) {
|
||||
.VarDecl => {
|
||||
const vari = node.cast(std.zig.ast.Node.VarDecl).?;
|
||||
return resolveTypeOfNode(tree, vari.type_node orelse vari.init_node.?) orelse null;
|
||||
const vari = node.cast(ast.Node.VarDecl).?;
|
||||
return resolveTypeOfNode(tree, vari.type_node orelse vari.init_node.?, import_ctx) orelse null;
|
||||
},
|
||||
.FnProto => {
|
||||
const func = node.cast(std.zig.ast.Node.FnProto).?;
|
||||
const func = node.cast(ast.Node.FnProto).?;
|
||||
switch (func.return_type) {
|
||||
.Explicit, .InferErrorSet => |return_type| {return resolveTypeOfNode(tree, return_type);}
|
||||
.Explicit, .InferErrorSet => |return_type| {return resolveTypeOfNode(tree, return_type, import_ctx);}
|
||||
}
|
||||
},
|
||||
.Identifier => {
|
||||
if (getChild(tree, &tree.root_node.base, tree.getNodeSource(node))) |child| {
|
||||
return resolveTypeOfNode(tree, child);
|
||||
return resolveTypeOfNode(tree, child, import_ctx);
|
||||
} else return null;
|
||||
},
|
||||
.ContainerDecl => {
|
||||
return node;
|
||||
},
|
||||
.ContainerField => {
|
||||
const field = node.cast(std.zig.ast.Node.ContainerField).?;
|
||||
return resolveTypeOfNode(tree, field.type_expr.?);
|
||||
const field = node.cast(ast.Node.ContainerField).?;
|
||||
return resolveTypeOfNode(tree, field.type_expr.?, import_ctx);
|
||||
},
|
||||
.SuffixOp => {
|
||||
const suffix_op = node.cast(std.zig.ast.Node.SuffixOp).?;
|
||||
const suffix_op = node.cast(ast.Node.SuffixOp).?;
|
||||
switch (suffix_op.op) {
|
||||
.Call => {
|
||||
return resolveTypeOfNode(tree, suffix_op.lhs.node);
|
||||
return resolveTypeOfNode(tree, suffix_op.lhs.node, import_ctx);
|
||||
},
|
||||
else => {}
|
||||
}
|
||||
},
|
||||
.InfixOp => {
|
||||
const infix_op = node.cast(std.zig.ast.Node.InfixOp).?;
|
||||
const infix_op = node.cast(ast.Node.InfixOp).?;
|
||||
switch (infix_op.op) {
|
||||
.Period => {
|
||||
var left = resolveTypeOfNode(tree, infix_op.lhs).?;
|
||||
var left = resolveTypeOfNode(tree, infix_op.lhs, import_ctx) orelse return null;
|
||||
if (nodeToString(tree, infix_op.rhs)) |string| {
|
||||
return getChild(tree, left, string);
|
||||
} else return null;
|
||||
@@ -224,14 +224,27 @@ pub fn resolveTypeOfNode(tree: *std.zig.ast.Tree, node: *std.zig.ast.Node) ?*std
|
||||
}
|
||||
},
|
||||
.PrefixOp => {
|
||||
const prefix_op = node.cast(std.zig.ast.Node.PrefixOp).?;
|
||||
const prefix_op = node.cast(ast.Node.PrefixOp).?;
|
||||
switch (prefix_op.op) {
|
||||
.PtrType => {
|
||||
return resolveTypeOfNode(tree, prefix_op.rhs);
|
||||
return resolveTypeOfNode(tree, prefix_op.rhs, import_ctx);
|
||||
},
|
||||
else => {}
|
||||
}
|
||||
},
|
||||
.BuiltinCall => {
|
||||
const builtin_call = node.cast(ast.Node.BuiltinCall).?;
|
||||
if (!std.mem.eql(u8, tree.tokenSlice(builtin_call.builtin_token), "@import")) return null;
|
||||
if (builtin_call.params.len > 1) return null;
|
||||
|
||||
const import_param = builtin_call.params.at(0).*;
|
||||
if (import_param.id != .StringLiteral) return null;
|
||||
|
||||
var import_str = tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token);
|
||||
import_str = import_str[1 .. import_str.len - 1];
|
||||
|
||||
return resolveImport(import_str);
|
||||
},
|
||||
else => {
|
||||
std.debug.warn("Type resolution case not implemented; {}\n", .{node.id});
|
||||
}
|
||||
@@ -239,8 +252,14 @@ pub fn resolveTypeOfNode(tree: *std.zig.ast.Tree, node: *std.zig.ast.Node) ?*std
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn getNodeFromTokens(tree: *std.zig.ast.Tree, node: *std.zig.ast.Node, tokenizer: *std.zig.Tokenizer) ?*std.zig.ast.Node {
|
||||
var current_node = node;
|
||||
fn resolveImport(import: []const u8) ?*ast.Node {
|
||||
// @TODO: Write this
|
||||
std.debug.warn("Resolving import {}\n", .{import});
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn getFieldAccessTypeNode(tree: *ast.Tree, tokenizer: *std.zig.Tokenizer, import_ctx: *types.ImportCtx) ?*ast.Node {
|
||||
var current_node = &tree.root_node.base;
|
||||
|
||||
while (true) {
|
||||
var next = tokenizer.next();
|
||||
@@ -249,13 +268,11 @@ pub fn getNodeFromTokens(tree: *std.zig.ast.Tree, node: *std.zig.ast.Node, token
|
||||
return current_node;
|
||||
},
|
||||
.Identifier => {
|
||||
// var root = current_node.cast(std.zig.ast.Node.Root).?;
|
||||
// var root = current_node.cast(ast.Node.Root).?;
|
||||
// current_node.
|
||||
if (getChild(tree, current_node, tokenizer.buffer[next.start..next.end])) |child| {
|
||||
if (resolveTypeOfNode(tree, child)) |node_type| {
|
||||
if (resolveTypeOfNode(tree, child)) |child_type| {
|
||||
current_node = child_type;
|
||||
} else return null;
|
||||
if (resolveTypeOfNode(tree, child, import_ctx)) |node_type| {
|
||||
current_node = node_type;
|
||||
} else return null;
|
||||
} else return null;
|
||||
},
|
||||
@@ -265,7 +282,7 @@ pub fn getNodeFromTokens(tree: *std.zig.ast.Tree, node: *std.zig.ast.Node, token
|
||||
return current_node;
|
||||
} else if (after_period.id == .Identifier) {
|
||||
if (getChild(tree, current_node, tokenizer.buffer[after_period.start..after_period.end])) |child| {
|
||||
if (resolveTypeOfNode(tree, child)) |child_type| {
|
||||
if (resolveTypeOfNode(tree, child, import_ctx)) |child_type| {
|
||||
current_node = child_type;
|
||||
} else return null;
|
||||
} else return null;
|
||||
@@ -280,8 +297,8 @@ pub fn getNodeFromTokens(tree: *std.zig.ast.Tree, node: *std.zig.ast.Node, token
|
||||
return current_node;
|
||||
}
|
||||
|
||||
pub fn getCompletionsFromNode(allocator: *std.mem.Allocator, tree: *std.zig.ast.Tree, node: *std.zig.ast.Node) ![]*std.zig.ast.Node {
|
||||
var nodes = std.ArrayList(*std.zig.ast.Node).init(allocator);
|
||||
pub fn getCompletionsFromNode(allocator: *std.mem.Allocator, tree: *ast.Tree, node: *ast.Node) ![]*ast.Node {
|
||||
var nodes = std.ArrayList(*ast.Node).init(allocator);
|
||||
|
||||
var index: usize = 0;
|
||||
while (node.iterate(index)) |child_node| {
|
||||
@@ -293,18 +310,18 @@ pub fn getCompletionsFromNode(allocator: *std.mem.Allocator, tree: *std.zig.ast.
|
||||
return nodes.items;
|
||||
}
|
||||
|
||||
pub fn nodeToString(tree: *std.zig.ast.Tree, node: *std.zig.ast.Node) ?[]const u8 {
|
||||
pub fn nodeToString(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
|
||||
switch (node.id) {
|
||||
.ContainerField => {
|
||||
const field = node.cast(std.zig.ast.Node.ContainerField).?;
|
||||
const field = node.cast(ast.Node.ContainerField).?;
|
||||
return tree.tokenSlice(field.name_token);
|
||||
},
|
||||
.Identifier => {
|
||||
const field = node.cast(std.zig.ast.Node.Identifier).?;
|
||||
const field = node.cast(ast.Node.Identifier).?;
|
||||
return tree.tokenSlice(field.token);
|
||||
},
|
||||
.FnProto => {
|
||||
const func = node.cast(std.zig.ast.Node.FnProto).?;
|
||||
const func = node.cast(ast.Node.FnProto).?;
|
||||
if (func.name_token) |name_token| {
|
||||
return tree.tokenSlice(name_token);
|
||||
}
|
||||
@@ -317,7 +334,7 @@ pub fn nodeToString(tree: *std.zig.ast.Tree, node: *std.zig.ast.Node) ?[]const u
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn nodesToString(tree: *std.zig.ast.Tree, maybe_nodes: ?[]*std.zig.ast.Node) void {
|
||||
pub fn nodesToString(tree: *ast.Tree, maybe_nodes: ?[]*ast.Node) void {
|
||||
if (maybe_nodes) |nodes| {
|
||||
for (nodes) |node| {
|
||||
std.debug.warn("- {}\n", .{nodeToString(tree, node)});
|
||||
|
||||
Reference in New Issue
Block a user