squiffy-compiler 7.0.0-beta.3 → 7.0.0-beta.5
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 +2 -0
- package/dist/analyse.js +43 -1
- package/dist/compiler.d.ts +7 -0
- package/dist/compiler.js +3 -0
- package/dist/emit.d.ts +1 -0
- package/dist/emit.js +6 -0
- package/dist/language-service.js +39 -3
- package/dist/node.d.ts +2 -0
- package/dist/node.js +6 -2
- package/dist/parse.js +13 -4
- package/dist/project.d.ts +2 -0
- package/dist/project.js +14 -2
- package/dist/setters.d.ts +40 -0
- package/dist/setters.js +411 -0
- package/dist/story-graph.d.ts +2 -1
- package/dist/story-graph.js +10 -3
- package/dist/syntax.d.ts +9 -1
- package/dist/text.d.ts +4 -1
- package/dist/text.js +56 -32
- package/dist/twine/convert.d.ts +75 -0
- package/dist/twine/convert.js +626 -0
- package/dist/twine/expression.d.ts +62 -0
- package/dist/twine/expression.js +201 -0
- package/dist/twine/harlowe.d.ts +17 -0
- package/dist/twine/harlowe.js +1856 -0
- package/dist/twine/story.d.ts +24 -0
- package/dist/twine/story.js +231 -0
- package/dist/twine/sugarcube.d.ts +33 -0
- package/dist/twine/sugarcube.js +1584 -0
- package/package.json +2 -2
- package/syntaxes/squiffy.tmLanguage.json +33 -7
package/dist/setters.js
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
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 runtime/src/infixExpressions.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")
|
|
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" || 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
|
+
// runtime/src/infixExpressions.ts does): on commas outside quotes and brackets. Keeps offsets.
|
|
255
|
+
function splitWithOffsets(setters) {
|
|
256
|
+
const result = [];
|
|
257
|
+
let depth = 0;
|
|
258
|
+
let quote = null;
|
|
259
|
+
let start = 0;
|
|
260
|
+
for (let i = 0; i <= setters.length; i++) {
|
|
261
|
+
const c = setters[i];
|
|
262
|
+
if (i < setters.length && quote) {
|
|
263
|
+
if (c === "\\")
|
|
264
|
+
i++;
|
|
265
|
+
else if (c === quote)
|
|
266
|
+
quote = null;
|
|
267
|
+
}
|
|
268
|
+
else if (c === "\"" || c === "'")
|
|
269
|
+
quote = c;
|
|
270
|
+
else if (c === "(")
|
|
271
|
+
depth++;
|
|
272
|
+
else if (c === ")")
|
|
273
|
+
depth = Math.max(0, depth - 1);
|
|
274
|
+
else if (i === setters.length || (c === "," && depth === 0)) {
|
|
275
|
+
result.push({ text: setters.slice(start, i), offset: start });
|
|
276
|
+
start = i + 1;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return result;
|
|
280
|
+
}
|
|
281
|
+
// Every setter in a file: @set, link setters and set="..." on {{section}} and {{passage}}
|
|
282
|
+
function setterSites(file) {
|
|
283
|
+
const sites = [];
|
|
284
|
+
const addList = (text, line, character, inHelperString) => {
|
|
285
|
+
for (const part of splitWithOffsets(text)) {
|
|
286
|
+
sites.push({ text: part.text, line, character: character + part.offset, inHelperString });
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
290
|
+
if (section.type !== "section")
|
|
291
|
+
continue;
|
|
292
|
+
for (const container of [section, ...section.passages]) {
|
|
293
|
+
for (const node of container.body) {
|
|
294
|
+
if (node.type === "attribute" && node.directive === "set") {
|
|
295
|
+
sites.push({ text: node.value, line: node.valueRange.start.line, character: node.valueRange.start.character, inHelperString: false });
|
|
296
|
+
}
|
|
297
|
+
else if (node.type === "text") {
|
|
298
|
+
for (const match of node.text.matchAll(helperSetPattern)) {
|
|
299
|
+
const start = match.index + match[0].length - match[1].length + 1;
|
|
300
|
+
addList(match[1].slice(1, -1), node.range.start.line, node.range.start.character + start, match[1][0] === "\"");
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
for (const link of container.links) {
|
|
305
|
+
const comma = link.target.indexOf(",");
|
|
306
|
+
const { targetRange } = link;
|
|
307
|
+
if (link.form === "unnamed" || comma === -1 || targetRange.start.line !== targetRange.end.line)
|
|
308
|
+
continue;
|
|
309
|
+
addList(link.target.substring(comma + 1), targetRange.start.line, targetRange.start.character + comma + 1, false);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return sites;
|
|
314
|
+
}
|
|
315
|
+
// The names of the attributes a file sets, or might: those on the left of a setter, those of
|
|
316
|
+
// @inc, @dec and @unset, and anything in quotes, which covers {{set "x" ...}}, data-attribute="x",
|
|
317
|
+
// id="x" on an input, squiffy.set("x", ...) in JavaScript, and so on.
|
|
318
|
+
export function attributesSetIn(file, text) {
|
|
319
|
+
const names = new Set();
|
|
320
|
+
for (const site of setterSites(file)) {
|
|
321
|
+
const name = /^\s*(?:not\s+)?(\w+)/.exec(site.text);
|
|
322
|
+
if (name)
|
|
323
|
+
names.add(name[1]);
|
|
324
|
+
}
|
|
325
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
326
|
+
if (section.type !== "section")
|
|
327
|
+
continue;
|
|
328
|
+
for (const container of [section, ...section.passages]) {
|
|
329
|
+
for (const node of container.body) {
|
|
330
|
+
if (node.type === "attribute" && node.directive !== "set")
|
|
331
|
+
names.add(node.value.trim());
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
for (const match of text.matchAll(/"([^"\\\n]*)"|'([^'\\\n]*)'/g)) {
|
|
336
|
+
names.add((match[1] ?? match[2]).trim());
|
|
337
|
+
}
|
|
338
|
+
return names;
|
|
339
|
+
}
|
|
340
|
+
// Finds setters whose value looks like text without quotes, written with the Squiffy 6 rules, in a
|
|
341
|
+
// story that uses expressions: name = sword, where sword isn't an attribute the story sets
|
|
342
|
+
// (isSet), or name = Hello there.
|
|
343
|
+
export function findUnquotedText(file, isSet) {
|
|
344
|
+
const result = [];
|
|
345
|
+
for (const site of setterSites(file)) {
|
|
346
|
+
// Only plain "=": text with +=, -=, *= or /= is an error whatever it's written as
|
|
347
|
+
const match = /^(\s*)(\w+)(\s*=(?!=)\s*)(.*?)\s*$/.exec(site.text);
|
|
348
|
+
if (!match)
|
|
349
|
+
continue;
|
|
350
|
+
const [, leading, attribute, eq, value] = match;
|
|
351
|
+
let kind = null;
|
|
352
|
+
if (/^[A-Za-z_]\w*$/.test(value)) {
|
|
353
|
+
if (!reservedWords.has(value) && !value.startsWith("_") && !isSet(value))
|
|
354
|
+
kind = "word";
|
|
355
|
+
}
|
|
356
|
+
else if (/\s/.test(value) && /^[^"'()+*%<>=!&|{}[\]@\\/]+$/.test(value) && !/(^|\s)-|\b(and|or|not)\b/.test(value)) {
|
|
357
|
+
kind = "words";
|
|
358
|
+
}
|
|
359
|
+
if (!kind)
|
|
360
|
+
continue;
|
|
361
|
+
const character = site.character + leading.length + attribute.length + eq.length;
|
|
362
|
+
result.push({
|
|
363
|
+
text: value,
|
|
364
|
+
range: { start: { line: site.line, character }, end: { line: site.line, character: character + value.length } },
|
|
365
|
+
attribute,
|
|
366
|
+
kind,
|
|
367
|
+
quoted: quote(value, site.inHelperString),
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
return result;
|
|
371
|
+
}
|
|
372
|
+
// Attributes that Squiffy sets itself, which a story shouldn't set as well, with what they are
|
|
373
|
+
export const BUILT_IN_ATTRIBUTES = new Map([
|
|
374
|
+
// #377: stories written before Squiffy counted turns often kept their own count called "turns"
|
|
375
|
+
["turns", "the number of links clicked in the story so far"],
|
|
376
|
+
]);
|
|
377
|
+
// Finds where a file sets one of the BUILT_IN_ATTRIBUTES: on the left of a setter, in @inc, @dec
|
|
378
|
+
// and @unset, and as the quoted first argument of set, inc, dec or unset, which covers
|
|
379
|
+
// {{set "turns" 1}} and squiffy.set("turns", 1) in JavaScript.
|
|
380
|
+
export function findBuiltInAttributeSets(file, text) {
|
|
381
|
+
const result = [];
|
|
382
|
+
const add = (name, line, character) => {
|
|
383
|
+
if (BUILT_IN_ATTRIBUTES.has(name)) {
|
|
384
|
+
result.push({ name, range: { start: { line, character }, end: { line, character: character + name.length } } });
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
for (const site of setterSites(file)) {
|
|
388
|
+
const match = /^(\s*(?:not\s+)?)(\w+)/.exec(site.text);
|
|
389
|
+
if (match)
|
|
390
|
+
add(match[2], site.line, site.character + match[1].length);
|
|
391
|
+
}
|
|
392
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
393
|
+
if (section.type !== "section")
|
|
394
|
+
continue;
|
|
395
|
+
for (const container of [section, ...section.passages]) {
|
|
396
|
+
for (const node of container.body) {
|
|
397
|
+
if (node.type !== "attribute" || node.directive === "set")
|
|
398
|
+
continue;
|
|
399
|
+
const match = /^(\s*)(\w+)/.exec(node.value);
|
|
400
|
+
if (match)
|
|
401
|
+
add(match[2], node.valueRange.start.line, node.valueRange.start.character + match[1].length);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
text.replace(/\r/g, "").split("\n").forEach((line, i) => {
|
|
406
|
+
for (const match of line.matchAll(/\b(?:set|inc|dec|unset)\s*\(?\s*(["'])(\w+)\1/g)) {
|
|
407
|
+
add(match[2], i, match.index + match[0].length - match[2].length - 1);
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
return result.sort((a, b) => a.range.start.line - b.range.start.line || a.range.start.character - b.range.start.character);
|
|
411
|
+
}
|
package/dist/story-graph.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Location } from "./language-service.js";
|
|
2
|
-
import type { Diagnostic, EmbedNode, LinkNode, PassageNode, Position, SectionNode, SquiffyFile } from "./syntax.js";
|
|
2
|
+
import type { Diagnostic, EmbedNode, GotoNode, LinkNode, PassageNode, Position, SectionNode, SquiffyFile } from "./syntax.js";
|
|
3
3
|
export interface StoryGraph {
|
|
4
4
|
nodes: GraphNode[];
|
|
5
5
|
edges: GraphEdge[];
|
|
@@ -39,6 +39,7 @@ export interface GraphUsage {
|
|
|
39
39
|
sectionNode: SectionNode;
|
|
40
40
|
link?: LinkNode;
|
|
41
41
|
embed?: EmbedNode;
|
|
42
|
+
goto?: GotoNode;
|
|
42
43
|
}
|
|
43
44
|
export type GraphTarget = {
|
|
44
45
|
kind: "section";
|
package/dist/story-graph.js
CHANGED
|
@@ -105,6 +105,9 @@ export function buildStoryGraph(source) {
|
|
|
105
105
|
else if (statement.type == "text") {
|
|
106
106
|
if (/\{\{~?\s*section\s/.test(statement.text))
|
|
107
107
|
unknown.add(from);
|
|
108
|
+
// {{goto}} with an expression, e.g. {{goto (random (array "a" "b"))}}
|
|
109
|
+
if (/\{\{~?\s*goto\s+[^\s"'}]/.test(statement.text))
|
|
110
|
+
unknown.add(from);
|
|
108
111
|
// {{live "x" section="name"}} and passage="name" show that section's or
|
|
109
112
|
// passage's text, and so its links, as {{embed}} does
|
|
110
113
|
for (const match of statement.text.matchAll(/\{\{~?\s*live\s[^}]*?\b(section|passage)\s*=\s*(["'])([^"']*)\2[^}]*\}\}/g)) {
|
|
@@ -176,13 +179,17 @@ export function buildStoryGraph(source) {
|
|
|
176
179
|
continue;
|
|
177
180
|
const target = source.resolve(usage);
|
|
178
181
|
const to = target && (target.kind == "section" ? sectionId(target.name) : passageId(target.section, target.name));
|
|
179
|
-
const range = usage.link
|
|
182
|
+
const range = (usage.link ?? usage.embed ?? usage.goto).range;
|
|
180
183
|
const conditional = isConditional(usage.container, range.start);
|
|
181
184
|
const location = { file: usage.file, range };
|
|
182
185
|
if (usage.embed) {
|
|
183
186
|
edges.push({ from, ...(to ? { to } : { target: usage.embed.name }), kind: "embed", conditional, location });
|
|
184
187
|
continue;
|
|
185
188
|
}
|
|
189
|
+
if (usage.goto) {
|
|
190
|
+
edges.push({ from, ...(to ? { to } : { target: usage.goto.name }), kind: "goto", conditional, location });
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
186
193
|
const link = usage.link;
|
|
187
194
|
const dynamic = !to && link.target.includes("{{");
|
|
188
195
|
if (dynamic)
|
|
@@ -342,7 +349,7 @@ export function storyGraphDiagnostics(graph) {
|
|
|
342
349
|
range: node.location.range,
|
|
343
350
|
severity: "warning",
|
|
344
351
|
code: "squiffy/unreachable-section",
|
|
345
|
-
message:
|
|
352
|
+
message: `The section "${node.name}" can't be reached from the start of the story, as nothing the player can reach links to it.`,
|
|
346
353
|
});
|
|
347
354
|
}
|
|
348
355
|
else if (node.deadEnd) {
|
|
@@ -351,7 +358,7 @@ export function storyGraphDiagnostics(graph) {
|
|
|
351
358
|
range: node.location.range,
|
|
352
359
|
severity: "warning",
|
|
353
360
|
code: "squiffy/dead-end",
|
|
354
|
-
message:
|
|
361
|
+
message: `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.`,
|
|
355
362
|
});
|
|
356
363
|
}
|
|
357
364
|
}
|
package/dist/syntax.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export interface Range {
|
|
|
7
7
|
end: Position;
|
|
8
8
|
}
|
|
9
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/external-files-unavailable" | "squiffy/no-next-section" | "squiffy/unknown-embed" | "squiffy/unknown-section-link" | "squiffy/unknown-passage-link" | "squiffy/duplicate-section" | "squiffy/duplicate-passage" | "squiffy/not-in-main-file" | "squiffy/redundant-import" | "squiffy/project-file" | "squiffy/unreachable-section" | "squiffy/dead-end";
|
|
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-section-link" | "squiffy/unknown-passage-link" | "squiffy/duplicate-section" | "squiffy/duplicate-passage" | "squiffy/not-in-main-file" | "squiffy/redundant-import" | "squiffy/project-file" | "squiffy/unreachable-section" | "squiffy/dead-end";
|
|
11
11
|
export interface Diagnostic {
|
|
12
12
|
file: string;
|
|
13
13
|
range: Range;
|
|
@@ -34,6 +34,7 @@ export interface SectionNode {
|
|
|
34
34
|
passages: PassageNode[];
|
|
35
35
|
links: LinkNode[];
|
|
36
36
|
embeds: EmbedNode[];
|
|
37
|
+
gotos: GotoNode[];
|
|
37
38
|
}
|
|
38
39
|
export interface PassageNode {
|
|
39
40
|
type: "passage";
|
|
@@ -44,6 +45,7 @@ export interface PassageNode {
|
|
|
44
45
|
body: StatementNode[];
|
|
45
46
|
links: LinkNode[];
|
|
46
47
|
embeds: EmbedNode[];
|
|
48
|
+
gotos: GotoNode[];
|
|
47
49
|
}
|
|
48
50
|
export interface SnippetNode {
|
|
49
51
|
type: "snippet";
|
|
@@ -148,3 +150,9 @@ export interface EmbedNode {
|
|
|
148
150
|
range: Range;
|
|
149
151
|
nameRange: Range;
|
|
150
152
|
}
|
|
153
|
+
export interface GotoNode {
|
|
154
|
+
type: "goto";
|
|
155
|
+
name: string;
|
|
156
|
+
range: Range;
|
|
157
|
+
nameRange: Range;
|
|
158
|
+
}
|
package/dist/text.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export interface TextEmbed {
|
|
|
15
15
|
nameStart: number;
|
|
16
16
|
nameEnd: number;
|
|
17
17
|
}
|
|
18
|
+
export type TextGoto = TextEmbed;
|
|
18
19
|
export interface NoNextSection {
|
|
19
20
|
display: string;
|
|
20
21
|
start: number;
|
|
@@ -24,9 +25,11 @@ export interface TransformResult {
|
|
|
24
25
|
output: string;
|
|
25
26
|
links: TextLink[];
|
|
26
27
|
embeds: TextEmbed[];
|
|
28
|
+
gotos: TextGoto[];
|
|
27
29
|
noNextSection: NoNextSection[];
|
|
28
30
|
}
|
|
29
|
-
export declare
|
|
31
|
+
export declare const balancedTarget: string;
|
|
32
|
+
export declare function transformText(text: string, getNextSectionName: () => string | null, expressions?: boolean): TransformResult;
|
|
30
33
|
export interface SourceLine {
|
|
31
34
|
line: number;
|
|
32
35
|
length: number;
|