AtlatestRepositorysigil-doc

sigil-doc / tree / testsd-p3-lowering-dump.sgl

1;;; SD-P3 -- canonical dump of LOWERED output for real manuscripts.
2;;;
3;;; Same instrument as `test/sd-p1-lowering-dump.sgl` (whole form sequence, every
4;;; datum, every source location including generated-origin chains) over a WIDER
5;;; corpus: the ten in-repo manuscripts plus, when `SD_P3_COURSE_DIR` is set, the
6;;; Guile Beginners course conversion. "Existing manuscripts do not change
7;;; meaning" is an EQUALITY claim, and this is what makes it one instead of an
8;;; absence of complaint.
9;;;
10;;; The corpus is printed with its denominator and the course directory is named
11;;; in the output, so a run that silently covered only the in-repo ten cannot be
12;;; mistaken for a run that covered everything.
13;;;
14;;; Nothing is evaluated: lowering is a pure source-to-syntax transformation, so
15;;; this runs no author code and needs none of the corpus's author modules.
16(import (sigil core) (sigil fs) (sigil process) (sigil language)
17 (sigil doc private parser))
19(define (emit-location loc depth)
20 (if (or (not loc) (> depth 40))
21 (display "#f")
22 (begin
23 (display "[") (write (srcloc-file loc))
24 (display " ") (display (srcloc-line loc))
25 (display ":") (display (srcloc-column loc))
26 (display " byte=") (display (srcloc-position loc))
27 (display " span=") (display (srcloc-span loc))
28 (let ((origin (srcloc-origin loc)))
29 (if origin
30 (begin (display " <-") (emit-location origin (+ depth 1)))
31 #f))
32 (display "]"))))
34;; Walk the improper-list structure explicitly. `write` alone would not show
35;; syntax wrappers or locations, which are the part most likely to move.
36(define (emit value)
37 (cond
38 ((syntax? value)
39 (display "#<")
40 (emit-location (syntax-srcloc value) 0)
41 (display " ")
42 (emit (syntax-datum value))
43 (display ">"))
44 ((null? value) (display "()"))
45 ((pair? value)
46 (display "(")
47 (let loop ((rest value) (first? #t))
48 (cond
49 ((null? rest) #t)
50 ((pair? rest)
51 (if first? #t (display " "))
52 (emit (car rest))
53 (loop (cdr rest) #f))
54 (else (display " . ") (emit rest))))
55 (display ")"))
56 (else (write value))))
58;; The manuscript's own path is printed as the LABEL, but the source name handed
59;; to the parser is the BASENAME. Locations embed the source name, and the course
60;; corpus lives at an absolute path outside this repo, so using the full path
61;; would make every course location depend on where the checkout sits -- which
62;; would defeat a BEFORE/AFTER comparison run from two different worktrees.
63(define (basename path)
64 (let loop ((i (string-length path)))
65 (cond ((= i 0) path)
66 ((char=? (string-ref path (- i 1)) #\/) (substring path i (string-length path)))
67 (else (loop (- i 1))))))
69(define (dump path)
70 (let* ((source (read-file-string path))
71 (name (basename path))
72 (header (inspect-language-source source name))
73 (forms (parse-document-forms source name (language-header-end header))))
74 (display "=== ") (display name)
75 (display " forms=") (display (length forms)) (newline)
76 (for-each (lambda (form) (emit form) (newline)) forms)))
78(define (sgd-files dir)
79 (let loop ((entries (sort-strings (directory-list dir))) (out '()))
80 (cond
81 ((null? entries) (reverse out))
82 (else
83 (let* ((name (car entries))
84 (n (string-length name)))
85 (loop (cdr entries)
86 (if (and (> n 4) (string=? (substring name (- n 4) n) ".sgd"))
87 (cons (string-append dir "/" name) out)
88 out)))))))
90;; `directory-list` order is filesystem order and is NOT guaranteed stable across
91;; two checkouts. Sorting is what makes the BEFORE and AFTER dumps comparable
92;; line by line rather than only as sets.
93(define (string-before? a b)
94 (let ((na (string-length a)) (nb (string-length b)))
95 (let loop ((i 0))
96 (cond ((= i na) (> nb na))
97 ((= i nb) #f)
98 ((char<? (string-ref a i) (string-ref b i)) #t)
99 ((char<? (string-ref b i) (string-ref a i)) #f)
100 (else (loop (+ i 1)))))))
101(define (sort-strings xs)
102 (let insert-sort ((rest xs) (out '()))
103 (if (null? rest) out
104 (insert-sort (cdr rest)
105 (let place ((ys out))
106 (cond ((null? ys) (list (car rest)))
107 ((string-before? (car rest) (car ys)) (cons (car rest) ys))
108 (else (cons (car ys) (place (cdr ys))))))))))
110;; Real manuscripts, not synthetic ones: these are what "unchanged" has to mean.
111;; Identical list to SD-P1's dump, deliberately -- `test/fixtures/sd-p4-collision.sgd`
112;; stays out for the reason recorded there.
113(define in-repo
114 (list "book/practical-sigil/01-useful-first-script.sgd"
115 "book/practical-sigil/02-change-the-question.sgd"
116 "test/fixtures/practical-sigil-chapter.sgd"
117 "test/fixtures/practical-sigil-change-question.sgd"
118 "test/fixtures/minimal.sgd"
119 "test/fixtures/semantic-structures.sgd"
120 "test/fixtures/glossary-lesson.sgd"
121 "test/fixtures/glossary-page.sgd"
122 "test/fixtures/d3-examples.sgd"
123 "test/fixtures/d3-adversarial.sgd"
124 ;; ---- ADDED BY SD-P3, and the DERIVATION matters more than the count.
125 ;;
126 ;; The ten manuscripts above are SD-P1's list. I inherited it and never
127 ;; asked what it OMITTED, which made SD-P1's scope silently mine.
128 ;; `test/gate-d3.sh` then caught a regression in `d3-duplicate.sgd` --
129 ;; a fixture this corpus did not contain -- holding the one shape that
130 ;; broke: `@example[...]{@code["sigil"]{1}}` on a single line, whose
131 ;; `@code` is not at a block boundary.
132 ;;
133 ;; DERIVED POPULATION, so the exclusions are decisions rather than
134 ;; oversights. Re-derived on the post-SD-P2 tree:
135 ;; `find book test -name '*.sgd'` = 21 files, 16 are here.
136 ;; The 5 absent, each with its reason:
137 ;;
138 ;; sd-p1-article-root.sgd root @article -- the DEFAULT profile
139 ;; sd-p1-folio-roots.sgd roots @note/@project rejects these, so
140 ;; sd-p4-note-root.sgd root @note -- they cannot be lowered
141 ;; by this dump at all
142 ;; sd-p3-bare-prose.sgd created by THIS phase, so no pre-SD-P3
143 ;; sd-p3-wrapped.sgd baseline can exist for either
144 ;;
145 ;; `sd-p4-collision.sgd` and `sd-p4-witness.sgd` ARE included now.
146 ;; SD-P1 excluded the first because it uses `@text{}`/`@expr{}`, which
147 ;; SD-P1 was changing; SD-P3 changes neither, so that reason does not
148 ;; transfer. An inherited exclusion needs its own justification, not
149 ;; the previous phase's.
150 "test/fixtures/d3-duplicate.sgd"
151 "test/fixtures/d3-empty.sgd"
152 "test/fixtures/d3-backend-mismatch.sgd"
153 "test/fixtures/sd-p4-collision.sgd"
154 "test/fixtures/sd-p4-witness.sgd"
155 ;; RE-DERIVED after the SD-P2 merge. That phase added a manuscript, and
156 ;; a corpus that is not re-derived when the tree changes is the same
157 ;; inherited-population defect one merge later.
158 "test/fixtures/url-links.sgd"))
160(define course-dir (or (getenv "SD_P3_COURSE_DIR") ""))
161(define course (if (string=? course-dir "") '() (sgd-files course-dir)))
162(define manuscripts (append in-repo course))
164(for-each dump manuscripts)
165(display "SD_P3_LOWERING_DUMP_COMPLETE ")
166(display (length manuscripts))
167(display " manuscripts in-repo ")
168(display (length in-repo))
169(display " course ")
170(display (length course))
171(newline)