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

+858
View File
@@ -0,0 +1,858 @@
#! @file answers.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the jobs a process of the server's works out from the files of the
#! workspace, and how each is answered: the diagnostics of every file, what the
#! name at a position is, the names that may be written there, what is being
#! called there, the names of a file as what they are, and where what a name
#! refers to is declared and written. Each answer is the text the server shapes
#! into what it sends the client.
import std::net;
import std::json;
import std::serial;
import sakura::analyzer;
import sakura::machine;
module sakura::server;
#! What a process of its own is asked to work out.
pub enum task : u8 {
check, #! The diagnostics of every file.
hover, #! What the name at a position is.
complete, #! The names that may be written at a position.
call, #! What is being called at a position, while its arguments are written.
tokens, #! The names of a file, as the checker made them.
name, #! What the name at a position refers to, and where it is declared.
refer, #! Everywhere what the name at a position refers to is written.
}
#! What is asked of a process.
pub struct job {
pub var task: task; #! What is asked.
pub var strict: bool; #! Whether files with mistakes of syntax are left out, as a check without drafts does.
pub var path: string; #! The file asked about, or empty.
pub var line: u32; #! The line of the position asked about, from 0.
pub var character: u32; #! Its UTF-16 code unit within the line, from 0.
pub var touched: string; #! The file last opened, edited, or asked about, or empty.
pub var entry: string; #! The file declaring `main` the last job was answered from, or empty.
}
#! What the server's processes work with: the program, its checker, and what
#! names the files of the editor's documents.
pub struct workplace {
pub var held: &mut program; #! The program's files, parsed.
pub var checker: &mut checker; #! Its checker.
pub var documents: &vector<document>; #! The documents open in the editor.
}
#! Written after a `.` being completed, so the value before it parses as one.
pub const placeholder: strview = "sakura__completion";
#! Right after `.`, a member name must follow for the value before it to parse,
#! so one is written into the text of the file being completed.
#! @param files The files, as gathered.
#! @param asked The job.
pub def write_placeholder(files: &mut vector<gathered>, asked: &job): void {
return if asked.task != task::complete;
for i in 0..files.length {
continue if files[i].path.span != asked.path.span;
val text := files[i].text.span;
val offset := locate(text, asked.line as u64, asked.character as u64);
val start := word_start(text, offset);
return if start != offset || start == 0 || text[(start - 1) as u64] != '.'
|| (start >= 2 && text[(start - 2) as u64] == '.');
var written := string::from(text.substr(0, offset as u64), nothing {});
written.append(placeholder);
written.append(text.substr(offset as u64, text.length - offset as u64));
files[i].text = move(&mut written);
return;
}
}
#! Where the word a byte of the text ends, or is within, begins.
#! @param text The text.
#! @param offset The byte.
#! @returns The byte the word begins at, which is the byte itself where no word
#! ends there.
def word_start(text: strview, offset: u32): u32 {
var start := offset;
while start > 0 && is_word_byte(text[(start - 1) as u64]) {
start--;
}
return start;
}
#! Answers a job from a checked program. The path of the file declaring `main`
#! the program begins with comes first, ended by a zero byte, so the server knows
#! which program answered.
#! @param place What the process works with.
#! @param asked The job.
#! @param out Where the answer is written.
pub def answer(place: &mut workplace, asked: &job, out: &mut string): void {
if place.held.entry != none {
out.append(place.held.paths[place.held.entry].span);
}
out.push(0);
match asked.task {
task::check => answer_check(place, out);
task::hover => answer_hover(place, asked, out);
task::complete => answer_complete(place, asked, out);
task::call => answer_call(place, asked, out);
task::tokens => answer_tokens(place, asked, out);
task::name => answer_name(place, asked, out);
task::refer => answer_refer(place, asked, out);
}
}
#! The URI a file is named by: the one the editor gave it where it is open,
#! which may be spelled other than as its path is, and else its path's.
#! @param place What the process works with.
#! @param path The file's path.
#! @param out Where the URI is written, as a JSON string.
def write_file_uri(place: &workplace, path: strview, out: &mut json::writer): void {
# The types the syntax makes are shown from a file of their own.
if path == builtin_path {
val shown := builtin_document();
if shown.length != 0 {
var uri := string::new(nothing {});
append_uri(&mut uri, shown.span);
out.text(uri.span);
return;
}
}
for &each in place.documents {
if each.path.span == path {
out.text(each.uri.span);
return;
}
}
var uri := string::new(nothing {});
append_uri(&mut uri, path);
out.text(uri.span);
}
#! Appends the URI a file's line of a check begins with: the one the editor gave
#! it where it is open, which may be spelled other than as its path is, unless
#! a space or a line feed in it would end it early, and else its path's.
#! @param place What the process works with.
#! @param path The file's path.
#! @param out Where the URI is written.
def append_line_uri(place: &workplace, path: strview, out: &mut string): void {
for &each in place.documents {
continue if each.path.span != path;
var plain := true;
for i in 0..each.uri.length {
plain = plain && each.uri[i] != ' ' && each.uri[i] != '\n';
}
if plain {
out.append(each.uri.span);
return;
}
break;
}
append_uri(out, path);
}
#! Writes a place a name is written as the protocol writes a location.
#! @param place What the process works with.
#! @param file The file, as the program counts them.
#! @param start The byte the name begins at.
#! @param end The byte it ends before.
#! @param out Where it is written.
def write_location(place: &workplace, file: u32, start: u32, end: u32, out: &mut json::writer): void {
out.begin_object();
out.member("uri");
write_file_uri(place, place.held.paths[file].span, out);
out.member("range");
write_range(out, place.held.source_text(file), &place.held.sources[file].lines, start, end);
out.end_object();
}
#! Writes a place the checker gave, by the file of its own count.
#! @param place What the process works with.
#! @param at The place.
#! @param out Where it is written.
def write_spot(place: &workplace, at: &spot, out: &mut json::writer): void {
val file: u32 = place.held.files[at.file];
write_location(place, file, at.offset, at.offset + at.length, out);
}
#! The file a job asks about, as the program holds it, and the position asked
#! about within it.
struct asked_file {
var file: u32; #! The file, as the program counts them.
var unit: u32; #! The checker's index of it.
var text: strview; #! Its text, as its source holds it.
var offset: u32; #! The byte of the text the position is at.
}
#! Finds the file a job asks about, and the position within it.
#! @param place What the process works with.
#! @param asked The job.
#! @returns The file, or nothing where the program does not check it.
def open_asked(place: &workplace, asked: &job): maybe<asked_file> {
val file := place.held.file_of(asked.path.span);
return nothing {} if file == none || place.held.units[file] == none;
val text := place.held.source_text(file);
return asked_file {
file = file,
unit = place.held.units[file],
text = text,
offset = locate(text, asked.line as u64, asked.character as u64)
};
}
#! Writes a line for each file with notes: its URI, as `append_line_uri` gives
#! it, a space, and the JSON array of its diagnostics. A file with mistakes of
#! syntax only shows those, since what the checker says of a file it could not
#! wholly read would mislead.
#! @param place What the process works with.
#! @param out Where the answer is written.
def answer_check(place: &mut workplace, out: &mut string): void {
# The program is checked whole, as `sakura check` would check it, so what is
# shown is what it would say. What only running gives is evaluated as
# checking asks for it, which notes what goes wrong as checking does, unless
# a file does not parse, as a program that does not is not run.
var notes := vector::<note>::new();
var whole := checker::new(&mut place.held.names, &mut notes);
place.held.give_whole(&mut whole);
var clean := true;
for &file in place.held.files {
clean = clean && !place.held.broken(file);
}
_ = check_evaluating(&mut whole, clean);
var diagnostics := message_writer();
for i in 0..place.held.paths.length {
val file := i as u32;
val broken := place.held.broken(file);
var found: u32 = 0;
diagnostics.clear();
diagnostics.begin_array();
for &each in place.held.parse_notes[i] {
write_diagnostic(place, file, &each, &mut diagnostics);
found++;
}
val unit: u32 = place.held.units[i];
if !broken && unit != none {
for &each in notes {
continue if each.file != unit;
write_diagnostic(place, file, &each, &mut diagnostics);
found++;
}
}
diagnostics.end_array();
continue if found == 0;
append_line_uri(place, place.held.paths[i].span, out);
out.push(' ');
append_written(out, &mut diagnostics);
out.push('\n');
}
}
#! What a note says: its message, then the instance of a generic it was found
#! in, where it was found in one, as `... (box<i64>)`.
#! @param each The note.
#! @param held The source of its file.
#! @returns The text.
pub def note_text(each: &note, held: &source): string {
var text := each.message(held);
if each.instance.length != 0 {
text.append(" (");
text.append(each.instance.span);
text.append(")");
}
return text;
}
#! Writes a note as a diagnostic of the protocol: the range of what it is
#! about, which marks the character it is at where it is about no text, its
#! message, with the instance of a generic it was found in, and the place it
#! refers to besides its own.
#! @param place What the process works with.
#! @param file The file it is of.
#! @param each The note.
#! @param out Where it is written.
def write_diagnostic(place: &workplace, file: u32, each: &note, out: &mut json::writer): void {
val text := place.held.source_text(file);
val start := each.offset;
var end := start + each.length;
if end == start && (start as u64) < text.length && text[start as u64] != '\n' {
val size := serial::utf8_length(text, start as u64);
end += size != 0 ? size as u32 : 1;
}
if end as u64 > text.length {
end = text.length as u32;
}
val message := note_text(each, &place.held.sources[file]);
val code := $"{each.code}";
out.begin_object();
out.member("range");
write_range(out, text, &place.held.sources[file].lines, start, end);
out.member("severity"); out.unsigned(each.warning ? 2 : 1);
out.member("source"); out.text("sakura");
out.member("code"); out.text(code.span);
out.member("message"); out.text(message.span);
if each.related.length != 0 && place.held.units[file] != none {
val other: u32 = place.held.files[each.related_file];
out.member("relatedInformation");
out.begin_array();
out.begin_object();
out.member("location");
write_location(place, other, each.related_offset, each.related_offset + each.related_length, out);
out.member("message");
out.text(each.related.span);
out.end_object();
out.end_array();
}
out.end_object();
}
#! Writes the hover of the name at the position, or nothing where there is none.
#! @param place What the process works with.
#! @param asked The job.
#! @param out Where the answer is written.
def answer_hover(place: &mut workplace, asked: &job, out: &mut string): void {
val opened := open_asked(place, asked) else { return; };
var shown := place.checker.hover(opened.unit, opened.offset) else { return; };
var markdown := string::new(nothing {});
append_code(&mut markdown, shown.signature.span);
if shown.documentation.length != 0 {
markdown.append("\n\n");
append_markdown(&mut markdown, shown.documentation.span);
}
var made := message_writer();
made.begin_object();
made.member("contents");
write_markdown(&mut made, markdown.span);
made.member("range");
write_range(&mut made, opened.text, &place.held.sources[opened.file].lines, shown.offset, shown.offset + shown.length);
made.end_object();
append_written(out, &mut made);
}
#! The kind of completion item of the protocol each kind of name is.
#! @param kind What the name is.
#! @returns The item's kind.
def item_kind(kind: symbol): u32 {
return match kind {
symbol::keyword => 14,
symbol::namespace => 9,
symbol::function => 3,
symbol::method => 2,
symbol::variable => 6,
symbol::constant => 21,
symbol::parameter => 6,
symbol::field => 5,
symbol::struct_type => 22,
symbol::trait_type => 8,
symbol::enum_type => 13,
symbol::enum_member => 20,
symbol::type_name => 7,
symbol::type_param => 25
};
}
#! Writes what an item shows beside its name in the list, read from the first
#! line of its signature, as its member `labelDetails`: the parameters of a
#! function, as `(&this, by: int)`, with what it gives, and the type of anything
#! else, as `int` of `val count: int`.
#! @param item The item.
#! @param called Whether the names follow `value.`, where a method's receiver
#! is the value, so it is not shown.
#! @param out Where it is written, within the item.
def write_label_details(item: &completion, called: bool, out: &mut json::writer): void {
val function := item.kind == symbol::function || item.kind == symbol::method;
val value := item.kind == symbol::variable || item.kind == symbol::constant || item.kind == symbol::parameter
|| item.kind == symbol::field;
return if !function && !value;
var signature := item.signature.span;
for i in 0..signature.length {
if signature[i] == '\n' {
signature = signature.substr(0, i);
break;
}
}
val name := item.name.span;
# The name as the signature declares it, followed by what describes it.
var after := signature.length + 1;
var at: u64 = 0;
while at + name.length <= signature.length && after > signature.length {
val next := at + name.length;
if signature.substr(at, name.length) == name && (at == 0 || !is_word_byte(signature[at - 1]))
&& next < signature.length && (signature[next] == '(' || signature[next] == '<' || signature[next] == ':') {
after = next;
}
at++;
}
return if after > signature.length;
val rest := signature.substr(after, signature.length - after);
if rest[0] == ':' && value {
val skip: u64 = rest.length > 1 && rest[1] == ' ' ? 2 : 1;
out.member("labelDetails");
out.begin_object();
out.member("description");
out.text(rest.substr(skip, rest.length - skip));
out.end_object();
return;
}
# The parameters end at the `)` closing the first `(`.
var depth := 0;
var close := rest.length;
for i in 0..rest.length {
if rest[i] == '(' {
depth++;
} else if rest[i] == ')' && depth > 0 {
depth--;
if depth == 0 {
close = i;
break;
}
}
}
return if close == rest.length || !function;
# Called on a value after `.`, a method is given its receiver, so it is not shown.
var shown := string::new(nothing {});
var open: u64 = 0;
while open < close && rest[open] != '(' {
open++;
}
if called && item.kind == symbol::method && open + 5 <= close && rest.substr(open + 1, 4) == "this" {
var past := open + 5;
while past < close && rest[past] != ',' && rest[past] != ')' {
past++;
}
if past < close && rest[past] == ',' {
past++;
}
while past < close && rest[past] == ' ' {
past++;
}
shown.append(rest.substr(0, open + 1));
shown.append(rest.substr(past, close + 1 - past));
} else {
shown.append(rest.substr(0, close + 1));
}
out.member("labelDetails");
out.begin_object();
out.member("detail");
out.text(shown.span);
var gives := rest.substr(close + 1, rest.length - close - 1);
if gives.length > 2 && gives[0] == ':' && gives[1] == ' ' {
gives = gives.substr(2, gives.length - 2);
# What a function gives ends where a `where` clause bounding it begins.
for i in 0..gives.length {
if gives.substr(i, gives.length - i).starts_with(" where ") {
gives = gives.substr(0, i);
break;
}
}
out.member("description");
out.text(gives);
}
out.end_object();
}
#! Writes the names that may be written at the position, in the order found, so
#! locals come before what is declared further away. The answer is the items,
#! a zero byte, then the documentation of each item in turn, each ended by a
#! zero byte, which the server keeps to give the one item the editor asks to
#! resolve rather than sending all of it with the list.
#! @param place What the process works with.
#! @param asked The job.
#! @param out Where the answer is written.
def answer_complete(place: &mut workplace, asked: &job, out: &mut string): void {
val opened := open_asked(place, asked) else { return; };
val items := place.checker.complete(opened.unit, opened.offset);
# Whether the names follow `value.`, where a method's receiver is the value.
val start := word_start(opened.text, opened.offset);
val called := start > 0 && opened.text[(start - 1) as u64] == '.';
var documentation := string::new(nothing {});
var made := message_writer();
made.begin_object();
made.member("isIncomplete"); made.boolean(false);
made.member("items");
made.begin_array();
for i in 0..items.length {
val item := &items[i];
made.begin_object();
made.member("label"); made.text(item.name.span);
made.member("kind"); made.unsigned(item_kind(item.kind) as u64);
val order := $"{i}";
var sorted := string::new(nothing {});
for _ in order.length..5 {
sorted.push('0');
}
sorted.append(order.span);
made.member("sortText"); made.text(sorted.span);
if item.insert.length != 0 {
made.member("insertText"); made.text(item.insert.span);
}
write_label_details(item, called, &mut made);
# The detail is a line, so a struct's signature shows whole in the
# documentation.
val signature := item.signature.span;
var newline := signature.length;
for k in 0..signature.length {
if signature[k] == '\n' {
newline = k;
break;
}
}
if signature.length != 0 {
made.member("detail"); made.text(signature.substr(0, newline));
}
var markdown := string::new(nothing {});
if newline < signature.length {
append_code(&mut markdown, signature);
}
if item.documentation.length != 0 {
if newline < signature.length {
markdown.append("\n\n");
}
append_markdown(&mut markdown, item.documentation.span);
}
if markdown.length != 0 {
made.member("data"); made.unsigned(i);
}
documentation.append(markdown.span);
documentation.push(0);
made.end_object();
}
made.end_array();
made.end_object();
append_written(out, &mut made);
out.push(0);
out.append(documentation.span);
}
#! Writes what is called at the position: its overloads, each with the
#! parameters of its signature marked, and which parameter is being written.
#! @param place What the process works with.
#! @param asked The job.
#! @param out Where the answer is written.
def answer_call(place: &mut workplace, asked: &job, out: &mut string): void {
val opened := open_asked(place, asked) else { return; };
var help := place.checker.calls_at(opened.unit, opened.offset) else { return; };
var made := message_writer();
made.begin_object();
made.member("signatures");
made.begin_array();
for &each in help.overloads {
made.begin_object();
made.member("label"); made.text(each.signature.span);
# The contract its body begins with comes before what is written of it.
if each.documentation.length != 0 || each.contract.length != 0 {
var markdown := string::new(nothing {});
if each.contract.length != 0 {
append_code(&mut markdown, each.contract.span);
}
if each.documentation.length != 0 {
if markdown.length != 0 {
markdown.append("\n\n");
}
append_markdown(&mut markdown, each.documentation.span);
}
made.member("documentation");
write_markdown(&mut made, markdown.span);
}
# Each parameter is a range of the label, which the editor marks in place.
made.member("parameters");
made.begin_array();
val label := each.signature.span;
var begin: u64 = 0;
while begin < label.length && label[begin] != '(' {
begin++;
}
var from := begin + 1;
var nested := 0;
for at in (begin + 1)..label.length {
val byte: u8 = label[at];
if byte == '(' || byte == '[' || byte == '<' {
nested++;
} else if (byte == ')' || byte == ']' || byte == '>') && nested > 0 {
nested--;
continue;
}
continue if nested > 0 || (byte != ',' && byte != ')');
while from < at && label[from] == ' ' {
from++;
}
if at > from {
made.begin_object();
made.member("label");
made.begin_array(); made.unsigned(from); made.unsigned(at); made.end_array();
made.end_object();
}
from = at + 1;
break if byte == ')';
}
made.end_array();
# Every argument after a variadic parameter's first is given to it too.
val active := each.variadic && each.arity > 0 && help.parameter >= each.arity ? each.arity - 1 : help.parameter;
made.member("activeParameter"); made.unsigned(active as u64);
made.end_object();
}
made.end_array();
made.member("activeSignature"); made.unsigned(0);
made.member("activeParameter"); made.unsigned(help.parameter as u64);
made.end_object();
append_written(out, &mut made);
}
#! The kind of semantic token each kind of name is, in the order of the legend
#! the server declares.
#! @param kind What the name is.
#! @returns The token's kind.
def token_kind(kind: symbol): u32 {
return match kind {
symbol::namespace => 0,
symbol::struct_type => 1,
symbol::trait_type => 2,
symbol::enum_type => 3,
symbol::type_name => 4,
symbol::type_param => 5,
symbol::parameter => 6,
symbol::variable => 7,
symbol::constant => 7,
symbol::field => 8,
symbol::enum_member => 9,
symbol::function => 10,
symbol::method => 11,
symbol::keyword => 12
};
}
#! Writes the names of the file as the protocol counts them: five numbers each,
#! the line and character of the first being from the beginning of the file and
#! of the rest from the name before it.
#! @param place What the process works with.
#! @param asked The job.
#! @param out Where the answer is written.
def answer_tokens(place: &mut workplace, asked: &job, out: &mut string): void {
val opened := open_asked(place, asked) else { return; };
val found := place.checker.highlights(opened.unit);
val starts := &place.held.sources[opened.file].lines;
var line: u32 = 0;
var character: u32 = 0;
var made := message_writer();
made.begin_object();
made.member("data");
made.begin_array();
for &each in found {
val begins := position_of(opened.text, starts, each.offset);
val ends := position_of(opened.text, starts, each.offset + each.length);
continue if ends.line != begins.line || ends.character <= begins.character || begins.line < line;
made.unsigned((begins.line - line) as u64);
made.unsigned((begins.line == line ? begins.character - character : begins.character) as u64);
made.unsigned((ends.character - begins.character) as u64);
made.unsigned(token_kind(each.kind) as u64);
made.unsigned(((each.declared ? 1 : 0) | (each.readonly ? 2 : 0)) as u64);
line = begins.line;
character = begins.character;
}
made.end_array();
made.end_object();
append_written(out, &mut made);
}
#! Writes what the name at the position refers to: the name as it is written,
#! where it is, where what it refers to is declared, and why it cannot be
#! renamed, which the requests about names shape into their answers.
#! @param place What the process works with.
#! @param asked The job.
#! @param out Where the answer is written.
def answer_name(place: &mut workplace, asked: &job, out: &mut string): void {
val opened := open_asked(place, asked) else { return; };
val named := place.checker.name_at(opened.unit, opened.offset) else { return; };
var made := message_writer();
made.begin_object();
made.member("name");
made.text(opened.text.substr(named.at.offset as u64, named.at.length as u64));
made.member("at");
write_spot(place, &named.at, &mut made);
if named.declared {
made.member("declaration");
write_spot(place, &named.declaration, &mut made);
}
if named.refusal.length != 0 {
made.member("refusal");
made.text(named.refusal);
}
made.end_object();
append_written(out, &mut made);
}
#! A place a name is written, by the file of the program's count, which every
#! program the name is sought in shares.
struct placed {
var file: u32; #! The file, as the program counts them.
var start: u32; #! The byte the name begins at.
var end: u32; #! The byte it ends before.
}
#! Writes everywhere what the name at the position refers to is written, in
#! every program, with where it is declared and why it cannot be renamed, as
#! `answer_name` does, whether it is a local or a member, and where its text is
#! written but was never resolved.
#! @param place What the process works with.
#! @param asked The job.
#! @param out Where the answer is written.
def answer_refer(place: &mut workplace, asked: &job, out: &mut string): void {
val opened := open_asked(place, asked) else { return; };
val named := place.checker.name_at(opened.unit, opened.offset) else { return; };
val written := opened.text.substr(named.at.offset as u64, named.at.length as u64);
var places := vector::<placed>::new();
var strays := vector::<placed>::new();
var seen := vector::<u64>::new();
var found := vector::<spot>::new();
var stray := vector::<spot>::new();
place.checker.refer_all(opened.unit, opened.offset, &mut found, &mut stray);
for &each in found {
keep_once(place.held.files[each.file], &each, &mut places, &mut seen);
}
for &each in stray {
keep_once(place.held.files[each.file], &each, &mut strays, &mut seen);
}
if named.declared && !named.local {
refer_elsewhere(place, &named.declaration, written, &mut places, &mut strays, &mut seen);
}
var made := message_writer();
made.begin_object();
made.member("name");
made.text(written);
made.member("locations");
write_places(place, &places, &mut made);
if named.declared {
made.member("declaration");
write_spot(place, &named.declaration, &mut made);
}
if named.refusal.length != 0 {
made.member("refusal");
made.text(named.refusal);
}
made.member("local"); made.boolean(named.local);
made.member("member"); made.boolean(named.member);
made.member("unresolved");
write_places(place, &strays, &mut made);
made.end_object();
append_written(out, &mut made);
}
#! Writes places a name is written, as an array of locations.
#! @param place What the process works with.
#! @param places The places.
#! @param out Where they are written.
def write_places(place: &workplace, places: &vector<placed>, out: &mut json::writer): void {
out.begin_array();
for &each in places {
write_location(place, each.file, each.start, each.end, out);
}
out.end_array();
}
#! Whether text holds a name as a word of its own.
#! @param text The text.
#! @param name The name.
#! @returns Whether it does.
def mentions(text: strview, name: strview): bool {
return false if name.length == 0;
var at: u64 = 0;
while at + name.length <= text.length {
if text.substr(at, name.length) == name {
val joined := (at > 0 && is_word_byte(text[at - 1])) || (at + name.length < text.length && is_word_byte(text[at + name.length]));
return true if !joined;
}
at++;
}
return false;
}
#! Keeps where the name is written in the programs the job was not answered
#! from. Every file but those beginning a program is in each of them, so only
#! those files are looked within, each by checking its program in turn, and
#! only where it mentions the name. What is declared in a file beginning a
#! program is not written in another program, so it is not sought.
#! @param place What the process works with.
#! @param declared Where what the name refers to is declared.
#! @param name The name as it is written.
#! @param places Where the places it is written are kept.
#! @param strays Where the places it is written unresolved are kept.
#! @param seen The places kept so far.
def refer_elsewhere(
place: &mut workplace,
declared: &spot,
name: strview,
places: &mut vector<placed>,
strays: &mut vector<placed>,
seen: &mut vector<u64>
): void {
val held := &mut place.held;
val home: u32 = held.files[declared.file];
return if held.mains[home];
for i in 0..held.paths.length {
val entry := i as u32;
continue if !held.mains[i] || entry == held.entry || !mentions(held.texts[i].span, name);
# The other program, checked with the same files, by a checker of its own.
var notes := vector::<note>::new();
var other := checker::new(&mut held.names, &mut notes);
target_checker(&mut other, held.system, held.native);
var units := vector::<u32>::new();
var files := vector::<u32>::new();
for j in 0..held.paths.length {
val file := j as u32;
if (held.mains[j] && file != entry) || (held.strict && held.broken(file)) {
units.append(none);
continue;
}
units.append(other.add(&held.sources[j], &held.syntaxes[j], held.roots[j], j < held.given));
files.append(file);
}
other.check();
val there: u32 = units[home];
continue if there == none;
var found := vector::<spot>::new();
var stray := vector::<spot>::new();
other.refer_all(there, declared.offset, &mut found, &mut stray);
for &each in found {
keep_once(files[each.file], &each, places, seen);
}
for &each in stray {
keep_once(files[each.file], &each, strays, seen);
}
}
}
#! Keeps a place a checker gave, once.
#! @param file The place's file, as the program counts them.
#! @param at The place.
#! @param into Where it is kept.
#! @param seen The places kept so far, by file and offset.
def keep_once(file: u32, at: &spot, into: &mut vector<placed>, seen: &mut vector<u64>): void {
val key := (file as u64 << 32) | at.offset as u64;
return if seen.span.contains(&key);
seen.append(key);
into.append(placed { file = file, start = at.offset, end = at.offset + at.length });
}