@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.
- package/LICENSE +21 -0
- package/README.md +68 -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 +354 -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 +26 -0
- package/dist/diagnostics/codes.d.ts.map +1 -0
- package/dist/diagnostics/codes.js +69 -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 +927 -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 +44 -0
- package/dist/parser/expression-lexer.d.ts.map +1 -0
- package/dist/parser/expression-lexer.js +166 -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 +158 -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 +57 -0
- package/dist/parser/serializer-safety.d.ts.map +1 -0
- package/dist/parser/serializer-safety.js +66 -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,927 @@
|
|
|
1
|
+
import { Diagnostics } from "../diagnostics/codes.js";
|
|
2
|
+
import { matchDirectiveLine, SIMPLE_PRESENTATION_DIRECTIVES } from "./directive-line.js";
|
|
3
|
+
import { joinLineText, lineEnd, lineStart, positionAt } from "./lines.js";
|
|
4
|
+
import { parseProseChunk } from "./markdown.js";
|
|
5
|
+
import { ExpressionSyntaxError, parseExpression } from "./expression-parser.js";
|
|
6
|
+
import { RESERVED_VARIABLE_NAMES } from "./expression-lexer.js";
|
|
7
|
+
import { findLastUnescapedArrow, SEPARATOR_WHITESPACE_PATTERN, SEPARATOR_WHITESPACE_SOURCE } from "./lexical-primitives.js";
|
|
8
|
+
function spanOf(line) {
|
|
9
|
+
return { start: lineStart(line), end: lineEnd(line) };
|
|
10
|
+
}
|
|
11
|
+
const SIMPLE_PRESENTATION_SET = new Set(SIMPLE_PRESENTATION_DIRECTIVES);
|
|
12
|
+
/** Records an expression-error diagnostic anchored at a syntax error's offset (and span, if any). */
|
|
13
|
+
function reportExpressionError(err, base, diagnostics) {
|
|
14
|
+
const start = {
|
|
15
|
+
line: base.line,
|
|
16
|
+
column: base.column + err.offset,
|
|
17
|
+
offset: base.offset + err.offset
|
|
18
|
+
};
|
|
19
|
+
const end = {
|
|
20
|
+
line: base.line,
|
|
21
|
+
column: base.column + err.endOffset,
|
|
22
|
+
offset: base.offset + err.endOffset
|
|
23
|
+
};
|
|
24
|
+
const span = { start, end };
|
|
25
|
+
if (err.code === "AT2005") {
|
|
26
|
+
diagnostics.push(Diagnostics.expressionNumericNotFinite(span));
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
diagnostics.push(Diagnostics.malformedExpression(err.message, span));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function tryParseExpression(argsText, line, argsStartColumn, diagnostics) {
|
|
33
|
+
const base = positionAt(line, argsStartColumn);
|
|
34
|
+
if (argsText.trim().length === 0) {
|
|
35
|
+
diagnostics.push(Diagnostics.malformedExpression("expected an expression", spanOf(line)));
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
return parseExpression(argsText, base);
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
if (err instanceof ExpressionSyntaxError) {
|
|
43
|
+
reportExpressionError(err, base, diagnostics);
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
throw err;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const PARAM_TOKEN_PATTERN = /^([A-Za-z_][A-Za-z0-9_]*)=(.+)$/;
|
|
50
|
+
function tokenizePresentationArgs(argsText) {
|
|
51
|
+
const params = {};
|
|
52
|
+
const paramValueRanges = {};
|
|
53
|
+
const paramOccurrences = {};
|
|
54
|
+
const positional = [];
|
|
55
|
+
const positionalRanges = [];
|
|
56
|
+
const tokens = [];
|
|
57
|
+
let unterminatedQuoteAt;
|
|
58
|
+
// A small deterministic single-pass scanner (docs/CORE_SPEC.md
|
|
59
|
+
// Section 14, Quoted Presentation positional arguments) — replaces the
|
|
60
|
+
// previous plain `argsText.matchAll(/\S+/g)` regex scan, which could not
|
|
61
|
+
// express quote-aware token grouping, escape decoding, or precise
|
|
62
|
+
// unterminated-quote diagnostics. Ordinary (unquoted) token behavior is
|
|
63
|
+
// preserved byte-for-byte: a token boundary is recognized on the exact
|
|
64
|
+
// same `\s` separator-whitespace semantics the previous regex used, and
|
|
65
|
+
// an ordinary token still runs to the next separator whitespace with no
|
|
66
|
+
// decoding of any kind. Quoted-token recognition activates ONLY when a
|
|
67
|
+
// token boundary's first character is `"` — a `"` occurring mid-token
|
|
68
|
+
// (not at a boundary) is never special and remains ordinary literal
|
|
69
|
+
// token content, exactly as before this feature existed. Every token
|
|
70
|
+
// (positional, named, or quoted-positional) is retained in physical scan
|
|
71
|
+
// order in `tokens`, the single source both `paramValueRanges` and the
|
|
72
|
+
// removal-range/contiguous-range computations below reuse — still one
|
|
73
|
+
// parsing pass, now with quote-awareness folded in rather than layered
|
|
74
|
+
// alongside it.
|
|
75
|
+
let i = 0;
|
|
76
|
+
const n = argsText.length;
|
|
77
|
+
while (i < n) {
|
|
78
|
+
const ch = argsText[i];
|
|
79
|
+
if (SEPARATOR_WHITESPACE_PATTERN.test(ch)) {
|
|
80
|
+
i += 1;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const tokenStart = i;
|
|
84
|
+
if (ch === '"') {
|
|
85
|
+
// Quoted positional token (docs/CORE_SPEC.md Section 14) — always
|
|
86
|
+
// positional, NEVER passed through `PARAM_TOKEN_PATTERN`: lexical
|
|
87
|
+
// grouping (this whole quoted token) is determined before named-
|
|
88
|
+
// parameter classification is ever considered, so a token like
|
|
89
|
+
// `"foo=bar.mp3"` can never be reclassified as a named parameter no
|
|
90
|
+
// matter what its decoded content looks like. Escape decoding
|
|
91
|
+
// happens inline, left to right, during this same scan — never a
|
|
92
|
+
// second pass, never a regex post-processing step. Exactly two
|
|
93
|
+
// escape sequences are recognized (`\"` and `\\`); any other `\X` is
|
|
94
|
+
// preserved as both literal characters, deliberately diverging from
|
|
95
|
+
// the expression lexer's escape model (which silently drops the
|
|
96
|
+
// backslash for an unrecognized escape — confirmed empirically to
|
|
97
|
+
// destroy Windows-style paths, e.g. `\Users` → `Users`) — this
|
|
98
|
+
// scanner keeps every authored backslash the author didn't
|
|
99
|
+
// explicitly escape.
|
|
100
|
+
let decoded = "";
|
|
101
|
+
i += 1; // past the opening quote
|
|
102
|
+
let closed = false;
|
|
103
|
+
while (i < n) {
|
|
104
|
+
const c = argsText[i];
|
|
105
|
+
if (c === '"') {
|
|
106
|
+
i += 1;
|
|
107
|
+
closed = true;
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
if (c === "\\" && i + 1 < n) {
|
|
111
|
+
const next = argsText[i + 1];
|
|
112
|
+
decoded += next === '"' || next === "\\" ? next : c + next;
|
|
113
|
+
i += 2;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
decoded += c;
|
|
117
|
+
i += 1;
|
|
118
|
+
}
|
|
119
|
+
if (!closed) {
|
|
120
|
+
unterminatedQuoteAt = tokenStart;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
// No authored separator whitespace was found between this quoted
|
|
124
|
+
// token's closing quote and whatever comes next — shell-style
|
|
125
|
+
// concatenation is not implemented as a general grammar feature
|
|
126
|
+
// (docs/CORE_SPEC.md Section 14), but this token must still end
|
|
127
|
+
// deterministically somewhere: it simply continues, raw and
|
|
128
|
+
// undecoded, through to the next separator whitespace, exactly like
|
|
129
|
+
// an ordinary token already does. This only matters for the
|
|
130
|
+
// pathological case of a quoted value with no trailing space before
|
|
131
|
+
// more content; the common, documented case (a quote followed by
|
|
132
|
+
// whitespace or end of `argsText`) is unaffected.
|
|
133
|
+
while (i < n && !SEPARATOR_WHITESPACE_PATTERN.test(argsText[i])) {
|
|
134
|
+
decoded += argsText[i];
|
|
135
|
+
i += 1;
|
|
136
|
+
}
|
|
137
|
+
const tokenEnd = i;
|
|
138
|
+
positional.push(decoded);
|
|
139
|
+
positionalRanges.push({ start: tokenStart, end: tokenEnd });
|
|
140
|
+
tokens.push({ start: tokenStart, end: tokenEnd, key: undefined });
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
while (i < n && !SEPARATOR_WHITESPACE_PATTERN.test(argsText[i]))
|
|
144
|
+
i += 1;
|
|
145
|
+
const tokenEnd = i;
|
|
146
|
+
const token = argsText.slice(tokenStart, tokenEnd);
|
|
147
|
+
const paramMatch = PARAM_TOKEN_PATTERN.exec(token);
|
|
148
|
+
if (paramMatch) {
|
|
149
|
+
const key = paramMatch[1];
|
|
150
|
+
const value = paramMatch[2];
|
|
151
|
+
params[key] = value;
|
|
152
|
+
const valueStart = tokenStart + key.length + 1; // past "key="
|
|
153
|
+
paramValueRanges[key] = { start: valueStart, end: valueStart + value.length };
|
|
154
|
+
paramOccurrences[key] = (paramOccurrences[key] ?? 0) + 1;
|
|
155
|
+
tokens.push({ start: tokenStart, end: tokenEnd, key });
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
positional.push(token);
|
|
159
|
+
positionalRanges.push({ start: tokenStart, end: tokenEnd });
|
|
160
|
+
tokens.push({ start: tokenStart, end: tokenEnd, key: undefined });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
// A safe, trivia-aware removal range for a named parameter exists only
|
|
164
|
+
// when its key occurs EXACTLY ONCE (docs/CORE_SPEC.md Section 14) —
|
|
165
|
+
// with 2+ occurrences, no single range could delete "the property"
|
|
166
|
+
// without either silently reviving an earlier duplicate (deleting only
|
|
167
|
+
// the effective last occurrence) or requiring multi-range deletion, so
|
|
168
|
+
// no range is produced for that key at all. Reuses the token adjacency
|
|
169
|
+
// already scanned above (one extra pass over the small in-memory
|
|
170
|
+
// `tokens` array — not a second scan of `argsText`, not a new grammar).
|
|
171
|
+
const paramRemovalRanges = {};
|
|
172
|
+
tokens.forEach((tok, index) => {
|
|
173
|
+
if (tok.key === undefined || paramOccurrences[tok.key] !== 1)
|
|
174
|
+
return;
|
|
175
|
+
if (index === 0) {
|
|
176
|
+
// Physically first argument token: remove the token plus the
|
|
177
|
+
// separator immediately FOLLOWING it. The whitespace between the
|
|
178
|
+
// directive name and this first argument is never touched — it
|
|
179
|
+
// lies outside argsText entirely, and deleting it would merge the
|
|
180
|
+
// directive name with whichever token becomes the new first
|
|
181
|
+
// argument (e.g. `@camera duration=0.50s zoom` must never become
|
|
182
|
+
// `@camerazoom`).
|
|
183
|
+
const next = tokens[index + 1];
|
|
184
|
+
paramRemovalRanges[tok.key] = { start: tok.start, end: next ? next.start : tok.end };
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
// Middle or last: remove the separator immediately PRECEDING the
|
|
188
|
+
// token plus the token itself. For a last-position token this
|
|
189
|
+
// naturally excludes trailing line trivia, which lies past the end
|
|
190
|
+
// of argsText entirely (see argumentsEndPosition).
|
|
191
|
+
const previous = tokens[index - 1];
|
|
192
|
+
paramRemovalRanges[tok.key] = { start: previous.end, end: tok.end };
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
// The contiguous positional run (docs/CORE_SPEC.md Section 14) — a
|
|
196
|
+
// strict, additive generalization of the single-positional-token case
|
|
197
|
+
// above, reusing the same `tokens` adjacency data with no second scan.
|
|
198
|
+
// Populated iff at least one positional token exists AND every token
|
|
199
|
+
// between the first and last positional token (inclusive) is itself
|
|
200
|
+
// positional — i.e. no named token interrupts the run. A named token
|
|
201
|
+
// physically before or after the entire run does not break contiguity.
|
|
202
|
+
let contiguousPositionalRange;
|
|
203
|
+
const positionalIndices = [];
|
|
204
|
+
tokens.forEach((tok, index) => {
|
|
205
|
+
if (tok.key === undefined)
|
|
206
|
+
positionalIndices.push(index);
|
|
207
|
+
});
|
|
208
|
+
if (positionalIndices.length > 0) {
|
|
209
|
+
const firstIndex = positionalIndices[0];
|
|
210
|
+
const lastIndex = positionalIndices[positionalIndices.length - 1];
|
|
211
|
+
const isContiguous = tokens.slice(firstIndex, lastIndex + 1).every((tok) => tok.key === undefined);
|
|
212
|
+
if (isContiguous) {
|
|
213
|
+
contiguousPositionalRange = {
|
|
214
|
+
start: tokens[firstIndex].start,
|
|
215
|
+
end: tokens[lastIndex].end
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
value: positional.length > 0 ? positional.join(" ") : undefined,
|
|
221
|
+
params,
|
|
222
|
+
paramValueRanges,
|
|
223
|
+
paramRemovalRanges,
|
|
224
|
+
positionalRanges,
|
|
225
|
+
contiguousPositionalRange,
|
|
226
|
+
unterminatedQuoteAt
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
const DURATION_PATTERN = /^(\d+(?:\.\d+)?)(ms|s)$/;
|
|
230
|
+
/** Normalizes a duration literal (`6s`, `1200ms`) to milliseconds; `undefined` if malformed. */
|
|
231
|
+
function parseDurationMs(text) {
|
|
232
|
+
const match = DURATION_PATTERN.exec(text);
|
|
233
|
+
if (!match)
|
|
234
|
+
return undefined;
|
|
235
|
+
const amount = Number(match[1]);
|
|
236
|
+
return match[2] === "s" ? Math.round(amount * 1000) : Math.round(amount);
|
|
237
|
+
}
|
|
238
|
+
const NUMBER_PATTERN = /^-?\d+(?:\.\d+)?$/;
|
|
239
|
+
/** Normalizes a bare numeric literal (`1.15`, `0.5`); `undefined` if malformed. */
|
|
240
|
+
function parseNumericParam(text) {
|
|
241
|
+
return NUMBER_PATTERN.test(text) ? Number(text) : undefined;
|
|
242
|
+
}
|
|
243
|
+
const KNOWN_CAMERA_ACTIONS = new Set(["zoom"]);
|
|
244
|
+
/**
|
|
245
|
+
* Parses, validates, and normalizes a presentation directive into a typed
|
|
246
|
+
* PresentationCommand (see ast/presentation.ts). Unit strings like `6s` and
|
|
247
|
+
* numeric params like `to=1.15` are converted to real numbers here — the
|
|
248
|
+
* only place this happens — so runtime code never re-parses directive text.
|
|
249
|
+
* Returns `undefined` (with a diagnostic) for malformed directives rather
|
|
250
|
+
* than producing a node with placeholder values.
|
|
251
|
+
*/
|
|
252
|
+
function parsePresentation(kind, argsText, span, line, argsStartColumn, diagnostics) {
|
|
253
|
+
const tokenized = tokenizePresentationArgs(argsText);
|
|
254
|
+
// An unclosed quoted positional token (docs/CORE_SPEC.md Section 14,
|
|
255
|
+
// Quoted Presentation positional arguments) reuses the existing AT1301
|
|
256
|
+
// (`malformedPresentation`) — no new diagnostic code — with a span from
|
|
257
|
+
// the opening quote through the end of this physical directive line
|
|
258
|
+
// (more precise than the whole-directive `span` every other AT1301 case
|
|
259
|
+
// below uses), computed directly from the already-available `line`/
|
|
260
|
+
// `argsStartColumn` with no second source scan. No PresentationNode is
|
|
261
|
+
// produced; parsing of subsequent physical lines is unaffected, since
|
|
262
|
+
// the caller's own line loop already advances unconditionally.
|
|
263
|
+
if (tokenized.unterminatedQuoteAt !== undefined) {
|
|
264
|
+
diagnostics.push(Diagnostics.malformedPresentation("unterminated quoted argument", {
|
|
265
|
+
start: positionAt(line, argsStartColumn + tokenized.unterminatedQuoteAt),
|
|
266
|
+
end: lineEnd(line)
|
|
267
|
+
}));
|
|
268
|
+
return undefined;
|
|
269
|
+
}
|
|
270
|
+
const { value, params, paramValueRanges, paramRemovalRanges, positionalRanges, contiguousPositionalRange } = tokenized;
|
|
271
|
+
// Absolute source spans for every recognized parameter value (docs/CORE_SPEC.md
|
|
272
|
+
// Section 25.36) — computed once, reused by every return below, always
|
|
273
|
+
// present (an empty record when the directive has no named parameters).
|
|
274
|
+
const parameterValueSpans = {};
|
|
275
|
+
for (const [key, range] of Object.entries(paramValueRanges)) {
|
|
276
|
+
parameterValueSpans[key] = {
|
|
277
|
+
start: positionAt(line, argsStartColumn + range.start),
|
|
278
|
+
end: positionAt(line, argsStartColumn + range.end)
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
// Absolute, duplicate-safe removal spans (docs/CORE_SPEC.md Section
|
|
282
|
+
// 14) — present only for a key with exactly one authored occurrence;
|
|
283
|
+
// computed once, reused by every return below.
|
|
284
|
+
const parameterRemovalSpans = {};
|
|
285
|
+
for (const [key, range] of Object.entries(paramRemovalRanges)) {
|
|
286
|
+
parameterRemovalSpans[key] = {
|
|
287
|
+
start: positionAt(line, argsStartColumn + range.start),
|
|
288
|
+
end: positionAt(line, argsStartColumn + range.end)
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
// The positional value has a safe, contiguous source span only when
|
|
292
|
+
// there is exactly one positional token (docs/CORE_SPEC.md Section
|
|
293
|
+
// 14) — with zero or two-or-more, `value` either doesn't exist or is a
|
|
294
|
+
// synthetic join of non-contiguous source text, so no span is produced.
|
|
295
|
+
const positionalValueSpan = positionalRanges.length === 1
|
|
296
|
+
? {
|
|
297
|
+
start: positionAt(line, argsStartColumn + positionalRanges[0].start),
|
|
298
|
+
end: positionAt(line, argsStartColumn + positionalRanges[0].end)
|
|
299
|
+
}
|
|
300
|
+
: undefined;
|
|
301
|
+
// Absolute contiguous-positional-run span (docs/CORE_SPEC.md Section
|
|
302
|
+
// 14) — a strict, additive generalization of `positionalValueSpan`;
|
|
303
|
+
// never derived when `contiguousPositionalRange` is `undefined` (zero
|
|
304
|
+
// positional tokens, or a named token interrupting the run).
|
|
305
|
+
const contiguousPositionalSpan = contiguousPositionalRange
|
|
306
|
+
? {
|
|
307
|
+
start: positionAt(line, argsStartColumn + contiguousPositionalRange.start),
|
|
308
|
+
end: positionAt(line, argsStartColumn + contiguousPositionalRange.end)
|
|
309
|
+
}
|
|
310
|
+
: undefined;
|
|
311
|
+
// The position immediately after the last non-whitespace character of
|
|
312
|
+
// `argsText` (docs/CORE_SPEC.md Section 14) — `argsText` is already
|
|
313
|
+
// `.trimEnd()`-ed by `matchDirectiveLine`, so `argsStartColumn +
|
|
314
|
+
// argsText.length` lands exactly there, before any trailing line
|
|
315
|
+
// whitespace/terminator. Never derived from the whole-node `span.end`,
|
|
316
|
+
// which includes that trailing whitespace.
|
|
317
|
+
const argumentsEndPosition = positionAt(line, argsStartColumn + argsText.length);
|
|
318
|
+
// Whole-command removal span (docs/CORE_SPEC.md Section 14) — always
|
|
319
|
+
// starts at the directive's own span.start (never adjusted backward
|
|
320
|
+
// into preceding source); extends span.end by exactly this directive's
|
|
321
|
+
// own physical line's terminator width (0 at true EOF, 1 for `\n`, 2 for
|
|
322
|
+
// `\r\n`) so deleting it never leaves an accidental blank line behind.
|
|
323
|
+
// Computed entirely from the `line` already in hand — no lookup of a
|
|
324
|
+
// different line by absolute number, no second source scan.
|
|
325
|
+
const commandRemovalSpan = {
|
|
326
|
+
start: span.start,
|
|
327
|
+
end: line.newlineLength === 0
|
|
328
|
+
? span.end
|
|
329
|
+
: { line: line.number + 1, column: 1, offset: line.endOffset + line.newlineLength }
|
|
330
|
+
};
|
|
331
|
+
function fail(reason) {
|
|
332
|
+
diagnostics.push(Diagnostics.malformedPresentation(reason, span));
|
|
333
|
+
return undefined;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* `parseDurationMs`/`parseNumericParam` only reject lexically malformed
|
|
337
|
+
* text (AT1301); a lexically-valid literal that converts to a non-finite
|
|
338
|
+
* number (e.g. an extreme digit run, or a finite seconds amount that
|
|
339
|
+
* overflows only after the `* 1000` conversion) is a distinct condition
|
|
340
|
+
* reported as AT1302 (docs/CORE_SPEC.md Section 24).
|
|
341
|
+
*/
|
|
342
|
+
function failNotFinite() {
|
|
343
|
+
diagnostics.push(Diagnostics.presentationNumericNotFinite(span));
|
|
344
|
+
return undefined;
|
|
345
|
+
}
|
|
346
|
+
switch (kind) {
|
|
347
|
+
case "background":
|
|
348
|
+
case "layer":
|
|
349
|
+
case "sfx": {
|
|
350
|
+
if (!value)
|
|
351
|
+
return fail(`@${kind} requires a file path`);
|
|
352
|
+
const command = kind === "background"
|
|
353
|
+
? { type: "Background", image: value }
|
|
354
|
+
: kind === "layer"
|
|
355
|
+
? { type: "Layer", image: value }
|
|
356
|
+
: { type: "Sfx", clip: value };
|
|
357
|
+
return {
|
|
358
|
+
type: "Presentation",
|
|
359
|
+
command,
|
|
360
|
+
span,
|
|
361
|
+
parameterValueSpans,
|
|
362
|
+
positionalValueSpan,
|
|
363
|
+
argumentsEndPosition,
|
|
364
|
+
parameterRemovalSpans,
|
|
365
|
+
contiguousPositionalSpan,
|
|
366
|
+
commandRemovalSpan
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
case "music": {
|
|
370
|
+
if (!value)
|
|
371
|
+
return fail("@music requires a file path");
|
|
372
|
+
let volume;
|
|
373
|
+
if (params["volume"] !== undefined) {
|
|
374
|
+
volume = parseNumericParam(params["volume"]);
|
|
375
|
+
if (volume === undefined)
|
|
376
|
+
return fail(`invalid volume "${params["volume"]}"`);
|
|
377
|
+
if (!Number.isFinite(volume))
|
|
378
|
+
return failNotFinite();
|
|
379
|
+
}
|
|
380
|
+
return {
|
|
381
|
+
type: "Presentation",
|
|
382
|
+
command: { type: "Music", track: value, volume },
|
|
383
|
+
span,
|
|
384
|
+
parameterValueSpans,
|
|
385
|
+
positionalValueSpan,
|
|
386
|
+
argumentsEndPosition,
|
|
387
|
+
parameterRemovalSpans,
|
|
388
|
+
contiguousPositionalSpan,
|
|
389
|
+
commandRemovalSpan
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
case "pause": {
|
|
393
|
+
if (!value)
|
|
394
|
+
return fail("@pause requires a duration");
|
|
395
|
+
const durationMs = parseDurationMs(value);
|
|
396
|
+
if (durationMs === undefined)
|
|
397
|
+
return fail(`invalid duration "${value}"`);
|
|
398
|
+
if (!Number.isFinite(durationMs))
|
|
399
|
+
return failNotFinite();
|
|
400
|
+
return {
|
|
401
|
+
type: "Presentation",
|
|
402
|
+
command: { type: "Pause", durationMs },
|
|
403
|
+
span,
|
|
404
|
+
parameterValueSpans,
|
|
405
|
+
positionalValueSpan,
|
|
406
|
+
argumentsEndPosition,
|
|
407
|
+
parameterRemovalSpans,
|
|
408
|
+
contiguousPositionalSpan,
|
|
409
|
+
commandRemovalSpan
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
case "camera": {
|
|
413
|
+
if (value === undefined)
|
|
414
|
+
return fail("@camera requires an action");
|
|
415
|
+
if (!KNOWN_CAMERA_ACTIONS.has(value)) {
|
|
416
|
+
return fail(`unsupported camera action "${value}"`);
|
|
417
|
+
}
|
|
418
|
+
if (params["to"] === undefined)
|
|
419
|
+
return fail('@camera zoom requires "to="');
|
|
420
|
+
const to = parseNumericParam(params["to"]);
|
|
421
|
+
if (to === undefined)
|
|
422
|
+
return fail(`invalid "to" value "${params["to"]}"`);
|
|
423
|
+
if (!Number.isFinite(to))
|
|
424
|
+
return failNotFinite();
|
|
425
|
+
let durationMs;
|
|
426
|
+
if (params["duration"] !== undefined) {
|
|
427
|
+
durationMs = parseDurationMs(params["duration"]);
|
|
428
|
+
if (durationMs === undefined)
|
|
429
|
+
return fail(`invalid duration "${params["duration"]}"`);
|
|
430
|
+
if (!Number.isFinite(durationMs))
|
|
431
|
+
return failNotFinite();
|
|
432
|
+
}
|
|
433
|
+
return {
|
|
434
|
+
type: "Presentation",
|
|
435
|
+
command: { type: "Camera", action: "zoom", to, durationMs },
|
|
436
|
+
span,
|
|
437
|
+
parameterValueSpans,
|
|
438
|
+
positionalValueSpan,
|
|
439
|
+
argumentsEndPosition,
|
|
440
|
+
parameterRemovalSpans,
|
|
441
|
+
contiguousPositionalSpan,
|
|
442
|
+
commandRemovalSpan
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
const SET_PATTERN = /^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.+)$/;
|
|
448
|
+
function parseSet(directive, line, diagnostics) {
|
|
449
|
+
const match = SET_PATTERN.exec(directive.args);
|
|
450
|
+
if (!match) {
|
|
451
|
+
diagnostics.push(Diagnostics.malformedExpression('expected "name = expression"', spanOf(line)));
|
|
452
|
+
return undefined;
|
|
453
|
+
}
|
|
454
|
+
const name = match[1];
|
|
455
|
+
const exprText = match[2];
|
|
456
|
+
const exprOffsetInArgs = directive.args.length - exprText.length;
|
|
457
|
+
const base = positionAt(line, directive.argsStartColumn + exprOffsetInArgs);
|
|
458
|
+
const nameSourceSpan = {
|
|
459
|
+
start: positionAt(line, directive.argsStartColumn),
|
|
460
|
+
end: positionAt(line, directive.argsStartColumn + name.length)
|
|
461
|
+
};
|
|
462
|
+
if (RESERVED_VARIABLE_NAMES.has(name)) {
|
|
463
|
+
diagnostics.push(Diagnostics.malformedExpression(`"${name}" is a reserved word and cannot be a variable name`, nameSourceSpan));
|
|
464
|
+
return undefined;
|
|
465
|
+
}
|
|
466
|
+
const expressionSourceSpan = {
|
|
467
|
+
start: base,
|
|
468
|
+
end: positionAt(line, directive.argsStartColumn + directive.args.length)
|
|
469
|
+
};
|
|
470
|
+
try {
|
|
471
|
+
const expression = parseExpression(exprText, base);
|
|
472
|
+
return {
|
|
473
|
+
type: "Set",
|
|
474
|
+
name,
|
|
475
|
+
expression,
|
|
476
|
+
span: spanOf(line),
|
|
477
|
+
nameSourceSpan,
|
|
478
|
+
expressionSourceSpan
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
catch (err) {
|
|
482
|
+
if (err instanceof ExpressionSyntaxError) {
|
|
483
|
+
reportExpressionError(err, base, diagnostics);
|
|
484
|
+
return undefined;
|
|
485
|
+
}
|
|
486
|
+
throw err;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
/** Bullet marker introducing a choice item: `* ...` or `- ...`. */
|
|
490
|
+
const CHOICE_BULLET_PATTERN = /^(\s*)[*-]\s+/;
|
|
491
|
+
/**
|
|
492
|
+
* The target (+ optional condition) segment following the arrow: `room` or
|
|
493
|
+
* `room if has_key`. Built from `SEPARATOR_WHITESPACE_SOURCE` rather than a
|
|
494
|
+
* hardcoded `\s` so the `if`-separator whitespace it recognizes is the exact
|
|
495
|
+
* same lexical fact `isChoiceTargetBoundarySafe` (`serializer-safety.ts`)
|
|
496
|
+
* checks for — the two cannot silently drift apart.
|
|
497
|
+
*/
|
|
498
|
+
const CHOICE_TAIL_PATTERN = new RegExp(`^([A-Za-z][A-Za-z0-9_-]*)(?:${SEPARATOR_WHITESPACE_SOURCE}+if${SEPARATOR_WHITESPACE_SOURCE}+(.+))?$`);
|
|
499
|
+
function parseChoice(lines, openIndex, diagnostics) {
|
|
500
|
+
const openLine = lines[openIndex];
|
|
501
|
+
let idx = openIndex + 1;
|
|
502
|
+
const items = [];
|
|
503
|
+
let lastEnd = lineEnd(openLine);
|
|
504
|
+
while (idx < lines.length) {
|
|
505
|
+
const line = lines[idx];
|
|
506
|
+
if (line.text.trim().length === 0) {
|
|
507
|
+
idx += 1;
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
const directive = matchDirectiveLine(line.text);
|
|
511
|
+
if (directive && (directive.name === "end" || directive.name === "scene")) {
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
const bulletMatch = CHOICE_BULLET_PATTERN.exec(line.text);
|
|
515
|
+
if (!bulletMatch) {
|
|
516
|
+
idx += 1;
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
const contentStart = bulletMatch[0].length;
|
|
520
|
+
const content = line.text.slice(contentStart);
|
|
521
|
+
const arrowIndex = findLastUnescapedArrow(content);
|
|
522
|
+
// A bullet-prefixed line (`- ...`/`* ...`) is always a ChoiceItem
|
|
523
|
+
// candidate — the bullet marker is the only signal this parser uses to
|
|
524
|
+
// distinguish authored items from unrelated narrative lines (which are
|
|
525
|
+
// silently skipped above, `!bulletMatch`, by design). Once a line clears
|
|
526
|
+
// that bar, failing to parse as a complete item is always reported
|
|
527
|
+
// (AT1005) rather than silently dropped, so a malformed item can never
|
|
528
|
+
// vanish into a smaller-than-authored Choice with no diagnostic.
|
|
529
|
+
if (arrowIndex === -1) {
|
|
530
|
+
diagnostics.push(Diagnostics.malformedChoiceItem('missing "-> target" after item text', spanOf(line)));
|
|
531
|
+
idx += 1;
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
// Display text is only the "if" delimiter's own condition-grammar is
|
|
535
|
+
// applied to the tail *after* the target — so an "if" appearing in
|
|
536
|
+
// display text (before the arrow) can never be mistaken for one.
|
|
537
|
+
const displayText = content.slice(0, arrowIndex).trim().replace(/\\->/g, "->");
|
|
538
|
+
const tailStart = arrowIndex + 2;
|
|
539
|
+
const tailRaw = content.slice(tailStart);
|
|
540
|
+
const tailLeadingWs = tailRaw.length - tailRaw.trimStart().length;
|
|
541
|
+
const tailTrimmed = tailRaw.trim();
|
|
542
|
+
const tailMatch = CHOICE_TAIL_PATTERN.exec(tailTrimmed);
|
|
543
|
+
if (!tailMatch) {
|
|
544
|
+
const tailStartOffset = contentStart + tailStart + tailLeadingWs;
|
|
545
|
+
const tailSpan = {
|
|
546
|
+
start: positionAt(line, tailStartOffset),
|
|
547
|
+
end: positionAt(line, tailStartOffset + tailTrimmed.length)
|
|
548
|
+
};
|
|
549
|
+
diagnostics.push(Diagnostics.malformedChoiceItem(`invalid target "${tailTrimmed}"`, tailSpan));
|
|
550
|
+
idx += 1;
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
const target = tailMatch[1];
|
|
554
|
+
const conditionText = tailMatch[2];
|
|
555
|
+
let condition;
|
|
556
|
+
let conditionSourceSpan;
|
|
557
|
+
if (conditionText !== undefined) {
|
|
558
|
+
const conditionStart = tailTrimmed.lastIndexOf(conditionText);
|
|
559
|
+
const base = positionAt(line, contentStart + tailStart + tailLeadingWs + conditionStart);
|
|
560
|
+
// A pure source-tooling fact about the Choice tail's own shape —
|
|
561
|
+
// populated whenever the grammar recognizes `target if <text>`,
|
|
562
|
+
// independently of whether `text` itself goes on to parse below.
|
|
563
|
+
// Derived from facts already computed above (`base`, `conditionText`),
|
|
564
|
+
// never from `condition.span` (see the field's doc comment for why).
|
|
565
|
+
conditionSourceSpan = {
|
|
566
|
+
start: base,
|
|
567
|
+
end: positionAt(line, contentStart + tailStart + tailLeadingWs + conditionStart + conditionText.length)
|
|
568
|
+
};
|
|
569
|
+
try {
|
|
570
|
+
condition = parseExpression(conditionText, base);
|
|
571
|
+
}
|
|
572
|
+
catch (err) {
|
|
573
|
+
if (err instanceof ExpressionSyntaxError) {
|
|
574
|
+
reportExpressionError(err, base, diagnostics);
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
throw err;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
items.push({ text: displayText, target, condition, span: spanOf(line), conditionSourceSpan });
|
|
582
|
+
idx += 1;
|
|
583
|
+
}
|
|
584
|
+
const terminator = idx < lines.length ? matchDirectiveLine(lines[idx].text) : null;
|
|
585
|
+
if (terminator && terminator.name === "end") {
|
|
586
|
+
lastEnd = lineEnd(lines[idx]);
|
|
587
|
+
idx += 1;
|
|
588
|
+
}
|
|
589
|
+
else {
|
|
590
|
+
diagnostics.push(Diagnostics.unclosedBlock("choice", spanOf(openLine)));
|
|
591
|
+
}
|
|
592
|
+
return {
|
|
593
|
+
block: { type: "Choice", items, span: { start: lineStart(openLine), end: lastEnd } },
|
|
594
|
+
nextIndex: idx
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
function parseConditional(lines, openIndex, diagnostics) {
|
|
598
|
+
const openLine = lines[openIndex];
|
|
599
|
+
const branches = [];
|
|
600
|
+
let idx = openIndex;
|
|
601
|
+
let kind = "if";
|
|
602
|
+
let lastEnd = lineEnd(openLine);
|
|
603
|
+
for (;;) {
|
|
604
|
+
const branchLine = lines[idx];
|
|
605
|
+
const branchDirective = matchDirectiveLine(branchLine.text);
|
|
606
|
+
const condition = kind === "else"
|
|
607
|
+
? undefined
|
|
608
|
+
: tryParseExpression(branchDirective.args, branchLine, branchDirective.argsStartColumn, diagnostics);
|
|
609
|
+
const conditionSourceSpan = kind !== "else" && branchDirective.args.length > 0
|
|
610
|
+
? {
|
|
611
|
+
start: positionAt(branchLine, branchDirective.argsStartColumn),
|
|
612
|
+
end: positionAt(branchLine, branchDirective.argsStartColumn + branchDirective.args.length)
|
|
613
|
+
}
|
|
614
|
+
: undefined;
|
|
615
|
+
idx += 1;
|
|
616
|
+
const seq = parseBlockSequence(lines, idx, diagnostics, new Set(["elseif", "else", "end"]));
|
|
617
|
+
const branchSpan = {
|
|
618
|
+
start: lineStart(branchLine),
|
|
619
|
+
end: seq.blocks.length > 0 ? seq.blocks[seq.blocks.length - 1].span.end : lineEnd(branchLine)
|
|
620
|
+
};
|
|
621
|
+
branches.push({
|
|
622
|
+
kind,
|
|
623
|
+
condition,
|
|
624
|
+
blocks: seq.blocks,
|
|
625
|
+
span: branchSpan,
|
|
626
|
+
followingLineEnding: lineEndingAt(lines, branchSpan.end),
|
|
627
|
+
conditionSourceSpan
|
|
628
|
+
});
|
|
629
|
+
idx = seq.nextIndex;
|
|
630
|
+
if (seq.stoppedBy === "end") {
|
|
631
|
+
lastEnd = lineEnd(lines[idx]);
|
|
632
|
+
idx += 1;
|
|
633
|
+
break;
|
|
634
|
+
}
|
|
635
|
+
if (seq.stoppedBy === "elseif") {
|
|
636
|
+
kind = "elseif";
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
if (seq.stoppedBy === "else") {
|
|
640
|
+
kind = "else";
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
diagnostics.push(Diagnostics.unclosedBlock("if", spanOf(openLine)));
|
|
644
|
+
break;
|
|
645
|
+
}
|
|
646
|
+
return {
|
|
647
|
+
block: { type: "Conditional", branches, span: { start: lineStart(openLine), end: lastEnd } },
|
|
648
|
+
nextIndex: idx
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
function parseVariant(lines, openIndex, diagnostics) {
|
|
652
|
+
const openLine = lines[openIndex];
|
|
653
|
+
const openDirective = matchDirectiveLine(openLine.text);
|
|
654
|
+
const variantId = openDirective.args.trim();
|
|
655
|
+
// A pure source-tooling fact about the `@variant` header's own shape —
|
|
656
|
+
// see the field's doc comment for why it is always present (zero-width
|
|
657
|
+
// for an empty ID) rather than `undefined`. Mirrors the branch-level
|
|
658
|
+
// `conditionSourceSpan` derivation above, one level up.
|
|
659
|
+
const idSourceSpan = openDirective.args.length > 0
|
|
660
|
+
? {
|
|
661
|
+
start: positionAt(openLine, openDirective.argsStartColumn),
|
|
662
|
+
end: positionAt(openLine, openDirective.argsStartColumn + openDirective.args.length)
|
|
663
|
+
}
|
|
664
|
+
: {
|
|
665
|
+
start: positionAt(openLine, openDirective.argsStartColumn),
|
|
666
|
+
end: positionAt(openLine, openDirective.argsStartColumn)
|
|
667
|
+
};
|
|
668
|
+
const branches = [];
|
|
669
|
+
let whenCount = 0;
|
|
670
|
+
let lastEnd = lineEnd(openLine);
|
|
671
|
+
let idx = openIndex + 1;
|
|
672
|
+
let seq = parseBlockSequence(lines, idx, diagnostics, new Set(["when", "otherwise", "end"]));
|
|
673
|
+
idx = seq.nextIndex;
|
|
674
|
+
while (seq.stoppedBy === "when" || seq.stoppedBy === "otherwise") {
|
|
675
|
+
const kind = seq.stoppedBy;
|
|
676
|
+
const branchLine = lines[idx];
|
|
677
|
+
const branchDirective = matchDirectiveLine(branchLine.text);
|
|
678
|
+
const condition = kind === "when"
|
|
679
|
+
? tryParseExpression(branchDirective.args, branchLine, branchDirective.argsStartColumn, diagnostics)
|
|
680
|
+
: undefined;
|
|
681
|
+
const branchId = kind === "when" ? `${variantId}:${whenCount++}` : `${variantId}:otherwise`;
|
|
682
|
+
// A pure source-tooling fact about the `@when` header's own shape —
|
|
683
|
+
// populated whenever the directive syntactically presents a condition
|
|
684
|
+
// payload, independently of whether that payload goes on to parse above.
|
|
685
|
+
// `branchDirective.args` already excludes the separator whitespace
|
|
686
|
+
// before it (consumed by `matchDirectiveLine`'s own required `[ \t]+`)
|
|
687
|
+
// and any trailing whitespace (`.trimEnd()`), so this is exactly the
|
|
688
|
+
// authored condition text with no rescanning and no dependence on
|
|
689
|
+
// `condition`/`condition.span` (see the field's doc comment for why).
|
|
690
|
+
const conditionSourceSpan = kind === "when" && branchDirective.args.length > 0
|
|
691
|
+
? {
|
|
692
|
+
start: positionAt(branchLine, branchDirective.argsStartColumn),
|
|
693
|
+
end: positionAt(branchLine, branchDirective.argsStartColumn + branchDirective.args.length)
|
|
694
|
+
}
|
|
695
|
+
: undefined;
|
|
696
|
+
idx += 1;
|
|
697
|
+
const bodySeq = parseBlockSequence(lines, idx, diagnostics, new Set(["when", "otherwise", "end"]));
|
|
698
|
+
const branchSpan = {
|
|
699
|
+
start: lineStart(branchLine),
|
|
700
|
+
end: bodySeq.blocks.length > 0
|
|
701
|
+
? bodySeq.blocks[bodySeq.blocks.length - 1].span.end
|
|
702
|
+
: lineEnd(branchLine)
|
|
703
|
+
};
|
|
704
|
+
branches.push({
|
|
705
|
+
kind,
|
|
706
|
+
branchId,
|
|
707
|
+
condition,
|
|
708
|
+
blocks: bodySeq.blocks,
|
|
709
|
+
span: branchSpan,
|
|
710
|
+
followingLineEnding: lineEndingAt(lines, branchSpan.end),
|
|
711
|
+
conditionSourceSpan
|
|
712
|
+
});
|
|
713
|
+
idx = bodySeq.nextIndex;
|
|
714
|
+
seq = bodySeq;
|
|
715
|
+
}
|
|
716
|
+
if (seq.stoppedBy === "end") {
|
|
717
|
+
lastEnd = lineEnd(lines[idx]);
|
|
718
|
+
idx += 1;
|
|
719
|
+
}
|
|
720
|
+
else {
|
|
721
|
+
diagnostics.push(Diagnostics.unclosedBlock("variant", spanOf(openLine)));
|
|
722
|
+
}
|
|
723
|
+
return {
|
|
724
|
+
block: {
|
|
725
|
+
type: "Variant",
|
|
726
|
+
id: variantId,
|
|
727
|
+
idSourceSpan,
|
|
728
|
+
branches,
|
|
729
|
+
span: { start: lineStart(openLine), end: lastEnd }
|
|
730
|
+
},
|
|
731
|
+
nextIndex: idx
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
* Parses a run of lines into StoryBlocks until either a directive in
|
|
736
|
+
* `terminators` is reached, a `@scene` line is reached (scenes always
|
|
737
|
+
* bound any enclosing construct), or the input is exhausted.
|
|
738
|
+
*
|
|
739
|
+
* This single function drives every nesting level (scene bodies,
|
|
740
|
+
* conditional/variant branch bodies) — nesting falls out of recursive
|
|
741
|
+
* calls to parseConditional/parseVariant/parseChoice below.
|
|
742
|
+
*/
|
|
743
|
+
export function parseBlockSequence(lines, start, diagnostics, terminators) {
|
|
744
|
+
const blocks = [];
|
|
745
|
+
let i = start;
|
|
746
|
+
let proseStart = -1;
|
|
747
|
+
const flushProse = (endExclusive) => {
|
|
748
|
+
if (proseStart === -1)
|
|
749
|
+
return;
|
|
750
|
+
const chunkLines = lines.slice(proseStart, endExclusive);
|
|
751
|
+
proseStart = -1;
|
|
752
|
+
if (chunkLines.length === 0)
|
|
753
|
+
return;
|
|
754
|
+
const text = joinLineText(chunkLines);
|
|
755
|
+
if (text.trim().length === 0)
|
|
756
|
+
return;
|
|
757
|
+
const first = chunkLines[0];
|
|
758
|
+
const proseBlocks = parseProseChunk(text, { startLine: first.number, startOffset: first.startOffset }, diagnostics);
|
|
759
|
+
blocks.push(...proseBlocks);
|
|
760
|
+
};
|
|
761
|
+
while (i < lines.length) {
|
|
762
|
+
const line = lines[i];
|
|
763
|
+
const directive = matchDirectiveLine(line.text);
|
|
764
|
+
if (!directive) {
|
|
765
|
+
if (proseStart === -1)
|
|
766
|
+
proseStart = i;
|
|
767
|
+
i += 1;
|
|
768
|
+
continue;
|
|
769
|
+
}
|
|
770
|
+
if (directive.name === "scene" || terminators.has(directive.name)) {
|
|
771
|
+
flushProse(i);
|
|
772
|
+
return { blocks, nextIndex: i, stoppedBy: directive.name };
|
|
773
|
+
}
|
|
774
|
+
flushProse(i);
|
|
775
|
+
if (SIMPLE_PRESENTATION_SET.has(directive.name)) {
|
|
776
|
+
const node = parsePresentation(directive.name, directive.args, spanOf(line), line, directive.argsStartColumn, diagnostics);
|
|
777
|
+
if (node)
|
|
778
|
+
blocks.push(node);
|
|
779
|
+
i += 1;
|
|
780
|
+
continue;
|
|
781
|
+
}
|
|
782
|
+
switch (directive.name) {
|
|
783
|
+
case "set": {
|
|
784
|
+
const node = parseSet(directive, line, diagnostics);
|
|
785
|
+
if (node)
|
|
786
|
+
blocks.push(node);
|
|
787
|
+
i += 1;
|
|
788
|
+
break;
|
|
789
|
+
}
|
|
790
|
+
case "goto": {
|
|
791
|
+
const targetSourceSpan = {
|
|
792
|
+
start: positionAt(line, directive.argsStartColumn),
|
|
793
|
+
end: positionAt(line, directive.argsStartColumn + directive.args.length)
|
|
794
|
+
};
|
|
795
|
+
const node = {
|
|
796
|
+
type: "Goto",
|
|
797
|
+
target: directive.args.trim(),
|
|
798
|
+
span: spanOf(line),
|
|
799
|
+
targetSourceSpan
|
|
800
|
+
};
|
|
801
|
+
blocks.push(node);
|
|
802
|
+
i += 1;
|
|
803
|
+
break;
|
|
804
|
+
}
|
|
805
|
+
case "if": {
|
|
806
|
+
const { block, nextIndex } = parseConditional(lines, i, diagnostics);
|
|
807
|
+
blocks.push(block);
|
|
808
|
+
i = nextIndex;
|
|
809
|
+
break;
|
|
810
|
+
}
|
|
811
|
+
case "variant": {
|
|
812
|
+
const { block, nextIndex } = parseVariant(lines, i, diagnostics);
|
|
813
|
+
blocks.push(block);
|
|
814
|
+
i = nextIndex;
|
|
815
|
+
break;
|
|
816
|
+
}
|
|
817
|
+
case "choice": {
|
|
818
|
+
const { block, nextIndex } = parseChoice(lines, i, diagnostics);
|
|
819
|
+
blocks.push(block);
|
|
820
|
+
i = nextIndex;
|
|
821
|
+
break;
|
|
822
|
+
}
|
|
823
|
+
case "elseif":
|
|
824
|
+
case "else":
|
|
825
|
+
case "end":
|
|
826
|
+
case "when":
|
|
827
|
+
case "otherwise": {
|
|
828
|
+
// A terminator directive outside the construct it belongs to (the
|
|
829
|
+
// matching construct parser above always intercepts these via
|
|
830
|
+
// `terminators`). No dedicated diagnostic code covers this shape
|
|
831
|
+
// of error in v0.1 — skip defensively rather than crash.
|
|
832
|
+
i += 1;
|
|
833
|
+
break;
|
|
834
|
+
}
|
|
835
|
+
default: {
|
|
836
|
+
diagnostics.push(Diagnostics.unknownDirective(directive.name, spanOf(line)));
|
|
837
|
+
i += 1;
|
|
838
|
+
break;
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
flushProse(i);
|
|
843
|
+
return { blocks, nextIndex: i, stoppedBy: null };
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* The deterministic id used for content appearing before the first explicit
|
|
847
|
+
* `@scene` directive — including an entire `@scene`-less prose-only
|
|
848
|
+
* document, where it becomes the only scene. If the author also declares
|
|
849
|
+
* an explicit `@scene main`, that is treated like any other id collision:
|
|
850
|
+
* the existing duplicate-scene validation (AT1003) reports it rather than
|
|
851
|
+
* silently producing two scenes that share an id.
|
|
852
|
+
*/
|
|
853
|
+
export const SYNTHETIC_LEADING_SCENE_ID = "main";
|
|
854
|
+
/** Fallback anchor for a synthetic scene when there is no line to anchor it to (an empty body). */
|
|
855
|
+
const DOCUMENT_START_POSITION = { line: 1, column: 1, offset: 0 };
|
|
856
|
+
/**
|
|
857
|
+
* The exact line-terminator bytes immediately following `position`
|
|
858
|
+
* (docs/CORE_SPEC.md Section 25.35) — read directly from the already-
|
|
859
|
+
* computed `SourceLine.newlineLength` of the physical line `position`
|
|
860
|
+
* falls on, never rescanned from raw source. `""` when that line has no
|
|
861
|
+
* terminator at all (true end-of-file).
|
|
862
|
+
*/
|
|
863
|
+
function lineEndingAt(lines, position) {
|
|
864
|
+
if (lines.length === 0)
|
|
865
|
+
return "";
|
|
866
|
+
// `lines` may be a slice starting after stripped frontmatter (docs/CORE_SPEC.md
|
|
867
|
+
// Section 12) — its own first entry's absolute `.number` (never assumed
|
|
868
|
+
// to be 1) anchors the lookup, so this remains correct regardless of
|
|
869
|
+
// whether `lines` is the full document or a post-frontmatter slice.
|
|
870
|
+
const index = position.line - lines[0].number;
|
|
871
|
+
const line = lines[index];
|
|
872
|
+
if (!line)
|
|
873
|
+
return "";
|
|
874
|
+
return line.newlineLength === 2 ? "\r\n" : line.newlineLength === 1 ? "\n" : "";
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* Splits the story body into scenes. Content appearing before the first
|
|
878
|
+
* `@scene` directive is collected into a leading scene with id
|
|
879
|
+
* `SYNTHETIC_LEADING_SCENE_ID` ("main"). A document with no leading
|
|
880
|
+
* content and no explicit `@scene` at all — including a completely empty
|
|
881
|
+
* source — still gets an (empty) synthetic main scene, so `StoryDocument`
|
|
882
|
+
* always contains at least one scene and `entryScene` always resolves to
|
|
883
|
+
* a real one.
|
|
884
|
+
*/
|
|
885
|
+
export function parseScenes(lines, diagnostics) {
|
|
886
|
+
const scenes = [];
|
|
887
|
+
const leading = parseBlockSequence(lines, 0, diagnostics, new Set());
|
|
888
|
+
if (leading.blocks.length > 0) {
|
|
889
|
+
const end = leading.blocks[leading.blocks.length - 1].span.end;
|
|
890
|
+
scenes.push({
|
|
891
|
+
type: "Scene",
|
|
892
|
+
id: SYNTHETIC_LEADING_SCENE_ID,
|
|
893
|
+
blocks: leading.blocks,
|
|
894
|
+
span: {
|
|
895
|
+
start: lineStart(lines[0]),
|
|
896
|
+
end
|
|
897
|
+
},
|
|
898
|
+
followingLineEnding: lineEndingAt(lines, end)
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
let i = leading.nextIndex;
|
|
902
|
+
while (i < lines.length) {
|
|
903
|
+
const sceneLine = lines[i];
|
|
904
|
+
const directive = matchDirectiveLine(sceneLine.text);
|
|
905
|
+
const sceneId = directive.args.trim();
|
|
906
|
+
i += 1;
|
|
907
|
+
const seq = parseBlockSequence(lines, i, diagnostics, new Set());
|
|
908
|
+
const span = {
|
|
909
|
+
start: lineStart(sceneLine),
|
|
910
|
+
end: seq.blocks.length > 0 ? seq.blocks[seq.blocks.length - 1].span.end : lineEnd(sceneLine)
|
|
911
|
+
};
|
|
912
|
+
scenes.push({ type: "Scene", id: sceneId, blocks: seq.blocks, span, followingLineEnding: lineEndingAt(lines, span.end) });
|
|
913
|
+
i = seq.nextIndex;
|
|
914
|
+
}
|
|
915
|
+
if (scenes.length === 0) {
|
|
916
|
+
const start = lines.length > 0 ? lineStart(lines[0]) : DOCUMENT_START_POSITION;
|
|
917
|
+
scenes.push({
|
|
918
|
+
type: "Scene",
|
|
919
|
+
id: SYNTHETIC_LEADING_SCENE_ID,
|
|
920
|
+
blocks: [],
|
|
921
|
+
span: { start, end: start },
|
|
922
|
+
followingLineEnding: lineEndingAt(lines, start)
|
|
923
|
+
});
|
|
924
|
+
}
|
|
925
|
+
return scenes;
|
|
926
|
+
}
|
|
927
|
+
//# sourceMappingURL=blocks.js.map
|