Files
2026-11-19 04:54:17 -06:00

830 lines
30 KiB
Plaintext

#! @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;
}