squiffy-compiler 6.0.1 → 7.0.0-beta.11
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 +10 -2
- package/dist/analyse.d.ts +96 -0
- package/dist/analyse.js +951 -0
- package/dist/compiler.d.ts +32 -40
- package/dist/compiler.js +26 -691
- package/dist/css-urls.d.ts +4 -0
- package/dist/css-urls.js +59 -0
- package/dist/emit.d.ts +71 -0
- package/dist/emit.js +192 -0
- package/dist/helpers.d.ts +1 -0
- package/dist/helpers.js +14 -0
- package/dist/ignore.d.ts +8 -0
- package/dist/ignore.js +71 -0
- package/dist/infix-expressions.d.ts +19 -0
- package/dist/infix-expressions.js +713 -0
- package/dist/language-service.d.ts +90 -0
- package/dist/language-service.js +883 -0
- package/dist/node.d.ts +23 -0
- package/dist/node.js +122 -0
- package/dist/parse.d.ts +22 -0
- package/dist/parse.js +316 -0
- package/dist/project.d.ts +129 -0
- package/dist/project.js +488 -0
- package/dist/quest/aslx.d.ts +51 -0
- package/dist/quest/aslx.js +184 -0
- package/dist/quest/convert.d.ts +20 -0
- package/dist/quest/convert.js +552 -0
- package/dist/quest/script.d.ts +92 -0
- package/dist/quest/script.js +453 -0
- package/dist/quest/scripts.d.ts +24 -0
- package/dist/quest/scripts.js +506 -0
- package/dist/quest/text.d.ts +18 -0
- package/dist/quest/text.js +323 -0
- package/dist/section-labels.d.ts +4 -0
- package/dist/section-labels.js +64 -0
- package/dist/setters.d.ts +49 -0
- package/dist/setters.js +477 -0
- package/dist/story-graph.d.ts +71 -0
- package/dist/story-graph.js +445 -0
- package/dist/syntax.d.ts +198 -0
- package/dist/syntax.js +8 -0
- package/dist/templates.d.ts +17 -0
- package/dist/templates.js +180 -0
- package/dist/text.d.ts +44 -0
- package/dist/text.js +348 -0
- package/dist/twine/audio.d.ts +21 -0
- package/dist/twine/audio.js +80 -0
- package/dist/twine/convert.d.ts +131 -0
- package/dist/twine/convert.js +1057 -0
- package/dist/twine/expression.d.ts +88 -0
- package/dist/twine/expression.js +318 -0
- package/dist/twine/harlowe.d.ts +27 -0
- package/dist/twine/harlowe.js +2341 -0
- package/dist/twine/settings.d.ts +9 -0
- package/dist/twine/settings.js +322 -0
- package/dist/twine/story.d.ts +24 -0
- package/dist/twine/story.js +231 -0
- package/dist/twine/sugarcube.d.ts +40 -0
- package/dist/twine/sugarcube.js +2172 -0
- package/dist/v5.js +2 -1
- package/package.json +35 -6
- package/syntaxes/squiffy.tmLanguage.json +1003 -0
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
// The whole story as a directed graph: its sections and passages, and the links between them,
|
|
2
|
+
// with which can be reached from the start and which sections are dead ends. The language
|
|
3
|
+
// service builds it from its index (see getStoryGraph), for the story map and for the
|
|
4
|
+
// "unreachable section" and "dead end" diagnostics, and the "sound on the first screen" one.
|
|
5
|
+
import { capitalise, sectionDetail, sectionLabels } from "./section-labels.js";
|
|
6
|
+
import { attributeValues } from "./setters.js";
|
|
7
|
+
export const sectionId = (name) => `section:${name}`;
|
|
8
|
+
export const passageId = (section, name) => `passage:${section}/${name}`;
|
|
9
|
+
const NAVIGATION = new Set(["section-link", "goto", "script"]);
|
|
10
|
+
const collapsed = (position) => ({ start: position, end: position });
|
|
11
|
+
const comparePositions = (a, b) => a.line - b.line || a.character - b.character;
|
|
12
|
+
// The parts of a container's text inside {{#if}} or {{#unless}} blocks, or {{#every}} (#451),
|
|
13
|
+
// whose text is shown later, if the player is still there
|
|
14
|
+
function conditionalRanges(container) {
|
|
15
|
+
const ranges = [];
|
|
16
|
+
const open = [];
|
|
17
|
+
for (const statement of container.body) {
|
|
18
|
+
if (statement.type != "text")
|
|
19
|
+
continue;
|
|
20
|
+
for (const match of statement.text.matchAll(/\{\{~?\s*([#/])(if|unless|every)\b/g)) {
|
|
21
|
+
const position = { line: statement.range.start.line, character: match.index };
|
|
22
|
+
if (match[1] == "#") {
|
|
23
|
+
open.push(position);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
const start = open.pop();
|
|
27
|
+
if (start)
|
|
28
|
+
ranges.push({ start, end: position });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
// An unclosed block runs to the end
|
|
33
|
+
for (const start of open)
|
|
34
|
+
ranges.push({ start, end: container.range.end });
|
|
35
|
+
return ranges;
|
|
36
|
+
}
|
|
37
|
+
// The end of a section's or passage's own text, leaving out blank lines and its passages
|
|
38
|
+
function contentEnd(container) {
|
|
39
|
+
const ownEnd = container.type == "section" && container.passages.length
|
|
40
|
+
? container.passages[0].range.start
|
|
41
|
+
: container.range.end;
|
|
42
|
+
let end = container.header?.range.end ?? container.range.start;
|
|
43
|
+
for (const statement of container.body) {
|
|
44
|
+
if (comparePositions(statement.range.start, ownEnd) >= 0)
|
|
45
|
+
break;
|
|
46
|
+
if (statement.type == "text" && !statement.text.trim())
|
|
47
|
+
continue;
|
|
48
|
+
if (comparePositions(statement.range.end, end) > 0)
|
|
49
|
+
end = statement.range.end;
|
|
50
|
+
}
|
|
51
|
+
return end;
|
|
52
|
+
}
|
|
53
|
+
export function buildStoryGraph(source) {
|
|
54
|
+
const nodes = [];
|
|
55
|
+
const edges = [];
|
|
56
|
+
const nodeById = new Map();
|
|
57
|
+
const containerIds = new Map();
|
|
58
|
+
// Containers with navigation that can't be followed
|
|
59
|
+
const unknown = new Set();
|
|
60
|
+
const endings = new Set();
|
|
61
|
+
// {{live}} helpers that show a section or passage
|
|
62
|
+
// {{goto name}}, where the attribute name says where to go
|
|
63
|
+
const attributeGotos = [];
|
|
64
|
+
const liveEmbeds = [];
|
|
65
|
+
const addNode = (node) => {
|
|
66
|
+
nodes.push(node);
|
|
67
|
+
nodeById.set(node.id, node);
|
|
68
|
+
};
|
|
69
|
+
for (const [name, section] of source.sections) {
|
|
70
|
+
const id = sectionId(name);
|
|
71
|
+
const node = section.node;
|
|
72
|
+
const range = node
|
|
73
|
+
? node.nameRange ?? node.header?.range ?? collapsed(node.range.start)
|
|
74
|
+
: collapsed({ line: 0, character: 0 });
|
|
75
|
+
const detail = node ? sectionDetail(node) : undefined;
|
|
76
|
+
addNode({
|
|
77
|
+
id, kind: "section", name, ...(detail ? { detail } : {}),
|
|
78
|
+
location: { file: section.file, range }, range: node?.range ?? range,
|
|
79
|
+
contentEnd: node ? contentEnd(node) : range.end,
|
|
80
|
+
reachable: false, deadEnd: false,
|
|
81
|
+
...(name == source.start ? { start: true } : {}),
|
|
82
|
+
});
|
|
83
|
+
if (node)
|
|
84
|
+
containerIds.set(node, id);
|
|
85
|
+
for (const [passageName, passage] of section.passages) {
|
|
86
|
+
const pid = passageId(name, passageName);
|
|
87
|
+
addNode({
|
|
88
|
+
id: pid, kind: "passage", name: passageName, section: name,
|
|
89
|
+
location: { file: section.file, range: passage.nameRange }, range: passage.range,
|
|
90
|
+
contentEnd: contentEnd(passage),
|
|
91
|
+
reachable: false, deadEnd: false,
|
|
92
|
+
});
|
|
93
|
+
containerIds.set(passage, pid);
|
|
94
|
+
}
|
|
95
|
+
// @end, and navigation the links don't show
|
|
96
|
+
for (const container of node ? [node, ...section.passages.values()] : []) {
|
|
97
|
+
const from = containerIds.get(container);
|
|
98
|
+
for (const statement of container.body) {
|
|
99
|
+
if (statement.type == "end") {
|
|
100
|
+
endings.add(name);
|
|
101
|
+
}
|
|
102
|
+
else if (statement.type == "text") {
|
|
103
|
+
if (/\{\{~?\s*section\s/.test(statement.text))
|
|
104
|
+
unknown.add(from);
|
|
105
|
+
// {{goto name}}, which goes to the section an attribute names, or {{goto}} with
|
|
106
|
+
// an expression, e.g. {{goto (random (array "a" "b"))}}
|
|
107
|
+
for (const match of statement.text.matchAll(/\{\{~?\s*goto\s+([^\s"'}~]+)/g)) {
|
|
108
|
+
const attribute = /^[A-Za-z_][\w.]*$/.test(match[1]) ? match[1] : undefined;
|
|
109
|
+
if (!attribute) {
|
|
110
|
+
unknown.add(from);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const line = statement.range.start.line;
|
|
114
|
+
attributeGotos.push({
|
|
115
|
+
from, file: section.file, attribute,
|
|
116
|
+
range: { start: { line, character: match.index }, end: { line, character: match.index + match[0].length } },
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
// {{live "x" section="name"}} and passage="name" show that section's or
|
|
120
|
+
// passage's text, and so its links, as {{embed}} does
|
|
121
|
+
for (const match of statement.text.matchAll(/\{\{~?\s*live\s[^}]*?\b(section|passage)\s*=\s*(["'])([^"']*)\2[^}]*\}\}/g)) {
|
|
122
|
+
const line = statement.range.start.line;
|
|
123
|
+
liveEmbeds.push({
|
|
124
|
+
container, from, file: section.file, sectionName: name, kind: match[1], target: match[3],
|
|
125
|
+
range: { start: { line, character: match.index }, end: { line, character: match.index + match[0].length } },
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else if (statement.type == "js") {
|
|
130
|
+
for (const match of statement.code.matchAll(/\.go\s*\(\s*(?:(["'])([^"'\\]*)\1\s*\))?/g)) {
|
|
131
|
+
const target = match[2];
|
|
132
|
+
if (target === undefined) {
|
|
133
|
+
unknown.add(from);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const start = statement.codeRange.start.character + match.index;
|
|
137
|
+
edges.push({
|
|
138
|
+
from,
|
|
139
|
+
...(source.sections.has(target) ? { to: sectionId(target) } : {}),
|
|
140
|
+
kind: "script",
|
|
141
|
+
conditional: false,
|
|
142
|
+
location: {
|
|
143
|
+
file: section.file,
|
|
144
|
+
range: { start: { line: statement.range.start.line, character: start }, end: { line: statement.range.start.line, character: start + match[0].length } },
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
for (const [name, label] of sectionLabels(source.files.values(), node => source.nodeNames.get(node))) {
|
|
153
|
+
const node = nodeById.get(sectionId(name));
|
|
154
|
+
if (node)
|
|
155
|
+
node.label = label;
|
|
156
|
+
}
|
|
157
|
+
// The link a +++ line adds to the section before it
|
|
158
|
+
for (const [file, tree] of source.files) {
|
|
159
|
+
const all = [tree.preamble, ...tree.sections];
|
|
160
|
+
for (let i = 1; i < all.length; i++) {
|
|
161
|
+
const node = all[i];
|
|
162
|
+
if (node.type != "section" || node.header?.type != "continue")
|
|
163
|
+
continue;
|
|
164
|
+
const previous = all[i - 1];
|
|
165
|
+
const name = source.nodeNames.get(node);
|
|
166
|
+
const previousName = previous.type == "section" ? source.nodeNames.get(previous) : undefined;
|
|
167
|
+
if (name === undefined || previousName === undefined)
|
|
168
|
+
continue;
|
|
169
|
+
edges.push({
|
|
170
|
+
from: sectionId(previousName),
|
|
171
|
+
to: sectionId(name),
|
|
172
|
+
kind: "section-link",
|
|
173
|
+
conditional: false,
|
|
174
|
+
location: { file, range: node.header.range },
|
|
175
|
+
text: node.header.text || "Continue...",
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const conditionals = new Map();
|
|
180
|
+
const isConditional = (container, position) => {
|
|
181
|
+
let ranges = conditionals.get(container);
|
|
182
|
+
if (!ranges) {
|
|
183
|
+
ranges = conditionalRanges(container);
|
|
184
|
+
conditionals.set(container, ranges);
|
|
185
|
+
}
|
|
186
|
+
return ranges.some(r => comparePositions(r.start, position) <= 0 && comparePositions(position, r.end) <= 0);
|
|
187
|
+
};
|
|
188
|
+
for (const usage of source.usages) {
|
|
189
|
+
if (usage.deactivate)
|
|
190
|
+
continue;
|
|
191
|
+
const from = containerIds.get(usage.container);
|
|
192
|
+
// Not part of the story, or replaced by a later section or passage of the same name
|
|
193
|
+
if (from === undefined)
|
|
194
|
+
continue;
|
|
195
|
+
const target = source.resolve(usage);
|
|
196
|
+
const to = target && (target.kind == "section" ? sectionId(target.name) : passageId(target.section, target.name));
|
|
197
|
+
const range = (usage.link ?? usage.embed ?? usage.goto).range;
|
|
198
|
+
const conditional = isConditional(usage.container, range.start);
|
|
199
|
+
const location = { file: usage.file, range };
|
|
200
|
+
if (usage.embed) {
|
|
201
|
+
edges.push({ from, ...(to ? { to } : { target: usage.embed.name }), kind: "embed", conditional, location });
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (usage.goto) {
|
|
205
|
+
edges.push({ from, ...(to ? { to } : { target: usage.goto.name }), kind: "goto", conditional, location });
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const link = usage.link;
|
|
209
|
+
const dynamic = !to && link.target.includes("{{");
|
|
210
|
+
if (dynamic)
|
|
211
|
+
unknown.add(from);
|
|
212
|
+
const resolvedName = target?.name;
|
|
213
|
+
const setters = link.form == "input" || (link.form != "next" && link.target.trim() == resolvedName)
|
|
214
|
+
? []
|
|
215
|
+
: link.target.split(",").slice(1).map(s => s.trim()).filter(Boolean);
|
|
216
|
+
const text = link.text ?? link.target.trim().split(",")[0].trim();
|
|
217
|
+
edges.push({
|
|
218
|
+
from,
|
|
219
|
+
...(to ? { to } : {}),
|
|
220
|
+
kind: link.linkType == "section" ? "section-link" : "passage-link",
|
|
221
|
+
conditional,
|
|
222
|
+
...(dynamic ? { dynamic } : {}),
|
|
223
|
+
location,
|
|
224
|
+
...(text ? { text } : {}),
|
|
225
|
+
...(!to && !dynamic && link.form != "next" ? { target: link.target.split(",")[0].trim() } : {}),
|
|
226
|
+
...(setters.length ? { setters } : {}),
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
for (const live of liveEmbeds) {
|
|
230
|
+
const to = live.kind == "section"
|
|
231
|
+
? source.sections.has(live.target) ? sectionId(live.target) : undefined
|
|
232
|
+
: source.sections.get(live.sectionName)?.passages.has(live.target) ? passageId(live.sectionName, live.target) : undefined;
|
|
233
|
+
edges.push({
|
|
234
|
+
from: live.from,
|
|
235
|
+
...(to ? { to } : { target: live.target }),
|
|
236
|
+
kind: "embed",
|
|
237
|
+
conditional: isConditional(live.container, live.range.start),
|
|
238
|
+
location: { file: live.file, range: live.range },
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
// {{goto name}} goes to whichever section the story sets name to. Where every value it's set
|
|
242
|
+
// to is a section's name, those are all the places it can go; otherwise it's somewhere that
|
|
243
|
+
// can't be known, as well as any sections it's set to.
|
|
244
|
+
const values = attributeGotos.length ? attributeValues(source.files.values()) : undefined;
|
|
245
|
+
for (const goto of attributeGotos) {
|
|
246
|
+
const found = values.get(goto.attribute);
|
|
247
|
+
const targets = [...found?.values ?? []];
|
|
248
|
+
const sections = targets.filter(name => source.sections.has(name));
|
|
249
|
+
if (!found || found.unknown || sections.length < targets.length || !sections.length)
|
|
250
|
+
unknown.add(goto.from);
|
|
251
|
+
for (const name of sections) {
|
|
252
|
+
edges.push({
|
|
253
|
+
from: goto.from, to: sectionId(name), kind: "goto", conditional: true, via: goto.attribute,
|
|
254
|
+
location: { file: goto.file, range: goto.range },
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// In the order they're written
|
|
259
|
+
const fileOrder = new Map([...source.files.keys()].map((file, i) => [file, i]));
|
|
260
|
+
edges.sort((a, b) => (fileOrder.get(a.location.file) ?? -1) - (fileOrder.get(b.location.file) ?? -1)
|
|
261
|
+
|| comparePositions(a.location.range.start, b.location.range.start));
|
|
262
|
+
const outgoing = new Map();
|
|
263
|
+
for (const edge of edges) {
|
|
264
|
+
let list = outgoing.get(edge.from);
|
|
265
|
+
if (!list)
|
|
266
|
+
outgoing.set(edge.from, list = []);
|
|
267
|
+
list.push(edge);
|
|
268
|
+
}
|
|
269
|
+
// Passages by section, for special passages
|
|
270
|
+
const passagesOf = new Map();
|
|
271
|
+
for (const node of nodes) {
|
|
272
|
+
if (node.kind != "passage")
|
|
273
|
+
continue;
|
|
274
|
+
let list = passagesOf.get(node.section);
|
|
275
|
+
if (!list)
|
|
276
|
+
passagesOf.set(node.section, list = []);
|
|
277
|
+
list.push(node);
|
|
278
|
+
}
|
|
279
|
+
// ---- Reachability ----
|
|
280
|
+
// Containers whose content is shown: reachable ones, and anything they embed
|
|
281
|
+
const shown = new Set();
|
|
282
|
+
const toShow = [];
|
|
283
|
+
const show = (id) => {
|
|
284
|
+
if (shown.has(id))
|
|
285
|
+
return;
|
|
286
|
+
shown.add(id);
|
|
287
|
+
toShow.push(id);
|
|
288
|
+
};
|
|
289
|
+
const reach = (id) => {
|
|
290
|
+
const node = id === undefined ? undefined : nodeById.get(id);
|
|
291
|
+
if (!node || node.reachable)
|
|
292
|
+
return;
|
|
293
|
+
node.reachable = true;
|
|
294
|
+
show(node.id);
|
|
295
|
+
if (node.kind == "section") {
|
|
296
|
+
// Passages such as @last and @1 run on their own when their section's passages do
|
|
297
|
+
for (const passage of passagesOf.get(node.name) ?? []) {
|
|
298
|
+
if (passage.name.startsWith("@"))
|
|
299
|
+
reach(passage.id);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
else if (node.name != "") {
|
|
303
|
+
// A passage called "" runs whenever another passage in its section does
|
|
304
|
+
reach(passageId(node.section, ""));
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
if (nodeById.has(sectionId(source.start))) {
|
|
308
|
+
reach(sectionId(source.start));
|
|
309
|
+
// The master section's content is part of every section
|
|
310
|
+
reach(sectionId(""));
|
|
311
|
+
}
|
|
312
|
+
while (toShow.length) {
|
|
313
|
+
const id = toShow.pop();
|
|
314
|
+
for (const edge of outgoing.get(id) ?? []) {
|
|
315
|
+
if (edge.to === undefined)
|
|
316
|
+
continue;
|
|
317
|
+
if (edge.kind == "embed")
|
|
318
|
+
show(edge.to);
|
|
319
|
+
else
|
|
320
|
+
reach(edge.to);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
// Shown inside something that can be reached, though not reached itself
|
|
324
|
+
for (const node of nodes) {
|
|
325
|
+
if (!node.reachable && shown.has(node.id))
|
|
326
|
+
node.embedded = true;
|
|
327
|
+
}
|
|
328
|
+
// ---- Dead ends ----
|
|
329
|
+
const masterIds = nodeById.has(sectionId(""))
|
|
330
|
+
? [sectionId(""), ...(passagesOf.get("") ?? []).map(p => p.id)]
|
|
331
|
+
: [];
|
|
332
|
+
for (const node of nodes) {
|
|
333
|
+
if (node.kind != "section")
|
|
334
|
+
continue;
|
|
335
|
+
const ending = endings.has(node.name);
|
|
336
|
+
if (ending)
|
|
337
|
+
node.ending = true;
|
|
338
|
+
// What's shown in this section: its own content, its passages', the master section's,
|
|
339
|
+
// and anything those embed
|
|
340
|
+
const seen = new Set();
|
|
341
|
+
const queue = [node.id, ...(passagesOf.get(node.name) ?? []).map(p => p.id), ...masterIds];
|
|
342
|
+
let wayOut = false;
|
|
343
|
+
let unknownExits = false;
|
|
344
|
+
while (queue.length) {
|
|
345
|
+
const id = queue.pop();
|
|
346
|
+
if (seen.has(id))
|
|
347
|
+
continue;
|
|
348
|
+
seen.add(id);
|
|
349
|
+
if (unknown.has(id))
|
|
350
|
+
unknownExits = true;
|
|
351
|
+
for (const edge of outgoing.get(id) ?? []) {
|
|
352
|
+
if (NAVIGATION.has(edge.kind))
|
|
353
|
+
wayOut = true;
|
|
354
|
+
else if (edge.kind == "embed" && edge.to)
|
|
355
|
+
queue.push(edge.to);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (unknownExits)
|
|
359
|
+
node.unknownExits = true;
|
|
360
|
+
const hasContent = source.sections.get(node.name)?.node !== undefined;
|
|
361
|
+
node.deadEnd = hasContent && node.name != "" && !ending && !wayOut && !unknownExits;
|
|
362
|
+
}
|
|
363
|
+
const sections = nodes.filter(n => n.kind == "section");
|
|
364
|
+
const reachabilityKnown = sections.some(n => n.start) && !sections.some(n => n.reachable && n.unknownExits);
|
|
365
|
+
return { nodes, edges, reachabilityKnown };
|
|
366
|
+
}
|
|
367
|
+
// "Unreachable section" and "dead end" warnings, for the language service's diagnostics
|
|
368
|
+
export function storyGraphDiagnostics(graph) {
|
|
369
|
+
const diagnostics = [];
|
|
370
|
+
const sections = graph.nodes.filter(n => n.kind == "section");
|
|
371
|
+
for (const node of sections) {
|
|
372
|
+
// The master section, which is part of every section
|
|
373
|
+
if (node.name == "")
|
|
374
|
+
continue;
|
|
375
|
+
if (!node.reachable) {
|
|
376
|
+
if (!graph.reachabilityKnown || node.embedded)
|
|
377
|
+
continue;
|
|
378
|
+
diagnostics.push({
|
|
379
|
+
file: node.location.file,
|
|
380
|
+
range: node.location.range,
|
|
381
|
+
severity: "warning",
|
|
382
|
+
code: "squiffy/unreachable-section",
|
|
383
|
+
message: `${capitalise(node.label ?? `the section "${node.name}"`)} can't be reached from the start of the story, as nothing the player can reach links to it.`,
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
else if (node.deadEnd) {
|
|
387
|
+
diagnostics.push({
|
|
388
|
+
file: node.location.file,
|
|
389
|
+
range: node.location.range,
|
|
390
|
+
severity: "warning",
|
|
391
|
+
code: "squiffy/dead-end",
|
|
392
|
+
message: `${capitalise(node.label ?? `the section "${node.name}"`)} has no way out, as it has no links to another section. If it's one of the story's endings, add @end to say so.`,
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return diagnostics;
|
|
397
|
+
}
|
|
398
|
+
// {{music "file"}} or {{sound "file"}}, as emit.ts finds them
|
|
399
|
+
const SOUND_HELPER = /\{\{~?\s*(music|sound)\s+(["'])([^"'{}]+)\2/g;
|
|
400
|
+
// Sound on the story's first screen: browsers don't let a page play sound until the player has
|
|
401
|
+
// clicked something, so music there waits for their first click, and a sound played once there
|
|
402
|
+
// isn't heard at all. That's the start section's own @music, @sound, {{music}} and {{sound}},
|
|
403
|
+
// and the master section's @music and @sound, which play with every section, the first too.
|
|
404
|
+
// (Its passages, and the start section's, are shown by a click, so they're fine.)
|
|
405
|
+
export function firstScreenSoundDiagnostics(source) {
|
|
406
|
+
const diagnostics = [];
|
|
407
|
+
const report = (file, range, kind, value, master) => {
|
|
408
|
+
const where = master ? "The master section plays this with every section, including the story's first screen, but browsers" : "Browsers";
|
|
409
|
+
const what = kind == "music"
|
|
410
|
+
? `so "${value}" won't start until the player's first click`
|
|
411
|
+
: `so "${value}" won't be heard on the story's first screen`;
|
|
412
|
+
diagnostics.push({
|
|
413
|
+
file,
|
|
414
|
+
range,
|
|
415
|
+
severity: "warning",
|
|
416
|
+
code: "squiffy/sound-on-first-screen",
|
|
417
|
+
message: `${where} don't play sound until the player has clicked something, ${what}. Begin the story with a title screen and a link such as "Begin", and play it in the section that link goes to.`,
|
|
418
|
+
});
|
|
419
|
+
};
|
|
420
|
+
const check = (name, master) => {
|
|
421
|
+
const section = source.sections.get(name);
|
|
422
|
+
if (!section?.node)
|
|
423
|
+
return;
|
|
424
|
+
for (const statement of section.node.body) {
|
|
425
|
+
if (statement.type == "sound") {
|
|
426
|
+
if (statement.value != "stop")
|
|
427
|
+
report(section.file, statement.range, statement.directive, statement.value, master);
|
|
428
|
+
}
|
|
429
|
+
else if (statement.type == "text" && !master) {
|
|
430
|
+
for (const match of statement.text.matchAll(SOUND_HELPER)) {
|
|
431
|
+
const before = statement.text.substring(0, match.index);
|
|
432
|
+
const lines = before.split("\n");
|
|
433
|
+
const line = statement.range.start.line + lines.length - 1;
|
|
434
|
+
const character = (lines.length == 1 ? statement.range.start.character : 0) + lines[lines.length - 1].length;
|
|
435
|
+
const range = { start: { line, character }, end: { line, character: character + match[0].length } };
|
|
436
|
+
report(section.file, range, match[1], match[3], false);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
if (source.start != "")
|
|
442
|
+
check(source.start, false);
|
|
443
|
+
check("", true);
|
|
444
|
+
return diagnostics;
|
|
445
|
+
}
|
package/dist/syntax.d.ts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
export interface Position {
|
|
2
|
+
line: number;
|
|
3
|
+
character: number;
|
|
4
|
+
}
|
|
5
|
+
export interface Range {
|
|
6
|
+
start: Position;
|
|
7
|
+
end: Position;
|
|
8
|
+
}
|
|
9
|
+
export type DiagnosticSeverity = "error" | "warning" | "hint";
|
|
10
|
+
export type DiagnosticCode = "squiffy/passage-without-section" | "squiffy/text-in-ui-block" | "squiffy/content-before-section" | "squiffy/v5-syntax" | "squiffy/at-attribute" | "squiffy/unquoted-text" | "squiffy/built-in-attribute" | "squiffy/external-files-unavailable" | "squiffy/no-next-section" | "squiffy/unknown-embed" | "squiffy/unknown-goto" | "squiffy/unknown-deactivate" | "squiffy/unknown-section-link" | "squiffy/unknown-passage-link" | "squiffy/duplicate-section" | "squiffy/duplicate-passage" | "squiffy/not-in-main-file" | "squiffy/repeat-not-in-passage" | "squiffy/transition" | "squiffy/redundant-import" | "squiffy/project-file" | "squiffy/unreachable-section" | "squiffy/dead-end" | "squiffy/sound-on-first-screen" | "squiffy/template-syntax" | "squiffy/unknown-name";
|
|
11
|
+
export interface Diagnostic {
|
|
12
|
+
file: string;
|
|
13
|
+
range: Range;
|
|
14
|
+
severity: DiagnosticSeverity;
|
|
15
|
+
code: DiagnosticCode;
|
|
16
|
+
message: string;
|
|
17
|
+
subject?: string;
|
|
18
|
+
}
|
|
19
|
+
export declare function formatDiagnostic(diagnostic: Diagnostic): string;
|
|
20
|
+
export interface SquiffyFile {
|
|
21
|
+
type: "file";
|
|
22
|
+
filename: string | undefined;
|
|
23
|
+
range: Range;
|
|
24
|
+
preamble: SectionNode;
|
|
25
|
+
sections: (SectionNode | SnippetNode)[];
|
|
26
|
+
}
|
|
27
|
+
export interface SectionNode {
|
|
28
|
+
type: "section";
|
|
29
|
+
kind: "named" | "continue" | "separator" | "preamble";
|
|
30
|
+
name?: string;
|
|
31
|
+
nameRange?: Range;
|
|
32
|
+
header?: SectionHeaderNode | ContinueNode | SeparatorNode;
|
|
33
|
+
range: Range;
|
|
34
|
+
body: StatementNode[];
|
|
35
|
+
passages: PassageNode[];
|
|
36
|
+
links: LinkNode[];
|
|
37
|
+
embeds: EmbedNode[];
|
|
38
|
+
gotos: GotoNode[];
|
|
39
|
+
deactivates: DeactivateNode[];
|
|
40
|
+
}
|
|
41
|
+
export interface PassageNode {
|
|
42
|
+
type: "passage";
|
|
43
|
+
name: string;
|
|
44
|
+
nameRange: Range;
|
|
45
|
+
header: PassageHeaderNode;
|
|
46
|
+
range: Range;
|
|
47
|
+
body: StatementNode[];
|
|
48
|
+
links: LinkNode[];
|
|
49
|
+
embeds: EmbedNode[];
|
|
50
|
+
gotos: GotoNode[];
|
|
51
|
+
deactivates: DeactivateNode[];
|
|
52
|
+
}
|
|
53
|
+
export interface SnippetNode {
|
|
54
|
+
type: "snippet";
|
|
55
|
+
name: string;
|
|
56
|
+
nameRange: Range;
|
|
57
|
+
header: SnippetHeaderNode;
|
|
58
|
+
range: Range;
|
|
59
|
+
content: string;
|
|
60
|
+
}
|
|
61
|
+
export interface SectionHeaderNode {
|
|
62
|
+
type: "section-header";
|
|
63
|
+
name: string;
|
|
64
|
+
range: Range;
|
|
65
|
+
}
|
|
66
|
+
export interface PassageHeaderNode {
|
|
67
|
+
type: "passage-header";
|
|
68
|
+
name: string;
|
|
69
|
+
range: Range;
|
|
70
|
+
}
|
|
71
|
+
export interface SnippetHeaderNode {
|
|
72
|
+
type: "snippet-header";
|
|
73
|
+
name: string;
|
|
74
|
+
range: Range;
|
|
75
|
+
}
|
|
76
|
+
export interface ContinueNode {
|
|
77
|
+
type: "continue";
|
|
78
|
+
text: string;
|
|
79
|
+
range: Range;
|
|
80
|
+
}
|
|
81
|
+
export interface SeparatorNode {
|
|
82
|
+
type: "separator";
|
|
83
|
+
range: Range;
|
|
84
|
+
}
|
|
85
|
+
export interface TitleNode {
|
|
86
|
+
type: "title";
|
|
87
|
+
value: string;
|
|
88
|
+
valueRange: Range;
|
|
89
|
+
range: Range;
|
|
90
|
+
}
|
|
91
|
+
export interface StartNode {
|
|
92
|
+
type: "start";
|
|
93
|
+
value: string;
|
|
94
|
+
valueRange: Range;
|
|
95
|
+
range: Range;
|
|
96
|
+
}
|
|
97
|
+
export interface ImportNode {
|
|
98
|
+
type: "import";
|
|
99
|
+
pattern: string;
|
|
100
|
+
patternRange: Range;
|
|
101
|
+
range: Range;
|
|
102
|
+
}
|
|
103
|
+
export interface UiBlockStartNode {
|
|
104
|
+
type: "ui-block-start";
|
|
105
|
+
range: Range;
|
|
106
|
+
}
|
|
107
|
+
export interface UiFileNode {
|
|
108
|
+
type: "ui-file";
|
|
109
|
+
path: string;
|
|
110
|
+
pathRange: Range;
|
|
111
|
+
range: Range;
|
|
112
|
+
}
|
|
113
|
+
export interface ClearNode {
|
|
114
|
+
type: "clear";
|
|
115
|
+
range: Range;
|
|
116
|
+
}
|
|
117
|
+
export interface RepeatNode {
|
|
118
|
+
type: "repeat";
|
|
119
|
+
range: Range;
|
|
120
|
+
}
|
|
121
|
+
export interface EndNode {
|
|
122
|
+
type: "end";
|
|
123
|
+
range: Range;
|
|
124
|
+
}
|
|
125
|
+
export interface SoundNode {
|
|
126
|
+
type: "sound";
|
|
127
|
+
directive: "music" | "sound";
|
|
128
|
+
value: string;
|
|
129
|
+
valueRange: Range;
|
|
130
|
+
range: Range;
|
|
131
|
+
}
|
|
132
|
+
export interface ClassNode {
|
|
133
|
+
type: "class";
|
|
134
|
+
value: string;
|
|
135
|
+
valueRange: Range;
|
|
136
|
+
range: Range;
|
|
137
|
+
}
|
|
138
|
+
export interface TagNode {
|
|
139
|
+
type: "tag";
|
|
140
|
+
value: string;
|
|
141
|
+
valueRange: Range;
|
|
142
|
+
range: Range;
|
|
143
|
+
}
|
|
144
|
+
export interface TransitionNode {
|
|
145
|
+
type: "transition";
|
|
146
|
+
directive: "transition" | "leave";
|
|
147
|
+
value: string;
|
|
148
|
+
valueRange: Range;
|
|
149
|
+
time?: string;
|
|
150
|
+
range: Range;
|
|
151
|
+
}
|
|
152
|
+
export interface AttributeNode {
|
|
153
|
+
type: "attribute";
|
|
154
|
+
directive: "set" | "unset" | "inc" | "dec";
|
|
155
|
+
value: string;
|
|
156
|
+
valueRange: Range;
|
|
157
|
+
amount?: string;
|
|
158
|
+
range: Range;
|
|
159
|
+
}
|
|
160
|
+
export interface JsNode {
|
|
161
|
+
type: "js";
|
|
162
|
+
code: string;
|
|
163
|
+
codeRange: Range;
|
|
164
|
+
range: Range;
|
|
165
|
+
}
|
|
166
|
+
export interface TextNode {
|
|
167
|
+
type: "text";
|
|
168
|
+
text: string;
|
|
169
|
+
range: Range;
|
|
170
|
+
}
|
|
171
|
+
export type StatementNode = SeparatorNode | TitleNode | StartNode | ImportNode | UiBlockStartNode | UiFileNode | ClearNode | RepeatNode | EndNode | AttributeNode | SoundNode | ClassNode | TagNode | TransitionNode | JsNode | TextNode;
|
|
172
|
+
export interface LinkNode {
|
|
173
|
+
type: "link";
|
|
174
|
+
linkType: "section" | "passage";
|
|
175
|
+
form: "named" | "unnamed" | "next" | "input";
|
|
176
|
+
text?: string;
|
|
177
|
+
target: string;
|
|
178
|
+
range: Range;
|
|
179
|
+
targetRange: Range;
|
|
180
|
+
}
|
|
181
|
+
export interface EmbedNode {
|
|
182
|
+
type: "embed";
|
|
183
|
+
name: string;
|
|
184
|
+
range: Range;
|
|
185
|
+
nameRange: Range;
|
|
186
|
+
}
|
|
187
|
+
export interface DeactivateNode {
|
|
188
|
+
type: "deactivate";
|
|
189
|
+
name: string;
|
|
190
|
+
range: Range;
|
|
191
|
+
nameRange: Range;
|
|
192
|
+
}
|
|
193
|
+
export interface GotoNode {
|
|
194
|
+
type: "goto";
|
|
195
|
+
name: string;
|
|
196
|
+
range: Range;
|
|
197
|
+
nameRange: Range;
|
|
198
|
+
}
|
package/dist/syntax.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// The syntax tree that parse() returns for one .squiffy file, and the structured diagnostics
|
|
2
|
+
// that analyse() reports. Positions are zero-based and ranges are end-exclusive, following
|
|
3
|
+
// LSP conventions, so a language server can pass them straight through.
|
|
4
|
+
// Formats a diagnostic the way the compiler has always reported problems, e.g.
|
|
5
|
+
// `WARNING: story.squiffy line 12: In section 'start' there is a link to ...`
|
|
6
|
+
export function formatDiagnostic(diagnostic) {
|
|
7
|
+
return `${diagnostic.severity.toUpperCase()}: ${diagnostic.file} line ${diagnostic.range.start.line + 1}: ${diagnostic.message}`;
|
|
8
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface TemplateError {
|
|
2
|
+
start: number;
|
|
3
|
+
end: number;
|
|
4
|
+
message: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function findTemplateError(text: string): TemplateError | null;
|
|
7
|
+
export interface BareName {
|
|
8
|
+
start: number;
|
|
9
|
+
end: number;
|
|
10
|
+
name: string;
|
|
11
|
+
root: string;
|
|
12
|
+
}
|
|
13
|
+
export interface TemplateNames {
|
|
14
|
+
bare: BareName[];
|
|
15
|
+
embedParameters: Set<string>;
|
|
16
|
+
}
|
|
17
|
+
export declare function findTemplateNames(text: string): TemplateNames;
|