Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

+270
View File
@@ -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;
}
File diff suppressed because it is too large. Load diff
+81
View File
@@ -0,0 +1,81 @@
#! @file highlights.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the names of a file as what the checker made of each, for an editor
#! to colour them by that rather than by how they are written.
module sakura::analyzer;
#! A name of a file, as what the checker made of it.
pub struct highlight {
pub var offset: u32; #! Where the name begins in the file's text.
pub var length: u32; #! How many bytes it covers.
pub var kind: symbol; #! What it is.
pub var declared: bool; #! Whether this is where it is declared.
pub var readonly: bool; #! Whether what it names is never written, as a `val` or a `const`.
}
#! The names of the file the checker resolved, in the order they are written,
#! each with what it is, so an editor colours the rest as it otherwise would. A
#! file whose syntax has mistakes gives the names of what parsed.
#! @param this The checker.
#! @param file The file.
#! @returns The names.
pub def checker::highlights(&mut this, file: u32): vector<highlight> {
var made := vector::<highlight>::new();
val syntax := &this.units[file].syntax;
val asked := this.begin_query(file);
val walked := &asked.walked;
for &node in walked.order {
val token: u32 = syntax.node_tokens[node];
# A contract's word, `old` within an `ensures`, and `undefined` as a
# variable's value are the language's own words there, though names
# everywhere else.
val held := syntax.node_kinds[node];
if held == nk::contract || held == nk::old_value || held == nk::undefined {
made.append(highlight {
offset = syntax.token_offsets[token], length = syntax.token_length(token, this.names), kind = symbol::keyword
});
continue;
}
continue if syntax.token_kinds[token] != tk::name;
val meant := this.meaning_of(node, walked, &asked.decls);
continue if !meant.found();
var declared := meant.local == node && meant.file == file;
if meant.decl != 0 {
declared = this.decls.nodes.p[meant.decl] == node && this.decls.files.p[meant.decl] == file;
}
if meant.is_module {
val outer: u32 = walked.parents[walked.parents[node]];
declared = outer != 0 && (syntax.node_kinds[outer] == nk::module_decl || syntax.node_kinds[outer] == nk::extern_module)
|| (walked.parents[node] != 0 && (syntax.node_kinds[walked.parents[node]] == nk::module_decl
|| syntax.node_kinds[walked.parents[node]] == nk::extern_module));
}
# A type named where it is used is never written there.
val kind := this.symbol_meant(&meant);
val names_type := kind == symbol::struct_type || kind == symbol::trait_type || kind == symbol::enum_type
|| kind == symbol::type_name || kind == symbol::type_param;
made.append(highlight {
offset = syntax.token_offsets[token],
length = syntax.token_length(token, this.names),
kind = kind,
declared = declared,
readonly = this.readonly_meant(&meant) || (names_type && !declared)
});
this.query_file(file);
}
# A receiver is written before the name of what it declares, as `rect` is in
# `def rect::area`, so the names are put in the order they are written, and a
# name the walk reached twice is one.
made.sort_by(def(a: &highlight, b: &highlight): bool { return a.offset < b.offset; });
var kept := vector::<highlight>::new();
for &each in made {
continue if kept.length != 0 && kept.last.offset == each.offset;
kept.append(each);
}
return kept;
}
+226
View File
@@ -0,0 +1,226 @@
#! @file hover.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains hovering: what an editor shows of the name at a position, which is
#! how what it refers to is declared, with the types the checker gave it and
#! the contract a function's body begins with, and its documentation.
import std::containers;
module sakura::analyzer;
#! What hovering a name shows.
pub struct hover {
pub var signature: string; #! How what it refers to is declared, as `def str::trim(this: &str): str` or `val count: int`, after the attributes written on it, and of a function, a line for each `expects` and `ensures` its body begins with.
pub var documentation: string; #! The documentation comment of what it refers to, without its `#!`, or empty.
pub var offset: u32; #! Where the name begins in the file's text.
pub var length: u32; #! How many bytes it covers.
}
#! What hovering the name at the offset of the file shows: a variable,
#! parameter, function, method, field, type, member of an enum, or module,
#! used or declared there.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @returns What it shows, or nothing where no name is there, or checking left
#! it unresolved.
pub def checker::hover(&mut this, file: u32, offset: u32): maybe<hover> {
val token := this.token_at(file, offset);
return nothing {} if token as u64 >= this.units[file].syntax.token_count;
val asked := this.begin_query(file);
var found := hover {
signature = string::new(nothing {}),
documentation = string::new(nothing {})
};
var shown := false;
for &node in asked.walked.order {
continue if this.token_of(node) != token;
shown = this.shown_at(node, &asked.walked, &asked.decls, &mut found);
break if shown;
}
return nothing {} if !shown;
return move(&mut found);
}
#! What hovering a node of the file being queried shows, or nothing where it
#! names nothing the checker resolved.
#! @param this The checker.
#! @param node The node.
#! @param walked The file's tree.
#! @param decls The declarations of the file's nodes.
#! @param out Where what it shows is written.
#! @returns Whether it shows anything.
def checker::shown_at(&mut this, node: u32, walked: &tree_walk, decls: &dict<u32, u32>, out: &mut hover): bool {
val file := this.at.file;
val spot := this.spot_of(file, this.token_of(node));
val meant := this.meaning_of(node, walked, decls);
var signature := string::new(nothing {});
var documentation := string::new(nothing {});
if meant.is_module {
signature = this.module_signature(meant.named_module);
} else if meant.decl != 0 {
val decl := meant.decl;
val home: u32 = this.decls.files.p[decl];
val declared: u32 = this.decls.nodes.p[decl];
if declared != 0 {
this.attributes_text(home, declared, &mut signature);
}
var written := this.signature_seen(decl, node, walked.parents[node]);
signature.append(written.span);
val contract := this.contract_of(decl);
if contract.length != 0 {
signature.append("\n");
signature.append(contract.span);
}
documentation = this.documentation_of(home, declared);
this.query_file(file);
} else if meant.local != 0 {
signature = this.local_signature(meant.file, meant.local);
documentation = this.documentation_of(meant.file, meant.local);
} else if this.kind_of(node) == nk::member && this.type_at(node) != 0 && this.type_at(node) != bt::error {
# What the compiler gives a value, as the length of a fixed array.
signature.append("val ");
signature.append(this.text_of(node));
signature.append(": ");
this.type_text(this.type_at(node), &mut signature);
} else {
return false;
}
out.signature = move(&mut signature);
out.documentation = move(&mut documentation);
out.offset = spot.offset;
out.length = spot.length;
return true;
}
#! The contract a function's body begins with, which is what callers are
#! promised: each `expects` and `ensures` before its first other statement, as
#! it is written, with its spaces and line breaks run together, a line each,
#! as `expects(b != 0)`.
#! @param this The checker.
#! @param decl The function.
#! @returns The lines, or empty text where it begins with none.
pub def checker::contract_of(&mut this, decl: u32): string {
var made := string::new(nothing {});
return made if this.decls.kinds.p[decl] != dk::function || this.decls.has(decl, df::accessor);
val file: u32 = this.decls.files.p[decl];
val syntax := &this.units[file].syntax;
val source := &this.units[file].source;
val node: u32 = this.decls.nodes.p[decl];
return made if node == 0 || syntax.node_kinds[node] != nk::function;
val body := syntax.record(node, function_slot::body);
return made if body == 0 || syntax.node_kinds[body] != nk::block;
val run := syntax.node_lefts[body];
for i in 0..syntax.run_length(run) {
val each := syntax.run_at(run, i as u32);
break if syntax.node_kinds[each] != nk::contract;
val first: u32 = syntax.node_tokens[each];
continue if syntax.token_names[first] == names::asserts_id;
# What it was parsed from ends at its `;`, which is left out.
var last: u32 = syntax.node_end(each);
if last > first && syntax.token_kinds[last] == tk::semicolon {
last--;
}
val begin: u64 = syntax.token_offsets[first];
val end: u64 = syntax.token_offsets[last] + syntax.token_length(last, this.names);
continue if end <= begin;
if made.length != 0 {
made.append("\n");
}
var spaced := false;
for &byte in source.make_lexeme(begin, end - begin) {
if byte == ' ' || byte == '\n' || byte == '\t' || byte == '\r' {
spaced = true;
continue;
}
if spaced && byte != ')' && made.length != 0 && made[made.length - 1] != '(' && made[made.length - 1] != '\n' {
made.push(' ');
}
spaced = false;
made.push(byte);
}
}
return made;
}
#! The instance a call of a node uses, where the node is what a call calls.
#! @param this The checker.
#! @param callee The node.
#! @returns The instance, or nothing.
def checker::called_by(&mut this, callee: u32): maybe<u32> {
# The call is found by its callee, which its node holds.
val syntax := &this.syn();
for at in 1..syntax.node_count {
val node := at as u32;
continue if syntax.node_kinds[node] != nk::call || this.left_of(node) != callee;
if this.called.get(&this.table_node_of(node)) is some(found) {
return found;
}
return nothing {};
}
return nothing {};
}
#! How a declaration is written where a node names it: a field reached through
#! a value of a generic type, as `p.x` of a `box<int>`, is of the type the value
#! gives it, and a method called through one is of the types it is given.
#! @param this The checker.
#! @param decl The declaration.
#! @param node The node naming it, of the file being queried.
#! @param parent The node holding it.
#! @returns The signature.
def checker::signature_seen(&mut this, decl: u32, node: u32, parent: u32): string {
val kind := this.kind_of(node);
val seen := this.type_at(node);
# A generic function a call uses is shown as the instance the call uses.
if this.decls.kinds.p[decl] == dk::function && !this.decls.has(decl, df::accessor) {
# The call is of the node, or of the path whose last segment it is.
var made: u32 = 0;
var callee := node;
if parent != 0 && kind == nk::segment && (this.kind_of(parent) == nk::path || this.kind_of(parent) == nk::type_name) {
callee = parent;
}
if this.called.get(&this.table_node_of(callee)) is some(found) {
made = found;
}
if made == 0 && callee != node {
if this.called.get(&this.table_node_of(node)) is some(found) {
made = found;
}
}
if made == 0 {
if this.called_by(callee) is some(found) {
made = found;
}
}
if made != 0 && made < this.instances.length as u32 && this.instances[made].decl == decl {
var out := string::new(nothing {});
if this.decls.has(decl, df::exported) {
out.append("pub ");
}
this.instance_text_of(decl, true, made, &mut out);
return out;
}
}
if (kind == nk::member || kind == nk::field_init) && seen != 0 && seen != bt::error
&& this.decls.kinds.p[decl] == dk::field && this.decls.types.p[decl] != seen {
var out := string::new(nothing {});
if this.decls.has(decl, df::exported) {
out.append("pub ");
}
out.append(this.decls.has(decl, df::mutable) ? "var " : "val ");
out.append(this.decl_text(decl));
out.append(": ");
this.type_text(seen, &mut out);
return out;
}
return this.signature_of(decl);
}
+540
View File
@@ -0,0 +1,540 @@
#! @file names.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains what the name at a position refers to, for an editor to go to where
#! that is declared, and everywhere it is written, for an editor to find its
#! references and rename it, with why some names cannot be renamed.
module sakura::analyzer;
#! What the name at a position refers to.
pub struct referent {
pub var at: spot; #! The name at the position.
pub var declaration: spot; #! Where what it refers to is declared, where `declared` says it is.
pub var declared: bool; #! Whether it has a declaration to go to, which a built-in type has not.
pub var refusal: strview; #! Why it cannot be renamed, as that it is built into the language, or empty where it can.
pub var local: bool; #! Whether it is a local, a parameter, a type parameter, or a name a pattern binds, which is only written within the declaration holding it.
pub var member: bool; #! Whether it is a member of a type, as a field, method, property, member of an enum, or associated variable, which is written after `.` or `::` wherever it is not declared.
}
#! What the name at the offset of the file refers to, used or declared there. A
#! name the offset is just past is the one asked about, as an editor puts the
#! cursor after the name it asks about.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @returns What it refers to, or nothing where no name the checker resolved is
#! there.
pub def checker::name_at(&mut this, file: u32, offset: u32): maybe<referent> {
var meant := checker::meant {};
val node := this.seek_name(file, offset, &mut meant);
return nothing {} if node == 0;
var made := referent {
at = this.spot_of(file, this.units[file].syntax.node_tokens[node]),
refusal = ""
};
var declared := spot {};
made.declared = this.declared_at(&meant, &mut declared);
made.declaration = declared;
made.refusal = this.refusal_of(&meant);
made.local = meant.local != 0 || (meant.decl != 0 && this.decls.kinds.p[meant.decl] == dk::type_param);
made.member = meant.decl != 0 && this.decls.owners.p[meant.decl] != 0
&& this.decls.kinds.p[meant.decl] != dk::type_param;
return made;
}
#! Finds the node naming something at the offset of the file, or at the byte
#! before it.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @param meant Where what it refers to is written.
#! @returns The node, or 0.
def checker::seek_name(&mut this, file: u32, offset: u32, meant: &mut checker::meant): u32 {
val syntax := &this.units[file].syntax;
val asked := this.begin_query(file);
var found: u32 = 0;
for back in 0..2 {
break if found != 0 || back as u32 > offset;
val token := this.token_at(file, offset - back as u32);
continue if token as u64 >= syntax.token_count || syntax.token_kinds[token] != tk::name;
for &node in asked.walked.order {
continue if this.token_of(node) != token;
val each := this.meaning_of(node, &asked.walked, &asked.decls);
if each.found() {
meant = each;
found = node;
break;
}
}
}
return found;
}
#! Where what a name refers to is declared: the name of its declaration, of the
#! node declaring a local, or of the first module declaration naming a module.
#! @param this The checker.
#! @param meant What the name refers to.
#! @param out Where it is written.
#! @returns Whether it has a declaration to go to.
def checker::declared_at(&mut this, meant: &checker::meant, out: &mut spot): bool {
if meant.local != 0 {
out = this.spot_of(meant.file, this.units[meant.file].syntax.node_tokens[meant.local]);
return true;
}
if meant.is_module {
return this.module_declared(meant.named_module, out);
}
val decl := meant.decl;
return false if decl == 0 || this.decls.kinds.p[decl] == dk::builtin;
val file: u32 = this.decls.files.p[decl];
val node: u32 = this.decls.nodes.p[decl];
return false if node == 0;
val syntax := &this.units[file].syntax;
val token: u32 = syntax.node_tokens[node];
return false if syntax.token_names[token] != this.decls.names.p[decl];
out = this.spot_of(file, token);
return true;
}
#! Where a module is declared: the last segment of the first `module` naming it,
#! in the order the files were given.
#! @param this The checker.
#! @param id The module.
#! @param out Where it is written.
#! @returns Whether a file declares it.
def checker::module_declared(&mut this, id: u32, out: &mut spot): bool {
for file in 0..this.units.length {
val syntax := &this.units[file].syntax;
val decls := syntax.node_lefts[this.units[file].root];
for &node in syntax.run_view(decls) {
continue if syntax.node_kinds[node] != nk::module_decl && syntax.node_kinds[node] != nk::extern_module;
val path: u32 = syntax.node_lefts[node];
continue if this.module_named(file as u32, path, 0xFFFFFFFF) != id;
val run: u32 = syntax.node_lefts[path];
val last := run == 0 ? path : syntax.run_at(run, syntax.run_length(run) - 1);
out = this.spot_of(file as u32, syntax.node_tokens[last]);
return true;
}
}
return false;
}
#! Why renaming what a name refers to would lose it, or empty text where it
#! would not: what is built in, a module, `this`, and what the compiler finds by
#! its name.
#! @param this The checker.
#! @param meant What the name refers to.
#! @returns Why it cannot be renamed, or empty text.
def checker::refusal_of(&mut this, meant: &checker::meant): strview {
if meant.is_module {
return "A module is named by every file that declares or imports it, which renaming does not change.";
}
if meant.local != 0 {
val name := this.local_text(meant.file, meant.local);
if name == "this" {
return "`this` is what every method calls its receiver.";
}
val syntax := &this.units[meant.file].syntax;
if syntax.node_kinds[meant.local] == nk::param && (syntax.flags_at(meant.local) & nf::implied as u32) != 0 {
return "`value` is what every `set` calls what it is given.";
}
return "";
}
val decl := meant.decl;
return "" if decl == 0;
if this.decls.kinds.p[decl] == dk::builtin || this.decls.nodes.p[decl] == 0 {
return "It is built into the language.";
}
if this.decls.kinds.p[decl] == dk::type_param && this.decls.names.p[decl] == names::self_id {
return "`self` is what implements the trait it is written within.";
}
if this.decls.kinds.p[decl] == dk::function && this.decls.has(decl, df::intrinsic) {
return "An `@intrinsic` function is bound to what implements it by its name.";
}
if this.decls.kinds.p[decl] == dk::function && this.decls.has(decl, df::foreign) && !this.has_body(decl) {
return "A function without a body in an `extern module` is the C function of its name.";
}
if (this.decls.kinds.p[decl] == dk::struct_type || this.decls.kinds.p[decl] == dk::union_type) && this.decls.has(decl, df::foreign) {
return this.decls.kinds.p[decl] == dk::union_type
? "A union of an `extern module` is C's: every field begins where it does, and nothing says which it holds."
: "A struct of an `extern module` is laid out as C lays it out.";
}
return this.compiler_names(decl);
}
#! Why renaming a declaration would lose it to the compiler, which finds some by
#! their names: the declarations of std it knows, as the types it makes values
#! of and the traits whose methods it calls, the methods those traits ask for,
#! the methods its arithmetic operators call, where a program begins, and the
#! runtime an executable calls into.
#! @param this The checker.
#! @param decl The declaration.
#! @returns Why it cannot be renamed, or empty text.
def checker::compiler_names(&mut this, decl: u32): strview {
for &each in this.known {
if each.value == decl && decl != 0 {
return match this.decls.kinds.p[decl] {
dk::trait_type => "The compiler finds it by its name, as a trait whose methods it calls or whose meaning it decides.",
dk::struct_type, dk::union_type, dk::enum_type, dk::alias_type => "The compiler finds it by its name, as a type it makes values of.",
else => "The compiler finds it by its name."
};
}
}
if this.in_runtime(decl) && this.decls.kinds.p[decl] != dk::field {
return "The compiler calls into `runtime` by name.";
}
if this.decls.kinds.p[decl] == dk::function && this.decls.owners.p[decl] == 0 && this.decls.modules.p[decl] == 0
&& this.decls.names.p[decl] == word::main {
return "A program begins at the function named `main`.";
}
# A method the compiler calls for every type implementing one of its traits.
if this.decls.kinds.p[decl] == dk::function && this.decls.owners.p[decl] != 0 {
var traits := this.traits_asking(decl);
for &trait_decl in traits {
for &each in this.known {
if each.value == trait_decl {
return "The compiler calls it by its name, for every type implementing the trait asking for it.";
}
}
}
}
# A method an operator calls, which a type has the operator by declaring: `add`
# for `+`, and the rest, taking the other operand, and `negate` for `-x` and
# `bit_not` for `~x`.
if this.decls.kinds.p[decl] == dk::function && this.decls.has(decl, df::method) {
val name: u32 = this.decls.names.p[decl];
val taken := this.types.part_count(this.decls.types.p[decl]);
if name == word::negate && taken == 1 {
return "The compiler calls it by its name, as the method `-x` is.";
}
if name == word::bit_not && taken == 1 {
return "The compiler calls it by its name, as the method `~x` is.";
}
if taken == 2 && (name == word::add || name == word::sub || name == word::mul
|| name == word::div || name == word::rem || name == word::bit_and
|| name == word::bit_or || name == word::bit_xor || name == word::shift_left
|| name == word::shift_right) {
return "The compiler calls it by its name, as the method an operator is, as `+` is `add`.";
}
}
return "";
}
#! Whether a declaration is of the module `runtime`, which only an executable
#! holds.
#! @param this The checker.
#! @param decl The declaration.
#! @returns Whether it is.
def checker::in_runtime(&this, decl: u32): bool {
val home: u32 = this.decls.modules.p[decl];
return home != 0 && this.modules.parents[home] == 0 && this.names.text(this.modules.names[home]) == "runtime";
}
#! The traits that ask for a method: its own type's, where that is a trait, and
#! those its type implements, or those compose, with a member of its name.
#! @param this The checker.
#! @param method The method.
#! @returns The traits' declarations.
def checker::traits_asking(&mut this, method: u32): vector<u32> {
var made := vector::<u32>::new();
val owner: u32 = this.decls.owners.p[method];
return made if owner == 0;
if this.decls.kinds.p[owner] == dk::trait_type {
made.append(owner);
}
this.add_asking(owner, this.decls.names.p[method], &mut made, 0);
return made;
}
#! Adds the traits a type implements, or a trait composes, and those they
#! compose, that have a member of the name.
#! @param this The checker.
#! @param decl The type or trait.
#! @param name The ID of the name.
#! @param into The traits found so far.
#! @param depth How deeply composition has been followed.
def checker::add_asking(&mut this, decl: u32, name: u32, into: &mut vector<u32>, depth: u32): void {
return if depth > 16;
val kind: dk = this.decls.kinds.p[decl];
return if kind != dk::struct_type && kind != dk::union_type && kind != dk::trait_type;
val run := this.composed_of(decl);
for i in 0..this.decls.list_length(run) {
val trait_decl := this.types.first_of(this.decls.list_at(run, i as u32));
continue if trait_decl == 0 || this.decls.kinds.p[trait_decl] != dk::trait_type;
if this.decls.member(trait_decl, name) != 0 {
var known := false;
for &each in into {
known = known || each == trait_decl;
}
if !known {
into.append(trait_decl);
}
}
this.add_asking(trait_decl, name, into, depth + 1);
}
}
#! Whether a trait is another, or composes it.
#! @param this The checker.
#! @param trait_decl The trait.
#! @param target The trait it may compose.
#! @param depth How deeply composition has been followed.
#! @returns Whether it does.
def checker::trait_composes(&mut this, trait_decl: u32, target: u32, depth: u32): bool {
return true if trait_decl == target;
return false if depth > 16 || this.decls.kinds.p[trait_decl] != dk::trait_type;
val run := this.composed_of(trait_decl);
for i in 0..this.decls.list_length(run) {
return true if this.trait_composes(this.types.first_of(this.decls.list_at(run, i as u32)), target, depth + 1);
}
return false;
}
#! Everywhere the name at the offset of the file refers to is written, its
#! declaration among them, in every file of the program, ordered by file and
#! then by where in it they are. Each place is written with the very text of
#! the name, so an editor renames it by replacing each.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @returns The places, which are none where no name the checker resolved is
#! there.
pub def checker::references(&mut this, file: u32, offset: u32): vector<spot> {
var found := vector::<spot>::new();
var strays := vector::<spot>::new();
this.refer_all(file, offset, &mut found, &mut strays);
return found;
}
#! The places the name at the offset of the file is written but the checker left
#! unresolved, as in a branch of `when` for another target, which may refer to
#! the same thing. A local is sought only within the declaration holding it.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @returns The places, ordered as `references` orders them.
pub def checker::unresolved(&mut this, file: u32, offset: u32): vector<spot> {
var found := vector::<spot>::new();
var strays := vector::<spot>::new();
this.refer_all(file, offset, &mut found, &mut strays);
return strays;
}
#! Everywhere what the name at a place refers to is written, and where its text
#! is written but unresolved, as `references` and `unresolved` give them.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @param found Where the places it is written are put.
#! @param strays Where the places it is written unresolved are put.
pub def checker::refer_all(&mut this, file: u32, offset: u32, found: &mut vector<spot>, strays: &mut vector<spot>): void {
var meant := checker::meant {};
val named := this.seek_name(file, offset, &mut meant);
return if named == 0;
var declared := spot {};
return if !this.declared_at(&meant, &mut declared);
val name: u32 = this.units[declared.file].syntax.token_names[this.token_at(declared.file, declared.offset)];
var sought := vector::<checker::meant>::new();
sought.append(meant);
this.widen(&meant, &mut sought);
# The top-level declaration holding a local, which alone may name it.
var home_top: u32 = 0;
if meant.local != 0 {
home_top = this.top_holding(meant.file, meant.local);
}
for each in 0..this.units.length {
val at := each as u32;
continue if meant.local != 0 && at != meant.file;
val syntax := &this.units[at].syntax;
val asked := this.begin_query(at);
val walked := &asked.walked;
for &node in walked.order {
val token: u32 = syntax.node_tokens[node];
continue if syntax.token_names[token] != name || syntax.token_kinds[token] != tk::name;
continue if home_top != 0 && this.top_of(walked, node) != home_top;
val held := this.meaning_of(node, walked, &asked.decls);
var hit := false;
for &want in sought {
hit = hit || held.same(&want);
}
if hit {
this.add_place(found, this.spot_of(at, token));
if syntax.node_kinds[node] == nk::type_param || syntax.node_kinds[node] == nk::const_param {
this.refer_bounds(at, node, found);
}
} else if !held.found() {
val kind := syntax.node_kinds[node];
val unnamed_type := (kind == nk::type_name && syntax.node_lefts[node] == 0) || kind == nk::segment;
if kind == nk::name || kind == nk::member || unnamed_type
|| (kind == nk::field_init && (syntax.flags_at(node) & nf::named as u32) != 0) {
this.add_place(strays, this.spot_of(at, token));
}
}
}
}
this.sort_places(found);
this.sort_places(strays);
}
#! Adds a place, once.
#! @param this The checker.
#! @param into The places.
#! @param place The place.
def checker::add_place(&this, into: &mut vector<spot>, place: spot): void {
for &each in into {
return if each.file == place.file && each.offset == place.offset;
}
into.append(place);
}
#! Orders places by the path of their file, then where in it they are, so the
#! places of a name come in the same order however the files were given.
#! @param this The checker.
#! @param places The places.
def checker::sort_places(&this, places: &mut vector<spot>): void {
places.sort_by(def(a: &spot, b: &spot): bool { return this.place_before(a, b); });
}
#! Whether one place comes before another.
#! @param this The checker.
#! @param a The one place.
#! @param b The other.
#! @returns Whether `a` comes first.
def checker::place_before(&this, a: &spot, b: &spot): bool {
if a.file != b.file {
return this.units[a.file].source.path.compare(this.units[b.file].source.path) < 0;
}
return a.offset < b.offset;
}
#! The top-level declaration holding a node of a file.
#! @param this The checker.
#! @param file The file.
#! @param node The node.
#! @returns The top-level declaration's node.
def checker::top_holding(&this, file: u32, node: u32): u32 {
val walked := this.tree_of(file);
return this.top_of(&walked, node);
}
#! The top-level node holding a node, which is the node itself where the
#! document holds it.
#! @param this The checker.
#! @param walked The file's tree.
#! @param node The node.
#! @returns The top-level node.
def checker::top_of(&this, walked: &tree_walk, node: u32): u32 {
var at := node;
var steps := 0;
while steps < 4096 {
val parent: u32 = walked.parents[at];
return at if parent == 0 || walked.parents[parent] == 0;
at = parent;
steps++;
}
return at;
}
#! A `where` clause names the type parameters it bounds, as `T` of
#! `where T: show`, though no node holds the name there, since the bounds are
#! given to the parameter. So the tokens after the parameter are read up to the
#! body, or the `;` ending the declaration, for each name that follows `where`
#! or `,` and is followed by `:`.
#! @param this The checker.
#! @param file The file.
#! @param param The `type_param` node.
#! @param into Where the places are put.
def checker::refer_bounds(&this, file: u32, param: u32, into: &mut vector<spot>): void {
val syntax := &this.units[file].syntax;
val name: u32 = syntax.token_names[syntax.node_tokens[param]];
var depth := 0;
var clause := false;
var at: u32 = syntax.node_tokens[param] + 1;
while at as u64 + 1 < syntax.token_count {
val kind := syntax.token_kinds[at];
if kind == tk::lparen || kind == tk::lbracket || kind == tk::less {
depth++;
} else if (kind == tk::rparen || kind == tk::rbracket || kind == tk::more) && depth > 0 {
depth--;
}
break if depth == 0 && (kind == tk::lbrace || kind == tk::semicolon);
if depth == 0 && kind == tk::kw_where {
clause = true;
} else if clause && kind == tk::name && syntax.token_names[at] == name && syntax.token_kinds[at + 1] == tk::colon {
val prior := syntax.token_kinds[at - 1];
if prior == tk::kw_where || prior == tk::comma {
this.add_place(into, this.spot_of(file, at));
}
}
at++;
}
}
#! A method a trait asks for is one name with each method implementing it, since
#! a type implements a trait by the names of its methods: renaming one alone
#! would leave a type no longer implementing a trait it declares. So a trait's
#! method is sought with the method of the name of every type declaring the
#! trait, directly or through one composing it, and a method of a type with the
#! members of the traits its type declares that ask for it, and so with theirs.
#! @param this The checker.
#! @param meant What the name refers to.
#! @param sought What is sought, which the rest are added to.
def checker::widen(&mut this, meant: &checker::meant, sought: &mut vector<checker::meant>): void {
val decl := meant.decl;
return if decl == 0 || this.decls.kinds.p[decl] != dk::function || this.decls.owners.p[decl] == 0;
var traits := this.traits_asking(decl);
return if traits.length == 0;
val name: u32 = this.decls.names.p[decl];
this.add_family(this.decls.owners.p[decl], name, sought);
for &trait_decl in traits {
this.add_family(trait_decl, name, sought);
for id in 1..this.decls.count {
val type_decl := id as u32;
val kind: dk = this.decls.kinds.p[type_decl];
continue if kind != dk::struct_type && kind != dk::union_type && kind != dk::trait_type;
val run := this.composed_of(type_decl);
for i in 0..this.decls.list_length(run) {
val named := this.types.first_of(this.decls.list_at(run, i as u32));
if this.trait_composes(named, trait_decl, 0) {
this.add_family(type_decl, name, sought);
break;
}
}
}
}
}
#! Adds each member of the name a type has, which overloads of a trait's method
#! are renamed together, as `at(&this)` and `at(&mut this)`.
#! @param this The checker.
#! @param owner The type.
#! @param name The ID of the name.
#! @param sought What is sought.
def checker::add_family(&mut this, owner: u32, name: u32, sought: &mut vector<checker::meant>): void {
var walk := this.decls.walk_overloads(this.decls.member(owner, name), 256);
while true {
val at := walk.next();
break if at == 0;
val each := checker::meant { decl = this.standing_for(at) };
var known := false;
for &held in sought {
known = known || held.same(&each);
}
if !known {
sought.append(each);
}
}
}
+80
View File
@@ -0,0 +1,80 @@
#! @file programs.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains what says which program a file belongs to. A file declaring `main`
#! begins a program, which holds it and every file declaring none, so an
#! editor whose workspace holds several, as a program and the program testing
#! it, checks one of them at a time.
module sakura::analyzer;
#! Whether a parsed file declares `main` outside any module, beginning a
#! program. A `main` in any branch of a `when` counts, whatever the target, and
#! one after a `module` is that module's and does not. A file whose syntax has
#! mistakes counts by what parsed.
#! @param this The file's syntax.
#! @param root Its `document` node.
#! @param main The ID of the name `main`.
#! @returns Whether it does.
pub def syntax::declares_main(&this, root: u32, main: u32): bool {
var found := false;
this.main_within(this.node_lefts[root], main, &mut found);
return found;
}
#! Whether a run of top-level declarations declares `main` before a `module`.
#! @param this The file's syntax.
#! @param run The run.
#! @param main The ID of the name `main`.
#! @param found Where whether one does is written.
#! @returns Whether a `module` ended the run, so nothing after it counts.
def syntax::main_within(&this, run: u32, main: u32, found: &mut bool): bool {
for &node in this.run_view(run) {
match this.node_kinds[node] {
nk::module_decl, nk::extern_module => return true;
nk::function => {
if this.token_names[this.node_tokens[node]] == main && this.record(node, function_slot::receiver) == 0 {
found = true;
}
}
nk::when_block => {
# Each branch is a block of declarations, or another `when`.
var branch := this.record(node, if_slot::then);
var other := this.record(node, if_slot::other);
var steps := 0;
while steps < 64 {
steps++;
if branch != 0 && this.node_kinds[branch] == nk::block {
return true if this.main_within(this.node_lefts[branch], main, found);
}
break if other == 0;
if this.node_kinds[other] == nk::when_block {
branch = this.record(other, if_slot::then);
other = this.record(other, if_slot::other);
} else {
branch = other;
other = 0;
}
}
}
else => {}
}
}
return false;
}
#! Whether any note made of a file is a mistake of its syntax, which the lexer
#! or the parser made, so what the checker says of it would mislead.
#! @param this The note.
#! @returns Whether it is.
pub val note::is_syntax: bool {
get {
return match this.code {
ec::syntax, ec::token, ec::expr, ec::ident, ec::kind, ec::decl, ec::escape, ec::char, ec::block => true,
else => false
};
}
}
+829
View File
@@ -0,0 +1,829 @@
#! @file signatures.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains how an editor is shown what a name is: the signature it is declared
#! with, with the types the checker gave it, as `def str::trim(this: &str): str`
#! or `val count: int`, and the documentation comment written for it.
module sakura::analyzer;
#! The most members a type's signature lists.
const checker::listed: u32 = 48;
#! How a declaration is written, with the types the checker gave it, as
#! `pub def vec2::length(this: &vec2): float`. A struct, union, trait, or enum
#! lists its members.
#! @param this The checker.
#! @param decl The declaration.
#! @returns The signature.
pub def checker::signature_of(&mut this, decl: u32): string {
var out := string::new(nothing {});
this.resolve(decl);
val kind: dk = this.decls.kinds.p[decl];
# What another module reaches is written as it was declared, `pub` and all.
if this.decls.has(decl, df::exported) && kind != dk::enum_member && kind != dk::type_param && kind != dk::builtin {
out.append("pub ");
}
match kind {
dk::function => {
if this.decls.has(decl, df::accessor) {
this.property_text(this.decls.owners.p[decl], &mut out);
} else {
this.function_text(decl, true, &mut out);
}
}
dk::property => this.property_text(decl, &mut out);
dk::field => this.field_text(decl, this.decls.names.p[decl], &mut out);
dk::enum_member => {
this.owner_prefix(decl, &mut out);
out.append(this.decl_text(decl));
out.append(" = ");
this.value_text(decl, &mut out);
}
dk::variable, dk::attribute => {
out.append(this.decls.has(decl, df::constant) ? "const " : this.decls.has(decl, df::mutable) ? "var " : "val ");
this.owner_prefix(decl, &mut out);
out.append(this.decl_text(decl));
out.append(": ");
this.type_text(this.decls.types.p[decl], &mut out);
}
dk::struct_type, dk::union_type => this.aggregate_text(decl, &mut out);
dk::trait_type => this.trait_text(decl, &mut out);
dk::enum_type => this.enum_text(decl, &mut out);
dk::alias_type => {
val syntax := &this.units[this.decls.files.p[decl]].syntax;
val node: u32 = this.decls.nodes.p[decl];
out.append("type ");
this.type_name_text(decl, &mut out);
this.generics_text(this.decls.files.p[decl], syntax.record(node, alias_slot::type_params), &mut out);
out.append(" = ");
this.type_text(this.decls.targets.p[decl], &mut out);
this.where_text(this.decls.files.p[decl], syntax.record(node, alias_slot::type_params), &mut out);
}
dk::type_param => this.type_param_text(decl, &mut out);
dk::assoc_type => {
out.append("type ");
out.append(this.decl_text(decl));
val bounds: u32 = this.decls.children.p[decl];
for i in 0..this.decls.list_length(bounds) {
out.append(i == 0 ? ": " : " + ");
this.type_text(this.decls.list_at(bounds, i as u32), &mut out);
}
}
dk::builtin => {
# A built-in type is written as notes spell it, as `int` for `i64`.
out.append("type ");
this.type_text(this.decls.types.p[decl], &mut out);
}
else => {
out.append("type ");
out.append(this.decl_text(decl));
}
}
return out;
}
#! The name a local is known by, as its declaration writes it, or of a parameter
#! an accessor implies, whose token is the `get` or `set`, `this` or `value`.
#! @param this The checker.
#! @param file The file.
#! @param node The node declaring it.
#! @returns The name.
pub def checker::local_text(&mut this, file: u32, node: u32): strview {
val syntax := &this.units[file].syntax;
val text := this.token_text(file, syntax.node_tokens[node]);
return text if syntax.node_kinds[node] != nk::param || (syntax.flags_at(node) & nf::implied as u32) == 0;
return text == "set" && (syntax.flags_at(node) & nf::mutable as u32) == 0 ? "value" : "this";
}
#! How a local is written, with the type the checker gave it, as `val n: int`,
#! or `n: int` for a parameter.
#! @param this The checker.
#! @param file The file.
#! @param node The node declaring it.
#! @returns The signature.
pub def checker::local_signature(&mut this, file: u32, node: u32): string {
var out := string::new(nothing {});
val syntax := &this.units[file].syntax;
val saved := this.at;
this.use_table(this.units[file].table);
var kind := this.type_at(node);
val kind_of := syntax.node_kinds[node];
val name := this.local_text(file, node);
if kind_of == nk::param {
val flags := syntax.flags_at(node);
# A parameter of a function without a body is no local, so its type is the
# function's.
if !this.has_flag(node, rf::local) {
kind = this.param_type(file, node);
}
out.append(name);
out.append(": ");
if (flags & nf::variadic as u32) != 0 && this.types.kind_of(kind) == ty::named && this.types.part_count(kind) == 1 {
this.type_text(this.types.part_at(kind, 0), &mut out);
out.append("...");
} else {
this.type_text(kind, &mut out);
}
this.at = saved;
return out;
}
val flags := kind_of == nk::variable ? syntax.flags_at(node) : 0;
if (flags & nf::constant as u32) != 0 {
out.append("const ");
} else {
out.append(this.has_flag(node, rf::mutable) || (flags & nf::mutable as u32) != 0 ? "var " : "val ");
}
out.append(name);
out.append(": ");
this.type_text(kind, &mut out);
this.at = saved;
return out;
}
#! The type of a parameter of a function, as its function's type gives it.
#! @param this The checker.
#! @param file The file.
#! @param node The `param` node.
#! @returns Its type, or the error type.
def checker::param_type(&mut this, file: u32, node: u32): u32 {
# Resolving a function may make more declarations of the file, but nothing is
# read of those it lists once one is resolved.
for &id in this.decls.by_file.of(file) {
continue if this.decls.kinds.p[id] != dk::function;
val syntax := &this.units[file].syntax;
val owner: u32 = this.decls.nodes.p[id];
val run := this.decls.has(id, df::accessor) ? syntax.node_lefts[owner] : syntax.record(owner, function_slot::params);
for i in 0..syntax.run_length(run) {
continue if syntax.run_at(run, i as u32) != node;
this.resolve(id);
val kind: u32 = this.decls.types.p[id];
return i < this.types.part_count(kind) ? this.types.part_at(kind, i as u32) : bt::error;
}
}
return bt::error;
}
#! How a module is written, as `module std::io`.
#! @param this The checker.
#! @param id The module.
#! @returns The signature.
pub def checker::module_signature(&this, id: u32): string {
var out := string::from("module ", nothing {});
this.module_text(id, &mut out);
return out;
}
#! Writes the path of a module, as `std::io`.
#! @param this The checker.
#! @param id The module.
#! @param out Where it is written.
pub def checker::module_text(&this, id: u32, out: &mut string): void {
return if id == 0;
val parent: u32 = this.modules.parents[id];
if parent != 0 {
this.module_text(parent, out);
out.append("::");
}
out.append(this.names.text(this.modules.names[id]));
}
#! Writes a type as notes spell it, as `&mut list<int>`.
#! @param this The checker.
#! @param kind The type.
#! @param out Where it is written.
def checker::type_text(&this, kind: u32, out: &mut string): void {
if kind == 0 {
out.append("?");
return;
}
val described := this.describe(kind);
out.append(described.span);
}
#! Writes the type a member is declared through, as `rect::` of `rect::area`.
#! @param this The checker.
#! @param decl The member.
#! @param out Where it is written.
def checker::owner_prefix(&this, decl: u32, out: &mut string): void {
val owner: u32 = this.decls.owners.p[decl];
return if owner == 0;
out.append(this.decl_text(owner));
out.append("::");
}
#! Writes the value of a member of an enum.
#! @param this The checker.
#! @param decl The member.
#! @param out Where it is written.
def checker::value_text(&this, decl: u32, out: &mut string): void {
var held := constant {};
if !this.decl_value(decl, &mut held) {
out.append("?");
return;
}
if held.negative {
out.append("-");
}
val text := $"{held.magnitude}";
out.append(text.span);
}
#! Writes a function as it is declared: `def`, its name, after the type it is
#! declared through where `owner` says, its own type parameters, its
#! parameters with the types the checker gave them, and what it gives.
#! @param this The checker.
#! @param decl The function.
#! @param owner Whether the type it is declared through is written.
#! @param out Where it is written.
def checker::function_text(&mut this, decl: u32, owner: bool, out: &mut string): void {
this.instance_text_of(decl, owner, 0, out);
}
#! Writes a function as `function_text` does, or where it is given an instance
#! of it, as the instance is: its type arguments in place of its own type
#! parameters, and its parameters and result of the types they give, as
#! `def max<int>(a: int, b: int): int`.
#! @param this The checker.
#! @param decl The function.
#! @param owner Whether the type it is declared through is written.
#! @param made The instance, or 0.
#! @param out Where it is written.
def checker::instance_text_of(&mut this, decl: u32, owner: bool, made: u32, out: &mut string): void {
val file: u32 = this.decls.files.p[decl];
val syntax := &this.units[file].syntax;
val node: u32 = this.decls.nodes.p[decl];
if this.decls.has(decl, df::optional) {
out.append("@optional ");
}
if this.decls.has(decl, df::intrinsic) {
out.append("@intrinsic ");
} else if this.decls.has(decl, df::constant) {
out.append("const ");
}
out.append("def ");
if owner {
this.owner_prefix(decl, out);
}
out.append(this.decl_text(decl));
var kind: u32 = this.decls.types.p[decl];
if made != 0 {
# The arguments of its own type parameters, which follow those of the type
# it is declared through, written as notes write them.
val held: instance = this.instances[made];
kind = held.kind;
val own := this.own_param_count(decl);
val count := this.types.part_count(held.args);
if own != 0 && own <= count {
val parts := this.types.parts_view(held.args);
out.append("<");
this.describe_args(parts[count - own:own as u64], out);
out.append(">");
}
} else {
this.generics_text(file, syntax.record(node, function_slot::type_params), out);
}
out.append("(");
val params := syntax.record(node, function_slot::params);
val count := syntax.run_length(params);
for i in 0..count {
if i != 0 {
out.append(", ");
}
val param := syntax.run_at(params, i as u32);
out.append(this.token_text(file, syntax.node_tokens[param]));
out.append(": ");
val held: u32 = kind != 0 && i < this.types.part_count(kind) ? this.types.part_at(kind, i as u32) : 0;
this.type_text(held, out);
# A pack's type is already written as spread, as `Ts...`.
if i as u32 + 1 == count && this.decls.has(decl, df::variadic) && held != 0 && !this.types.is_open_pack(held) {
out.append("...");
}
}
out.append("): ");
this.type_text(kind != 0 ? this.types.first_of(kind) : 0, out);
if made == 0 {
this.where_text(file, syntax.record(node, function_slot::type_params), out);
}
}
#! Writes a property as it is declared rather than as either of its accessors:
#! `val` where it only reads, `var` where it also writes, and its type.
#! @param this The checker.
#! @param property The property.
#! @param out Where it is written.
def checker::property_text(&mut this, property: u32, out: &mut string): void {
this.resolve(property);
var intrinsic := this.decls.has(property, df::intrinsic);
val run: u32 = this.decls.children.p[property];
for &each in this.decls.list_view(run) {
intrinsic = intrinsic || this.decls.has(each, df::intrinsic);
}
if intrinsic {
out.append("@intrinsic ");
}
out.append(this.property_setter(property) != 0 ? "var " : "val ");
this.owner_prefix(property, out);
out.append(this.decl_text(property));
out.append(": ");
this.type_text(this.decls.types.p[property], out);
}
#! Writes a field as it is declared, by the name it is reached by, as `var x: int`
#! or `val base: ...pair`.
#! @param this The checker.
#! @param field The field.
#! @param name The ID of the name it is reached by.
#! @param out Where it is written.
def checker::field_text(&mut this, field: u32, name: u32, out: &mut string): void {
this.resolve(field);
out.append(this.decls.has(field, df::mutable) ? "var " : "val ");
out.append(this.names.text(name));
out.append(": ");
if this.is_spread(field) {
out.append("...");
}
this.type_text(this.decls.types.p[field], out);
}
#! Writes the name a type is declared with, after the type it is declared
#! through as that is written, as `list<T>::iter`.
#! @param this The checker.
#! @param decl The type.
#! @param out Where it is written.
def checker::type_name_text(&this, decl: u32, out: &mut string): void {
val file: u32 = this.decls.files.p[decl];
val node: u32 = this.decls.nodes.p[decl];
val syntax := &this.units[file].syntax;
val receiver := node == 0 ? 0 : syntax.record(node, decl_slot::receiver);
if receiver != 0 {
val run: u32 = syntax.node_lefts[receiver];
val count := run == 0 ? 1 : syntax.run_length(run);
for i in 0..count {
val segment := run == 0 ? receiver : syntax.run_at(run, i as u32);
out.append(this.token_text(file, syntax.node_tokens[segment]));
this.generics_text(file, run == 0 ? 0 : syntax.node_lefts[segment], out);
out.append("::");
}
}
out.append(this.decl_text(decl));
}
#! Writes the names of a run of type parameters, as `<K, V>`, or nothing where
#! the run is empty.
#! @param this The checker.
#! @param file The file.
#! @param run The run of `type_param` nodes.
#! @param out Where it is written.
def checker::generics_text(&this, file: u32, run: u32, out: &mut string): void {
val syntax := &this.units[file].syntax;
return if run == 0 || syntax.run_length(run) == 0;
out.append("<");
for i in 0..syntax.run_length(run) {
if i != 0 {
out.append(", ");
}
# A pattern parameter is written whole, as `def(Ts...): R` or `L[N]`.
val param := syntax.run_at(run, i as u32);
if syntax.node_kinds[param] == nk::type_pattern || syntax.node_kinds[param] == nk::array_pattern {
var held := this.written(file, param);
out.append(held.span);
} else {
out.append(this.token_text(file, syntax.node_tokens[param]));
}
}
out.append(">");
}
#! Writes the `where` clause bounding a run of type parameters, as
#! ` where T: show + equatable`, or nothing where none is bounded.
#! @param this The checker.
#! @param file The file.
#! @param run The run of `type_param` nodes.
#! @param out Where it is written.
def checker::where_text(&this, file: u32, run: u32, out: &mut string): void {
val syntax := &this.units[file].syntax;
return if run == 0;
var first := true;
for &each in syntax.run_view(run) {
var param := each;
continue if syntax.node_kinds[param] == nk::type_pattern;
# An array pattern's element is bounded as a type parameter is.
if syntax.node_kinds[param] == nk::array_pattern {
param = syntax.node_lefts[param];
}
val bounds: u32 = syntax.node_lefts[param];
continue if bounds == 0 || syntax.run_length(bounds) == 0;
out.append(first ? " where " : ", ");
this.param_text(file, param, out);
first = false;
}
}
#! Writes a type parameter as it is declared, with its bounds, as
#! `T: show + equatable`, `Ts...`, or `N: u64`.
#! @param this The checker.
#! @param file The file.
#! @param param The `type_param` or `const_param` node.
#! @param out Where it is written.
def checker::param_text(&this, file: u32, param: u32, out: &mut string): void {
val syntax := &this.units[file].syntax;
out.append(this.token_text(file, syntax.node_tokens[param]));
if syntax.node_kinds[param] == nk::type_param && (syntax.node_rights[param] & nf::packed as u32) != 0 {
out.append("...");
}
var joined := ": ";
if syntax.node_kinds[param] == nk::const_param && syntax.node_rights[param] != 0 {
out.append(": ");
var held := this.written(file, syntax.node_rights[param]);
out.append(held.span);
joined = " + ";
} else if syntax.node_kinds[param] == nk::const_param {
# The length an array pattern names, as `N` of `L[N]`.
out.append(": u64");
joined = " + ";
}
val bounds: u32 = syntax.node_lefts[param];
for i in 0..(bounds == 0 ? 0 : syntax.run_length(bounds)) {
out.append(i == 0 ? joined : " + ");
var held := this.written(file, syntax.run_at(bounds, i as u32));
out.append(held.span);
}
}
#! Writes a type parameter, as `type parameter T: show`, or a constant one, as
#! `constant parameter N: u64`.
#! @param this The checker.
#! @param decl The type parameter.
#! @param out Where it is written.
def checker::type_param_text(&this, decl: u32, out: &mut string): void {
val file: u32 = this.decls.files.p[decl];
val node: u32 = this.decls.nodes.p[decl];
val syntax := &this.units[file].syntax;
out.append(this.decls.has(decl, df::valued) ? "constant parameter " : "type parameter ");
if node != 0 && (syntax.node_kinds[node] == nk::type_param || syntax.node_kinds[node] == nk::const_param) {
this.param_text(file, node, out);
} else {
out.append(this.decl_text(decl));
}
}
#! Writes the traits a type implements, or a trait composes, as they are
#! written, as `: show, equatable`.
#! @param this The checker.
#! @param decl The type.
#! @param out Where it is written.
def checker::traits_text(&this, decl: u32, out: &mut string): void {
val file: u32 = this.decls.files.p[decl];
val node: u32 = this.decls.nodes.p[decl];
val syntax := &this.units[file].syntax;
val run := node == 0 ? 0 : syntax.record(node, aggregate_slot::traits);
for i in 0..(run == 0 ? 0 : syntax.run_length(run)) {
out.append(i == 0 ? ": " : ", ");
var held := this.written(file, syntax.run_at(run, i as u32));
out.append(held.span);
}
}
#! Writes a struct or union with its fields.
#! @param this The checker.
#! @param decl The type.
#! @param out Where it is written.
def checker::aggregate_text(&mut this, decl: u32, out: &mut string): void {
val file: u32 = this.decls.files.p[decl];
val node: u32 = this.decls.nodes.p[decl];
val syntax := &this.units[file].syntax;
val union_type := this.decls.kinds.p[decl] == dk::union_type;
out.append(union_type ? "union " : "struct ");
this.type_name_text(decl, out);
this.generics_text(file, syntax.record(node, aggregate_slot::type_params), out);
this.traits_text(decl, out);
this.where_text(file, syntax.record(node, aggregate_slot::type_params), out);
out.append(" {");
var listed: u32 = 0;
val run: u32 = this.decls.children.p[decl];
for i in 0..this.decls.list_length(run) {
val field := this.decls.list_at(run, i as u32);
continue if this.decls.kinds.p[field] != dk::field;
break if listed == checker::listed;
out.append("\n ");
if this.decls.has(field, df::exported) {
out.append("pub ");
}
this.field_text(field, this.decls.names.p[field], out);
out.append(";");
listed++;
}
out.append(listed != 0 ? "\n}" : "}");
}
#! Writes a trait with the methods it asks for.
#! @param this The checker.
#! @param decl The trait.
#! @param out Where it is written.
def checker::trait_text(&mut this, decl: u32, out: &mut string): void {
out.append("trait ");
this.type_name_text(decl, out);
val file: u32 = this.decls.files.p[decl];
val node: u32 = this.decls.nodes.p[decl];
this.generics_text(file, this.units[file].syntax.record(node, aggregate_slot::type_params), out);
this.traits_text(decl, out);
out.append(" {");
var listed: u32 = 0;
val run: u32 = this.decls.children.p[decl];
for i in 0..this.decls.list_length(run) {
val member := this.decls.list_at(run, i as u32);
continue if this.decls.kinds.p[member] != dk::function;
break if listed == checker::listed;
this.resolve(member);
out.append("\n ");
this.function_text(member, false, out);
out.append(";");
listed++;
}
out.append(listed != 0 ? "\n}" : "}");
}
#! Writes an enum with its members and their values.
#! @param this The checker.
#! @param decl The enum.
#! @param out Where it is written.
def checker::enum_text(&mut this, decl: u32, out: &mut string): void {
out.append("enum ");
this.type_name_text(decl, out);
out.append(": ");
this.type_text(this.decls.targets.p[decl], out);
out.append(" {");
var listed: u32 = 0;
val run: u32 = this.decls.children.p[decl];
for i in 0..this.decls.list_length(run) {
val member := this.decls.list_at(run, i as u32);
continue if this.decls.kinds.p[member] != dk::enum_member;
break if listed == checker::listed;
this.resolve(member);
out.append("\n ");
out.append(this.decl_text(member));
out.append(" = ");
this.value_text(member, out);
out.append(",");
listed++;
}
out.append(listed != 0 ? "\n}" : "}");
}
#! Writes the attributes written on a declaration, each on a line of its own as
#! it is written, as `@test`.
#! @param this The checker.
#! @param file The file.
#! @param node The declaration's node.
#! @param out Where they are written.
def checker::attributes_text(&this, file: u32, node: u32, out: &mut string): void {
val syntax := &this.units[file].syntax;
val run := match syntax.node_kinds[node] {
nk::function, nk::variable, nk::field, nk::property, nk::struct_decl, nk::union_decl,
nk::trait_decl, nk::assoc_type, nk::enumeration, nk::alias => syntax.record(node, decl_slot::attributes),
nk::enumerator => syntax.node_lefts[node],
else => 0
};
for i in 0..(run == 0 ? 0 : syntax.run_length(run)) {
var held := this.written(file, syntax.run_at(run, i as u32));
out.append(held.span);
out.append("\n");
}
}
#! The documentation comment of a declaration: the `#!` lines written on lines
#! of their own right above it, and a `#!` after it on its own line, without
#! their markers. A parameter or type parameter is described by its line of
#! its declaration's comment, `@param` or `@tparam`.
#! @param this The checker.
#! @param file The file.
#! @param node The node declaring it.
#! @param walked The file's tree, where it is at hand, which says what holds a
#! parameter.
#! @returns The comment, or empty text.
pub def checker::documentation_of(&this, file: u32, node: u32): string {
var made := string::new(nothing {});
return made if node == 0;
val syntax := &this.units[file].syntax;
val kind := syntax.node_kinds[node];
if kind == nk::param || kind == nk::type_param || kind == nk::const_param {
val owner := this.owner_of_param(file, node);
return made if owner == 0;
var whole := this.documentation_of(file, owner);
return this.tagged(whole.span, kind == nk::param ? "param" : "tparam", this.token_text(file, syntax.node_tokens[node]));
}
val text := this.units[file].source.make_lexeme(0, this.units[file].source.length);
val start: u32 = syntax.token_offsets[syntax.start_of(node)];
var line := start;
while line > 0 && text[(line - 1) as u64] != '\n' {
line--;
}
# The comment above a line is of the declaration it begins, as `def str::trim`,
# and not of the names declared after it on the line.
var leading := true;
for at in line..start {
val byte: u8 = text[at as u64];
leading = leading && (byte == ' ' || byte == '\t' || byte == ':' || byte == '<' || byte == '>' || byte == ','
|| byte == '&' || byte == '_' || (byte >= 'a' && byte <= 'z') || (byte >= 'A' && byte <= 'Z') || (byte >= '0' && byte <= '9'));
}
var lines := vector::<strview>::new();
var above := line;
while leading && above > 0 && lines.length < 64 {
var previous := above - 1;
while previous > 0 && text[(previous - 1) as u64] != '\n' {
previous--;
}
var at := previous;
while at < above && (text[at as u64] == ' ' || text[at as u64] == '\t') {
at++;
}
break if above - at < 3 || text[at as u64] != '#' || text[(at + 1) as u64] != '!' || text[(at + 2) as u64] == '<';
lines.append(this.comment_text(text, at + 2));
above = previous;
}
var i := lines.length;
while i > 0 {
i--;
made.append(lines[i]);
made.append("\n");
}
# A `#!` after the declaration on its own line, outside any string there.
var quote: u8 = 0;
var at: u32 = syntax.token_offsets[syntax.node_tokens[node]] + syntax.token_length(syntax.node_tokens[node], this.names);
while (at as u64) < text.length && text[at as u64] != '\n' {
val byte: u8 = text[at as u64];
if quote != 0 {
if byte == '\\' {
at++;
} else if byte == quote {
quote = 0;
}
} else if byte == '"' || byte == '\'' {
quote = byte;
} else if byte == '#' {
if at as u64 + 1 < text.length && text[(at + 1) as u64] == '!' {
val after := at as u64 + 2 < text.length && text[(at + 2) as u64] == '<' ? at + 3 : at + 2;
made.append(this.comment_text(text, after));
made.append("\n");
}
break;
}
at++;
}
while made.length != 0 && made[made.length - 1] == '\n' {
made = string::from(made.substr(0, made.length - 1), nothing {});
}
return made;
}
#! The text of a comment from after its marker to the end of its line, with one
#! space after the marker and the spacing ending the line left out.
#! @param this The checker.
#! @param text The file's text.
#! @param from Where the text after the marker begins.
#! @returns The text.
def checker::comment_text(&this, text: strview, from: u32): strview {
var start := from as u64;
var stop := start;
while stop < text.length && text[stop] != '\n' {
stop++;
}
if start < stop && text[start] == ' ' {
start++;
}
while stop > start && (text[stop - 1] == '\r' || text[stop - 1] == ' ') {
stop--;
}
return text.substr(start, stop - start);
}
#! What a documentation comment says of one of its parameters: the text after
#! `@param name`, or `@tparam name`, through the lines continuing it.
#! @param this The checker.
#! @param text The comment.
#! @param tag `param` or `tparam`.
#! @param name The parameter's name.
#! @returns The text, or empty text.
def checker::tagged(&this, text: strview, tag: strview, name: strview): string {
var made := string::new(nothing {});
var within := false;
var line: u64 = 0;
while line < text.length {
var stop := line;
while stop < text.length && text[stop] != '\n' {
stop++;
}
var at := line;
while at < stop && (text[at] == ' ' || text[at] == '\t') {
at++;
}
if at < stop && (text[at] == '@' || text[at] == '\\') {
within = false;
if stop - at > tag.length && text.substr(at + 1, tag.length) == tag {
at += 1 + tag.length;
if at < stop && text[at] == '[' {
while at < stop && text[at] != ']' {
at++;
}
at++;
}
while at < stop && (text[at] == ' ' || text[at] == '\t') {
at++;
}
val word := at;
while at < stop && is_word_byte(text[at]) {
at++;
}
if text.substr(word, at - word) == name {
while at < stop && (text[at] == ' ' || text[at] == '\t') {
at++;
}
made.append(text.substr(at, stop - at));
within = true;
}
}
} else if within && at < stop {
made.append(" ");
made.append(text.substr(at, stop - at));
}
line = stop + 1;
}
return made;
}
#! The node of the declaration a parameter or type parameter belongs to, whose
#! documentation describes it, or 0.
#! @param this The checker.
#! @param file The file.
#! @param node The `param`, `type_param`, or `const_param` node.
#! @returns The declaration's node, or 0.
def checker::owner_of_param(&this, file: u32, node: u32): u32 {
val syntax := &this.units[file].syntax;
for &id in this.decls.by_file.of(file) {
val owner: u32 = this.decls.nodes.p[id];
continue if owner == 0;
match syntax.node_kinds[owner] {
nk::function => {
return owner if this.run_holds(syntax, syntax.record(owner, function_slot::params), node)
|| this.run_holds(syntax, syntax.record(owner, function_slot::type_params), node)
|| this.receiver_holds(syntax, syntax.record(owner, function_slot::receiver), node);
}
nk::struct_decl, nk::union_decl, nk::trait_decl, nk::alias => {
return owner if this.run_holds(syntax, syntax.record(owner, decl_slot::type_params), node);
}
nk::variable, nk::property => {
return owner if this.receiver_holds(syntax, syntax.record(owner, decl_slot::receiver), node);
}
else => {}
}
}
return 0;
}
#! Whether a run of a syntax holds a node.
#! @param this The checker.
#! @param syntax The syntax.
#! @param run The run.
#! @param node The node.
#! @returns Whether it does.
def checker::run_holds(&this, syntax: &syntax, run: u32, node: u32): bool {
return false if run == 0;
for &each in syntax.run_view(run) {
return true if each == node;
}
return false;
}
#! Whether the last segment of a receiver declares a type parameter, as `T` of
#! `def box<T>::get`.
#! @param this The checker.
#! @param syntax The syntax.
#! @param receiver The receiver's `type_name` node, or 0.
#! @param node The type parameter's node.
#! @returns Whether it does.
def checker::receiver_holds(&this, syntax: &syntax, receiver: u32, node: u32): bool {
return false if receiver == 0;
val run: u32 = syntax.node_lefts[receiver];
return false if run == 0;
for &each in syntax.run_view(run) {
return true if this.run_holds(syntax, syntax.node_lefts[each], node);
}
return false;
}
+603
View File
@@ -0,0 +1,603 @@
#! @file symbols.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains what the queries an editor asks of a checked program share: what a
#! name is, as `symbol` says, where one is written, as a `spot`, the walk of a
#! file that finds what holds each node, and what the name a node writes
#! refers to.
#!
#! The queries read what checking recorded of each node, as the declaration a
#! name refers to and the type of each expression, together with the tables the
#! check built. They ask the checker nothing that reports, so a query never
#! adds a note.
import std::containers;
module sakura::analyzer;
#! What a name is, as an editor offers it to complete and colours it.
pub enum symbol : u8 {
keyword, #! A keyword, as `def`.
namespace, #! A module.
function, #! A function, or a function declared through a type.
method, #! A method, which takes `this`.
variable, #! A variable, or a local.
constant, #! A constant, or a variable that is never written, declared outside any function.
parameter, #! A parameter.
field, #! A field, or a property.
struct_type, #! A struct or union.
trait_type, #! A trait.
enum_type, #! An enum.
enum_member, #! A member of an enum.
type_name, #! Any other type, as a built-in one or an alias.
type_param, #! A type parameter.
}
#! Where a name is written: its file, and the bytes of its text it covers.
pub struct spot {
pub var file: u32; #! The index of the file.
pub var offset: u32; #! Where the name begins in the file's text.
pub var length: u32; #! How many bytes it covers.
}
#! What a name refers to: a declaration, a local, or a module.
struct checker::meant {
var decl: u32; #! The declaration, or 0.
var file: u32; #! The file of the local.
var local: u32; #! The node declaring the local, as a `variable` or `param`, or 0.
var named_module: u32; #! The module, where `is_module` says it is one.
var is_module: bool; #! Whether it is a module.
}
#! Whether a name refers to anything.
#! @param this What it refers to.
#! @returns Whether it does.
def checker::meant::found(&this): bool {
return this.decl != 0 || this.local != 0 || this.is_module;
}
#! Whether two names refer to the same thing.
#! @param this What one refers to.
#! @param other What the other refers to.
#! @returns Whether they do.
def checker::meant::same(&this, other: &checker::meant): bool {
return this.decl == other.decl && this.local == other.local && this.file == other.file
&& this.is_module == other.is_module && this.named_module == other.named_module;
}
#! The nodes of a file a walk from its `document` meets, each before the nodes
#! it holds, and the node holding each. The nodes a reparse left behind are no
#! longer in the tree, and the walk never meets them.
struct tree_walk {
var order: vector<u32>; #! The nodes, in the order the walk meets them.
var parents: vector<u32>; #! The node holding each, by its index, or 0.
}
#! The tree of the file, walked from its `document`.
#! @param this The checker.
#! @param file The file.
#! @returns The tree.
def checker::tree_of(&this, file: u32): tree_walk {
val syntax := &this.units[file].syntax;
var made := tree_walk { order = vector::<u32>::new(), parents = vector::<u32>::new() };
made.parents.resize(syntax.node_count);
var todo := vector::<u32>::new();
var held := vector::<u32>::new();
todo.append(this.units[file].root);
while todo.length != 0 {
val at: u32 = todo.last;
todo.truncate();
made.order.append(at);
held.clear();
syntax.children(at, &mut held);
var i := held.length;
while i > 0 {
i--;
val child: u32 = held[i];
continue if child as u64 >= syntax.node_count || made.parents[child] != 0;
made.parents[child] = at;
todo.append(child);
}
}
return made;
}
#! The token of the file whose text holds the offset, or the count of its
#! tokens where none does, as between two.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @returns The token.
def checker::token_at(&this, file: u32, offset: u32): u32 {
val syntax := &this.units[file].syntax;
val count := syntax.token_count as u32;
# The token before the first beginning past the offset is the one that may
# hold it.
val after := syntax.token_offsets.span.upper_bound(&offset) as u32;
return count if after == 0;
val token := after - 1;
val start: u32 = syntax.token_offsets[token];
return token if offset < start + syntax.token_length(token, this.names);
return count;
}
#! Where the token is written.
#! @param this The checker.
#! @param file The file.
#! @param token The token.
#! @returns Its spot.
def checker::spot_of(&this, file: u32, token: u32): spot {
val syntax := &this.units[file].syntax;
return spot { file = file, offset = syntax.token_offsets[token], length = syntax.token_length(token, this.names) };
}
#! The text of a token of a file.
#! @param this The checker.
#! @param file The file.
#! @param token The token.
#! @returns The text, which views the file's source.
def checker::token_text(&this, file: u32, token: u32): strview {
val syntax := &this.units[file].syntax;
return this.units[file].source.make_lexeme(syntax.token_offsets[token], syntax.token_length(token, this.names));
}
#! The text a node of a file is written as, from its first token through its
#! last, with each run of spacing made one space, as `std::list<T>`.
#! @param this The checker.
#! @param file The file.
#! @param node The node.
#! @returns The text.
def checker::written(&this, file: u32, node: u32): string {
val syntax := &this.units[file].syntax;
var made := string::new(nothing {});
return made if node == 0;
val first: u32 = syntax.node_tokens[node];
var last: u32 = syntax.node_end(node);
last = last < first ? first : last;
val start: u32 = syntax.token_offsets[first];
val end: u32 = syntax.token_offsets[last] + syntax.token_length(last, this.names);
val text := this.units[file].source.make_lexeme(start, end - start);
var spaced := false;
for i in 0..text.length {
val byte: u8 = text[i];
if byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r' {
spaced = true;
continue;
}
if spaced && made.length != 0 {
made.push(' ');
}
spaced = false;
made.push(byte);
}
return made;
}
#! A query of a file under way: where checking was before it began, the walk
#! of the file's tree, and the declarations of the file's nodes. It closes
#! where its scope ends, however the query returns, which puts back where
#! checking was.
struct checker::query : owning {
var checker: &mut checker; #! The checker queried.
var saved: checker::frame; #! Where checking was, which closing puts back.
var walked: tree_walk; #! The file's tree.
var decls: dict<u32, u32>; #! The declarations each node of the file declares, by the node.
var open: bool; #! Whether it is yet to close.
}
#! Begins a query of the file: its results are what is read, nothing the
#! checker works out depends on the query, and nothing it reports is noted,
#! until the query closes.
#! @param this The checker.
#! @param file The file.
#! @returns The query, holding the file's tree and the declarations of its
#! nodes.
def checker::begin_query(&mut this, file: u32): checker::query {
val saved := this.at;
this.use_table(this.units[file].table);
this.at.decl = 0;
this.at.instance = 0;
this.at.scope = 0;
this.at.home = 0;
this.quiet++;
this.owned_names = true;
return checker::query {
checker = this,
saved = saved,
walked = this.tree_of(file),
decls = this.decls_in(file),
open = true
};
}
#! Ends the query, putting back where checking was, once.
#! @param this The query.
def checker::query::close(&mut this): void {
return if !this.open;
this.open = false;
this.checker.quiet--;
this.checker.owned_names = this.checker.quiet != 0 && this.checker.owned_names;
this.checker.at = this.saved;
close(&mut this.walked);
close(&mut this.decls);
}
#! Moves the query to another file, whose results are read from then on.
#! @param this The checker.
#! @param file The file.
def checker::query_file(&mut this, file: u32): void {
this.use_table(this.units[file].table);
this.at.decl = 0;
this.at.instance = 0;
}
#! The module the declarations a file makes before the offset are in: that of
#! the last `module` before it, or the program's own.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @returns The module.
def checker::home_at(&mut this, file: u32, offset: u32): u32 {
val syntax := &this.units[file].syntax;
val decls := syntax.node_lefts[this.units[file].root];
var home: u32 = 0;
for &node in syntax.run_view(decls) {
break if syntax.token_offsets[syntax.node_tokens[node]] > offset;
continue if syntax.node_kinds[node] != nk::module_decl && syntax.node_kinds[node] != nk::extern_module;
val found := this.module_named(file, syntax.node_lefts[node], 0xFFFFFFFF);
if found != 0 {
home = found;
}
}
return home;
}
#! The module a `path` of a file names from the program's own, through its
#! segment at `last`, or 0 where it names none.
#! @param this The checker.
#! @param file The file.
#! @param path The `path` node.
#! @param last The position of the last segment read, or more for all.
#! @returns The module, or 0.
def checker::module_named(&this, file: u32, path: u32, last: u32): u32 {
val syntax := &this.units[file].syntax;
val run: u32 = syntax.node_lefts[path];
if run == 0 {
return this.modules.find(0, syntax.token_names[syntax.node_tokens[path]]);
}
var at: u32 = 0;
for i in 0..syntax.run_length(run) {
break if i as u32 > last;
val segment := syntax.run_at(run, i as u32);
at = this.modules.find(at, syntax.token_names[syntax.node_tokens[segment]]);
return 0 if at == 0;
}
return at;
}
#! The declarations each node of a file declares, by the node.
#! @param this The checker.
#! @param file The file.
#! @returns The declarations, by their nodes.
def checker::decls_in(&this, file: u32): dict<u32, u32> {
var made := dict::<u32, u32>::new();
for &id in this.decls.by_file.of(file) {
continue if this.decls.kinds.p[id] == dk::none || this.decls.kinds.p[id] == dk::builtin;
made.insert(this.decls.nodes.p[id], id);
}
return made;
}
#! What a declaration a name refers to stands for: a property for its accessors,
#! which are renamed and shown as one.
#! @param this The checker.
#! @param decl The declaration.
#! @returns What it stands for.
def checker::standing_for(&this, decl: u32): u32 {
if decl != 0 && this.decls.kinds.p[decl] == dk::function && this.decls.has(decl, df::accessor) {
return this.decls.owners.p[decl];
}
return decl;
}
#! What a reference a node records refers to.
#! @param this The checker.
#! @param ref The reference, as `ref_at` gives it.
#! @returns What it refers to.
def checker::meant_by(&this, ref: u32): checker::meant {
return checker::meant {} if ref == 0;
if (ref & ref_local) != 0 {
return checker::meant { file = this.at.file, local = ref & ref_mask };
}
if (ref & ref_type) != 0 {
# A pack whose types are not known is of the pack parameter it is.
val kind := ref & ref_mask;
val decl := this.type_decl(kind);
if decl == 0 && this.types.kind_of(kind) == ty::pack && this.types.part_count(kind) == 0 {
return checker::meant { decl = this.types.first_of(kind) };
}
return checker::meant { decl = decl };
}
return checker::meant {} if ref >= this.decls.count as u32;
return checker::meant { decl = this.standing_for(ref) };
}
#! What the name a node of the file being queried writes refers to, or nothing
#! where it writes none, or one checking left unresolved.
#! @param this The checker.
#! @param node The node.
#! @param walked The file's tree.
#! @param decls The declarations of the file's nodes.
#! @returns What it refers to.
def checker::meaning_of(&mut this, node: u32, walked: &tree_walk, decls: &dict<u32, u32>): checker::meant {
val parent: u32 = walked.parents[node];
match this.kind_of(node) {
nk::name => {
val ref := this.ref_at(node);
return this.meant_by(ref) if ref != 0;
# A name a pattern binds, or a loop or `else` names, declares it.
if this.has_flag(node, rf::local) {
return checker::meant { file = this.at.file, local = node };
}
# A field a pattern names, as `number` of `number(n)`, which the pattern's
# call refers to.
if parent != 0 && this.kind_of(parent) == nk::call && this.left_of(parent) == node {
val held := this.ref_at(parent);
if held != 0 && (held & (ref_local | ref_type)) == 0 && held < this.decls.count as u32
&& this.decls.kinds.p[held] == dk::field {
return checker::meant { decl = held };
}
}
return checker::meant {};
}
nk::segment => return this.segment_meaning(parent, node, walked, decls);
nk::type_name, nk::path => {
# A name of one segment is its own segment, and one of more is its
# segments', which the walk meets after it.
return checker::meant {} if this.left_of(node) != 0;
return this.segment_meaning(node, node, walked, decls);
}
nk::member, nk::member_of => {
val ref := this.ref_at(node);
return this.meant_by(ref) if ref != 0;
return checker::meant {} if this.kind_of(node) == nk::member_of;
return this.member_meaning(this.type_at(this.left_of(node)), this.name_of(node));
}
nk::field_init => {
return checker::meant {} if !this.node_has(node, nf::named);
return this.meant_by(this.ref_at(node));
}
nk::rename => {
# The name a spread renames is a field of the struct spread.
return checker::meant {} if parent == 0 || this.kind_of(parent) != nk::spread;
val spread := this.type_name_meaning(this.left_of(parent));
return checker::meant {} if spread.decl == 0;
return this.member_meaning(this.decls.types.p[spread.decl], this.name_of(node));
}
nk::function, nk::struct_decl, nk::union_decl, nk::trait_decl, nk::alias, nk::enumeration,
nk::enumerator, nk::field, nk::property, nk::assoc_type, nk::type_param, nk::const_param => {
if decls.get(&node) is some(found) {
return checker::meant { decl = this.standing_for(found) };
}
return checker::meant {};
}
nk::variable => {
if decls.get(&node) is some(found) {
return checker::meant { decl = found };
}
return checker::meant { file = this.at.file, local = node } if this.has_flag(node, rf::local);
return checker::meant {};
}
nk::param => {
# A parameter an accessor implies has no name of its own written, and its
# token is the `get` or `set`.
return checker::meant {} if this.node_has(node, nf::implied);
# A parameter of a function without a body is no local, but is still
# the function's.
return checker::meant { file = this.at.file, local = node };
}
else => return checker::meant {};
}
return checker::meant {};
}
#! What a segment of a `type_name`, `path`, `import`, or `module` names: the
#! last what the whole names, and those before it the modules and types it is
#! within.
#! @param this The checker.
#! @param holder The `type_name` or `path` node holding the segment.
#! @param segment The segment, which is the holder itself where it has one.
#! @param walked The file's tree.
#! @param decls The declarations of the file's nodes.
#! @returns What it names.
def checker::segment_meaning(&mut this, holder: u32, segment: u32, walked: &tree_walk, decls: &dict<u32, u32>): checker::meant {
return checker::meant {} if holder == 0;
val ref := this.ref_at(segment);
if ref != 0 {
return this.meant_by(ref);
}
# The position of the segment in the holder.
val run := this.left_of(holder);
var index: u32 = 0;
for i in 0..(run == 0 ? 0 : this.run_length(run)) {
if this.run_at(run, i as u32) == segment {
index = i as u32;
}
}
val last := run == 0 || index + 1 == this.run_length(run);
# A path an `import` or `module` names is of modules alone.
val outer: u32 = walked.parents[holder];
if outer != 0 && (this.kind_of(outer) == nk::import_decl || this.kind_of(outer) == nk::module_decl
|| this.kind_of(outer) == nk::extern_module) {
val found := this.module_named(this.at.file, holder, index);
return checker::meant { named_module = found, is_module = true } if found != 0;
return checker::meant {};
}
# The type a declaration is made through, as `rect` of `def rect::area`, is
# what it is a member of, and a segment before the last, what that is
# declared through in turn.
if outer != 0 && this.is_receiving(outer) && this.record(outer, decl_slot::receiver) == holder {
if decls.get(&outer) is some(found) {
var at: u32 = this.decls.owners.p[found];
if this.decls.kinds.p[at] == dk::property {
at = this.decls.owners.p[at];
}
val count := run == 0 ? 1 : this.run_length(run);
var steps := count - 1 - index;
while steps > 0 && at != 0 {
at = this.decls.owners.p[at];
steps--;
}
return checker::meant { decl = at } if at != 0;
}
}
# A whole path refers to what its node says, and its last segment with it.
if last && segment != holder {
val whole := this.ref_at(holder);
if whole != 0 {
return this.meant_by(whole);
}
}
# After `is`, a name that is no type is the field of the union tested, which
# the test refers to.
if outer != 0 && this.kind_of(outer) == nk::is_test && this.record(outer, is_slot::target) == holder && index == 0 {
val held := this.ref_at(outer);
if held != 0 && (held & (ref_local | ref_type)) == 0 && held < this.decls.count as u32 {
return checker::meant { decl = held };
}
}
# A segment naming no declaration may name a module, from where the file is.
if !last || run != 0 {
val name := this.name_of(segment);
var home := this.home_at(this.at.file, this.units[this.at.file].syntax.token_offsets[this.token_of(segment)]);
if index == 0 {
val saved := this.at.home;
this.at.home = home;
val found := this.lookup_module(name);
this.at.home = saved;
return checker::meant { named_module = found, is_module = true } if found != 0;
return checker::meant {};
}
val before := this.segment_meaning(holder, this.run_at(run, index - 1), walked, decls);
if before.is_module {
val found := this.modules.find(before.named_module, name);
return checker::meant { named_module = found, is_module = true } if found != 0;
}
}
return checker::meant {};
}
#! What a `type_name` names, as its last segment refers to it.
#! @param this The checker.
#! @param node The `type_name` node.
#! @returns What it names.
def checker::type_name_meaning(&mut this, node: u32): checker::meant {
return checker::meant {} if node == 0 || this.kind_of(node) != nk::type_name;
val run := this.left_of(node);
val last := run == 0 ? node : this.run_at(run, this.run_length(run) - 1);
return this.meant_by(this.ref_at(last));
}
#! Whether a node declares what may be made through a type, as `rect` of
#! `def rect::area`, which its record's second slot names.
#! @param this The checker.
#! @param node The node.
#! @returns Whether it does.
def checker::is_receiving(&this, node: u32): bool {
return match this.kind_of(node) {
nk::function, nk::variable, nk::property, nk::struct_decl, nk::union_decl, nk::trait_decl,
nk::enumeration, nk::alias => true,
else => false
};
}
#! The member of a type a name reaches after `.`, as a field, a property, or a
#! method, or nothing where the type has none of the name.
#! @param this The checker.
#! @param kind The type of the value.
#! @param name The ID of the name.
#! @returns What the name refers to.
def checker::member_meaning(&mut this, kind: u32, name: u32): checker::meant {
return checker::meant {} if kind == 0 || kind == bt::error;
val found := this.find_member(this.reached(kind), name);
return checker::meant {} if found.decl == 0;
return checker::meant { decl = this.standing_for(found.decl) };
}
#! What a declaration is, as `symbol` says.
#! @param this The checker.
#! @param decl The declaration.
#! @returns What it is.
def checker::symbol_of(&mut this, decl: u32): symbol {
val kind: dk = this.decls.kinds.p[decl];
if kind == dk::function {
# A property is read and written by its name alone, so it is offered and
# coloured as a field is rather than as the methods its accessors are.
return symbol::field if this.decls.has(decl, df::accessor);
return this.decls.owners.p[decl] != 0 && this.decls.has(decl, df::method) ? symbol::method : symbol::function;
}
if kind == dk::type_param {
return this.decls.has(decl, df::valued) ? symbol::constant : symbol::type_param;
}
if kind == dk::variable {
return this.decls.has(decl, df::mutable) ? symbol::variable : symbol::constant;
}
return match kind {
dk::property => symbol::field,
dk::field => symbol::field,
dk::attribute => symbol::constant,
dk::enum_member => symbol::enum_member,
dk::struct_type, dk::union_type => symbol::struct_type,
dk::trait_type => symbol::trait_type,
dk::enum_type => symbol::enum_type,
else => symbol::type_name
};
}
#! What a name refers to is, as `symbol` says.
#! @param this The checker.
#! @param meant What it refers to.
#! @returns What it is.
def checker::symbol_meant(&mut this, meant: &checker::meant): symbol {
return symbol::namespace if meant.is_module;
return this.symbol_of(meant.decl) if meant.decl != 0;
val kind := this.units[meant.file].syntax.node_kinds[meant.local];
return kind == nk::param ? symbol::parameter : symbol::variable;
}
#! Whether what a name refers to is never written, as a `val`, a `const`, or a
#! member of an enum.
#! @param this The checker.
#! @param meant What it refers to.
#! @returns Whether it is.
def checker::readonly_meant(&mut this, meant: &checker::meant): bool {
return true if meant.is_module;
if meant.decl != 0 {
return match this.decls.kinds.p[meant.decl] {
dk::variable, dk::field, dk::attribute => !this.decls.has(meant.decl, df::mutable),
dk::enum_member => true,
dk::property => this.property_setter(meant.decl) == 0,
dk::type_param => true,
else => false
};
}
# A parameter is a binding the body writes, so it is not marked read-only.
val syntax := &this.units[meant.file].syntax;
return false if syntax.node_kinds[meant.local] == nk::param;
val saved := this.at;
this.use_table(this.units[meant.file].table);
val writes := this.has_flag(meant.local, rf::mutable);
this.at = saved;
return !writes;
}