@aftertext/compiler 0.0.0-stage → 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +38 -2
- package/dist/ast/expression.d.ts +55 -0
- package/dist/ast/expression.d.ts.map +1 -0
- package/dist/ast/expression.js +2 -0
- package/dist/ast/expression.js.map +1 -0
- package/dist/ast/index.d.ts +6 -0
- package/dist/ast/index.d.ts.map +1 -0
- package/dist/ast/index.js +2 -0
- package/dist/ast/index.js.map +1 -0
- package/dist/ast/inline.d.ts +39 -0
- package/dist/ast/inline.d.ts.map +1 -0
- package/dist/ast/inline.js +2 -0
- package/dist/ast/inline.js.map +1 -0
- package/dist/ast/presentation.d.ts +41 -0
- package/dist/ast/presentation.d.ts.map +1 -0
- package/dist/ast/presentation.js +8 -0
- package/dist/ast/presentation.js.map +1 -0
- package/dist/ast/span.d.ts +19 -0
- package/dist/ast/span.d.ts.map +1 -0
- package/dist/ast/span.js +2 -0
- package/dist/ast/span.js.map +1 -0
- package/dist/ast/story.d.ts +350 -0
- package/dist/ast/story.d.ts.map +1 -0
- package/dist/ast/story.js +2 -0
- package/dist/ast/story.js.map +1 -0
- package/dist/compile.d.ts +25 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +19 -0
- package/dist/compile.js.map +1 -0
- package/dist/diagnostics/codes.d.ts +25 -0
- package/dist/diagnostics/codes.d.ts.map +1 -0
- package/dist/diagnostics/codes.js +66 -0
- package/dist/diagnostics/codes.js.map +1 -0
- package/dist/diagnostics/index.d.ts +3 -0
- package/dist/diagnostics/index.d.ts.map +1 -0
- package/dist/diagnostics/index.js +2 -0
- package/dist/diagnostics/index.js.map +1 -0
- package/dist/diagnostics/types.d.ts +19 -0
- package/dist/diagnostics/types.d.ts.map +1 -0
- package/dist/diagnostics/types.js +2 -0
- package/dist/diagnostics/types.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/parser/blocks.d.ts +40 -0
- package/dist/parser/blocks.d.ts.map +1 -0
- package/dist/parser/blocks.js +907 -0
- package/dist/parser/blocks.js.map +1 -0
- package/dist/parser/directive-line.d.ts +21 -0
- package/dist/parser/directive-line.d.ts.map +1 -0
- package/dist/parser/directive-line.js +50 -0
- package/dist/parser/directive-line.js.map +1 -0
- package/dist/parser/document.d.ts +13 -0
- package/dist/parser/document.d.ts.map +1 -0
- package/dist/parser/document.js +28 -0
- package/dist/parser/document.js.map +1 -0
- package/dist/parser/expression-lexer.d.ts +26 -0
- package/dist/parser/expression-lexer.d.ts.map +1 -0
- package/dist/parser/expression-lexer.js +137 -0
- package/dist/parser/expression-lexer.js.map +1 -0
- package/dist/parser/expression-parser.d.ts +13 -0
- package/dist/parser/expression-parser.d.ts.map +1 -0
- package/dist/parser/expression-parser.js +201 -0
- package/dist/parser/expression-parser.js.map +1 -0
- package/dist/parser/frontmatter.d.ts +23 -0
- package/dist/parser/frontmatter.d.ts.map +1 -0
- package/dist/parser/frontmatter.js +118 -0
- package/dist/parser/frontmatter.js.map +1 -0
- package/dist/parser/index.d.ts +2 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +2 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/lexical-primitives.d.ts +38 -0
- package/dist/parser/lexical-primitives.d.ts.map +1 -0
- package/dist/parser/lexical-primitives.js +46 -0
- package/dist/parser/lexical-primitives.js.map +1 -0
- package/dist/parser/lines.d.ts +32 -0
- package/dist/parser/lines.d.ts.map +1 -0
- package/dist/parser/lines.js +60 -0
- package/dist/parser/lines.js.map +1 -0
- package/dist/parser/markdown.d.ts +19 -0
- package/dist/parser/markdown.d.ts.map +1 -0
- package/dist/parser/markdown.js +221 -0
- package/dist/parser/markdown.js.map +1 -0
- package/dist/parser/serializer-safety.d.ts +56 -0
- package/dist/parser/serializer-safety.d.ts.map +1 -0
- package/dist/parser/serializer-safety.js +65 -0
- package/dist/parser/serializer-safety.js.map +1 -0
- package/dist/validation/index.d.ts +2 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +2 -0
- package/dist/validation/index.js.map +1 -0
- package/dist/validation/validate.d.ts +4 -0
- package/dist/validation/validate.d.ts.map +1 -0
- package/dist/validation/validate.js +221 -0
- package/dist/validation/validate.js.map +1 -0
- package/package.json +43 -4
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serializer-boundary-safety predicates (docs/PROJECT_SPEC.md Section 25.40,
|
|
3
|
+
* "Serializer Injection Safety Amendment") — small, additive, compiler-owned
|
|
4
|
+
* public helpers for the Editor's whole-`StoryBlock` insertion feature.
|
|
5
|
+
*
|
|
6
|
+
* These are NOT content validators. They answer a narrower, purely lexical
|
|
7
|
+
* question: can this raw field, once interpolated into generated DSL
|
|
8
|
+
* source, be reinterpreted by the compiler's own tokenizer as additional
|
|
9
|
+
* structure the caller never intended — silently, with zero diagnostic?
|
|
10
|
+
* Content validity (is this a real identifier / expression / duration?)
|
|
11
|
+
* remains entirely the parser's job, via its existing diagnostics
|
|
12
|
+
* (`AT1005`, `AT2001`, unknown-reference, etc.), unaffected by either
|
|
13
|
+
* predicate here.
|
|
14
|
+
*
|
|
15
|
+
* Both predicates reuse the exact lexical facts (`SEPARATOR_WHITESPACE_PATTERN`,
|
|
16
|
+
* `findLastUnescapedArrow`) the actual parser (`blocks.ts`) uses, from the
|
|
17
|
+
* shared, dependency-free `lexical-primitives.ts` module — so this file and
|
|
18
|
+
* the parser's own tokenization cannot silently drift apart. This module
|
|
19
|
+
* itself has no `remark`/`mdast` dependency, so it is safe to re-export
|
|
20
|
+
* from the public compiler surface.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Conservative gate for a Choice item's `target` field (Section 25.40).
|
|
24
|
+
* Returns `false` whenever `value` contains compiler-recognized separator
|
|
25
|
+
* whitespace, or an unescaped `->` — either of which the Choice parser's own
|
|
26
|
+
* tail grammar (`CHOICE_TAIL_PATTERN`) and last-unescaped-arrow item
|
|
27
|
+
* delimiter (`findLastUnescapedArrow`) could reinterpret as additional
|
|
28
|
+
* structure (an implicit `if <condition>`, or a re-split item/target
|
|
29
|
+
* boundary) rather than the single unconditional target the caller
|
|
30
|
+
* intended.
|
|
31
|
+
*
|
|
32
|
+
* Intentionally conservative, not a complete target validator: a
|
|
33
|
+
* boundary-safe value can still be content-invalid (e.g. `"123"`, which
|
|
34
|
+
* fails `CHOICE_TAIL_PATTERN`'s identifier shape) — that remains the
|
|
35
|
+
* parser's own job via `AT1005`, undisturbed by this predicate. No
|
|
36
|
+
* syntactically valid *unconditional* Choice target ever contains separator
|
|
37
|
+
* whitespace, so this gate never rejects a value that could have been one.
|
|
38
|
+
*/
|
|
39
|
+
export declare function isChoiceTargetBoundarySafe(value: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Conservative gate for a Presentation named-argument value (`Music.volume`,
|
|
42
|
+
* `Camera.to`, `Camera.duration`, Section 25.40). Presentation's own
|
|
43
|
+
* argument tokenizer (`tokenizePresentationArgs`) splits `argsText` into
|
|
44
|
+
* tokens on this exact separator-whitespace definition, and reclassifies
|
|
45
|
+
* any resulting `key=value`-shaped token as a distinct named parameter —
|
|
46
|
+
* so a raw value containing separator whitespace could silently inject an
|
|
47
|
+
* additional parameter (or truncate the intended one) rather than remaining
|
|
48
|
+
* one atomic named-argument value.
|
|
49
|
+
*
|
|
50
|
+
* Intentionally conservative, not a numeric/duration validator: a
|
|
51
|
+
* boundary-safe value can still be content-invalid (e.g. `"bananas"` for a
|
|
52
|
+
* volume) — that remains the parser's own job via its existing diagnostics,
|
|
53
|
+
* undisturbed by this predicate.
|
|
54
|
+
*/
|
|
55
|
+
export declare function isPresentationNamedArgumentValueBoundarySafe(value: string): boolean;
|
|
56
|
+
//# 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;;;;;;;;;;;;;;;;;;;;GAoBG;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,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serializer-boundary-safety predicates (docs/PROJECT_SPEC.md Section 25.40,
|
|
3
|
+
* "Serializer Injection Safety Amendment") — small, additive, compiler-owned
|
|
4
|
+
* public helpers for the Editor's whole-`StoryBlock` insertion feature.
|
|
5
|
+
*
|
|
6
|
+
* These are NOT content validators. They answer a narrower, purely lexical
|
|
7
|
+
* question: can this raw field, once interpolated into generated DSL
|
|
8
|
+
* source, be reinterpreted by the compiler's own tokenizer as additional
|
|
9
|
+
* structure the caller never intended — silently, with zero diagnostic?
|
|
10
|
+
* Content validity (is this a real identifier / expression / duration?)
|
|
11
|
+
* remains entirely the parser's job, via its existing diagnostics
|
|
12
|
+
* (`AT1005`, `AT2001`, unknown-reference, etc.), unaffected by either
|
|
13
|
+
* predicate here.
|
|
14
|
+
*
|
|
15
|
+
* Both predicates reuse the exact lexical facts (`SEPARATOR_WHITESPACE_PATTERN`,
|
|
16
|
+
* `findLastUnescapedArrow`) the actual parser (`blocks.ts`) uses, from the
|
|
17
|
+
* shared, dependency-free `lexical-primitives.ts` module — so this file and
|
|
18
|
+
* the parser's own tokenization cannot silently drift apart. This module
|
|
19
|
+
* itself has no `remark`/`mdast` dependency, so it is safe to re-export
|
|
20
|
+
* from the public compiler surface.
|
|
21
|
+
*/
|
|
22
|
+
import { findLastUnescapedArrow, SEPARATOR_WHITESPACE_PATTERN } from "./lexical-primitives.js";
|
|
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 function isChoiceTargetBoundarySafe(value) {
|
|
41
|
+
if (SEPARATOR_WHITESPACE_PATTERN.test(value))
|
|
42
|
+
return false;
|
|
43
|
+
if (findLastUnescapedArrow(value) !== -1)
|
|
44
|
+
return false;
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Conservative gate for a Presentation named-argument value (`Music.volume`,
|
|
49
|
+
* `Camera.to`, `Camera.duration`, Section 25.40). Presentation's own
|
|
50
|
+
* argument tokenizer (`tokenizePresentationArgs`) splits `argsText` into
|
|
51
|
+
* tokens on this exact separator-whitespace definition, and reclassifies
|
|
52
|
+
* any resulting `key=value`-shaped token as a distinct named parameter —
|
|
53
|
+
* so a raw value containing separator whitespace could silently inject an
|
|
54
|
+
* additional parameter (or truncate the intended one) rather than remaining
|
|
55
|
+
* one atomic named-argument value.
|
|
56
|
+
*
|
|
57
|
+
* Intentionally conservative, not a numeric/duration validator: a
|
|
58
|
+
* boundary-safe value can still be content-invalid (e.g. `"bananas"` for a
|
|
59
|
+
* volume) — that remains the parser's own job via its existing diagnostics,
|
|
60
|
+
* undisturbed by this predicate.
|
|
61
|
+
*/
|
|
62
|
+
export function isPresentationNamedArgumentValueBoundarySafe(value) {
|
|
63
|
+
return !SEPARATOR_WHITESPACE_PATTERN.test(value);
|
|
64
|
+
}
|
|
65
|
+
//# 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;;;;;;;;;;;;;;;;;;;;GAoBG;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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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/PROJECT_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/PROJECT_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/PROJECT_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,MA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C;oBACX,aAAa,CAAC,KAAK,CAAC,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,CAAC,CAAC;oBACvE,MAAM;gBACR,CAAC;gBACD,KAAK,aAAa,CAAC,CAAC,CAAC;oBACnB,KAAK,MAAM,MAAM,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC;wBACpC,IAAI,MAAM,CAAC,SAAS;4BAAE,aAAa,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,CAAC,CAAC;oBAC/F,CAAC;oBACD,MAAM;gBACR,CAAC;gBACD,KAAK,SAAS,CAAC,CAAC,CAAC;oBACf,KAAK,MAAM,MAAM,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC;wBACpC,IAAI,MAAM,CAAC,SAAS;4BAAE,aAAa,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,CAAC,CAAC;oBAC/F,CAAC;oBACD,MAAM;gBACR,CAAC;gBACD;oBACE,MAAM;YACV,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aftertext/compiler",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "AfterText compiler: parses AfterText source into a validated Story AST.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/z0rimo/AfterText.git",
|
|
9
|
+
"directory": "packages/compiler"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc -p tsconfig.json",
|
|
28
|
+
"prepack": "tsc -b --force",
|
|
29
|
+
"typecheck": "tsc -p tsconfig.typecheck.json",
|
|
30
|
+
"test": "vitest run"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"mdast-util-to-string": "^4.0.0",
|
|
34
|
+
"unified": "^11.0.5",
|
|
35
|
+
"remark-parse": "^11.0.0",
|
|
36
|
+
"yaml": "^2.5.1"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/mdast": "^4.0.4",
|
|
40
|
+
"@types/unist": "^3.0.3",
|
|
41
|
+
"@types/node": "^22.7.5",
|
|
42
|
+
"typescript": "^5.6.3",
|
|
43
|
+
"vitest": "^2.1.2"
|
|
44
|
+
}
|
|
45
|
+
}
|