1154 lines
43 KiB
Plaintext
1154 lines
43 KiB
Plaintext
#! @file libraries.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains how the files of a program's libraries are found: those of each
|
||
|
|
#! module its files refer to, and of each module those refer to, until nothing
|
||
|
|
#! new is, so a program reads the modules it uses and none it does not.
|
||
|
|
#!
|
||
|
|
#! A library is a folder, and its modules are its folders and files by their
|
||
|
|
#! paths: in the library `std`, the module `std::a::b` is the folder `a/b`, or
|
||
|
|
#! the one file `a/b.sakura`, and the root `std` is the folder itself. A file or
|
||
|
|
#! folder within a module's folder belongs to it where its first `module` line
|
||
|
|
#! says so, and is otherwise a module of its own, as `std/testing.sakura` and
|
||
|
|
#! `std/io/` are; a folder is of each module a file within it is of.
|
||
|
|
#!
|
||
|
|
#! A library's name is the module its folder is, which may be within another,
|
||
|
|
#! as `std::yaml` or `sakura::analyzer`. A module is found in the library whose
|
||
|
|
#! name is the most of its path, so the library `std::yaml` holds `std::yaml`
|
||
|
|
#! and what is within it, and `std` the rest of `std`. A module holding a
|
||
|
|
#! library and in none itself, as `sakura` of `sakura::analyzer`, has no files.
|
||
|
|
#!
|
||
|
|
#! A file refers to each module it imports, to the modules its sections are
|
||
|
|
#! of and those holding them, whose names it sees, and to the module each path
|
||
|
|
#! it writes begins with, as `system` of `system::fd` or `std::io` of
|
||
|
|
#! `std::io::println`. A path's first segment is found as name lookup finds a
|
||
|
|
#! module: within the module the file is of or one holding it, then a library.
|
||
|
|
#! A segment naming no module's folder or file, as a type's, reads nothing.
|
||
|
|
|
||
|
|
import std::io;
|
||
|
|
import system;
|
||
|
|
import std::containers;
|
||
|
|
module sakura::analyzer;
|
||
|
|
|
||
|
|
|
||
|
|
#! A library a program may read: its name, which its root module is, and its
|
||
|
|
#! folder.
|
||
|
|
pub struct library {
|
||
|
|
pub var name: string; #! The library's name, as `std` or `std::yaml`.
|
||
|
|
pub var folder: string; #! Its folder.
|
||
|
|
pub var whole: bool; #! Whether it is read whole, as `core` is, rather than by what refers to it.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What finds the files of the modules a program's files refer to, keeping
|
||
|
|
#! which it found, so each file is read once.
|
||
|
|
#!
|
||
|
|
#! What it asks of the disk it asks once: each folder is listed once, and the
|
||
|
|
#! first lines of each file are read once, which say the module it is of, and
|
||
|
|
#! whether its one section is of another target than the one it reads for.
|
||
|
|
pub struct resolver {
|
||
|
|
var libraries: vector<library>; #! The libraries modules are found in.
|
||
|
|
var files: dict<string, bool>; #! The files found or read before, by path.
|
||
|
|
var modules: dict<string, bool>; #! The modules whose files were found, by path, as `std::io`.
|
||
|
|
var exists: dict<string, bool>; #! Whether each module looked for has a file or folder of its own, by path.
|
||
|
|
|
||
|
|
# The paths files write, by the IDs of their names, as a tree, the empty path
|
||
|
|
# first: what was worked out of each is kept with it.
|
||
|
|
var paths: vector<module_path>; #! Each path written, by its node.
|
||
|
|
var path_ids: keymap; #! Each path's node, by its parent's and its last name's: `parent << 32 | name`.
|
||
|
|
|
||
|
|
# What the disk was asked: the folders listed, the first, 0, standing for a
|
||
|
|
# path that is no folder or holds nothing, and what each file's first lines
|
||
|
|
# said, the modules they name by an ID of their own, 0 standing for none.
|
||
|
|
var folders: vector<shelf_folder>; #! Each folder listed.
|
||
|
|
var folder_ids: dict<string, u32>; #! Each folder's index in `folders`, by its path.
|
||
|
|
var heads: dict<string, file_head>; #! What each file's first lines said, by its path.
|
||
|
|
var module_ids: dict<string, u32>; #! The ID of each module a file or a path named, by its path, as `std::io`.
|
||
|
|
var held_modules: keymap; #! Whether each folder holds a file of each module, 1 where it does and 2 where it does not: `folder << 32 | module`.
|
||
|
|
|
||
|
|
var target: when_target; #! The target files are read for.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A folder as it was listed: what it holds.
|
||
|
|
struct shelf_folder {
|
||
|
|
var entries: vector<listed_entry>; #! What it holds, in the order of their names.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What a file's first lines say.
|
||
|
|
struct file_head {
|
||
|
|
var of: u32; #! The module of its first `module` line, by its ID, or 0 where it has none first.
|
||
|
|
var cut: u64; #! How much of its text is read: up to the end of its one section's line where that section is of another target, or 0 for all of it.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The target a program's files are read for, as a `when` names it.
|
||
|
|
pub struct when_target {
|
||
|
|
pub var os: string; #! The operating system, as `linux`, or empty, where every file is read whole.
|
||
|
|
pub var arch: string; #! The processor, as `x86_64`.
|
||
|
|
pub var backend: string; #! How the program runs, as `vm` or `native`.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A path a file writes, as the tree of them holds it: the path without its
|
||
|
|
#! last name, the name, and what was worked out of it.
|
||
|
|
struct module_path {
|
||
|
|
var parent: u32; #! The node of the path without its last name.
|
||
|
|
var name: u32; #! The ID of its last name.
|
||
|
|
var depth: u32; #! How many names it has.
|
||
|
|
var named: u8; #! Whether it names a module: 0 not yet asked, 1 where it does not, and 2 where it does.
|
||
|
|
var found: bool; #! Whether the files of the module it names were found.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Makes a resolver of no library yet, which has found nothing.
|
||
|
|
#! @returns The resolver.
|
||
|
|
pub def resolver::new(): resolver {
|
||
|
|
var made := resolver {
|
||
|
|
libraries = vector::<library>::new(),
|
||
|
|
files = dict::<string, bool>::new(),
|
||
|
|
modules = dict::<string, bool>::new(),
|
||
|
|
exists = dict::<string, bool>::new(),
|
||
|
|
paths = vector::<module_path>::new(),
|
||
|
|
path_ids = keymap::new(),
|
||
|
|
folders = vector::<shelf_folder>::new(),
|
||
|
|
folder_ids = dict::<string, u32>::new(),
|
||
|
|
heads = dict::<string, file_head>::new(),
|
||
|
|
module_ids = dict::<string, u32>::new(),
|
||
|
|
held_modules = keymap::new(),
|
||
|
|
target = when_target {
|
||
|
|
os = string::new(nothing {}),
|
||
|
|
arch = string::new(nothing {}),
|
||
|
|
backend = string::new(nothing {})
|
||
|
|
}
|
||
|
|
};
|
||
|
|
# The empty path, which every path is within, and the folder standing for
|
||
|
|
# none.
|
||
|
|
made.paths.append(module_path {});
|
||
|
|
made.folders.append(shelf_folder { entries = vector::<listed_entry>::new() });
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reads the files for a target, so a file whose one section is of another
|
||
|
|
#! target is read only as far as that section's line: its imports and its
|
||
|
|
#! module are the program's, but nothing it declares is.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param os The operating system, as `linux`.
|
||
|
|
#! @param arch The processor, as `x86_64`.
|
||
|
|
#! @param backend How the program runs, as `vm` or `native`.
|
||
|
|
pub def resolver::read_for(&mut this, os: strview, arch: strview, backend: strview): void {
|
||
|
|
this.target = when_target {
|
||
|
|
os = string::from(os, nothing {}),
|
||
|
|
arch = string::from(arch, nothing {}),
|
||
|
|
backend = string::from(backend, nothing {})
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How much of a file found is read: up to the end of its one section's line,
|
||
|
|
#! where that section is of another target than the one read for.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param at The file's path.
|
||
|
|
#! @returns How many bytes, or 0 where it is read whole.
|
||
|
|
pub def resolver::cut_of(&this, at: strview): u64 {
|
||
|
|
val key := string::from(at, nothing {});
|
||
|
|
if this.heads.get(&key) is some(&known) {
|
||
|
|
return known.cut;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Adds a library modules are found in, unless one of its name was added
|
||
|
|
#! before, which is the one found by the name.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param name The library's name, as `std` or `std::yaml`.
|
||
|
|
#! @param folder Its folder.
|
||
|
|
pub def resolver::add_library(&mut this, name: strview, folder: strview): void {
|
||
|
|
return if this.library_of(name) is some(_);
|
||
|
|
this.libraries.append(library { name = string::from(name, nothing {}), folder = string::from(folder, nothing {}) });
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Adds a library read whole, as `core` is, whose files the caller reads, so
|
||
|
|
#! none is found by what refers to it.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param name The library's name.
|
||
|
|
#! @param folder Its folder.
|
||
|
|
pub def resolver::whole(&mut this, name: strview, folder: strview): void {
|
||
|
|
return if this.library_of(name) is some(_);
|
||
|
|
this.libraries.append(library {
|
||
|
|
name = string::from(name, nothing {}), folder = string::from(folder, nothing {}), whole = true
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The libraries modules are found in.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @returns A copy of each.
|
||
|
|
pub def resolver::held(&this): vector<library> {
|
||
|
|
var made := vector::<library>::new();
|
||
|
|
for &each in this.libraries {
|
||
|
|
made.append(library { name = string::copy_of(&each.name), folder = string::copy_of(&each.folder), whole = each.whole });
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Notes a file read whatever it refers to, as a program's own and `core`'s
|
||
|
|
#! are, so no module brings it again.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param path The file's path.
|
||
|
|
pub def resolver::mark(&mut this, path: strview): void {
|
||
|
|
this.files.set(string::from(path, nothing {}), true);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Finds the files of a module asked for by its path, as `runtime` is for
|
||
|
|
#! an executable, and of the modules holding it.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param wanted The module's path.
|
||
|
|
#! @param into Where the paths of the files not found before are added.
|
||
|
|
pub def resolver::want(&mut this, wanted: strview, into: &mut vector<string>): void {
|
||
|
|
var segments := vector::<string>::new();
|
||
|
|
for piece in wanted.split(':') {
|
||
|
|
continue if piece.length == 0;
|
||
|
|
segments.append(string::from(piece, nothing {}));
|
||
|
|
}
|
||
|
|
for count in 1..segments.length + 1 {
|
||
|
|
this.find_module(segments[0:count], into);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Finds the files of the modules a file refers to: those it imports, those its
|
||
|
|
#! sections are of and those holding them, and the module each path it writes
|
||
|
|
#! begins with.
|
||
|
|
#!
|
||
|
|
#! Each path is looked for by the IDs of its names, in a tree of the paths every
|
||
|
|
#! file wrote, so a path written again, in this file or another, is looked for
|
||
|
|
#! once, and its text is made only where it is asked whether it names a module.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param syntax The file, parsed.
|
||
|
|
#! @param names The names it was read with, which are those of every file
|
||
|
|
#! the resolver is given.
|
||
|
|
#! @param into Where the paths of the files not found before are added, in
|
||
|
|
#! the order they are first referred to.
|
||
|
|
pub def resolver::refer(&mut this, syntax: &syntax, names: &names, into: &mut vector<string>): void {
|
||
|
|
# The modules its sections are of, whose names, and those of the modules
|
||
|
|
# holding them, the file sees.
|
||
|
|
var homes := vector::<u32>::new();
|
||
|
|
for node in 0..syntax.node_count {
|
||
|
|
val kind: nk = syntax.node_kinds[node];
|
||
|
|
continue if kind != nk::module_decl && kind != nk::extern_module;
|
||
|
|
val made := this.path_node(syntax, syntax.node_lefts[node]);
|
||
|
|
continue if made == 0;
|
||
|
|
this.find_prefixes(made, names, into);
|
||
|
|
homes.append(made);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A file writes the same paths again and again, as `nk::name`, so each is
|
||
|
|
# found once, by its node.
|
||
|
|
var tried := keymap::new();
|
||
|
|
for node in 0..syntax.node_count {
|
||
|
|
val kind: nk = syntax.node_kinds[node];
|
||
|
|
var path: u32 = 0;
|
||
|
|
if kind == nk::import_decl {
|
||
|
|
path = syntax.node_lefts[node];
|
||
|
|
} else if kind == nk::path || kind == nk::type_name {
|
||
|
|
continue if syntax.node_lefts[node] == 0 || syntax.run_length(syntax.node_lefts[node]) < 2;
|
||
|
|
path = node as u32;
|
||
|
|
}
|
||
|
|
continue if path == 0;
|
||
|
|
val written := this.path_node(syntax, path);
|
||
|
|
continue if written == 0 || !tried.insert(written as u64, 1);
|
||
|
|
this.find_path(written, homes.span, names, into);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The node of the path of a `path`, `type_name`, or `name` node's segments,
|
||
|
|
#! made where it is new.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param syntax The file.
|
||
|
|
#! @param node The node, or 0.
|
||
|
|
#! @returns The path's node, or 0 where the node is none of those.
|
||
|
|
def resolver::path_node(&mut this, syntax: &syntax, node: u32): u32 {
|
||
|
|
return 0 if node == 0;
|
||
|
|
val kind: nk = syntax.node_kinds[node];
|
||
|
|
return 0 if kind != nk::path && kind != nk::type_name && kind != nk::name;
|
||
|
|
val run: u32 = syntax.node_lefts[node];
|
||
|
|
if kind == nk::name || run == 0 {
|
||
|
|
return this.child(0, syntax.token_names[syntax.node_tokens[node]]);
|
||
|
|
}
|
||
|
|
var at: u32 = 0;
|
||
|
|
for i in 0..syntax.run_length(run) {
|
||
|
|
val segment := syntax.run_at(run, i as u32);
|
||
|
|
at = this.child(at, syntax.token_names[syntax.node_tokens[segment]]);
|
||
|
|
}
|
||
|
|
return at;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The node of the path that is another's with one more name, made where it is
|
||
|
|
#! new.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param parent The other path's node, 0 for the empty path.
|
||
|
|
#! @param name The name's ID.
|
||
|
|
#! @returns The node.
|
||
|
|
def resolver::child(&mut this, parent: u32, name: u32): u32 {
|
||
|
|
val key := pair_key(parent, name);
|
||
|
|
val held := this.path_ids.get_or(&key, 0);
|
||
|
|
return held if held != 0;
|
||
|
|
val made := this.paths.length as u32;
|
||
|
|
this.paths.append(module_path { parent = parent, name = name, depth = this.paths[parent].depth + 1 });
|
||
|
|
this.path_ids.set(key, made);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The names of a path, by their text, its first first.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param node The path's node.
|
||
|
|
#! @param names The names its IDs are of.
|
||
|
|
#! @returns The names.
|
||
|
|
def resolver::segments_of(&this, node: u32, names: &names): vector<string> {
|
||
|
|
var made := vector::<string>::new();
|
||
|
|
made.resize(this.paths[node].depth as u64);
|
||
|
|
var at := node;
|
||
|
|
while at != 0 {
|
||
|
|
val held: module_path = this.paths[at];
|
||
|
|
made[held.depth as u64 - 1] = string::from(names.text(held.name), nothing {});
|
||
|
|
at = held.parent;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a path names a module, as `module_exists` says, asked once.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param node The path's node.
|
||
|
|
#! @param names The names its IDs are of.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def resolver::names_module(&mut this, node: u32, names: &names): bool {
|
||
|
|
if this.paths[node].named == 0 {
|
||
|
|
val segments := this.segments_of(node, names);
|
||
|
|
this.paths[node].named = this.module_exists(segments.span) ? 2 : 1;
|
||
|
|
}
|
||
|
|
return this.paths[node].named == 2;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Finds the files of the module a path names, once.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param node The path's node.
|
||
|
|
#! @param names The names its IDs are of.
|
||
|
|
#! @param into Where the paths of the files not found before are added.
|
||
|
|
def resolver::find_node(&mut this, node: u32, names: &names, into: &mut vector<string>): void {
|
||
|
|
return if this.paths[node].found;
|
||
|
|
this.paths[node].found = true;
|
||
|
|
val segments := this.segments_of(node, names);
|
||
|
|
this.find_module(segments.span, into);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Finds the files of the modules a path and each path that begins it name, the
|
||
|
|
#! shortest first, as a file's `module` line refers to them.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param node The path's node.
|
||
|
|
#! @param names The names its IDs are of.
|
||
|
|
#! @param into Where the paths of the files not found before are added.
|
||
|
|
def resolver::find_prefixes(&mut this, node: u32, names: &names, into: &mut vector<string>): void {
|
||
|
|
var chain := vector::<u32>::new();
|
||
|
|
var at := node;
|
||
|
|
while at != 0 {
|
||
|
|
chain.append(at);
|
||
|
|
at = this.paths[at].parent;
|
||
|
|
}
|
||
|
|
var i := chain.length;
|
||
|
|
while i > 0 {
|
||
|
|
i--;
|
||
|
|
this.find_node(chain[i], names, into);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Finds the files of the modules a path names as many of its names as name
|
||
|
|
#! modules: from the first, within a module the file is of or one holding it,
|
||
|
|
#! nearest first, then as a library.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param written The path's node.
|
||
|
|
#! @param homes The nodes of the modules the file's sections are of.
|
||
|
|
#! @param names The names its IDs are of.
|
||
|
|
#! @param into Where the paths of the files not found before are added.
|
||
|
|
def resolver::find_path(&mut this, written: u32, homes: span<u32>, names: &names, into: &mut vector<string>): void {
|
||
|
|
# The path's names, its first first.
|
||
|
|
var rest := vector::<u32>::new();
|
||
|
|
rest.resize(this.paths[written].depth as u64);
|
||
|
|
var at := written;
|
||
|
|
while at != 0 {
|
||
|
|
rest[this.paths[at].depth as u64 - 1] = this.paths[at].name;
|
||
|
|
at = this.paths[at].parent;
|
||
|
|
}
|
||
|
|
return if rest.length == 0;
|
||
|
|
|
||
|
|
for &home in homes {
|
||
|
|
var holder := home;
|
||
|
|
while holder != 0 {
|
||
|
|
val tried := this.child(holder, rest[0]);
|
||
|
|
if this.names_module(tried, names) {
|
||
|
|
this.find_deeper(tried, rest[1:], names, into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
holder = this.paths[holder].parent;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
val tried := this.child(0, rest[0]);
|
||
|
|
return if !this.names_module(tried, names);
|
||
|
|
this.find_deeper(tried, rest[1:], names, into);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Finds the files of a module the path's first name found, then of those
|
||
|
|
#! within it its next names name, while they name any.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param found The node of the module found, which grows by each name.
|
||
|
|
#! @param rest The IDs of the path's names after the first.
|
||
|
|
#! @param names The names its IDs are of.
|
||
|
|
#! @param into Where the paths of the files not found before are added.
|
||
|
|
def resolver::find_deeper(&mut this, found: u32, rest: span<u32>, names: &names, into: &mut vector<string>): void {
|
||
|
|
this.find_node(found, names, into);
|
||
|
|
var at := found;
|
||
|
|
for &name in rest {
|
||
|
|
at = this.child(at, name);
|
||
|
|
return if !this.names_module(at, names);
|
||
|
|
this.find_node(at, names, into);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The library of a name, or none.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param name The name.
|
||
|
|
#! @returns The library's index, or none.
|
||
|
|
def resolver::library_of(&this, name: strview): maybe<u64> {
|
||
|
|
for i in 0..this.libraries.length {
|
||
|
|
return i if this.libraries[i].name.span == name;
|
||
|
|
}
|
||
|
|
return nothing {};
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The library a module is found in, and how much of its path the library's
|
||
|
|
#! name is.
|
||
|
|
struct shelf {
|
||
|
|
var index: u64; #! The library's index.
|
||
|
|
var depth: u64; #! How many of the module's segments its name is.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The library a module is found in: the one whose name is the most of the
|
||
|
|
#! module's path, from its start.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param segments The module's path.
|
||
|
|
#! @returns The library, or none where no library's name begins the path.
|
||
|
|
def resolver::library_for(&this, segments: span<string>): maybe<shelf> {
|
||
|
|
var found: u64 = 0;
|
||
|
|
var deepest: u64 = 0;
|
||
|
|
for i in 0..this.libraries.length {
|
||
|
|
val depth := covered(this.libraries[i].name.span, segments);
|
||
|
|
if depth > deepest {
|
||
|
|
found = i;
|
||
|
|
deepest = depth;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nothing {} if deepest == 0;
|
||
|
|
return shelf { index = found, depth = deepest };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a module holds a library within it, as `sakura` holds
|
||
|
|
#! `sakura::analyzer`.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param segments The module's path.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def resolver::holds_library(&this, segments: span<string>): bool {
|
||
|
|
for &each in this.libraries {
|
||
|
|
return true if holds(each.name.span, segments);
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many of a module's segments a library's name is, where the name is the
|
||
|
|
#! module's path or its start, as `std::yaml` is two of `std::yaml::text`.
|
||
|
|
#! @param name The library's name.
|
||
|
|
#! @param segments The module's path.
|
||
|
|
#! @returns How many, or 0 where the name does not begin the path.
|
||
|
|
def covered(name: strview, segments: span<string>): u64 {
|
||
|
|
var at: u64 = 0;
|
||
|
|
for count in 0..segments.length {
|
||
|
|
val piece := segments[count].span;
|
||
|
|
return 0 if name.length - at < piece.length || name.substr(at, piece.length) != piece;
|
||
|
|
at += piece.length;
|
||
|
|
return count + 1 if at == name.length;
|
||
|
|
return 0 if name.length - at < 2 || name[at] != ':' || name[at + 1] != ':';
|
||
|
|
at += 2;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a library's name is within a module, as `sakura::analyzer` is within
|
||
|
|
#! `sakura`.
|
||
|
|
#! @param name The library's name.
|
||
|
|
#! @param segments The module's path.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
def holds(name: strview, segments: span<string>): bool {
|
||
|
|
var at: u64 = 0;
|
||
|
|
for &each in segments {
|
||
|
|
val piece := each.span;
|
||
|
|
return false if name.length - at < piece.length + 2 || name.substr(at, piece.length) != piece;
|
||
|
|
at += piece.length;
|
||
|
|
return false if name[at] != ':' || name[at + 1] != ':';
|
||
|
|
at += 2;
|
||
|
|
}
|
||
|
|
return segments.length != 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a module's path names a library, or a folder or file within one of
|
||
|
|
#! that module, as its first `module` line says, or holds a library. Each is
|
||
|
|
#! looked at once.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param segments The module's path.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def resolver::module_exists(&mut this, segments: span<string>): bool {
|
||
|
|
val found := this.library_for(segments) else { return this.holds_library(segments); };
|
||
|
|
return true if segments.length == found.depth;
|
||
|
|
var key := module_key(segments);
|
||
|
|
if this.exists.get(&key) is some(&known) {
|
||
|
|
return known;
|
||
|
|
}
|
||
|
|
val of := this.module_id(key.span);
|
||
|
|
var at := this.within(&found, segments);
|
||
|
|
var named := this.folder_holds(this.folder(at.span), of);
|
||
|
|
if !named {
|
||
|
|
at.append(".sakura");
|
||
|
|
named = this.file_of(at.span, of);
|
||
|
|
}
|
||
|
|
named = named || this.holds_library(segments);
|
||
|
|
this.exists.set(move(&mut key), named);
|
||
|
|
return named;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A module's path as text, as `std::io`.
|
||
|
|
#! @param segments The path's segments.
|
||
|
|
#! @returns The text.
|
||
|
|
def module_key(segments: span<string>): string {
|
||
|
|
var made := string::new(nothing {});
|
||
|
|
for i in 0..segments.length {
|
||
|
|
if i != 0 {
|
||
|
|
made.append("::");
|
||
|
|
}
|
||
|
|
made.append(segments[i].span);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The path of a module's folder within its library, as `std/io` of `std::io`.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param found The library, and how much of the path its name is.
|
||
|
|
#! @param segments The module's path, its library first.
|
||
|
|
#! @returns The path.
|
||
|
|
def resolver::within(&this, found: &shelf, segments: span<string>): string {
|
||
|
|
var made := string::copy_of(&this.libraries[found.index].folder);
|
||
|
|
for i in found.depth..segments.length {
|
||
|
|
made.push('/');
|
||
|
|
made.append(segments[i].span);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Finds the files of a module, once: its file, as `a/b.sakura`, and those of
|
||
|
|
#! its folder, as `a/b`, that are of the module, with the folders within it
|
||
|
|
#! that are.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param segments The module's path.
|
||
|
|
#! @param into Where the paths of the files not found before are added.
|
||
|
|
def resolver::find_module(&mut this, segments: span<string>, into: &mut vector<string>): void {
|
||
|
|
val found := this.library_for(segments) else { return; };
|
||
|
|
return if this.libraries[found.index].whole;
|
||
|
|
var key := module_key(segments);
|
||
|
|
return if this.modules.get(&key) is some(_);
|
||
|
|
this.modules.set(string::copy_of(&key), true);
|
||
|
|
|
||
|
|
val of := this.module_id(key.span);
|
||
|
|
var at := this.within(&found, segments);
|
||
|
|
if segments.length > found.depth {
|
||
|
|
var file := $"{at}.sakura";
|
||
|
|
if this.file_of(file.span, of) {
|
||
|
|
this.add_file(file.span, into);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this.find_in_folder(this.folder(at.span), of, into);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Finds the files of a folder that are of a module, and those of the folders
|
||
|
|
#! within it that are, each in the order of its name.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param index The folder's index in `folders`.
|
||
|
|
#! @param of The module's ID.
|
||
|
|
#! @param into Where the paths of the files not found before are added.
|
||
|
|
def resolver::find_in_folder(&mut this, index: u32, of: u32, into: &mut vector<string>): void {
|
||
|
|
for i in 0..this.folders[index].entries.length {
|
||
|
|
val path := string::copy_of(&this.folders[index].entries[i].path);
|
||
|
|
if this.folders[index].entries[i].folder {
|
||
|
|
val inner := this.listed_folder(path.span);
|
||
|
|
if this.folder_holds(inner, of) {
|
||
|
|
this.find_in_folder(inner, of, into);
|
||
|
|
}
|
||
|
|
} else if this.head(path.span).of == of {
|
||
|
|
this.add_file(path.span, into);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The index of a folder in `folders`, listed where it was not before. A folder
|
||
|
|
#! within a library is listed only where the folder holding it lists it, so a
|
||
|
|
#! path naming none, as most a file writes do, is not looked for on the disk.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param at The folder's path.
|
||
|
|
#! @returns Its index, or 0 where the path is no folder or holds nothing a
|
||
|
|
#! library is read from.
|
||
|
|
def resolver::folder(&mut this, at: strview): u32 {
|
||
|
|
var key := string::from(at, nothing {});
|
||
|
|
if this.folder_ids.get(&key) is some(&known) {
|
||
|
|
return known;
|
||
|
|
}
|
||
|
|
if !this.is_shelf(at) {
|
||
|
|
val slash := at.last_index_of(&'/') else { return 0; };
|
||
|
|
val outer := this.folder(at.substr(0, slash));
|
||
|
|
var held := false;
|
||
|
|
for &each in this.folders[outer].entries {
|
||
|
|
held = held || each.folder && each.path.span == at;
|
||
|
|
}
|
||
|
|
if !held {
|
||
|
|
this.folder_ids.set(move(&mut key), 0);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return this.list_folder(move(&mut key));
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The index of a folder another's entries hold, listed where it was not
|
||
|
|
#! before.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param at The folder's path, as the entry holding it gives it.
|
||
|
|
#! @returns Its index, or 0 where it holds nothing a library is read from.
|
||
|
|
def resolver::listed_folder(&mut this, at: strview): u32 {
|
||
|
|
var key := string::from(at, nothing {});
|
||
|
|
if this.folder_ids.get(&key) is some(&known) {
|
||
|
|
return known;
|
||
|
|
}
|
||
|
|
return this.list_folder(move(&mut key));
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lists a folder not listed before and keeps it, by its path.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param at The folder's path.
|
||
|
|
#! @returns Its index, or 0 where it holds nothing a library is read from.
|
||
|
|
def resolver::list_folder(&mut this, at: string): u32 {
|
||
|
|
var listed := listing(at.span);
|
||
|
|
var index: u32 = 0;
|
||
|
|
if listed.length != 0 {
|
||
|
|
index = this.folders.length as u32;
|
||
|
|
this.folders.append(shelf_folder { entries = move(&mut listed) });
|
||
|
|
}
|
||
|
|
this.folder_ids.set(move(&mut at), index);
|
||
|
|
return index;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a path is a library's folder.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param at The path.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
def resolver::is_shelf(&this, at: strview): bool {
|
||
|
|
for &each in this.libraries {
|
||
|
|
return true if each.folder.span == at;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a folder holds a file of a module, or a folder within it does, as
|
||
|
|
#! `folder_of` says, from what was listed and read once: its files first, then
|
||
|
|
#! its folders, ending at the first file of the module found, and kept for the
|
||
|
|
#! folder and module, so each is asked once.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param index The folder's index in `folders`.
|
||
|
|
#! @param of The module's ID.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def resolver::folder_holds(&mut this, index: u32, of: u32): bool {
|
||
|
|
return false if index == 0 || of == 0;
|
||
|
|
val key := pair_key(index, of);
|
||
|
|
val known := this.held_modules.get_or(&key, 0);
|
||
|
|
return known == 1 if known != 0;
|
||
|
|
# A folder reached again through a link while it is asked holds nothing more.
|
||
|
|
this.held_modules.set(key, 2);
|
||
|
|
var holds := false;
|
||
|
|
for i in 0..this.folders[index].entries.length {
|
||
|
|
continue if this.folders[index].entries[i].folder;
|
||
|
|
val path := string::copy_of(&this.folders[index].entries[i].path);
|
||
|
|
if this.head(path.span).of == of {
|
||
|
|
holds = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if !holds {
|
||
|
|
for i in 0..this.folders[index].entries.length {
|
||
|
|
continue if !this.folders[index].entries[i].folder;
|
||
|
|
val path := string::copy_of(&this.folders[index].entries[i].path);
|
||
|
|
if this.folder_holds(this.listed_folder(path.span), of) {
|
||
|
|
holds = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this.held_modules.set(key, holds ? 1 : 2);
|
||
|
|
return holds;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a path is a file of a module: one its folder holds, whose first
|
||
|
|
#! `module` line names the module.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param at The file's path.
|
||
|
|
#! @param of The module's ID.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
def resolver::file_of(&mut this, at: strview, of: u32): bool {
|
||
|
|
val slash := at.last_index_of(&'/') else { return false; };
|
||
|
|
val index := this.folder(at.substr(0, slash));
|
||
|
|
var listed := false;
|
||
|
|
for &each in this.folders[index].entries {
|
||
|
|
listed = listed || !each.folder && each.path.span == at;
|
||
|
|
}
|
||
|
|
return listed && this.head(at).of == of;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The ID of a module's path, made where it is new.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param path The module's path, as `std::io`, or empty for none.
|
||
|
|
#! @returns The ID, or 0 for none.
|
||
|
|
def resolver::module_id(&mut this, path: strview): u32 {
|
||
|
|
return 0 if path.length == 0;
|
||
|
|
var key := string::from(path, nothing {});
|
||
|
|
if this.module_ids.get(&key) is some(&known) {
|
||
|
|
return known;
|
||
|
|
}
|
||
|
|
val made := this.module_ids.length as u32 + 1;
|
||
|
|
this.module_ids.set(move(&mut key), made);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What a file's first lines say, read once: the module of its first `module`
|
||
|
|
#! line, and how much of it is read for the target.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param at The file's path.
|
||
|
|
#! @returns What they say.
|
||
|
|
def resolver::head(&mut this, at: strview): file_head {
|
||
|
|
var key := string::from(at, nothing {});
|
||
|
|
if this.heads.get(&key) is some(&known) {
|
||
|
|
return known;
|
||
|
|
}
|
||
|
|
var text := head_text(at);
|
||
|
|
val line := find_section(text.span);
|
||
|
|
var made := file_head {
|
||
|
|
of = this.module_id(text.span.substr(line.name, line.name_length))
|
||
|
|
};
|
||
|
|
# The rest is read where the line says it is of another target, which few do.
|
||
|
|
if line.condition_length != 0 && this.target.os.length != 0 {
|
||
|
|
var reading := when_reading { text = text.span.substr(line.condition, line.condition_length) };
|
||
|
|
val holds := reading.holds(&this.target);
|
||
|
|
if reading.decided() && !holds {
|
||
|
|
var found := path::parse(at) else (_) { return made; };
|
||
|
|
val whole := io::read_file(&found) else (_) { return made; };
|
||
|
|
if sections_in(whole.span) == 1 {
|
||
|
|
made.cut = line.end;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this.heads.set(move(&mut key), made);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Adds a file's path, unless it was found or read before.
|
||
|
|
#! @param this The resolver.
|
||
|
|
#! @param path The path.
|
||
|
|
#! @param into Where it is added.
|
||
|
|
def resolver::add_file(&mut this, path: strview, into: &mut vector<string>): void {
|
||
|
|
var key := string::from(path, nothing {});
|
||
|
|
return if this.files.get(&key) is some(_);
|
||
|
|
this.files.set(string::copy_of(&key), true);
|
||
|
|
into.append(move(&mut key));
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The names of the modules within a module of a library, by its folder, as
|
||
|
|
#! completion offers them after `std::`, without reading their files whole,
|
||
|
|
#! with those of the libraries within it, as `yaml` of `std::yaml`.
|
||
|
|
#! @param libraries The libraries.
|
||
|
|
#! @param segments The module's path, its library first.
|
||
|
|
#! @returns The names, as `io` of `std::io`.
|
||
|
|
pub def modules_within(libraries: span<library>, segments: span<string>): vector<string> {
|
||
|
|
var made := vector::<string>::new();
|
||
|
|
return made if segments.length == 0;
|
||
|
|
|
||
|
|
# The libraries within it give their next segments.
|
||
|
|
val outer := module_key(segments);
|
||
|
|
for &each in libraries {
|
||
|
|
continue if !holds(each.name.span, segments);
|
||
|
|
val rest := each.name.span.substr(outer.length + 2, each.name.length - outer.length - 2);
|
||
|
|
val next := rest.index_of(&':') else rest.length;
|
||
|
|
val name := rest.substr(0, next);
|
||
|
|
var known := false;
|
||
|
|
for &each in made {
|
||
|
|
known = known || each.span == name;
|
||
|
|
}
|
||
|
|
if !known {
|
||
|
|
made.append(string::from(name, nothing {}));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Its library's folder gives the rest.
|
||
|
|
var folder := string::new(nothing {});
|
||
|
|
var deepest: u64 = 0;
|
||
|
|
for &each in libraries {
|
||
|
|
val depth := covered(each.name.span, segments);
|
||
|
|
if depth > deepest {
|
||
|
|
folder = string::copy_of(&each.folder);
|
||
|
|
deepest = depth;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return made if folder.length == 0;
|
||
|
|
for i in deepest..segments.length {
|
||
|
|
folder.push('/');
|
||
|
|
folder.append(segments[i].span);
|
||
|
|
}
|
||
|
|
for &each in listing(folder.span) {
|
||
|
|
# The name is the entry's own, less its `.sakura`.
|
||
|
|
var name := each.path.span.substr(folder.length + 1, each.path.length - folder.length - 1);
|
||
|
|
if !each.folder {
|
||
|
|
name = name.substr(0, name.length - 7);
|
||
|
|
}
|
||
|
|
val inner := $"{outer}::{name}";
|
||
|
|
val of := each.folder ? folder_of(each.path.span, inner.span) : first_module(each.path.span).span == inner.span;
|
||
|
|
if of {
|
||
|
|
made.append(string::from(name, nothing {}));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A name a folder holds: a `.sakura` file or a folder, which hidden ones are
|
||
|
|
#! not.
|
||
|
|
struct listed_entry {
|
||
|
|
var path: string; #! The folder and the name joined.
|
||
|
|
var folder: bool; #! Whether it is a folder.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What a folder holds that a library is read from: its `.sakura` files and
|
||
|
|
#! its folders, in the order of their names, so the same files are read in the
|
||
|
|
#! same order whatever order the system lists them in.
|
||
|
|
#! @param folder The folder.
|
||
|
|
#! @returns What it holds.
|
||
|
|
def listing(folder: strview): vector<listed_entry> {
|
||
|
|
var made := vector::<listed_entry>::new();
|
||
|
|
var at := path::parse(folder) else (_) { return made; };
|
||
|
|
var opened := io::directory::open(&at) else (_) { return made; };
|
||
|
|
var names := vector::<string>::new();
|
||
|
|
var kinds := vector::<bool>::new();
|
||
|
|
while opened.next() is some(each) {
|
||
|
|
continue if each.name.length == 0 || each.name[0] == '.';
|
||
|
|
# Most of what folders hold beside a library's files is other files, which
|
||
|
|
# are passed by name alone.
|
||
|
|
continue if each.kind != file_kind::directory && each.kind != file_kind::link && !each.name.ends_with(".sakura");
|
||
|
|
var joined := $"{folder}/{each.name}";
|
||
|
|
var is_dir := each.kind == file_kind::directory;
|
||
|
|
if each.kind == file_kind::link {
|
||
|
|
is_dir = is_folder(joined.span);
|
||
|
|
}
|
||
|
|
continue if !is_dir && !each.name.ends_with(".sakura");
|
||
|
|
made.append(listed_entry { path = move(&mut joined), folder = is_dir });
|
||
|
|
}
|
||
|
|
made.sort_by(def(a: &listed_entry, b: &listed_entry): bool { return a.path.compare(b.path.span) < 0; });
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The paths of every `.sakura` file within a folder and the folders within it,
|
||
|
|
#! each folder's in the order of their names, as `core` is read whole.
|
||
|
|
#! @param folder The folder.
|
||
|
|
#! @param into Where the paths are added.
|
||
|
|
pub def every_file(folder: strview, into: &mut vector<string>): void {
|
||
|
|
var listed := listing(folder);
|
||
|
|
for &mut each in listed {
|
||
|
|
if each.folder {
|
||
|
|
every_file(each.path.span, into);
|
||
|
|
} else {
|
||
|
|
into.append(move(&mut each.path));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many `.sakura` files are within a folder and those within it, which is
|
||
|
|
#! the most a program reads of it.
|
||
|
|
#! @param folder The folder.
|
||
|
|
#! @returns How many.
|
||
|
|
pub def count_files(folder: strview): u64 {
|
||
|
|
var made := vector::<string>::new();
|
||
|
|
every_file(folder, &mut made);
|
||
|
|
return made.length;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a folder holds a file of a module, or a folder within it does. Any
|
||
|
|
#! file says, not only the first by name, so a file of a module within it, as
|
||
|
|
#! `a/b/c.sakura` of `a::b::c` beside the files of `a::b`, hides nothing.
|
||
|
|
#! @param folder The folder.
|
||
|
|
#! @param of The module's path, as `std::io`.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def folder_of(folder: strview, of: strview): bool {
|
||
|
|
val listed := listing(folder);
|
||
|
|
for &each in listed {
|
||
|
|
return true if !each.folder && first_module(each.path.span).span == of;
|
||
|
|
}
|
||
|
|
for &each in listed {
|
||
|
|
return true if each.folder && folder_of(each.path.span, of);
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The module of a file's first `module` or `extern module` line, read from the
|
||
|
|
#! start of its text, past its comments and imports.
|
||
|
|
#! @param at The file's path.
|
||
|
|
#! @returns The module's path, as `std::io`, or empty where the file declares
|
||
|
|
#! anything before such a line, or cannot be read.
|
||
|
|
pub def first_module(at: strview): string {
|
||
|
|
val text := head_text(at);
|
||
|
|
val line := find_section(text.span);
|
||
|
|
return string::from(text.span.substr(line.name, line.name_length), nothing {});
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The first lines of a file: its first 4,096 bytes, or the whole of it where
|
||
|
|
#! its first `module` line, or what comes before one, runs past them.
|
||
|
|
#! @param at The file's path.
|
||
|
|
#! @returns The text, or empty where the file cannot be read.
|
||
|
|
def head_text(at: strview): string {
|
||
|
|
var found := path::parse(at) else (_) { return string::new(nothing {}); };
|
||
|
|
var file := io::file::open(&found) else (_) { return string::new(nothing {}); };
|
||
|
|
var buffer: u8[4096];
|
||
|
|
val count := file.read(slice::<u8>::over((&mut buffer) as *mut u8, 4096)) else (_) { return string::new(nothing {}); };
|
||
|
|
var made := string::from(strview::over((&buffer) as *u8, count), nothing {});
|
||
|
|
if count == 4096 && find_section(made.span).open {
|
||
|
|
var whole := io::read_file(&found) else (_) { return made; };
|
||
|
|
return whole;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Where a text's first `module` or `extern module` line is, past its
|
||
|
|
#! comments, imports, and `extern import` lines: the module's path, its `when`
|
||
|
|
#! condition, and the line's end.
|
||
|
|
struct section_line {
|
||
|
|
var name: u64; #! Where the module's path begins.
|
||
|
|
var name_length: u64; #! How long the path is, 0 where no such line comes first.
|
||
|
|
var condition: u64; #! Where the line's `when` condition begins.
|
||
|
|
var condition_length: u64; #! How long the condition is, 0 where the line has none.
|
||
|
|
var end: u64; #! Just past the line's `;`, or 0 where it has none.
|
||
|
|
var open: bool; #! Whether the text ran out before the line, or a declaration before it, was found.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Finds a text's first `module` or `extern module` line.
|
||
|
|
#! @param text The text.
|
||
|
|
#! @returns Where the line is.
|
||
|
|
def find_section(text: strview): section_line {
|
||
|
|
var made := section_line { open = true };
|
||
|
|
var at: u64 = 0;
|
||
|
|
while at < text.length {
|
||
|
|
val byte := text[at];
|
||
|
|
if byte == ' ' || byte == '\t' || byte == '\r' || byte == '\n' {
|
||
|
|
at++;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if byte == '#' {
|
||
|
|
while at < text.length && text[at] != '\n' { at++; }
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
var word := word_at(text, at);
|
||
|
|
if word == "extern" {
|
||
|
|
at += word.length;
|
||
|
|
while at < text.length && (text[at] == ' ' || text[at] == '\t') { at++; }
|
||
|
|
word = word_at(text, at);
|
||
|
|
}
|
||
|
|
if word == "import" {
|
||
|
|
while at < text.length && text[at] != ';' { at++; }
|
||
|
|
at++;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
made.open = false;
|
||
|
|
return made if word != "module";
|
||
|
|
at += word.length;
|
||
|
|
while at < text.length && (text[at] == ' ' || text[at] == '\t') { at++; }
|
||
|
|
made.name = at;
|
||
|
|
while at < text.length && (text[at].is_alnum || text[at] == '_' || text[at] == ':') { at++; }
|
||
|
|
made.name_length = at - made.name;
|
||
|
|
while at < text.length && (text[at] == ' ' || text[at] == '\t') { at++; }
|
||
|
|
val after := word_at(text, at);
|
||
|
|
val condition := after == "when" ? at + after.length : at;
|
||
|
|
while at < text.length && text[at] != ';' { at++; }
|
||
|
|
if at == text.length {
|
||
|
|
made.open = true;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
if after == "when" {
|
||
|
|
made.condition = condition;
|
||
|
|
made.condition_length = at - condition;
|
||
|
|
}
|
||
|
|
made.end = at + 1;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The module the first `module` or `extern module` line of a text names, past
|
||
|
|
#! its comments, imports, and `extern import` lines.
|
||
|
|
#! @param text The text.
|
||
|
|
#! @returns The module's path, or empty where none comes first.
|
||
|
|
def module_named(text: strview): string {
|
||
|
|
val line := find_section(text);
|
||
|
|
return string::from(text.substr(line.name, line.name_length), nothing {});
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many lines of a text begin with `module` or `extern module`, each a
|
||
|
|
#! section of a file.
|
||
|
|
#! @param text The text.
|
||
|
|
#! @returns How many.
|
||
|
|
def sections_in(text: strview): u64 {
|
||
|
|
var count: u64 = 0;
|
||
|
|
var at: u64 = 0;
|
||
|
|
while at < text.length {
|
||
|
|
while at < text.length && (text[at] == ' ' || text[at] == '\t') { at++; }
|
||
|
|
var word := word_at(text, at);
|
||
|
|
if word == "extern" {
|
||
|
|
var after := at + word.length;
|
||
|
|
while after < text.length && (text[after] == ' ' || text[after] == '\t') { after++; }
|
||
|
|
word = word_at(text, after);
|
||
|
|
}
|
||
|
|
if word == "module" {
|
||
|
|
count++;
|
||
|
|
}
|
||
|
|
while at < text.length && text[at] != '\n' { at++; }
|
||
|
|
at++;
|
||
|
|
}
|
||
|
|
return count;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reads the `when` condition of a section's line from its text, as the checker
|
||
|
|
#! decides it once the file is parsed: `!`, `&&`, `||`, parentheses, and
|
||
|
|
#! `key == value` or `key != value` of `os`, `arch`, and `backend`.
|
||
|
|
struct when_reading {
|
||
|
|
var text: strview; #! The condition's text.
|
||
|
|
var at: u64; #! How far it is read.
|
||
|
|
var failed: bool; #! Whether it says what the reading does not know, which leaves it undecided.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether the target meets the condition, where `decided` says it is known.
|
||
|
|
#! @param this The reading.
|
||
|
|
#! @param target The target.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def when_reading::holds(&mut this, target: &when_target): bool {
|
||
|
|
val made := this.either(target);
|
||
|
|
this.skip_blanks();
|
||
|
|
this.failed = this.failed || this.at != this.text.length;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether the condition was read whole, so `holds` said whether it holds.
|
||
|
|
#! @param this The reading.
|
||
|
|
#! @returns Whether it was.
|
||
|
|
def when_reading::decided(&this): bool {
|
||
|
|
return !this.failed;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Reads conditions joined by `||`.
|
||
|
|
def when_reading::either(&mut this, target: &when_target): bool {
|
||
|
|
var made := this.both(target);
|
||
|
|
while this.takes("||") {
|
||
|
|
val right := this.both(target);
|
||
|
|
made = made || right;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Reads conditions joined by `&&`.
|
||
|
|
def when_reading::both(&mut this, target: &when_target): bool {
|
||
|
|
var made := this.single(target);
|
||
|
|
while this.takes("&&") {
|
||
|
|
val right := this.single(target);
|
||
|
|
made = made && right;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Reads `!` and a condition, one in parentheses, or `key == value`.
|
||
|
|
def when_reading::single(&mut this, target: &when_target): bool {
|
||
|
|
return !this.single(target) if this.takes("!");
|
||
|
|
if this.takes("(") {
|
||
|
|
val made := this.either(target);
|
||
|
|
this.failed = this.failed || !this.takes(")");
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
val key := this.word();
|
||
|
|
val equal := this.takes("==");
|
||
|
|
if !equal && !this.takes("!=") {
|
||
|
|
this.failed = true;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
val value := this.word();
|
||
|
|
var held := false;
|
||
|
|
if key == "os" {
|
||
|
|
held = value == target.os.span;
|
||
|
|
} else if key == "arch" {
|
||
|
|
held = value == target.arch.span;
|
||
|
|
} else if key == "backend" {
|
||
|
|
held = value == target.backend.span;
|
||
|
|
} else {
|
||
|
|
this.failed = true;
|
||
|
|
}
|
||
|
|
return equal ? held : !held;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Reads the text given where it comes next, past blanks.
|
||
|
|
def when_reading::takes(&mut this, token: strview): bool {
|
||
|
|
this.skip_blanks();
|
||
|
|
return false if this.text.length - this.at < token.length || this.text.substr(this.at, token.length) != token;
|
||
|
|
this.at += token.length;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Reads a word, past blanks.
|
||
|
|
def when_reading::word(&mut this): strview {
|
||
|
|
this.skip_blanks();
|
||
|
|
val made := word_at(this.text, this.at);
|
||
|
|
this.at += made.length;
|
||
|
|
this.failed = this.failed || made.length == 0;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Reads past blanks.
|
||
|
|
def when_reading::skip_blanks(&mut this): void {
|
||
|
|
while this.at < this.text.length && (this.text[this.at] == ' ' || this.text[this.at] == '\t'
|
||
|
|
|| this.text[this.at] == '\r' || this.text[this.at] == '\n') {
|
||
|
|
this.at++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The word, letters, digits, and underscores, beginning at a place of a text.
|
||
|
|
#! @param text The text.
|
||
|
|
#! @param at The place.
|
||
|
|
#! @returns The word, which is empty where none begins there.
|
||
|
|
def word_at(text: strview, at: u64): strview {
|
||
|
|
var end := at;
|
||
|
|
while end < text.length && (text[end].is_alnum || text[end] == '_') { end++; }
|
||
|
|
return text.substr(at, end - at);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a path names a file, following a link it ends in.
|
||
|
|
#! @param at The path.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def is_file(at: strview): bool {
|
||
|
|
var found := path::parse(at) else (_) { return false; };
|
||
|
|
val info := io::info(&found) else (_) { return false; };
|
||
|
|
return info.kind == file_kind::regular;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a path names a folder, following a link it ends in.
|
||
|
|
#! @param at The path.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def is_folder(at: strview): bool {
|
||
|
|
var found := path::parse(at) else (_) { return false; };
|
||
|
|
val info := io::info(&found) else (_) { return false; };
|
||
|
|
return info.kind == file_kind::directory;
|
||
|
|
}
|