| 1 | (* Doc: what a module offers, from its source. |
| 2 | |
| 3 | Doc Module.raz |
| 4 | prints the module's own comment, the one just before module, then |
| 5 | each exported declaration as it is written, with the comment just |
| 6 | before it. |
| 7 | |
| 8 | Doc man Module.raz ... |
| 9 | prints the same of each module, in mdoc, for a library's manual page: |
| 10 | the module's name a heading, each declaration a literal display, |
| 11 | each comment filled text, indented, but for its lines indented |
| 12 | further, which are code; a line of the text that starts with "- " |
| 13 | begins a list's bullet (see comment). It writes the modules only; the page's |
| 14 | head (its date, name and description, up to .Sh MODULES) and tail |
| 15 | are files of the library's own, put around it by its Makefile. |
| 16 | |
| 17 | A comment on lines of its own just before const, type or var heads |
| 18 | that group: it is written once, before the group's declarations, |
| 19 | as a module's own comment is, and each declaration keeps the comment |
| 20 | just before it. |
| 21 | |
| 22 | Doc examples report.md dir |
| 23 | Doc examples page.7 dir |
| 24 | writes each example of the report, or of a manual page, that is a |
| 25 | whole module, from module to end, with no ... left out, to |
| 26 | dir/Name.raz, and prints the names, one a line. *) |
| 27 | module Doc; |
| 28 | import Out, Rt, Str, Buf, Heap, Scan, Files; |
| 29 | |
| 30 | var |
| 31 | sc: Scan.Scanner; (* the text being read, at the current symbol *) |
| 32 | src: []const U8; |
| 33 | docStart, docEnd: INT; (* the comment just before it, or -1 *) |
| 34 | man: BOOL; (* writing mdoc *) |
| 35 | atStart: BOOL; (* emit is at the start of a line *) |
| 36 | shown: BOOL; (* a literal display of mdoc's is open *) |
| 37 | listed: BOOL; (* a bullet list of mdoc's is open *) |
| 38 | groupStart, groupEnd: INT; (* the comment that heads the group being read, not yet written, or -1 *) |
| 39 | |
| 40 | (* On to the next symbol that isn't a comment, keeping where the last |
| 41 | comment that starts a line was: one after code on its line belongs |
| 42 | to that code. *) |
| 43 | proc next; |
| 44 | var newLine: BOOL; |
| 45 | begin |
| 46 | docStart := -1; |
| 47 | newLine := false; |
| 48 | repeat |
| 49 | Scan.next(sc); |
| 50 | newLine := newLine or sc.lineBreak; |
| 51 | if (sc.kind = Scan.comment) and newLine then |
| 52 | docStart := sc.start; docEnd := sc.stop; |
| 53 | end; |
| 54 | until sc.kind # Scan.comment; |
| 55 | end; |
| 56 | |
| 57 | (* c, written out; in mdoc, so that roff takes it as text: a backslash |
| 58 | is \e, and a line that would start with . or ' is a control line |
| 59 | unless \& comes first. *) |
| 60 | proc emit(c: U8); |
| 61 | begin |
| 62 | if man and (c = '\\') then Out.str("\\e"); |
| 63 | else |
| 64 | if man and atStart and ((c = '.') or (c = '\'')) then Out.str("\\&"); end; |
| 65 | Out.char(c); |
| 66 | end; |
| 67 | atStart := c = 10; |
| 68 | end; |
| 69 | |
| 70 | (* a line of mdoc's own, such as .Ed *) |
| 71 | proc control(s: []const U8); |
| 72 | begin |
| 73 | Out.str(s); |
| 74 | Out.ln; |
| 75 | atStart := true; |
| 76 | end; |
| 77 | |
| 78 | (* the bullet list, if open, closed *) |
| 79 | proc closeList; |
| 80 | begin |
| 81 | if listed then control(".El"); end; |
| 82 | listed := false; |
| 83 | end; |
| 84 | |
| 85 | (* the literal display, if open, closed *) |
| 86 | proc close; |
| 87 | begin |
| 88 | if shown then control(".Ed"); end; |
| 89 | shown := false; |
| 90 | end; |
| 91 | |
| 92 | proc text: []const U8; |
| 93 | begin |
| 94 | return Scan.text(sc); |
| 95 | end; |
| 96 | |
| 97 | proc is(s: []const U8): BOOL; |
| 98 | begin |
| 99 | return text() = s; |
| 100 | end; |
| 101 | |
| 102 | (* Whether the current word starts a declaration, a section or the end. *) |
| 103 | proc reserved: BOOL; |
| 104 | begin |
| 105 | return is("const") or is("type") or is("var") or is("proc") or is("extern") |
| 106 | or is("import") or is("link") or is("begin") or is("end"); |
| 107 | end; |
| 108 | |
| 109 | (* On to the ; that ends what starts here, past anything bracketed and |
| 110 | any record or variant; returns where it ends. *) |
| 111 | proc toSemicolon: INT; |
| 112 | var depth: INT; |
| 113 | begin |
| 114 | depth := 0; |
| 115 | while sc.kind # Scan.eof do |
| 116 | if is("(") or is("[") or is("{") or is("record") or is("variant") then INC(depth); |
| 117 | elsif is(")") or is("]") or is("}") or is("end") then DEC(depth); |
| 118 | elsif is(";") and (depth <= 0) then return sc.stop; |
| 119 | end; |
| 120 | next; |
| 121 | end; |
| 122 | return sc.pos; |
| 123 | end; |
| 124 | |
| 125 | (* stop, or the end of a comment that follows it on its line. *) |
| 126 | proc trailing(stop: INT): INT; |
| 127 | var t: Scan.Scanner; |
| 128 | begin |
| 129 | while (stop < LEN(src)) and ((src[stop] = ' ') or (src[stop] = 9)) do INC(stop); end; |
| 130 | t := sc; |
| 131 | t.pos := stop; |
| 132 | Scan.next(t); |
| 133 | if (t.kind = Scan.comment) and (t.start = stop) then return t.stop; end; |
| 134 | return stop; |
| 135 | end; |
| 136 | |
| 137 | (* Where the line holding p starts. *) |
| 138 | proc lineStart(p: INT): INT; |
| 139 | begin |
| 140 | while (p > 0) and (src[p - 1] # 10) do DEC(p); end; |
| 141 | return p; |
| 142 | end; |
| 143 | |
| 144 | (* src[start..stop] as written, each line after the first moved left as |
| 145 | far as the first one's column. *) |
| 146 | proc block(start, stop: INT); |
| 147 | var col, i, n: INT; |
| 148 | begin |
| 149 | col := start - lineStart(start); |
| 150 | i := start; |
| 151 | while i < stop do |
| 152 | emit(src[i]); |
| 153 | if src[i] = 10 then |
| 154 | n := 0; |
| 155 | while (n < col) and (i + 1 < stop) and (src[i + 1] = ' ') do INC(i); INC(n); end; |
| 156 | end; |
| 157 | INC(i); |
| 158 | end; |
| 159 | emit(10); |
| 160 | end; |
| 161 | |
| 162 | (* Whether the space at src[p] ends a sentence: a full stop, ? or ! |
| 163 | comes before it, maybe behind closing brackets or quotes. mdoc wants |
| 164 | each sentence on a line of its own, so that roff knows where one |
| 165 | ends and spaces it so. *) |
| 166 | proc sentenceEnds(start, p: INT): BOOL; |
| 167 | begin |
| 168 | DEC(p); |
| 169 | while (p > start) and ((src[p] = ')') or (src[p] = ']') or (src[p] = '"') or (src[p] = '\'')) do |
| 170 | DEC(p); |
| 171 | end; |
| 172 | return (p >= start) and ((src[p] = '.') or (src[p] = '?') or (src[p] = '!')); |
| 173 | end; |
| 174 | |
| 175 | (* The comment from start to stop, without its brackets, each line |
| 176 | indented by four; in mdoc, filled, each sentence starting a line, |
| 177 | but for the lines indented past its own, which are code, kept as |
| 178 | they are in a literal display. mdoc's displays don't nest, so the |
| 179 | filled one ends where the code starts and starts again after. A line |
| 180 | that starts with "- " at the text's own margin starts a bullet of a |
| 181 | list; the lines at the margin after it continue it, and a blank line |
| 182 | or code ends it. *) |
| 183 | proc comment(start, stop: INT); |
| 184 | var i, j, k, m, n, deep, indent: INT; |
| 185 | begin |
| 186 | if start < 0 then return; end; |
| 187 | deep := start - lineStart(start) + 3; |
| 188 | indent := -1; (* the indent of the code being written, or -1 *) |
| 189 | i := start + 2; |
| 190 | j := stop - 2; |
| 191 | while (i < j) and Scan.isSpace(src[i]) do INC(i); end; |
| 192 | while (j > i) and Scan.isSpace(src[j - 1]) do DEC(j); end; |
| 193 | if man then control(".Bd -filled -offset indent -compact"); end; |
| 194 | while i < j do |
| 195 | k := i; |
| 196 | while (k < j) and (src[k] # 10) do INC(k); end; |
| 197 | n := 0; |
| 198 | while (i + n < k) and (src[i + n] = ' ') do INC(n); end; |
| 199 | if man and (n > deep) and (indent < 0) then |
| 200 | closeList; |
| 201 | control(".Ed"); |
| 202 | control(".Bd -literal -offset indent -compact"); |
| 203 | indent := n; |
| 204 | elsif man and (n <= deep) and (indent >= 0) then |
| 205 | control(".Ed"); |
| 206 | control(".Bd -filled -offset indent -compact"); |
| 207 | indent := -1; |
| 208 | end; |
| 209 | if man and (n = k - i) and (indent < 0) then |
| 210 | closeList; |
| 211 | control(".Pp"); |
| 212 | elsif man and (indent < 0) then |
| 213 | m := i + n; |
| 214 | if (m + 1 < k) and (src[m] = '-') and (src[m + 1] = ' ') then |
| 215 | if not listed then control(".Bl -bullet -compact"); listed := true; end; |
| 216 | control(".It"); |
| 217 | INC(m, 2); |
| 218 | while (m < k) and (src[m] = ' ') do INC(m); end; |
| 219 | end; |
| 220 | while m < k do |
| 221 | if (src[m] = ' ') and sentenceEnds(i + n, m) then |
| 222 | emit(10); |
| 223 | while (m + 1 < k) and (src[m + 1] = ' ') do INC(m); end; |
| 224 | else emit(src[m]); |
| 225 | end; |
| 226 | INC(m); |
| 227 | end; |
| 228 | emit(10); |
| 229 | else |
| 230 | if not man then Out.str(" "); |
| 231 | elsif n > indent then n := indent; |
| 232 | end; |
| 233 | for m := i + n to k - 1 do emit(src[m]); end; |
| 234 | emit(10); |
| 235 | end; |
| 236 | i := k + 1; |
| 237 | end; |
| 238 | if man then closeList; control(".Ed"); end; |
| 239 | end; |
| 240 | |
| 241 | (* The comment that heads a group of constants, types or variables, |
| 242 | written once, before the group's first exported declaration. *) |
| 243 | proc heading; |
| 244 | begin |
| 245 | if not man then Out.ln; |
| 246 | else close; |
| 247 | end; |
| 248 | comment(groupStart, groupEnd); |
| 249 | groupStart := -1; |
| 250 | end; |
| 251 | |
| 252 | (* One declaration, from its first token, the comment before it kept. |
| 253 | In mdoc a module's test, its main, is left out, and declarations |
| 254 | with no comment share a display. *) |
| 255 | proc declaration(prefix: []const U8; exported: BOOL; start, ds, de, stop: INT); |
| 256 | begin |
| 257 | if not exported then return; end; |
| 258 | if (groupStart >= 0) and (LEN(prefix) > 0) then heading; end; |
| 259 | if not man then Out.ln; |
| 260 | elsif Str.hasPrefix(src[start..stop], "proc main*") then return; |
| 261 | elsif not shown then control(".Bd -literal"); shown := true; |
| 262 | end; |
| 263 | if LEN(prefix) > 0 then Out.str(prefix); Out.str(" "); end; |
| 264 | block(start, trailing(stop)); |
| 265 | if ds >= 0 then close; end; |
| 266 | comment(ds, de); |
| 267 | end; |
| 268 | |
| 269 | (* name, and whether a * follows it; leaves the token after them. *) |
| 270 | proc name: BOOL; |
| 271 | begin |
| 272 | next; |
| 273 | next; |
| 274 | if is("*") then next; return true; end; |
| 275 | return false; |
| 276 | end; |
| 277 | |
| 278 | (* A procedure's body: to the end that closes its begin or code, the |
| 279 | procedure name after it and its ;. *) |
| 280 | proc skipBody; |
| 281 | var depth: INT; sawBegin: BOOL; |
| 282 | begin |
| 283 | depth := 0; |
| 284 | sawBegin := false; |
| 285 | while sc.kind # Scan.eof do |
| 286 | if is("begin") or is("code") then |
| 287 | if depth = 0 then sawBegin := true; end; |
| 288 | INC(depth); |
| 289 | elsif is("if") or is("while") or is("for") or is("match") or is("record") or is("variant") then |
| 290 | INC(depth); |
| 291 | elsif is("end") then |
| 292 | DEC(depth); |
| 293 | if (depth = 0) and sawBegin then |
| 294 | next; |
| 295 | if sc.kind = Scan.word then next; end; |
| 296 | return; |
| 297 | end; |
| 298 | end; |
| 299 | next; |
| 300 | end; |
| 301 | end; |
| 302 | |
| 303 | proc procedure; |
| 304 | var start, ds, de, stop: INT; exp: BOOL; |
| 305 | begin |
| 306 | start := sc.start; ds := docStart; de := docEnd; |
| 307 | exp := name(); |
| 308 | stop := toSemicolon(); |
| 309 | next; |
| 310 | if is("forward") then _ := toSemicolon(); |
| 311 | else skipBody; |
| 312 | end; |
| 313 | declaration("", exp, start, ds, de, stop); |
| 314 | next; |
| 315 | end; |
| 316 | |
| 317 | proc section; |
| 318 | var word: []const U8; start, ds, de, stop: INT; exp: BOOL; |
| 319 | begin |
| 320 | word := text(); |
| 321 | groupStart := docStart; groupEnd := docEnd; |
| 322 | next; |
| 323 | while (sc.kind = Scan.word) and not reserved() do |
| 324 | start := sc.start; ds := docStart; de := docEnd; |
| 325 | sc.pos := sc.start; (* name reads the name again *) |
| 326 | exp := name(); |
| 327 | stop := toSemicolon(); |
| 328 | declaration(word, exp, start, ds, de, stop); |
| 329 | next; |
| 330 | end; |
| 331 | groupStart := -1; |
| 332 | end; |
| 333 | |
| 334 | proc external; |
| 335 | var start, ds, de, stop: INT; exp: BOOL; |
| 336 | begin |
| 337 | start := sc.start; ds := docStart; de := docEnd; |
| 338 | next; |
| 339 | exp := name(); |
| 340 | stop := toSemicolon(); |
| 341 | declaration("", exp, start, ds, de, stop); |
| 342 | next; |
| 343 | end; |
| 344 | |
| 345 | proc document(path: []const U8); |
| 346 | var b: Buf.Buf; start, ds, de: INT; |
| 347 | begin |
| 348 | groupStart := -1; |
| 349 | Buf.init(b, Heap.heap); |
| 350 | if not Files.read(path, b) then Rt.stop("Doc: can't read the file"); end; |
| 351 | src := Buf.bytes(b); |
| 352 | Scan.init(sc, src); |
| 353 | next; |
| 354 | while (sc.kind # Scan.eof) and not is("module") do next; end; |
| 355 | if sc.kind = Scan.eof then Rt.stop("Doc: no module"); end; |
| 356 | start := sc.start; |
| 357 | ds := docStart; de := docEnd; |
| 358 | next; |
| 359 | if man then |
| 360 | close; |
| 361 | Out.str(".Ss "); Out.str(text()); emit(10); |
| 362 | declaration("", true, start, ds, de, toSemicolon()); |
| 363 | else |
| 364 | block(start, toSemicolon()); |
| 365 | comment(ds, de); |
| 366 | end; |
| 367 | next; |
| 368 | while (sc.kind # Scan.eof) and not is("begin") and not is("end") do |
| 369 | if is("const") or is("type") or is("var") then section; |
| 370 | elsif is("proc") then procedure; |
| 371 | elsif is("extern") then external; |
| 372 | elsif is("import") or is("link") then _ := toSemicolon(); next; |
| 373 | else next; |
| 374 | end; |
| 375 | end; |
| 376 | end; |
| 377 | |
| 378 | (* line, from the report or a manual page's literal block, added to b |
| 379 | as the program has it: in a page, \e is a backslash, \(ga a |
| 380 | backquote and \& nothing. *) |
| 381 | proc addLine(var b: Buf.Buf; line: []const U8; page: BOOL); |
| 382 | var i: INT; |
| 383 | begin |
| 384 | i := 0; |
| 385 | while i < LEN(line) do |
| 386 | if page and (line[i] = '\\') and (i + 1 < LEN(line)) and (line[i + 1] = 'e') then |
| 387 | Buf.add(b, '\\'); |
| 388 | INC(i); |
| 389 | elsif page and Str.hasPrefix(line[i..LEN(line)], "\\(ga") then |
| 390 | Buf.add(b, '`'); |
| 391 | INC(i, 3); |
| 392 | elsif page and (line[i] = '\\') and (i + 1 < LEN(line)) and (line[i + 1] = '&') then |
| 393 | INC(i); |
| 394 | else Buf.add(b, line[i]); |
| 395 | end; |
| 396 | INC(i); |
| 397 | end; |
| 398 | Buf.add(b, 10); |
| 399 | end; |
| 400 | |
| 401 | (* text written to the file dir/name and ext, its path made in out *) |
| 402 | proc write(var out: Buf.Buf; dir, name, ext, text: []const U8); |
| 403 | begin |
| 404 | Buf.reset(out); |
| 405 | Buf.str(out, dir); Buf.str(out, "/"); Buf.str(out, name); Buf.str(out, ext); |
| 406 | if not Files.write(Buf.bytes(out), text) then Rt.stop("Doc: can't write an example"); end; |
| 407 | end; |
| 408 | |
| 409 | (* The examples that are whole modules, each to dir: those of the |
| 410 | report, between lines of ```, or of a manual page, between .Bd |
| 411 | -literal and .Ed. In a page, a block that starts on the line after |
| 412 | a module's is what that program prints, written to dir/Name.shown, |
| 413 | so that the page can be checked against the program. *) |
| 414 | proc examples(path, dir: []const U8); |
| 415 | var |
| 416 | b, out, prog: Buf.Buf; s, line, first, last, name, shows: []const U8; |
| 417 | i, k, n, after: INT; inBlock, whole, page, fence: BOOL; |
| 418 | begin |
| 419 | Buf.init(b, Heap.heap); |
| 420 | Buf.init(out, Heap.heap); |
| 421 | Buf.init(prog, Heap.heap); |
| 422 | if not Files.read(path, b) then Rt.stop("Doc: can't read the examples' file"); end; |
| 423 | s := Buf.bytes(b); |
| 424 | page := (LEN(path) < 3) or (path[LEN(path) - 3..LEN(path)] # ".md"); |
| 425 | inBlock := false; |
| 426 | after := -1; |
| 427 | i := 0; |
| 428 | while i < LEN(s) do |
| 429 | k := i; |
| 430 | while (k < LEN(s)) and (s[k] # 10) do INC(k); end; |
| 431 | line := s[i..k]; |
| 432 | fence := not page and (Str.trim(line) = "```"); |
| 433 | if inBlock and (fence or page and (line = ".Ed")) then |
| 434 | if whole and Str.hasPrefix(first, "module ") and Str.hasPrefix(last, "end ") then |
| 435 | name := first[7..LEN(first)]; |
| 436 | n := 0; |
| 437 | while (n < LEN(name)) and Scan.isWord(name[n]) do INC(n); end; |
| 438 | name := name[0..n]; |
| 439 | write(out, dir, name, ".raz", Buf.bytes(prog)); |
| 440 | Out.str(name); Out.ln; |
| 441 | after := k + 1; |
| 442 | elsif LEN(shows) > 0 then |
| 443 | write(out, dir, shows, ".shown", Buf.bytes(prog)); |
| 444 | end; |
| 445 | inBlock := false; |
| 446 | elsif not inBlock and (fence or page and Str.hasPrefix(line, ".Bd -literal")) then |
| 447 | inBlock := true; |
| 448 | shows := ""; |
| 449 | if page and (i = after) then shows := name; end; |
| 450 | Buf.reset(prog); |
| 451 | first := ""; last := ""; |
| 452 | whole := true; |
| 453 | elsif inBlock then |
| 454 | addLine(prog, line, page); |
| 455 | if Str.trim(line) = "..." then whole := false; end; |
| 456 | if LEN(Str.trim(line)) > 0 then |
| 457 | if LEN(first) = 0 then first := Str.trim(line); end; |
| 458 | last := Str.trim(line); |
| 459 | end; |
| 460 | end; |
| 461 | i := k + 1; |
| 462 | end; |
| 463 | end; |
| 464 | |
| 465 | (* The modules named from argument 2 on, as mdoc: what goes between a |
| 466 | manual page's .Sh MODULES and its next heading. The page's head and |
| 467 | tail, and its date, belong to the page, not to this tool: raz's |
| 468 | Makefile puts doc/raz-lib.head and doc/raz-lib.tail around it. *) |
| 469 | proc page; |
| 470 | var i: INT; |
| 471 | begin |
| 472 | man := true; |
| 473 | atStart := true; |
| 474 | groupStart := -1; |
| 475 | for i := 2 to ARGC() - 1 do document(Files.arg(i)); end; |
| 476 | close; |
| 477 | end; |
| 478 | |
| 479 | proc main*; |
| 480 | begin |
| 481 | if (ARGC() = 4) and (Files.arg(1) = "examples") then examples(Files.arg(2), Files.arg(3)); |
| 482 | elsif (ARGC() >= 3) and (Files.arg(1) = "man") then page; |
| 483 | elsif ARGC() = 2 then document(Files.arg(1)); |
| 484 | else Rt.stop("usage: Doc Module.raz, Doc man Module.raz ..., or Doc examples file dir"); |
| 485 | end; |
| 486 | end; |
| 487 | |
| 488 | end Doc. |