(* Doc: what a module offers, from its source. Doc Module.raz prints the module's own comment, the one just before module, then each exported declaration as it is written, with the comment just before it. Doc man Module.raz ... prints the same of each module, in mdoc, for a library's manual page: the module's name a heading, each declaration a literal display, each comment filled text, indented, but for its lines indented further, which are code; a line of the text that starts with "- " begins a list's bullet (see comment). It writes the modules only; the page's head (its date, name and description, up to .Sh MODULES) and tail are files of the library's own, put around it by its Makefile. A comment on lines of its own just before const, type or var heads that group: it is written once, before the group's declarations, as a module's own comment is, and each declaration keeps the comment just before it. Doc examples report.md dir Doc examples page.7 dir writes each example of the report, or of a manual page, that is a whole module, from module to end, with no ... left out, to dir/Name.raz, and prints the names, one a line. *) module Doc; import Out, Rt, Str, Buf, Heap, Scan, Files; var sc: Scan.Scanner; (* the text being read, at the current symbol *) src: []const U8; docStart, docEnd: INT; (* the comment just before it, or -1 *) man: BOOL; (* writing mdoc *) atStart: BOOL; (* emit is at the start of a line *) shown: BOOL; (* a literal display of mdoc's is open *) listed: BOOL; (* a bullet list of mdoc's is open *) groupStart, groupEnd: INT; (* the comment that heads the group being read, not yet written, or -1 *) (* On to the next symbol that isn't a comment, keeping where the last comment that starts a line was: one after code on its line belongs to that code. *) proc next; var newLine: BOOL; begin docStart := -1; newLine := false; repeat Scan.next(sc); newLine := newLine or sc.lineBreak; if (sc.kind = Scan.comment) and newLine then docStart := sc.start; docEnd := sc.stop; end; until sc.kind # Scan.comment; end; (* c, written out; in mdoc, so that roff takes it as text: a backslash is \e, and a line that would start with . or ' is a control line unless \& comes first. *) proc emit(c: U8); begin if man and (c = '\\') then Out.str("\\e"); else if man and atStart and ((c = '.') or (c = '\'')) then Out.str("\\&"); end; Out.char(c); end; atStart := c = 10; end; (* a line of mdoc's own, such as .Ed *) proc control(s: []const U8); begin Out.str(s); Out.ln; atStart := true; end; (* the bullet list, if open, closed *) proc closeList; begin if listed then control(".El"); end; listed := false; end; (* the literal display, if open, closed *) proc close; begin if shown then control(".Ed"); end; shown := false; end; proc text: []const U8; begin return Scan.text(sc); end; proc is(s: []const U8): BOOL; begin return text() = s; end; (* Whether the current word starts a declaration, a section or the end. *) proc reserved: BOOL; begin return is("const") or is("type") or is("var") or is("proc") or is("extern") or is("import") or is("link") or is("begin") or is("end"); end; (* On to the ; that ends what starts here, past anything bracketed and any record or variant; returns where it ends. *) proc toSemicolon: INT; var depth: INT; begin depth := 0; while sc.kind # Scan.eof do if is("(") or is("[") or is("{") or is("record") or is("variant") then INC(depth); elsif is(")") or is("]") or is("}") or is("end") then DEC(depth); elsif is(";") and (depth <= 0) then return sc.stop; end; next; end; return sc.pos; end; (* stop, or the end of a comment that follows it on its line. *) proc trailing(stop: INT): INT; var t: Scan.Scanner; begin while (stop < LEN(src)) and ((src[stop] = ' ') or (src[stop] = 9)) do INC(stop); end; t := sc; t.pos := stop; Scan.next(t); if (t.kind = Scan.comment) and (t.start = stop) then return t.stop; end; return stop; end; (* Where the line holding p starts. *) proc lineStart(p: INT): INT; begin while (p > 0) and (src[p - 1] # 10) do DEC(p); end; return p; end; (* src[start..stop] as written, each line after the first moved left as far as the first one's column. *) proc block(start, stop: INT); var col, i, n: INT; begin col := start - lineStart(start); i := start; while i < stop do emit(src[i]); if src[i] = 10 then n := 0; while (n < col) and (i + 1 < stop) and (src[i + 1] = ' ') do INC(i); INC(n); end; end; INC(i); end; emit(10); end; (* Whether the space at src[p] ends a sentence: a full stop, ? or ! comes before it, maybe behind closing brackets or quotes. mdoc wants each sentence on a line of its own, so that roff knows where one ends and spaces it so. *) proc sentenceEnds(start, p: INT): BOOL; begin DEC(p); while (p > start) and ((src[p] = ')') or (src[p] = ']') or (src[p] = '"') or (src[p] = '\'')) do DEC(p); end; return (p >= start) and ((src[p] = '.') or (src[p] = '?') or (src[p] = '!')); end; (* The comment from start to stop, without its brackets, each line indented by four; in mdoc, filled, each sentence starting a line, but for the lines indented past its own, which are code, kept as they are in a literal display. mdoc's displays don't nest, so the filled one ends where the code starts and starts again after. A line that starts with "- " at the text's own margin starts a bullet of a list; the lines at the margin after it continue it, and a blank line or code ends it. *) proc comment(start, stop: INT); var i, j, k, m, n, deep, indent: INT; begin if start < 0 then return; end; deep := start - lineStart(start) + 3; indent := -1; (* the indent of the code being written, or -1 *) i := start + 2; j := stop - 2; while (i < j) and Scan.isSpace(src[i]) do INC(i); end; while (j > i) and Scan.isSpace(src[j - 1]) do DEC(j); end; if man then control(".Bd -filled -offset indent -compact"); end; while i < j do k := i; while (k < j) and (src[k] # 10) do INC(k); end; n := 0; while (i + n < k) and (src[i + n] = ' ') do INC(n); end; if man and (n > deep) and (indent < 0) then closeList; control(".Ed"); control(".Bd -literal -offset indent -compact"); indent := n; elsif man and (n <= deep) and (indent >= 0) then control(".Ed"); control(".Bd -filled -offset indent -compact"); indent := -1; end; if man and (n = k - i) and (indent < 0) then closeList; control(".Pp"); elsif man and (indent < 0) then m := i + n; if (m + 1 < k) and (src[m] = '-') and (src[m + 1] = ' ') then if not listed then control(".Bl -bullet -compact"); listed := true; end; control(".It"); INC(m, 2); while (m < k) and (src[m] = ' ') do INC(m); end; end; while m < k do if (src[m] = ' ') and sentenceEnds(i + n, m) then emit(10); while (m + 1 < k) and (src[m + 1] = ' ') do INC(m); end; else emit(src[m]); end; INC(m); end; emit(10); else if not man then Out.str(" "); elsif n > indent then n := indent; end; for m := i + n to k - 1 do emit(src[m]); end; emit(10); end; i := k + 1; end; if man then closeList; control(".Ed"); end; end; (* The comment that heads a group of constants, types or variables, written once, before the group's first exported declaration. *) proc heading; begin if not man then Out.ln; else close; end; comment(groupStart, groupEnd); groupStart := -1; end; (* One declaration, from its first token, the comment before it kept. In mdoc a module's test, its main, is left out, and declarations with no comment share a display. *) proc declaration(prefix: []const U8; exported: BOOL; start, ds, de, stop: INT); begin if not exported then return; end; if (groupStart >= 0) and (LEN(prefix) > 0) then heading; end; if not man then Out.ln; elsif Str.hasPrefix(src[start..stop], "proc main*") then return; elsif not shown then control(".Bd -literal"); shown := true; end; if LEN(prefix) > 0 then Out.str(prefix); Out.str(" "); end; block(start, trailing(stop)); if ds >= 0 then close; end; comment(ds, de); end; (* name, and whether a * follows it; leaves the token after them. *) proc name: BOOL; begin next; next; if is("*") then next; return true; end; return false; end; (* A procedure's body: to the end that closes its begin or code, the procedure name after it and its ;. *) proc skipBody; var depth: INT; sawBegin: BOOL; begin depth := 0; sawBegin := false; while sc.kind # Scan.eof do if is("begin") or is("code") then if depth = 0 then sawBegin := true; end; INC(depth); elsif is("if") or is("while") or is("for") or is("match") or is("record") or is("variant") then INC(depth); elsif is("end") then DEC(depth); if (depth = 0) and sawBegin then next; if sc.kind = Scan.word then next; end; return; end; end; next; end; end; proc procedure; var start, ds, de, stop: INT; exp: BOOL; begin start := sc.start; ds := docStart; de := docEnd; exp := name(); stop := toSemicolon(); next; if is("forward") then _ := toSemicolon(); else skipBody; end; declaration("", exp, start, ds, de, stop); next; end; proc section; var word: []const U8; start, ds, de, stop: INT; exp: BOOL; begin word := text(); groupStart := docStart; groupEnd := docEnd; next; while (sc.kind = Scan.word) and not reserved() do start := sc.start; ds := docStart; de := docEnd; sc.pos := sc.start; (* name reads the name again *) exp := name(); stop := toSemicolon(); declaration(word, exp, start, ds, de, stop); next; end; groupStart := -1; end; proc external; var start, ds, de, stop: INT; exp: BOOL; begin start := sc.start; ds := docStart; de := docEnd; next; exp := name(); stop := toSemicolon(); declaration("", exp, start, ds, de, stop); next; end; proc document(path: []const U8); var b: Buf.Buf; start, ds, de: INT; begin groupStart := -1; Buf.init(b, Heap.heap); if not Files.read(path, b) then Rt.stop("Doc: can't read the file"); end; src := Buf.bytes(b); Scan.init(sc, src); next; while (sc.kind # Scan.eof) and not is("module") do next; end; if sc.kind = Scan.eof then Rt.stop("Doc: no module"); end; start := sc.start; ds := docStart; de := docEnd; next; if man then close; Out.str(".Ss "); Out.str(text()); emit(10); declaration("", true, start, ds, de, toSemicolon()); else block(start, toSemicolon()); comment(ds, de); end; next; while (sc.kind # Scan.eof) and not is("begin") and not is("end") do if is("const") or is("type") or is("var") then section; elsif is("proc") then procedure; elsif is("extern") then external; elsif is("import") or is("link") then _ := toSemicolon(); next; else next; end; end; end; (* line, from the report or a manual page's literal block, added to b as the program has it: in a page, \e is a backslash, \(ga a backquote and \& nothing. *) proc addLine(var b: Buf.Buf; line: []const U8; page: BOOL); var i: INT; begin i := 0; while i < LEN(line) do if page and (line[i] = '\\') and (i + 1 < LEN(line)) and (line[i + 1] = 'e') then Buf.add(b, '\\'); INC(i); elsif page and Str.hasPrefix(line[i..LEN(line)], "\\(ga") then Buf.add(b, '`'); INC(i, 3); elsif page and (line[i] = '\\') and (i + 1 < LEN(line)) and (line[i + 1] = '&') then INC(i); else Buf.add(b, line[i]); end; INC(i); end; Buf.add(b, 10); end; (* text written to the file dir/name and ext, its path made in out *) proc write(var out: Buf.Buf; dir, name, ext, text: []const U8); begin Buf.reset(out); Buf.str(out, dir); Buf.str(out, "/"); Buf.str(out, name); Buf.str(out, ext); if not Files.write(Buf.bytes(out), text) then Rt.stop("Doc: can't write an example"); end; end; (* The examples that are whole modules, each to dir: those of the report, between lines of ```, or of a manual page, between .Bd -literal and .Ed. In a page, a block that starts on the line after a module's is what that program prints, written to dir/Name.shown, so that the page can be checked against the program. *) proc examples(path, dir: []const U8); var b, out, prog: Buf.Buf; s, line, first, last, name, shows: []const U8; i, k, n, after: INT; inBlock, whole, page, fence: BOOL; begin Buf.init(b, Heap.heap); Buf.init(out, Heap.heap); Buf.init(prog, Heap.heap); if not Files.read(path, b) then Rt.stop("Doc: can't read the examples' file"); end; s := Buf.bytes(b); page := (LEN(path) < 3) or (path[LEN(path) - 3..LEN(path)] # ".md"); inBlock := false; after := -1; i := 0; while i < LEN(s) do k := i; while (k < LEN(s)) and (s[k] # 10) do INC(k); end; line := s[i..k]; fence := not page and (Str.trim(line) = "```"); if inBlock and (fence or page and (line = ".Ed")) then if whole and Str.hasPrefix(first, "module ") and Str.hasPrefix(last, "end ") then name := first[7..LEN(first)]; n := 0; while (n < LEN(name)) and Scan.isWord(name[n]) do INC(n); end; name := name[0..n]; write(out, dir, name, ".raz", Buf.bytes(prog)); Out.str(name); Out.ln; after := k + 1; elsif LEN(shows) > 0 then write(out, dir, shows, ".shown", Buf.bytes(prog)); end; inBlock := false; elsif not inBlock and (fence or page and Str.hasPrefix(line, ".Bd -literal")) then inBlock := true; shows := ""; if page and (i = after) then shows := name; end; Buf.reset(prog); first := ""; last := ""; whole := true; elsif inBlock then addLine(prog, line, page); if Str.trim(line) = "..." then whole := false; end; if LEN(Str.trim(line)) > 0 then if LEN(first) = 0 then first := Str.trim(line); end; last := Str.trim(line); end; end; i := k + 1; end; end; (* The modules named from argument 2 on, as mdoc: what goes between a manual page's .Sh MODULES and its next heading. The page's head and tail, and its date, belong to the page, not to this tool: raz's Makefile puts doc/raz-lib.head and doc/raz-lib.tail around it. *) proc page; var i: INT; begin man := true; atStart := true; groupStart := -1; for i := 2 to ARGC() - 1 do document(Files.arg(i)); end; close; end; proc main*; begin if (ARGC() = 4) and (Files.arg(1) = "examples") then examples(Files.arg(2), Files.arg(3)); elsif (ARGC() >= 3) and (Files.arg(1) = "man") then page; elsif ARGC() = 2 then document(Files.arg(1)); else Rt.stop("usage: Doc Module.raz, Doc man Module.raz ..., or Doc examples file dir"); end; end; end Doc.