AtlatestRepositorysigil-doc
1
;;; SD-P1 -- the root container as a reader/profile parameter (review B2).2
;;;3
;;; Two properties this suite is built around, both learned the expensive way4
;;; on this track:5
;;;6
;;; * EVERY location assertion recomputes its expected byte offset, line and7
;;; column INDEPENDENTLY, walking the manuscript character by character.8
;;; Asking the parser to confirm its own arithmetic passes under any9
;;; indexing bug.10
;;; * The span arm asserts its DISCRIMINATING PRECONDITION -- that byte and11
;;; scalar offsets genuinely disagree at the point it probes -- rather than12
;;; believing it about the fixture. SD-P4's first state test probed a point13
;;; where its two manuscripts were both still ASCII and would have passed14
;;; vacuously.15
;;;16
;;; Everything here is imported from `(sigil doc reader)` alone. That is17
;;; deliberate: it demonstrates that a delegating language such as18
;;; `#lang folio/doc` needs no private module to parameterize the root19
;;; container. The three-argument `parse-document-source` / `parse-document-forms`20
;;; spellings are unchanged and are covered by `test/test-reader.sgl`, which21
;;; this phase does not touch.22
(import (sigil test) (sigil fs) (sigil syntax)23
(sigil doc) (sigil doc author) (sigil doc private author-runtime)24
(sigil doc reader)25
;; ONLY the profile. `(sigil doc book reader)` also exports26
;; `read-language-source`, and an unqualified import silently shadows27
;; the BASE language's reader -- which this suite calls by that name.28
;; Cost of learning that: one arm that reported a `sigil/doc/book:`29
;; diagnostic while claiming to exercise `#lang sigil/doc`.30
(only (sigil doc book reader) sigil-doc-book-profile))32
;; ---------------------------------------------------------------- helpers34
(define (utf8-width c)35
(let ((n (char->integer c)))36
(cond ((<= n #x7f) 1) ((<= n #x7ff) 2) ((<= n #xffff) 3) (else 4))))38
;; Independent UTF-8 byte offset of scalar index `index`.39
(define (byte-offset source index)40
(let loop ((i 0) (bytes 0))41
(if (= i index) bytes (loop (+ i 1) (+ bytes (utf8-width (string-ref source i)))))))43
;; Independent scalar line/column at scalar index `index`.44
(define (cursor source index)45
(let loop ((i 0) (line 1) (column 1))46
(if (= i index) (list line column)47
(if (char=? (string-ref source i) #\newline)48
(loop (+ i 1) (+ line 1) 1)49
(loop (+ i 1) line (+ column 1))))))51
;; Scalar index of the single occurrence of `needle`. Raises when the needle is52
;; absent OR repeated: a fixture edit that made an anchor ambiguous would53
;; otherwise silently move the assertion to a different node.54
(define (index-of source needle)55
(let* ((n (string-length source)) (m (string-length needle)))56
(let loop ((i 0) (found #f))57
(cond58
((> (+ i m) n)59
(if found found (error (string-append "anchor not found: " needle))))60
((string=? (substring source i (+ i m)) needle)61
(if found (error (string-append "anchor is ambiguous: " needle))62
(loop (+ i 1) i)))63
(else (loop (+ i 1) found))))))65
(define (contains? haystack needle)66
(let ((n (string-length haystack)) (m (string-length needle)))67
(let loop ((i 0))68
(cond ((> (+ i m) n) #f)69
((string=? (substring haystack i (+ i m)) needle) #t)70
(else (loop (+ i 1)))))))72
(define (message thunk)73
(guard (exn (else (exception-message exn))) (thunk) "NO ERROR"))75
(define (source-of path) (read-file-string path))77
;; The manuscript origin of a lowered root. Generated syntax is located at78
;; `<sigil/doc-generated>` and carries the authoritative manuscript srcloc as79
;; its origin; that origin is what a consumer needs and what this suite pins.80
(define (root-origin root) (srcloc-origin (syntax-srcloc root)))82
(define folio-profile (make-document-profile "folio/doc" '(note project) 'one-or-more))83
(define folio-one-profile (make-document-profile "folio/doc" '(note project) 'one))84
;; The byte offset just past the `#lang` line. Derived from the source rather85
;; than written as 16 or 21: SD-P8 gave this suite manuscripts in two languages86
;; whose header lines differ in length, and a hardcoded offset would silently87
;; slice into the wrong place for one of them.88
(define (header-end-of source)89
(let loop ((i 0))90
(cond ((>= i (string-length source)) i)91
((char=? (string-ref source i) #\newline) (+ i 1))92
(else (loop (+ i 1))))))94
(define folio-path "test/fixtures/sd-p1-folio-roots.sgd")95
(define folio-source (source-of folio-path))97
;; Roots checked across the suite, printed so the gate can assert the walk was98
;; not empty. A span arm that examined nothing must never read as a pass.99
(define roots-checked 0)101
;; Assert one root's whole location against independently recomputed truth, and102
;; that its span names exactly the slice it claims.103
(define (check-root root source open-anchor close-anchor name)104
(let* ((start (index-of source open-anchor))105
(end (+ (index-of source close-anchor) (string-length close-anchor)))106
(start-cursor (cursor source start))107
(loc (root-origin root)))108
(set! roots-checked (+ roots-checked 1))109
;; `name` is not asserted from the lowered syntax shape here. A root lowers110
;; to `(author-at <loc> (lambda () (author-normalize (<name> ...) ...)))`,111
;; so digging the name out means walking four levels of generated syntax112
;; that this phase does not own. The identity assertions below are the113
;; source SLICE -- which must literally open with this root's own `@name[`114
;; and close with its own terminator -- and the arm that evaluates an115
;; `@article` root to a document of kind `article`, which proves dispatch by116
;; name end to end.117
name118
(assert-equal (car start-cursor) (srcloc-line loc))119
(assert-equal (cadr start-cursor) (srcloc-column loc))120
(assert-equal (byte-offset source start) (srcloc-position loc))121
(assert-equal (- (byte-offset source end) (byte-offset source start))122
(srcloc-span loc))123
;; The slice the span names must actually be this root's surface syntax.124
(assert-equal open-anchor (substring source start (+ start (string-length open-anchor))))125
(assert-equal close-anchor (substring source (- end (string-length close-anchor)) end))126
(list start end)))128
;; ---------------------------------------------------------------- suite130
(test "a profile with a different root set parses, with exact locations"131
(let* ((parts (parse-document-parts-with-profile132
folio-profile folio-source folio-path 16))133
(forms (car parts))134
(roots (cadr parts)))135
(assert-equal '() forms)136
(assert-equal 2 (length roots))137
(let* ((first-span (check-root (car roots) folio-source138
"@note[\"topics/alpha\"" "@end note\n" 'note))139
(second-span (check-root (cadr roots) folio-source140
"@project[\"projects/beta\"" "@end project\n" 'project))141
(second-start (car second-span)))142
;; THE DISCRIMINATING PRECONDITION. Multi-byte prose sits inside the first143
;; root, so at the second root's start the byte offset and the scalar144
;; index must genuinely differ. Without this the byte assertions above145
;; would hold just as well under a parser that returned scalars.146
(assert-true (not (= (byte-offset folio-source second-start) second-start)))147
;; ...and the roots are returned in manuscript order, not scan-stack order.148
(assert-true (< (car first-span) second-start)))))150
(test "the root allowlist rejects a root it does not name, in both directions"151
;; The CLASS, not a spelling: a root outside the set is refused whichever152
;; profile is asked, so a parser that ignored the allowlist reddens here153
;; twice rather than once.154
;;155
;; SD-P8 made this arm three-way rather than two-way. Since D8 the base and156
;; the book genre name DIFFERENT roots, so `@chapter` is now a root some157
;; sigil-doc profile rejects -- which is the case the two-profile version158
;; could not express.159
(let ((article-source "#lang sigil/doc\n@article[\"C\" \"A\"]{@p{body}}")160
(chapter-source "#lang sigil/doc/book\n@chapter[\"C\" \"A\"]{@p{body}}"))161
;; A folio profile must NOT accept sigil/doc's root.162
(assert-equal163
"folio/doc: top-level author forms must precede at least one @note or @project"164
(message (lambda () (parse-document-parts-with-profile165
folio-profile article-source "c.sgd" 16))))166
;; ...and sigil/doc's profile must NOT accept a folio root.167
(assert-equal168
"sigil/doc: top-level author forms must precede exactly one @article"169
(message (lambda () (parse-document-parts-with-profile170
sigil-doc-profile folio-source folio-path 16))))171
;; ...nor the genre language's root, which is the direction D8 created.172
(assert-equal173
"sigil/doc: top-level author forms must precede exactly one @article"174
(message (lambda () (parse-document-parts-with-profile175
sigil-doc-profile chapter-source "c.sgd" 21))))176
;; ...and the genre language refuses the BASE's root, symmetrically.177
(assert-equal178
"sigil/doc/book: top-level author forms must precede exactly one @chapter"179
(message (lambda () (parse-document-parts-with-profile180
sigil-doc-book-profile article-source "a.sgd" 16))))181
;; Each profile still accepts the root it DOES name -- otherwise a parser182
;; that rejected everything would satisfy every assertion above.183
(let ((parts (parse-document-parts-with-profile184
sigil-doc-profile article-source "c.sgd" 16)))185
(assert-equal 1 (length (cadr parts))))186
(let ((parts (parse-document-parts-with-profile187
sigil-doc-book-profile chapter-source "c.sgd" 21)))188
(assert-equal 1 (length (cadr parts))))))190
(test "multiplicity is a parameter, and is enforced in both directions"191
(let ((single "#lang sigil/doc\n@note[\"topics/only\"]{@p{one root}}\n")192
(two-articles193
"#lang sigil/doc\n@article[\"A\" \"A\"]{@p{x}}\n@article[\"B\" \"B\"]{@p{y}}\n")194
(two-chapters195
"#lang sigil/doc/book\n@chapter[\"A\" \"A\"]{@p{x}}\n@chapter[\"B\" \"B\"]{@p{y}}\n"))196
;; one-or-more accepts one AND several.197
(assert-equal 1 (length (cadr (parse-document-parts-with-profile198
folio-profile single "s.sgd" 16))))199
(assert-equal 2 (length (cadr (parse-document-parts-with-profile200
folio-profile folio-source folio-path 16))))201
;; The same root set under `one` refuses the second root, and says so.202
(assert-equal203
"folio/doc: top-level author forms must precede exactly one @note or @project"204
(message (lambda () (parse-document-parts-with-profile205
folio-one-profile folio-source folio-path 16))))206
;; ...and still accepts a single-root manuscript, so the arm above is not207
;; passing because `one` rejects everything.208
(assert-equal 1 (length (cadr (parse-document-parts-with-profile209
folio-one-profile single "s.sgd" 16))))210
;; sigil/doc's own multiplicity is unchanged by SD-P8; only its root moved.211
(assert-equal212
"sigil/doc: top-level author forms must precede exactly one @article"213
(message (lambda () (parse-document-parts-with-profile214
sigil-doc-profile two-articles "t.sgd" 16))))215
;; ...and the genre language carries the same `one`, which is what makes a216
;; two-chapter manuscript a diagnostic rather than a dropped root.217
(assert-equal218
"sigil/doc/book: top-level author forms must precede exactly one @chapter"219
(message (lambda () (parse-document-parts-with-profile220
sigil-doc-book-profile two-chapters "t.sgd" 21))))))222
(test "the base profile is D8's policy and D8's diagnostics, exactly"223
;; THE PINNED LITERALS. These are the two strings `#lang sigil/doc` raises,224
;; and they are DERIVED from `sigil-doc-profile` rather than written out in225
;; the parser, so this arm is what stops the derivation from quietly226
;; rewording a shipped diagnostic.227
;;228
;; SD-P8 CHANGED THEM, from `@chapter` to `@article`, and that is exactly the229
;; migration shape this estate distrusts: updating a pinned literal to match230
;; the new output proves nothing on its own. Each literal below was therefore231
;; shown still able to FAIL after the migration -- see232
;; `test/sabotage-sd-p8.sh` legs 1-3, which perturb the PRODUCT (the profile233
;; and the derivation) rather than the assertion and require this suite to234
;; redden at these arms by name.235
(assert-equal "sigil/doc" (document-profile-name sigil-doc-profile))236
(assert-equal '(article) (document-profile-roots sigil-doc-profile))237
(assert-equal 'one (document-profile-multiplicity sigil-doc-profile))238
(assert-equal239
"sigil/doc: source must contain exactly one @article"240
(message (lambda () (parse-document-parts-with-profile241
sigil-doc-profile "#lang sigil/doc\n@(define x 1)\n" "n.sgd" 16))))242
(assert-equal243
"sigil/doc: source must contain exactly one @article"244
(message (lambda () (parse-document-parts-with-profile245
sigil-doc-profile "#lang sigil/doc\n" "e.sgd" 16))))246
(assert-equal247
"sigil/doc: top-level author forms must precede exactly one @article"248
(message (lambda () (parse-document-parts-with-profile249
sigil-doc-profile250
"#lang sigil/doc\n@article[\"C\" \"A\"]{@p{b}}\n@(define y 2)\n"251
"a.sgd" 16))))252
;; Three-or-more root sets get an Oxford-comma phrase; pinned so the253
;; formatter is covered beyond the one- and two-root cases. The names here254
;; are ARBITRARY command symbols, deliberately not the model's kinds -- root255
;; commands are the profile's vocabulary and document kinds are the model's,256
;; and D8 is the ruling that separated them.257
(assert-equal258
"sigil/doc: source must contain exactly one @article, @memo, or @chapter"259
(message (lambda () (parse-document-parts-with-profile260
(make-document-profile "sigil/doc" '(article memo chapter) 'one)261
"#lang sigil/doc\n" "o.sgd" 16)))))263
(test "the book genre language is a sibling profile with its own diagnostics"264
;; SD-P8. `#lang sigil/doc/book` is the D8 home of the book/print genre. It265
;; is a PROFILE, not a second parser: everything below the root -- the266
;; grammar, the locations, the author vocabulary -- is `#lang sigil/doc`'s,267
;; unchanged and undelegated-around.268
;;269
;; Its two literals are pinned for the same reason the base's are, and are270
;; shown able to fail by the same sabotage legs. The `sigil/doc/book:` prefix271
;; is the load-bearing part: a genre language that reported `sigil/doc:`272
;; would send an author to the wrong language's documentation.273
(assert-equal "sigil/doc/book" (document-profile-name sigil-doc-book-profile))274
(assert-equal '(chapter) (document-profile-roots sigil-doc-book-profile))275
(assert-equal 'one (document-profile-multiplicity sigil-doc-book-profile))276
(assert-equal277
"sigil/doc/book: source must contain exactly one @chapter"278
(message (lambda () (parse-document-parts-with-profile279
sigil-doc-book-profile "#lang sigil/doc/book\n" "e.sgd" 21))))280
(assert-equal281
"sigil/doc/book: source must contain exactly one @chapter"282
(message (lambda () (parse-document-parts-with-profile283
sigil-doc-book-profile284
"#lang sigil/doc/book\n@(define x 1)\n" "n.sgd" 21))))285
(assert-equal286
"sigil/doc/book: top-level author forms must precede exactly one @chapter"287
(message (lambda () (parse-document-parts-with-profile288
sigil-doc-book-profile289
"#lang sigil/doc/book\n@chapter[\"C\" \"A\"]{@p{b}}\n@(define y 2)\n"290
"b.sgd" 21))))291
;; POSITIVE CONTROL. Every assertion above is a refusal, and a profile that292
;; refused everything would satisfy all three. This one must be accepted.293
(let ((parts (parse-document-parts-with-profile294
sigil-doc-book-profile295
"#lang sigil/doc/book\n@chapter[\"C\" \"A\"]{@p{b}}\n" "g.sgd" 21)))296
(assert-equal '() (car parts))297
(assert-equal 1 (length (cadr parts)))))299
(test "the model's kinds are reachable from source, and `book` is gone"300
;; B2's other half, rewritten by SD-P8.301
;;302
;; BEFORE D8 this arm had to build a `container-profile` naming `@article`303
;; and `@book`, because neither kind was reachable from any shipped language.304
;; `article` no longer needs one: it is what `#lang sigil/doc` itself admits,305
;; so the arm now drives the DEFAULT profile and the coverage got stronger306
;; rather than weaker. `chapter` is reachable through the genre profile.307
;;308
;; `book` is gone from the model entirely (D8 item 4): it was constructed309
;; nowhere in this package or in folio, and a book is an ASSEMBLY built by310
;; `make-print-publication`, not a document kind. Its old arm is replaced by311
;; the refusal below, which is a stronger statement than the construction it312
;; replaces -- it says the kind cannot come back by accident.313
(define (evaluate profile source name)314
(let loop ((forms (parse-document-forms-with-profile profile source name315
(header-end-of source)))316
(last #f))317
(if (null? forms) last (loop (cdr forms) (eval (car forms))))))318
;; `@article` through the SHIPPED base profile, from the fixture on disk.319
(let ((article (evaluate sigil-doc-profile320
(source-of "test/fixtures/sd-p1-article-root.sgd")321
"test/fixtures/sd-p1-article-root.sgd")))322
(assert-true (document? article))323
(assert-equal 'article (document-kind article))324
(assert-equal "An Article Root" (document-title article)))325
;; `@chapter` through the SHIPPED genre profile. This is also the positive326
;; control on the arm: a failure above is then about the root set rather than327
;; about evaluation being broken.328
(let ((chapter (evaluate sigil-doc-book-profile329
"#lang sigil/doc/book\n@chapter[\"C\" \"A\"]{@p{body}}" "c.sgd")))330
(assert-true (document? chapter))331
(assert-equal 'chapter (document-kind chapter)))332
;; THE DROPPED KIND. `make-document` must refuse `book` by name, and must333
;; still accept the two that survive -- otherwise a constructor that refused334
;; everything would satisfy the first assertion.335
(let ((loc (make-srcloc "k.sgd" 1 1 0 1)))336
(assert-error (make-document 'book "A Book" (list "A")337
(list (make-paragraph (list (make-text "b" loc)) loc)) loc))338
(assert-true (contains?339
(message (lambda () (make-document 'book "A Book" (list "A")340
(list (make-paragraph (list (make-text "b" loc)) loc))341
loc)))342
"kind must be article or chapter"))343
(assert-equal 'article (document-kind344
(make-document 'article "An Article" (list "A")345
(list (make-paragraph (list (make-text "b" loc)) loc)) loc)))346
(assert-equal 'chapter (document-kind347
(make-document 'chapter "A Chapter" (list "A")348
(list (make-paragraph (list (make-text "b" loc)) loc)) loc)))))350
;; ---------------------------------------------------------------- t-3aecae352
(test "a manuscript-local define with keyword and rest parameters binds under #lang"353
;; WHAT THIS ARM CAN AND CANNOT SHOW. It gates like any other arm -- it goes354
;; red when the behaviour breaks. What it cannot do is demonstrate355
;; red-before / green-after for the ticket that produced it, because it was356
;; never red: the behaviour it pins has always worked on the `#lang` path. It357
;; exists because a comment in this very file asserted the opposite (added358
;; 2026-08-10 in `acb9adf`, corrected here 2026-08-11) and a ticket was filed359
;; against the wrong component before anyone contradicted it. Its job is to360
;; make that false claim impossible to restate: if anyone "fixes" the parser or361
;; the reader on the belief that this spelling does not bind in a manuscript,362
;; this goes red.363
;;364
;; It CAN fail, which is worth demonstrating rather than asserting.365
;; Sabotage-verified 2026-08-11 against master `7066ffc`: mutating the366
;; fixture's `kind` default from "note" to "info" took this suite from 12367
;; passed to 11 passed / 1 failed with368
;; expected "note: Default" but got "info: Default"369
;; and the fixture was then restored. Do the same before trusting any edit370
;; here. Also verified load-bearing: removing `(rest: body)`, replacing it with371
;; a dotted rest, or switching `(keys: …)` to `(opts: …)` each turn this red.372
;;373
;; The real defect t-3aecae turned out to name lives in sigil core, not in374
;; sigil/doc, and is reachable from the REPL and nREPL rather than from a375
;; manuscript -- see folio `investigations/eval-skips-define-macro-expansion`.376
;;377
;; The manuscript is brought in with `load`, NOT through the `evaluate` helper378
;; above, and that is the point: `load` runs the whole file through the compile379
;; path -- language reader, then the top-level macro-expansion pass -- which is380
;; the path an author's `#lang sigil/doc` manuscript actually takes, and the381
;; pass runtime `eval` never runs. Note `load` does NOT create a module: it382
;; evaluates into the CALLER's module, so the manuscript's `callout` and383
;; `document` land at this file's top level, which is why `document` is384
;; reachable below. The load is inside the arm rather than at file scope so a385
;; fixture problem fails THIS arm instead of the whole file.386
(load "test/fixtures/sd-manuscript-keyword-define.sgd")387
(let ((doc (eval 'document)))388
(assert-true (document? doc))389
(assert-equal 'chapter (document-kind doc))390
(assert-equal "Keyword Define" (document-title doc))391
;; Shape is asserted BEFORE anything indexes into it, so a wrong shape392
;; reports as a failed length assertion rather than as a `car` type error.393
(let ((sections (document-children doc)))394
(assert-equal 1 (length sections))395
(let ((callouts (section-children (car sections))))396
(assert-equal 2 (length callouts))397
;; Callout 1: the keyword argument was passed EXPLICITLY (`kind: "WARN"`),398
;; and the `(rest: body)` parameter captured the `@p{tail}` child. Both399
;; halves of the spelling under test, in one node.400
(let ((explicit (car callouts)))401
(assert-true (admonition? explicit))402
(assert-equal 'tip (admonition-kind explicit))403
(assert-equal "WARN: Explicit" (text-content (car (admonition-title explicit))))404
(assert-equal 1 (length (admonition-children explicit)))405
(assert-equal "tail" (text-content406
(car (paragraph-children407
(car (admonition-children explicit)))))))408
;; Callout 2: the SAME command with no `kind:` argument took the parameter409
;; list's default. Without the keyword machinery there is no default to410
;; take, so this is what distinguishes "keyword params work" from "the411
;; command happened to be callable". Guarded identically to callout 1.412
(let ((defaulted (cadr callouts)))413
(assert-true (admonition? defaulted))414
(assert-equal 'tip (admonition-kind defaulted))415
(assert-equal "note: Default" (text-content (car (admonition-title defaulted))))416
(assert-equal 1 (length (admonition-children defaulted)))417
(assert-equal "plain" (text-content418
(car (paragraph-children419
(car (admonition-children defaulted)))))))))))421
(test "a profile is validated where it is constructed"422
;; A profile admitting no root would reject every document with a message423
;; naming no command -- an always-red gate that looks like a manuscript424
;; error. Every one of these must fail at construction, not at parse time.425
(assert-error (make-document-profile "sigil/doc" '() 'one))426
(assert-error (make-document-profile "sigil/doc" '(chapter "note") 'one))427
(assert-error (make-document-profile "sigil/doc" '(chapter chapter) 'one))428
(assert-error (make-document-profile "sigil/doc" '(chapter) 'zero-or-more))429
(assert-error (make-document-profile "sigil/doc" '(chapter) 'many))430
(assert-error (make-document-profile "" '(chapter) 'one))431
(assert-error (make-document-profile 'sigil-doc '(chapter) 'one))432
(assert-error (make-document-profile "sigil/doc" 'chapter 'one))433
;; ...and a valid one is accepted, so the arm is not passing because the434
;; constructor rejects everything.435
(assert-true (document-profile? (make-document-profile "x/doc" '(a b) 'one-or-more)))436
;; A non-profile must not be accepted as one by the parse entry points.437
(assert-error (parse-document-parts-with-profile438
'not-a-profile "#lang sigil/doc\n@article[\"C\" \"A\"]{@p{b}}" "x.sgd" 16)))440
(test "a profile does not leak the aggregates it owns"441
;; The ownership rule the semantic model uses, applied to configuration: a442
;; caller must not be able to widen a profile's root set after construction,443
;; from either side of it.444
(let* ((roots (list 'note 'project))445
(profile (make-document-profile "folio/doc" roots 'one-or-more)))446
(set-car! roots 'chapter)447
(assert-equal '(note project) (document-profile-roots profile))448
(let ((handed-back (document-profile-roots profile)))449
(set-car! handed-back 'chapter)450
(assert-equal '(note project) (document-profile-roots profile)))))452
(test "@text and @expr are dispatched as author commands, not pseudo-kinds"453
;; The lowering half of SD-P4's recorded collision. The parser's internal454
;; prose and author-expression nodes have five slots; a COMMAND has six. Every455
;; classification below is by slot count, so an author's `@text{}`/`@expr{}`456
;; is never mistaken for one of the parser's own nodes -- which matters most457
;; at top level, where this phase's root allowlist is itself a head-symbol458
;; test. Measured before/after: test/sd-p1-classification-probe.sgl.459
;; `sigil-doc-profile` explicitly rather than the three-argument460
;; `parse-document-forms`: this suite imports only `(sigil doc reader)`, to461
;; prove a delegating language needs no private module. The three-argument462
;; spellings are unchanged and are covered by `test/test-reader.sgl`.463
(define (evaluate source name)464
(let loop ((forms (parse-document-forms-with-profile sigil-doc-profile source name 16))465
(last #f))466
(if (null? forms) last (loop (cdr forms) (eval (car forms))))))467
;; A manuscript that defines `expr` gets ITS command. Before this phase the468
;; lowering path spliced the node's raw internal child list into generated469
;; syntax instead of calling anything.470
(let* ((source (string-append471
"#lang sigil/doc\n"472
"@(define (expr . body) (apply em body))\n"473
"@article[\"Collision\" \"A\"]{@p{before @expr{inside} after}}"))474
(children (paragraph-children (car (document-children (evaluate source "x.sgd"))))))475
(assert-equal 3 (length children))476
(assert-true (emphasis? (cadr children)))477
(assert-equal "inside" (text-content (car (emphasis-children (cadr children))))))478
;; `@text{}` at top level is a COMMAND, so it reaches the root policy and is479
;; refused by name. Before this phase it reached the empty-prose filter, where480
;; `caddr` of a command node is its child LIST and `trim` was handed a list.481
(assert-equal482
"sigil/doc: top-level author forms must precede exactly one @article"483
(message (lambda () (parse-document-parts-with-profile484
sigil-doc-profile "#lang sigil/doc\n@text{a text command}\n"485
"t.sgd" 16))))486
;; `trim-body-edges` is the subtle site: it does not merely misread a487
;; `@text{}` command, it REBUILDS it as a five-slot prose node carrying the488
;; folded source slice, so everything downstream then behaves correctly on the489
;; wrong node. At a body edge the command must survive as a command.490
(let ((message-at-edge491
(message (lambda () (evaluate492
"#lang sigil/doc\n@article[\"E\" \"A\"]{\n@text{at the edge}\n}"493
"e.sgd")))))494
(assert-true (not (contains? message-at-edge "NO ERROR")))495
;; Dispatched to the author `text` command, which wants a string and was496
;; handed a node -- NOT silently rendered as a paragraph of literal source.497
(assert-true (contains? message-at-edge "make-text")))498
;; `inline-code-content` concatenates `caddr` of each child; for a command499
;; that is the child LIST, so a head-symbol test hands `string-append` a list.500
(assert-true501
(contains? (message (lambda () (evaluate502
"#lang sigil/doc\n@article[\"C\" \"A\"]{@p{@code{@text{x}}}}"503
"c.sgd")))504
"inline @code content must be text and document escapes"))505
;; POSITIVE CONTROLS -- the genuine five-slot pseudo-nodes must be unaffected.506
;; Bare prose is still folded into paragraphs...507
(let ((doc (evaluate "#lang sigil/doc\n@article[\"P\" \"A\"]{\nbare prose\n}" "p.sgd")))508
(assert-true (paragraph? (car (document-children doc))))509
(assert-equal "bare prose" (text-content (car (paragraph-children (car (document-children doc)))))))510
;; ...and a real top-level `@(...)` is still hoisted as an author form.511
(let ((parts (parse-document-parts-with-profile512
sigil-doc-profile513
"#lang sigil/doc\n@(define z 1)\n@article[\"Z\" \"A\"]{@p{b}}" "z.sgd" 16)))514
(assert-equal 1 (length (car parts)))515
(assert-equal 1 (length (cadr parts)))))517
(test "the delegating reader emits a usable form sequence for both multiplicities"518
(define (context name) (list (cons source-name: name) (cons header-end-offset: 16)))519
;; `one` produces exactly the shape `#lang sigil/doc` has always produced:520
;; prelude import, author forms, `(define document <root>)`, `document`.521
;; NOTE ON SHAPE: each emitted FORM is a syntax object, but its datum is an522
;; ordinary list whose head is a bare symbol -- `(define document <root>)`,523
;; where only `<root>` is itself syntax. So `syntax-datum` applies to the form,524
;; and plain list accessors apply inside it.525
(let* ((forms (read-language-source526
"#lang sigil/doc\n@article[\"C\" \"A\"]{@p{b}}" (context "c.sgd")))527
(tail (syntax-datum (car (reverse forms))))528
(definition (syntax-datum (cadr (reverse forms)))))529
(assert-equal 3 (length forms))530
(assert-equal 'document tail)531
(assert-equal 'define (car definition))532
(assert-equal 'document (cadr definition)))533
;; `one-or-more` binds the list instead, in manuscript order.534
(let* ((forms (read-language-source-with-profile535
folio-profile folio-source (context folio-path)))536
(tail (syntax-datum (car (reverse forms))))537
(definition (syntax-datum (cadr (reverse forms)))))538
(assert-equal 'document-roots tail)539
(assert-equal 'define (car definition))540
(assert-equal 'document-roots (cadr definition))541
;; `(list <root> <root>)` -- the `list` head plus two roots, in order.542
(assert-equal 'list (car (caddr definition)))543
(assert-equal 3 (length (caddr definition)))))545
(test "a root-policy error still precedes any lowering error"546
;; The pre-SD-P1 loop decided "is this the last node?" BEFORE lowering the547
;; chapter, so a policy failure always won over a failure inside the root's548
;; body. A single-pass rewrite that lowers each root as it meets it inverts549
;; that silently, and only a doubly-malformed manuscript can tell. This pins550
;; the precedence so the two-pass structure cannot be "simplified" away.551
(let* ((bad-code "@code[\"a\" \"b\"]{x}")552
(both (string-append "#lang sigil/doc\n@article[\"C\" \"A\"]{\n"553
bad-code "\n}\ntrailing prose\n"))554
(only-bad-code (string-append "#lang sigil/doc\n@article[\"C\" \"A\"]{\n"555
bad-code "\n}\n"))556
(both-message (message (lambda () (parse-document-parts-with-profile557
sigil-doc-profile both "b.sgd" 16))))558
(code-message (message (lambda () (parse-document-parts-with-profile559
sigil-doc-profile only-bad-code "o.sgd" 16)))))560
;; POSITIVE CONTROL FIRST: the body really is malformed, and lowering really561
;; does report it when nothing else is wrong. Without this, the assertion562
;; below would hold just as well over a body that lowers cleanly.563
(assert-true (contains? code-message "invalid arguments for @code"))564
;; ...and with a policy violation ALSO present, the policy error is the one565
;; reported -- the manuscript is never partially lowered.566
(assert-equal567
"sigil/doc: top-level author forms must precede exactly one @article"568
both-message)569
(assert-true (not (contains? both-message "invalid arguments for @code")))))571
(test "the suite checked a non-empty set of roots"572
;; A denominator, printed into the gate transcript. Every location assertion573
;; above lives inside `check-root`; if that walk ever collapsed to nothing,574
;; the arms would pass in silence.575
(display "SD_P1_ROOTS_CHECKED ") (display roots-checked) (newline)576
(assert-true (>= roots-checked 2)))