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,323 @@
|
|
|
1
|
+
// Converts the text of a Quest gamebook page, which is HTML with Quest's text processor commands
|
|
2
|
+
// in it ({if ...}, {once:...}, {page:...} and so on), into Squiffy text.
|
|
3
|
+
import { attributeRef, display, print, quote, Unconvertible } from "../twine/expression.js";
|
|
4
|
+
import { escapeLinkBrackets, sectionLink, urlLink } from "../twine/convert.js";
|
|
5
|
+
// Finds the next {...} the way Quest does: the first {, and the } that matches it, counting the
|
|
6
|
+
// ones in between. Undefined if there isn't one.
|
|
7
|
+
function nextCommand(text, from) {
|
|
8
|
+
const start = text.indexOf("{", from);
|
|
9
|
+
if (start == -1)
|
|
10
|
+
return undefined;
|
|
11
|
+
let depth = 1;
|
|
12
|
+
for (let i = start + 1; i < text.length; i++) {
|
|
13
|
+
if (text[i] == "{")
|
|
14
|
+
depth++;
|
|
15
|
+
else if (text[i] == "}" && --depth == 0)
|
|
16
|
+
return { start, end: i };
|
|
17
|
+
}
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
export function convertQuestText(text, ctx, mode) {
|
|
21
|
+
let result = "";
|
|
22
|
+
let pos = 0;
|
|
23
|
+
text = text.replace(/\r\n?/g, "\n");
|
|
24
|
+
for (;;) {
|
|
25
|
+
const command = nextCommand(text, pos);
|
|
26
|
+
if (!command)
|
|
27
|
+
break;
|
|
28
|
+
result += literal(text.substring(pos, command.start), mode);
|
|
29
|
+
const inner = text.substring(command.start + 1, command.end);
|
|
30
|
+
try {
|
|
31
|
+
result += convertCommand(inner, ctx, mode);
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
if (!(e instanceof Unconvertible))
|
|
35
|
+
throw e;
|
|
36
|
+
result += ctx.unconvertible(`{${inner}}`, `This couldn't be converted: ${e.message}.`);
|
|
37
|
+
}
|
|
38
|
+
pos = command.end + 1;
|
|
39
|
+
}
|
|
40
|
+
return result + literal(text.substring(pos), mode);
|
|
41
|
+
}
|
|
42
|
+
// The commands Quest's gamebook text processor has, in the order it tries them
|
|
43
|
+
const commands = [
|
|
44
|
+
"if ", "either ", "here ", "nothere ", "popup:", "i:", "b:", "s:", "u:", "colour:", "color:", "back:", "object:",
|
|
45
|
+
"command:", "page:", "exit:", "once:", "notfirst:", "random:", "rndalt:", "img:", "counter:", "select:", "eval:", "=",
|
|
46
|
+
];
|
|
47
|
+
function convertCommand(section, ctx, mode) {
|
|
48
|
+
const command = commands.find(c => section.startsWith(c));
|
|
49
|
+
const nested = (t) => convertQuestText(t, ctx, mode);
|
|
50
|
+
switch (command) {
|
|
51
|
+
case "if ":
|
|
52
|
+
return convertIf(section, ctx, mode);
|
|
53
|
+
case "i:":
|
|
54
|
+
case "b:":
|
|
55
|
+
case "s:":
|
|
56
|
+
case "u:": {
|
|
57
|
+
const tag = section[0];
|
|
58
|
+
return `<${tag}>${nested(section.substring(2))}</${tag}>`;
|
|
59
|
+
}
|
|
60
|
+
case "colour:":
|
|
61
|
+
case "color:":
|
|
62
|
+
case "back:": {
|
|
63
|
+
// Quest splits these at every colon, not just the first two
|
|
64
|
+
const parts = section.split(":");
|
|
65
|
+
const colour = parts[1] ?? "";
|
|
66
|
+
const rest = parts.slice(2).join(":");
|
|
67
|
+
const property = command == "back:" ? "background-color" : "color";
|
|
68
|
+
return `<span style="${property}: ${escapeAttribute(colour.trim())}">${nested(rest)}</span>`;
|
|
69
|
+
}
|
|
70
|
+
case "command:":
|
|
71
|
+
case "page:":
|
|
72
|
+
return convertPageLink(section, ctx, mode);
|
|
73
|
+
// Quest remembers each of these by its own text, as {{#once}} remembers each of its own
|
|
74
|
+
case "once:":
|
|
75
|
+
return `{{#once}}${nested(section.substring(5))}{{/once}}`;
|
|
76
|
+
case "notfirst:":
|
|
77
|
+
return `{{#once}}{{else}}${nested(section.substring(9))}{{/once}}`;
|
|
78
|
+
case "random:":
|
|
79
|
+
return convertRandom(section.substring(7), ctx);
|
|
80
|
+
case "img:": {
|
|
81
|
+
const file = section.substring(4).trim();
|
|
82
|
+
ctx.asset(file);
|
|
83
|
+
return `<img src="${escapeAttribute(file)}" alt="">`;
|
|
84
|
+
}
|
|
85
|
+
case "counter:":
|
|
86
|
+
// Quest shows 0 for a counter that hasn't been set, as adding 0 does
|
|
87
|
+
return `{{= ${attributeRef(ctx.attribute(section.substring(8)))} + 0}}`;
|
|
88
|
+
case "popup:": {
|
|
89
|
+
const parts = section.substring(6).split(":");
|
|
90
|
+
const link = parts[0];
|
|
91
|
+
const content = parts.slice(1).join(":");
|
|
92
|
+
if (!ctx.addPassage || mode == "inline") {
|
|
93
|
+
ctx.approximate(`{${section}}`, "Squiffy has no popups, and this one is in a link's text, so only its link text has been kept.");
|
|
94
|
+
return nested(link);
|
|
95
|
+
}
|
|
96
|
+
// A passage link shows the popup's text when it's clicked
|
|
97
|
+
const name = ctx.addPassage(link, convertQuestText(content, ctx, "block"));
|
|
98
|
+
return `[${escapeLinkBrackets(nested(link))}](${name})`;
|
|
99
|
+
}
|
|
100
|
+
case "either ":
|
|
101
|
+
case "eval:":
|
|
102
|
+
case "=":
|
|
103
|
+
throw new Unconvertible("Quest expressions in text can't be converted yet");
|
|
104
|
+
case "here ":
|
|
105
|
+
case "nothere ":
|
|
106
|
+
case "object:":
|
|
107
|
+
case "exit:":
|
|
108
|
+
throw new Unconvertible("it's for Quest's text adventures, not gamebooks");
|
|
109
|
+
case "rndalt:":
|
|
110
|
+
case "select:":
|
|
111
|
+
throw new Unconvertible(`Squiffy has nothing like {${command}}`);
|
|
112
|
+
}
|
|
113
|
+
return convertOther(section, ctx, mode);
|
|
114
|
+
}
|
|
115
|
+
// {game.score} shows an attribute. Anything else in curly brackets is shown as it is.
|
|
116
|
+
function convertOther(section, ctx, mode) {
|
|
117
|
+
const dot = section.indexOf(".");
|
|
118
|
+
if (dot != -1) {
|
|
119
|
+
const object = section.substring(0, dot);
|
|
120
|
+
const attribute = section.substring(dot + 1);
|
|
121
|
+
if (object == "game" && /^[\w ]+$/.test(attribute)) {
|
|
122
|
+
return display({ k: "var", name: ctx.attribute(attribute) });
|
|
123
|
+
}
|
|
124
|
+
if (/^[\w ]+$/.test(object) && /^\w+$/.test(attribute) && (object == "player" || ctx.target(object))) {
|
|
125
|
+
if (object == "player")
|
|
126
|
+
checkPlayerAttribute(attribute, `{${section}}`, ctx);
|
|
127
|
+
return display({ k: "var", name: ctx.attribute(objectAttribute(object, attribute)) });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return literalBraces(convertQuestText(section, ctx, mode));
|
|
131
|
+
}
|
|
132
|
+
// Text Quest shows in curly brackets, as it doesn't know the command. They're written as
|
|
133
|
+
// entities, so Squiffy doesn't take them for Squiffy 5 commands.
|
|
134
|
+
function literalBraces(inner) {
|
|
135
|
+
return "{" + inner + "}";
|
|
136
|
+
}
|
|
137
|
+
// The Squiffy attribute for an attribute of the player or a page, such as player.strength. Flags
|
|
138
|
+
// and counters are attributes of the game, and keep their names.
|
|
139
|
+
export function objectAttribute(object, attribute) {
|
|
140
|
+
return object == "game" ? attribute : `${object}_${attribute}`;
|
|
141
|
+
}
|
|
142
|
+
// Reports an attribute of the player that nothing in the game sets, such as player.alias in a
|
|
143
|
+
// game that never asks for the player's name, as it shows nothing in Squiffy
|
|
144
|
+
function checkPlayerAttribute(attribute, text, ctx) {
|
|
145
|
+
if (ctx.playerHas?.(attribute) !== false)
|
|
146
|
+
return;
|
|
147
|
+
ctx.approximate(text, `Nothing in the game sets player.${attribute}, so this shows nothing. Set it at the start of the story, or ask the player for it with a text input.`);
|
|
148
|
+
}
|
|
149
|
+
// {if flag:text}, {if not flag:text}, {if counter>5:text}, {if game.flag:text}, {if Page3.visited:text}
|
|
150
|
+
function convertIf(section, ctx, mode) {
|
|
151
|
+
const command = section.substring(3);
|
|
152
|
+
const colon = command.indexOf(":");
|
|
153
|
+
// Quest shows an {if} with no text as it is
|
|
154
|
+
if (colon == -1)
|
|
155
|
+
return literalBraces(convertQuestText(section, ctx, mode));
|
|
156
|
+
const text = convertQuestText(command.substring(colon + 1), ctx, mode);
|
|
157
|
+
const condition = command.substring(0, colon);
|
|
158
|
+
const operator = ["<=", ">=", "<>", "<", ">", "="].find(op => condition.includes(op));
|
|
159
|
+
let expr;
|
|
160
|
+
if (!operator) {
|
|
161
|
+
let name = condition.trim();
|
|
162
|
+
let negate = false;
|
|
163
|
+
if (name.startsWith("not ")) {
|
|
164
|
+
negate = true;
|
|
165
|
+
name = name.substring(4).trim();
|
|
166
|
+
}
|
|
167
|
+
expr = reference(name, ctx);
|
|
168
|
+
if (negate)
|
|
169
|
+
expr = { k: "not", e: expr };
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
const index = condition.indexOf(operator);
|
|
173
|
+
const lhs = reference(condition.substring(0, index).trim(), ctx);
|
|
174
|
+
const rhsText = condition.substring(index + operator.length).trim();
|
|
175
|
+
const rhs = /^-?\d+(\.\d+)?$/.test(rhsText) ? { k: "num", v: Number(rhsText) } : { k: "str", v: rhsText };
|
|
176
|
+
expr = { k: "bin", op: operator == "<>" ? "!=" : operator, l: lhs, r: rhs };
|
|
177
|
+
}
|
|
178
|
+
return `{{#if ${printCondition(expr, mode)}}}${text}{{/if}}`;
|
|
179
|
+
}
|
|
180
|
+
// A condition, as it's written in an {{#if}}. A link's text goes in a text="..." string, where
|
|
181
|
+
// the compiler writes a " as \", which the condition can't then read, so text in it is quoted
|
|
182
|
+
// with single quotes.
|
|
183
|
+
function printCondition(expr, mode) {
|
|
184
|
+
if (mode == "block")
|
|
185
|
+
return print(expr);
|
|
186
|
+
const printed = print(expr, { preferSingle: true });
|
|
187
|
+
if (printed.includes("\""))
|
|
188
|
+
throw new Unconvertible("a condition in a link's text can't compare with text that has a quote mark in it");
|
|
189
|
+
return printed;
|
|
190
|
+
}
|
|
191
|
+
// A flag or counter, game.attribute, or page.visited, in an {if}
|
|
192
|
+
function reference(name, ctx) {
|
|
193
|
+
const dot = name.indexOf(".");
|
|
194
|
+
if (dot == -1)
|
|
195
|
+
return { k: "var", name: ctx.attribute(name) };
|
|
196
|
+
const object = name.substring(0, dot).trim();
|
|
197
|
+
const attribute = name.substring(dot + 1).trim();
|
|
198
|
+
if (!/^\w+$/.test(attribute))
|
|
199
|
+
throw new Unconvertible(`Squiffy can't read ${name}`);
|
|
200
|
+
if (object == "game")
|
|
201
|
+
return { k: "var", name: ctx.attribute(attribute) };
|
|
202
|
+
const target = ctx.target(object);
|
|
203
|
+
if (attribute == "visited") {
|
|
204
|
+
if (target && "section" in target)
|
|
205
|
+
return { k: "call", fn: "seen", args: [{ k: "str", v: target.section }] };
|
|
206
|
+
if (!target) {
|
|
207
|
+
ctx.approximate(name, `There's no page called "${object}", so this is never true, as in Quest.`);
|
|
208
|
+
return { k: "bool", v: false };
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (object == "player" || target)
|
|
212
|
+
return { k: "var", name: ctx.attribute(objectAttribute(object, attribute)) };
|
|
213
|
+
throw new Unconvertible(`there's nothing called "${object}" in the game`);
|
|
214
|
+
}
|
|
215
|
+
// {page:Page2}, {page:Page2:text}, and {command:Page2:text}, which does the same in a gamebook
|
|
216
|
+
function convertPageLink(section, ctx, mode) {
|
|
217
|
+
let page = section.substring(section.indexOf(":") + 1);
|
|
218
|
+
let text = page;
|
|
219
|
+
const colon = page.indexOf(":");
|
|
220
|
+
if (colon != -1) {
|
|
221
|
+
text = page.substring(colon + 1);
|
|
222
|
+
page = page.substring(0, colon);
|
|
223
|
+
}
|
|
224
|
+
const linkText = convertQuestText(text, ctx, "inline");
|
|
225
|
+
if (mode == "inline") {
|
|
226
|
+
ctx.approximate(`{${section}}`, "This link is in a link's text, or one of a random choice, where Squiffy can't have a link, so only its text has been kept.");
|
|
227
|
+
return linkText;
|
|
228
|
+
}
|
|
229
|
+
const target = ctx.target(page);
|
|
230
|
+
if (!target) {
|
|
231
|
+
ctx.approximate(`{${section}}`, `There's no page called "${page}", so this isn't a link.`);
|
|
232
|
+
return linkText;
|
|
233
|
+
}
|
|
234
|
+
if ("url" in target)
|
|
235
|
+
return urlLink(linkText, target.url);
|
|
236
|
+
return sectionLink(linkText, target.section);
|
|
237
|
+
}
|
|
238
|
+
// Quest's Tsplit: splits at colons that aren't inside {...}
|
|
239
|
+
function splitItems(text) {
|
|
240
|
+
const items = [];
|
|
241
|
+
let depth = 0;
|
|
242
|
+
let start = 0;
|
|
243
|
+
for (let i = 0; i < text.length; i++) {
|
|
244
|
+
if (text[i] == "{")
|
|
245
|
+
depth++;
|
|
246
|
+
else if (text[i] == "}")
|
|
247
|
+
depth--;
|
|
248
|
+
else if (text[i] == ":" && depth <= 0) {
|
|
249
|
+
items.push(text.substring(start, i));
|
|
250
|
+
start = i + 1;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
items.push(text.substring(start));
|
|
254
|
+
return items;
|
|
255
|
+
}
|
|
256
|
+
function convertRandom(text, ctx) {
|
|
257
|
+
const items = splitItems(text).map(item => {
|
|
258
|
+
const converted = convertQuestText(item, ctx, "inline");
|
|
259
|
+
if (/\{\{|\[\[/.test(converted))
|
|
260
|
+
throw new Unconvertible("Squiffy's {{random}} can't have links or other commands in its choices");
|
|
261
|
+
return converted;
|
|
262
|
+
});
|
|
263
|
+
return `{{random (array${items.map(i => " " + quote(i)).join("")})}}`;
|
|
264
|
+
}
|
|
265
|
+
function escapeAttribute(s) {
|
|
266
|
+
return s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/\[/g, "[").replace(/\]/g, "]");
|
|
267
|
+
}
|
|
268
|
+
// Text from the page, with no commands in it, as Squiffy text. Quest's text is HTML, not
|
|
269
|
+
// Markdown, so anything Markdown would read is escaped, outside HTML tags. In a page's text,
|
|
270
|
+
// <br/> and line breaks become Squiffy lines, and two or more of them a new paragraph.
|
|
271
|
+
function literal(text, mode) {
|
|
272
|
+
if (!text)
|
|
273
|
+
return "";
|
|
274
|
+
let result = "";
|
|
275
|
+
let pos = 0;
|
|
276
|
+
for (const tag of text.matchAll(/<\/?[A-Za-z][^<>]*>/g)) {
|
|
277
|
+
result += prose(text.substring(pos, tag.index));
|
|
278
|
+
result += tag[0].replace(/\[/g, "[").replace(/\]/g, "]").replace(/\{\{/g, "{{").replace(/\}\}/g, "}}");
|
|
279
|
+
pos = tag.index + tag[0].length;
|
|
280
|
+
}
|
|
281
|
+
result += prose(text.substring(pos));
|
|
282
|
+
if (mode == "inline")
|
|
283
|
+
return result.replace(/\s*\n\s*/g, " ");
|
|
284
|
+
return result.replace(/(?:[ \t]*(?:<br\s*\/?>|\n)[ \t]*)+/gi, breaks => {
|
|
285
|
+
const count = (breaks.match(/<br\s*\/?>|\n/gi) ?? []).length;
|
|
286
|
+
return count >= 2 ? "\n\n" : "\n";
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
function prose(text) {
|
|
290
|
+
let result = "";
|
|
291
|
+
for (let i = 0; i < text.length; i++) {
|
|
292
|
+
const c = text[i];
|
|
293
|
+
if (c == "*")
|
|
294
|
+
result += "*";
|
|
295
|
+
else if (c == "`")
|
|
296
|
+
result += "`";
|
|
297
|
+
else if (c == "~")
|
|
298
|
+
result += "~";
|
|
299
|
+
else if (c == "\\")
|
|
300
|
+
result += "\";
|
|
301
|
+
else if (c == "[")
|
|
302
|
+
result += "[";
|
|
303
|
+
else if (c == "]")
|
|
304
|
+
result += "]";
|
|
305
|
+
else if (c == "_" && !(/\w/.test(text[i - 1] ?? "") && /\w/.test(text[i + 1] ?? "")))
|
|
306
|
+
result += "_";
|
|
307
|
+
// A { here has no }, so Quest shows it, and the rest of the text, as it is
|
|
308
|
+
else if (c == "{")
|
|
309
|
+
result += "{";
|
|
310
|
+
else if (c == "}")
|
|
311
|
+
result += "}";
|
|
312
|
+
else
|
|
313
|
+
result += c;
|
|
314
|
+
}
|
|
315
|
+
return result;
|
|
316
|
+
}
|
|
317
|
+
// Lines that Markdown would read as a heading, list or quote, which in Quest are just text
|
|
318
|
+
export function escapeLineStarts(text) {
|
|
319
|
+
return text.split("\n").map(line => line
|
|
320
|
+
.replace(/^(\s*)#/, "$1#")
|
|
321
|
+
.replace(/^(\s*)([-+])(\s)/, (_, space, c, after) => `${space}${c == "-" ? "-" : "+"}${after}`)
|
|
322
|
+
.replace(/^(\s*\d+)\.(\s)/, "$1.$2")).join("\n");
|
|
323
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { SectionNode, SquiffyFile } from "./syntax.js";
|
|
2
|
+
export declare function sectionDetail(node: SectionNode): string | undefined;
|
|
3
|
+
export declare const capitalise: (text: string) => string;
|
|
4
|
+
export declare function sectionLabels(files: Iterable<SquiffyFile>, nameOf: (node: SectionNode) => string | undefined): Map<string, string>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// What messages call sections, so that a section without a name of its own isn't called by the
|
|
2
|
+
// name the compiler gives it. Used by the compiler's warnings, the story graph's and the CLI's.
|
|
3
|
+
// What the outline calls a section without a name of its own
|
|
4
|
+
export function sectionDetail(node) {
|
|
5
|
+
if (node.kind == "preamble")
|
|
6
|
+
return "(Start)";
|
|
7
|
+
if (node.header?.type == "continue")
|
|
8
|
+
return `+++${node.header.text}`;
|
|
9
|
+
if (node.header?.type == "separator")
|
|
10
|
+
return "---";
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
const ORDINALS = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"];
|
|
14
|
+
const ordinal = (n) => ORDINALS[n - 1] ?? `${n}th`;
|
|
15
|
+
export const capitalise = (text) => text.charAt(0).toUpperCase() + text.slice(1);
|
|
16
|
+
// What messages call each section, by name. A section without a name of its own is called by its
|
|
17
|
+
// +++ or --- line, not the name the compiler gives it (_continue1, ...), which the author never
|
|
18
|
+
// wrote and can't search for. As a story can have lots of "+++Continue..." lines, it also says
|
|
19
|
+
// which named section it comes after, and which one it is if there are several:
|
|
20
|
+
// 'the section "cave"', 'the "+++Continue..." after "cave"', 'the second "---" after "cave"',
|
|
21
|
+
// 'the third "+++Next"' (before any named section), 'the text before the first section'.
|
|
22
|
+
export function sectionLabels(files, nameOf) {
|
|
23
|
+
const labels = new Map();
|
|
24
|
+
for (const tree of files) {
|
|
25
|
+
// The sections without names of their own since the last named one
|
|
26
|
+
let after;
|
|
27
|
+
let run = [];
|
|
28
|
+
const finish = () => {
|
|
29
|
+
const total = new Map();
|
|
30
|
+
for (const { detail } of run)
|
|
31
|
+
total.set(detail, (total.get(detail) ?? 0) + 1);
|
|
32
|
+
const seen = new Map();
|
|
33
|
+
for (const { name, detail } of run) {
|
|
34
|
+
const n = (seen.get(detail) ?? 0) + 1;
|
|
35
|
+
seen.set(detail, n);
|
|
36
|
+
const which = total.get(detail) > 1 ? `${ordinal(n)} ` : "";
|
|
37
|
+
const where = after !== undefined ? ` after "${after}"` : "";
|
|
38
|
+
labels.set(name, `the ${which}"${detail}"${where}`);
|
|
39
|
+
}
|
|
40
|
+
run = [];
|
|
41
|
+
};
|
|
42
|
+
for (const node of [tree.preamble, ...tree.sections]) {
|
|
43
|
+
if (node.type != "section")
|
|
44
|
+
continue;
|
|
45
|
+
const name = nameOf(node);
|
|
46
|
+
if (name === undefined)
|
|
47
|
+
continue;
|
|
48
|
+
if (node.kind == "preamble") {
|
|
49
|
+
labels.set(name, "the text before the first section");
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const detail = sectionDetail(node);
|
|
53
|
+
if (detail === undefined) {
|
|
54
|
+
finish();
|
|
55
|
+
after = name;
|
|
56
|
+
labels.set(name, `the section "${name}"`);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
run.push({ name, detail });
|
|
60
|
+
}
|
|
61
|
+
finish();
|
|
62
|
+
}
|
|
63
|
+
return labels;
|
|
64
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Range, SquiffyFile } from "./syntax.js";
|
|
2
|
+
export interface SetterConversionProblem {
|
|
3
|
+
line: number;
|
|
4
|
+
text: string;
|
|
5
|
+
reason: string;
|
|
6
|
+
}
|
|
7
|
+
export interface SetterConversionResult {
|
|
8
|
+
script: string;
|
|
9
|
+
converted: number;
|
|
10
|
+
problems: SetterConversionProblem[];
|
|
11
|
+
}
|
|
12
|
+
export type SetterConversion = {
|
|
13
|
+
text: string;
|
|
14
|
+
} | {
|
|
15
|
+
problem: string;
|
|
16
|
+
};
|
|
17
|
+
export declare function convertSetter(setter: string, options?: {
|
|
18
|
+
preferSingleQuotes?: boolean;
|
|
19
|
+
}): SetterConversion;
|
|
20
|
+
export declare function convertSetters(script: string): SetterConversionResult;
|
|
21
|
+
export interface AtAttribute {
|
|
22
|
+
text: string;
|
|
23
|
+
range: Range;
|
|
24
|
+
}
|
|
25
|
+
export declare function findAtAttributes(file: SquiffyFile): AtAttribute[];
|
|
26
|
+
export declare function splitWithOffsets(setters: string): {
|
|
27
|
+
text: string;
|
|
28
|
+
offset: number;
|
|
29
|
+
}[];
|
|
30
|
+
export declare function attributesSetIn(file: SquiffyFile, text: string): Set<string>;
|
|
31
|
+
export interface UnquotedText {
|
|
32
|
+
text: string;
|
|
33
|
+
range: Range;
|
|
34
|
+
attribute: string;
|
|
35
|
+
kind: "word" | "words";
|
|
36
|
+
quoted: string | null;
|
|
37
|
+
}
|
|
38
|
+
export declare function findUnquotedText(file: SquiffyFile, isSet: (name: string) => boolean): UnquotedText[];
|
|
39
|
+
export declare const BUILT_IN_ATTRIBUTES: ReadonlyMap<string, string>;
|
|
40
|
+
export interface BuiltInAttributeSet {
|
|
41
|
+
name: string;
|
|
42
|
+
range: Range;
|
|
43
|
+
}
|
|
44
|
+
export declare function findBuiltInAttributeSets(file: SquiffyFile, text: string): BuiltInAttributeSet[];
|
|
45
|
+
export interface AttributeValues {
|
|
46
|
+
values: Set<string>;
|
|
47
|
+
unknown: boolean;
|
|
48
|
+
}
|
|
49
|
+
export declare function attributeValues(files: Iterable<SquiffyFile>): Map<string, AttributeValues>;
|