AtlatestRepositorysigil-doc

sigil-doc / tree / testsd-p3-boundary-census.sgl

1;;; SD-P3 -- census of BLOCK-CONTEXT children that are NOT at a block boundary.
2;;;
3;;; SD-P3 folds a non-block-boundary command into the enclosing paragraph run.
4;;; Everything at a block boundary keeps today's semantics. So the population
5;;; whose meaning can possibly change is exactly:
6;;;
7;;; nodes appearing in BLOCK context whose `@` is not at a block boundary.
8;;;
9;;; Today every one of those is lifted to block position, and the container
10;;; rejects it unless it happens to evaluate to a block node. This probe
11;;; measures that population over real manuscripts BEFORE the change, so
12;;; "no existing manuscript changes meaning" is a measurement rather than a
13;;; belief.
14;;;
15;;; It reports DENOMINATORS as well as hits: a census that prints only its hits
16;;; cannot be distinguished from a census that examined nothing.
17;;;
18;;; Passive: `(sigil doc inspect)` runs no author code.
19(import (sigil core) (sigil fs) (sigil process) (sigil language) (sigil doc inspect))
21;; The same test the parser applies at scan time (`block-boundary?` in
22;; `private/parser.sgl`): scan back over horizontal space to a newline or the
23;; start of the manuscript.
24(define (horizontal-space? c) (or (char=? c #\space) (char=? c #\tab)))
25(define (block-boundary? source i)
26 (let loop ((j (- i 1)))
27 (cond ((< j 0) #t)
28 ((char=? (string-ref source j) #\newline) #t)
29 ((horizontal-space? (string-ref source j)) (loop (- j 1)))
30 (else #f))))
32;; Block context propagates exactly as `lower-block`/`lower-inline` propagate
33;; it: the roots' children are block, a block-context command's children are
34;; block unless the command is `p` or `code`, and everything below an inline
35;; context is inline.
36(define (inline-body-command? name) (or (eq? name 'p) (eq? name 'code)))
38(define examined 0)
39(define hits-command 0)
40(define hits-expr 0)
42(define (note-hit source path node kind)
43 (let ((start (tree-node-start node)))
44 (display "HIT ") (display path)
45 (display " ") (display kind)
46 (display " ") (write (or (tree-node-name node) 'expression))
47 (display " scalar=") (display start)
48 (display " line=") (display (srcloc-line (tree-node-location node)))
49 (display " column=") (display (srcloc-column (tree-node-location node)))
50 (display " slice=")
51 (write (substring source start (if (< (+ start 24) (string-length source)) (+ start 24) (string-length source))))
52 (newline)))
54(define (walk-block source path nodes)
55 (for-each
56 (lambda (node)
57 (let ((kind (tree-node-kind node)))
58 (if (not (eq? kind 'text))
59 (begin
60 (set! examined (+ examined 1))
61 (if (not (block-boundary? source (tree-node-start node)))
62 (begin
63 (if (eq? kind 'command)
64 (set! hits-command (+ hits-command 1))
65 (set! hits-expr (+ hits-expr 1)))
66 (note-hit source path node kind))
67 #f))
68 #f)
69 (if (and (eq? kind 'command)
70 (not (inline-body-command? (tree-node-name node))))
71 (walk-block source path (tree-node-children node))
72 #f)))
73 nodes))
75(define (census path)
76 (let* ((source (read-file-string path))
77 (header (inspect-language-source source path))
78 (tree (inspect-document-source source path (language-header-end header)))
79 (before-examined examined)
80 (before-command hits-command)
81 (before-expr hits-expr))
82 (walk-block source path
83 (apply append (map tree-node-children (document-tree-roots tree))))
84 (display "(report ") (write path)
85 (display " examined ") (display (- examined before-examined))
86 (display " nonboundary-command ") (display (- hits-command before-command))
87 (display " nonboundary-expr ") (display (- hits-expr before-expr))
88 (display ")") (newline)))
90(define (sgd-files dir)
91 (let loop ((entries (directory-list dir)) (out '()))
92 (cond
93 ((null? entries) (reverse out))
94 (else
95 (let* ((name (car entries))
96 (full (string-append dir "/" name))
97 (n (string-length name)))
98 (loop (cdr entries)
99 (if (and (> n 4) (string=? (substring name (- n 4) n) ".sgd"))
100 (cons full out)
101 out)))))))
103(define in-repo
104 (list "book/practical-sigil/01-useful-first-script.sgd"
105 "book/practical-sigil/02-change-the-question.sgd"
106 "test/fixtures/practical-sigil-chapter.sgd"
107 "test/fixtures/practical-sigil-change-question.sgd"
108 "test/fixtures/minimal.sgd"
109 "test/fixtures/semantic-structures.sgd"
110 "test/fixtures/glossary-lesson.sgd"
111 "test/fixtures/glossary-page.sgd"
112 "test/fixtures/d3-examples.sgd"
113 "test/fixtures/d3-adversarial.sgd"
114 ;; The corpus MUST match `test/sd-p3-lowering-dump.sgl` exactly. The
115 ;; gate cross-checks the manuscripts whose lowering changed against the
116 ;; manuscripts this census flags, and two instruments can only agree
117 ;; about a population they both cover. Derivation and the five
118 ;; justified exclusions are recorded in that file.
119 "test/fixtures/d3-duplicate.sgd"
120 "test/fixtures/d3-empty.sgd"
121 "test/fixtures/d3-backend-mismatch.sgd"
122 "test/fixtures/sd-p4-collision.sgd"
123 "test/fixtures/sd-p4-witness.sgd"
124 ;; RE-DERIVED after the SD-P2 merge. That phase added a manuscript, and
125 ;; a corpus that is not re-derived when the tree changes is the same
126 ;; inherited-population defect one merge later.
127 "test/fixtures/url-links.sgd"))
129(define course-dir
130 (or (getenv "SD_P3_COURSE_DIR") ""))
132(define manuscripts
133 (append in-repo
134 (if (string=? course-dir "") '() (sgd-files course-dir))))
136(for-each census manuscripts)
137(display "SD_P3_BOUNDARY_CENSUS_COMPLETE manuscripts ")
138(display (length manuscripts))
139(display " examined ") (display examined)
140(display " nonboundary-command ") (display hits-command)
141(display " nonboundary-expr ") (display hits-expr)
142(newline)