squiffy-compiler 6.0.1 → 7.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analyse.d.ts +77 -0
- package/dist/analyse.js +520 -0
- package/dist/compiler.d.ts +12 -40
- package/dist/compiler.js +18 -691
- package/dist/emit.d.ts +43 -0
- package/dist/emit.js +114 -0
- package/dist/language-service.d.ts +77 -0
- package/dist/language-service.js +669 -0
- package/dist/parse.d.ts +17 -0
- package/dist/parse.js +258 -0
- package/dist/syntax.d.ts +146 -0
- package/dist/syntax.js +8 -0
- package/dist/text.d.ts +37 -0
- package/dist/text.js +288 -0
- package/package.json +16 -5
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { Diagnostic, PassageNode, SectionNode, SquiffyFile } from "./syntax.js";
|
|
2
|
+
import { SourceLine } from "./text.js";
|
|
3
|
+
export interface ExternalFiles {
|
|
4
|
+
getMatchingFilenames(pattern: string): Promise<string[]>;
|
|
5
|
+
getContent(filename: string): Promise<string>;
|
|
6
|
+
getLocalFilename(filename: string): string;
|
|
7
|
+
}
|
|
8
|
+
export interface AnalyseSettings {
|
|
9
|
+
scriptBaseFilename?: string;
|
|
10
|
+
script: string;
|
|
11
|
+
externalFiles?: ExternalFiles;
|
|
12
|
+
parseCache?: ParseCache;
|
|
13
|
+
recover?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare class ParseCache {
|
|
16
|
+
private files;
|
|
17
|
+
parse(text: string, filename?: string): SquiffyFile;
|
|
18
|
+
}
|
|
19
|
+
export interface SectionSymbol {
|
|
20
|
+
name: string;
|
|
21
|
+
file: string;
|
|
22
|
+
node?: SectionNode;
|
|
23
|
+
passages: PassageSymbol[];
|
|
24
|
+
}
|
|
25
|
+
export interface PassageSymbol {
|
|
26
|
+
name: string;
|
|
27
|
+
node: PassageNode;
|
|
28
|
+
}
|
|
29
|
+
export interface Analysis {
|
|
30
|
+
success: boolean;
|
|
31
|
+
files: SquiffyFile[];
|
|
32
|
+
diagnostics: Diagnostic[];
|
|
33
|
+
sections: SectionSymbol[];
|
|
34
|
+
}
|
|
35
|
+
export declare class Story {
|
|
36
|
+
sections: Record<string, Section>;
|
|
37
|
+
title: string;
|
|
38
|
+
scripts: string[];
|
|
39
|
+
stylesheets: string[];
|
|
40
|
+
start: string;
|
|
41
|
+
id: string | null;
|
|
42
|
+
uiJs: string[];
|
|
43
|
+
constructor();
|
|
44
|
+
addSection(name: string, filename: string | undefined, line: number, node: SectionNode | undefined): Section;
|
|
45
|
+
}
|
|
46
|
+
declare class TextContainer {
|
|
47
|
+
text: string[];
|
|
48
|
+
textSources: SourceLine[];
|
|
49
|
+
js: string[];
|
|
50
|
+
clear: boolean;
|
|
51
|
+
attributes: string[];
|
|
52
|
+
output: string;
|
|
53
|
+
addText(text: string, source: SourceLine): void;
|
|
54
|
+
}
|
|
55
|
+
export declare class Section extends TextContainer {
|
|
56
|
+
name: string;
|
|
57
|
+
filename: string | undefined;
|
|
58
|
+
line: number;
|
|
59
|
+
node: SectionNode | undefined;
|
|
60
|
+
passages: Record<string, Passage>;
|
|
61
|
+
constructor(name: string, filename: string | undefined, line: number, node: SectionNode | undefined);
|
|
62
|
+
addPassage(name: string, line: number, node: PassageNode): Passage;
|
|
63
|
+
}
|
|
64
|
+
export declare class Passage extends TextContainer {
|
|
65
|
+
name: string;
|
|
66
|
+
line: number;
|
|
67
|
+
node: PassageNode;
|
|
68
|
+
constructor(name: string, line: number, node: PassageNode);
|
|
69
|
+
}
|
|
70
|
+
export interface AnalysisInternals {
|
|
71
|
+
story: Story;
|
|
72
|
+
legacyErrors: string[];
|
|
73
|
+
legacyWarnings: string[];
|
|
74
|
+
}
|
|
75
|
+
export declare const analysisInternals: WeakMap<Analysis, AnalysisInternals>;
|
|
76
|
+
export declare function analyse(settings: AnalyseSettings): Promise<Analysis>;
|
|
77
|
+
export {};
|
package/dist/analyse.js
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
import { parse } from "./parse.js";
|
|
2
|
+
import { positionMapper, transformText } from "./text.js";
|
|
3
|
+
import { findV5Syntax } from "./v5.js";
|
|
4
|
+
// Caches the syntax tree of each file by filename, reparsing a file only when its text changes.
|
|
5
|
+
export class ParseCache {
|
|
6
|
+
constructor() {
|
|
7
|
+
Object.defineProperty(this, "files", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
configurable: true,
|
|
10
|
+
writable: true,
|
|
11
|
+
value: new Map()
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
parse(text, filename) {
|
|
15
|
+
const key = filename ?? "";
|
|
16
|
+
const cached = this.files.get(key);
|
|
17
|
+
if (cached && cached.text === text) {
|
|
18
|
+
return cached.file;
|
|
19
|
+
}
|
|
20
|
+
const file = parse(text, filename);
|
|
21
|
+
this.files.set(key, { text, file });
|
|
22
|
+
return file;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// ---- Story model, which emit() turns into output ----
|
|
26
|
+
export class Story {
|
|
27
|
+
constructor() {
|
|
28
|
+
Object.defineProperty(this, "sections", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: {}
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(this, "title", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: ""
|
|
39
|
+
});
|
|
40
|
+
Object.defineProperty(this, "scripts", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
value: []
|
|
45
|
+
});
|
|
46
|
+
Object.defineProperty(this, "stylesheets", {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
configurable: true,
|
|
49
|
+
writable: true,
|
|
50
|
+
value: []
|
|
51
|
+
});
|
|
52
|
+
Object.defineProperty(this, "start", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
configurable: true,
|
|
55
|
+
writable: true,
|
|
56
|
+
value: ""
|
|
57
|
+
});
|
|
58
|
+
Object.defineProperty(this, "id", {
|
|
59
|
+
enumerable: true,
|
|
60
|
+
configurable: true,
|
|
61
|
+
writable: true,
|
|
62
|
+
value: null
|
|
63
|
+
});
|
|
64
|
+
Object.defineProperty(this, "uiJs", {
|
|
65
|
+
enumerable: true,
|
|
66
|
+
configurable: true,
|
|
67
|
+
writable: true,
|
|
68
|
+
value: []
|
|
69
|
+
});
|
|
70
|
+
this.id = crypto.randomUUID();
|
|
71
|
+
}
|
|
72
|
+
addSection(name, filename, line, node) {
|
|
73
|
+
const section = new Section(name, filename, line, node);
|
|
74
|
+
this.sections[name] = section;
|
|
75
|
+
return section;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
class TextContainer {
|
|
79
|
+
constructor() {
|
|
80
|
+
Object.defineProperty(this, "text", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true,
|
|
84
|
+
value: []
|
|
85
|
+
});
|
|
86
|
+
Object.defineProperty(this, "textSources", {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
configurable: true,
|
|
89
|
+
writable: true,
|
|
90
|
+
value: []
|
|
91
|
+
});
|
|
92
|
+
Object.defineProperty(this, "js", {
|
|
93
|
+
enumerable: true,
|
|
94
|
+
configurable: true,
|
|
95
|
+
writable: true,
|
|
96
|
+
value: []
|
|
97
|
+
});
|
|
98
|
+
Object.defineProperty(this, "clear", {
|
|
99
|
+
enumerable: true,
|
|
100
|
+
configurable: true,
|
|
101
|
+
writable: true,
|
|
102
|
+
value: false
|
|
103
|
+
});
|
|
104
|
+
Object.defineProperty(this, "attributes", {
|
|
105
|
+
enumerable: true,
|
|
106
|
+
configurable: true,
|
|
107
|
+
writable: true,
|
|
108
|
+
value: []
|
|
109
|
+
});
|
|
110
|
+
// The text after links have been converted
|
|
111
|
+
Object.defineProperty(this, "output", {
|
|
112
|
+
enumerable: true,
|
|
113
|
+
configurable: true,
|
|
114
|
+
writable: true,
|
|
115
|
+
value: ""
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
addText(text, source) {
|
|
119
|
+
this.text.push(text);
|
|
120
|
+
this.textSources.push(source);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
export class Section extends TextContainer {
|
|
124
|
+
constructor(name, filename, line, node) {
|
|
125
|
+
super();
|
|
126
|
+
Object.defineProperty(this, "name", {
|
|
127
|
+
enumerable: true,
|
|
128
|
+
configurable: true,
|
|
129
|
+
writable: true,
|
|
130
|
+
value: name
|
|
131
|
+
});
|
|
132
|
+
Object.defineProperty(this, "filename", {
|
|
133
|
+
enumerable: true,
|
|
134
|
+
configurable: true,
|
|
135
|
+
writable: true,
|
|
136
|
+
value: filename
|
|
137
|
+
});
|
|
138
|
+
Object.defineProperty(this, "line", {
|
|
139
|
+
enumerable: true,
|
|
140
|
+
configurable: true,
|
|
141
|
+
writable: true,
|
|
142
|
+
value: line
|
|
143
|
+
});
|
|
144
|
+
Object.defineProperty(this, "node", {
|
|
145
|
+
enumerable: true,
|
|
146
|
+
configurable: true,
|
|
147
|
+
writable: true,
|
|
148
|
+
value: node
|
|
149
|
+
});
|
|
150
|
+
Object.defineProperty(this, "passages", {
|
|
151
|
+
enumerable: true,
|
|
152
|
+
configurable: true,
|
|
153
|
+
writable: true,
|
|
154
|
+
value: {}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
addPassage(name, line, node) {
|
|
158
|
+
const passage = new Passage(name, line, node);
|
|
159
|
+
this.passages[name] = passage;
|
|
160
|
+
return passage;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
export class Passage extends TextContainer {
|
|
164
|
+
constructor(name, line, node) {
|
|
165
|
+
super();
|
|
166
|
+
Object.defineProperty(this, "name", {
|
|
167
|
+
enumerable: true,
|
|
168
|
+
configurable: true,
|
|
169
|
+
writable: true,
|
|
170
|
+
value: name
|
|
171
|
+
});
|
|
172
|
+
Object.defineProperty(this, "line", {
|
|
173
|
+
enumerable: true,
|
|
174
|
+
configurable: true,
|
|
175
|
+
writable: true,
|
|
176
|
+
value: line
|
|
177
|
+
});
|
|
178
|
+
Object.defineProperty(this, "node", {
|
|
179
|
+
enumerable: true,
|
|
180
|
+
configurable: true,
|
|
181
|
+
writable: true,
|
|
182
|
+
value: node
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
export const analysisInternals = new WeakMap();
|
|
187
|
+
const lineRange = (node) => node.range;
|
|
188
|
+
// Resolves @imports, builds the story's sections and passages, and reports diagnostics.
|
|
189
|
+
export async function analyse(settings) {
|
|
190
|
+
const story = new Story();
|
|
191
|
+
const cache = settings.parseCache ?? new ParseCache();
|
|
192
|
+
const files = [];
|
|
193
|
+
const diagnostics = [];
|
|
194
|
+
const legacyErrors = [];
|
|
195
|
+
const legacyWarnings = [];
|
|
196
|
+
let autoSectionCount = 0;
|
|
197
|
+
const report = (severity, code, filename, range, message, legacyLine) => {
|
|
198
|
+
diagnostics.push({ file: filename ?? "", range, severity, code, message });
|
|
199
|
+
const legacy = `${severity.toUpperCase()}: ${filename} line ${legacyLine}: ${message}`;
|
|
200
|
+
(severity == "error" ? legacyErrors : legacyWarnings).push(legacy);
|
|
201
|
+
};
|
|
202
|
+
async function processFile(text, filename, isFirst) {
|
|
203
|
+
const file = cache.parse(text, filename);
|
|
204
|
+
files.push(file);
|
|
205
|
+
const lines = text.replace(/\r/g, "").split("\n");
|
|
206
|
+
warnAboutV5Syntax(text, lines, filename);
|
|
207
|
+
let section = null;
|
|
208
|
+
let passage = null;
|
|
209
|
+
let inUiBlock = false;
|
|
210
|
+
const ensureSectionExists = (line) => {
|
|
211
|
+
if (!section && isFirst) {
|
|
212
|
+
return story.addSection("_default", filename, line, file.preamble);
|
|
213
|
+
}
|
|
214
|
+
return section;
|
|
215
|
+
};
|
|
216
|
+
const addAutoSection = (node, line) => {
|
|
217
|
+
autoSectionCount++;
|
|
218
|
+
const autoSectionName = `_continue${autoSectionCount}`;
|
|
219
|
+
section = story.addSection(autoSectionName, filename, line, node);
|
|
220
|
+
passage = null;
|
|
221
|
+
return autoSectionName;
|
|
222
|
+
};
|
|
223
|
+
const sectionRequired = (node) => {
|
|
224
|
+
report("error", "squiffy/content-before-section", filename, lineRange(node), "This needs to be in a section, but it comes before the first section in this file.", node.range.start.line + 1);
|
|
225
|
+
return false;
|
|
226
|
+
};
|
|
227
|
+
const source = (node) => ({ line: node.range.start.line, length: lines[node.range.start.line].length });
|
|
228
|
+
const handleSectionHeader = (node) => {
|
|
229
|
+
const header = node.header;
|
|
230
|
+
const lineCount = header.range.start.line + 1;
|
|
231
|
+
switch (header.type) {
|
|
232
|
+
case "section-header":
|
|
233
|
+
section = story.addSection(header.name, filename, lineCount, node);
|
|
234
|
+
passage = null;
|
|
235
|
+
return true;
|
|
236
|
+
case "continue": {
|
|
237
|
+
const previousSection = ensureSectionExists(lineCount);
|
|
238
|
+
if (!previousSection)
|
|
239
|
+
return sectionRequired(header);
|
|
240
|
+
const autoSectionName = addAutoSection(node, lineCount);
|
|
241
|
+
previousSection.addText(`[[${header.text || "Continue..."}]](${autoSectionName})`, source(header));
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
case "separator":
|
|
245
|
+
return handleSeparator(node, lineCount);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
const handleSeparator = (node, lineCount) => {
|
|
249
|
+
inUiBlock = false;
|
|
250
|
+
if (!section) {
|
|
251
|
+
// Just add the _default section if we haven't started yet
|
|
252
|
+
ensureSectionExists(lineCount);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
addAutoSection(node ?? file.preamble, lineCount);
|
|
256
|
+
}
|
|
257
|
+
return true;
|
|
258
|
+
};
|
|
259
|
+
const handlePassageHeader = (node) => {
|
|
260
|
+
const lineCount = node.header.range.start.line + 1;
|
|
261
|
+
if (!section) {
|
|
262
|
+
report("error", "squiffy/passage-without-section", filename, node.nameRange, `Can't add passage "${node.name}" as no section has been created.`, lineCount);
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
passage = section.addPassage(node.name, lineCount, node);
|
|
266
|
+
return true;
|
|
267
|
+
};
|
|
268
|
+
const handleStatement = async (node) => {
|
|
269
|
+
const lineCount = node.range.start.line + 1;
|
|
270
|
+
switch (node.type) {
|
|
271
|
+
case "separator":
|
|
272
|
+
return handleSeparator(null, lineCount);
|
|
273
|
+
case "ui-block-start":
|
|
274
|
+
inUiBlock = true;
|
|
275
|
+
return true;
|
|
276
|
+
case "ui-file":
|
|
277
|
+
if (settings.externalFiles) {
|
|
278
|
+
story.uiJs.push(await settings.externalFiles.getContent(node.path));
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
warnExternalFilesUnavailable(`@ui ${node.path}`, node, filename);
|
|
282
|
+
}
|
|
283
|
+
return true;
|
|
284
|
+
case "clear":
|
|
285
|
+
if (!passage) {
|
|
286
|
+
section = ensureSectionExists(lineCount);
|
|
287
|
+
if (!section)
|
|
288
|
+
return sectionRequired(node);
|
|
289
|
+
section.clear = true;
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
passage.clear = true;
|
|
293
|
+
}
|
|
294
|
+
return true;
|
|
295
|
+
case "title":
|
|
296
|
+
story.title = node.value;
|
|
297
|
+
return true;
|
|
298
|
+
case "start":
|
|
299
|
+
story.start = node.value;
|
|
300
|
+
return true;
|
|
301
|
+
case "import":
|
|
302
|
+
if (!settings.externalFiles) {
|
|
303
|
+
warnExternalFilesUnavailable(`@import ${node.pattern}`, node, filename);
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
for (const importFilename of await settings.externalFiles.getMatchingFilenames(node.pattern)) {
|
|
307
|
+
if (importFilename.endsWith(".squiffy")) {
|
|
308
|
+
const content = await settings.externalFiles.getContent(importFilename);
|
|
309
|
+
const success = await processFile(content, importFilename, false);
|
|
310
|
+
if (!success && !settings.recover)
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
else if (importFilename.endsWith(".js")) {
|
|
314
|
+
story.scripts.push(settings.externalFiles.getLocalFilename(importFilename));
|
|
315
|
+
}
|
|
316
|
+
else if (importFilename.endsWith(".css")) {
|
|
317
|
+
story.stylesheets.push(settings.externalFiles.getLocalFilename(importFilename));
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return true;
|
|
321
|
+
case "attribute": {
|
|
322
|
+
section = ensureSectionExists(lineCount);
|
|
323
|
+
let attribute;
|
|
324
|
+
switch (node.directive) {
|
|
325
|
+
case "set":
|
|
326
|
+
attribute = node.value;
|
|
327
|
+
break;
|
|
328
|
+
case "unset":
|
|
329
|
+
attribute = "not " + node.value;
|
|
330
|
+
break;
|
|
331
|
+
case "inc":
|
|
332
|
+
attribute = node.value + "+=" + (node.amount ?? "1");
|
|
333
|
+
break;
|
|
334
|
+
case "dec":
|
|
335
|
+
attribute = node.value + "-=" + (node.amount ?? "1");
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
if (passage) {
|
|
339
|
+
passage.attributes.push(attribute);
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
if (!section)
|
|
343
|
+
return sectionRequired(node);
|
|
344
|
+
section.attributes.push(attribute);
|
|
345
|
+
}
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
case "js":
|
|
349
|
+
if (inUiBlock) {
|
|
350
|
+
story.uiJs.push(node.code);
|
|
351
|
+
}
|
|
352
|
+
else if (!passage) {
|
|
353
|
+
section = ensureSectionExists(lineCount);
|
|
354
|
+
if (!section)
|
|
355
|
+
return sectionRequired(node);
|
|
356
|
+
section.js.push(node.code);
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
passage.js.push(node.code);
|
|
360
|
+
}
|
|
361
|
+
return true;
|
|
362
|
+
case "text":
|
|
363
|
+
if (inUiBlock) {
|
|
364
|
+
report("error", "squiffy/text-in-ui-block", filename, lineRange(node), "Unexpected text in @ui block.", lineCount);
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
else if (!passage) {
|
|
368
|
+
section = ensureSectionExists(lineCount);
|
|
369
|
+
// In an imported file, text before the first section is ignored
|
|
370
|
+
section?.addText(node.text, source(node));
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
passage.addText(node.text, source(node));
|
|
374
|
+
}
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
for (const node of [file.preamble, ...file.sections]) {
|
|
379
|
+
if (node.type == "snippet") {
|
|
380
|
+
section = null;
|
|
381
|
+
passage = null;
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
if (node.header && !handleSectionHeader(node) && !settings.recover)
|
|
385
|
+
return false;
|
|
386
|
+
for (const statement of node.body) {
|
|
387
|
+
if (!await handleStatement(statement) && !settings.recover)
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
for (const passageNode of node.passages) {
|
|
391
|
+
if (!handlePassageHeader(passageNode)) {
|
|
392
|
+
if (!settings.recover)
|
|
393
|
+
return false;
|
|
394
|
+
// Skip the passage, since there's nowhere to put it
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
for (const statement of passageNode.body) {
|
|
398
|
+
if (!await handleStatement(statement) && !settings.recover)
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
function warnExternalFilesUnavailable(line, node, filename) {
|
|
406
|
+
report("warning", "squiffy/external-files-unavailable", filename, lineRange(node), `"${line}" was ignored, as other files can't be loaded here`, node.range.start.line + 1);
|
|
407
|
+
}
|
|
408
|
+
function warnAboutV5Syntax(text, lines, filename) {
|
|
409
|
+
const truncate = (s) => s.length > 60 ? s.substring(0, 57) + "..." : s;
|
|
410
|
+
for (const match of findV5Syntax(text)) {
|
|
411
|
+
let message = `${truncate(match.text)} looks like Squiffy 5 text processor syntax, which isn't supported in Squiffy 6.`;
|
|
412
|
+
if (match.suggestion) {
|
|
413
|
+
message += match.suggestion.length > 60
|
|
414
|
+
? " Convert it to Handlebars syntax."
|
|
415
|
+
: ` Use ${match.suggestion} instead.`;
|
|
416
|
+
}
|
|
417
|
+
else if (match.text.startsWith("{else:")) {
|
|
418
|
+
message += " Use {{else}} inside an {{#if}} block instead.";
|
|
419
|
+
}
|
|
420
|
+
const line = match.line - 1;
|
|
421
|
+
const lineText = lines[line] ?? "";
|
|
422
|
+
const found = lineText.indexOf(match.text.split("\n")[0]);
|
|
423
|
+
const start = Math.max(found, 0);
|
|
424
|
+
const end = found >= 0 ? Math.min(found + match.text.length, lineText.length) : lineText.length;
|
|
425
|
+
report("warning", "squiffy/v5-syntax", filename, { start: { line, character: start }, end: { line, character: end } }, message, match.line);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
// Set once the sections are all known, before checking links
|
|
429
|
+
let sectionNames = [];
|
|
430
|
+
let sectionIndexes = new Map();
|
|
431
|
+
function checkText(container, section, passage) {
|
|
432
|
+
const getNextSectionName = () => {
|
|
433
|
+
return sectionNames[(sectionIndexes.get(section.name) ?? -1) + 1] || null;
|
|
434
|
+
};
|
|
435
|
+
const result = transformText(container.text.join("\n"), getNextSectionName);
|
|
436
|
+
container.output = result.output;
|
|
437
|
+
const toPosition = positionMapper(container.textSources, container.text.map(t => t.length));
|
|
438
|
+
const toRange = (start, end) => ({ start: toPosition(start), end: toPosition(end) });
|
|
439
|
+
const where = passage
|
|
440
|
+
? `In section '${section.name}', passage '${passage.name}'`
|
|
441
|
+
: `In section '${section.name}'`;
|
|
442
|
+
const legacyLine = passage ? passage.line : section.line;
|
|
443
|
+
const warn = (code, start, end, message, line = legacyLine) => {
|
|
444
|
+
report("warning", code, section.filename, toRange(start, end), message, line);
|
|
445
|
+
};
|
|
446
|
+
for (const missing of result.noNextSection) {
|
|
447
|
+
warn("squiffy/no-next-section", missing.start, missing.end, `In section '${section.name}', there is a ${missing.display} link but no following section exists`, section.line);
|
|
448
|
+
}
|
|
449
|
+
for (const link of result.links) {
|
|
450
|
+
if (link.form == "next")
|
|
451
|
+
continue;
|
|
452
|
+
if (link.linkType == "section") {
|
|
453
|
+
if (!linkDestinationExists(link.target, story.sections)) {
|
|
454
|
+
warn("squiffy/unknown-section-link", link.start, link.end, `${where} there is a link to a section called [[${link.target}]], which doesn't exist`);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
else if (!linkDestinationExists(link.target, section.passages)) {
|
|
458
|
+
warn("squiffy/unknown-passage-link", link.start, link.end, `${where} there is a link to a passage called [${link.target}], which doesn't exist`);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
// Like the runtime's embed helper, a name can refer to a passage in the
|
|
462
|
+
// current section or to any section.
|
|
463
|
+
for (const embed of result.embeds) {
|
|
464
|
+
if (!(embed.name in section.passages) && !(embed.name in story.sections)) {
|
|
465
|
+
warn("squiffy/unknown-embed", embed.start, embed.end, `${where} there is an embed of {{embed "${embed.name}"}}, but no section or passage with that name exists`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
// With recover, processFile carries on after errors, so check for them too
|
|
470
|
+
const success = await processFile(settings.script, settings.scriptBaseFilename, true) && !legacyErrors.length;
|
|
471
|
+
if (success || settings.recover) {
|
|
472
|
+
if (!Object.keys(story.sections).length) {
|
|
473
|
+
story.addSection("_default", settings.scriptBaseFilename, 0, undefined);
|
|
474
|
+
}
|
|
475
|
+
if (!story.start) {
|
|
476
|
+
story.start = Object.keys(story.sections)[0];
|
|
477
|
+
}
|
|
478
|
+
sectionNames = Object.keys(story.sections);
|
|
479
|
+
sectionIndexes = new Map(sectionNames.map((name, i) => [name, i]));
|
|
480
|
+
for (const section of Object.values(story.sections)) {
|
|
481
|
+
checkText(section, section, null);
|
|
482
|
+
for (const passage of Object.values(section.passages)) {
|
|
483
|
+
checkText(passage, section, passage);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
const analysis = {
|
|
488
|
+
success,
|
|
489
|
+
files,
|
|
490
|
+
diagnostics,
|
|
491
|
+
sections: Object.values(story.sections).map(section => ({
|
|
492
|
+
name: section.name,
|
|
493
|
+
file: section.filename ?? "",
|
|
494
|
+
...(section.node ? { node: section.node } : {}),
|
|
495
|
+
passages: Object.values(section.passages).map(passage => ({ name: passage.name, node: passage.node })),
|
|
496
|
+
})),
|
|
497
|
+
};
|
|
498
|
+
analysisInternals.set(analysis, { story, legacyErrors, legacyWarnings });
|
|
499
|
+
return analysis;
|
|
500
|
+
}
|
|
501
|
+
function linkDestinationExists(link, keys) {
|
|
502
|
+
// Link destination data may look like:
|
|
503
|
+
// passageName
|
|
504
|
+
// passageName, my_attribute=2
|
|
505
|
+
// passage name with a comma, in the name
|
|
506
|
+
// We're only interested in checking if the named passage or section exists.
|
|
507
|
+
// First check if the full link is a valid destination (handles names containing commas).
|
|
508
|
+
const trimmedLink = link.trim();
|
|
509
|
+
if (hasOwn(keys, trimmedLink)) {
|
|
510
|
+
return true;
|
|
511
|
+
}
|
|
512
|
+
// Otherwise, assume a comma separates the destination from setters.
|
|
513
|
+
const linkDestination = trimmedLink.split(",")[0].trim();
|
|
514
|
+
return hasOwn(keys, linkDestination);
|
|
515
|
+
}
|
|
516
|
+
// The same as Object.keys(keys).includes(key) for the plain objects used here, without building
|
|
517
|
+
// an array for every link.
|
|
518
|
+
function hasOwn(keys, key) {
|
|
519
|
+
return Object.prototype.hasOwnProperty.call(keys, key);
|
|
520
|
+
}
|
package/dist/compiler.d.ts
CHANGED
|
@@ -1,54 +1,26 @@
|
|
|
1
|
+
import { ExternalFiles, ParseCache } from "./analyse.js";
|
|
2
|
+
import { CompileSuccess } from "./emit.js";
|
|
3
|
+
import type { Diagnostic } from "./syntax.js";
|
|
1
4
|
export { findV5Syntax, convertV5Syntax } from "./v5.js";
|
|
2
5
|
export type { V5SyntaxMatch, V5ConversionProblem, V5ConversionResult } from "./v5.js";
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
uiJsIndex?: number;
|
|
11
|
-
sections: Record<string, OutputSection>;
|
|
12
|
-
}
|
|
13
|
-
interface OutputSection {
|
|
14
|
-
text?: string;
|
|
15
|
-
clear?: boolean;
|
|
16
|
-
attributes?: string[];
|
|
17
|
-
jsIndex?: number;
|
|
18
|
-
passages?: Record<string, OutputPassage>;
|
|
19
|
-
passageCount?: number;
|
|
20
|
-
}
|
|
21
|
-
interface OutputPassage {
|
|
22
|
-
text?: string;
|
|
23
|
-
clear?: boolean;
|
|
24
|
-
attributes?: string[];
|
|
25
|
-
jsIndex?: number;
|
|
26
|
-
}
|
|
6
|
+
export { parse } from "./parse.js";
|
|
7
|
+
export { analyse, ParseCache } from "./analyse.js";
|
|
8
|
+
export type { AnalyseSettings, Analysis, ExternalFiles, SectionSymbol, PassageSymbol } from "./analyse.js";
|
|
9
|
+
export { emit } from "./emit.js";
|
|
10
|
+
export type { EmitSettings, Output, UiInfo, CompileSuccess } from "./emit.js";
|
|
11
|
+
export { formatDiagnostic } from "./syntax.js";
|
|
12
|
+
export type * from "./syntax.js";
|
|
27
13
|
interface CompilerSettings {
|
|
28
14
|
scriptBaseFilename?: string;
|
|
29
15
|
script: string;
|
|
30
16
|
onWarning?: (message: string) => void;
|
|
31
17
|
externalFiles?: ExternalFiles;
|
|
32
18
|
globalJs?: boolean;
|
|
33
|
-
|
|
34
|
-
interface ExternalFiles {
|
|
35
|
-
getMatchingFilenames(pattern: string): Promise<string[]>;
|
|
36
|
-
getContent(filename: string): Promise<string>;
|
|
37
|
-
getLocalFilename(filename: string): string;
|
|
38
|
-
}
|
|
39
|
-
interface UiInfo {
|
|
40
|
-
title: string;
|
|
41
|
-
externalScripts: string[];
|
|
42
|
-
externalStylesheets: string[];
|
|
43
|
-
}
|
|
44
|
-
export interface CompileSuccess {
|
|
45
|
-
success: true;
|
|
46
|
-
output: Output;
|
|
47
|
-
getJs: (excludeHeader?: boolean) => Promise<string>;
|
|
48
|
-
getUiInfo: () => UiInfo;
|
|
19
|
+
parseCache?: ParseCache;
|
|
49
20
|
}
|
|
50
21
|
export interface CompileError {
|
|
51
22
|
success: false;
|
|
52
23
|
errors: string[];
|
|
24
|
+
diagnostics: Diagnostic[];
|
|
53
25
|
}
|
|
54
26
|
export declare function compile(settings: CompilerSettings): Promise<CompileSuccess | CompileError>;
|