/** * A WIT root's document, read through the compiler host. * * The WIT parser takes source text and reads nothing. The root of a * WIT-backed package, or of a `--wit` world, is a path: one `.wit` * file, or a directory of them. This reads it the way the compiler * reads modules, through `CompilerHost`, so a host that keeps its files * in memory serves WIT exactly as it serves source. */ import { WitFile, WitFileStart, WitDocument, joinWitDocument } from 'zena-lang:wit-parser/document'; import { CompilerHost } from './library-loader.zena'; import { growable } from 'zena:core'; /** * The WIT document at `path`: one file, or a directory's `.wit` files * joined by `joinWitDocument`. * * A directory's files are named by their path rather than by the bare * name the host listed, so a diagnostic against the document names a * file the reader can open. Every name under one directory shares its * prefix, so this does not change the order they are joined in. * * A single file is its own document, taken verbatim: `--wit` names it * directly, so it is read whatever it is called. */ export async function readWitDocument(host: CompilerHost, path: String): Future { let names = await host.listDir(path); if (names == null) { let starts = growable([new WitFileStart(path, 0)]); return new WitDocument(await host.readFile(path), starts); } let files = new GrowableArray(); for (let name in names as GrowableArray) { if (name.endsWith(".wit")) { files.push(new WitFile(`${path}/${name}`, await host.readFile(`${path}/${name}`))); } } if (files.length == 0) { throw new Error("No .wit files in directory: " + path); } return joinWitDocument(files); } /** * `readWitDocument`'s source alone, for a caller with nothing to report * against it. */ export async function readWitSource(host: CompilerHost, path: String): Future { return (await readWitDocument(host, path)).source; }