AtlatestRepositorysigil-doc

sigil-doc / tree / testsd-p1-classification-probe.sgl

1;;; SD-P1 -- head-symbol vs slot-count classification probe.
2;;;
3;;; This probe ASSERTS NOTHING. It RECORDS what the lowering path does with
4;;; `@text{...}` and `@expr{...}`, the two author commands whose head symbol
5;;; collides with the parser's internal five-slot `text`/`expr` pseudo-kinds
6;;; (see docs/passive-parse-api.md and topics/sigil-doc-passive-parse-api).
7;;;
8;;; It exists because "the fix regresses no currently-correct manuscript" is a
9;;; MEASUREMENT, not a premise. Run it against the pre-SD-P1 parser to record the
10;;; BEFORE, and against the post-SD-P1 parser to record the AFTER; the gate and
11;;; the session log carry both transcripts side by side.
12;;;
13;;; Every case prints `SD_P1_CASE <label> :: <outcome>`, where the outcome is
14;;; either the exception message or the lowered document rendered to plain text.
15;;; Cases A-E and I are the COLLIDING spellings, in four distinct positions:
16;;; top level, block, inline, and inside an inline `@code` body. Cases F, G and
17;;; H are POSITIVE CONTROLS -- ordinary commands, ordinary prose, and a genuine
18;;; `@(...)` author expression -- whose behaviour must be IDENTICAL before and
19;;; after, so a transcript in which everything moved is distinguishable from one
20;;; in which only the collisions moved.
21;;; TWO INSTRUMENT DEFECTS were found while building this probe, both worth
22;;; knowing about before you trust any run of it:
23;;;
24;;; 1. Its first version imported neither `(sigil doc author)` nor the author
25;;; runtime, so every lowered document died on `unbound variable
26;;; 'author-at'` -- including the POSITIVE CONTROLS. That is what exposed
27;;; it: a probe whose controls fail is broken, not informative.
28;;; 2. `sigil -L <other>/src` run from THIS package's root does NOT shadow the
29;;; package's own `src/`. A "pre-SD-P1" run driven that way silently
30;;; measures the CURRENT parser. The pristine tree must be run from its own
31;;; package root, and `test/gate-sd-p1.sh` asserts a positive control --
32;;; the pre-SD-P1 parser must NOT resolve `sigil-doc-profile` -- before it
33;;; believes any BEFORE transcript.
34;;; SD-P8 (2026-08-22) MOVED EVERY `@chapter` HERE TO `@article`, because ruling
35;;; D8 made `@article` the root `#lang sigil/doc` admits and this probe drives
36;;; the DEFAULT profile through `parse-document-source`.
37;;;
38;;; THE THREE POSITIVE CONTROLS BELOW STAYED BYTE-IDENTICAL TO THE PINNED
39;;; PRE-SD-P1 TRANSCRIPT, AND THAT IS LUCK, NOT DESIGN. `@article` and
40;;; `@chapter` are both 8 characters, so no byte offset in any case moved.
41;;; `test/gate-sd-p1.sh` arm 2 requires F, G and H to match
42;;; `test/transcripts/sd-p1/classification-before.txt` exactly, so that arm is
43;;; LENGTH-SENSITIVE to this file's root spelling and nothing in it says so. A
44;;; future root rename of a different length will redden it for a reason that
45;;; has nothing to do with the classification it checks. If you are reading this
46;;; because that happened: the controls are still correct, their OFFSETS moved.
47;;;
48(import (sigil core) (sigil doc) (sigil doc author)
49 (sigil doc private author-runtime) (sigil doc private parser))
51;; One case must occupy exactly ONE line. `document->plain` emits newlines, and
52;; a multi-line outcome would let the gate's per-line comparison see only each
53;; case's first line -- which for the positive controls is the constant prefix
54;; `OK: <title>` and would therefore match before and after no matter how the
55;; body changed. Flattening is what keeps those controls load-bearing.
56(define (flatten text)
57 (let loop ((cs (string->list text)) (out '()))
58 (cond
59 ((null? cs) (list->string (reverse out)))
60 ((char=? (car cs) #\newline) (loop (cdr cs) (cons #\n (cons #\\ out))))
61 (else (loop (cdr cs) (cons (car cs) out))))))
63(define (outcome thunk)
64 (flatten
65 (guard (exn (else (string-append "RAISED: " (exception-message exn))))
66 (thunk))))
68;; Lower and evaluate, then render. Any failure at either step is reported as
69;; the message, so a case that cannot be lowered and a case that cannot be
70;; evaluated are both visible rather than fatal.
71(define (lower-and-render source name)
72 (outcome
73 (lambda ()
74 (let ((document (eval (parse-document-source source name 16))))
75 (string-append "OK: " (document->plain document))))))
77(define (lower-only source name)
78 (outcome (lambda () (begin (parse-document-source source name 16) "OK: lowered"))))
80(define (report label value)
81 (display "SD_P1_CASE ") (display label) (display " :: ") (display value) (newline))
83;; ---------------------------------------------------------------- cases
85;; A. `@text{...}` as the sole top-level node. The root-policy loop filters
86;; empty top-level TEXT nodes with `(trim (caddr node))`; for a COMMAND node
87;; `caddr` is the child list, not a string.
88(report "A-text-command-at-top-level"
89 (lower-only "#lang sigil/doc\n@text{a text command}\n" "a.sgd"))
91;; B. `@expr{...}` as the sole top-level node. The same loop treats a node whose
92;; head is `expr` as a hoisted author form and splices `caddr` -- again the
93;; child list -- into the generated form sequence.
94(report "B-expr-command-at-top-level"
95 (lower-only "#lang sigil/doc\n@expr{an expr command}\n" "b.sgd"))
97;; C. `@text{...}` at BLOCK position inside a chapter. `lower-block` matches on
98;; the head symbol and runs `paragraph-ranges` over the command's own source.
99(report "C-text-command-at-block-position"
100 (lower-and-render
101 "#lang sigil/doc\n@article[\"C\" \"A\"]{\n@text{a text command}\n}" "c.sgd"))
103;; D. `@expr{...}` at BLOCK position inside a chapter.
104(report "D-expr-command-at-block-position"
105 (lower-and-render
106 "#lang sigil/doc\n@article[\"D\" \"A\"]{\n@expr{an expr command}\n}" "d.sgd"))
108;; E. `@text{...}` at INLINE position inside a paragraph.
109(report "E-text-command-inline"
110 (lower-and-render
111 "#lang sigil/doc\n@article[\"E\" \"A\"]{@p{before @text{inline text} after}}" "e.sgd"))
113;; I. `@text{...}` inside an INLINE `@code{...}` body. `inline-code-content`
114;; concatenates `caddr` of each child; for a five-slot text node that is a
115;; STRING, but for a six-slot command it is the child LIST.
116(report "I-text-command-inside-inline-code"
117 (lower-and-render
118 "#lang sigil/doc\n@article[\"I\" \"A\"]{@p{@code{@text{x}}}}" "i.sgd"))
120;; F. POSITIVE CONTROL -- an ordinary inline command with no name collision.
121;; This must render identically before and after the change.
122(report "F-control-em-inline"
123 (lower-and-render
124 "#lang sigil/doc\n@article[\"F\" \"A\"]{@p{before @em{emphasis} after}}" "f.sgd"))
126;; G. POSITIVE CONTROL -- ordinary prose at block position, which is a genuine
127;; five-slot `text` pseudo-node and must keep lowering to a paragraph.
128(report "G-control-bare-prose-block"
129 (lower-and-render
130 "#lang sigil/doc\n@article[\"G\" \"A\"]{\nbare prose paragraph\n}" "g.sgd"))
132;; H. POSITIVE CONTROL -- a genuine top-level `@(...)` author expression, which
133;; is a five-slot `expr` pseudo-node and must still be hoisted as a form.
134;; Reports the FORM COUNT (one author form plus the root), so this control
135;; observes hoisting succeeding rather than an error message.
136(report "H-control-author-expression-at-top-level"
137 (outcome
138 (lambda ()
139 (string-append
140 "OK: forms="
141 (number->string
142 (length (parse-document-forms
143 "#lang sigil/doc\n@(define x 1)\n@article[\"H\" \"A\"]{@p{body}}"
144 "h.sgd" 16)))))))
146(display "SD_P1_CLASSIFICATION_PROBE_COMPLETE 9 cases")
147(newline)