@aftertext/compiler 0.1.1 → 0.4.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/README.md +1 -1
- package/dist/ast/index.d.ts +2 -2
- package/dist/ast/index.d.ts.map +1 -1
- package/dist/ast/presentation.d.ts +18 -4
- package/dist/ast/presentation.d.ts.map +1 -1
- package/dist/ast/presentation.js +4 -3
- package/dist/ast/presentation.js.map +1 -1
- package/dist/ast/story.d.ts +11 -1
- package/dist/ast/story.d.ts.map +1 -1
- package/dist/diagnostics/codes.d.ts +5 -0
- package/dist/diagnostics/codes.d.ts.map +1 -1
- package/dist/diagnostics/codes.js +22 -1
- package/dist/diagnostics/codes.js.map +1 -1
- package/dist/diagnostics/types.d.ts +1 -1
- package/dist/diagnostics/types.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/limits.d.ts +16 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/limits.js +16 -0
- package/dist/limits.js.map +1 -0
- package/dist/parser/blocks.d.ts +1 -1
- package/dist/parser/blocks.d.ts.map +1 -1
- package/dist/parser/blocks.js +389 -101
- package/dist/parser/blocks.js.map +1 -1
- package/dist/parser/directive-line.d.ts +8 -2
- package/dist/parser/directive-line.d.ts.map +1 -1
- package/dist/parser/directive-line.js +23 -6
- package/dist/parser/directive-line.js.map +1 -1
- package/dist/parser/expression-parser.d.ts.map +1 -1
- package/dist/parser/expression-parser.js +57 -15
- package/dist/parser/expression-parser.js.map +1 -1
- package/dist/parser/frontmatter.d.ts.map +1 -1
- package/dist/parser/frontmatter.js +24 -6
- package/dist/parser/frontmatter.js.map +1 -1
- package/dist/parser/lines.d.ts.map +1 -1
- package/dist/parser/lines.js +9 -2
- package/dist/parser/lines.js.map +1 -1
- package/dist/parser/markdown.d.ts.map +1 -1
- package/dist/parser/markdown.js +37 -11
- package/dist/parser/markdown.js.map +1 -1
- package/dist/validation/validate.d.ts.map +1 -1
- package/dist/validation/validate.js +2 -1
- package/dist/validation/validate.js.map +1 -1
- package/package.json +1 -2
package/dist/parser/blocks.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Diagnostics } from "../diagnostics/codes.js";
|
|
2
|
-
import { matchDirectiveLine, SIMPLE_PRESENTATION_DIRECTIVES } from "./directive-line.js";
|
|
2
|
+
import { matchDirectiveLine, REGISTERED_DIRECTIVES, SIMPLE_PRESENTATION_DIRECTIVES } from "./directive-line.js";
|
|
3
3
|
import { joinLineText, lineEnd, lineStart, positionAt } from "./lines.js";
|
|
4
|
+
import { MAX_BLOCK_DEPTH } from "../limits.js";
|
|
4
5
|
import { parseProseChunk } from "./markdown.js";
|
|
5
6
|
import { ExpressionSyntaxError, parseExpression } from "./expression-parser.js";
|
|
6
7
|
import { RESERVED_VARIABLE_NAMES } from "./expression-lexer.js";
|
|
@@ -47,10 +48,19 @@ function tryParseExpression(argsText, line, argsStartColumn, diagnostics) {
|
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
50
|
const PARAM_TOKEN_PATTERN = /^([A-Za-z_][A-Za-z0-9_]*)=(.+)$/;
|
|
51
|
+
/**
|
|
52
|
+
* A string-keyed dictionary with no prototype, so an author-supplied key such
|
|
53
|
+
* as `constructor` or `__proto__` is ordinary data: it is never confused with
|
|
54
|
+
* an inherited property and assigning it never changes the dictionary's
|
|
55
|
+
* prototype.
|
|
56
|
+
*/
|
|
57
|
+
function dictionary() {
|
|
58
|
+
return Object.create(null);
|
|
59
|
+
}
|
|
50
60
|
function tokenizePresentationArgs(argsText) {
|
|
51
|
-
const params =
|
|
52
|
-
const paramValueRanges =
|
|
53
|
-
const paramOccurrences =
|
|
61
|
+
const params = dictionary();
|
|
62
|
+
const paramValueRanges = dictionary();
|
|
63
|
+
const paramOccurrences = dictionary();
|
|
54
64
|
const positional = [];
|
|
55
65
|
const positionalRanges = [];
|
|
56
66
|
const tokens = [];
|
|
@@ -168,7 +178,7 @@ function tokenizePresentationArgs(argsText) {
|
|
|
168
178
|
// no range is produced for that key at all. Reuses the token adjacency
|
|
169
179
|
// already scanned above (one extra pass over the small in-memory
|
|
170
180
|
// `tokens` array — not a second scan of `argsText`, not a new grammar).
|
|
171
|
-
const paramRemovalRanges =
|
|
181
|
+
const paramRemovalRanges = dictionary();
|
|
172
182
|
tokens.forEach((tok, index) => {
|
|
173
183
|
if (tok.key === undefined || paramOccurrences[tok.key] !== 1)
|
|
174
184
|
return;
|
|
@@ -241,6 +251,9 @@ function parseNumericParam(text) {
|
|
|
241
251
|
return NUMBER_PATTERN.test(text) ? Number(text) : undefined;
|
|
242
252
|
}
|
|
243
253
|
const KNOWN_CAMERA_ACTIONS = new Set(["zoom"]);
|
|
254
|
+
/** Not an AST type — unlike Camera's `action`, Sprite's action is encoded entirely by the command's `type` discriminant (docs/CORE_SPEC.md Section 31.2). */
|
|
255
|
+
const KNOWN_SPRITE_ACTIONS = new Set(["show", "hide"]);
|
|
256
|
+
const KNOWN_SPRITE_POSITIONS = new Set(["left", "center", "right"]);
|
|
244
257
|
/**
|
|
245
258
|
* Parses, validates, and normalizes a presentation directive into a typed
|
|
246
259
|
* PresentationCommand (see ast/presentation.ts). Unit strings like `6s` and
|
|
@@ -271,23 +284,25 @@ function parsePresentation(kind, argsText, span, line, argsStartColumn, diagnost
|
|
|
271
284
|
// Absolute source spans for every recognized parameter value (docs/CORE_SPEC.md
|
|
272
285
|
// Section 25.36) — computed once, reused by every return below, always
|
|
273
286
|
// present (an empty record when the directive has no named parameters).
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
287
|
+
// Built with `Object.fromEntries` (own data properties) so a key such as
|
|
288
|
+
// `__proto__` is recorded rather than silently dropped.
|
|
289
|
+
const parameterValueSpans = Object.fromEntries(Object.entries(paramValueRanges).map(([key, range]) => [
|
|
290
|
+
key,
|
|
291
|
+
{
|
|
277
292
|
start: positionAt(line, argsStartColumn + range.start),
|
|
278
293
|
end: positionAt(line, argsStartColumn + range.end)
|
|
279
|
-
}
|
|
280
|
-
|
|
294
|
+
}
|
|
295
|
+
]));
|
|
281
296
|
// Absolute, duplicate-safe removal spans (docs/CORE_SPEC.md Section
|
|
282
297
|
// 14) — present only for a key with exactly one authored occurrence;
|
|
283
298
|
// computed once, reused by every return below.
|
|
284
|
-
const parameterRemovalSpans =
|
|
285
|
-
|
|
286
|
-
|
|
299
|
+
const parameterRemovalSpans = Object.fromEntries(Object.entries(paramRemovalRanges).map(([key, range]) => [
|
|
300
|
+
key,
|
|
301
|
+
{
|
|
287
302
|
start: positionAt(line, argsStartColumn + range.start),
|
|
288
303
|
end: positionAt(line, argsStartColumn + range.end)
|
|
289
|
-
}
|
|
290
|
-
|
|
304
|
+
}
|
|
305
|
+
]));
|
|
291
306
|
// The positional value has a safe, contiguous source span only when
|
|
292
307
|
// there is exactly one positional token (docs/CORE_SPEC.md Section
|
|
293
308
|
// 14) — with zero or two-or-more, `value` either doesn't exist or is a
|
|
@@ -442,9 +457,56 @@ function parsePresentation(kind, argsText, span, line, argsStartColumn, diagnost
|
|
|
442
457
|
commandRemovalSpan
|
|
443
458
|
};
|
|
444
459
|
}
|
|
460
|
+
case "sprite": {
|
|
461
|
+
if (value === undefined)
|
|
462
|
+
return fail("@sprite requires an action");
|
|
463
|
+
if (!KNOWN_SPRITE_ACTIONS.has(value)) {
|
|
464
|
+
return fail(`unsupported sprite action "${value}"`);
|
|
465
|
+
}
|
|
466
|
+
if (params["id"] === undefined)
|
|
467
|
+
return fail(`@sprite ${value} requires "id="`);
|
|
468
|
+
const id = params["id"];
|
|
469
|
+
if (value === "hide") {
|
|
470
|
+
return {
|
|
471
|
+
type: "Presentation",
|
|
472
|
+
command: { type: "HideSprite", id },
|
|
473
|
+
span,
|
|
474
|
+
parameterValueSpans,
|
|
475
|
+
positionalValueSpan,
|
|
476
|
+
argumentsEndPosition,
|
|
477
|
+
parameterRemovalSpans,
|
|
478
|
+
contiguousPositionalSpan,
|
|
479
|
+
commandRemovalSpan
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
// value === "show" — the only other member of KNOWN_SPRITE_ACTIONS.
|
|
483
|
+
if (params["image"] === undefined)
|
|
484
|
+
return fail('@sprite show requires "image="');
|
|
485
|
+
const image = params["image"];
|
|
486
|
+
if (params["position"] === undefined)
|
|
487
|
+
return fail('@sprite show requires "position="');
|
|
488
|
+
if (!KNOWN_SPRITE_POSITIONS.has(params["position"])) {
|
|
489
|
+
return fail(`unsupported position "${params["position"]}"`);
|
|
490
|
+
}
|
|
491
|
+
const position = params["position"];
|
|
492
|
+
return {
|
|
493
|
+
type: "Presentation",
|
|
494
|
+
command: { type: "ShowSprite", id, image, position },
|
|
495
|
+
span,
|
|
496
|
+
parameterValueSpans,
|
|
497
|
+
positionalValueSpan,
|
|
498
|
+
argumentsEndPosition,
|
|
499
|
+
parameterRemovalSpans,
|
|
500
|
+
contiguousPositionalSpan,
|
|
501
|
+
commandRemovalSpan
|
|
502
|
+
};
|
|
503
|
+
}
|
|
445
504
|
}
|
|
446
505
|
}
|
|
447
|
-
|
|
506
|
+
// `\S.*` (not `.+`) after the separator keeps `\s*` and the expression from
|
|
507
|
+
// overlapping, so the pattern cannot backtrack across whitespace. `args` are
|
|
508
|
+
// trimmed and never contain a line terminator, so it accepts the same lines.
|
|
509
|
+
const SET_PATTERN = /^([A-Za-z_]\w*)\s*=\s*(\S.*)$/;
|
|
448
510
|
function parseSet(directive, line, diagnostics) {
|
|
449
511
|
const match = SET_PATTERN.exec(directive.args);
|
|
450
512
|
if (!match) {
|
|
@@ -486,6 +548,168 @@ function parseSet(directive, line, diagnostics) {
|
|
|
486
548
|
throw err;
|
|
487
549
|
}
|
|
488
550
|
}
|
|
551
|
+
// ---------------------------------------------------------------------------
|
|
552
|
+
// Dialogue: @say
|
|
553
|
+
// ---------------------------------------------------------------------------
|
|
554
|
+
/**
|
|
555
|
+
* Recognizes a Dialogue header token (docs/CORE_SPEC.md Section 32.6).
|
|
556
|
+
* Deliberately `(.*)` rather than Presentation's `(.+)` (`PARAM_TOKEN_PATTERN`
|
|
557
|
+
* above): `speaker=` with nothing after it must still be recognized as *the*
|
|
558
|
+
* speaker token, with an empty value, so it is diagnosed as "missing/empty
|
|
559
|
+
* speaker" (category 1) rather than falling through to "unsupported header
|
|
560
|
+
* token" (category 2) — Section 32.11's locked diagnostic precedence.
|
|
561
|
+
*/
|
|
562
|
+
const SAY_SPEAKER_TOKEN_PATTERN = /^speaker=(.*)$/;
|
|
563
|
+
const SAY_SPEAKER_PREFIX_LENGTH = "speaker=".length;
|
|
564
|
+
/**
|
|
565
|
+
* Parses `@say`'s header argument text (docs/CORE_SPEC.md Section 32.6/32.7).
|
|
566
|
+
* A small, Dialogue-local scanner — deliberately NOT `tokenizePresentationArgs`,
|
|
567
|
+
* which Section 14 locks to the seven Presentation directive families only
|
|
568
|
+
* and which Dialogue (not a PresentationCommand, Section 32.12) must not
|
|
569
|
+
* extend. Reuses only the shared `SEPARATOR_WHITESPACE_PATTERN` token-boundary
|
|
570
|
+
* primitive both already rely on. `@say` has no positional-argument token at
|
|
571
|
+
* all (Section 32.6): every token must be `speaker=...`-shaped, or the whole
|
|
572
|
+
* header is malformed. A duplicate `speaker=` is not an error — the last
|
|
573
|
+
* occurrence wins for both the semantic value and `speakerSourceSpan`
|
|
574
|
+
* (Section 32.6).
|
|
575
|
+
*/
|
|
576
|
+
function parseDialogueHeader(directive, line, diagnostics) {
|
|
577
|
+
const argsText = directive.args;
|
|
578
|
+
let speakerValue;
|
|
579
|
+
let speakerValueStart = -1;
|
|
580
|
+
let speakerValueEnd = -1;
|
|
581
|
+
let unsupportedToken;
|
|
582
|
+
let pos = 0;
|
|
583
|
+
while (pos < argsText.length) {
|
|
584
|
+
while (pos < argsText.length && SEPARATOR_WHITESPACE_PATTERN.test(argsText[pos]))
|
|
585
|
+
pos += 1;
|
|
586
|
+
if (pos >= argsText.length)
|
|
587
|
+
break;
|
|
588
|
+
const tokenStart = pos;
|
|
589
|
+
while (pos < argsText.length && !SEPARATOR_WHITESPACE_PATTERN.test(argsText[pos]))
|
|
590
|
+
pos += 1;
|
|
591
|
+
const tokenEnd = pos;
|
|
592
|
+
const match = SAY_SPEAKER_TOKEN_PATTERN.exec(argsText.slice(tokenStart, tokenEnd));
|
|
593
|
+
if (match) {
|
|
594
|
+
// Last-occurrence-wins (docs/CORE_SPEC.md Section 32.6), mirroring the
|
|
595
|
+
// compiler's existing duplicate-named-parameter convention (Section 14).
|
|
596
|
+
speakerValue = match[1];
|
|
597
|
+
speakerValueStart = tokenStart + SAY_SPEAKER_PREFIX_LENGTH;
|
|
598
|
+
speakerValueEnd = tokenEnd;
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
// First unsupported token only — Section 32.11's diagnostic precedence
|
|
602
|
+
// reports at most one AT1301 per malformed `@say`, so later unsupported
|
|
603
|
+
// tokens (if any) are never separately diagnosed.
|
|
604
|
+
unsupportedToken ??= { start: tokenStart, end: tokenEnd };
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
// Category 1 (missing/empty speaker) takes precedence over category 2
|
|
608
|
+
// (unsupported header token) regardless of token order — Section 32.11.
|
|
609
|
+
if (speakerValue === undefined || speakerValue.trim().length === 0) {
|
|
610
|
+
diagnostics.push(Diagnostics.malformedDialogue("missing or empty speaker", spanOf(line)));
|
|
611
|
+
return { ok: false, speaker: "", speakerSourceSpan: undefined };
|
|
612
|
+
}
|
|
613
|
+
if (unsupportedToken) {
|
|
614
|
+
diagnostics.push(Diagnostics.malformedDialogue("unsupported header token", {
|
|
615
|
+
start: positionAt(line, directive.argsStartColumn + unsupportedToken.start),
|
|
616
|
+
end: positionAt(line, directive.argsStartColumn + unsupportedToken.end)
|
|
617
|
+
}));
|
|
618
|
+
return { ok: false, speaker: "", speakerSourceSpan: undefined };
|
|
619
|
+
}
|
|
620
|
+
return {
|
|
621
|
+
ok: true,
|
|
622
|
+
speaker: speakerValue,
|
|
623
|
+
speakerSourceSpan: {
|
|
624
|
+
start: positionAt(line, directive.argsStartColumn + speakerValueStart),
|
|
625
|
+
end: positionAt(line, directive.argsStartColumn + speakerValueEnd)
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
/** A zero-length line, or a line containing only spaces and/or tabs (docs/CORE_SPEC.md Section 32.3's exact blank-line predicate). Deliberately `/^[ \t]*$/`, not `.trim()`: `String.prototype.trim()` also strips other Unicode whitespace (e.g. U+00A0, U+2028/U+2029) that Section 32.3 does not — a line containing only such characters must remain non-blank Markdown content, not leading trivia or a body terminator. */
|
|
630
|
+
const BLANK_LINE_PATTERN = /^[ \t]*$/;
|
|
631
|
+
function isBlankLine(line) {
|
|
632
|
+
return BLANK_LINE_PATTERN.test(line.text);
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Parses one Dialogue block starting at the `@say` header line at `openIndex`
|
|
636
|
+
* (docs/CORE_SPEC.md Section 32). Body-boundary scan (Section 32.3) always
|
|
637
|
+
* runs, even when the header is malformed, so recovery is deterministic
|
|
638
|
+
* (Section 32.11): the complete claimed extent — header plus every scanned
|
|
639
|
+
* body line — is dropped together on any failure; the terminating blank
|
|
640
|
+
* line, directive line, `@scene`, or EOF is never consumed.
|
|
641
|
+
*/
|
|
642
|
+
function parseDialogue(lines, openIndex, diagnostics) {
|
|
643
|
+
const headerLine = lines[openIndex];
|
|
644
|
+
const directive = matchDirectiveLine(headerLine.text);
|
|
645
|
+
const header = parseDialogueHeader(directive, headerLine, diagnostics);
|
|
646
|
+
let i = openIndex + 1;
|
|
647
|
+
while (i < lines.length && isBlankLine(lines[i]))
|
|
648
|
+
i += 1; // leading trivia (Section 32.3 step 1)
|
|
649
|
+
const bodyStart = i;
|
|
650
|
+
while (i < lines.length &&
|
|
651
|
+
!isBlankLine(lines[i]) &&
|
|
652
|
+
!matchDirectiveLine(lines[i].text) // a directive line, @scene included, ends the body
|
|
653
|
+
) {
|
|
654
|
+
i += 1;
|
|
655
|
+
}
|
|
656
|
+
const bodyEnd = i;
|
|
657
|
+
if (!header.ok) {
|
|
658
|
+
return { block: undefined, nextIndex: bodyEnd };
|
|
659
|
+
}
|
|
660
|
+
if (bodyStart === bodyEnd) {
|
|
661
|
+
const headerEnd = lineEnd(headerLine);
|
|
662
|
+
diagnostics.push(Diagnostics.malformedDialogue("missing dialogue content", { start: headerEnd, end: headerEnd }));
|
|
663
|
+
return { block: undefined, nextIndex: bodyEnd };
|
|
664
|
+
}
|
|
665
|
+
const bodyLines = lines.slice(bodyStart, bodyEnd);
|
|
666
|
+
const first = bodyLines[0];
|
|
667
|
+
const text = joinLineText(bodyLines);
|
|
668
|
+
// Buffered, not pushed to `diagnostics` directly: `parseProseChunk` can
|
|
669
|
+
// emit its own warnings (e.g. AT3001 for an unsupported construct falling
|
|
670
|
+
// back to plain text) as a side effect of parsing, independently of
|
|
671
|
+
// whether the resulting blocks even satisfy Dialogue's cardinality rule.
|
|
672
|
+
// Section 32.11 locks exactly one diagnostic per malformed `@say` — so
|
|
673
|
+
// these are discarded on every malformed-body path below and merged into
|
|
674
|
+
// `diagnostics` only once the body is confirmed to be a valid single
|
|
675
|
+
// Paragraph.
|
|
676
|
+
const bodyDiagnostics = [];
|
|
677
|
+
const blocks = parseProseChunk(text, { startLine: first.number, startOffset: first.startOffset }, bodyDiagnostics);
|
|
678
|
+
if (blocks.length === 0) {
|
|
679
|
+
const headerEnd = lineEnd(headerLine);
|
|
680
|
+
diagnostics.push(Diagnostics.malformedDialogue("missing dialogue content", { start: headerEnd, end: headerEnd }));
|
|
681
|
+
return { block: undefined, nextIndex: bodyEnd };
|
|
682
|
+
}
|
|
683
|
+
if (blocks.length > 1) {
|
|
684
|
+
// The complete claimed body extent (Section 32.11) is the physical
|
|
685
|
+
// bodyLines range, not the first/last parsed block's own span — a
|
|
686
|
+
// parsed block's span can start after leading indentation, which would
|
|
687
|
+
// otherwise silently exclude claimed body trivia from the diagnostic.
|
|
688
|
+
diagnostics.push(Diagnostics.malformedDialogue("dialogue content must be a single paragraph", {
|
|
689
|
+
start: lineStart(first),
|
|
690
|
+
end: lineEnd(bodyLines.at(-1))
|
|
691
|
+
}));
|
|
692
|
+
return { block: undefined, nextIndex: bodyEnd };
|
|
693
|
+
}
|
|
694
|
+
const only = blocks[0];
|
|
695
|
+
if (only.type !== "Paragraph") {
|
|
696
|
+
diagnostics.push(Diagnostics.malformedDialogue("dialogue content must be a paragraph, not a heading", only.span));
|
|
697
|
+
return { block: undefined, nextIndex: bodyEnd };
|
|
698
|
+
}
|
|
699
|
+
for (const diagnostic of bodyDiagnostics)
|
|
700
|
+
diagnostics.push(diagnostic);
|
|
701
|
+
return {
|
|
702
|
+
block: {
|
|
703
|
+
type: "Dialogue",
|
|
704
|
+
speaker: header.speaker,
|
|
705
|
+
content: only.children,
|
|
706
|
+
span: { start: lineStart(headerLine), end: only.span.end },
|
|
707
|
+
speakerSourceSpan: header.speakerSourceSpan,
|
|
708
|
+
contentSourceSpan: only.span
|
|
709
|
+
},
|
|
710
|
+
nextIndex: bodyEnd
|
|
711
|
+
};
|
|
712
|
+
}
|
|
489
713
|
/** Bullet marker introducing a choice item: `* ...` or `- ...`. */
|
|
490
714
|
const CHOICE_BULLET_PATTERN = /^(\s*)[*-]\s+/;
|
|
491
715
|
/**
|
|
@@ -511,6 +735,16 @@ function parseChoice(lines, openIndex, diagnostics) {
|
|
|
511
735
|
if (directive && (directive.name === "end" || directive.name === "scene")) {
|
|
512
736
|
break;
|
|
513
737
|
}
|
|
738
|
+
// A directive-shaped line inside a choice body can never be an item, and
|
|
739
|
+
// is skipped like any non-item line — but it is authored structure, so it
|
|
740
|
+
// is reported (unlike ordinary narrative text, which stays a silent skip).
|
|
741
|
+
if (directive) {
|
|
742
|
+
diagnostics.push(REGISTERED_DIRECTIVES.has(directive.name)
|
|
743
|
+
? Diagnostics.misplacedDirective(directive.name, "inside a choice", spanOf(line))
|
|
744
|
+
: Diagnostics.unknownDirective(directive.name, spanOf(line)));
|
|
745
|
+
idx += 1;
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
514
748
|
const bulletMatch = CHOICE_BULLET_PATTERN.exec(line.text);
|
|
515
749
|
if (!bulletMatch) {
|
|
516
750
|
idx += 1;
|
|
@@ -534,7 +768,7 @@ function parseChoice(lines, openIndex, diagnostics) {
|
|
|
534
768
|
// Display text is only the "if" delimiter's own condition-grammar is
|
|
535
769
|
// applied to the tail *after* the target — so an "if" appearing in
|
|
536
770
|
// display text (before the arrow) can never be mistaken for one.
|
|
537
|
-
const displayText = content.slice(0, arrowIndex).trim().
|
|
771
|
+
const displayText = content.slice(0, arrowIndex).trim().replaceAll(String.raw `\->`, "->");
|
|
538
772
|
const tailStart = arrowIndex + 2;
|
|
539
773
|
const tailRaw = content.slice(tailStart);
|
|
540
774
|
const tailLeadingWs = tailRaw.length - tailRaw.trimStart().length;
|
|
@@ -594,7 +828,7 @@ function parseChoice(lines, openIndex, diagnostics) {
|
|
|
594
828
|
nextIndex: idx
|
|
595
829
|
};
|
|
596
830
|
}
|
|
597
|
-
function parseConditional(lines, openIndex, diagnostics) {
|
|
831
|
+
function parseConditional(lines, openIndex, diagnostics, depth) {
|
|
598
832
|
const openLine = lines[openIndex];
|
|
599
833
|
const branches = [];
|
|
600
834
|
let idx = openIndex;
|
|
@@ -613,7 +847,7 @@ function parseConditional(lines, openIndex, diagnostics) {
|
|
|
613
847
|
}
|
|
614
848
|
: undefined;
|
|
615
849
|
idx += 1;
|
|
616
|
-
const seq = parseBlockSequence(lines, idx, diagnostics, new Set(["elseif", "else", "end"]));
|
|
850
|
+
const seq = parseBlockSequence(lines, idx, diagnostics, new Set(["elseif", "else", "end"]), depth + 1);
|
|
617
851
|
const branchSpan = {
|
|
618
852
|
start: lineStart(branchLine),
|
|
619
853
|
end: seq.blocks.length > 0 ? seq.blocks[seq.blocks.length - 1].span.end : lineEnd(branchLine)
|
|
@@ -648,7 +882,7 @@ function parseConditional(lines, openIndex, diagnostics) {
|
|
|
648
882
|
nextIndex: idx
|
|
649
883
|
};
|
|
650
884
|
}
|
|
651
|
-
function parseVariant(lines, openIndex, diagnostics) {
|
|
885
|
+
function parseVariant(lines, openIndex, diagnostics, depth) {
|
|
652
886
|
const openLine = lines[openIndex];
|
|
653
887
|
const openDirective = matchDirectiveLine(openLine.text);
|
|
654
888
|
const variantId = openDirective.args.trim();
|
|
@@ -669,8 +903,17 @@ function parseVariant(lines, openIndex, diagnostics) {
|
|
|
669
903
|
let whenCount = 0;
|
|
670
904
|
let lastEnd = lineEnd(openLine);
|
|
671
905
|
let idx = openIndex + 1;
|
|
672
|
-
let seq = parseBlockSequence(lines, idx, diagnostics, new Set(["when", "otherwise", "end"]));
|
|
906
|
+
let seq = parseBlockSequence(lines, idx, diagnostics, new Set(["when", "otherwise", "end"]), depth + 1);
|
|
673
907
|
idx = seq.nextIndex;
|
|
908
|
+
// Content between the `@variant` header and the first branch belongs to no
|
|
909
|
+
// branch and is not kept in the AST; report the range that is ignored.
|
|
910
|
+
const ignored = seq.blocks;
|
|
911
|
+
if (ignored.length > 0) {
|
|
912
|
+
diagnostics.push(Diagnostics.variantContentBeforeBranch({
|
|
913
|
+
start: ignored[0].span.start,
|
|
914
|
+
end: ignored.at(-1).span.end
|
|
915
|
+
}));
|
|
916
|
+
}
|
|
674
917
|
while (seq.stoppedBy === "when" || seq.stoppedBy === "otherwise") {
|
|
675
918
|
const kind = seq.stoppedBy;
|
|
676
919
|
const branchLine = lines[idx];
|
|
@@ -694,7 +937,7 @@ function parseVariant(lines, openIndex, diagnostics) {
|
|
|
694
937
|
}
|
|
695
938
|
: undefined;
|
|
696
939
|
idx += 1;
|
|
697
|
-
const bodySeq = parseBlockSequence(lines, idx, diagnostics, new Set(["when", "otherwise", "end"]));
|
|
940
|
+
const bodySeq = parseBlockSequence(lines, idx, diagnostics, new Set(["when", "otherwise", "end"]), depth + 1);
|
|
698
941
|
const branchSpan = {
|
|
699
942
|
start: lineStart(branchLine),
|
|
700
943
|
end: bodySeq.blocks.length > 0
|
|
@@ -731,6 +974,47 @@ function parseVariant(lines, openIndex, diagnostics) {
|
|
|
731
974
|
nextIndex: idx
|
|
732
975
|
};
|
|
733
976
|
}
|
|
977
|
+
/**
|
|
978
|
+
* Returns the index just past the `@end` that closes the construct opened at
|
|
979
|
+
* `openIndex`, counting nested `@if` / `@variant` / `@choice` openers (a `@choice` body is opaque
|
|
980
|
+
* up to its first `@end`, matching `parseChoice`). Stops
|
|
981
|
+
* early at a `@scene` line (scenes bound every construct) or end of input.
|
|
982
|
+
* Iterative on purpose: it is used on input too deep to recurse over.
|
|
983
|
+
*/
|
|
984
|
+
function skipConstruct(lines, openIndex) {
|
|
985
|
+
let level = 0;
|
|
986
|
+
let inChoice = false;
|
|
987
|
+
for (let j = openIndex; j < lines.length; j++) {
|
|
988
|
+
const name = matchDirectiveLine(lines[j].text)?.name;
|
|
989
|
+
if (name === undefined)
|
|
990
|
+
continue;
|
|
991
|
+
if (name === "scene" && j > openIndex)
|
|
992
|
+
return j;
|
|
993
|
+
const step = skipStep(name, inChoice);
|
|
994
|
+
level += step.delta;
|
|
995
|
+
inChoice = step.inChoice;
|
|
996
|
+
if (step.delta < 0 && level === 0)
|
|
997
|
+
return j + 1;
|
|
998
|
+
}
|
|
999
|
+
return lines.length;
|
|
1000
|
+
}
|
|
1001
|
+
/**
|
|
1002
|
+
* How one directive changes `skipConstruct`'s scan: the nesting-level delta
|
|
1003
|
+
* and whether it is inside a `@choice` body. `parseChoice` treats its body as
|
|
1004
|
+
* opaque up to the first `@end`, so directive-shaped lines inside it open
|
|
1005
|
+
* nothing here either.
|
|
1006
|
+
*/
|
|
1007
|
+
function skipStep(name, inChoice) {
|
|
1008
|
+
if (name === "end")
|
|
1009
|
+
return { delta: -1, inChoice: false };
|
|
1010
|
+
if (inChoice)
|
|
1011
|
+
return { delta: 0, inChoice };
|
|
1012
|
+
if (name === "choice")
|
|
1013
|
+
return { delta: 1, inChoice: true };
|
|
1014
|
+
if (name === "if" || name === "variant")
|
|
1015
|
+
return { delta: 1, inChoice };
|
|
1016
|
+
return { delta: 0, inChoice };
|
|
1017
|
+
}
|
|
734
1018
|
/**
|
|
735
1019
|
* Parses a run of lines into StoryBlocks until either a directive in
|
|
736
1020
|
* `terminators` is reached, a `@scene` line is reached (scenes always
|
|
@@ -740,23 +1024,18 @@ function parseVariant(lines, openIndex, diagnostics) {
|
|
|
740
1024
|
* conditional/variant branch bodies) — nesting falls out of recursive
|
|
741
1025
|
* calls to parseConditional/parseVariant/parseChoice below.
|
|
742
1026
|
*/
|
|
743
|
-
export function parseBlockSequence(lines, start, diagnostics, terminators) {
|
|
1027
|
+
export function parseBlockSequence(lines, start, diagnostics, terminators, depth = 0) {
|
|
744
1028
|
const blocks = [];
|
|
745
1029
|
let i = start;
|
|
746
1030
|
let proseStart = -1;
|
|
747
1031
|
const flushProse = (endExclusive) => {
|
|
748
|
-
if (proseStart
|
|
749
|
-
|
|
750
|
-
|
|
1032
|
+
if (proseStart !== -1) {
|
|
1033
|
+
// Not `push(...run)`: a spread passes every element as a call argument,
|
|
1034
|
+
// which overflows the engine's argument/stack limit for a very long run.
|
|
1035
|
+
for (const block of parseProseRun(lines, proseStart, endExclusive, diagnostics))
|
|
1036
|
+
blocks.push(block);
|
|
1037
|
+
}
|
|
751
1038
|
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
1039
|
};
|
|
761
1040
|
while (i < lines.length) {
|
|
762
1041
|
const line = lines[i];
|
|
@@ -767,80 +1046,83 @@ export function parseBlockSequence(lines, start, diagnostics, terminators) {
|
|
|
767
1046
|
i += 1;
|
|
768
1047
|
continue;
|
|
769
1048
|
}
|
|
1049
|
+
flushProse(i);
|
|
770
1050
|
if (directive.name === "scene" || terminators.has(directive.name)) {
|
|
771
|
-
flushProse(i);
|
|
772
1051
|
return { blocks, nextIndex: i, stoppedBy: directive.name };
|
|
773
1052
|
}
|
|
774
|
-
|
|
775
|
-
if (
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
1053
|
+
const parsed = parseDirectiveBlock(directive, lines, i, diagnostics, depth);
|
|
1054
|
+
if (parsed.block)
|
|
1055
|
+
blocks.push(parsed.block);
|
|
1056
|
+
i = parsed.nextIndex;
|
|
1057
|
+
}
|
|
1058
|
+
flushProse(i);
|
|
1059
|
+
return { blocks, nextIndex: i, stoppedBy: null };
|
|
1060
|
+
}
|
|
1061
|
+
/** Parses the prose lines `[startIndex, endExclusive)` (no directives) into Markdown blocks. */
|
|
1062
|
+
function parseProseRun(lines, startIndex, endExclusive, diagnostics) {
|
|
1063
|
+
const chunkLines = lines.slice(startIndex, endExclusive);
|
|
1064
|
+
if (chunkLines.length === 0)
|
|
1065
|
+
return [];
|
|
1066
|
+
const text = joinLineText(chunkLines);
|
|
1067
|
+
if (text.trim().length === 0)
|
|
1068
|
+
return [];
|
|
1069
|
+
const first = chunkLines[0];
|
|
1070
|
+
return parseProseChunk(text, { startLine: first.number, startOffset: first.startOffset }, diagnostics);
|
|
1071
|
+
}
|
|
1072
|
+
/**
|
|
1073
|
+
* Parses the single directive (or construct) starting at `lines[index]`.
|
|
1074
|
+
* `block` is undefined when the directive produces no node (an invalid
|
|
1075
|
+
* presentation/set, a stray terminator, an unknown directive, or an
|
|
1076
|
+
* over-deep construct that was skipped).
|
|
1077
|
+
*/
|
|
1078
|
+
function parseDirectiveBlock(directive, lines, index, diagnostics, depth) {
|
|
1079
|
+
const line = lines[index];
|
|
1080
|
+
const single = (block) => ({ block, nextIndex: index + 1 });
|
|
1081
|
+
if (SIMPLE_PRESENTATION_SET.has(directive.name)) {
|
|
1082
|
+
return single(parsePresentation(directive.name, directive.args, spanOf(line), line, directive.argsStartColumn, diagnostics));
|
|
1083
|
+
}
|
|
1084
|
+
switch (directive.name) {
|
|
1085
|
+
case "set":
|
|
1086
|
+
return single(parseSet(directive, line, diagnostics));
|
|
1087
|
+
case "goto": {
|
|
1088
|
+
const node = {
|
|
1089
|
+
type: "Goto",
|
|
1090
|
+
target: directive.args.trim(),
|
|
1091
|
+
span: spanOf(line),
|
|
1092
|
+
targetSourceSpan: {
|
|
792
1093
|
start: positionAt(line, directive.argsStartColumn),
|
|
793
1094
|
end: positionAt(line, directive.argsStartColumn + directive.args.length)
|
|
794
|
-
}
|
|
795
|
-
|
|
796
|
-
|
|
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
|
-
}
|
|
1095
|
+
}
|
|
1096
|
+
};
|
|
1097
|
+
return single(node);
|
|
840
1098
|
}
|
|
1099
|
+
case "say":
|
|
1100
|
+
return parseDialogue(lines, index, diagnostics);
|
|
1101
|
+
case "if":
|
|
1102
|
+
case "variant":
|
|
1103
|
+
if (depth >= MAX_BLOCK_DEPTH) {
|
|
1104
|
+
diagnostics.push(Diagnostics.blocksNestedTooDeeply(MAX_BLOCK_DEPTH, spanOf(line)));
|
|
1105
|
+
return { block: undefined, nextIndex: skipConstruct(lines, index) };
|
|
1106
|
+
}
|
|
1107
|
+
return directive.name === "if"
|
|
1108
|
+
? parseConditional(lines, index, diagnostics, depth)
|
|
1109
|
+
: parseVariant(lines, index, diagnostics, depth);
|
|
1110
|
+
case "choice":
|
|
1111
|
+
return parseChoice(lines, index, diagnostics);
|
|
1112
|
+
case "elseif":
|
|
1113
|
+
case "else":
|
|
1114
|
+
case "end":
|
|
1115
|
+
case "when":
|
|
1116
|
+
case "otherwise":
|
|
1117
|
+
// A terminator directive outside the construct it belongs to (the
|
|
1118
|
+
// matching construct parser always intercepts these via
|
|
1119
|
+
// `terminators`). The line is dropped as before; AT1009 reports it.
|
|
1120
|
+
diagnostics.push(Diagnostics.misplacedDirective(directive.name, "here", spanOf(line)));
|
|
1121
|
+
return single(undefined);
|
|
1122
|
+
default:
|
|
1123
|
+
diagnostics.push(Diagnostics.unknownDirective(directive.name, spanOf(line)));
|
|
1124
|
+
return single(undefined);
|
|
841
1125
|
}
|
|
842
|
-
flushProse(i);
|
|
843
|
-
return { blocks, nextIndex: i, stoppedBy: null };
|
|
844
1126
|
}
|
|
845
1127
|
/**
|
|
846
1128
|
* The deterministic id used for content appearing before the first explicit
|
|
@@ -903,6 +1185,12 @@ export function parseScenes(lines, diagnostics) {
|
|
|
903
1185
|
const sceneLine = lines[i];
|
|
904
1186
|
const directive = matchDirectiveLine(sceneLine.text);
|
|
905
1187
|
const sceneId = directive.args.trim();
|
|
1188
|
+
if (sceneId === "") {
|
|
1189
|
+
// Zero-width insertion point right after the `@scene` keyword
|
|
1190
|
+
// (`@` + name), where an id could be written.
|
|
1191
|
+
const insertion = positionAt(sceneLine, directive.name.length + 1);
|
|
1192
|
+
diagnostics.push(Diagnostics.emptySceneId({ start: insertion, end: insertion }));
|
|
1193
|
+
}
|
|
906
1194
|
i += 1;
|
|
907
1195
|
const seq = parseBlockSequence(lines, i, diagnostics, new Set());
|
|
908
1196
|
const span = {
|