@aftertext/compiler 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +68 -2
  3. package/dist/ast/expression.d.ts +55 -0
  4. package/dist/ast/expression.d.ts.map +1 -0
  5. package/dist/ast/expression.js +2 -0
  6. package/dist/ast/expression.js.map +1 -0
  7. package/dist/ast/index.d.ts +6 -0
  8. package/dist/ast/index.d.ts.map +1 -0
  9. package/dist/ast/index.js +2 -0
  10. package/dist/ast/index.js.map +1 -0
  11. package/dist/ast/inline.d.ts +39 -0
  12. package/dist/ast/inline.d.ts.map +1 -0
  13. package/dist/ast/inline.js +2 -0
  14. package/dist/ast/inline.js.map +1 -0
  15. package/dist/ast/presentation.d.ts +41 -0
  16. package/dist/ast/presentation.d.ts.map +1 -0
  17. package/dist/ast/presentation.js +8 -0
  18. package/dist/ast/presentation.js.map +1 -0
  19. package/dist/ast/span.d.ts +19 -0
  20. package/dist/ast/span.d.ts.map +1 -0
  21. package/dist/ast/span.js +2 -0
  22. package/dist/ast/span.js.map +1 -0
  23. package/dist/ast/story.d.ts +354 -0
  24. package/dist/ast/story.d.ts.map +1 -0
  25. package/dist/ast/story.js +2 -0
  26. package/dist/ast/story.js.map +1 -0
  27. package/dist/compile.d.ts +25 -0
  28. package/dist/compile.d.ts.map +1 -0
  29. package/dist/compile.js +19 -0
  30. package/dist/compile.js.map +1 -0
  31. package/dist/diagnostics/codes.d.ts +26 -0
  32. package/dist/diagnostics/codes.d.ts.map +1 -0
  33. package/dist/diagnostics/codes.js +69 -0
  34. package/dist/diagnostics/codes.js.map +1 -0
  35. package/dist/diagnostics/index.d.ts +3 -0
  36. package/dist/diagnostics/index.d.ts.map +1 -0
  37. package/dist/diagnostics/index.js +2 -0
  38. package/dist/diagnostics/index.js.map +1 -0
  39. package/dist/diagnostics/types.d.ts +19 -0
  40. package/dist/diagnostics/types.d.ts.map +1 -0
  41. package/dist/diagnostics/types.js +2 -0
  42. package/dist/diagnostics/types.js.map +1 -0
  43. package/dist/index.d.ts +5 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +11 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/parser/blocks.d.ts +40 -0
  48. package/dist/parser/blocks.d.ts.map +1 -0
  49. package/dist/parser/blocks.js +927 -0
  50. package/dist/parser/blocks.js.map +1 -0
  51. package/dist/parser/directive-line.d.ts +21 -0
  52. package/dist/parser/directive-line.d.ts.map +1 -0
  53. package/dist/parser/directive-line.js +50 -0
  54. package/dist/parser/directive-line.js.map +1 -0
  55. package/dist/parser/document.d.ts +13 -0
  56. package/dist/parser/document.d.ts.map +1 -0
  57. package/dist/parser/document.js +28 -0
  58. package/dist/parser/document.js.map +1 -0
  59. package/dist/parser/expression-lexer.d.ts +44 -0
  60. package/dist/parser/expression-lexer.d.ts.map +1 -0
  61. package/dist/parser/expression-lexer.js +166 -0
  62. package/dist/parser/expression-lexer.js.map +1 -0
  63. package/dist/parser/expression-parser.d.ts +13 -0
  64. package/dist/parser/expression-parser.d.ts.map +1 -0
  65. package/dist/parser/expression-parser.js +201 -0
  66. package/dist/parser/expression-parser.js.map +1 -0
  67. package/dist/parser/frontmatter.d.ts +23 -0
  68. package/dist/parser/frontmatter.d.ts.map +1 -0
  69. package/dist/parser/frontmatter.js +158 -0
  70. package/dist/parser/frontmatter.js.map +1 -0
  71. package/dist/parser/index.d.ts +2 -0
  72. package/dist/parser/index.d.ts.map +1 -0
  73. package/dist/parser/index.js +2 -0
  74. package/dist/parser/index.js.map +1 -0
  75. package/dist/parser/lexical-primitives.d.ts +38 -0
  76. package/dist/parser/lexical-primitives.d.ts.map +1 -0
  77. package/dist/parser/lexical-primitives.js +46 -0
  78. package/dist/parser/lexical-primitives.js.map +1 -0
  79. package/dist/parser/lines.d.ts +32 -0
  80. package/dist/parser/lines.d.ts.map +1 -0
  81. package/dist/parser/lines.js +60 -0
  82. package/dist/parser/lines.js.map +1 -0
  83. package/dist/parser/markdown.d.ts +19 -0
  84. package/dist/parser/markdown.d.ts.map +1 -0
  85. package/dist/parser/markdown.js +221 -0
  86. package/dist/parser/markdown.js.map +1 -0
  87. package/dist/parser/serializer-safety.d.ts +57 -0
  88. package/dist/parser/serializer-safety.d.ts.map +1 -0
  89. package/dist/parser/serializer-safety.js +66 -0
  90. package/dist/parser/serializer-safety.js.map +1 -0
  91. package/dist/validation/index.d.ts +2 -0
  92. package/dist/validation/index.d.ts.map +1 -0
  93. package/dist/validation/index.js +2 -0
  94. package/dist/validation/index.js.map +1 -0
  95. package/dist/validation/validate.d.ts +4 -0
  96. package/dist/validation/validate.d.ts.map +1 -0
  97. package/dist/validation/validate.js +221 -0
  98. package/dist/validation/validate.js.map +1 -0
  99. package/package.json +43 -4
