#! @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. pub struct resolver { var libraries: vector; #! The libraries modules are found in. var files: dict; #! The files found or read before, by path. var modules: dict; #! The modules whose files were found, by path, as `std::io`. var exists: dict; #! 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; #! 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`. } #! 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::::new(), files = dict::::new(), modules = dict::::new(), exists = dict::::new(), paths = vector::::new(), path_ids = keymap::new() }; # The empty path, which every path is within. made.paths.append(module_path {}); return made; } #! 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 { var made := vector::::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; } #! How many files the libraries not read whole hold, which is the most what #! refers to them can bring. #! @param this The resolver. #! @returns How many. pub def resolver::files_held(&this): u64 { var total: u64 = 0; for &each in this.libraries { total += each.whole ? 0 : count_files(each.folder.view()); } return total; } #! 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): void { var segments := vector::::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.view()[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): void { # The modules its sections are of, whose names, and those of the modules # holding them, the file sees. var homes := vector::::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.extras[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.view(), 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; val count: u32 = syntax.extras[run]; for i in 0..count { val segment: u32 = syntax.extras[run as u64 + 1 + i]; 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 { var made := vector::::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.view()) ? 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): void { return if this.paths[node].found; this.paths[node].found = true; val segments := this.segments_of(node, names); this.find_module(segments.view(), 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): void { var chain := vector::::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, names: &names, into: &mut vector): void { # The path's names, its first first. var rest := vector::::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.view()[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.view()[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, names: &names, into: &mut vector): 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 { for i in 0..this.libraries.length { return i if this.libraries[i].name.view() == 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): maybe { var found: u64 = 0; var deepest: u64 = 0; for i in 0..this.libraries.length { val depth := covered(this.libraries[i].name.view(), 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): bool { for &each in this.libraries { return true if holds(each.name.view(), 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): u64 { var at: u64 = 0; for count in 0..segments.length { val piece := segments[count].view(); 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): bool { var at: u64 = 0; for &each in segments { val piece := each.view(); 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): 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; } var at := this.within(&found, segments); var named := is_folder(at.view()) && folder_of(at.view(), key.view()); if !named { at.append(".sakura"); named = is_file(at.view()) && first_module(at.view()).view() == key.view(); } 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 { var made := string::new(nothing {}); for i in 0..segments.length { if i != 0 { made.append("::"); } made.append(segments[i].view()); } 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 { var made := string::copy_of(&this.libraries[found.index].folder); for i in found.depth..segments.length { made.push('/'); made.append(segments[i].view()); } 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, into: &mut vector): 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); var at := this.within(&found, segments); if segments.length > found.depth { var file := $"{at}.sakura"; if is_file(file.view()) && first_module(file.view()).view() == key.view() { this.add_file(file.view(), into); } } if is_folder(at.view()) { this.find_in_folder(at.view(), key.view(), 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 folder The folder. #! @param of The module's path, as `std::io`. #! @param into Where the paths of the files not found before are added. def resolver::find_in_folder(&mut this, folder: strview, of: strview, into: &mut vector): void { var listed := listing(folder); for &each in listed { if each.folder { if folder_of(each.path.view(), of) { this.find_in_folder(each.path.view(), of, into); } } else if first_module(each.path.view()).view() == of { this.add_file(each.path.view(), into); } } } #! 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): 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, segments: span): vector { var made := vector::::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.view(), segments); val rest := each.name.view().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.view() == 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.view(), 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].view()); } for &each in listing(folder.view()) { # The name is the entry's own, less its `.sakura`. var name := each.path.view().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.view(), inner.view()) : first_module(each.path.view()).view() == inner.view(); 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 { var made := vector::::new(); var at := path::parse(folder) else (_) { return made; }; var opened := io::directory::open(&at) else (_) { return made; }; var names := vector::::new(); var kinds := vector::::new(); while opened.next() is some(each) { continue if each.name.length == 0 || each.name[0] == '.'; var joined := $"{folder}/{each.name}"; var is_dir := each.kind == file_kind::directory; if each.kind == file_kind::link { is_dir = is_folder(joined.view()); } 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.view()) < 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): void { var listed := listing(folder); for &mut each in listed { if each.folder { every_file(each.path.view(), 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::::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.view()).view() == of; } for &each in listed { return true if each.folder && folder_of(each.path.view(), 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 { 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::::over((&mut buffer) as *mut u8, 4096)) else (_) { return string::new(nothing {}); }; var text := strview::over((&buffer) as *u8, count); var made := module_named(text); # A file whose first lines run past what was read is read whole. if made.length == 0 && count == 4096 { var whole := io::read_file(&found) else (_) { return made; }; made = module_named(whole.view()); } 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 { 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; } break if word != "module"; at += word.length; while at < text.length && (text[at] == ' ' || text[at] == '\t') { at++; } val start := at; while at < text.length && (text[at].is_alnum || text[at] == '_' || text[at] == ':') { at++; } return string::from(text.substr(start, at - start), nothing {}); } return string::new(nothing {}); } #! 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; }