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
package/dist/setters.js
ADDED
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
// Converts setters in @set, links and the set= option of {{section}} and {{passage}} from the
|
|
2
|
+
// Squiffy 6 rules to the rules of a story with a squiffy.json (#373), where the right-hand side is
|
|
3
|
+
// an expression, as in {{#if}}:
|
|
4
|
+
//
|
|
5
|
+
// Squiffy 6 With a squiffy.json
|
|
6
|
+
// @set weapon = sword @set weapon = "sword"
|
|
7
|
+
// @set best = @score @set best = score
|
|
8
|
+
// @set gold += @price @set gold += price
|
|
9
|
+
// [[Go]](next, name = Bob) [[Go]](next, name = "Bob")
|
|
10
|
+
//
|
|
11
|
+
// Numbers, true and false, flags (has_key, not has_key), := and @inc/@dec mean the same under
|
|
12
|
+
// both rules, so they're left alone. The Squiffy 6 rules are simple enough that the conversion is
|
|
13
|
+
// exact: a converted story runs the same as before, once it has a squiffy.json. Anything that
|
|
14
|
+
// can't be converted exactly is left as it is and listed as a problem.
|
|
15
|
+
//
|
|
16
|
+
// Conversion isn't idempotent: a converted @set best = score would be converted again to
|
|
17
|
+
// @set best = "score". So it's only for stories without a squiffy.json.
|
|
18
|
+
import { parse } from "./parse.js";
|
|
19
|
+
// The same as the tokenizer's numbers and literals in infix-expressions.ts
|
|
20
|
+
const numberPattern = /^-?\d+(\.\d+)?$/;
|
|
21
|
+
const reservedWords = new Set(["and", "or", "not", "true", "false", "null", "undefined"]);
|
|
22
|
+
// Text as a quoted string. preferSingle is for setters inside a set="..." string, where double
|
|
23
|
+
// quotes would need escaping.
|
|
24
|
+
function quote(text, preferSingle) {
|
|
25
|
+
// The tokenizer treats a backslash as escaping the next character, but Handlebars doesn't
|
|
26
|
+
// remove it (except before a quote), so text with a backslash can't be written exactly
|
|
27
|
+
if (text.includes("\\"))
|
|
28
|
+
return null;
|
|
29
|
+
const hasDouble = text.includes("\"");
|
|
30
|
+
const hasSingle = text.includes("'");
|
|
31
|
+
if (hasDouble && hasSingle)
|
|
32
|
+
return null;
|
|
33
|
+
if (hasDouble || (preferSingle && !hasSingle))
|
|
34
|
+
return `'${text}'`;
|
|
35
|
+
return `"${text}"`;
|
|
36
|
+
}
|
|
37
|
+
// @name as an attribute in an expression: the name, or [name] if it isn't a plain word
|
|
38
|
+
function attribute(name) {
|
|
39
|
+
if (/^\w+$/.test(name) && !reservedWords.has(name) && !/^\d/.test(name))
|
|
40
|
+
return name;
|
|
41
|
+
if (name === "" || name.includes("]"))
|
|
42
|
+
return null;
|
|
43
|
+
return `[${name}]`;
|
|
44
|
+
}
|
|
45
|
+
// The value side of name = value under the Squiffy 6 rules, as an expression
|
|
46
|
+
function convertValue(rhs, preferSingle) {
|
|
47
|
+
if (rhs === "true" || rhs === "false")
|
|
48
|
+
return { text: rhs };
|
|
49
|
+
if (rhs === "")
|
|
50
|
+
return { problem: "it has no value" };
|
|
51
|
+
// Squiffy 6 treated anything that isn't NaN as a number, using parseFloat
|
|
52
|
+
if (!isNaN(rhs)) {
|
|
53
|
+
if (numberPattern.test(rhs))
|
|
54
|
+
return { text: rhs };
|
|
55
|
+
const n = String(parseFloat(rhs));
|
|
56
|
+
return numberPattern.test(n) ? { text: n } : { problem: `"${rhs}" isn't a plain number` };
|
|
57
|
+
}
|
|
58
|
+
if (rhs.startsWith("@")) {
|
|
59
|
+
const name = attribute(rhs.substring(1));
|
|
60
|
+
return name === null ? { problem: `"${rhs}" isn't an attribute name that can be converted` } : { text: name };
|
|
61
|
+
}
|
|
62
|
+
const quoted = quote(rhs, preferSingle);
|
|
63
|
+
return quoted === null ? { problem: "the text has both kinds of quote, or a backslash, in it" } : { text: quoted };
|
|
64
|
+
}
|
|
65
|
+
// Converts one setter, e.g. "weapon = sword" to "weapon = \"sword\"". Whitespace around it is
|
|
66
|
+
// kept. The result is the same text if nothing needs changing.
|
|
67
|
+
export function convertSetter(setter, options = {}) {
|
|
68
|
+
const preferSingle = options.preferSingleQuotes ?? false;
|
|
69
|
+
const leading = setter.match(/^\s*/)[0];
|
|
70
|
+
const trailing = setter.substring(leading.length).match(/\s*$/)[0];
|
|
71
|
+
const expr = setter.substring(leading.length, setter.length - trailing.length);
|
|
72
|
+
const wrap = (c) => "text" in c ? { text: leading + c.text + trailing } : c;
|
|
73
|
+
// name := text processes {{...}} in the text, under both rules
|
|
74
|
+
if (/^\w*\s*:=/.test(expr))
|
|
75
|
+
return { text: setter };
|
|
76
|
+
const set = /^(\w*)(\s*=\s*)(.*)$/.exec(expr);
|
|
77
|
+
if (set) {
|
|
78
|
+
const [, lhs, eq, rhs] = set;
|
|
79
|
+
if (!lhs)
|
|
80
|
+
return { problem: "it has no attribute name before \"=\"" };
|
|
81
|
+
const value = convertValue(rhs, preferSingle);
|
|
82
|
+
return "text" in value ? wrap({ text: lhs + eq + value.text }) : value;
|
|
83
|
+
}
|
|
84
|
+
const operate = /^(\w*)(\s*[+\-*/]=\s*)(.*)$/.exec(expr);
|
|
85
|
+
if (operate) {
|
|
86
|
+
const [, lhs, op, rhs] = operate;
|
|
87
|
+
if (!lhs)
|
|
88
|
+
return { problem: "it has no attribute name" };
|
|
89
|
+
const value = convertValue(rhs.trim(), preferSingle);
|
|
90
|
+
return "text" in value ? wrap({ text: lhs + op + value.text }) : value;
|
|
91
|
+
}
|
|
92
|
+
// A flag: name or not name
|
|
93
|
+
if (/^(not )?\w+$/.test(expr))
|
|
94
|
+
return { text: setter };
|
|
95
|
+
return { problem: "it isn't an attribute name, or name = value" };
|
|
96
|
+
}
|
|
97
|
+
// Converts a comma-separated list of setters. Squiffy 6 split them on every comma.
|
|
98
|
+
function convertSetterList(setters, preferSingle) {
|
|
99
|
+
let converted = 0;
|
|
100
|
+
const problems = [];
|
|
101
|
+
const parts = setters.split(",").map(part => {
|
|
102
|
+
if (part.trim() === "")
|
|
103
|
+
return part;
|
|
104
|
+
const result = convertSetter(part, { preferSingleQuotes: preferSingle });
|
|
105
|
+
if ("problem" in result) {
|
|
106
|
+
problems.push(`${part.trim()}: ${result.problem}`);
|
|
107
|
+
return part;
|
|
108
|
+
}
|
|
109
|
+
if (result.text !== part)
|
|
110
|
+
converted++;
|
|
111
|
+
return result.text;
|
|
112
|
+
});
|
|
113
|
+
return { text: parts.join(","), converted, problems };
|
|
114
|
+
}
|
|
115
|
+
// {{section ...}} or {{passage ...}} with a set="..." or set='...' string
|
|
116
|
+
const helperSetPattern = /\{\{~?\s*(?:section|passage)\b[^}]*?\bset=("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*')/g;
|
|
117
|
+
// Converts every setter in a .squiffy file. See the top of this file.
|
|
118
|
+
export function convertSetters(script) {
|
|
119
|
+
const crlf = script.includes("\r\n");
|
|
120
|
+
const lines = script.replace(/\r/g, "").split("\n");
|
|
121
|
+
const file = parse(script);
|
|
122
|
+
const edits = [];
|
|
123
|
+
const problems = [];
|
|
124
|
+
let converted = 0;
|
|
125
|
+
const problem = (line, text, reason) => {
|
|
126
|
+
problems.push({ line: line + 1, text, reason });
|
|
127
|
+
};
|
|
128
|
+
const replace = (range, text) => {
|
|
129
|
+
edits.push({ line: range.start.line, start: range.start.character, end: range.end.character, text });
|
|
130
|
+
};
|
|
131
|
+
const containers = [];
|
|
132
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
133
|
+
if (section.type !== "section")
|
|
134
|
+
continue;
|
|
135
|
+
containers.push(section, ...section.passages);
|
|
136
|
+
}
|
|
137
|
+
for (const container of containers) {
|
|
138
|
+
for (const node of container.body) {
|
|
139
|
+
if (node.type === "attribute" && node.directive === "set") {
|
|
140
|
+
const result = convertSetter(node.value);
|
|
141
|
+
if ("problem" in result) {
|
|
142
|
+
problem(node.range.start.line, node.value, result.problem);
|
|
143
|
+
}
|
|
144
|
+
else if (result.text !== node.value) {
|
|
145
|
+
replace(node.valueRange, result.text);
|
|
146
|
+
converted++;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else if (node.type === "text") {
|
|
150
|
+
for (const match of node.text.matchAll(helperSetPattern)) {
|
|
151
|
+
const literal = match[1];
|
|
152
|
+
const delimiter = literal[0];
|
|
153
|
+
// The setters as Handlebars reads the string
|
|
154
|
+
const inner = literal.slice(1, -1).replace(new RegExp(`\\\\${delimiter}`, "g"), delimiter);
|
|
155
|
+
const result = convertSetterList(inner, delimiter === "\"");
|
|
156
|
+
for (const reason of result.problems)
|
|
157
|
+
problem(node.range.start.line, literal, reason);
|
|
158
|
+
if (result.converted) {
|
|
159
|
+
const escaped = result.text.replace(new RegExp(delimiter, "g"), `\\${delimiter}`);
|
|
160
|
+
const start = node.range.start.character + match.index + match[0].length - literal.length;
|
|
161
|
+
replace({ start: { line: node.range.start.line, character: start }, end: { line: node.range.start.line, character: start + literal.length } }, delimiter + escaped + delimiter);
|
|
162
|
+
converted += result.converted;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
for (const link of container.links) {
|
|
168
|
+
converted += convertLink(link, lines, replace, problem);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Apply the edits from the end of each line, so earlier positions stay right
|
|
172
|
+
edits.sort((a, b) => b.line - a.line || b.start - a.start);
|
|
173
|
+
for (const edit of edits) {
|
|
174
|
+
const line = lines[edit.line];
|
|
175
|
+
lines[edit.line] = line.substring(0, edit.start) + edit.text + line.substring(edit.end);
|
|
176
|
+
}
|
|
177
|
+
const output = lines.join("\n");
|
|
178
|
+
return { script: crlf ? output.replace(/\n/g, "\r\n") : output, converted, problems };
|
|
179
|
+
}
|
|
180
|
+
// A link's setters: everything after the first comma of its target. For a next link, the target
|
|
181
|
+
// is just the setters part, e.g. ", gold=1".
|
|
182
|
+
function convertLink(link, lines, replace, problem) {
|
|
183
|
+
if (link.form === "unnamed" || link.form === "input")
|
|
184
|
+
return 0;
|
|
185
|
+
const comma = link.target.indexOf(",");
|
|
186
|
+
if (comma === -1)
|
|
187
|
+
return 0;
|
|
188
|
+
const { targetRange } = link;
|
|
189
|
+
if (targetRange.start.line !== targetRange.end.line
|
|
190
|
+
|| lines[targetRange.start.line].substring(targetRange.start.character, targetRange.end.character) !== link.target) {
|
|
191
|
+
problem(link.range.start.line, link.target, "the link's setters couldn't be found");
|
|
192
|
+
return 0;
|
|
193
|
+
}
|
|
194
|
+
const result = convertSetterList(link.target.substring(comma + 1), false);
|
|
195
|
+
for (const reason of result.problems)
|
|
196
|
+
problem(link.range.start.line, link.target, reason);
|
|
197
|
+
if (result.converted) {
|
|
198
|
+
replace(targetRange, link.target.substring(0, comma + 1) + result.text);
|
|
199
|
+
}
|
|
200
|
+
return result.converted;
|
|
201
|
+
}
|
|
202
|
+
// Finds "@name" on the right-hand side of setters, outside quotes. Under the Squiffy 6 rules that's
|
|
203
|
+
// how a setter used another attribute, but with expressions it's just name: Handlebars reads
|
|
204
|
+
// @name as one of its own data variables, which is never set.
|
|
205
|
+
export function findAtAttributes(file) {
|
|
206
|
+
const result = [];
|
|
207
|
+
// Scans text that starts at (line, character) in the file
|
|
208
|
+
const scan = (text, line, character) => {
|
|
209
|
+
let quote = null;
|
|
210
|
+
for (let i = 0; i < text.length; i++) {
|
|
211
|
+
const c = text[i];
|
|
212
|
+
if (quote) {
|
|
213
|
+
if (c === "\\")
|
|
214
|
+
i++;
|
|
215
|
+
else if (c === quote)
|
|
216
|
+
quote = null;
|
|
217
|
+
}
|
|
218
|
+
else if (c === "\"" || c === "'")
|
|
219
|
+
quote = c;
|
|
220
|
+
else if (c === "@" && !/\w/.test(text[i - 1] ?? "") && /\w/.test(text[i + 1] ?? "")) {
|
|
221
|
+
const name = /^@\w+/.exec(text.substring(i))[0];
|
|
222
|
+
result.push({ text: name, range: { start: { line, character: character + i }, end: { line, character: character + i + name.length } } });
|
|
223
|
+
i += name.length - 1;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
228
|
+
if (section.type !== "section")
|
|
229
|
+
continue;
|
|
230
|
+
for (const container of [section, ...section.passages]) {
|
|
231
|
+
for (const node of container.body) {
|
|
232
|
+
if (node.type === "attribute" && node.directive === "set") {
|
|
233
|
+
scan(node.value, node.valueRange.start.line, node.valueRange.start.character);
|
|
234
|
+
}
|
|
235
|
+
else if (node.type === "text") {
|
|
236
|
+
for (const match of node.text.matchAll(helperSetPattern)) {
|
|
237
|
+
const start = match.index + match[0].length - match[1].length + 1;
|
|
238
|
+
scan(match[1].slice(1, -1), node.range.start.line, node.range.start.character + start);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
for (const link of container.links) {
|
|
243
|
+
const comma = link.target.indexOf(",");
|
|
244
|
+
const { targetRange } = link;
|
|
245
|
+
if (link.form === "unnamed" || link.form === "input" || comma === -1 || targetRange.start.line !== targetRange.end.line)
|
|
246
|
+
continue;
|
|
247
|
+
scan(link.target.substring(comma + 1), targetRange.start.line, targetRange.start.character + comma + 1);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return result;
|
|
252
|
+
}
|
|
253
|
+
// Splits a list of setters with the rules of a story that uses expressions (as splitSetters in
|
|
254
|
+
// infix-expressions.ts does): on commas outside quotes, brackets and objects. Keeps
|
|
255
|
+
// offsets.
|
|
256
|
+
export function splitWithOffsets(setters) {
|
|
257
|
+
const result = [];
|
|
258
|
+
let depth = 0;
|
|
259
|
+
let quote = null;
|
|
260
|
+
let start = 0;
|
|
261
|
+
for (let i = 0; i <= setters.length; i++) {
|
|
262
|
+
const c = setters[i];
|
|
263
|
+
if (i < setters.length && quote) {
|
|
264
|
+
if (c === "\\")
|
|
265
|
+
i++;
|
|
266
|
+
else if (c === quote)
|
|
267
|
+
quote = null;
|
|
268
|
+
}
|
|
269
|
+
else if (c === "\"" || c === "'")
|
|
270
|
+
quote = c;
|
|
271
|
+
else if (c === "(" || c === "[" || c === "{")
|
|
272
|
+
depth++;
|
|
273
|
+
else if (c === ")" || c === "]" || c === "}")
|
|
274
|
+
depth = Math.max(0, depth - 1);
|
|
275
|
+
else if (i === setters.length || (c === "," && depth === 0)) {
|
|
276
|
+
result.push({ text: setters.slice(start, i), offset: start });
|
|
277
|
+
start = i + 1;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
// Every setter in a file: @set, link setters and set="..." on {{section}} and {{passage}}
|
|
283
|
+
function setterSites(file) {
|
|
284
|
+
const sites = [];
|
|
285
|
+
const addList = (text, line, character, inHelperString) => {
|
|
286
|
+
for (const part of splitWithOffsets(text)) {
|
|
287
|
+
sites.push({ text: part.text, line, character: character + part.offset, inHelperString });
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
291
|
+
if (section.type !== "section")
|
|
292
|
+
continue;
|
|
293
|
+
for (const container of [section, ...section.passages]) {
|
|
294
|
+
for (const node of container.body) {
|
|
295
|
+
if (node.type === "attribute" && node.directive === "set") {
|
|
296
|
+
sites.push({ text: node.value, line: node.valueRange.start.line, character: node.valueRange.start.character, inHelperString: false });
|
|
297
|
+
}
|
|
298
|
+
else if (node.type === "text") {
|
|
299
|
+
for (const match of node.text.matchAll(helperSetPattern)) {
|
|
300
|
+
const start = match.index + match[0].length - match[1].length + 1;
|
|
301
|
+
addList(match[1].slice(1, -1), node.range.start.line, node.range.start.character + start, match[1][0] === "\"");
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
for (const link of container.links) {
|
|
306
|
+
const comma = link.target.indexOf(",");
|
|
307
|
+
const { targetRange } = link;
|
|
308
|
+
if (link.form === "unnamed" || link.form === "input" || comma === -1 || targetRange.start.line !== targetRange.end.line)
|
|
309
|
+
continue;
|
|
310
|
+
addList(link.target.substring(comma + 1), targetRange.start.line, targetRange.start.character + comma + 1, false);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return sites;
|
|
315
|
+
}
|
|
316
|
+
// The names of the attributes a file sets, or might: those on the left of a setter, those of
|
|
317
|
+
// @inc, @dec and @unset, and anything in quotes, which covers {{set "x" ...}}, data-attribute="x",
|
|
318
|
+
// id="x" on an input, squiffy.set("x", ...) in JavaScript, and so on.
|
|
319
|
+
export function attributesSetIn(file, text) {
|
|
320
|
+
const names = new Set();
|
|
321
|
+
for (const site of setterSites(file)) {
|
|
322
|
+
const name = /^\s*(?:not\s+)?(\w+)/.exec(site.text);
|
|
323
|
+
if (name)
|
|
324
|
+
names.add(name[1]);
|
|
325
|
+
}
|
|
326
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
327
|
+
if (section.type !== "section")
|
|
328
|
+
continue;
|
|
329
|
+
for (const container of [section, ...section.passages]) {
|
|
330
|
+
for (const node of container.body) {
|
|
331
|
+
if (node.type === "attribute" && node.directive !== "set")
|
|
332
|
+
names.add(node.value.trim());
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
for (const match of text.matchAll(/"([^"\\\n]*)"|'([^'\\\n]*)'/g)) {
|
|
337
|
+
names.add((match[1] ?? match[2]).trim());
|
|
338
|
+
}
|
|
339
|
+
return names;
|
|
340
|
+
}
|
|
341
|
+
// Finds setters whose value looks like text without quotes, written with the Squiffy 6 rules, in a
|
|
342
|
+
// story that uses expressions: name = sword, where sword isn't an attribute the story sets
|
|
343
|
+
// (isSet), or name = Hello there.
|
|
344
|
+
export function findUnquotedText(file, isSet) {
|
|
345
|
+
const result = [];
|
|
346
|
+
for (const site of setterSites(file)) {
|
|
347
|
+
// Only plain "=": text with +=, -=, *= or /= is an error whatever it's written as
|
|
348
|
+
// The attribute can be a value inside an object, as in hero.name = Ann (#442)
|
|
349
|
+
const match = /^(\s*)(\w+(?:\.\w+)*)(\s*=(?!=)\s*)(.*?)\s*$/.exec(site.text);
|
|
350
|
+
if (!match)
|
|
351
|
+
continue;
|
|
352
|
+
const [, leading, attribute, eq, value] = match;
|
|
353
|
+
let kind = null;
|
|
354
|
+
if (/^[A-Za-z_]\w*$/.test(value)) {
|
|
355
|
+
if (!reservedWords.has(value) && !value.startsWith("_") && !isSet(value))
|
|
356
|
+
kind = "word";
|
|
357
|
+
}
|
|
358
|
+
else if (/\s/.test(value) && /^[^"'()+*%<>=!&|{}[\]@\\/]+$/.test(value) && !/(^|\s)-|\b(and|or|not)\b/.test(value)) {
|
|
359
|
+
kind = "words";
|
|
360
|
+
}
|
|
361
|
+
if (!kind)
|
|
362
|
+
continue;
|
|
363
|
+
const character = site.character + leading.length + attribute.length + eq.length;
|
|
364
|
+
result.push({
|
|
365
|
+
text: value,
|
|
366
|
+
range: { start: { line: site.line, character }, end: { line: site.line, character: character + value.length } },
|
|
367
|
+
attribute,
|
|
368
|
+
kind,
|
|
369
|
+
quoted: quote(value, site.inHelperString),
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
return result;
|
|
373
|
+
}
|
|
374
|
+
// Attributes that Squiffy sets itself, which a story shouldn't set as well, with what they are
|
|
375
|
+
export const BUILT_IN_ATTRIBUTES = new Map([
|
|
376
|
+
// #377: stories written before Squiffy counted turns often kept their own count called "turns"
|
|
377
|
+
["turns", "the number of links clicked in the story so far"],
|
|
378
|
+
// #56: and a story may have kept track of where the player had been
|
|
379
|
+
["previous", "the section or passage the player saw just before this one"],
|
|
380
|
+
["previous_section", "the section the player was in before this one"],
|
|
381
|
+
]);
|
|
382
|
+
// Finds where a file sets one of the BUILT_IN_ATTRIBUTES: on the left of a setter, in @inc, @dec
|
|
383
|
+
// and @unset, and as the quoted first argument of set, inc, dec or unset, which covers
|
|
384
|
+
// {{set "turns" 1}} and squiffy.set("turns", 1) in JavaScript.
|
|
385
|
+
export function findBuiltInAttributeSets(file, text) {
|
|
386
|
+
const result = [];
|
|
387
|
+
const add = (name, line, character) => {
|
|
388
|
+
if (BUILT_IN_ATTRIBUTES.has(name)) {
|
|
389
|
+
result.push({ name, range: { start: { line, character }, end: { line, character: character + name.length } } });
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
for (const site of setterSites(file)) {
|
|
393
|
+
const match = /^(\s*(?:not\s+)?)(\w+)/.exec(site.text);
|
|
394
|
+
if (match)
|
|
395
|
+
add(match[2], site.line, site.character + match[1].length);
|
|
396
|
+
}
|
|
397
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
398
|
+
if (section.type !== "section")
|
|
399
|
+
continue;
|
|
400
|
+
for (const container of [section, ...section.passages]) {
|
|
401
|
+
for (const node of container.body) {
|
|
402
|
+
if (node.type !== "attribute" || node.directive === "set")
|
|
403
|
+
continue;
|
|
404
|
+
const match = /^(\s*)(\w+)/.exec(node.value);
|
|
405
|
+
if (match)
|
|
406
|
+
add(match[2], node.valueRange.start.line, node.valueRange.start.character + match[1].length);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
text.replace(/\r/g, "").split("\n").forEach((line, i) => {
|
|
411
|
+
for (const match of line.matchAll(/\b(?:set|inc|dec|unset)\s*\(?\s*(["'])(\w+)\1/g)) {
|
|
412
|
+
add(match[2], i, match.index + match[0].length - match[2].length - 1);
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
return result.sort((a, b) => a.range.start.line - b.range.start.line || a.range.start.character - b.range.start.character);
|
|
416
|
+
}
|
|
417
|
+
// A value as written in a setter, {{set}} or squiffy.set(): text in quotes is that text, and
|
|
418
|
+
// values that clear an attribute or can't name anything (false, true, null, numbers) are
|
|
419
|
+
// nothing. A plain word is text by the Squiffy 6 rules, so it's taken as text too, but as it
|
|
420
|
+
// might be an attribute instead, it's also unknown. Anything else is unknown.
|
|
421
|
+
function literalValue(text) {
|
|
422
|
+
const value = text.trim();
|
|
423
|
+
const quoted = /^(["'])([^"'\\]*)\1$/.exec(value);
|
|
424
|
+
if (quoted)
|
|
425
|
+
return { value: quoted[2] };
|
|
426
|
+
if (/^(true|false|null|undefined)$/.test(value) || numberPattern.test(value))
|
|
427
|
+
return {};
|
|
428
|
+
if (/^[A-Za-z_]\w*$/.test(value))
|
|
429
|
+
return { value, unknown: true };
|
|
430
|
+
return { unknown: true };
|
|
431
|
+
}
|
|
432
|
+
// The values a story's files set each attribute to: with @set and link setters, {{set}} and
|
|
433
|
+
// {{unset}}, and squiffy.set() in its JavaScript. For {{goto name}}, where the attribute says
|
|
434
|
+
// which section to go to.
|
|
435
|
+
export function attributeValues(files) {
|
|
436
|
+
const result = new Map();
|
|
437
|
+
const add = (attribute, found) => {
|
|
438
|
+
let entry = result.get(attribute);
|
|
439
|
+
if (!entry)
|
|
440
|
+
result.set(attribute, entry = { values: new Set(), unknown: false });
|
|
441
|
+
if (found.value)
|
|
442
|
+
entry.values.add(found.value);
|
|
443
|
+
if (found.unknown)
|
|
444
|
+
entry.unknown = true;
|
|
445
|
+
};
|
|
446
|
+
for (const file of files) {
|
|
447
|
+
for (const site of setterSites(file)) {
|
|
448
|
+
const match = /^\s*(not\s+)?(\w+(?:\.\w+)*)\s*(?:(:?=(?!=)|[-+*/]=)\s*(.*?))?\s*$/.exec(site.text);
|
|
449
|
+
if (!match)
|
|
450
|
+
continue;
|
|
451
|
+
const [, not, attribute, operator, value] = match;
|
|
452
|
+
if (not || operator === undefined)
|
|
453
|
+
add(attribute, {});
|
|
454
|
+
else
|
|
455
|
+
add(attribute, operator == "=" || operator == ":=" ? literalValue(value) : { unknown: true });
|
|
456
|
+
}
|
|
457
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
458
|
+
if (section.type !== "section")
|
|
459
|
+
continue;
|
|
460
|
+
for (const container of [section, ...section.passages]) {
|
|
461
|
+
for (const node of container.body) {
|
|
462
|
+
if (node.type === "text") {
|
|
463
|
+
for (const match of node.text.matchAll(/\{\{~?\s*set\s+(["'])([\w.]+)\1\s+(.*?)\s*~?\}\}/g)) {
|
|
464
|
+
add(match[2], literalValue(match[3]));
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
else if (node.type === "js") {
|
|
468
|
+
for (const match of node.code.matchAll(/\bset\(\s*(["'])([\w.]+)\1\s*,\s*([^)]*?)\s*\)/g)) {
|
|
469
|
+
add(match[2], literalValue(match[3]));
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
return result;
|
|
477
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { Location } from "./language-service.js";
|
|
2
|
+
import type { DeactivateNode, Diagnostic, EmbedNode, GotoNode, LinkNode, PassageNode, Position, Range, SectionNode, SquiffyFile } from "./syntax.js";
|
|
3
|
+
export interface StoryGraph {
|
|
4
|
+
nodes: GraphNode[];
|
|
5
|
+
edges: GraphEdge[];
|
|
6
|
+
reachabilityKnown: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface GraphNode {
|
|
9
|
+
id: string;
|
|
10
|
+
kind: "section" | "passage";
|
|
11
|
+
name: string;
|
|
12
|
+
section?: string;
|
|
13
|
+
detail?: string;
|
|
14
|
+
label?: string;
|
|
15
|
+
location: Location;
|
|
16
|
+
range: Range;
|
|
17
|
+
contentEnd: Position;
|
|
18
|
+
start?: boolean;
|
|
19
|
+
reachable: boolean;
|
|
20
|
+
embedded?: boolean;
|
|
21
|
+
deadEnd: boolean;
|
|
22
|
+
ending?: boolean;
|
|
23
|
+
unknownExits?: boolean;
|
|
24
|
+
}
|
|
25
|
+
export type GraphEdgeKind = "section-link" | "passage-link" | "embed" | "goto" | "script";
|
|
26
|
+
export interface GraphEdge {
|
|
27
|
+
from: string;
|
|
28
|
+
to?: string;
|
|
29
|
+
kind: GraphEdgeKind;
|
|
30
|
+
conditional: boolean;
|
|
31
|
+
dynamic?: boolean;
|
|
32
|
+
via?: string;
|
|
33
|
+
location: Location;
|
|
34
|
+
text?: string;
|
|
35
|
+
target?: string;
|
|
36
|
+
setters?: string[];
|
|
37
|
+
}
|
|
38
|
+
export declare const sectionId: (name: string) => string;
|
|
39
|
+
export declare const passageId: (section: string, name: string) => string;
|
|
40
|
+
export interface GraphUsage {
|
|
41
|
+
file: string;
|
|
42
|
+
container: SectionNode | PassageNode;
|
|
43
|
+
sectionNode: SectionNode;
|
|
44
|
+
link?: LinkNode;
|
|
45
|
+
embed?: EmbedNode;
|
|
46
|
+
goto?: GotoNode;
|
|
47
|
+
deactivate?: DeactivateNode;
|
|
48
|
+
}
|
|
49
|
+
export type GraphTarget = {
|
|
50
|
+
kind: "section";
|
|
51
|
+
name: string;
|
|
52
|
+
} | {
|
|
53
|
+
kind: "passage";
|
|
54
|
+
section: string;
|
|
55
|
+
name: string;
|
|
56
|
+
};
|
|
57
|
+
export interface GraphSource {
|
|
58
|
+
files: Map<string, SquiffyFile>;
|
|
59
|
+
sections: Map<string, {
|
|
60
|
+
file: string;
|
|
61
|
+
node?: SectionNode;
|
|
62
|
+
passages: Map<string, PassageNode>;
|
|
63
|
+
}>;
|
|
64
|
+
nodeNames: Map<SectionNode, string>;
|
|
65
|
+
usages: GraphUsage[];
|
|
66
|
+
start: string;
|
|
67
|
+
resolve(usage: GraphUsage): GraphTarget | undefined;
|
|
68
|
+
}
|
|
69
|
+
export declare function buildStoryGraph(source: GraphSource): StoryGraph;
|
|
70
|
+
export declare function storyGraphDiagnostics(graph: StoryGraph): Diagnostic[];
|
|
71
|
+
export declare function firstScreenSoundDiagnostics(source: GraphSource): Diagnostic[];
|