@@ -0,0 +1,221 @@
1
+ import { unified } from "unified";
2
+ import remarkParse from "remark-parse";
3
+ import { toString as mdastToString } from "mdast-util-to-string";
4
+ import { Diagnostics } from "../diagnostics/codes.js";
5
+ import { SEPARATOR_WHITESPACE_PATTERN } from "./lexical-primitives.js";
6
+ /**
7
+ * Bridges remark's mdast into AfterText-owned AST types. mdast/unified
8
+ * types are used only inside this module — nothing from `mdast` or
9
+ * `unified` is re-exported from the compiler's public API. This is the
10
+ * one place allowed to "leak" parser-library concepts, and it does so by
11
+ * translation, not by re-export.
12
+ */
13
+ const markdownProcessor = unified().use(remarkParse);
14
+ function remapPosition(pos, anchor) {
15
+ return {
16
+ line: anchor.startLine + (pos.line - 1),
17
+ column: pos.column,
18
+ offset: anchor.startOffset + pos.offset
19
+ };
20
+ }
21
+ function remapSpan(position, anchor) {
22
+ if (!position) {
23
+ const zero = { line: anchor.startLine, column: 1, offset: anchor.startOffset };
24
+ return { start: zero, end: zero };
25
+ }
26
+ return {
27
+ start: remapPosition({ line: position.start.line, column: position.start.column, offset: position.start.offset ?? 0 }, anchor),
28
+ end: remapPosition({ line: position.end.line, column: position.end.column, offset: position.end.offset ?? 0 }, anchor)
29
+ };
30
+ }
31
+ function convertInlineChildren(children, anchor, diagnostics) {
32
+ const result = [];
33
+ for (const child of children) {
34
+ const node = convertInline(child, anchor, diagnostics);
35
+ if (node)
36
+ result.push(node);
37
+ }
38
+ return result;
39
+ }
40
+ function convertInline(node, anchor, diagnostics) {
41
+ const span = remapSpan(node.position, anchor);
42
+ switch (node.type) {
43
+ case "text":
44
+ return { type: "Text", value: node.value, span };
45
+ case "emphasis":
46
+ return { type: "Emphasis", children: convertInlineChildren(node.children, anchor, diagnostics), span };
47
+ case "strong":
48
+ return { type: "Strong", children: convertInlineChildren(node.children, anchor, diagnostics), span };
49
+ case "inlineCode":
50
+ return { type: "InlineCode", value: node.value, span };
51
+ case "link":
52
+ return {
53
+ type: "Link",
54
+ url: node.url,
55
+ children: convertInlineChildren(node.children, anchor, diagnostics),
56
+ span
57
+ };
58
+ case "break":
59
+ return { type: "LineBreak", span };
60
+ default: {
61
+ // Fallback for inline constructs outside v0.1's supported subset
62
+ // (images, footnotes, raw html, ...): flatten to plain text rather
63
+ // than reimplementing the rest of Markdown's inline grammar, but
64
+ // warn so this doesn't silently change document meaning.
65
+ diagnostics.push(Diagnostics.unsupportedMarkdown(node.type, span));
66
+ const text = mdastToString(node);
67
+ return text.length > 0 ? { type: "Text", value: text, span } : undefined;
68
+ }
69
+ }
70
+ }
71
+ /**
72
+ * Advances `position` forward by `count` characters on the same source
73
+ * line. Safe only when the caller knows the scan cannot cross a line
74
+ * ending — true for an ATX Heading's own opening-marker/separator region,
75
+ * which is always confined to that Heading's single physical line.
76
+ */
77
+ function advancePositionOnLine(position, count) {
78
+ return { line: position.line, column: position.column + count, offset: position.offset + count };
79
+ }
80
+ /**
81
+ * Computes `HeadingNode.contentSourceSpan` for a Heading with zero inline
82
+ * children (an empty authored ATX Heading — Setext Headings cannot have
83
+ * empty content, see docs/CORE_SPEC.md Section 25.49). Scans forward
84
+ * from immediately after the opening `#` marker run, consuming contiguous
85
+ * separator whitespace, bounded by `span.end.offset` — which already
86
+ * excludes the line ending but includes any trailing whitespace and any
87
+ * recognized ATX closing-hash run. Stopping at the first non-whitespace
88
+ * character (or at `span.end.offset`) therefore lands exactly before a
89
+ * closing-hash run when one is present, or at the end of trailing
90
+ * whitespace when one is not — in both cases the correct zero-width
91
+ * position, derived purely from already-computed facts (never a second
92
+ * Markdown parser).
93
+ */
94
+ function computeEmptyHeadingContentSpan(depth, span, chunkText, anchor) {
95
+ const markerEnd = advancePositionOnLine(span.start, depth);
96
+ let offset = markerEnd.offset;
97
+ const upperBound = span.end.offset;
98
+ while (offset < upperBound && SEPARATOR_WHITESPACE_PATTERN.test(chunkText[offset - anchor.startOffset])) {
99
+ offset++;
100
+ }
101
+ const position = advancePositionOnLine(markerEnd, offset - markerEnd.offset);
102
+ return { start: position, end: position };
103
+ }
104
+ /**
105
+ * Computes `HeadingNode.contentSourceSpan` for a Heading with at least one
106
+ * inline child: the first child's span start through the last child's span
107
+ * end already excludes the opening marker/separator, any recognized ATX
108
+ * closing-hash run, trailing Heading whitespace, the Setext underline, and
109
+ * the line ending — confirmed sufficient by investigation, zero additional
110
+ * computation needed.
111
+ */
112
+ function computeNonemptyHeadingContentSpan(children) {
113
+ const first = children[0];
114
+ const last = children[children.length - 1];
115
+ return { start: first.span.start, end: last.span.end };
116
+ }
117
+ /**
118
+ * Computes `HeadingNode.levelSourceSpan` (docs/CORE_SPEC.md Section
119
+ * 25.50). ATX and Setext style is distinguished purely from already-
120
+ * published `span` facts — `span.start.line === span.end.line` for ATX
121
+ * (a single physical line) vs. Setext (always two-or-more physical lines,
122
+ * content plus underline) — an architectural source fact, not a heuristic,
123
+ * confirmed by investigation with no counterexample found.
124
+ *
125
+ * ATX: the opening marker run is exactly `[span.start.offset,
126
+ * span.start.offset + depth)` — the same fact `computeEmptyHeadingContentSpan`
127
+ * already uses to find the marker's end.
128
+ *
129
+ * Setext: the underline character run sits on the Heading's own final
130
+ * physical line, which `span.end` already terminates (excluding the line
131
+ * ending but including underline trailing whitespace). Scanning backward
132
+ * from `span.end.offset` — first past trailing separator whitespace, then
133
+ * past the homogeneous run of the underline character itself — locates the
134
+ * run's exact boundaries. Both scans are bounded within `chunkText`, which
135
+ * this module already holds for the current prose chunk; this is the same
136
+ * bounded-scan technique `computeEmptyHeadingContentSpan` already uses, not
137
+ * a second Markdown parser. Safe because every character involved (marker
138
+ * indentation, the underline run, and trailing whitespace) is ASCII and
139
+ * confined to one physical line, so offset and column move in lockstep.
140
+ */
141
+ function computeHeadingLevelSpan(span, depth, chunkText, anchor) {
142
+ if (span.start.line === span.end.line) {
143
+ return { start: span.start, end: advancePositionOnLine(span.start, depth) };
144
+ }
145
+ const charAt = (offset) => chunkText[offset - anchor.startOffset];
146
+ let endOffset = span.end.offset;
147
+ while (endOffset > span.start.offset && SEPARATOR_WHITESPACE_PATTERN.test(charAt(endOffset - 1))) {
148
+ endOffset--;
149
+ }
150
+ const markerChar = charAt(endOffset - 1);
151
+ let startOffset = endOffset;
152
+ while (startOffset > span.start.offset && charAt(startOffset - 1) === markerChar) {
153
+ startOffset--;
154
+ }
155
+ const end = {
156
+ line: span.end.line,
157
+ column: span.end.column - (span.end.offset - endOffset),
158
+ offset: endOffset
159
+ };
160
+ const start = {
161
+ line: span.end.line,
162
+ column: end.column - (endOffset - startOffset),
163
+ offset: startOffset
164
+ };
165
+ return { start, end };
166
+ }
167
+ function convertBlock(node, anchor, diagnostics, chunkText) {
168
+ const span = remapSpan(node.position, anchor);
169
+ if (node.type === "paragraph") {
170
+ const paragraph = node;
171
+ const result = {
172
+ type: "Paragraph",
173
+ children: convertInlineChildren(paragraph.children, anchor, diagnostics),
174
+ span
175
+ };
176
+ return result;
177
+ }
178
+ if (node.type === "heading") {
179
+ const heading = node;
180
+ const children = convertInlineChildren(heading.children, anchor, diagnostics);
181
+ const contentSourceSpan = children.length > 0
182
+ ? computeNonemptyHeadingContentSpan(children)
183
+ : computeEmptyHeadingContentSpan(heading.depth, span, chunkText, anchor);
184
+ const levelSourceSpan = computeHeadingLevelSpan(span, heading.depth, chunkText, anchor);
185
+ const result = {
186
+ type: "Heading",
187
+ depth: heading.depth,
188
+ children,
189
+ span,
190
+ contentSourceSpan,
191
+ levelSourceSpan
192
+ };
193
+ return result;
194
+ }
195
+ // Fallback for block constructs outside v0.1's supported subset (lists,
196
+ // blockquotes, code fences, tables, ...): rendered as a plain paragraph
197
+ // of their flattened text so no source content is silently dropped, but
198
+ // warn so this doesn't silently change document meaning.
199
+ diagnostics.push(Diagnostics.unsupportedMarkdown(node.type, span));
200
+ const text = mdastToString(node);
201
+ const fallback = {
202
+ type: "Paragraph",
203
+ children: text.length > 0 ? [{ type: "Text", value: text, span }] : [],
204
+ span
205
+ };
206
+ return fallback;
207
+ }
208
+ /**
209
+ * Parses a contiguous run of prose source text (already known to contain no
210
+ * AfterText directives) as Markdown, converting the result into
211
+ * AfterText-owned StoryBlock nodes anchored back to the full document via
212
+ * `anchor`. Constructs outside v0.1's supported subset (lists, blockquotes,
213
+ * images, code blocks, ...) still fall back to flattened plain text, but
214
+ * emit an AT3001 warning rather than changing document meaning silently.
215
+ */
216
+ export function parseProseChunk(text, anchor, diagnostics) {
217
+ const root = markdownProcessor.parse(text);
218
+ const parent = root;
219
+ return parent.children.map((child) => convertBlock(child, anchor, diagnostics, text));
220
+ }
221
+ //# sourceMappingURL=markdown.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"markdown.js","sourceRoot":"","sources":["../../src/parser/markdown.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,WAAW,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,QAAQ,IAAI,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAYjE,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,EAAE,4BAA4B,EAAE,MAAM,yBAAyB,CAAC;AAEvE;;;;;;GAMG;AACH,MAAM,iBAAiB,GAAG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,CAAC"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Serializer-boundary-safety predicates (docs/CORE_SPEC.md Section 25.40,
3
+ * "Source-Generation Safety Helpers") — small, additive, compiler-owned
4
+ * public helpers for tools that generate AfterText source (for example
5
+ * structured source editors).
6
+ *
7
+ * These are NOT content validators. They answer a narrower, purely lexical
8
+ * question: can this raw field, once interpolated into generated DSL
9
+ * source, be reinterpreted by the compiler's own tokenizer as additional
10
+ * structure the caller never intended — silently, with zero diagnostic?
11
+ * Content validity (is this a real identifier / expression / duration?)
12
+ * remains entirely the parser's job, via its existing diagnostics
13
+ * (`AT1005`, `AT2001`, unknown-reference, etc.), unaffected by either
14
+ * predicate here.
15
+ *
16
+ * Both predicates reuse the exact lexical facts (`SEPARATOR_WHITESPACE_PATTERN`,
17
+ * `findLastUnescapedArrow`) the actual parser (`blocks.ts`) uses, from the
18
+ * shared, dependency-free `lexical-primitives.ts` module — so this file and
19
+ * the parser's own tokenization cannot silently drift apart. This module
20
+ * itself has no `remark`/`mdast` dependency, so it is safe to re-export
21
+ * from the public compiler surface.
22
+ */
23
+ /**
24
+ * Conservative gate for a Choice item's `target` field (Section 25.40).
25
+ * Returns `false` whenever `value` contains compiler-recognized separator
26
+ * whitespace, or an unescaped `->` — either of which the Choice parser's own
27
+ * tail grammar (`CHOICE_TAIL_PATTERN`) and last-unescaped-arrow item
28
+ * delimiter (`findLastUnescapedArrow`) could reinterpret as additional
29
+ * structure (an implicit `if <condition>`, or a re-split item/target
30
+ * boundary) rather than the single unconditional target the caller
31
+ * intended.
32
+ *
33
+ * Intentionally conservative, not a complete target validator: a
34
+ * boundary-safe value can still be content-invalid (e.g. `"123"`, which
35
+ * fails `CHOICE_TAIL_PATTERN`'s identifier shape) — that remains the
36
+ * parser's own job via `AT1005`, undisturbed by this predicate. No
37
+ * syntactically valid *unconditional* Choice target ever contains separator
38
+ * whitespace, so this gate never rejects a value that could have been one.
39
+ */
40
+ export declare function isChoiceTargetBoundarySafe(value: string): boolean;
41
+ /**
42
+ * Conservative gate for a Presentation named-argument value (`Music.volume`,
43
+ * `Camera.to`, `Camera.duration`, Section 25.40). Presentation's own
44
+ * argument tokenizer (`tokenizePresentationArgs`) splits `argsText` into
45
+ * tokens on this exact separator-whitespace definition, and reclassifies
46
+ * any resulting `key=value`-shaped token as a distinct named parameter —
47
+ * so a raw value containing separator whitespace could silently inject an
48
+ * additional parameter (or truncate the intended one) rather than remaining
49
+ * one atomic named-argument value.
50
+ *
51
+ * Intentionally conservative, not a numeric/duration validator: a
52
+ * boundary-safe value can still be content-invalid (e.g. `"bananas"` for a
53
+ * volume) — that remains the parser's own job via its existing diagnostics,
54
+ * undisturbed by this predicate.
55
+ */
56
+ export declare function isPresentationNamedArgumentValueBoundarySafe(value: string): boolean;
57
+ //# sourceMappingURL=serializer-safety.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serializer-safety.d.ts","sourceRoot":"","sources":["../../src/parser/serializer-safety.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAIH;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAIjE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,4CAA4C,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAEnF"}
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Serializer-boundary-safety predicates (docs/CORE_SPEC.md Section 25.40,
3
+ * "Source-Generation Safety Helpers") — small, additive, compiler-owned
4
+ * public helpers for tools that generate AfterText source (for example
5
+ * structured source editors).
6
+ *
7
+ * These are NOT content validators. They answer a narrower, purely lexical
8
+ * question: can this raw field, once interpolated into generated DSL
9
+ * source, be reinterpreted by the compiler's own tokenizer as additional
10
+ * structure the caller never intended — silently, with zero diagnostic?
11
+ * Content validity (is this a real identifier / expression / duration?)
12
+ * remains entirely the parser's job, via its existing diagnostics
13
+ * (`AT1005`, `AT2001`, unknown-reference, etc.), unaffected by either
14
+ * predicate here.
15
+ *
16
+ * Both predicates reuse the exact lexical facts (`SEPARATOR_WHITESPACE_PATTERN`,
17
+ * `findLastUnescapedArrow`) the actual parser (`blocks.ts`) uses, from the
18
+ * shared, dependency-free `lexical-primitives.ts` module — so this file and
19
+ * the parser's own tokenization cannot silently drift apart. This module
20
+ * itself has no `remark`/`mdast` dependency, so it is safe to re-export
21
+ * from the public compiler surface.
22
+ */
23
+ import { findLastUnescapedArrow, SEPARATOR_WHITESPACE_PATTERN } from "./lexical-primitives.js";
24
+ /**
25
+ * Conservative gate for a Choice item's `target` field (Section 25.40).
26
+ * Returns `false` whenever `value` contains compiler-recognized separator
27
+ * whitespace, or an unescaped `->` — either of which the Choice parser's own
28
+ * tail grammar (`CHOICE_TAIL_PATTERN`) and last-unescaped-arrow item
29
+ * delimiter (`findLastUnescapedArrow`) could reinterpret as additional
30
+ * structure (an implicit `if <condition>`, or a re-split item/target
31
+ * boundary) rather than the single unconditional target the caller
32
+ * intended.
33
+ *
34
+ * Intentionally conservative, not a complete target validator: a
35
+ * boundary-safe value can still be content-invalid (e.g. `"123"`, which
36
+ * fails `CHOICE_TAIL_PATTERN`'s identifier shape) — that remains the
37
+ * parser's own job via `AT1005`, undisturbed by this predicate. No
38
+ * syntactically valid *unconditional* Choice target ever contains separator
39
+ * whitespace, so this gate never rejects a value that could have been one.
40
+ */
41
+ export function isChoiceTargetBoundarySafe(value) {
42
+ if (SEPARATOR_WHITESPACE_PATTERN.test(value))
43
+ return false;
44
+ if (findLastUnescapedArrow(value) !== -1)
45
+ return false;
46
+ return true;
47
+ }
48
+ /**
49
+ * Conservative gate for a Presentation named-argument value (`Music.volume`,
50
+ * `Camera.to`, `Camera.duration`, Section 25.40). Presentation's own
51
+ * argument tokenizer (`tokenizePresentationArgs`) splits `argsText` into
52
+ * tokens on this exact separator-whitespace definition, and reclassifies
53
+ * any resulting `key=value`-shaped token as a distinct named parameter —
54
+ * so a raw value containing separator whitespace could silently inject an
55
+ * additional parameter (or truncate the intended one) rather than remaining
56
+ * one atomic named-argument value.
57
+ *
58
+ * Intentionally conservative, not a numeric/duration validator: a
59
+ * boundary-safe value can still be content-invalid (e.g. `"bananas"` for a
60
+ * volume) — that remains the parser's own job via its existing diagnostics,
61
+ * undisturbed by this predicate.
62
+ */
63
+ export function isPresentationNamedArgumentValueBoundarySafe(value) {
64
+ return !SEPARATOR_WHITESPACE_PATTERN.test(value);
65
+ }
66
+ //# sourceMappingURL=serializer-safety.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serializer-safety.js","sourceRoot":"","sources":["../../src/parser/serializer-safety.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,sBAAsB,EAAE,4BAA4B,EAAE,MAAM,yBAAyB,CAAC;AAE/F;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,0BAA0B,CAAC,KAAa;IACtD,IAAI,4BAA4B,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAC3D,IAAI,sBAAsB,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAAE,OAAO,KAAK,CAAC;IACvD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,4CAA4C,CAAC,KAAa;IACxE,OAAO,CAAC,4BAA4B,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACnD,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { validate } from "./validate.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/validation/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { validate } from "./validate.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/validation/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { StoryDocument } from "../ast/story.js";
2
+ import type { Diagnostic } from "../diagnostics/types.js";
3
+ export declare function validate(document: StoryDocument): Diagnostic[];
4
+ //# sourceMappingURL=validate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../src/validation/validate.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAyB,aAAa,EAAe,MAAM,iBAAiB,CAAC;AAEzF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAsH1D,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,aAAa,GAAG,UAAU,EAAE,CA8G9D"}
@@ -0,0 +1,221 @@
1
+ import { Diagnostics } from "../diagnostics/codes.js";
2
+ /**
3
+ * Reader Memory query builtins recognized by this compiler (docs/CORE_SPEC.md
4
+ * Section 17.15) — a fixed, non-extensible-by-authors registry. `kind`
5
+ * says which id-space (scene or Variant) the builtin's single string-literal
6
+ * argument must reference.
7
+ */
8
+ const READER_MEMORY_BUILTINS = {
9
+ visited: { kind: "scene" },
10
+ visit_count: { kind: "scene" },
11
+ seen_variant: { kind: "variant" },
12
+ last_seen_variant_branch: { kind: "variant" }
13
+ };
14
+ /** Depth-first walk of every `CallExpression` reachable from `expression`, including nested call arguments. */
15
+ function* walkCalls(expression) {
16
+ switch (expression.type) {
17
+ case "Literal":
18
+ case "Identifier":
19
+ return;
20
+ case "Unary":
21
+ yield* walkCalls(expression.argument);
22
+ return;
23
+ case "Binary":
24
+ yield* walkCalls(expression.left);
25
+ yield* walkCalls(expression.right);
26
+ return;
27
+ case "Call":
28
+ yield expression;
29
+ for (const arg of expression.args)
30
+ yield* walkCalls(arg);
31
+ return;
32
+ }
33
+ }
34
+ const DOCUMENT_START = { line: 1, column: 1, offset: 0 };
35
+ /**
36
+ * Best available anchor for a diagnostic about `document.entryScene`: the
37
+ * frontmatter block's span when the author wrote an explicit `entry`
38
+ * (there is nothing more specific to point at — see the AT1201/AT1202
39
+ * per-key spans for why a coarser anchor is acceptable here), otherwise a
40
+ * fallback position at the very start of the document.
41
+ */
42
+ function entrySceneSpan(document) {
43
+ if (document.metadata.entry !== undefined && document.metadata.span) {
44
+ return document.metadata.span;
45
+ }
46
+ return { start: DOCUMENT_START, end: DOCUMENT_START };
47
+ }
48
+ /** Depth-first walk over a block tree, descending into conditional/variant branch bodies. */
49
+ function* walkBlocks(blocks) {
50
+ for (const block of blocks) {
51
+ yield block;
52
+ if (block.type === "Conditional") {
53
+ for (const branch of block.branches) {
54
+ yield* walkBlocks(branch.blocks);
55
+ }
56
+ }
57
+ else if (block.type === "Variant") {
58
+ for (const branch of block.branches) {
59
+ yield* walkBlocks(branch.blocks);
60
+ }
61
+ }
62
+ }
63
+ }
64
+ /**
65
+ * Structural validation over a parsed StoryDocument: duplicate scenes,
66
+ * dangling scene references (from @goto, choice targets, and the
67
+ * normalized entry scene), variant id/branch requirements, and Reader
68
+ * Memory builtin calls (unknown callee, wrong arity, non-literal argument,
69
+ * unknown Scene/Variant reference — docs/CORE_SPEC.md Section 17.16).
70
+ * Directive- and block-shape errors (AT1001, AT1002, AT1201, AT1202,
71
+ * AT2001) are detected earlier, during parsing itself.
72
+ */
73
+ /**
74
+ * Validates every `CallExpression` reachable from `expression` — Reader
75
+ * Memory builtin name, arity, argument literal-ness, and Scene/Variant
76
+ * reference existence (docs/CORE_SPEC.md Section 17.16). An unknown
77
+ * callee is diagnosed on its own terms, never conflated with an unknown
78
+ * `StoryState` identifier (that remains a runtime concern, Section 17.8 —
79
+ * `StoryState` is open/dynamic; the builtin namespace is closed and fully
80
+ * known here).
81
+ */
82
+ function validateCalls(expression, scenesById, variantsById, diagnostics) {
83
+ for (const call of walkCalls(expression)) {
84
+ const builtin = READER_MEMORY_BUILTINS[call.callee];
85
+ if (!builtin) {
86
+ diagnostics.push(Diagnostics.unknownBuiltin(call.callee, call.span));
87
+ continue;
88
+ }
89
+ if (call.args.length !== 1) {
90
+ diagnostics.push(Diagnostics.wrongBuiltinArity(call.callee, 1, call.args.length, call.span));
91
+ continue;
92
+ }
93
+ const arg = call.args[0];
94
+ if (arg.type !== "Literal" || typeof arg.value !== "string") {
95
+ diagnostics.push(Diagnostics.invalidBuiltinArgument(call.callee, call.span));
96
+ continue;
97
+ }
98
+ if (builtin.kind === "scene") {
99
+ if (!scenesById.has(arg.value)) {
100
+ diagnostics.push(Diagnostics.unknownSceneReference(arg.value, call.span));
101
+ }
102
+ }
103
+ else {
104
+ if (!variantsById.has(arg.value)) {
105
+ diagnostics.push(Diagnostics.unknownVariantReference(arg.value, call.span));
106
+ }
107
+ }
108
+ }
109
+ }
110
+ export function validate(document) {
111
+ const diagnostics = [];
112
+ const scenesById = new Map();
113
+ for (const scene of document.scenes) {
114
+ if (scenesById.has(scene.id)) {
115
+ diagnostics.push(Diagnostics.duplicateScene(scene.id, scene.span));
116
+ }
117
+ else {
118
+ scenesById.set(scene.id, scene);
119
+ }
120
+ }
121
+ if (!scenesById.has(document.entryScene)) {
122
+ diagnostics.push(Diagnostics.unknownSceneReference(document.entryScene, entrySceneSpan(document)));
123
+ }
124
+ // Collected fully, before any reference is checked, so a Reader Memory
125
+ // query (or a future reference kind) may validly refer to a Variant
126
+ // declared later in the document — the same forward-reference guarantee
127
+ // `scenesById` above already provides for @goto/Choice targets.
128
+ const variantsById = new Map();
129
+ for (const scene of document.scenes) {
130
+ for (const block of walkBlocks(scene.blocks)) {
131
+ if (block.type === "Conditional") {
132
+ // "@else" matches unconditionally once Runtime's branch resolution
133
+ // reaches it (mirrors "@otherwise"'s identical semantics below), so
134
+ // any branch authored after it — whether another "@elseif" or a
135
+ // duplicate "@else" — is permanently unreachable. Reports every such
136
+ // branch, not just the first, matching duplicateScene's/
137
+ // variantBranchAfterOtherwise's exhaustive-not-first-only convention.
138
+ let seenElse = false;
139
+ for (const branch of block.branches) {
140
+ if (seenElse) {
141
+ diagnostics.push(Diagnostics.conditionalBranchAfterElse(branch.span));
142
+ }
143
+ if (branch.kind === "else") {
144
+ seenElse = true;
145
+ }
146
+ }
147
+ continue;
148
+ }
149
+ if (block.type !== "Variant")
150
+ continue;
151
+ if (variantsById.has(block.id)) {
152
+ diagnostics.push(Diagnostics.duplicateVariantId(block.id, block.span));
153
+ }
154
+ else {
155
+ variantsById.set(block.id, block);
156
+ }
157
+ const hasWhenBranch = block.branches.some((branch) => branch.kind === "when");
158
+ if (!hasWhenBranch) {
159
+ diagnostics.push(Diagnostics.variantWithoutWhenBranch(block.id, block.span));
160
+ }
161
+ // "@otherwise" matches unconditionally the instant Runtime's branch
162
+ // resolution reaches it (see resolveVariant), so any branch authored
163
+ // after it is permanently unreachable — this reports every such
164
+ // branch, not just the first, matching duplicateScene's
165
+ // exhaustive-not-first-only convention above.
166
+ let seenOtherwise = false;
167
+ for (const branch of block.branches) {
168
+ if (seenOtherwise) {
169
+ diagnostics.push(Diagnostics.variantBranchAfterOtherwise(block.id, branch.span));
170
+ }
171
+ if (branch.kind === "otherwise") {
172
+ seenOtherwise = true;
173
+ }
174
+ }
175
+ }
176
+ }
177
+ for (const scene of document.scenes) {
178
+ for (const block of walkBlocks(scene.blocks)) {
179
+ switch (block.type) {
180
+ case "Goto": {
181
+ if (!scenesById.has(block.target)) {
182
+ diagnostics.push(Diagnostics.unknownSceneReference(block.target, block.span));
183
+ }
184
+ break;
185
+ }
186
+ case "Choice": {
187
+ for (const item of block.items) {
188
+ if (!scenesById.has(item.target)) {
189
+ diagnostics.push(Diagnostics.unknownSceneReference(item.target, item.span));
190
+ }
191
+ if (item.condition)
192
+ validateCalls(item.condition, scenesById, variantsById, diagnostics);
193
+ }
194
+ break;
195
+ }
196
+ case "Set": {
197
+ validateCalls(block.expression, scenesById, variantsById, diagnostics);
198
+ break;
199
+ }
200
+ case "Conditional": {
201
+ for (const branch of block.branches) {
202
+ if (branch.condition)
203
+ validateCalls(branch.condition, scenesById, variantsById, diagnostics);
204
+ }
205
+ break;
206
+ }
207
+ case "Variant": {
208
+ for (const branch of block.branches) {
209
+ if (branch.condition)
210
+ validateCalls(branch.condition, scenesById, variantsById, diagnostics);
211
+ }
212
+ break;
213
+ }
214
+ default:
215
+ break;
216
+ }
217
+ }
218
+ }
219
+ return diagnostics;
220
+ }
221
+ //# sourceMappingURL=validate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate.js","sourceRoot":"","sources":["../../src/validation/validate.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAGtD;;;;;GAKG;AACH,MAAM,sBAAsB,GAAqE;IAC/F,OAAO,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE;IAC1B,WAAW,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE;IAC9B,YAAY,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;IACjC,wBAAwB,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;CAC9C,CAAC;AAEF,+GAA+G;AAC/G,QAAQ,CAAC,CAAC,SAAS,CAAC,UAA0B;IAC5C,QAAQ,UAAU,CAAC,IAAI,EAAE,CAAC;QACxB,KAAK,SAAS,CAAC;QACf,KAAK,YAAY;YACf,OAAO;QACT,KAAK,OAAO;YACV,KAAK,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;YACtC,OAAO;QACT,KAAK,QAAQ;YACX,KAAK,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;YAClC,KAAK,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;YACnC,OAAO;QACT,KAAK,MAAM;YACT,MAAM,UAAU,CAAC;YACjB,KAAK,MAAM,GAAG,IAAI,UAAU,CAAC,IAAI;gBAAE,KAAK,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;YACzD,OAAO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