#! @file reading.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains how a blossomfile is read into a project: from YAML's document, #! whose values keep their lines and tags, with every key checked against those #! a mapping takes, a key written twice refused, and each `${NAME}` replaced by #! the option's value as the text is read. import std::io; import std::yaml; module blossom::models; #! The systems a target may be built for, as `--target` names them. pub val platforms: strview[2] = ["linux-x86_64", "windows-x86_64"]; #! Why a blossomfile couldn't be read: where, and what's wrong. pub struct problem { pub var file: string; #! The blossomfile, as its path is written. pub var line: u32; #! The line, counted from 1, or 0 where it's no line's. pub var message: string; #! What's wrong, in a sentence. } #! Says what's wrong, after where. #! @param this The problem. #! @returns The sentence, as `blossomfile:4: ...`. pub def problem::describe(&this): string { if this.line == 0 { return $"{this.file.span}: {this.message.span}"; } return $"{this.file.span}:{this.line}: {this.message.span}"; } #! Reads a blossomfile. #! @param text Its text. #! @param file Its path, absolute. #! @param overrides Values for its options, as `-o` gives them, which must #! each name one it declares. #! @param platform What every target is built for, as `--target` says, or #! empty, which goes into its output folder's name. #! @returns The project, or the first problem the file has. pub def read_project( text: strview, file: &path, overrides: span, platform: strview ): result { var at := reading { file = file.span, options = vector::::new() }; var folder := file.parent() else { return problem { file = string::from(file.span, nothing {}), message = string::from("A blossomfile is in a folder.", nothing {}) }; }; var made := project { file = path::copy_of(file), folder = move(folder) }; at.read(text, &mut made, overrides, platform); if at.failed { return move(at.problem); } return move(made); } # What reading a blossomfile goes through: its file, the options so far, which # `${NAME}` is replaced by, and the first problem found. struct reading { var file: strview; var options: vector; var failed: bool; var problem: problem; } # Notes the first problem, at a line. def reading::fail(&mut this, line: u32, message: string): void { return if this.failed; this.failed = true; this.problem = problem { file = string::from(this.file, nothing {}), line = line, message = move(message) }; } # Reads the whole file into a project. def reading::read(&mut this, text: strview, made: &mut project, overrides: span, platform: strview): void { val doc := yaml::parse(text) else (e) { this.fail(e.line, string::from(yaml::describe(e.kind), nothing {})); return; }; val root := doc.root; if root.is_null { this.fail(0, string::from("The blossomfile is empty: it needs `name`, `gpid`, and `semv`.", nothing {})); return; } return if !this.keys(&root, ["name", "gpid", "semv", "options", "requires", "targets", "tests", "exports", "install", "package"], "the blossomfile"); # Who it is comes first, then its options, which every text after may use. made.name = this.needed(&root, "name"); made.gpid = this.needed(&root, "gpid"); made.semv = this.needed(&root, "semv"); return if this.failed; if !is_version(made.semv.span) { this.fail(root.get("semv").some.place.line, $"`{made.semv.span}` is not a version, as `1.2.0` or `1.2.0-alpha`."); return; } if root.get("options") is some(options) { return if !this.keys(&options, span::::blank(), "options"); for each in options.members() { if each.name == "bin" { this.fail(each.key.place.line, string::from("`bin` is blossom's: it names the folder of the programs built.", nothing {})); return; } var value := this.text(&each.value, "An option's value"); for &given in overrides { if given.name.span == each.name { value = this.expanded(given.value.span, 0); } } this.options.append(setting { name = string::from(each.name, nothing {}), value = move(value) }); } } for &given in overrides { var declared := false; for &each in this.options { declared = declared || each.name == given.name; } if !declared { this.fail(0, $"`{given.name.span}`, given a value, names no option of {made.identity.span}."); return; } } for &each in this.options { made.options.append(setting { name = string::copy_of(&each.name), value = string::copy_of(&each.value) }); } made.key = output_key(made.identity.span, made.options.span, platform); val bin := made.output.join("bin"); this.options.append(setting { name = string::from("bin", nothing {}), value = string::from(bin.span, nothing {}) }); return if this.failed; if root.get("requires") is some(requires) { for each in this.list(&requires, "`requires`") { made.requires.append(this.requirement(&each)); return if this.failed; } } if root.get("targets") is some(targets) { for each in this.list(&targets, "`targets`") { made.targets.append(this.target(&each, false)); return if this.failed; } } if root.get("tests") is some(tests) { for each in this.list(&tests, "`tests`") { made.tests.append(this.target(&each, true)); return if this.failed; } } if root.get("exports") is some(exports) { made.exports = this.texts(&exports, "`exports`"); } if root.get("install") is some(install) { for each in this.list(&install, "`install`") { made.install.append(this.step(&each)); return if this.failed; } } if root.get("package") is some(package) { return if !this.keys(&package, ["lic", "desc", "auth", "url"], "`package`"); made.package = package_info { lic = this.optional(&package, "lic"), desc = this.optional(&package, "desc"), auth = this.optional(&package, "auth"), url = this.optional(&package, "url") }; } return if this.failed; this.validate(made, &root); } # Reads a requirement: its text alone, as `REMOTE@REF` or a folder, or the block # of `remote`, `branch`, and `options`. def reading::requirement(&mut this, value: &yaml::value): requirement { val line := value.place.line; if value.kind != yaml::kind::mapping { val text := this.text(value, "A requirement"); var made := requirement { line = line }; if is_folder_text(text.span) { made.remote = string::copy_of(&text); made.local = true; return made; } val split := ref_at(text.span); made.remote = string::from(text.span.substr(0, split), nothing {}); made.branch = split < text.length ? string::from(text.span.substr(split + 1, text.length - split - 1), nothing {}) : string::from("main", nothing {}); if made.remote.length == 0 || made.branch.length == 0 { this.fail(line, $"`{text.span}` is no requirement: write `REMOTE@REF` or a folder, as `../shapes`."); } return made; } var made := requirement { line = line }; return made if !this.keys(value, ["remote", "branch", "options"], "a requirement"); made.remote = this.needed(value, "remote"); made.local = is_folder_text(made.remote.span); made.branch = string::from("main", nothing {}); if value.get("branch") is some(branch) { made.branch = this.text(&branch, "`branch`"); if made.local { this.fail(branch.place.line, string::from("A folder takes no `branch`.", nothing {})); } } if value.get("options") is some(options) { return made if !this.keys(&options, span::::blank(), "a requirement's options"); for each in options.members() { made.options.append(setting { name = string::from(each.name, nothing {}), value = this.text(&each.value, "An option's value") }); } } return made; } # Reads a target, or a test, which takes the keys of tests too. def reading::target(&mut this, value: &yaml::value, test: bool): target { var made := target { line = value.place.line }; if test { return made if !this.keys(value, ["name", "type", "level", "petal", "target", "deps", "srcs", "opts", "runs", "checks", "args"], "a test"); } else { return made if !this.keys(value, ["name", "type", "level", "petal", "target", "deps", "srcs", "opts", "root"], "a target"); } made.name = this.needed(value, "name"); val kind := this.needed(value, "type"); return made if this.failed; if kind == "program" { made.kind = target_kind::program; } else if kind == "library" && !test { made.kind = target_kind::library; } else { val line := value.get("type").some.place.line; this.fail(line, test ? $"A test's `type` is `program`, not `{kind.span}`." : $"`type` is `program` or `library`, not `{kind.span}`."); return made; } if made.kind == target_kind::library { for key in ["level", "petal", "target", "srcs", "opts"] { if value.get(key) is some(given) { this.fail(given.place.line, $"A library takes no `{key}`: it's read by the programs that import it."); return made; } } if !is_library_name(made.name.span) { this.fail(made.line, $"`{made.name.span}` names no module: a library's name is its module's, with `.` for `::`, as `hello.geometry`."); return made; } made.root = $"./{made.name.span}"; if value.get("root") is some(root) { made.root = this.text(&root, "`root`"); } } else { if value.get("root") is some(root) { this.fail(root.place.line, string::from("Only a library takes a `root`.", nothing {})); return made; } if !is_file_name(made.name.span) { this.fail(made.line, $"`{made.name.span}` can't name a program's file: write letters, digits, `.`, `_`, or `-`."); return made; } } if value.get("level") is some(level) { val text := this.text(&level, "`level`"); if text != "0" && text != "1" && text != "2" { this.fail(level.place.line, $"`level` is 0, 1, or 2, not `{text.span}`."); return made; } made.level = (text[0] - '0') as u32; } if value.get("petal") is some(petal) { made.petal = this.boolean(&petal, "`petal`"); } if value.get("target") is some(system) { made.system = this.text(&system, "`target`"); if !is_platform(made.system.span) { this.fail(system.place.line, $"`{made.system.span}` is no target: write `linux-x86_64` or `windows-x86_64`."); return made; } } if value.get("deps") is some(deps) { made.deps = this.texts(&deps, "`deps`"); } if value.get("srcs") is some(srcs) { made.srcs = this.texts(&srcs, "`srcs`"); } if value.get("opts") is some(opts) { made.opts = this.texts(&opts, "`opts`"); } if value.get("args") is some(args) { made.args = this.texts(&args, "`args`"); } made.checks = true; if value.get("runs") is some(runs) { val text := this.text(&runs, "`runs`"); if text == "native" { made.runs = runner::native; made.checks = false; } else if text != "machine" { this.fail(runs.place.line, $"`runs` is `machine` or `native`, not `{text.span}`."); return made; } } if value.get("checks") is some(checks) { made.checks = this.boolean(&checks, "`checks`"); } # A program needs files, which a test finds in `tests/` unless it says. if made.kind == target_kind::program && made.srcs.length == 0 { if !test { this.fail(made.line, $"`{made.name.span}` needs `srcs`: its files, folders, or globs."); return made; } made.srcs.append(string::from("tests/**/*.sakura", nothing {})); } return made; } # Reads a step of installing: a tag saying what it does, and its text. def reading::step(&mut this, value: &yaml::value): step { val line := value.place.line; var made := step { line = line }; val tag := value.tag; if tag is none { this.fail(line, string::from("A step of `install` says what it does with a tag, as `!include ./share`.", nothing {})); return made; } if tag.some != "!include" { this.fail(line, $"`{tag.some}` is no step blossom knows: it knows `!include`."); return made; } made.tag = string::from(tag.some, nothing {}); val text := this.text(value, "A step"); val split := text.span.find(":"); if split is some(at) { made.source = string::from(text.span.substr(0, at), nothing {}); made.dest = string::from(text.span.substr(at + 1, text.length - at - 1), nothing {}); } else { made.source = string::copy_of(&text); } if made.source.length == 0 || !is_within(made.source.span) { this.fail(line, $"`{made.source.span}` is outside the project: a source is a path within it, as `./share`."); } else if text.span.find(":") is some && (made.dest.length == 0 || !is_within(made.dest.span)) { this.fail(line, $"`{made.dest.span}` is outside the prefix: a destination is a path within it, as `share/doc`."); } return made; } # Checks what reading each part couldn't: names given once, exports that are # libraries, and libraries' roots within the project. def reading::validate(&mut this, made: &mut project, root: &yaml::value): void { val count := made.targets.length + made.tests.length; for k in 0..count { val each := &made.at(k); for j in 0..k { if made.at(j).name == each.name { this.fail(each.line, $"`{each.name.span}` names two targets."); return; } } if each.kind == target_kind::library && !is_within(each.root.span) { this.fail(each.line, $"`{each.root.span}` is outside the project: a library's root is a folder within it."); return; } } for &name in made.exports { val found := made.find(name.span); if found is none || found.some >= made.targets.length || made.targets[found.some].kind != target_kind::library { this.fail(root.get("exports").some.place.line, $"`{name.span}` is exported, but it's no library of {made.identity.span}."); return; } } } # Checks a mapping's keys: each one of those allowed, unless none are named, # where any is, and none written twice, which YAML would keep only one of. def reading::keys(&mut this, map: &yaml::value, allowed: span, what: strview): bool { if map.kind != yaml::kind::mapping { this.fail(map.place.line, $"{what} is a mapping of keys and values."); return false; } var seen := vector::::new(); for each in map.members() { val line := each.key.place.line; var known := allowed.length == 0; for &name in allowed { known = known || name == each.name; } if !known { this.fail(line, $"`{each.name}` is no key of {what}."); return false; } for &prior in seen { if prior == each.name { this.fail(line, $"`{each.name}` is written twice in {what}."); return false; } } seen.append(each.name); } return true; } # The elements of a sequence, or none where the value is another kind, which # is noted. def reading::list(&mut this, value: &yaml::value, what: strview): yaml::value::element_cursor { if value.kind != yaml::kind::sequence && !value.is_null { this.fail(value.place.line, $"{what} is a list, of lines starting with `-`."); } return value.elements(); } # A scalar's text, with options replaced. def reading::text(&mut this, value: &yaml::value, what: strview): string { val read := value.as_scalar() else (_) { this.fail(value.place.line, $"{what} is a scalar, not a list or a mapping."); return string::new(nothing {}); }; return this.expanded(read, value.place.line); } # The texts of a list of scalars. def reading::texts(&mut this, value: &yaml::value, what: strview): vector { var made := vector::::new(); for each in this.list(value, what) { made.append(this.text(&each, $"Each of {what}".span)); } return made; } # A key's text, which must be written. def reading::needed(&mut this, map: &yaml::value, key: strview): string { if map.get(key) is some(value) { var made := this.text(&value, $"`{key}`".span); if made.length == 0 { this.fail(value.place.line, $"`{key}` is empty."); } return made; } this.fail(map.place.line, $"`{key}` is needed."); return string::new(nothing {}); } # A key's text, or empty where it isn't written. def reading::optional(&mut this, map: &yaml::value, key: strview): string { if map.get(key) is some(value) { return this.text(&value, $"`{key}`".span); } return string::new(nothing {}); } # A scalar's truth, written `true` or `false`. def reading::boolean(&mut this, value: &yaml::value, what: strview): bool { val text := this.text(value, what); if text != "true" && text != "false" { this.fail(value.place.line, $"{what} is `true` or `false`, not `{text.span}`."); } return text == "true"; } # Text with each `${NAME}` replaced by the option's value, noting a name that's # no option's. def reading::expanded(&mut this, text: strview, line: u32): string { var made := string::new(nothing {}); var at := 0u; while at < text.length { if text[at] == '$' && at + 1 < text.length && text[at + 1] == '{' { val rest := text.substr(at + 2, text.length - at - 2); if rest.find("}") is some(length) { val name := rest.substr(0, length); var found := false; for &each in this.options { if !found && each.name.span == name { made.append(each.value.span); found = true; } } if !found { var message := string::from("`$", nothing {}); message.push('{'); message.append(name); message.append("}` names no option declared before it."); this.fail(line, move(message)); return made; } at += length + 3; continue; } } made.push(text[at]); at++; } return made; } #! The name of a project's output folder: the 64-bit hash of its identity, its #! options' values, and what its targets are built for, in hex. #! @param identity As `malunal/hello@1.0.0`. #! @param options Its options' values. #! @param platform What `--target` says, or empty. #! @returns Sixteen hex digits. pub def output_key(identity: strview, options: span, platform: strview): string { var text := string::from(identity, nothing {}); # In order by name, so writing the options in another order changes nothing. var order := vector::::new(); for k in 0..options.length { var at := order.length; while at > 0 && options[order[at - 1]].name.span.compare(options[k].name.span) > 0 { at--; } order.insert_at(at, k); } for k in order { text.append($"\n{options[k].name.span}={options[k].value.span}".span); } text.append($"\ntarget={platform}".span); return hex(hash(&text)); } #! A number as sixteen hex digits. #! @param value The number. #! @returns Its digits, the highest first. pub def hex(value: u64): string { val digits: strview = "0123456789abcdef"; var made := string::new(nothing {}); var shift := 64u; while shift > 0 { shift -= 4; made.push(digits[(value >> shift) & 15]); } return made; } #! Whether text is a version: three numbers, as `1.2.0`, then a `-` and what #! comes before release, as `-alpha`, and a `+` and what it was built from, as #! semantic versioning has them. #! @param text The text. #! @returns Whether it is one. pub def is_version(text: strview): bool { var at := 0u; for part in 0..3 { if part > 0 { return false if at >= text.length || text[at] != '.'; at++; } val start := at; while at < text.length && text[at].is_digit { at++; } return false if at == start; } if at < text.length && (text[at] == '-' || text[at] == '+') { at++; return false if at == text.length; while at < text.length { val byte := text[at]; return false if !(byte.is_alnum || byte == '.' || byte == '-' || byte == '+'); at++; } } return at == text.length; } #! Whether a requirement's text is a folder rather than a remote: a path #! starting with `.` or a root. #! @param text The text. #! @returns Whether it is. pub def is_folder_text(text: strview): bool { return text.length != 0 && (text[0] == '.' || text[0] == '/' || text[0] == '\\' || (text.length > 1 && text[1] == ':' && text[0].is_alpha && text.find("://") is none)); } # Where the `@` before a remote's ref is: the first after the remote's last `/` # or `:`, or the text's length where there is none. def ref_at(text: strview): u64 { var from := 0u; for k in 0..text.length { if text[k] == '/' || text[k] == ':' { from = k + 1; } } for k in from..text.length { return k if text[k] == '@'; } return text.length; } # Whether a name is a module's, with `.` for `::`, as `hello.geometry`, and not # one of the toolchain's own. def is_library_name(name: strview): bool { return false if name == "std" || name == "core" || name == "system" || name == "runtime"; var start := 0u; for k in 0..=name.length { if k == name.length || name[k] == '.' { return false if k == start || name[start].is_digit; start = k + 1; continue; } return false if !name[k].is_ident; } return true; } # Whether a name can be a program's file. def is_file_name(name: strview): bool { return false if name.length == 0 || name == "." || name == ".."; for k in 0..name.length { val byte := name[k]; return false if !(byte.is_alnum || byte == '.' || byte == '_' || byte == '-'); } return true; } #! Whether `--target`, or a target's `target`, names a system blossom builds for. #! @param text The name. #! @returns Whether it does. pub def is_platform(text: strview): bool { for &each in platforms { return true if each == text; } return false; } # Whether a path stays within the folder it's taken from: relative, with no `..`. def is_within(text: strview): bool { return false if text.length == 0 || text[0] == '/' || text[0] == '\\' || (text.length > 1 && text[1] == ':'); for part in text.split('/') { return false if part == ".."; for inner in part.split('\\') { return false if inner == ".."; } } return true; }