From bbe505c8d48e822c012c9820dfe93b83dc61bcab Mon Sep 17 00:00:00 2001 From: Ralph Amissah Date: Thu, 24 Sep 2026 16:33:53 -0400 Subject: --validate, and a second reader, in Gleam --validate: the specification, executed. A validator written from ssp-format-specification.md and from nothing else, so that a file can be held against the document rather than against an implementation. Each finding names the specification section it comes from, so a disagreement can be taken back to the text. It checks the version banner and its major part, the file shape and the order of the preamble blocks and the nine sections, the two spaces of a preamble pair, the object declaration and its sixteen kinds and eight heading levels, that a property is one of the 42 and in the emitted order, that text lines carry their prefix, the ten keys of doc_has, and that no line is of a shape the specification does not define. Every .ssp of the markup document samples validates clean. Five classes of deliberate breakage are caught: an unknown major version, a property that is not one of the 42, a property out of order, a kind that is not one of the sixteen, and a block that is neither preamble nor section. Writing it found two rules the specification implied and did not state (repeated properties are consecutive; no property follows the text), and both were written into it. One repository, a reader per language. The readers are not separate tools that share a name. They are the same specification implemented more than once, and the interesting thing about them is that they agree, so they are published as one thing with that claim at the top of it. The D reader moves to dlang/; the Gleam reader arrives at gleam/. The language directory is named for the language and not for the tool: gleam refuses a hyphenated package name, so ssp-reader cannot be a gleam package and must be ssp_reader. A .gitignore for the repository, denying everything and whitelisting what belongs, covering both languages. Both build under nix, sandboxed and without network. There is no gleam builder in nixpkgs and gleam build fetches from Hex, so the Gleam build uses nix-gleam's builder, kept in gleam/nix-gleam/ (MIT), which fetches each Hex package by the checksum manifest.toml already carries, with no hash to maintain; the result is an escript wrapped with erlang. The D reader depends on nothing beyond phobos, so --skip-registry=all is the whole of what it takes: ldc by default, dmd available as ssp-reader-dmd (given gcc14, GCC 15 having introduced nullptr in headers dmd's ImportC does not yet parse). (assisted by Claude-Code) --- gleam/src/ssp_reader.gleam | 199 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 gleam/src/ssp_reader.gleam (limited to 'gleam/src/ssp_reader.gleam') diff --git a/gleam/src/ssp_reader.gleam b/gleam/src/ssp_reader.gleam new file mode 100644 index 0000000..d8c4c78 --- /dev/null +++ b/gleam/src/ssp_reader.gleam @@ -0,0 +1,199 @@ +//// ssp-reader, in Gleam. +//// +//// Reads a .ssp, the object-centric document abstraction written as text, +//// and says what it found and what it did not understand. +//// +//// Written from the specification alone: +//// ssp-format-specification.md, of the doc-abstraction-format spec +//// ocda-model.md, of the same + +import argv +import gleam/dict +import gleam/int +import gleam/io +import gleam/list +import gleam/result +import gleam/string +import simplifile +import ssp/parse +import ssp/types.{type Document, type Object} + +pub fn main() -> Nil { + case argv.load().arguments { + [] -> usage() + args -> { + let any_version = list.contains(args, "--any-version") + let outline = list.contains(args, "--outline") + let report = list.contains(args, "--report-unknown") + case list.filter(args, fn(a) { !string.starts_with(a, "--") }) { + [path, ..] -> run(path, any_version, outline, report) + [] -> usage() + } + } + } +} + +fn usage() -> Nil { + io.println("ssp-reader - reads a .ssp and says what is in it") + io.println("") + io.println(" ssp_reader [options] ") + io.println("") + io.println(" --outline the headings, as a tree") + io.println(" --report-unknown what was read and not understood") + io.println(" --any-version read a format major this reader does not know") +} + +fn run(path: String, any_version: Bool, outline: Bool, report: Bool) -> Nil { + case simplifile.read(from: path) { + Error(_) -> io.println("error: cannot read " <> path) + Ok(content) -> { + let document = parse.parse(content) + case version_ok(document, any_version) { + False -> Nil + True -> { + summary(document, path) + case outline { + True -> print_outline(document) + False -> Nil + } + case report { + True -> print_unknown(document) + False -> Nil + } + } + } + } + } +} + +/// Specification section 2.1: check the major part and refuse what is not +/// known, rather than loading an unfamiliar artefact half populated. +fn version_ok(document: Document, any_version: Bool) -> Bool { + let major = types.version_major(document) + case document.version, major == types.format_major_read, any_version { + "", _, _ -> { + io.println("warning: no version banner, reading anyway") + True + } + _, True, _ -> True + _, False, True -> { + io.println( + "warning: format v" + <> document.version + <> ", this reader knows v" + <> types.format_major_read + <> ".x; reading anyway", + ) + True + } + _, False, False -> { + io.println( + "error: format v" + <> document.version + <> ", this reader knows v" + <> types.format_major_read + <> ".x", + ) + io.println( + " a major version differs: a property may have changed meaning", + ) + io.println(" --any-version reads it regardless.") + False + } + } +} + +fn summary(document: Document, path: String) -> Nil { + io.println("Parsed: " <> path) + io.println(" Format: v" <> document.version) + io.println(" Source: " <> document.source_filename) + case dict.get(document.source, "language") { + Ok(language) -> io.println(" Language: " <> language) + Error(_) -> Nil + } + io.println(" Title: " <> meta(document, "title.main")) + io.println(" Author: " <> meta(document, "creator.author")) + let counts = + document.sections + |> list.map(fn(s) { s.name <> "=" <> int.to_string(list.length(s.objects)) }) + |> string.join(" ") + io.println(" Sections: " <> counts) +} + +fn meta(document: Document, key: String) -> String { + document.meta |> dict.get(key) |> result.unwrap("") +} + +/// The headings, indented by level. Model section 4: the eight levels are +/// A to D for the title matter and 1 to 4 for the body. +fn print_outline(document: Document) -> Nil { + io.println("") + document.sections + |> list.each(fn(section) { + section.objects + |> list.filter(fn(o) { o.kind == "heading" && types.renders(o) }) + |> list.each(fn(o) { io.println(heading_line(o)) }) + }) +} + +fn heading_line(object: Object) -> String { + let depth = case object.level { + "A" -> 0 + "B" -> 1 + "C" -> 2 + "D" -> 3 + "1" -> 4 + "2" -> 5 + "3" -> 6 + "4" -> 7 + _ -> 8 + } + string.repeat(" ", depth) + <> object.level + <> "~ [" + <> int.to_string(object.ocn) + <> "] " + <> first_line(object.text) +} + +fn first_line(text: List(String)) -> String { + case text { + [line, ..] -> line + [] -> "" + } +} + +/// A reader that drops what it does not know is conforming; one that +/// cannot say what it dropped cannot show a specification to be complete. +fn print_unknown(document: Document) -> Nil { + io.println("") + case dict.size(document.unknown_properties) { + 0 -> io.println("read and understood: every property was used") + n -> { + io.println("read and not used (" <> int.to_string(n) <> " distinct):") + document.unknown_properties + |> dict.to_list + |> list.sort(fn(a, b) { string.compare(a.0, b.0) }) + |> list.each(fn(pair) { + io.println( + " ." <> pair.0 <> " " <> int.to_string(pair.1), + ) + }) + } + } + case dict.size(document.unknown_blocks) { + 0 -> Nil + _ -> { + io.println("blocks not understood:") + document.unknown_blocks + |> dict.to_list + |> list.each(fn(pair) { + io.println(" @" <> pair.0 <> " " <> int.to_string(pair.1)) + }) + } + } + case document.malformed_lines { + 0 -> Nil + n -> io.println("lines of no known shape: " <> int.to_string(n)) + } +} -- cgit v1.2.3