@rohal12/spindle 0.59.21 → 0.59.23
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 +3 -0
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +1608 -1238
- package/dist/pkg/macro-registry.js +575 -0
- package/dist/pkg/story-variables.js +1566 -1234
- package/dist/pkg/tooling.js +22 -16
- package/package.json +2 -1
- package/src/code-check.ts +293 -56
- package/src/components/macros/SaveManager.tsx +1 -0
- package/src/hooks/use-modal-focus.ts +41 -1
- package/src/markup/ast.ts +88 -1
- package/src/markup/line-column.ts +14 -0
- package/src/markup/pair.ts +331 -0
- package/src/markup/parse.ts +166 -36
- package/src/markup/spindle.d.peggy.ts +3 -3
- package/src/markup/spindle.peggy +108 -183
- package/src/markup/tokens.ts +49 -9
- package/src/markup/validate.ts +167 -79
- package/src/saves/save-manager.ts +8 -0
- package/src/saves/storage.ts +25 -6
- package/src/static-literal.ts +292 -0
- package/src/store.ts +5 -1
- package/src/story-variables.ts +148 -10
- package/src/tooling.ts +89 -8
- package/src/types-drift-check.ts +52 -0
- package/src/widgets/ast-scanner.ts +16 -4
- package/types/tooling.d.ts +449 -7
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shape of a JavaScript literal, read from its text without running it:
|
|
3
|
+
* what a StoryVariables initializer is when it is a literal, or an array or
|
|
4
|
+
* object literal of literals (see parseDeclarations in story-variables.ts).
|
|
5
|
+
*
|
|
6
|
+
* The reader is deliberately small. Anything it does not recognise (a call,
|
|
7
|
+
* an operator, a reference, a spread) is "not static", which is no error: the
|
|
8
|
+
* value is simply not known without running the code.
|
|
9
|
+
*/
|
|
10
|
+
import { scanStringLiteral } from './js-lexer';
|
|
11
|
+
|
|
12
|
+
/** The type of a variable by its default (see story-variables.ts VarType). */
|
|
13
|
+
type LiteralType =
|
|
14
|
+
| 'number'
|
|
15
|
+
| 'string'
|
|
16
|
+
| 'boolean'
|
|
17
|
+
| 'array'
|
|
18
|
+
| 'object'
|
|
19
|
+
| 'null';
|
|
20
|
+
|
|
21
|
+
/** The shape of a literal (see story-variables.ts FieldSchema). */
|
|
22
|
+
export interface LiteralShape {
|
|
23
|
+
type: LiteralType;
|
|
24
|
+
/** The members an object literal gives statically, by key. */
|
|
25
|
+
fields?: Map<string, LiteralShape>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** What a literal reads as. */
|
|
29
|
+
export type StaticLiteral =
|
|
30
|
+
/** A value of this shape. */
|
|
31
|
+
| { shape: LiteralShape }
|
|
32
|
+
/** A value no variable can hold: its `typeof`. */
|
|
33
|
+
| { unsupported: 'function' | 'undefined' | 'bigint' }
|
|
34
|
+
/** Not known without running it. */
|
|
35
|
+
| undefined;
|
|
36
|
+
|
|
37
|
+
const NUMBER_RE =
|
|
38
|
+
/(?:0[xX][\da-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d+)?)(n?)/y;
|
|
39
|
+
const IDENT_RE = /[A-Za-z_$][\w$]*/y;
|
|
40
|
+
const ARROW_RE =
|
|
41
|
+
/(?:async\s+)?(?:\((?:[^()]|\([^()]*\))*\)|[A-Za-z_$][\w$]*)\s*=>/y;
|
|
42
|
+
|
|
43
|
+
/** Read `src`, one literal and nothing else. */
|
|
44
|
+
export function staticLiteral(src: string): StaticLiteral {
|
|
45
|
+
const reader = new Reader(src);
|
|
46
|
+
const value = reader.value();
|
|
47
|
+
reader.space();
|
|
48
|
+
return reader.i === src.length ? value : undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
class Reader {
|
|
52
|
+
i = 0;
|
|
53
|
+
constructor(private readonly src: string) {}
|
|
54
|
+
|
|
55
|
+
/** Skip whitespace and comments. */
|
|
56
|
+
space(): void {
|
|
57
|
+
const { src } = this;
|
|
58
|
+
for (;;) {
|
|
59
|
+
while (/\s/.test(src.charAt(this.i))) this.i++;
|
|
60
|
+
if (src.startsWith('//', this.i)) {
|
|
61
|
+
const end = src.indexOf('\n', this.i);
|
|
62
|
+
this.i = end < 0 ? src.length : end;
|
|
63
|
+
} else if (src.startsWith('/*', this.i)) {
|
|
64
|
+
const end = src.indexOf('*/', this.i + 2);
|
|
65
|
+
this.i = end < 0 ? src.length : end + 2;
|
|
66
|
+
} else return;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The value at the cursor, which is left after it (or anywhere, if not static). */
|
|
71
|
+
value(): StaticLiteral {
|
|
72
|
+
const { src } = this;
|
|
73
|
+
this.space();
|
|
74
|
+
const c = src.charAt(this.i);
|
|
75
|
+
if (c === '"' || c === "'") {
|
|
76
|
+
return this.string().closed ? { shape: { type: 'string' } } : undefined;
|
|
77
|
+
}
|
|
78
|
+
if (c === '`') return this.template();
|
|
79
|
+
if (c === '[')
|
|
80
|
+
return this.skipBalanced() ? { shape: { type: 'array' } } : undefined;
|
|
81
|
+
if (c === '{') return this.object();
|
|
82
|
+
if (/[\d.+-]/.test(c)) return this.number();
|
|
83
|
+
if (/[A-Za-z_$(]/.test(c)) return this.word();
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Skip the string literal at the cursor: its body, and whether it is closed. */
|
|
88
|
+
private string(): { body: string; closed: boolean } {
|
|
89
|
+
const { end, closed } = scanStringLiteral(this.src, this.i);
|
|
90
|
+
const body = this.src.slice(this.i + 1, closed ? end - 1 : end);
|
|
91
|
+
this.i = end;
|
|
92
|
+
return { body, closed };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A template literal: a string, but for `${}`. */
|
|
96
|
+
private template(): StaticLiteral {
|
|
97
|
+
const { src } = this;
|
|
98
|
+
for (let i = this.i + 1; i < src.length; i++) {
|
|
99
|
+
const c = src.charAt(i);
|
|
100
|
+
if (c === '\\') i++;
|
|
101
|
+
else if (c === '`') {
|
|
102
|
+
this.i = i + 1;
|
|
103
|
+
return { shape: { type: 'string' } };
|
|
104
|
+
} else if (c === '$' && src.charAt(i + 1) === '{') return undefined;
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A number, or a sign and one. */
|
|
110
|
+
private number(): StaticLiteral {
|
|
111
|
+
const { src } = this;
|
|
112
|
+
let i = this.i;
|
|
113
|
+
if (src.charAt(i) === '+' || src.charAt(i) === '-') {
|
|
114
|
+
i++;
|
|
115
|
+
while (/\s/.test(src.charAt(i))) i++;
|
|
116
|
+
}
|
|
117
|
+
NUMBER_RE.lastIndex = i;
|
|
118
|
+
const match = NUMBER_RE.exec(src);
|
|
119
|
+
if (match) {
|
|
120
|
+
this.i = NUMBER_RE.lastIndex;
|
|
121
|
+
return match[1]
|
|
122
|
+
? { unsupported: 'bigint' }
|
|
123
|
+
: { shape: { type: 'number' } };
|
|
124
|
+
}
|
|
125
|
+
IDENT_RE.lastIndex = i;
|
|
126
|
+
const word = IDENT_RE.exec(src)?.[0];
|
|
127
|
+
if (word === 'Infinity') {
|
|
128
|
+
this.i = IDENT_RE.lastIndex;
|
|
129
|
+
return { shape: { type: 'number' } };
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** A keyword, a function, or a name (which is not static). */
|
|
135
|
+
private word(): StaticLiteral {
|
|
136
|
+
const { src } = this;
|
|
137
|
+
ARROW_RE.lastIndex = this.i;
|
|
138
|
+
if (ARROW_RE.test(src)) return this.function();
|
|
139
|
+
IDENT_RE.lastIndex = this.i;
|
|
140
|
+
const word = IDENT_RE.exec(src)?.[0];
|
|
141
|
+
if (word === undefined) return undefined;
|
|
142
|
+
const end = IDENT_RE.lastIndex;
|
|
143
|
+
switch (word) {
|
|
144
|
+
case 'true':
|
|
145
|
+
case 'false':
|
|
146
|
+
this.i = end;
|
|
147
|
+
return { shape: { type: 'boolean' } };
|
|
148
|
+
case 'null':
|
|
149
|
+
this.i = end;
|
|
150
|
+
return { shape: { type: 'null' } };
|
|
151
|
+
case 'NaN':
|
|
152
|
+
this.i = end;
|
|
153
|
+
return { shape: { type: 'number' } };
|
|
154
|
+
case 'undefined':
|
|
155
|
+
this.i = end;
|
|
156
|
+
return { unsupported: 'undefined' };
|
|
157
|
+
case 'function':
|
|
158
|
+
case 'class':
|
|
159
|
+
case 'async':
|
|
160
|
+
return this.function();
|
|
161
|
+
}
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** A function (or class): the rest is its body, which is not read. */
|
|
166
|
+
private function(): StaticLiteral {
|
|
167
|
+
return this.unsupported({ unsupported: 'function' });
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** `value`, which makes the whole literal unsupported: the rest is not read. */
|
|
171
|
+
private unsupported(value: {
|
|
172
|
+
unsupported: 'function' | 'undefined' | 'bigint';
|
|
173
|
+
}) {
|
|
174
|
+
this.i = this.src.length;
|
|
175
|
+
return value;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** An object literal: the members that are static. */
|
|
179
|
+
private object(): StaticLiteral {
|
|
180
|
+
const { src } = this;
|
|
181
|
+
const fields = new Map<string, LiteralShape>();
|
|
182
|
+
this.i++;
|
|
183
|
+
for (;;) {
|
|
184
|
+
this.space();
|
|
185
|
+
const c = src.charAt(this.i);
|
|
186
|
+
if (c === '}') {
|
|
187
|
+
this.i++;
|
|
188
|
+
return { shape: { type: 'object', fields } };
|
|
189
|
+
}
|
|
190
|
+
if (c === ',') {
|
|
191
|
+
this.i++;
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (this.i >= src.length) return undefined;
|
|
195
|
+
const key = this.key();
|
|
196
|
+
this.space();
|
|
197
|
+
if (key !== undefined && src.charAt(this.i) === ':') {
|
|
198
|
+
this.i++;
|
|
199
|
+
const at = this.i;
|
|
200
|
+
const value = this.value();
|
|
201
|
+
this.space();
|
|
202
|
+
if (value && 'unsupported' in value) return this.unsupported(value);
|
|
203
|
+
if (value && ',}'.includes(src.charAt(this.i))) {
|
|
204
|
+
fields.set(key, value.shape);
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
// Not static: skip to the end of the member
|
|
208
|
+
this.i = at;
|
|
209
|
+
} else if (key !== undefined && src.charAt(this.i) === '(') {
|
|
210
|
+
return this.unsupported({ unsupported: 'function' });
|
|
211
|
+
}
|
|
212
|
+
if (!this.skipMember()) return undefined;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The key of a member (a name, a string or a number), if it has one. */
|
|
217
|
+
private key(): string | undefined {
|
|
218
|
+
const { src } = this;
|
|
219
|
+
const c = src.charAt(this.i);
|
|
220
|
+
if (c === '"' || c === "'") {
|
|
221
|
+
const { body, closed } = this.string();
|
|
222
|
+
return closed ? body : undefined;
|
|
223
|
+
}
|
|
224
|
+
const word = /[\w$]+/y;
|
|
225
|
+
word.lastIndex = this.i;
|
|
226
|
+
const found = word.exec(src)?.[0];
|
|
227
|
+
if (found === undefined) return undefined;
|
|
228
|
+
this.i = word.lastIndex;
|
|
229
|
+
return found;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Skip to the `,` or `}` that ends a member, balancing what is inside. */
|
|
233
|
+
private skipMember(): boolean {
|
|
234
|
+
const { src } = this;
|
|
235
|
+
while (this.i < src.length) {
|
|
236
|
+
const c = src.charAt(this.i);
|
|
237
|
+
if (c === ',' || c === '}') return true;
|
|
238
|
+
if (!this.skipOne()) return false;
|
|
239
|
+
}
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Skip an array literal (or any bracketed group) at the cursor. */
|
|
244
|
+
private skipBalanced(): boolean {
|
|
245
|
+
const { src } = this;
|
|
246
|
+
const close = { '[': ']', '{': '}', '(': ')' }[src.charAt(this.i)];
|
|
247
|
+
if (!close) return false;
|
|
248
|
+
this.i++;
|
|
249
|
+
while (this.i < src.length) {
|
|
250
|
+
if (src.charAt(this.i) === close) {
|
|
251
|
+
this.i++;
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
if (!this.skipOne()) return false;
|
|
255
|
+
}
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Skip one character, or one string, template, comment or group. */
|
|
260
|
+
private skipOne(): boolean {
|
|
261
|
+
const { src } = this;
|
|
262
|
+
const c = src.charAt(this.i);
|
|
263
|
+
if (c === '"' || c === "'") return this.string().closed;
|
|
264
|
+
if (c === '`') return this.skipTemplate();
|
|
265
|
+
if (src.startsWith('//', this.i) || src.startsWith('/*', this.i)) {
|
|
266
|
+
this.space();
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
if (c === '[' || c === '{' || c === '(') return this.skipBalanced();
|
|
270
|
+
if (c === ']' || c === '}' || c === ')') return false;
|
|
271
|
+
this.i++;
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** Skip a template literal, with the expressions in it. */
|
|
276
|
+
private skipTemplate(): boolean {
|
|
277
|
+
const { src } = this;
|
|
278
|
+
this.i++;
|
|
279
|
+
while (this.i < src.length) {
|
|
280
|
+
const c = src.charAt(this.i);
|
|
281
|
+
if (c === '\\') this.i += 2;
|
|
282
|
+
else if (c === '`') {
|
|
283
|
+
this.i++;
|
|
284
|
+
return true;
|
|
285
|
+
} else if (c === '$' && src.charAt(this.i + 1) === '{') {
|
|
286
|
+
this.i++;
|
|
287
|
+
if (!this.skipBalanced()) return false;
|
|
288
|
+
} else this.i++;
|
|
289
|
+
}
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
}
|
package/src/store.ts
CHANGED
|
@@ -989,11 +989,14 @@ export interface StoryState {
|
|
|
989
989
|
* passed to the `beforeload`/`afterload` events. Pass the save's
|
|
990
990
|
* `playthroughId` when loading a save: the game moves to that playthrough
|
|
991
991
|
* (no change if it is the current one). Restoring the session passes none.
|
|
992
|
+
* `issued` is the place in the order of replacements a load that read
|
|
993
|
+
* before applying took at its call (see issueStateReplacement).
|
|
992
994
|
*/
|
|
993
995
|
loadFromPayload: (
|
|
994
996
|
payload: SavePayload,
|
|
995
997
|
slot?: string,
|
|
996
998
|
playthroughId?: string,
|
|
999
|
+
issued?: number,
|
|
997
1000
|
) => void;
|
|
998
1001
|
getHistoryVariables: (index: number) => Record<string, unknown>;
|
|
999
1002
|
setTransition: (config: TransitionConfig | null) => void;
|
|
@@ -1722,8 +1725,9 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1722
1725
|
payload: SavePayload,
|
|
1723
1726
|
slot?: string,
|
|
1724
1727
|
playthroughId?: string,
|
|
1728
|
+
issued?: number,
|
|
1725
1729
|
) => {
|
|
1726
|
-
const applying = slotLoadApplying;
|
|
1730
|
+
const applying = slotLoadApplying ?? issued;
|
|
1727
1731
|
slotLoadApplying = null;
|
|
1728
1732
|
const leaving = slotLoadLeaving;
|
|
1729
1733
|
slotLoadLeaving = null;
|
package/src/story-variables.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Passage } from './parser';
|
|
2
|
-
import type { Token } from './markup/tokens';
|
|
2
|
+
import type { Spans, Token } from './markup/tokens';
|
|
3
3
|
import { MarkupError, tokenizeMarkup } from './markup/parse';
|
|
4
4
|
import { isCodeAttribute, splitSigilTemplate } from './markup/code-attributes';
|
|
5
5
|
import { errorMessage } from './utils/error-message';
|
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
import { getMacroParameters, type ParameterDef } from './registry';
|
|
19
19
|
import { NOT_MARKUP } from './markup/validate';
|
|
20
20
|
import { createNamespace, variableNameError } from './utils/namespace';
|
|
21
|
+
import { staticLiteral, type LiteralShape } from './static-literal';
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* The tokens of markup, or none if it is malformed: validateMarkup reports
|
|
@@ -102,32 +103,169 @@ function inferSchema(
|
|
|
102
103
|
return { type: jsType as VarType };
|
|
103
104
|
}
|
|
104
105
|
|
|
106
|
+
/** What is wrong with a declaration (see DeclarationError). */
|
|
107
|
+
export type DeclarationErrorCode =
|
|
108
|
+
| 'invalid-declaration'
|
|
109
|
+
| 'invalid-name'
|
|
110
|
+
| 'duplicate-declaration'
|
|
111
|
+
| 'unsupported-value';
|
|
112
|
+
|
|
105
113
|
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
114
|
+
* A declaration of a StoryVariables or StoryTransients passage: the name
|
|
115
|
+
* (without the sigil) and the initializer expression (without surrounding
|
|
116
|
+
* space) are written from `nameStart` to `nameEnd` and from `valueStart` to
|
|
117
|
+
* `valueEnd` (UTF-16 offsets).
|
|
108
118
|
*/
|
|
109
|
-
export
|
|
119
|
+
export interface Declaration extends Spans<'name'>, Spans<'value'> {
|
|
120
|
+
name: string;
|
|
121
|
+
/**
|
|
122
|
+
* The shape of the initializer when it is static: a literal, or an array or
|
|
123
|
+
* object literal of them (an object has the members that are static).
|
|
124
|
+
* Absent when it is not.
|
|
125
|
+
*/
|
|
126
|
+
schema?: FieldSchema;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* An invalid line of a StoryVariables or StoryTransients passage, from
|
|
131
|
+
* `offset` up to `end` (UTF-16 offsets into the content).
|
|
132
|
+
*/
|
|
133
|
+
export interface DeclarationError extends Record<'offset' | 'end', number> {
|
|
134
|
+
code: DeclarationErrorCode;
|
|
135
|
+
/** What is wrong, as parseStoryVariables throws it after the passage name. */
|
|
136
|
+
message: string;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const UNSUPPORTED_TYPE = 'Expected number, string, boolean, array, or object.';
|
|
140
|
+
|
|
141
|
+
/** The schema of a literal shape (see staticLiteral). */
|
|
142
|
+
function shapeSchema(shape: LiteralShape): FieldSchema {
|
|
143
|
+
if (!shape.fields) return { type: shape.type };
|
|
144
|
+
const fields = new Map<string, FieldSchema>();
|
|
145
|
+
for (const [key, member] of shape.fields)
|
|
146
|
+
fields.set(key, shapeSchema(member));
|
|
147
|
+
return { type: shape.type, fields };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Read the declarations of a StoryVariables (`$name = value`) or
|
|
152
|
+
* StoryTransients (`%name = value`) passage, without evaluating them and
|
|
153
|
+
* without throwing: every valid declaration, and every invalid line as an
|
|
154
|
+
* error. It is the grammar parseStoryVariables reads with.
|
|
155
|
+
*
|
|
156
|
+
* A declaration is a line of the form `$name = expression`; blank lines are
|
|
157
|
+
* skipped. A line that is not, and a name that cannot be a variable, are
|
|
158
|
+
* errors. So is an initializer that no variable can hold (a function,
|
|
159
|
+
* `undefined`), when it is a literal; whether any other initializer can only
|
|
160
|
+
* be known by running it. A name declared again is an error too, but both
|
|
161
|
+
* declarations are returned (the later wins when evaluated).
|
|
162
|
+
*/
|
|
163
|
+
export function parseDeclarations(
|
|
110
164
|
content: string,
|
|
111
165
|
sigil: '$' | '%' = '$',
|
|
112
|
-
):
|
|
113
|
-
const
|
|
166
|
+
): { declarations: Declaration[]; errors: DeclarationError[] } {
|
|
167
|
+
const declarations: Declaration[] = [];
|
|
168
|
+
const errors: DeclarationError[] = [];
|
|
114
169
|
const DECLARATION_RE = declarationRegex(sigil);
|
|
115
|
-
const
|
|
170
|
+
const seen = new Set<string>();
|
|
116
171
|
|
|
172
|
+
let lineStart = 0;
|
|
117
173
|
for (const rawLine of content.split('\n')) {
|
|
174
|
+
const at = lineStart + rawLine.length - rawLine.trimStart().length;
|
|
175
|
+
lineStart += rawLine.length + 1;
|
|
118
176
|
const line = rawLine.trim();
|
|
119
177
|
if (!line) continue;
|
|
120
178
|
|
|
121
179
|
const match = line.match(DECLARATION_RE);
|
|
180
|
+
const error = (
|
|
181
|
+
code: DeclarationErrorCode,
|
|
182
|
+
start: number,
|
|
183
|
+
end: number,
|
|
184
|
+
message: string,
|
|
185
|
+
) => errors.push({ code, offset: start, end, message });
|
|
122
186
|
if (!match) {
|
|
123
|
-
|
|
124
|
-
|
|
187
|
+
error(
|
|
188
|
+
'invalid-declaration',
|
|
189
|
+
at,
|
|
190
|
+
at + line.length,
|
|
191
|
+
`Invalid declaration: "${line}". Expected: ${sigil}name = value`,
|
|
125
192
|
);
|
|
193
|
+
continue;
|
|
126
194
|
}
|
|
127
195
|
|
|
128
196
|
const [, name, expr] = match as [string, string, string];
|
|
197
|
+
const nameStart = at + sigil.length;
|
|
198
|
+
const nameEnd = nameStart + name.length;
|
|
129
199
|
const nameError = variableNameError(name, sigil + name);
|
|
130
|
-
if (nameError)
|
|
200
|
+
if (nameError) {
|
|
201
|
+
error('invalid-name', nameStart, nameEnd, nameError);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const valueEnd = at + line.length;
|
|
205
|
+
const declaration: Declaration = {
|
|
206
|
+
name,
|
|
207
|
+
nameStart,
|
|
208
|
+
nameEnd,
|
|
209
|
+
valueStart: valueEnd - expr.length,
|
|
210
|
+
valueEnd,
|
|
211
|
+
};
|
|
212
|
+
if (seen.has(name)) {
|
|
213
|
+
error(
|
|
214
|
+
'duplicate-declaration',
|
|
215
|
+
nameStart,
|
|
216
|
+
nameEnd,
|
|
217
|
+
`Duplicate declaration of ${sigil}${name}`,
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
seen.add(name);
|
|
221
|
+
const literal = staticLiteral(expr);
|
|
222
|
+
if (literal && 'shape' in literal) {
|
|
223
|
+
declaration.schema = shapeSchema(literal.shape);
|
|
224
|
+
} else if (literal) {
|
|
225
|
+
error(
|
|
226
|
+
'unsupported-value',
|
|
227
|
+
declaration.valueStart,
|
|
228
|
+
valueEnd,
|
|
229
|
+
`Unsupported type "${literal.unsupported}" for value ${expr}. ${UNSUPPORTED_TYPE}`,
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
declarations.push(declaration);
|
|
233
|
+
}
|
|
234
|
+
return { declarations, errors };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Parse a StoryVariables or StoryTransients passage content into a schema map.
|
|
239
|
+
* Each line: `$varName = expression` (or `%varName = expression` for transients)
|
|
240
|
+
* The expressions are evaluated; parseDeclarations reads the same lines
|
|
241
|
+
* without.
|
|
242
|
+
*/
|
|
243
|
+
export function parseStoryVariables(
|
|
244
|
+
content: string,
|
|
245
|
+
sigil: '$' | '%' = '$',
|
|
246
|
+
): Map<string, VariableSchema> {
|
|
247
|
+
const schema = new Map<string, VariableSchema>();
|
|
248
|
+
const passageName = sigil === '%' ? 'StoryTransients' : 'StoryVariables';
|
|
249
|
+
const { declarations, errors } = parseDeclarations(content, sigil);
|
|
250
|
+
|
|
251
|
+
// Lines in order: the first that is invalid, or that cannot be evaluated,
|
|
252
|
+
// is the error. What parseDeclarations reports about a value that is
|
|
253
|
+
// evaluated here is left to the evaluation (its wording is the runtime's).
|
|
254
|
+
const lines = [
|
|
255
|
+
...errors
|
|
256
|
+
.filter(
|
|
257
|
+
(e) => e.code === 'invalid-declaration' || e.code === 'invalid-name',
|
|
258
|
+
)
|
|
259
|
+
.map((e) => ({ at: e.offset, error: e })),
|
|
260
|
+
...declarations.map((d) => ({ at: d.nameStart, declaration: d })),
|
|
261
|
+
].sort((a, b) => a.at - b.at);
|
|
262
|
+
|
|
263
|
+
for (const line of lines) {
|
|
264
|
+
if ('error' in line) {
|
|
265
|
+
throw new Error(`${passageName}: ${line.error.message}`);
|
|
266
|
+
}
|
|
267
|
+
const { name, valueStart, valueEnd } = line.declaration;
|
|
268
|
+
const expr = content.slice(valueStart, valueEnd);
|
|
131
269
|
let value: unknown;
|
|
132
270
|
try {
|
|
133
271
|
value = new Function('return (' + expr + ')')();
|
package/src/tooling.ts
CHANGED
|
@@ -12,14 +12,25 @@ import {
|
|
|
12
12
|
import { isBlockMacro } from './markup/ast';
|
|
13
13
|
import { tokenizeMarkupTolerant } from './markup/parse';
|
|
14
14
|
import {
|
|
15
|
-
collectPassageReferences,
|
|
16
15
|
parameterLookup,
|
|
16
|
+
passagePiecesOf,
|
|
17
|
+
pieceOffset,
|
|
18
|
+
referencesOf,
|
|
17
19
|
type PassageReference,
|
|
20
|
+
type Piece,
|
|
18
21
|
} from './code-check';
|
|
19
22
|
import { blockWidgetNames } from './widgets/widget-def';
|
|
20
23
|
import type { ParameterDef } from './registry';
|
|
21
24
|
|
|
22
|
-
export { parseStoryVariables } from './story-variables';
|
|
25
|
+
export { parseDeclarations, parseStoryVariables } from './story-variables';
|
|
26
|
+
export type {
|
|
27
|
+
Declaration,
|
|
28
|
+
DeclarationError,
|
|
29
|
+
DeclarationErrorCode,
|
|
30
|
+
FieldSchema,
|
|
31
|
+
VarType,
|
|
32
|
+
VariableSchema,
|
|
33
|
+
} from './story-variables';
|
|
23
34
|
export { checkParameterTypes } from './registry';
|
|
24
35
|
export { formatDiagnostic } from './markup/validate';
|
|
25
36
|
|
|
@@ -40,7 +51,32 @@ export {
|
|
|
40
51
|
} from './markup/parse';
|
|
41
52
|
export type { ParseMarkupOptions, TolerantTokens } from './markup/parse';
|
|
42
53
|
export { SIGIL_SCOPES, isSigil } from './markup/tokens';
|
|
43
|
-
export type {
|
|
54
|
+
export type {
|
|
55
|
+
AttributeSpan,
|
|
56
|
+
ExpressionToken,
|
|
57
|
+
HtmlToken,
|
|
58
|
+
LinkToken,
|
|
59
|
+
MacroToken,
|
|
60
|
+
SelectorSpan,
|
|
61
|
+
Selectors,
|
|
62
|
+
TextToken,
|
|
63
|
+
Token,
|
|
64
|
+
VariableScope,
|
|
65
|
+
VariableToken,
|
|
66
|
+
} from './markup/tokens';
|
|
67
|
+
export { pairMarkup } from './markup/pair';
|
|
68
|
+
export { pieceOffset };
|
|
69
|
+
export type {
|
|
70
|
+
PairedBody,
|
|
71
|
+
PairedBranch,
|
|
72
|
+
PairedMarkup,
|
|
73
|
+
PairedNode,
|
|
74
|
+
PairingError,
|
|
75
|
+
PairingErrorCode,
|
|
76
|
+
PairMarkupOptions,
|
|
77
|
+
} from './markup/pair';
|
|
78
|
+
export { isBlockMacro } from './markup/ast';
|
|
79
|
+
export type { MarkupErrorCode } from './markup/parse';
|
|
44
80
|
export {
|
|
45
81
|
endsWithOperator,
|
|
46
82
|
readQuoted,
|
|
@@ -56,8 +92,21 @@ export {
|
|
|
56
92
|
} from './components/macros/macro-args';
|
|
57
93
|
export type { PassageTarget } from './components/macros/macro-args';
|
|
58
94
|
export { transform } from './transform';
|
|
59
|
-
export type {
|
|
60
|
-
|
|
95
|
+
export type {
|
|
96
|
+
ArgumentErrorPiece,
|
|
97
|
+
CodePiece,
|
|
98
|
+
PassagePiece,
|
|
99
|
+
PassageReference,
|
|
100
|
+
Piece,
|
|
101
|
+
PieceBase,
|
|
102
|
+
TextPiece,
|
|
103
|
+
} from './code-check';
|
|
104
|
+
export type {
|
|
105
|
+
MarkupDiagnostic,
|
|
106
|
+
MarkupDiagnosticCode,
|
|
107
|
+
MarkupDiagnosticData,
|
|
108
|
+
MarkupPassage,
|
|
109
|
+
} from './markup/validate';
|
|
61
110
|
|
|
62
111
|
/** What tooling knows about a macro (see MacroMetadata). */
|
|
63
112
|
/** Options for validating markup from tooling. */
|
|
@@ -117,15 +166,47 @@ export function validateStoryMarkup(
|
|
|
117
166
|
});
|
|
118
167
|
}
|
|
119
168
|
|
|
169
|
+
/** Whether a macro takes a body, among `macros` and the built-in ones. */
|
|
170
|
+
function blockLookup(macros: readonly ToolingMacro[]) {
|
|
171
|
+
const blocks = new Set(
|
|
172
|
+
macros.filter((m) => m.block).map((m) => m.name.toLowerCase()),
|
|
173
|
+
);
|
|
174
|
+
return (name: string) => blocks.has(name.toLowerCase()) || isBlockMacro(name);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* What a passage's markup runs and names, and where: the pieces of code
|
|
179
|
+
* (`{$expr}` displays, `{do}` bodies, conditions, macro arguments by their
|
|
180
|
+
* declared parameter types, strings that hold code), the passage names written
|
|
181
|
+
* out (links, `passage` arguments), the texts that hold markup of their own
|
|
182
|
+
* (labels, attribute values) and the arguments that don't have their
|
|
183
|
+
* parameters' forms, in source order with offsets (UTF-16) into `source`. The
|
|
184
|
+
* markup in a text follows it, as pieces marked `nested` with offsets into
|
|
185
|
+
* `source` too. Malformed tags are skipped, so it reads half-typed markup.
|
|
186
|
+
* The macros are those the markup may use, built-in and user-defined.
|
|
187
|
+
*/
|
|
188
|
+
export function passagePieces(
|
|
189
|
+
source: string,
|
|
190
|
+
macros: Iterable<ToolingMacro> = [],
|
|
191
|
+
): Piece[] {
|
|
192
|
+
const all = [...macros];
|
|
193
|
+
const { tokens } = tokenizeMarkupTolerant(source);
|
|
194
|
+
return [
|
|
195
|
+
...passagePiecesOf(source, tokens, parameterLookup(all), {
|
|
196
|
+
isBlock: blockLookup(all),
|
|
197
|
+
}),
|
|
198
|
+
];
|
|
199
|
+
}
|
|
200
|
+
|
|
120
201
|
/**
|
|
121
202
|
* The passages the markup of `source` names (see PassageReference), against
|
|
122
203
|
* the given macros: what the story-start check looks up. Malformed tags are
|
|
123
|
-
* skipped, so it reads half-typed markup.
|
|
204
|
+
* skipped, so it reads half-typed markup. It is `passagePieces`, less what
|
|
205
|
+
* names no passage.
|
|
124
206
|
*/
|
|
125
207
|
export function collectStoryPassageReferences(
|
|
126
208
|
source: string,
|
|
127
209
|
macros: Iterable<ToolingMacro>,
|
|
128
210
|
): PassageReference[] {
|
|
129
|
-
|
|
130
|
-
return collectPassageReferences(source, tokens, parameterLookup(macros));
|
|
211
|
+
return referencesOf(passagePieces(source, macros));
|
|
131
212
|
}
|