Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,270 @@
|
||||
#! @file calling.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what is being called at a position, for an editor to show while the
|
||||
#! arguments are written: every overload of what is called, and the parameter
|
||||
#! the position writes.
|
||||
|
||||
import std::containers;
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! An overload of what is being called.
|
||||
pub struct overload {
|
||||
pub var signature: string; #! How it is declared, as `hover` shows it.
|
||||
pub var documentation: string; #! Its documentation comment, or empty.
|
||||
pub var contract: string; #! The `expects` and `ensures` its body begins with, a line each, or empty.
|
||||
pub var arity: u32; #! How many parameters its signature declares.
|
||||
pub var variadic: bool; #! Whether its last parameter takes every argument after the others.
|
||||
}
|
||||
|
||||
#! What is being called at a position.
|
||||
pub struct calls {
|
||||
pub var overloads: vector<overload>; #! Each overload, the one the arguments written so far fit first.
|
||||
pub var parameter: u32; #! The parameter the position writes, counting the receiver of a method called through a value.
|
||||
}
|
||||
|
||||
#! The call whose parentheses are open at a position: the token naming what it
|
||||
#! calls, and the argument being written.
|
||||
struct open_call {
|
||||
var name: u32; #! The token naming what is called.
|
||||
var argument: u32; #! The argument being written, from 0.
|
||||
}
|
||||
|
||||
#! A bracket open before a position, as `open_calls` reads them.
|
||||
struct open_bracket {
|
||||
var closes: tk; #! The token that closes it.
|
||||
var call: bool; #! Whether it is a call's parentheses.
|
||||
var name: u32; #! The token naming what a call calls.
|
||||
var argument: u32; #! The argument of a call being written, from 0.
|
||||
}
|
||||
|
||||
#! What is being called at the offset of the file: the innermost call whose
|
||||
#! parentheses are open there, whose overloads are given, the one a call of the
|
||||
#! arguments written so far would choose first.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @param offset The byte of its text.
|
||||
#! @returns What is called, or nothing where no call is open there.
|
||||
pub def checker::calls_at(&mut this, file: u32, offset: u32): maybe<calls> {
|
||||
var opens := this.open_calls(file, offset);
|
||||
return nothing {} if opens.length == 0;
|
||||
|
||||
# The innermost call whose callee is a function, or a value of a type with an
|
||||
# `invoke`, as a call of it is a call of that.
|
||||
val asked := this.begin_query(file);
|
||||
var member := false;
|
||||
var decl: u32 = 0;
|
||||
var argument: u32 = 0;
|
||||
var i := opens.length;
|
||||
while i > 0 && decl == 0 {
|
||||
i--;
|
||||
member = false;
|
||||
decl = this.called_decl(file, offset, &opens[i], &asked.walked, &asked.decls, &mut member);
|
||||
argument = opens[i].argument;
|
||||
}
|
||||
# A method called through a value is given its receiver, which its signature
|
||||
# names first, so the argument written is the one after it.
|
||||
if member && decl != 0 && this.decls.has(decl, df::method) {
|
||||
argument++;
|
||||
}
|
||||
return nothing {} if decl == 0 || this.decls.kinds.p[decl] != dk::function;
|
||||
|
||||
var made := calls { overloads = vector::<overload>::new(), parameter = argument };
|
||||
var walk := this.decls.walk_overloads(this.first_overload(decl), 32);
|
||||
while true {
|
||||
val at := walk.next();
|
||||
break if at == 0;
|
||||
if !this.decls.has(at, df::failed) {
|
||||
this.resolve(at);
|
||||
val kind: u32 = this.decls.types.p[at];
|
||||
made.overloads.append(overload {
|
||||
signature = this.signature_of(at),
|
||||
documentation = this.documentation_of(this.decls.files.p[at], this.decls.nodes.p[at]),
|
||||
contract = this.contract_of(at),
|
||||
arity = kind != 0 ? this.types.part_count(kind) : 0,
|
||||
variadic = this.decls.has(at, df::variadic)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
# The overload the arguments written so far fit comes first.
|
||||
for i in 0..made.overloads.length {
|
||||
continue if made.overloads[i].arity <= argument && !made.overloads[i].variadic;
|
||||
var fitting := move(&mut made.overloads[i]);
|
||||
made.overloads.remove_at(i);
|
||||
made.overloads.insert_at(0, move(&mut fitting));
|
||||
break;
|
||||
}
|
||||
return move(&mut made);
|
||||
}
|
||||
|
||||
#! The first of the overloads of a function: the first member of its name of
|
||||
#! the type it is declared through, or the first declaration of its name of its
|
||||
#! module.
|
||||
#! @param this The checker.
|
||||
#! @param decl The function.
|
||||
#! @returns The first overload.
|
||||
def checker::first_overload(&this, decl: u32): u32 {
|
||||
val owner: u32 = this.decls.owners.p[decl];
|
||||
val first := owner != 0
|
||||
? this.first_member(decl)
|
||||
: this.modules.decl(this.decls.modules.p[decl], this.decls.names.p[decl]);
|
||||
return first != 0 ? first : decl;
|
||||
}
|
||||
|
||||
#! Finds the calls whose parentheses are open at the offset: the name each
|
||||
#! calls, and the argument being written. The tokens are read rather than the
|
||||
#! tree, since a call is rarely whole while its arguments are written.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @param offset The byte of its text.
|
||||
#! @returns The calls, the innermost last.
|
||||
def checker::open_calls(&this, file: u32, offset: u32): vector<open_call> {
|
||||
val syntax := &this.units[file].syntax;
|
||||
var open := vector::<open_bracket>::new();
|
||||
var called: u32 = 0;
|
||||
var has_called := false;
|
||||
var scope := false;
|
||||
for at in 0..syntax.token_count {
|
||||
val token := at as u32;
|
||||
break if syntax.token_offsets[token] >= offset;
|
||||
val kind := syntax.token_kinds[token];
|
||||
break if kind == tk::eof;
|
||||
if kind == tk::lparen || kind == tk::lbracket || kind == tk::lbrace {
|
||||
open.append(open_bracket {
|
||||
closes = kind == tk::lparen ? tk::rparen : kind == tk::lbracket ? tk::rbracket : tk::rbrace,
|
||||
call = kind == tk::lparen && has_called, name = called
|
||||
});
|
||||
} else if kind == tk::less && scope {
|
||||
# Type arguments follow `::`, as `f::<u8>(x)`, and their commas are not the
|
||||
# call's, so they take a frame of their own.
|
||||
open.append(open_bracket { closes = tk::more });
|
||||
} else if kind == tk::rparen || kind == tk::rbracket || kind == tk::rbrace || kind == tk::more {
|
||||
if open.length != 0 && open.last().closes == kind {
|
||||
open.truncate();
|
||||
}
|
||||
} else if kind == tk::comma {
|
||||
if open.length != 0 && open.last().call {
|
||||
open.last().argument++;
|
||||
}
|
||||
}
|
||||
|
||||
# The name a `(` follows is the last one written before it.
|
||||
if kind == tk::name {
|
||||
called = token;
|
||||
has_called = true;
|
||||
} else if kind != tk::scope && kind != tk::less && kind != tk::more && kind != tk::dot {
|
||||
has_called = false;
|
||||
}
|
||||
scope = kind == tk::scope;
|
||||
}
|
||||
|
||||
# The calls still open, which the position is writing an argument of, the
|
||||
# innermost last.
|
||||
var made := vector::<open_call>::new();
|
||||
for &each in open {
|
||||
if each.call {
|
||||
made.append(open_call { name = each.name, argument = each.argument });
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The function a call open at a position calls: what the node naming it
|
||||
#! refers to, and otherwise a function of the name the position can name.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @param offset The byte of its text.
|
||||
#! @param open The call.
|
||||
#! @param walked The file's tree.
|
||||
#! @param decls The declarations of the file's nodes.
|
||||
#! @param member Where whether it is called through a value is written.
|
||||
#! @returns The function, or 0.
|
||||
def checker::called_decl(
|
||||
&mut this,
|
||||
file: u32,
|
||||
offset: u32,
|
||||
open: &open_call,
|
||||
walked: &tree_walk,
|
||||
decls: &dict<u32, u32>,
|
||||
member: &mut bool
|
||||
): u32 {
|
||||
for &node in walked.order {
|
||||
continue if this.token_of(node) != open.name;
|
||||
val kind := this.kind_of(node);
|
||||
continue if kind != nk::name && kind != nk::member && kind != nk::segment && kind != nk::path;
|
||||
member = kind == nk::member;
|
||||
val meant := this.meaning_of(node, walked, decls);
|
||||
if meant.decl != 0 && this.decls.kinds.p[meant.decl] == dk::function {
|
||||
return meant.decl;
|
||||
}
|
||||
if kind == nk::member {
|
||||
val found := this.member_meaning(this.type_at(this.left_of(node)), this.name_of(node));
|
||||
return found.decl if found.decl != 0 && this.decls.kinds.p[found.decl] == dk::function;
|
||||
}
|
||||
|
||||
# A value of a type with an `invoke` is called through it, which is given
|
||||
# the value as its receiver.
|
||||
val held := this.type_at(node);
|
||||
if held != 0 && held != bt::error {
|
||||
val found := this.find_member(this.reached(held), word::invoke);
|
||||
if found.decl != 0 && this.decls.kinds.p[found.decl] == dk::function {
|
||||
member = true;
|
||||
return found.decl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
# A call being written rarely parses, so where the tree has no node for the
|
||||
# name, or the checker made nothing of the one it has, the name is looked up
|
||||
# as the checker would: a function of the module, of an import, or one built
|
||||
# in.
|
||||
this.at.home = this.home_at(file, offset);
|
||||
val found := this.lookup_global(this.units[file].syntax.token_names[open.name], 0);
|
||||
this.at.home = 0;
|
||||
return found;
|
||||
}
|
||||
|
||||
#! The type of the argument being written at the offset of the file, of the
|
||||
#! call whose parentheses are open there, or 0 where none is.
|
||||
#! @param this The checker.
|
||||
#! @param file The file.
|
||||
#! @param offset The byte of its text.
|
||||
#! @param walked The file's tree.
|
||||
#! @param decls The declarations of the file's nodes.
|
||||
#! @returns The type, or 0.
|
||||
def checker::argument_type(&mut this, file: u32, offset: u32, walked: &tree_walk, decls: &dict<u32, u32>): u32 {
|
||||
var opens := this.open_calls(file, offset);
|
||||
return 0 if opens.length == 0;
|
||||
var member := false;
|
||||
val open := opens.last();
|
||||
val decl := this.called_decl(file, offset, open, walked, decls, &mut member);
|
||||
return 0 if decl == 0 || this.decls.kinds.p[decl] != dk::function;
|
||||
var argument := open.argument;
|
||||
if member && this.decls.has(decl, df::method) {
|
||||
argument++;
|
||||
}
|
||||
|
||||
# The first overload with a parameter for the argument, as the call is read
|
||||
# so far. Every argument after a variadic parameter's first is of it too.
|
||||
var walk := this.decls.walk_overloads(this.first_overload(decl), 32);
|
||||
while true {
|
||||
val at := walk.next();
|
||||
break if at == 0;
|
||||
this.resolve(at);
|
||||
val kind: u32 = this.decls.types.p[at];
|
||||
val count := kind != 0 ? this.types.part_count(kind) : 0;
|
||||
val variadic := this.decls.has(at, df::variadic);
|
||||
if argument < count {
|
||||
return this.types.part_at(kind, argument);
|
||||
}
|
||||
if variadic && count != 0 {
|
||||
return this.types.part_at(kind, count - 1);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user