@rohal12/spindle 0.62.4 → 0.62.5
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/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +84 -23
- package/dist/pkg/story-variables.js +81 -14
- package/package.json +1 -1
- package/src/code-check.ts +25 -2
- package/src/completion-context.ts +2 -4
- package/src/components/macros/PassageDisplay.tsx +3 -16
- package/src/format/format-passage.ts +3 -3
- package/src/interpolation.ts +3 -3
- package/src/js-bindings.ts +7 -4
- package/src/js-lexer.ts +5 -1
- package/src/markup/code-attributes.ts +3 -4
- package/src/markup/code-end.ts +48 -1
- package/src/markup/pair.ts +14 -2
- package/src/markup/render.tsx +2 -2
- package/src/registry.ts +3 -0
- package/src/saves/save-manager.ts +14 -3
- package/src/store.ts +30 -16
- package/src/story-variables.ts +2 -4
- package/src/tooling.ts +2 -0
- package/src/widgets/ast-scanner.ts +2 -1
- package/types/tooling.d.ts +2 -0
package/src/interpolation.ts
CHANGED
|
@@ -255,9 +255,6 @@ class TextEvaluator {
|
|
|
255
255
|
}
|
|
256
256
|
|
|
257
257
|
private macro(node: MacroNode, scope: TextScope, depth: number): string {
|
|
258
|
-
// Rendering ignores a stray sub-macro too (see renderMacro).
|
|
259
|
-
if (isSubMacro(node.name)) return '';
|
|
260
|
-
|
|
261
258
|
const widget = getWidget(node.name);
|
|
262
259
|
if (widget) {
|
|
263
260
|
let locals = scope.locals;
|
|
@@ -285,6 +282,9 @@ class TextEvaluator {
|
|
|
285
282
|
});
|
|
286
283
|
}
|
|
287
284
|
|
|
285
|
+
// Registered widgets take precedence over stray companion markers.
|
|
286
|
+
if (isSubMacro(node.name)) return '';
|
|
287
|
+
|
|
288
288
|
const text = getMacroText(node.name);
|
|
289
289
|
if (!text) {
|
|
290
290
|
const message = getMacro(node.name)
|
package/src/js-bindings.ts
CHANGED
|
@@ -160,6 +160,7 @@ export class Bindings {
|
|
|
160
160
|
private readGroups(): void {
|
|
161
161
|
const { code, groups } = this;
|
|
162
162
|
const stack: Group[] = [];
|
|
163
|
+
const active: Record<Kind, number[]> = { '{': [], '(': [], '[': [] };
|
|
163
164
|
for (let i = 0; i < code.length; i++) {
|
|
164
165
|
const c = code.charAt(i);
|
|
165
166
|
if (c === '{' || c === '(' || c === '[') {
|
|
@@ -170,16 +171,18 @@ export class Bindings {
|
|
|
170
171
|
parent: stack[stack.length - 1],
|
|
171
172
|
};
|
|
172
173
|
groups.push(group);
|
|
174
|
+
active[c].push(stack.length);
|
|
173
175
|
stack.push(group);
|
|
174
176
|
this.byOpen.set(i, group);
|
|
175
177
|
} else if (CLOSERS[c]) {
|
|
176
178
|
// Tolerate a closer with no opener: the innermost of its kind closes
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (k
|
|
179
|
+
const positions = active[CLOSERS[c]!];
|
|
180
|
+
const k = positions[positions.length - 1];
|
|
181
|
+
if (k !== undefined) {
|
|
180
182
|
stack[k]!.close = i;
|
|
181
183
|
this.byClose.set(i, stack[k]!);
|
|
182
|
-
|
|
184
|
+
// Discard each recovered group from its kind's index once.
|
|
185
|
+
while (stack.length > k) active[stack.pop()!.kind].pop();
|
|
183
186
|
}
|
|
184
187
|
}
|
|
185
188
|
}
|
package/src/js-lexer.ts
CHANGED
|
@@ -905,7 +905,11 @@ export function findCodeEnd(
|
|
|
905
905
|
* class field named `class` inside a template literal) mislead it; the
|
|
906
906
|
* parser knows. Only asked when the scan finds no end.
|
|
907
907
|
*/
|
|
908
|
-
function parsedCodeEnd(
|
|
908
|
+
export function parsedCodeEnd(
|
|
909
|
+
src: string,
|
|
910
|
+
start: number,
|
|
911
|
+
goal: JsGoal,
|
|
912
|
+
): number {
|
|
909
913
|
const p = tokenizerAt(src, start, goal) as ParserState & {
|
|
910
914
|
parse(): unknown;
|
|
911
915
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isSigil } from './tokens';
|
|
2
|
-
import {
|
|
2
|
+
import { indexCodeEnds } from './code-end';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Attributes whose value is code with braces of its own, not text: event
|
|
@@ -33,6 +33,7 @@ export function splitSigilTemplate(
|
|
|
33
33
|
const parts: SigilPart[] = [];
|
|
34
34
|
let text = '';
|
|
35
35
|
let i = 0;
|
|
36
|
+
const closeBrace = indexCodeEnds(template);
|
|
36
37
|
|
|
37
38
|
while (i < template.length) {
|
|
38
39
|
const brace = template.indexOf('{', i);
|
|
@@ -42,9 +43,7 @@ export function splitSigilTemplate(
|
|
|
42
43
|
}
|
|
43
44
|
text += template.slice(i, brace);
|
|
44
45
|
i = brace + 1;
|
|
45
|
-
let end = isSigil(template[i])
|
|
46
|
-
? defaultCodeEnd.closeBrace(template, i, i)
|
|
47
|
-
: -1;
|
|
46
|
+
let end = isSigil(template[i]) ? closeBrace(i) : -1;
|
|
48
47
|
if (end === -1 && incomplete && isSigil(template[i])) end = template.length;
|
|
49
48
|
if (end === -1) {
|
|
50
49
|
text += '{';
|
package/src/markup/code-end.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { findCodeEnd } from '../js-lexer';
|
|
1
|
+
import { findCodeEnd, parsedCodeEnd } from '../js-lexer';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Where the JavaScript inside markup ends. The grammar (spindle.peggy) leaves
|
|
@@ -175,3 +175,50 @@ export function jsCodeEnd(onOpenEnd?: OnOpenCodeEnd): CodeEnd {
|
|
|
175
175
|
|
|
176
176
|
/** The JavaScript reader Spindle uses today: js-lexer, then a lenient scan. */
|
|
177
177
|
export const defaultCodeEnd: CodeEnd = jsCodeEnd();
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Index possible brace ends for repeated recovery attempts in one template.
|
|
181
|
+
* Within plain code, nested braces must close first. At a quote, slash or
|
|
182
|
+
* escape we stop making lexical assumptions, but the remaining raw `}`s
|
|
183
|
+
* still bound how many pending braces could close. Indexing visits each
|
|
184
|
+
* opener once, so unmatched plain-code openers can skip suffix scans.
|
|
185
|
+
*/
|
|
186
|
+
export function indexCodeEnds(input: string): (start: number) => number {
|
|
187
|
+
const possible = new Set<number>();
|
|
188
|
+
const pending: number[] = [];
|
|
189
|
+
let remaining = 0;
|
|
190
|
+
let lastBrace = -1;
|
|
191
|
+
for (let i = 0; i < input.length; i++) {
|
|
192
|
+
if (input[i] === '}') {
|
|
193
|
+
remaining++;
|
|
194
|
+
lastBrace = i;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const uncertain = () => {
|
|
198
|
+
for (
|
|
199
|
+
let i = Math.max(0, pending.length - remaining);
|
|
200
|
+
i < pending.length;
|
|
201
|
+
i++
|
|
202
|
+
) {
|
|
203
|
+
possible.add(pending[i]!);
|
|
204
|
+
}
|
|
205
|
+
pending.length = 0;
|
|
206
|
+
};
|
|
207
|
+
for (let i = 0; i < input.length; i++) {
|
|
208
|
+
const c = input[i]!;
|
|
209
|
+
if (c === '{') pending.push(i);
|
|
210
|
+
else if (c === '}') {
|
|
211
|
+
remaining--;
|
|
212
|
+
const open = pending.pop();
|
|
213
|
+
if (open !== undefined) possible.add(open);
|
|
214
|
+
} else if (/['"`/\\]/.test(c)) uncertain();
|
|
215
|
+
}
|
|
216
|
+
return (start) => {
|
|
217
|
+
if (start > lastBrace) return -1;
|
|
218
|
+
// A syntax error may end the parser's recovery at an inner `}`, even
|
|
219
|
+
// when neither the token reader nor the lenient reader can close here.
|
|
220
|
+
return possible.has(start - 1)
|
|
221
|
+
? defaultCodeEnd.closeBrace(input, start, start)
|
|
222
|
+
: parsedCodeEnd(input, start, 'expression');
|
|
223
|
+
};
|
|
224
|
+
}
|
package/src/markup/pair.ts
CHANGED
|
@@ -199,6 +199,11 @@ export function pairMarkup(
|
|
|
199
199
|
|
|
200
200
|
const root: PairedNode[] = [];
|
|
201
201
|
const stack: Frame[] = [];
|
|
202
|
+
// Positions of active openers, separated by tag kind as well as name.
|
|
203
|
+
// Every frame is indexed once and removed once, including recovery cuts.
|
|
204
|
+
const openPositions = new Map<string, number[]>();
|
|
205
|
+
const tagKey = (tag: MacroToken | HtmlToken) =>
|
|
206
|
+
`${tag.type}:${lowerName(tag)}`;
|
|
202
207
|
const add = (node: PairedNode) => (last(stack)?.into ?? root).push(node);
|
|
203
208
|
|
|
204
209
|
/**
|
|
@@ -212,6 +217,10 @@ export function pairMarkup(
|
|
|
212
217
|
) => {
|
|
213
218
|
const { node, body } = stack.pop()!;
|
|
214
219
|
const open = node.token as MacroToken | HtmlToken;
|
|
220
|
+
const key = tagKey(open);
|
|
221
|
+
const positions = openPositions.get(key)!;
|
|
222
|
+
positions.pop();
|
|
223
|
+
if (positions.length === 0) openPositions.delete(key);
|
|
215
224
|
const noticedAt = close ? close.end : noticedAtEnd;
|
|
216
225
|
for (const { tag } of body.branches) {
|
|
217
226
|
if (
|
|
@@ -261,8 +270,7 @@ export function pairMarkup(
|
|
|
261
270
|
// The closer ends the innermost element, as the runtime reads it;
|
|
262
271
|
// when it names one further out, it ends that one, and the elements
|
|
263
272
|
// inside it are left unclosed
|
|
264
|
-
|
|
265
|
-
while (outer >= 0 && !closes(stack[outer]!.node.token, token)) outer--;
|
|
273
|
+
const outer = last(openPositions.get(tagKey(token)) ?? []) ?? -1;
|
|
266
274
|
end('mismatch', token, outer === -1);
|
|
267
275
|
if (outer !== -1) {
|
|
268
276
|
while (stack.length > outer + 1) end('cut', token);
|
|
@@ -316,6 +324,10 @@ export function pairMarkup(
|
|
|
316
324
|
}
|
|
317
325
|
const body: PairedBody = { children: [], branches: [] };
|
|
318
326
|
node.body = body;
|
|
327
|
+
const key = tagKey(token as MacroToken | HtmlToken);
|
|
328
|
+
const positions = openPositions.get(key) ?? [];
|
|
329
|
+
positions.push(stack.length);
|
|
330
|
+
openPositions.set(key, positions);
|
|
319
331
|
stack.push({ node, body, into: body.children });
|
|
320
332
|
}
|
|
321
333
|
while (stack.length > 0) end('unclosed');
|
package/src/markup/render.tsx
CHANGED
|
@@ -879,8 +879,6 @@ function nodeKey(node: ASTNode): string {
|
|
|
879
879
|
}
|
|
880
880
|
|
|
881
881
|
function renderMacro(node: MacroNode, key: string) {
|
|
882
|
-
if (isSubMacro(node.name)) return null;
|
|
883
|
-
|
|
884
882
|
// What an invocation passes on to a widget and to a built-in macro alike
|
|
885
883
|
const invocation = {
|
|
886
884
|
rawArgs: node.rawArgs,
|
|
@@ -901,6 +899,8 @@ function renderMacro(node: MacroNode, key: string) {
|
|
|
901
899
|
);
|
|
902
900
|
}
|
|
903
901
|
|
|
902
|
+
if (isSubMacro(node.name)) return null;
|
|
903
|
+
|
|
904
904
|
const Component = getMacro(node.name);
|
|
905
905
|
if (Component) {
|
|
906
906
|
return (
|
package/src/registry.ts
CHANGED
|
@@ -276,6 +276,9 @@ export function getMacroParameters(
|
|
|
276
276
|
return metadataRegistry.get(name)?.parameters;
|
|
277
277
|
}
|
|
278
278
|
|
|
279
|
+
getMacroParameters.source = (name: string) =>
|
|
280
|
+
metadataRegistry.get(name)?.source;
|
|
281
|
+
|
|
279
282
|
export function getMacroRegistry(): MacroMetadata[] {
|
|
280
283
|
return Array.from(metadataRegistry.values());
|
|
281
284
|
}
|
|
@@ -318,6 +318,12 @@ export function establishPlaythrough(
|
|
|
318
318
|
|
|
319
319
|
// --- Save Hooks ---
|
|
320
320
|
|
|
321
|
+
/** A synchronous hook capture; dispose also runs when a hook throws. */
|
|
322
|
+
export interface SaveCapture {
|
|
323
|
+
(): SavePayload;
|
|
324
|
+
dispose(): void;
|
|
325
|
+
}
|
|
326
|
+
|
|
321
327
|
/**
|
|
322
328
|
* Run a save through the `beforesave` / `aftersave` hooks. `beforesave` fires
|
|
323
329
|
* before the payload is captured, so data a hook sets is part of the save;
|
|
@@ -339,13 +345,18 @@ export function establishPlaythrough(
|
|
|
339
345
|
export function saveWithHooks<T>(
|
|
340
346
|
slot: string | undefined,
|
|
341
347
|
custom: Record<string, unknown> | undefined,
|
|
342
|
-
beginCapture: () =>
|
|
348
|
+
beginCapture: () => SaveCapture,
|
|
343
349
|
write: (payload: SavePayload) => Promise<T>,
|
|
344
350
|
): Promise<T> {
|
|
345
351
|
try {
|
|
346
352
|
const capture = beginCapture();
|
|
347
|
-
|
|
348
|
-
|
|
353
|
+
let payload: SavePayload;
|
|
354
|
+
try {
|
|
355
|
+
withoutDraws(() => emit('beforesave', slot, custom));
|
|
356
|
+
payload = capture();
|
|
357
|
+
} finally {
|
|
358
|
+
capture.dispose();
|
|
359
|
+
}
|
|
349
360
|
return write(payload).then((result) => {
|
|
350
361
|
withoutDraws(() => emit('aftersave', slot));
|
|
351
362
|
return result;
|
package/src/store.ts
CHANGED
|
@@ -35,6 +35,7 @@ import {
|
|
|
35
35
|
startNewPlaythrough,
|
|
36
36
|
quickSave,
|
|
37
37
|
saveWithHooks,
|
|
38
|
+
type SaveCapture,
|
|
38
39
|
loadSlotSave,
|
|
39
40
|
adoptPlaythrough,
|
|
40
41
|
populateKnownSaves,
|
|
@@ -493,6 +494,7 @@ function enterStart(
|
|
|
493
494
|
): void {
|
|
494
495
|
state.currentPassage = passage;
|
|
495
496
|
state.navigationId++;
|
|
497
|
+
state.replacementNavigationId = state.navigationId;
|
|
496
498
|
state.variables = variables;
|
|
497
499
|
state.transient = freshTransients(transientDefaults);
|
|
498
500
|
state.temporary = createNamespace();
|
|
@@ -841,11 +843,11 @@ function loadedEntryMoment(
|
|
|
841
843
|
}
|
|
842
844
|
|
|
843
845
|
/** The variable paths `Story.set` writes while a save's hooks run. */
|
|
844
|
-
|
|
846
|
+
const hookWrites = new Set<Map<string, string[]>>();
|
|
845
847
|
|
|
846
848
|
/** Note that `path` of the variables was written (see keepHookWrites). */
|
|
847
849
|
export function recordHookWrite(path: string[]): void {
|
|
848
|
-
hookWrites
|
|
850
|
+
for (const writes of hookWrites) writes.set(pathKey(path), path);
|
|
849
851
|
}
|
|
850
852
|
|
|
851
853
|
/**
|
|
@@ -997,6 +999,8 @@ export interface StoryState {
|
|
|
997
999
|
* so the passage display can remount even when the passage name is unchanged.
|
|
998
1000
|
*/
|
|
999
1001
|
navigationId: number;
|
|
1002
|
+
/** Navigation that most recently initialized, restarted or loaded the game. */
|
|
1003
|
+
replacementNavigationId: number;
|
|
1000
1004
|
variables: Record<string, unknown>;
|
|
1001
1005
|
variableDefaults: Record<string, unknown>;
|
|
1002
1006
|
transient: Record<string, unknown>;
|
|
@@ -1080,7 +1084,7 @@ export interface StoryState {
|
|
|
1080
1084
|
* function builds the payload after them, and keeps the variables the
|
|
1081
1085
|
* hooks changed in the saved moment's snapshot, so a load restores them.
|
|
1082
1086
|
*/
|
|
1083
|
-
beginSave: () =>
|
|
1087
|
+
beginSave: () => SaveCapture;
|
|
1084
1088
|
/**
|
|
1085
1089
|
* Replace the game state with a live (deserialized) payload. `slot` is
|
|
1086
1090
|
* passed to the `beforeload`/`afterload` events. Pass the save's
|
|
@@ -1303,6 +1307,7 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1303
1307
|
storyData: null,
|
|
1304
1308
|
currentPassage: '',
|
|
1305
1309
|
navigationId: 0,
|
|
1310
|
+
replacementNavigationId: 0,
|
|
1306
1311
|
variables: createNamespace(),
|
|
1307
1312
|
variableDefaults: {},
|
|
1308
1313
|
transient: createNamespace(),
|
|
@@ -1834,20 +1839,28 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1834
1839
|
const before = get().variables;
|
|
1835
1840
|
const replaced = latestStateApplied;
|
|
1836
1841
|
const writes = new Map<string, string[]>();
|
|
1837
|
-
hookWrites
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
// A restart or load in a hook left the game the save was issued
|
|
1841
|
-
// for (its playthrough) for another: no save describes both
|
|
1842
|
-
if (latestStateApplied !== replaced) {
|
|
1843
|
-
throw new Error(
|
|
1844
|
-
'spindle: a beforesave handler restarted or loaded the game, so the save was not made',
|
|
1845
|
-
);
|
|
1846
|
-
}
|
|
1847
|
-
const payload = get().getSavePayload();
|
|
1848
|
-
keepHookWrites(payload, before, get().variables, [...writes.values()]);
|
|
1849
|
-
return payload;
|
|
1842
|
+
hookWrites.add(writes);
|
|
1843
|
+
const dispose = () => {
|
|
1844
|
+
hookWrites.delete(writes);
|
|
1850
1845
|
};
|
|
1846
|
+
return Object.assign(
|
|
1847
|
+
() => {
|
|
1848
|
+
dispose();
|
|
1849
|
+
// A restart or load in a hook left the game the save was issued
|
|
1850
|
+
// for (its playthrough) for another: no save describes both
|
|
1851
|
+
if (latestStateApplied !== replaced) {
|
|
1852
|
+
throw new Error(
|
|
1853
|
+
'spindle: a beforesave handler restarted or loaded the game, so the save was not made',
|
|
1854
|
+
);
|
|
1855
|
+
}
|
|
1856
|
+
const payload = get().getSavePayload();
|
|
1857
|
+
keepHookWrites(payload, before, get().variables, [
|
|
1858
|
+
...writes.values(),
|
|
1859
|
+
]);
|
|
1860
|
+
return payload;
|
|
1861
|
+
},
|
|
1862
|
+
{ dispose },
|
|
1863
|
+
);
|
|
1851
1864
|
},
|
|
1852
1865
|
|
|
1853
1866
|
loadFromPayload: (
|
|
@@ -1937,6 +1950,7 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1937
1950
|
set((state) => {
|
|
1938
1951
|
state.currentPassage = payload.passage;
|
|
1939
1952
|
state.navigationId++;
|
|
1953
|
+
state.replacementNavigationId = state.navigationId;
|
|
1940
1954
|
state.variables = createNamespace(
|
|
1941
1955
|
deepClone(entry?.variables ?? payload.variables),
|
|
1942
1956
|
);
|
package/src/story-variables.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
pieceOffset,
|
|
23
23
|
widgetPieces,
|
|
24
24
|
withParseCache,
|
|
25
|
+
executesRawBody,
|
|
25
26
|
type ParametersOf,
|
|
26
27
|
type Piece,
|
|
27
28
|
} from './code-check';
|
|
@@ -832,10 +833,7 @@ function collectTokenRefs(
|
|
|
832
833
|
}
|
|
833
834
|
}
|
|
834
835
|
|
|
835
|
-
if (
|
|
836
|
-
token.name.toLowerCase() === 'do' &&
|
|
837
|
-
!parametersOf.widget?.(token.name)
|
|
838
|
-
) {
|
|
836
|
+
if (executesRawBody(token.name, parametersOf)) {
|
|
839
837
|
// A {do} body is JavaScript: scan its source text as code, however
|
|
840
838
|
// the markup tokens split it up.
|
|
841
839
|
let close = t + 1;
|
package/src/tooling.ts
CHANGED
|
@@ -177,6 +177,8 @@ export interface ValidateMarkupOptions {
|
|
|
177
177
|
|
|
178
178
|
export interface ToolingMacro {
|
|
179
179
|
name: string;
|
|
180
|
+
/** Preserve builtin provenance; an explicit macro without it is a replacement. */
|
|
181
|
+
source?: 'builtin' | 'user';
|
|
180
182
|
block: boolean;
|
|
181
183
|
subMacros: string[];
|
|
182
184
|
/**
|
|
@@ -2,11 +2,12 @@ import type { ASTNode } from '../markup/ast';
|
|
|
2
2
|
import type { Token } from '../markup/tokens';
|
|
3
3
|
import { codeAndText, parameterLookup, type ParametersOf } from '../code-check';
|
|
4
4
|
import { parseMarkup, type MarkupHooks } from '../markup/parse';
|
|
5
|
-
import { getMacroRegistry } from '../registry';
|
|
5
|
+
import { getMacroRegistry, getMacroParameters } from '../registry';
|
|
6
6
|
|
|
7
7
|
/** The declared parameters of the macros registered so far. */
|
|
8
8
|
export const registeredParameters: ParametersOf = (name) =>
|
|
9
9
|
parameterLookup(getMacroRegistry())(name);
|
|
10
|
+
registeredParameters.source = getMacroParameters.source;
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Whether the text of an attribute value or macro label renders a
|
package/types/tooling.d.ts
CHANGED
|
@@ -653,6 +653,8 @@ export declare function collectPassageReferences(
|
|
|
653
653
|
/** What tooling knows about a macro (a `MacroMetadata` has all of it). */
|
|
654
654
|
export interface ToolingMacro {
|
|
655
655
|
name: string;
|
|
656
|
+
/** Preserve builtin provenance; an explicit macro without it is a replacement. */
|
|
657
|
+
source?: 'builtin' | 'user';
|
|
656
658
|
block: boolean;
|
|
657
659
|
subMacros: string[];
|
|
658
660
|
/** A widget's parameters and their argument contracts. */
|