diff options
| author | Ralph Amissah <ralph.amissah@gmail.com> | 2026-09-24 16:33:53 -0400 |
|---|---|---|
| committer | Ralph Amissah <ralph.amissah@gmail.com> | 2026-09-24 16:33:53 -0400 |
| commit | bbe505c8d48e822c012c9820dfe93b83dc61bcab (patch) | |
| tree | 3385f5420959ff20af295c3125a080297555a72b /gleam/src/ssp/parse.gleam | |
| parent | dealing with six properties including .is_of_type (diff) | |
--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)
Diffstat (limited to 'gleam/src/ssp/parse.gleam')
| -rw-r--r-- | gleam/src/ssp/parse.gleam | 284 |
1 files changed, 284 insertions, 0 deletions
diff --git a/gleam/src/ssp/parse.gleam b/gleam/src/ssp/parse.gleam new file mode 100644 index 0000000..9458317 --- /dev/null +++ b/gleam/src/ssp/parse.gleam @@ -0,0 +1,284 @@ +//// Parsing a .ssp. +//// +//// Specification section 3: the format is line oriented, and every line +//// is one of eight shapes told apart by its first character. So the +//// parser is a fold over the lines carrying the state the shapes need. + +import gleam/dict.{type Dict} +import gleam/int +import gleam/list +import gleam/option +import gleam/string +import ssp/types.{type Document, type Object, type Section, Object, Section} + +/// specification section 6.1: the sixteen kinds +pub const kinds = [ + "para", "heading", "toc", "bookindex", "endnote", "code", "bibliography", + "comment", "verse", "glossary", "quote", "poem", "block", "table", "blurb", + "group", +] + +/// specification section 4: the four preamble blocks +pub const preamble_blocks = ["source", "meta", "make", "doc_has"] + +/// specification section 4: the nine sections, in the order they appear +pub const section_names = [ + "head", "toc", "body", "endnotes", "glossary", "bibliography", "bookindex", + "blurb", "tail", +] + +/// specification section 6.3: the 42 properties, in the order emitted +pub const properties = [ + "identifier", "part", "section", "parent", "last_descendant", "children", + "ancestors", "ancestors_collapsed", "dom_status", "dom_status_collapsed", + "heading_lev_collapsed", "parent_lev", "dummy", "ocn_off", "is_of_type", + "attrib", "meta_lang", "meta_syntax", "sha256", "image", "indent", "bullet", + "lang", "has", "table_cols", "table_widths", "table_aligns", "table_header", + "code_linenumbers", "stow_link", "segment", "anchor", "segment_prev", + "segment_next", "heading_lev_anchor", "segment_epub", "segment_html_is", + "segment_epub_is", "segment_lv4_is", "heading_ancestors_text", "lev4_subtoc", + "anchor_tag", +] + +/// The properties this reader makes use of. The rest are counted and +/// ignored, which is conforming behaviour within a major version +/// (specification section 8.1) so long as it is not done in silence. +const properties_used = [ + "part", "parent", "ancestors", "is_of_type", "identifier", +] + +type State { + State( + document: Document, + /// the block currently open, "" when none + block: String, + /// the section currently open, "" when in the preamble + section: String, + /// objects of the current section, newest first + objects: List(Object), + /// the object being read, if any + current: Result(Object, Nil), + /// sections completed, newest first + done: List(Section), + ) +} + +pub fn parse(content: String) -> Document { + let state = + content + |> string.split(on: "\n") + |> list.fold(from: initial_state(), with: line) + finish(state) +} + +fn initial_state() -> State { + State( + document: types.new_document(), + block: "", + section: "", + objects: [], + current: Error(Nil), + done: [], + ) +} + +fn line(state: State, raw: String) -> State { + case raw { + "" -> commit_object(state) + "% SiSU Document Abstraction v" <> version -> + State( + ..state, + document: types.Document( + ..state.document, + version: string.trim(version), + ), + ) + "% Source: " <> filename -> + State( + ..state, + document: types.Document( + ..state.document, + source_filename: string.trim(filename), + ), + ) + "%" <> _ -> state + "@" <> rest -> open_block(state, rest) + "}" -> close_block(state) + " " <> rest -> preamble_pair(state, rest) + "[" <> rest -> declaration(state, rest) + "." <> rest -> property(state, rest) + "| " <> rest -> text_line(state, rest) + _ -> + State( + ..state, + document: types.Document( + ..state.document, + malformed_lines: state.document.malformed_lines + 1, + ), + ) + } +} + +fn open_block(state: State, rest: String) -> State { + let name = case string.split_once(rest, " {") { + Ok(#(name, _)) -> name + Error(_) -> rest + } + case list.contains(preamble_blocks, name), list.contains(section_names, name) { + True, _ -> State(..state, block: name) + _, True -> State(..state, block: name, section: name, objects: []) + _, _ -> + State( + ..state, + block: name, + document: types.Document( + ..state.document, + unknown_blocks: bump(state.document.unknown_blocks, name), + ), + ) + } +} + +fn close_block(state: State) -> State { + let state = commit_object(state) + case state.section { + "" -> State(..state, block: "") + name -> + State( + ..state, + block: "", + section: "", + objects: [], + done: [Section(name: name, objects: list.reverse(state.objects)), ..state.done], + ) + } +} + +fn preamble_pair(state: State, rest: String) -> State { + case string.split_once(rest, ": ") { + Error(_) -> state + Ok(#(key, value)) -> { + let document = state.document + let document = case state.block { + "source" -> + types.Document(..document, source: dict.insert(document.source, key, value)) + "meta" -> + types.Document(..document, meta: dict.insert(document.meta, key, value)) + "make" -> + types.Document(..document, make: dict.insert(document.make, key, value)) + "doc_has" -> + types.Document( + ..document, + doc_has: dict.insert(document.doc_has, key, value), + ) + _ -> document + } + State(..state, document: document) + } + } +} + +/// specification section 6.1 +/// [<ocn>] <kind> +/// [<ocn>] heading :<level> +/// [<ocn>] heading :<level> <identifier> +fn declaration(state: State, rest: String) -> State { + let state = commit_object(state) + case string.split_once(rest, "] ") { + Error(_) -> state + Ok(#(ocn_text, tail)) -> { + let ocn = case int.parse(ocn_text) { + Ok(n) -> n + Error(_) -> 0 + } + let object = case string.split(tail, on: " ") { + ["heading", level, ..identifier] -> + Object( + ..types.new_object(), + ocn: ocn, + kind: "heading", + level: string.drop_start(level, 1), + identifier: string.join(identifier, " "), + ) + [kind, ..] -> Object(..types.new_object(), ocn: ocn, kind: kind) + [] -> types.new_object() + } + State(..state, current: Ok(object)) + } + } +} + +fn property(state: State, rest: String) -> State { + case state.current, string.split_once(rest, ": ") { + Ok(object), Ok(#(name, value)) -> { + let object = + Object(..object, properties: [#(name, value), ..object.properties]) + let object = case name { + "part" -> Object(..object, part: value) + "is_of_type" -> Object(..object, is_of_type: value) + "identifier" -> Object(..object, identifier: value) + "parent" -> + Object(..object, parent: case int.parse(value) { + Ok(n) -> n + Error(_) -> 0 + }) + "ancestors" -> Object(..object, ancestors: numbers(value)) + _ -> object + } + // a property this reader does not use, whether or not the + // specification names it, is counted rather than passed over: within + // a major version ignoring it is conforming, doing so in silence is + // what stops a reader being a check on the specification + let document = case list.contains(properties_used, name) { + True -> state.document + False -> + types.Document( + ..state.document, + unknown_properties: bump(state.document.unknown_properties, name), + ) + } + State(..state, current: Ok(object), document: document) + } + _, _ -> state + } +} + +fn text_line(state: State, rest: String) -> State { + case state.current { + Ok(object) -> State(..state, current: Ok(Object(..object, text: [rest, ..object.text]))) + Error(_) -> state + } +} + +fn commit_object(state: State) -> State { + case state.current { + Error(_) -> state + Ok(object) -> + State( + ..state, + current: Error(Nil), + objects: [Object(..object, text: list.reverse(object.text), + properties: list.reverse(object.properties)), ..state.objects], + ) + } +} + +fn finish(state: State) -> Document { + let state = close_block(commit_object(state)) + types.Document(..state.document, sections: list.reverse(state.done)) +} + +fn bump(counts: Dict(String, Int), key: String) -> Dict(String, Int) { + dict.upsert(counts, key, fn(existing) { + case existing { + option.Some(n) -> n + 1 + option.None -> 1 + } + }) +} + +fn numbers(value: String) -> List(Int) { + value + |> string.split(on: " ") + |> list.filter_map(int.parse) +} |
