@rohal12/spindle 0.62.9 → 0.64.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/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +365 -191
- package/dist/pkg/macro-registry.js +4 -1
- package/dist/pkg/macro-registry.json +4 -1
- package/dist/pkg/story-variables.js +447 -179
- package/dist/pkg/tooling.js +2 -0
- package/package.json +1 -1
- package/src/code-check.ts +162 -45
- package/src/completion-context.ts +4 -4
- package/src/components/macros/Cycle.tsx +2 -1
- package/src/components/macros/If.tsx +1 -0
- package/src/components/macros/Listbox.tsx +2 -1
- package/src/components/macros/option-clauses.ts +20 -0
- package/src/components/macros/option-utils.ts +3 -2
- package/src/js-lexer.ts +154 -134
- package/src/markup/ast.ts +22 -9
- package/src/markup/expansion-budget.ts +23 -0
- package/src/markup/pair.ts +6 -4
- package/src/markup/parse.ts +2 -2
- package/src/markup/render.tsx +23 -15
- package/src/markup/validate.ts +13 -0
- package/src/observe/observe.ts +8 -3
- package/src/passage-roles.ts +19 -0
- package/src/registry.ts +33 -13
- package/src/store.ts +6 -23
- package/src/story-variables.ts +48 -15
- package/src/tooling.ts +120 -16
- package/src/widgets/ast-scanner.ts +17 -13
- package/src/widgets/widget-def.ts +3 -0
- package/types/tooling.d.ts +27 -0
package/src/registry.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ComponentType } from 'preact';
|
|
2
2
|
import type { ASTNode, Branch } from './markup/ast';
|
|
3
|
+
import { consumesOptionClauses } from './components/macros/option-clauses';
|
|
3
4
|
import {
|
|
4
5
|
effectiveNamedDefinitions,
|
|
5
6
|
NameMap,
|
|
@@ -247,10 +248,19 @@ export function registerMacroMetadata(
|
|
|
247
248
|
}
|
|
248
249
|
}
|
|
249
250
|
metadataRegistry.set(name, metadata);
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
251
|
+
indexCompanionParents(companionParents, metadata);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Share index construction between live registrations and stateless tooling. */
|
|
255
|
+
function indexCompanionParents(
|
|
256
|
+
index: NameMap<Set<string>>,
|
|
257
|
+
macro: Pick<MacroMetadata, 'name' | 'subMacros'>,
|
|
258
|
+
): void {
|
|
259
|
+
const name = macro.name.toLowerCase();
|
|
260
|
+
for (const sub of macro.subMacros) {
|
|
261
|
+
const parents = index.get(sub) ?? new Set<string>();
|
|
262
|
+
parents.add(name);
|
|
263
|
+
index.set(sub, parents);
|
|
254
264
|
}
|
|
255
265
|
}
|
|
256
266
|
|
|
@@ -261,17 +271,27 @@ export function branchParents(
|
|
|
261
271
|
): string[] {
|
|
262
272
|
if (!macros) {
|
|
263
273
|
return [...(companionParents.get(name) ?? [])].filter(
|
|
264
|
-
(parent) => parent
|
|
274
|
+
(parent) => !consumesOptionClauses(metadataRegistry.get(parent)!),
|
|
265
275
|
);
|
|
266
276
|
}
|
|
267
|
-
return
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
277
|
+
return [...branchParentLookup(macros)(name)];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Build once per effective metadata snapshot, then look up each token. */
|
|
281
|
+
export function branchParentLookup(
|
|
282
|
+
macros: readonly (Pick<MacroMetadata, 'name' | 'subMacros'> & {
|
|
283
|
+
source?: 'builtin' | 'user';
|
|
284
|
+
widget?: unknown;
|
|
285
|
+
})[],
|
|
286
|
+
): (name: string) => readonly string[] {
|
|
287
|
+
const parents = new NameMap<Set<string>>();
|
|
288
|
+
for (const macro of effectiveNamedDefinitions(macros)) {
|
|
289
|
+
if (consumesOptionClauses(macro)) continue;
|
|
290
|
+
indexCompanionParents(parents, macro);
|
|
291
|
+
}
|
|
292
|
+
const index = new NameMap<readonly string[]>();
|
|
293
|
+
for (const [sub, names] of parents) index.set(sub, [...names]);
|
|
294
|
+
return (name) => index.get(name) ?? [];
|
|
275
295
|
}
|
|
276
296
|
|
|
277
297
|
/** The declared parameters of the macro `name`, if it declares any. */
|
package/src/store.ts
CHANGED
|
@@ -64,7 +64,7 @@ import {
|
|
|
64
64
|
underChange,
|
|
65
65
|
type PathChange,
|
|
66
66
|
} from './structural';
|
|
67
|
-
import { getByPath } from './utils/object-path';
|
|
67
|
+
import { getByPath, setByPath } from './utils/object-path';
|
|
68
68
|
import { isIndexKey } from './utils/value-kinds';
|
|
69
69
|
import {
|
|
70
70
|
noPassageError,
|
|
@@ -95,31 +95,14 @@ import {
|
|
|
95
95
|
type Counts,
|
|
96
96
|
type Namespace,
|
|
97
97
|
} from './utils/namespace';
|
|
98
|
+
import { isSpecialPassage } from './passage-roles';
|
|
99
|
+
export { isSpecialPassage } from './passage-roles';
|
|
98
100
|
|
|
99
101
|
enablePatches();
|
|
100
102
|
// Story state holds Map and Set values: Immer must be able to draft them
|
|
101
103
|
// when a write (a dot path, a macro binding) reaches one
|
|
102
104
|
enableMapSet();
|
|
103
105
|
|
|
104
|
-
const SPECIAL_PASSAGES = new Set([
|
|
105
|
-
'StoryInit',
|
|
106
|
-
'StoryInterface',
|
|
107
|
-
'StoryVariables',
|
|
108
|
-
'StoryTransients',
|
|
109
|
-
'StoryLoading',
|
|
110
|
-
'SaveTitle',
|
|
111
|
-
'PassageReady',
|
|
112
|
-
'PassageHeader',
|
|
113
|
-
'PassageFooter',
|
|
114
|
-
'PassageDone',
|
|
115
|
-
'StoryContext',
|
|
116
|
-
]);
|
|
117
|
-
|
|
118
|
-
/** Whether `name` is a special passage, which navigation never goes to. */
|
|
119
|
-
export function isSpecialPassage(name: string): boolean {
|
|
120
|
-
return SPECIAL_PASSAGES.has(name);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
106
|
// ---------------------------------------------------------------------------
|
|
124
107
|
// Patch-based variable history (module-level, outside Zustand)
|
|
125
108
|
// ---------------------------------------------------------------------------
|
|
@@ -1515,14 +1498,14 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1515
1498
|
setVariable: (name: string, value: unknown) => {
|
|
1516
1499
|
set((state) => {
|
|
1517
1500
|
checkVariableName(name, `$${name}`);
|
|
1518
|
-
state.variables[name]
|
|
1501
|
+
setByPath(state.variables, [name], value);
|
|
1519
1502
|
});
|
|
1520
1503
|
},
|
|
1521
1504
|
|
|
1522
1505
|
setTemporary: (name: string, value: unknown) => {
|
|
1523
1506
|
set((state) => {
|
|
1524
1507
|
checkVariableName(name, `_${name}`);
|
|
1525
|
-
state.temporary[name]
|
|
1508
|
+
setByPath(state.temporary, [name], value);
|
|
1526
1509
|
});
|
|
1527
1510
|
},
|
|
1528
1511
|
|
|
@@ -1543,7 +1526,7 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1543
1526
|
setTransient: (name: string, value: unknown) => {
|
|
1544
1527
|
set((state) => {
|
|
1545
1528
|
checkVariableName(name, `%${name}`);
|
|
1546
|
-
state.transient[name]
|
|
1529
|
+
setByPath(state.transient, [name], value);
|
|
1547
1530
|
});
|
|
1548
1531
|
},
|
|
1549
1532
|
|
package/src/story-variables.ts
CHANGED
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
widgetPieces,
|
|
26
26
|
withParseCache,
|
|
27
27
|
executesRawBody,
|
|
28
|
+
macroArgumentLookup,
|
|
28
29
|
type ParametersOf,
|
|
29
30
|
type Piece,
|
|
30
31
|
} from './code-check';
|
|
@@ -44,6 +45,7 @@ import {
|
|
|
44
45
|
} from './utils/namespace';
|
|
45
46
|
import { staticLiteral, type LiteralShape } from './static-literal';
|
|
46
47
|
import { parse } from 'acorn';
|
|
48
|
+
import { MarkupExpansionBudget } from './markup/expansion-budget';
|
|
47
49
|
|
|
48
50
|
/**
|
|
49
51
|
* The tokens of markup, or none if it is malformed (validateMarkup reports
|
|
@@ -502,6 +504,7 @@ const BUILTIN_STORE_VAR_MACROS: readonly string[] = [
|
|
|
502
504
|
|
|
503
505
|
/** What reading the variable references of markup depends on. */
|
|
504
506
|
interface ReferenceOptions {
|
|
507
|
+
expansionBudget?: MarkupExpansionBudget;
|
|
505
508
|
/** The (lowercase) names of the input macros that bind a variable. */
|
|
506
509
|
storeVarMacros: ReadonlySet<string>;
|
|
507
510
|
parametersOf: ParametersOf;
|
|
@@ -572,7 +575,24 @@ function scanInterpolations(
|
|
|
572
575
|
options: ReferenceOptions,
|
|
573
576
|
): void {
|
|
574
577
|
if (!text.includes('{')) return;
|
|
575
|
-
|
|
578
|
+
const budget = options.expansionBudget!;
|
|
579
|
+
if (!budget.enter(text.length)) return;
|
|
580
|
+
try {
|
|
581
|
+
const offset = at(0);
|
|
582
|
+
const sourceOffsets =
|
|
583
|
+
at(text.length) - offset === text.length
|
|
584
|
+
? undefined
|
|
585
|
+
: Array.from({ length: text.length + 1 }, (_, i) => at(i));
|
|
586
|
+
collectTokenRefs(
|
|
587
|
+
text,
|
|
588
|
+
tokensOf(text, true, options),
|
|
589
|
+
(i) => sourceOffsets?.[i] ?? offset + i,
|
|
590
|
+
emit,
|
|
591
|
+
options,
|
|
592
|
+
);
|
|
593
|
+
} finally {
|
|
594
|
+
budget.leave();
|
|
595
|
+
}
|
|
576
596
|
}
|
|
577
597
|
|
|
578
598
|
/**
|
|
@@ -783,6 +803,7 @@ function collectTokenRefs(
|
|
|
783
803
|
options: ReferenceOptions,
|
|
784
804
|
): void {
|
|
785
805
|
const { storeVarMacros, parametersOf } = options;
|
|
806
|
+
const argumentRole = macroArgumentLookup(tokens, parametersOf);
|
|
786
807
|
for (let t = 0; t < tokens.length; t++) {
|
|
787
808
|
const token = tokens[t]!;
|
|
788
809
|
if (
|
|
@@ -826,29 +847,38 @@ function collectTokenRefs(
|
|
|
826
847
|
else scanInterpolations(value, valueIn, emit, options);
|
|
827
848
|
}
|
|
828
849
|
} else if (token.type === 'macro' && !token.isClose) {
|
|
850
|
+
const role = argumentRole(token);
|
|
829
851
|
const binding =
|
|
852
|
+
role === 'invocation' &&
|
|
830
853
|
storeVarMacros.has(token.name.toLowerCase()) &&
|
|
831
854
|
!parametersOf.widget?.(token.name)
|
|
832
855
|
? boundReference(token, at, options.all)
|
|
833
856
|
: undefined;
|
|
834
|
-
|
|
835
|
-
token,
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
857
|
+
if (role === 'condition') {
|
|
858
|
+
scanCode(token.rawArgs, (i) => at(token.argsStart + i), emit);
|
|
859
|
+
} else if (role === 'invocation')
|
|
860
|
+
scanArgs(
|
|
861
|
+
token,
|
|
862
|
+
parametersOf,
|
|
863
|
+
at,
|
|
864
|
+
(ref) => {
|
|
865
|
+
// A parameterless custom storeVar macro also scans its arguments
|
|
866
|
+
// as code. Keep the binding's decoded path and span just once.
|
|
867
|
+
if (
|
|
868
|
+
!binding ||
|
|
869
|
+
ref.start < binding.start ||
|
|
870
|
+
ref.start >= binding.end
|
|
871
|
+
)
|
|
872
|
+
emit(ref);
|
|
873
|
+
},
|
|
874
|
+
options,
|
|
875
|
+
);
|
|
846
876
|
// Selectors (`{.{$cls} button}`) are interpolated when rendered
|
|
847
877
|
scanSelectors(content, token, at, emit, options);
|
|
848
878
|
|
|
849
879
|
if (binding) emit(binding);
|
|
850
880
|
|
|
851
|
-
if (executesRawBody(token.name, parametersOf)) {
|
|
881
|
+
if (role === 'invocation' && executesRawBody(token.name, parametersOf)) {
|
|
852
882
|
// A {do} body is JavaScript: scan its source text as code, however
|
|
853
883
|
// the markup tokens split it up.
|
|
854
884
|
let close = t + 1;
|
|
@@ -891,7 +921,10 @@ export function collectVariableReferences(
|
|
|
891
921
|
tokensOf(content, false, options),
|
|
892
922
|
(i) => i,
|
|
893
923
|
(ref) => found.push(ref),
|
|
894
|
-
|
|
924
|
+
{
|
|
925
|
+
...options,
|
|
926
|
+
expansionBudget: new MarkupExpansionBudget(content.length),
|
|
927
|
+
},
|
|
895
928
|
),
|
|
896
929
|
);
|
|
897
930
|
// The scan visits a macro's arguments before its selectors, and bindings
|
package/src/tooling.ts
CHANGED
|
@@ -4,7 +4,12 @@ export {
|
|
|
4
4
|
type CompletionContext,
|
|
5
5
|
type CompletionContainer,
|
|
6
6
|
} from './completion-context';
|
|
7
|
-
import {
|
|
7
|
+
import { branchParentLookup } from './registry';
|
|
8
|
+
import {
|
|
9
|
+
consumesOptionClauses,
|
|
10
|
+
isOptionClause,
|
|
11
|
+
} from './components/macros/option-clauses';
|
|
12
|
+
import { pairMarkup, type PairedNode } from './markup/pair';
|
|
8
13
|
/**
|
|
9
14
|
* What the tooling entry point (`@rohal12/spindle/tooling`) bundles as
|
|
10
15
|
* dist/pkg/story-variables.js: the StoryVariables parser and the markup
|
|
@@ -16,7 +21,12 @@ import {
|
|
|
16
21
|
type MarkupDiagnostic,
|
|
17
22
|
type MarkupPassage,
|
|
18
23
|
} from './markup/validate';
|
|
19
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
isBlockMacro,
|
|
26
|
+
isBuiltinBlockMacro,
|
|
27
|
+
isRawMacro,
|
|
28
|
+
isRegisteredBlockMacro,
|
|
29
|
+
} from './markup/ast';
|
|
20
30
|
import {
|
|
21
31
|
tokenizeMarkup as tokenizeRawMarkup,
|
|
22
32
|
tokenizeMarkupTolerant as tokenizeRawMarkupTolerant,
|
|
@@ -44,7 +54,7 @@ import {
|
|
|
44
54
|
type VariableReference,
|
|
45
55
|
} from './story-variables';
|
|
46
56
|
import type { ParameterDef } from './registry';
|
|
47
|
-
import type { Token } from './markup/tokens';
|
|
57
|
+
import type { MacroToken, Token } from './markup/tokens';
|
|
48
58
|
import type { WidgetDef } from './widgets/widget-def';
|
|
49
59
|
|
|
50
60
|
export { parseDeclarations, parseStoryVariables } from './story-variables';
|
|
@@ -122,6 +132,7 @@ export type {
|
|
|
122
132
|
PairMarkupOptions,
|
|
123
133
|
} from './markup/pair';
|
|
124
134
|
export { isBlockMacro } from './markup/ast';
|
|
135
|
+
export { isSpecialPassage } from './passage-roles';
|
|
125
136
|
export type { MarkupErrorCode } from './markup/parse';
|
|
126
137
|
export {
|
|
127
138
|
endsWithOperator,
|
|
@@ -231,7 +242,7 @@ function tokenizationOptions(
|
|
|
231
242
|
isRaw: (name) =>
|
|
232
243
|
isRawMacro(name, parametersOf.widget?.(name) !== undefined),
|
|
233
244
|
isBlock: blockLookup(macros),
|
|
234
|
-
branchParents: (
|
|
245
|
+
branchParents: branchParentLookup(macros),
|
|
235
246
|
...options.hooks,
|
|
236
247
|
},
|
|
237
248
|
};
|
|
@@ -271,9 +282,15 @@ export function validateStoryMarkup(
|
|
|
271
282
|
const widgets = effectiveWidgetDefinitions(
|
|
272
283
|
readWidgetDefinitions(list, parameterLookup(all)),
|
|
273
284
|
);
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
285
|
+
const effective = effectiveToolingMacros([
|
|
286
|
+
...all,
|
|
287
|
+
...widgets.map((widget) => ({
|
|
288
|
+
name: widget.name,
|
|
289
|
+
block: widget.block,
|
|
290
|
+
subMacros: [],
|
|
291
|
+
widget,
|
|
292
|
+
})),
|
|
293
|
+
]);
|
|
277
294
|
const parametersOf = parameterLookup([
|
|
278
295
|
...all,
|
|
279
296
|
...definedWidgetMacros(widgets),
|
|
@@ -282,15 +299,13 @@ export function validateStoryMarkup(
|
|
|
282
299
|
const name = macro.name.toLowerCase();
|
|
283
300
|
known.add(name);
|
|
284
301
|
for (const sub of macro.subMacros) known.add(sub.toLowerCase());
|
|
285
|
-
if (macro.block) blocks.add(name);
|
|
286
302
|
}
|
|
287
303
|
return validateMarkup(list, {
|
|
288
|
-
branchParents: (
|
|
304
|
+
branchParents: branchParentLookup(effective),
|
|
289
305
|
isKnownMacro: (name) => known.has(name),
|
|
290
306
|
macroNames: known,
|
|
291
307
|
parametersOf,
|
|
292
|
-
isBlockMacro: (
|
|
293
|
-
blocks.has(name.toLowerCase()) || isBlockMacro(name),
|
|
308
|
+
isBlockMacro: blockLookup(effective),
|
|
294
309
|
checkPassageNames: options.checkPassageNames,
|
|
295
310
|
tolerant: options.tolerant,
|
|
296
311
|
});
|
|
@@ -302,11 +317,100 @@ function definedWidgetMacros(widgets: readonly WidgetDefinition[]) {
|
|
|
302
317
|
}
|
|
303
318
|
|
|
304
319
|
/** Whether a macro takes a body, among `macros` and the built-in ones. */
|
|
305
|
-
function blockLookup(macros: readonly ToolingMacro[]) {
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
320
|
+
function blockLookup(macros: readonly ToolingMacro[], includeRuntime = true) {
|
|
321
|
+
const definitions = new NameMap<boolean>();
|
|
322
|
+
for (const macro of effectiveToolingMacros(macros)) {
|
|
323
|
+
definitions.set(macro.name, macro.block);
|
|
324
|
+
}
|
|
325
|
+
return (name: string) =>
|
|
326
|
+
(includeRuntime && isRegisteredBlockMacro(name)) ||
|
|
327
|
+
(definitions.get(name) ??
|
|
328
|
+
(includeRuntime ? isBlockMacro(name) : isBuiltinBlockMacro(name)));
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** A macro head's contextual role, with original UTF-16 token spans. */
|
|
332
|
+
export interface MacroCall {
|
|
333
|
+
token: MacroToken;
|
|
334
|
+
kind: 'invocation' | 'branch' | 'value';
|
|
335
|
+
/** The opening head consuming this clause; absent for invocations. */
|
|
336
|
+
owner?: MacroToken;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Classify heads against explicit effective metadata, without running code.
|
|
341
|
+
* Labels and attributes are separate markup scopes and never inherit a
|
|
342
|
+
* surrounding control's clause ownership. This describes dispatch, not
|
|
343
|
+
* whether a conditional body will execute.
|
|
344
|
+
*/
|
|
345
|
+
export function macroCalls(
|
|
346
|
+
source: string,
|
|
347
|
+
macros: Iterable<ToolingMacro> = [],
|
|
348
|
+
): MacroCall[] {
|
|
349
|
+
const all = effectiveToolingMacros(macros);
|
|
350
|
+
const definitions = new NameMap(all.map((macro) => [macro.name, macro]));
|
|
351
|
+
const parametersOf = parameterLookup(all);
|
|
352
|
+
const options = tokenizationOptions({
|
|
353
|
+
macros: all,
|
|
354
|
+
hooks: { isBlock: blockLookup(all, false) },
|
|
355
|
+
});
|
|
356
|
+
const hooks = options.hooks!;
|
|
357
|
+
const tokens = tokenizeRawMarkupTolerant(source, options).tokens;
|
|
358
|
+
const calls: MacroCall[] = [];
|
|
359
|
+
const classify = (tokens: readonly Token[]) => {
|
|
360
|
+
const found = new Map<MacroToken, MacroCall>();
|
|
361
|
+
const pending: { node: PairedNode; parent?: MacroToken }[] = pairMarkup(
|
|
362
|
+
tokens,
|
|
363
|
+
hooks,
|
|
364
|
+
).nodes.map((node) => ({ node }));
|
|
365
|
+
while (pending.length) {
|
|
366
|
+
const { node, parent } = pending.pop()!;
|
|
367
|
+
const token = node.token;
|
|
368
|
+
if (token.type === 'macro') {
|
|
369
|
+
const definition = parent && definitions.get(parent.name);
|
|
370
|
+
const value =
|
|
371
|
+
definition &&
|
|
372
|
+
consumesOptionClauses(definition) &&
|
|
373
|
+
isOptionClause(token.name);
|
|
374
|
+
found.set(token, {
|
|
375
|
+
token,
|
|
376
|
+
kind: value ? 'value' : 'invocation',
|
|
377
|
+
...(value && { owner: parent }),
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
if (!node.body) continue;
|
|
381
|
+
const owner = token.type === 'macro' ? token : undefined;
|
|
382
|
+
for (const child of node.body.children)
|
|
383
|
+
pending.push({ node: child, parent: owner });
|
|
384
|
+
for (const branch of node.body.branches) {
|
|
385
|
+
found.set(branch.tag, { token: branch.tag, kind: 'branch', owner });
|
|
386
|
+
for (const child of branch.children)
|
|
387
|
+
pending.push({ node: child, parent: owner });
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
for (const token of tokens) {
|
|
391
|
+
if (token.type !== 'macro' || token.isClose) continue;
|
|
392
|
+
const call = found.get(token) ?? { token, kind: 'branch' as const };
|
|
393
|
+
calls.push(call);
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
classify(tokens);
|
|
397
|
+
for (const piece of passagePiecesOf(source, tokens, parametersOf, {
|
|
398
|
+
isBlock: hooks.isBlock,
|
|
399
|
+
})) {
|
|
400
|
+
if (piece.kind !== 'text' || !piece.tokens.length) continue;
|
|
401
|
+
classify(piece.tokens);
|
|
402
|
+
}
|
|
403
|
+
// Consumed arguments are literal even if a same-named widget would
|
|
404
|
+
// normally evaluate them as markup. Drop heads inside those arguments.
|
|
405
|
+
let literalEnd = -1;
|
|
406
|
+
return calls
|
|
407
|
+
.sort((a, b) => a.token.start - b.token.start)
|
|
408
|
+
.filter((call) => {
|
|
409
|
+
if (call.token.start < literalEnd) return false;
|
|
410
|
+
if (call.kind === 'value' || call.kind === 'branch')
|
|
411
|
+
literalEnd = call.token.argsEnd;
|
|
412
|
+
return true;
|
|
413
|
+
});
|
|
310
414
|
}
|
|
311
415
|
|
|
312
416
|
/**
|
|
@@ -3,6 +3,7 @@ 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
5
|
import { getMacroRegistry, getMacroParameters } from '../registry';
|
|
6
|
+
import { MarkupExpansionBudget } from '../markup/expansion-budget';
|
|
6
7
|
|
|
7
8
|
/** The declared parameters of the macros registered so far. */
|
|
8
9
|
export const registeredParameters: ParametersOf = (name) =>
|
|
@@ -17,16 +18,19 @@ function textContainsChildren(
|
|
|
17
18
|
text: string,
|
|
18
19
|
parametersOf: ParametersOf,
|
|
19
20
|
hooks?: Partial<MarkupHooks>,
|
|
21
|
+
budget = new MarkupExpansionBudget(text.length),
|
|
20
22
|
): boolean {
|
|
21
23
|
if (!text.includes('{')) return false;
|
|
22
|
-
|
|
24
|
+
if (!budget.enter(text.length)) return false;
|
|
23
25
|
try {
|
|
24
|
-
nodes = parseMarkup(text, { text: true, hooks });
|
|
26
|
+
const nodes = parseMarkup(text, { text: true, hooks });
|
|
27
|
+
return astContainsChildren(nodes, parametersOf, hooks, budget);
|
|
25
28
|
} catch {
|
|
26
29
|
// The renderer reports the markup that does not parse
|
|
27
30
|
return false;
|
|
31
|
+
} finally {
|
|
32
|
+
budget.leave();
|
|
28
33
|
}
|
|
29
|
-
return astContainsChildren(nodes, parametersOf, hooks);
|
|
30
34
|
}
|
|
31
35
|
|
|
32
36
|
/**
|
|
@@ -42,11 +46,12 @@ export function tokenTextContainsChildren(
|
|
|
42
46
|
parametersOf: ParametersOf = registeredParameters,
|
|
43
47
|
src = '',
|
|
44
48
|
hooks?: Partial<MarkupHooks>,
|
|
49
|
+
budget?: MarkupExpansionBudget,
|
|
45
50
|
): boolean {
|
|
46
51
|
for (const piece of codeAndText(src, [token], parametersOf)) {
|
|
47
52
|
if (
|
|
48
53
|
piece.kind === 'text' &&
|
|
49
|
-
textContainsChildren(piece.text, parametersOf, hooks)
|
|
54
|
+
textContainsChildren(piece.text, parametersOf, hooks, budget)
|
|
50
55
|
) {
|
|
51
56
|
return true;
|
|
52
57
|
}
|
|
@@ -55,7 +60,7 @@ export function tokenTextContainsChildren(
|
|
|
55
60
|
}
|
|
56
61
|
|
|
57
62
|
/**
|
|
58
|
-
*
|
|
63
|
+
* Scan an AST node array for a VariableNode
|
|
59
64
|
* with scope 'local' and name 'children' (@children), in the content and in
|
|
60
65
|
* the attributes and labels that render markup. A nested `{widget}` is not
|
|
61
66
|
* entered: its slot belongs to the innermost widget (as widgetDefinitions
|
|
@@ -65,8 +70,11 @@ export function astContainsChildren(
|
|
|
65
70
|
nodes: ASTNode[],
|
|
66
71
|
parametersOf: ParametersOf = registeredParameters,
|
|
67
72
|
hooks?: Partial<MarkupHooks>,
|
|
73
|
+
budget?: MarkupExpansionBudget,
|
|
68
74
|
): boolean {
|
|
69
|
-
|
|
75
|
+
const pending = [...nodes];
|
|
76
|
+
while (pending.length) {
|
|
77
|
+
const node = pending.pop()!;
|
|
70
78
|
if (
|
|
71
79
|
node.type === 'variable' &&
|
|
72
80
|
node.scope === 'local' &&
|
|
@@ -96,17 +104,13 @@ export function astContainsChildren(
|
|
|
96
104
|
argsStart: 0,
|
|
97
105
|
argsEnd: 0,
|
|
98
106
|
};
|
|
99
|
-
if (
|
|
100
|
-
tokenTextContainsChildren(tag, parametersOf, '', hooks) ||
|
|
101
|
-
astContainsChildren(node.children, parametersOf, hooks)
|
|
102
|
-
) {
|
|
107
|
+
if (tokenTextContainsChildren(tag, parametersOf, '', hooks, budget)) {
|
|
103
108
|
return true;
|
|
104
109
|
}
|
|
110
|
+
for (const child of node.children) pending.push(child);
|
|
105
111
|
if (node.type === 'macro' && node.branches) {
|
|
106
112
|
for (const branch of node.branches) {
|
|
107
|
-
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
113
|
+
for (const child of branch.children) pending.push(child);
|
|
110
114
|
}
|
|
111
115
|
}
|
|
112
116
|
}
|
|
@@ -10,6 +10,7 @@ import type { MacroToken } from '../markup/tokens';
|
|
|
10
10
|
import type { ParametersOf } from '../code-check';
|
|
11
11
|
import { NameMap } from '../utils/macro-names';
|
|
12
12
|
import { registeredParameters, tokenTextContainsChildren } from './ast-scanner';
|
|
13
|
+
import { MarkupExpansionBudget } from '../markup/expansion-budget';
|
|
13
14
|
|
|
14
15
|
/** A {widget} definition's name, then its `@` parameters. */
|
|
15
16
|
export const WIDGET_PARAMETERS = [
|
|
@@ -146,6 +147,7 @@ export function widgetDefinitions(
|
|
|
146
147
|
if (passage.name !== 'StoryInit' && !passage.tags?.includes('widget')) {
|
|
147
148
|
continue;
|
|
148
149
|
}
|
|
150
|
+
const budget = new MarkupExpansionBudget(passage.content.length);
|
|
149
151
|
// The open definitions: their tags, and whether a slot was seen
|
|
150
152
|
const open: {
|
|
151
153
|
token: MacroToken;
|
|
@@ -172,6 +174,7 @@ export function widgetDefinitions(
|
|
|
172
174
|
parametersOf,
|
|
173
175
|
passage.content,
|
|
174
176
|
hooks,
|
|
177
|
+
budget,
|
|
175
178
|
))
|
|
176
179
|
) {
|
|
177
180
|
open[open.length - 1]!.isBlock = true;
|
package/types/tooling.d.ts
CHANGED
|
@@ -171,6 +171,7 @@ export interface MarkupDiagnostic {
|
|
|
171
171
|
* | --- | --- |
|
|
172
172
|
* | `unknown-macro` | `name`, `suggestions` (the closest known macro, if close) |
|
|
173
173
|
* | `unknown-passage` | `name`, `macro` (`link` for `[[…]]`), `suggestions` |
|
|
174
|
+
* | `special-passage` | `name`, `macro` |
|
|
174
175
|
* | `unquoted-passage-name` | `name`, `macro` |
|
|
175
176
|
* | `argument-error` | `macro` |
|
|
176
177
|
* | `code-syntax` | `macro`, for a macro argument |
|
|
@@ -181,6 +182,7 @@ export type MarkupDiagnosticCode =
|
|
|
181
182
|
| 'argument-error'
|
|
182
183
|
| 'code-syntax'
|
|
183
184
|
| 'unknown-passage'
|
|
185
|
+
| 'special-passage'
|
|
184
186
|
| 'unquoted-passage-name';
|
|
185
187
|
|
|
186
188
|
/** What a diagnostic names, by code (see {@link MarkupDiagnosticCode}). */
|
|
@@ -887,6 +889,8 @@ export interface CodePiece extends PieceBase {
|
|
|
887
889
|
export interface PassagePiece extends PieceBase {
|
|
888
890
|
kind: 'passage';
|
|
889
891
|
name: string;
|
|
892
|
+
/** A known navigation target, rather than included/dialog content. */
|
|
893
|
+
navigation?: true;
|
|
890
894
|
/** The markup it is in, for an error: `[[Go->Hall]]`. */
|
|
891
895
|
label: string;
|
|
892
896
|
/** How much of the markup is the name, as written (quotes included). */
|
|
@@ -978,6 +982,26 @@ export interface PairingError {
|
|
|
978
982
|
data: Record<string, string>;
|
|
979
983
|
}
|
|
980
984
|
|
|
985
|
+
/** A macro head’s contextual dispatch role. */
|
|
986
|
+
export interface MacroCall {
|
|
987
|
+
/** Opening macro head, with UTF-16 spans in the original source. */
|
|
988
|
+
token: MacroToken;
|
|
989
|
+
kind: 'invocation' | 'branch' | 'value';
|
|
990
|
+
/** Opening head consuming this clause; absent for invocations or stray branches. */
|
|
991
|
+
owner?: MacroToken;
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
/**
|
|
995
|
+
* Classify macro heads using explicit effective macro/widget metadata.
|
|
996
|
+
* Includes independently evaluated labels and attributes, with original
|
|
997
|
+
* source spans. Tolerates malformed markup. Describes dispatch, not whether
|
|
998
|
+
* a conditional body executes. Pass builtinMacros to enable builtin rules.
|
|
999
|
+
*/
|
|
1000
|
+
export function macroCalls(
|
|
1001
|
+
source: string,
|
|
1002
|
+
macros?: Iterable<ToolingMacro>,
|
|
1003
|
+
): MacroCall[];
|
|
1004
|
+
|
|
981
1005
|
/** A branch (`{else}`, `{case}`, …) of a macro, with what follows it. */
|
|
982
1006
|
export interface PairedBranch {
|
|
983
1007
|
tag: MacroToken;
|
|
@@ -1050,6 +1074,9 @@ export declare function pairMarkup(
|
|
|
1050
1074
|
*/
|
|
1051
1075
|
export declare function isBlockMacro(name: string): boolean;
|
|
1052
1076
|
|
|
1077
|
+
/** Whether a case-sensitive passage name is reserved and cannot be navigated to. */
|
|
1078
|
+
export declare function isSpecialPassage(name: string): boolean;
|
|
1079
|
+
|
|
1053
1080
|
// ---------------------------------------------------------------------------
|
|
1054
1081
|
// Declarations
|
|
1055
1082
|
// ---------------------------------------------------------------------------
|