#! @file program.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the files a check reads, and the program they make: `core`, every #! `.sakura` file of the workspace folders, and the modules of `system` and the #! standard library those refer to, with the runtime where an executable is #! checked for, read as a check reads them, each with the #! text open in the editor where it is open, and otherwise its text on disk, #! lexed and parsed, and those of one program checked. #! #! Where several files declare `main`, each begins a program holding every #! file that declares none, and one is checked at a time, so their `main`s do #! not clash: that of the file last opened, edited, or asked about where it #! declares `main`, else the one checked last, else the one whose file comes #! first by path. The rest are parsed, so their mistakes of syntax are shown, #! but nothing in them is declared or checked. #! #! Once made, a program is brought up to date with the files as they are by #! parsing again only what changed of each file that changed, and checking #! again only what that affects, as `checker::update` does. import std::io; import system; import sakura::analyzer; module sakura::server; #! A document open in the editor. pub struct document { pub var uri: string; #! The URI the editor names it by. pub var path: string; #! Its path, or empty where it is no file, as an unsaved one. pub var text: string; #! Its text, as the editor has it. } #! A file a check reads: its path, and the text it is read with. pub struct gathered { pub var path: string; #! The file's path. pub var text: string; #! Its text. } #! What the files of a check are gathered from. pub struct gathering { pub var core: strview; #! `core`'s directory, or empty. pub var system: strview; #! `system`'s directory, or empty. pub var runtime: strview; #! The runtime's directory, or empty. pub var libraries: span; #! The libraries, each as `NAME=DIR`, the standard library's among them. pub var roots: &vector; #! The workspace folders. pub var documents: &vector; #! The documents open in the editor. } #! The most directories deep the folders are read. const deepest: u32 = 64; #! A name a directory holds, and what it names. struct listed { var name: string; var kind: file_kind; } #! Gathers the files a check reads before what they refer to: the types the #! syntax makes and `core`'s, then each of the workspace folders', or the #! documents open in the editor where there is no folder, and any of `system`, #! the runtime, and the libraries, the standard library's among them, open in #! the editor, whose modules are read whole. #! Each is read with the text open in the editor where it is open. #! @param from What they are gathered from. #! @returns The files. pub def gather_all(from: &gathering): vector { var made := vector::::new(); if from.core.length != 0 { made.append(gathered { path = string::from(builtin_path, nothing {}), text = string::from(builtin_text, nothing {}) }); gather_directory(from, from.core, &mut made, 0, true); } for &each in from.documents { if is_sakura(each.path.span) && (within_folder(each.path.span, from.system) || within_folder(each.path.span, from.runtime) || within_libraries(from, each.path.span)) { gather_file(from, each.path.span, &mut made); } } for &root in from.roots { gather_directory(from, root.span, &mut made, 0, false); } # Without a workspace folder, the files open in the editor are the program. if from.roots.length == 0 { for &each in from.documents { if each.path.length != 0 && is_sakura(each.path.span) { gather_file(from, each.path.span, &mut made); } } } return made; } #! The directory of a library given as `NAME=DIR`. #! @param given The library. #! @returns The directory, or empty where none is given. def library_folder(given: strview): strview { val split := given.index_of(&'=') else { return strview::blank(); }; return given.substr(split + 1, given.length - split - 1); } #! Whether a path is one of the libraries' directories. #! @param from What the files are gathered from. #! @param at The path. #! @returns Whether it is. def is_library_folder(from: &gathering, at: strview): bool { for &each in from.libraries { return true if library_folder(each.span) == at; } return false; } #! Whether a path is within one of the libraries' directories. #! @param from What the files are gathered from. #! @param at The path. #! @returns Whether it is. def within_libraries(from: &gathering, at: strview): bool { for &each in from.libraries { return true if within_folder(at, library_folder(each.span)); } return false; } #! Whether a path is within a folder. #! @param at The path. #! @param folder The folder, or empty, which holds nothing. #! @returns Whether it is. def within_folder(at: strview, folder: strview): bool { return folder.length != 0 && at.length > folder.length && at.starts_with(folder) && at[folder.length] == '/'; } #! The resolver of the libraries a check reads: `core`, read whole, and #! `system`, the runtime, and the libraries, the standard library's among them, #! read by what refers to them. #! @param from What the files are gathered from. #! @returns The resolver. pub def library_finder(from: &gathering): resolver { var made := resolver::new(); if from.core.length != 0 { made.whole("core", from.core); } if from.system.length != 0 { made.add_library("system", from.system); } if from.runtime.length != 0 { made.add_library("runtime", from.runtime); } for &each in from.libraries { val split := each.span.index_of(&'=') else { continue; }; made.add_library(each.span.substr(0, split), library_folder(each.span)); } return made; } #! The text of a library's file, as the editor has it where it is open, and #! otherwise as it is on disk, as much of it as is read for the target, or #! empty where it cannot be read. #! @param from What the files are gathered from. #! @param at The file's path. #! @param keep How many bytes of the file on disk are read, as the #! resolver's `cut_of` says, or 0 for all of them. #! @returns The text. def library_text(from: &gathering, at: strview, keep: u64): string { for &each in from.documents { return string::copy_of(&each.text) if each.path.span == at; } var text := string::new(nothing {}); _ = read_text(at, &mut text); if keep != 0 && keep < text.length { text.resize(keep); } return text; } #! Adds a file to what is gathered, once, with the text open in the editor where #! it is open, and otherwise its text on disk. #! @param from What the files are gathered from. #! @param at The file's path. #! @param into What is gathered so far. def gather_file(from: &gathering, at: strview, into: &mut vector): void { for &each in into { return if each.path.span == at; } for &each in from.documents { if each.path.span == at { into.append(gathered { path = string::from(at, nothing {}), text = string::copy_of(&each.text) }); return; } } var text := string::new(nothing {}); return if !read_text(at, &mut text); into.append(gathered { path = string::from(at, nothing {}), text = move(&mut text) }); } #! Gathers the `.sakura` files of a directory and those within it, by name, #! passing over hidden directories, `node_modules`, and, within a workspace #! folder, the libraries, which are gathered on their own. A link to a #! directory is not followed, so a link cannot lead the reading in a circle. #! @param from What the files are gathered from. #! @param at The directory's path. #! @param into What is gathered so far. #! @param depth How deep the directory is. #! @param library Whether it is a library's, as `core`'s. def gather_directory(from: &gathering, at: strview, into: &mut vector, depth: u32, library: bool): void { return if depth > deepest; var found := path::parse(at) else (_) { return; }; var listing := io::directory::open(&found) else (_) { return; }; var entries := vector::::new(); while listing.next() is some(each) { continue if each.name.length == 0 || each.name[0] == '.' || each.name == "node_modules"; entries.append(listed { name = string::from(each.name, nothing {}), kind = each.kind }); } # Read in the order of their names, so a check reads the files alike however # the system lists them. entries.sort_by(def(a: &listed, b: &listed): bool { return a.name.compare(b.name.span) < 0; }); var inner := vector::::new(); for &each in entries { var joined := $"{at}/{each.name}"; var kind: file_kind = each.kind; if kind == file_kind::link { var linked := path::parse(joined.span) else (_) { continue; }; val info := io::info(&linked) else (_) { continue; }; continue if info.kind == file_kind::directory; kind = info.kind; } if kind == file_kind::directory { # `core`, `system`, the runtime, and the libraries within a workspace # folder are gathered on their own. continue if !library && is_library_folder(from, joined.span); continue if !library && from.core.length != 0 && joined.span == from.core; continue if !library && from.system.length != 0 && joined.span == from.system; continue if !library && from.runtime.length != 0 && joined.span == from.runtime; inner.append(move(&mut joined)); } else if kind == file_kind::regular && is_sakura(each.name.span) { gather_file(from, joined.span, into); } } for &each in inner { gather_directory(from, each.span, into, depth + 1, library); } } #! What says none, of a file a program leaves out or a program begun by none. pub const none: u32 = 0xFFFFFFFF; #! The files of a check, lexed and parsed, and which make the program checked. #! The checker refers to what this holds, so once it is made this stays where #! it is. pub struct program { pub var names: names; #! The names the files were lexed with. pub var parse_notes: vector>; #! What lexing and parsing noted of each file. pub var check_notes: vector; #! What checking noted, each by the checker's index of its file. pub var paths: vector; #! The path of each file. pub var texts: vector; #! The text each was parsed with. pub var sources: vector; #! Each file's source. pub var syntaxes: vector; #! What each parsed into. pub var roots: vector; #! The `document` node of each. pub var mains: vector; #! Whether each declares `main`. pub var units: vector; #! The checker's index of each, or `none` where the program leaves it out. pub var files: vector; #! The file of each of the checker's indexes. pub var entry: u32; #! The file declaring `main` the program begins with, or `none`. pub var strict: bool; #! Whether files with mistakes of syntax are left out, as a check without drafts does. pub var native: bool; #! Whether it is checked for an executable. pub var system: strview; #! The operating system it is checked for, as `linux`. pub var given: u64; #! How many of the files were gathered; the rest are the libraries' files they refer to. var finder: resolver; #! What found the libraries' files, which finds what an edit refers to anew. } #! Lexes and parses the files gathered, then the files of the libraries' modules #! they refer to, a round at a time, as a check reads them, ready to be checked #! as a program. #! @param files The files gathered. #! @param strict Whether files with mistakes of syntax are left out of the #! program. #! @param native Whether it is checked for an executable, which reads the #! runtime too. #! @param system The operating system it is checked for, as `linux`. #! @param from What the files were gathered from, which says where the #! libraries are. #! @returns The files, parsed. pub def program::parse(files: &vector, strict: bool, native: bool, system: strview, from: &gathering): program { var made := program { names = names::new(), parse_notes = vector::>::new(), check_notes = vector::::new(), paths = vector::::new(), texts = vector::::new(), sources = vector::::new(), syntaxes = vector::::new(), roots = vector::::new(), mains = vector::::new(), units = vector::::new(), files = vector::::new(), entry = none, strict = strict, native = native, system = system, given = files.length, finder = library_finder(from) }; made.finder.read_for(system, "x86_64", native ? "native" : "vm"); # Each file's path, source, and syntax stay where they are once the checker # refers to them, which is once every file is read. for &each in files { made.finder.mark(each.path.span); made.add(each.path.span, &each.text); } # What the files refer to is read after them, until a round refers to # nothing new. var done: u64 = 0; while done < made.paths.length { var found := vector::::new(); if done == 0 && native { made.finder.want("runtime", &mut found); } val to := made.paths.length; for i in done..to { made.finder.refer(&made.syntaxes[i], &made.names, &mut found); } done = to; for &each in found { val text := library_text(from, each.span, made.finder.cut_of(each.span)); made.add(each.span, &text); } } return made; } #! Adds a file to the program, lexed and parsed. #! @param this The program. #! @param path The file's path. #! @param text Its text. def program::add(&mut this, path: strview, text: &string): void { val i := this.paths.length; this.paths.append(string::from(path, nothing {})); this.texts.append(string::copy_of(text)); this.parse_notes.append(vector::::new()); this.sources.append(source::from(this.paths[i].span, text.span)); this.syntaxes.append(syntax::new()); val main := this.names.intern(&"main"); var lexing := lexer::new(&mut this.sources[i], &mut this.names, &mut this.parse_notes[i], i as u32); lexing.tokenize_all(&mut this.syntaxes[i]); var parsing := parser::new(&mut this.syntaxes[i], &this.names, &mut this.parse_notes[i], i as u32); this.roots.append(parsing.parse()); this.mains.append(this.syntaxes[i].declares_main(this.roots[i], main)); } #! Whether a file has mistakes of syntax. #! @param this The program. #! @param file The file. #! @returns Whether it has. pub def program::broken(&this, file: u32): bool { return this.parse_notes[file].length != 0; } #! The text of a file as its source holds it, which the places the checker #! gives are within: the text it was parsed with, but with each CR LF line #! break the LF alone. A position of the protocol is the same in either, since #! it counts lines, and characters within a line, which the CR ends. #! @param this The program. #! @param file The file. #! @returns The text. pub def program::source_text(&this, file: u32): strview { val held := &this.sources[file]; return held.make_lexeme(0, held.length); } #! The file declaring `main` a program begins with: the one last opened, edited, #! or asked about where it declares `main`, else the one checked last, else the #! one whose path comes first. #! @param this The program. #! @param touched The path of the file last opened, edited, or asked about, or #! empty. #! @param last The path of the file the last program began with, or empty. #! @returns The file, or `none` where none declares `main`. pub def program::choose(&this, touched: strview, last: strview): u32 { var chosen := none; for i in 0..this.paths.length { if this.mains[i] && this.paths[i].span == touched && this.usable(i as u32) { chosen = i as u32; } } return chosen if chosen != none; for i in 0..this.paths.length { if this.mains[i] && this.paths[i].span == last && this.usable(i as u32) { chosen = i as u32; } } return chosen if chosen != none; for i in 0..this.paths.length { continue if !this.mains[i] || !this.usable(i as u32); if chosen == none || this.paths[i].compare(this.paths[chosen].span) < 0 { chosen = i as u32; } } return chosen; } #! Whether a file may be checked: any, unless the program is strict, which #! leaves out files with mistakes of syntax. #! @param this The program. #! @param file The file. #! @returns Whether it may be. def program::usable(&this, file: u32): bool { return !this.strict || !this.broken(file); } #! Sets what a checker checks a program for, which `when` decides by: the #! operating system, and the backend of an executable where it is checked for #! one. #! @param checker The checker. #! @param system The operating system, as `linux`. #! @param native Whether it is checked for an executable. pub def target_checker(checker: &mut checker, system: strview, native: bool): void { checker.target.os = checker.names.intern(&system); if native { checker.target.backend = checker.names.intern(&"native"); } } #! Gives the checker the files of the program the file begins: it, and every #! file declaring no `main`. #! @param this The program. #! @param checker The checker, made of this program's names and notes. #! @param entry The file declaring `main`, or `none`. pub def program::begin(&mut this, checker: &mut checker, entry: u32): void { this.entry = entry; target_checker(checker, this.system, this.native); checker.shelves = this.finder.held(); for i in 0..this.paths.length { val file := i as u32; val taken := (!this.mains[i] || file == entry) && this.usable(file); if !taken { this.units.append(none); continue; } this.units.append(checker.add(&this.sources[i], &this.syntaxes[i], this.roots[i], i < this.given)); this.files.append(file); } } #! What bringing a program up to date with the files found. pub enum synced : u8 { same, #! It is up to date, having checked again what changed. stale, #! The files moved on in a way it cannot follow: some are new or gone, or another program is to be checked. } #! Brings a program up to date with the files as they are now: each file whose #! text changed is parsed again where it changed, and what that affects is #! checked again. Where files are new or gone, a file refers to a module not #! read, or a file's `main` changes which program is checked, the program #! cannot follow, and says so. #! @param this The program. #! @param checker Its checker. #! @param files The files as they are now. #! @param touched The path of the file last opened, edited, or asked about. #! @param last The path of the file the last program began with. #! @param checks Whether what changed is checked again, rather than only #! parsed again, as for a check, which checks the program whole. #! @param from What the files were gathered from, which the libraries' #! files are read again from. #! @returns Whether it is up to date. pub def program::sync( &mut this, checker: &mut checker, files: &vector, touched: strview, last: strview, checks: bool, from: &gathering ): synced { return synced::stale if files.length != this.given; for i in 0..files.length { return synced::stale if files[i].path.span != this.paths[i].span; } val main := this.names.intern(&"main"); for i in 0..this.paths.length { val now := i < this.given ? string::copy_of(&files[i].text) : library_text(from, this.paths[i].span, this.finder.cut_of(this.paths[i].span)); continue if now.span == this.texts[i].span; val file := i as u32; val was_broken := this.broken(file); var fresh_source := source::from(this.paths[i].span, now.span); var fresh := syntax::new(); var fresh_notes := vector::::new(); var lexing := lexer::new(&mut fresh_source, &mut this.names, &mut fresh_notes, file); lexing.tokenize_all(&mut fresh); this.roots[i] = this.syntaxes[i].reparse(&mut fresh, &this.names, this.roots[i], &mut this.parse_notes[i], &mut fresh_notes, file); this.sources[i] = move(&mut fresh_source); this.texts[i] = string::copy_of(&now); # A file referring to a module not read changes what the program holds. var found := vector::::new(); this.finder.refer(&this.syntaxes[i], &this.names, &mut found); return synced::stale if found.length != 0; # A file changing whether it declares `main`, or a strict program's file # gaining or losing its mistakes, changes what the program holds. val mains := this.syntaxes[i].declares_main(this.roots[i], main); return synced::stale if mains != this.mains[i]; return synced::stale if this.strict && this.broken(file) != was_broken; if this.units[i] != none && checks { checker.update(this.units[i], &this.sources[i], this.roots[i]); } } return synced::stale if this.choose(touched, last) != this.entry; return synced::same; } #! Gives a checker of its own the program whole, to check it as `sakura check` #! would, the files in the order the program's checker has them, so each has #! the same index in both. #! @param this The program. #! @param checker The checker, made of this program's names and notes of its #! own. pub def program::give_whole(&mut this, checker: &mut checker): void { target_checker(checker, this.system, this.native); for &file in this.files { checker.add(&this.sources[file], &this.syntaxes[file], this.roots[file], file < this.given); } } #! The file of a path, or `none`. #! @param this The program. #! @param at The path. #! @returns The file, or `none`. pub def program::file_of(&this, at: strview): u32 { for i in 0..this.paths.length { return i as u32 if this.paths[i].span == at; } return none; }