squiffy-compiler 7.0.0-beta.4 → 7.0.0-beta.6
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 +3 -0
- package/dist/analyse.js +73 -12
- package/dist/compiler.d.ts +12 -2
- package/dist/compiler.js +6 -2
- package/dist/emit.d.ts +6 -0
- package/dist/emit.js +17 -1
- package/dist/language-service.js +36 -5
- package/dist/node.d.ts +8 -1
- package/dist/node.js +19 -5
- package/dist/parse.js +18 -2
- package/dist/project.d.ts +56 -0
- package/dist/project.js +159 -17
- package/dist/quest/aslx.d.ts +50 -0
- package/dist/quest/aslx.js +181 -0
- package/dist/quest/convert.d.ts +20 -0
- package/dist/quest/convert.js +475 -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 +501 -0
- package/dist/quest/text.d.ts +17 -0
- package/dist/quest/text.js +305 -0
- package/dist/setters.d.ts +6 -0
- package/dist/setters.js +43 -0
- package/dist/story-graph.d.ts +3 -1
- package/dist/story-graph.js +12 -3
- package/dist/syntax.d.ts +22 -2
- package/dist/text.d.ts +4 -0
- package/dist/text.js +28 -21
- package/dist/twine/convert.d.ts +82 -0
- package/dist/twine/convert.js +744 -0
- package/dist/twine/expression.d.ts +68 -0
- package/dist/twine/expression.js +215 -0
- package/dist/twine/harlowe.d.ts +18 -0
- package/dist/twine/harlowe.js +1912 -0
- package/dist/twine/story.d.ts +24 -0
- package/dist/twine/story.js +231 -0
- package/dist/twine/sugarcube.d.ts +34 -0
- package/dist/twine/sugarcube.js +1621 -0
- package/dist/v5.js +2 -1
- package/package.json +3 -2
- package/syntaxes/squiffy.tmLanguage.json +1 -1
|
@@ -0,0 +1,305 @@
|
|
|
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 { 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
|
+
case "once:":
|
|
74
|
+
return `{{#if ${print(visits(ctx))} = 1}}${nested(section.substring(5))}{{/if}}`;
|
|
75
|
+
case "notfirst:":
|
|
76
|
+
return `{{#if ${print(visits(ctx))} > 1}}${nested(section.substring(9))}{{/if}}`;
|
|
77
|
+
case "random:":
|
|
78
|
+
return convertRandom(section.substring(7), ctx);
|
|
79
|
+
case "img:": {
|
|
80
|
+
const file = section.substring(4).trim();
|
|
81
|
+
ctx.asset(file);
|
|
82
|
+
return `<img src="${escapeAttribute(file)}" alt="">`;
|
|
83
|
+
}
|
|
84
|
+
case "counter:":
|
|
85
|
+
// Quest shows 0 for a counter that hasn't been set, as adding 0 does
|
|
86
|
+
return `{{= ${attributeRef(ctx.attribute(section.substring(8)))} + 0}}`;
|
|
87
|
+
case "popup:": {
|
|
88
|
+
const parts = section.substring(6).split(":");
|
|
89
|
+
const link = parts[0];
|
|
90
|
+
const content = parts.slice(1).join(":");
|
|
91
|
+
if (!ctx.addPassage || mode == "inline") {
|
|
92
|
+
ctx.approximate(`{${section}}`, "Squiffy has no popups, and this one is in a link's text, so only its link text has been kept.");
|
|
93
|
+
return nested(link);
|
|
94
|
+
}
|
|
95
|
+
// A passage link shows the popup's text when it's clicked
|
|
96
|
+
const name = ctx.addPassage(link, convertQuestText(content, ctx, "block"));
|
|
97
|
+
return `[${nested(link).replace(/\]/g, "]").replace(/\[/g, "[")}](${name})`;
|
|
98
|
+
}
|
|
99
|
+
case "either ":
|
|
100
|
+
case "eval:":
|
|
101
|
+
case "=":
|
|
102
|
+
throw new Unconvertible("Quest expressions in text can't be converted yet");
|
|
103
|
+
case "here ":
|
|
104
|
+
case "nothere ":
|
|
105
|
+
case "object:":
|
|
106
|
+
case "exit:":
|
|
107
|
+
throw new Unconvertible("it's for Quest's text adventures, not gamebooks");
|
|
108
|
+
case "rndalt:":
|
|
109
|
+
case "select:":
|
|
110
|
+
throw new Unconvertible(`Squiffy has nothing like {${command}}`);
|
|
111
|
+
}
|
|
112
|
+
return convertOther(section, ctx, mode);
|
|
113
|
+
}
|
|
114
|
+
// {game.score} shows an attribute. Anything else in curly brackets is shown as it is.
|
|
115
|
+
function convertOther(section, ctx, mode) {
|
|
116
|
+
const dot = section.indexOf(".");
|
|
117
|
+
if (dot != -1) {
|
|
118
|
+
const object = section.substring(0, dot);
|
|
119
|
+
const attribute = section.substring(dot + 1);
|
|
120
|
+
if (object == "game" && /^[\w ]+$/.test(attribute)) {
|
|
121
|
+
return display({ k: "var", name: ctx.attribute(attribute) });
|
|
122
|
+
}
|
|
123
|
+
if (/^[\w ]+$/.test(object) && /^\w+$/.test(attribute) && (object == "player" || ctx.target(object))) {
|
|
124
|
+
return display({ k: "var", name: ctx.attribute(objectAttribute(object, attribute)) });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return literalBraces(convertQuestText(section, ctx, mode));
|
|
128
|
+
}
|
|
129
|
+
// Text Quest shows in curly brackets, as it doesn't know the command. They're written as
|
|
130
|
+
// entities, so Squiffy doesn't take them for Squiffy 5 commands.
|
|
131
|
+
function literalBraces(inner) {
|
|
132
|
+
return "{" + inner + "}";
|
|
133
|
+
}
|
|
134
|
+
// The Squiffy attribute for an attribute of the player or a page, such as player.strength. Flags
|
|
135
|
+
// and counters are attributes of the game, and keep their names.
|
|
136
|
+
export function objectAttribute(object, attribute) {
|
|
137
|
+
return object == "game" ? attribute : `${object}_${attribute}`;
|
|
138
|
+
}
|
|
139
|
+
function visits(ctx) {
|
|
140
|
+
return { k: "call", fn: "visits", args: [{ k: "str", v: ctx.section }] };
|
|
141
|
+
}
|
|
142
|
+
// {if flag:text}, {if not flag:text}, {if counter>5:text}, {if game.flag:text}, {if Page3.visited:text}
|
|
143
|
+
function convertIf(section, ctx, mode) {
|
|
144
|
+
const command = section.substring(3);
|
|
145
|
+
const colon = command.indexOf(":");
|
|
146
|
+
// Quest shows an {if} with no text as it is
|
|
147
|
+
if (colon == -1)
|
|
148
|
+
return literalBraces(convertQuestText(section, ctx, mode));
|
|
149
|
+
const text = convertQuestText(command.substring(colon + 1), ctx, mode);
|
|
150
|
+
const condition = command.substring(0, colon);
|
|
151
|
+
const operator = ["<=", ">=", "<>", "<", ">", "="].find(op => condition.includes(op));
|
|
152
|
+
let expr;
|
|
153
|
+
if (!operator) {
|
|
154
|
+
let name = condition.trim();
|
|
155
|
+
let negate = false;
|
|
156
|
+
if (name.startsWith("not ")) {
|
|
157
|
+
negate = true;
|
|
158
|
+
name = name.substring(4).trim();
|
|
159
|
+
}
|
|
160
|
+
expr = reference(name, ctx);
|
|
161
|
+
if (negate)
|
|
162
|
+
expr = { k: "not", e: expr };
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
const index = condition.indexOf(operator);
|
|
166
|
+
const lhs = reference(condition.substring(0, index).trim(), ctx);
|
|
167
|
+
const rhsText = condition.substring(index + operator.length).trim();
|
|
168
|
+
const rhs = /^-?\d+(\.\d+)?$/.test(rhsText) ? { k: "num", v: Number(rhsText) } : { k: "str", v: rhsText };
|
|
169
|
+
expr = { k: "bin", op: operator == "<>" ? "!=" : operator, l: lhs, r: rhs };
|
|
170
|
+
}
|
|
171
|
+
return `{{#if ${print(expr)}}}${text}{{/if}}`;
|
|
172
|
+
}
|
|
173
|
+
// A flag or counter, game.attribute, or page.visited, in an {if}
|
|
174
|
+
function reference(name, ctx) {
|
|
175
|
+
const dot = name.indexOf(".");
|
|
176
|
+
if (dot == -1)
|
|
177
|
+
return { k: "var", name: ctx.attribute(name) };
|
|
178
|
+
const object = name.substring(0, dot).trim();
|
|
179
|
+
const attribute = name.substring(dot + 1).trim();
|
|
180
|
+
if (!/^\w+$/.test(attribute))
|
|
181
|
+
throw new Unconvertible(`Squiffy can't read ${name}`);
|
|
182
|
+
if (object == "game")
|
|
183
|
+
return { k: "var", name: ctx.attribute(attribute) };
|
|
184
|
+
const target = ctx.target(object);
|
|
185
|
+
if (attribute == "visited") {
|
|
186
|
+
if (target && "section" in target)
|
|
187
|
+
return { k: "call", fn: "seen", args: [{ k: "str", v: target.section }] };
|
|
188
|
+
if (!target) {
|
|
189
|
+
ctx.approximate(name, `There's no page called "${object}", so this is never true, as in Quest.`);
|
|
190
|
+
return { k: "bool", v: false };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (object == "player" || target)
|
|
194
|
+
return { k: "var", name: ctx.attribute(objectAttribute(object, attribute)) };
|
|
195
|
+
throw new Unconvertible(`there's nothing called "${object}" in the game`);
|
|
196
|
+
}
|
|
197
|
+
// {page:Page2}, {page:Page2:text}, and {command:Page2:text}, which does the same in a gamebook
|
|
198
|
+
function convertPageLink(section, ctx, mode) {
|
|
199
|
+
let page = section.substring(section.indexOf(":") + 1);
|
|
200
|
+
let text = page;
|
|
201
|
+
const colon = page.indexOf(":");
|
|
202
|
+
if (colon != -1) {
|
|
203
|
+
text = page.substring(colon + 1);
|
|
204
|
+
page = page.substring(0, colon);
|
|
205
|
+
}
|
|
206
|
+
const linkText = convertQuestText(text, ctx, "inline");
|
|
207
|
+
if (mode == "inline") {
|
|
208
|
+
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.");
|
|
209
|
+
return linkText;
|
|
210
|
+
}
|
|
211
|
+
const target = ctx.target(page);
|
|
212
|
+
if (!target) {
|
|
213
|
+
ctx.approximate(`{${section}}`, `There's no page called "${page}", so this isn't a link.`);
|
|
214
|
+
return linkText;
|
|
215
|
+
}
|
|
216
|
+
if ("url" in target)
|
|
217
|
+
return urlLink(linkText, target.url);
|
|
218
|
+
return sectionLink(linkText, target.section);
|
|
219
|
+
}
|
|
220
|
+
// Quest's Tsplit: splits at colons that aren't inside {...}
|
|
221
|
+
function splitItems(text) {
|
|
222
|
+
const items = [];
|
|
223
|
+
let depth = 0;
|
|
224
|
+
let start = 0;
|
|
225
|
+
for (let i = 0; i < text.length; i++) {
|
|
226
|
+
if (text[i] == "{")
|
|
227
|
+
depth++;
|
|
228
|
+
else if (text[i] == "}")
|
|
229
|
+
depth--;
|
|
230
|
+
else if (text[i] == ":" && depth <= 0) {
|
|
231
|
+
items.push(text.substring(start, i));
|
|
232
|
+
start = i + 1;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
items.push(text.substring(start));
|
|
236
|
+
return items;
|
|
237
|
+
}
|
|
238
|
+
function convertRandom(text, ctx) {
|
|
239
|
+
const items = splitItems(text).map(item => {
|
|
240
|
+
const converted = convertQuestText(item, ctx, "inline");
|
|
241
|
+
if (/\{\{|\[\[/.test(converted))
|
|
242
|
+
throw new Unconvertible("Squiffy's {{random}} can't have links or other commands in its choices");
|
|
243
|
+
return converted;
|
|
244
|
+
});
|
|
245
|
+
return `{{random (array${items.map(i => " " + quote(i)).join("")})}}`;
|
|
246
|
+
}
|
|
247
|
+
function escapeAttribute(s) {
|
|
248
|
+
return s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/\[/g, "[").replace(/\]/g, "]");
|
|
249
|
+
}
|
|
250
|
+
// Text from the page, with no commands in it, as Squiffy text. Quest's text is HTML, not
|
|
251
|
+
// Markdown, so anything Markdown would read is escaped, outside HTML tags. In a page's text,
|
|
252
|
+
// <br/> and line breaks become Squiffy lines, and two or more of them a new paragraph.
|
|
253
|
+
function literal(text, mode) {
|
|
254
|
+
if (!text)
|
|
255
|
+
return "";
|
|
256
|
+
let result = "";
|
|
257
|
+
let pos = 0;
|
|
258
|
+
for (const tag of text.matchAll(/<\/?[A-Za-z][^<>]*>/g)) {
|
|
259
|
+
result += prose(text.substring(pos, tag.index));
|
|
260
|
+
result += tag[0].replace(/\[/g, "[").replace(/\]/g, "]").replace(/\{\{/g, "{{").replace(/\}\}/g, "}}");
|
|
261
|
+
pos = tag.index + tag[0].length;
|
|
262
|
+
}
|
|
263
|
+
result += prose(text.substring(pos));
|
|
264
|
+
if (mode == "inline")
|
|
265
|
+
return result.replace(/\s*\n\s*/g, " ");
|
|
266
|
+
return result.replace(/(?:[ \t]*(?:<br\s*\/?>|\n)[ \t]*)+/gi, breaks => {
|
|
267
|
+
const count = (breaks.match(/<br\s*\/?>|\n/gi) ?? []).length;
|
|
268
|
+
return count >= 2 ? "\n\n" : "\n";
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
function prose(text) {
|
|
272
|
+
let result = "";
|
|
273
|
+
for (let i = 0; i < text.length; i++) {
|
|
274
|
+
const c = text[i];
|
|
275
|
+
if (c == "*")
|
|
276
|
+
result += "*";
|
|
277
|
+
else if (c == "`")
|
|
278
|
+
result += "`";
|
|
279
|
+
else if (c == "~")
|
|
280
|
+
result += "~";
|
|
281
|
+
else if (c == "\\")
|
|
282
|
+
result += "\";
|
|
283
|
+
else if (c == "[")
|
|
284
|
+
result += "[";
|
|
285
|
+
else if (c == "]")
|
|
286
|
+
result += "]";
|
|
287
|
+
else if (c == "_" && !(/\w/.test(text[i - 1] ?? "") && /\w/.test(text[i + 1] ?? "")))
|
|
288
|
+
result += "_";
|
|
289
|
+
// A { here has no }, so Quest shows it, and the rest of the text, as it is
|
|
290
|
+
else if (c == "{")
|
|
291
|
+
result += "{";
|
|
292
|
+
else if (c == "}")
|
|
293
|
+
result += "}";
|
|
294
|
+
else
|
|
295
|
+
result += c;
|
|
296
|
+
}
|
|
297
|
+
return result;
|
|
298
|
+
}
|
|
299
|
+
// Lines that Markdown would read as a heading, list or quote, which in Quest are just text
|
|
300
|
+
export function escapeLineStarts(text) {
|
|
301
|
+
return text.split("\n").map(line => line
|
|
302
|
+
.replace(/^(\s*)#/, "$1#")
|
|
303
|
+
.replace(/^(\s*)([-+])(\s)/, (_, space, c, after) => `${space}${c == "-" ? "-" : "+"}${after}`)
|
|
304
|
+
.replace(/^(\s*\d+)\.(\s)/, "$1.$2")).join("\n");
|
|
305
|
+
}
|
package/dist/setters.d.ts
CHANGED
|
@@ -32,3 +32,9 @@ export interface UnquotedText {
|
|
|
32
32
|
quoted: string | null;
|
|
33
33
|
}
|
|
34
34
|
export declare function findUnquotedText(file: SquiffyFile, isSet: (name: string) => boolean): UnquotedText[];
|
|
35
|
+
export declare const BUILT_IN_ATTRIBUTES: ReadonlyMap<string, string>;
|
|
36
|
+
export interface BuiltInAttributeSet {
|
|
37
|
+
name: string;
|
|
38
|
+
range: Range;
|
|
39
|
+
}
|
|
40
|
+
export declare function findBuiltInAttributeSets(file: SquiffyFile, text: string): BuiltInAttributeSet[];
|
package/dist/setters.js
CHANGED
|
@@ -369,3 +369,46 @@ export function findUnquotedText(file, isSet) {
|
|
|
369
369
|
}
|
|
370
370
|
return result;
|
|
371
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
|
+
// #56: and a story may have kept track of where the player had been
|
|
377
|
+
["previous", "the section or passage the player saw just before this one"],
|
|
378
|
+
["previous_section", "the section the player was in before this one"],
|
|
379
|
+
]);
|
|
380
|
+
// Finds where a file sets one of the BUILT_IN_ATTRIBUTES: on the left of a setter, in @inc, @dec
|
|
381
|
+
// and @unset, and as the quoted first argument of set, inc, dec or unset, which covers
|
|
382
|
+
// {{set "turns" 1}} and squiffy.set("turns", 1) in JavaScript.
|
|
383
|
+
export function findBuiltInAttributeSets(file, text) {
|
|
384
|
+
const result = [];
|
|
385
|
+
const add = (name, line, character) => {
|
|
386
|
+
if (BUILT_IN_ATTRIBUTES.has(name)) {
|
|
387
|
+
result.push({ name, range: { start: { line, character }, end: { line, character: character + name.length } } });
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
for (const site of setterSites(file)) {
|
|
391
|
+
const match = /^(\s*(?:not\s+)?)(\w+)/.exec(site.text);
|
|
392
|
+
if (match)
|
|
393
|
+
add(match[2], site.line, site.character + match[1].length);
|
|
394
|
+
}
|
|
395
|
+
for (const section of [file.preamble, ...file.sections]) {
|
|
396
|
+
if (section.type !== "section")
|
|
397
|
+
continue;
|
|
398
|
+
for (const container of [section, ...section.passages]) {
|
|
399
|
+
for (const node of container.body) {
|
|
400
|
+
if (node.type !== "attribute" || node.directive === "set")
|
|
401
|
+
continue;
|
|
402
|
+
const match = /^(\s*)(\w+)/.exec(node.value);
|
|
403
|
+
if (match)
|
|
404
|
+
add(match[2], node.valueRange.start.line, node.valueRange.start.character + match[1].length);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
text.replace(/\r/g, "").split("\n").forEach((line, i) => {
|
|
409
|
+
for (const match of line.matchAll(/\b(?:set|inc|dec|unset)\s*\(?\s*(["'])(\w+)\1/g)) {
|
|
410
|
+
add(match[2], i, match.index + match[0].length - match[2].length - 1);
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
return result.sort((a, b) => a.range.start.line - b.range.start.line || a.range.start.character - b.range.start.character);
|
|
414
|
+
}
|
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 { DeactivateNode, 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,8 @@ export interface GraphUsage {
|
|
|
39
39
|
sectionNode: SectionNode;
|
|
40
40
|
link?: LinkNode;
|
|
41
41
|
embed?: EmbedNode;
|
|
42
|
+
goto?: GotoNode;
|
|
43
|
+
deactivate?: DeactivateNode;
|
|
42
44
|
}
|
|
43
45
|
export type GraphTarget = {
|
|
44
46
|
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)) {
|
|
@@ -170,19 +173,25 @@ export function buildStoryGraph(source) {
|
|
|
170
173
|
return ranges.some(r => comparePositions(r.start, position) <= 0 && comparePositions(position, r.end) <= 0);
|
|
171
174
|
};
|
|
172
175
|
for (const usage of source.usages) {
|
|
176
|
+
if (usage.deactivate)
|
|
177
|
+
continue;
|
|
173
178
|
const from = containerIds.get(usage.container);
|
|
174
179
|
// Not part of the story, or replaced by a later section or passage of the same name
|
|
175
180
|
if (from === undefined)
|
|
176
181
|
continue;
|
|
177
182
|
const target = source.resolve(usage);
|
|
178
183
|
const to = target && (target.kind == "section" ? sectionId(target.name) : passageId(target.section, target.name));
|
|
179
|
-
const range = usage.link
|
|
184
|
+
const range = (usage.link ?? usage.embed ?? usage.goto).range;
|
|
180
185
|
const conditional = isConditional(usage.container, range.start);
|
|
181
186
|
const location = { file: usage.file, range };
|
|
182
187
|
if (usage.embed) {
|
|
183
188
|
edges.push({ from, ...(to ? { to } : { target: usage.embed.name }), kind: "embed", conditional, location });
|
|
184
189
|
continue;
|
|
185
190
|
}
|
|
191
|
+
if (usage.goto) {
|
|
192
|
+
edges.push({ from, ...(to ? { to } : { target: usage.goto.name }), kind: "goto", conditional, location });
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
186
195
|
const link = usage.link;
|
|
187
196
|
const dynamic = !to && link.target.includes("{{");
|
|
188
197
|
if (dynamic)
|
|
@@ -342,7 +351,7 @@ export function storyGraphDiagnostics(graph) {
|
|
|
342
351
|
range: node.location.range,
|
|
343
352
|
severity: "warning",
|
|
344
353
|
code: "squiffy/unreachable-section",
|
|
345
|
-
message:
|
|
354
|
+
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
355
|
});
|
|
347
356
|
}
|
|
348
357
|
else if (node.deadEnd) {
|
|
@@ -351,7 +360,7 @@ export function storyGraphDiagnostics(graph) {
|
|
|
351
360
|
range: node.location.range,
|
|
352
361
|
severity: "warning",
|
|
353
362
|
code: "squiffy/dead-end",
|
|
354
|
-
message:
|
|
363
|
+
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
364
|
});
|
|
356
365
|
}
|
|
357
366
|
}
|
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/at-attribute" | "squiffy/unquoted-text" | "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-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/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,8 @@ export interface SectionNode {
|
|
|
34
34
|
passages: PassageNode[];
|
|
35
35
|
links: LinkNode[];
|
|
36
36
|
embeds: EmbedNode[];
|
|
37
|
+
gotos: GotoNode[];
|
|
38
|
+
deactivates: DeactivateNode[];
|
|
37
39
|
}
|
|
38
40
|
export interface PassageNode {
|
|
39
41
|
type: "passage";
|
|
@@ -44,6 +46,8 @@ export interface PassageNode {
|
|
|
44
46
|
body: StatementNode[];
|
|
45
47
|
links: LinkNode[];
|
|
46
48
|
embeds: EmbedNode[];
|
|
49
|
+
gotos: GotoNode[];
|
|
50
|
+
deactivates: DeactivateNode[];
|
|
47
51
|
}
|
|
48
52
|
export interface SnippetNode {
|
|
49
53
|
type: "snippet";
|
|
@@ -109,6 +113,10 @@ export interface ClearNode {
|
|
|
109
113
|
type: "clear";
|
|
110
114
|
range: Range;
|
|
111
115
|
}
|
|
116
|
+
export interface RepeatNode {
|
|
117
|
+
type: "repeat";
|
|
118
|
+
range: Range;
|
|
119
|
+
}
|
|
112
120
|
export interface EndNode {
|
|
113
121
|
type: "end";
|
|
114
122
|
range: Range;
|
|
@@ -132,7 +140,7 @@ export interface TextNode {
|
|
|
132
140
|
text: string;
|
|
133
141
|
range: Range;
|
|
134
142
|
}
|
|
135
|
-
export type StatementNode = SeparatorNode | TitleNode | StartNode | ImportNode | UiBlockStartNode | UiFileNode | ClearNode | EndNode | AttributeNode | JsNode | TextNode;
|
|
143
|
+
export type StatementNode = SeparatorNode | TitleNode | StartNode | ImportNode | UiBlockStartNode | UiFileNode | ClearNode | RepeatNode | EndNode | AttributeNode | JsNode | TextNode;
|
|
136
144
|
export interface LinkNode {
|
|
137
145
|
type: "link";
|
|
138
146
|
linkType: "section" | "passage";
|
|
@@ -148,3 +156,15 @@ export interface EmbedNode {
|
|
|
148
156
|
range: Range;
|
|
149
157
|
nameRange: Range;
|
|
150
158
|
}
|
|
159
|
+
export interface DeactivateNode {
|
|
160
|
+
type: "deactivate";
|
|
161
|
+
name: string;
|
|
162
|
+
range: Range;
|
|
163
|
+
nameRange: Range;
|
|
164
|
+
}
|
|
165
|
+
export interface GotoNode {
|
|
166
|
+
type: "goto";
|
|
167
|
+
name: string;
|
|
168
|
+
range: Range;
|
|
169
|
+
nameRange: Range;
|
|
170
|
+
}
|
package/dist/text.d.ts
CHANGED
|
@@ -15,6 +15,8 @@ export interface TextEmbed {
|
|
|
15
15
|
nameStart: number;
|
|
16
16
|
nameEnd: number;
|
|
17
17
|
}
|
|
18
|
+
export type TextGoto = TextEmbed;
|
|
19
|
+
export type TextDeactivate = TextEmbed;
|
|
18
20
|
export interface NoNextSection {
|
|
19
21
|
display: string;
|
|
20
22
|
start: number;
|
|
@@ -24,6 +26,8 @@ export interface TransformResult {
|
|
|
24
26
|
output: string;
|
|
25
27
|
links: TextLink[];
|
|
26
28
|
embeds: TextEmbed[];
|
|
29
|
+
gotos: TextGoto[];
|
|
30
|
+
deactivates: TextDeactivate[];
|
|
27
31
|
noNextSection: NoNextSection[];
|
|
28
32
|
}
|
|
29
33
|
export declare const balancedTarget: string;
|
package/dist/text.js
CHANGED
|
@@ -170,9 +170,10 @@ export function transformText(text, getNextSectionName, expressions = false) {
|
|
|
170
170
|
};
|
|
171
171
|
// Handlebars expressions can contain square brackets that aren't links, such as
|
|
172
172
|
// {{someArray.[1]}} or {{someHelper "[x]"}}, so swap each one for a placeholder while
|
|
173
|
-
// converting links. Text between block helpers like {{#if x}}...{{/if}} is still converted.
|
|
173
|
+
// converting links. Text between block helpers like {{#if x}}...{{/if}} is still converted. A
|
|
174
|
+
// comment ends at --}}, and can have }} and links in it before that.
|
|
174
175
|
const mustaches = [];
|
|
175
|
-
input.replace(/\{\{\{[\s\S]*?\}\}\}|\{\{[\s\S]*?\}\}/g, ([match]) => {
|
|
176
|
+
input.replace(/\{\{!--[\s\S]*?--\}\}|\{\{\{[\s\S]*?\}\}\}|\{\{[\s\S]*?\}\}/g, ([match]) => {
|
|
176
177
|
mustaches.push(match);
|
|
177
178
|
return `${mustaches.length - 1}`;
|
|
178
179
|
});
|
|
@@ -181,8 +182,9 @@ export function transformText(text, getNextSectionName, expressions = false) {
|
|
|
181
182
|
// [[Buy]](shop, gold -= (price - 1) * 2), so the target can contain balanced brackets, up to
|
|
182
183
|
// three deep. Without, it ends at the first ")", as in Squiffy 6.
|
|
183
184
|
const target = expressions ? balancedTarget : "(?:.*?)";
|
|
184
|
-
// nextSectionLinkRegex matches [[text>]] - a link to the next section
|
|
185
|
-
|
|
185
|
+
// nextSectionLinkRegex matches [[text>]] - a link to the next section. Followed by a (target),
|
|
186
|
+
// it's a named link whose text ends with >, such as [[<i>Go in</i>]](cave).
|
|
187
|
+
const nextSectionLinkRegex = /\[\[([^\]]*?)>\]\](?!\()/g;
|
|
186
188
|
input.replace(nextSectionLinkRegex, ([match, rawText], offset) => {
|
|
187
189
|
const text = restore(rawText);
|
|
188
190
|
const nextSectionName = getNextSectionName();
|
|
@@ -263,23 +265,28 @@ export function transformText(text, getNextSectionName, expressions = false) {
|
|
|
263
265
|
return `{{passage "${escapeForHandlebarsString(name)}"}}${suffix}`;
|
|
264
266
|
});
|
|
265
267
|
input.replace(/(\d+)/g, ([, i]) => mustaches[Number(i)]);
|
|
266
|
-
//
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
268
|
+
// Matches {{embed "name"}} or {{goto 'name'}} with a literal name.
|
|
269
|
+
const findNamed = (helper) => {
|
|
270
|
+
const result = [];
|
|
271
|
+
const regex = new RegExp(`\\{\\{~?\\s*${helper}\\s+(["'])(.*?)\\1`, "g");
|
|
272
|
+
let match;
|
|
273
|
+
while ((match = regex.exec(input.value))) {
|
|
274
|
+
const start = match.index;
|
|
275
|
+
const end = start + match[0].length;
|
|
276
|
+
const nameEnd = end - 1;
|
|
277
|
+
const nameStart = nameEnd - match[2].length;
|
|
278
|
+
result.push({
|
|
279
|
+
name: match[2],
|
|
280
|
+
start: input.mapStart(start),
|
|
281
|
+
end: input.mapEnd(end),
|
|
282
|
+
nameStart: input.mapStart(nameStart),
|
|
283
|
+
nameEnd: input.mapEnd(nameEnd),
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
return result;
|
|
287
|
+
};
|
|
288
|
+
embeds.push(...findNamed("embed"));
|
|
289
|
+
return { output: input.value, links, embeds, gotos: findNamed("goto"), deactivates: findNamed("deactivate"), noNextSection };
|
|
283
290
|
}
|
|
284
291
|
// Returns a function that converts an offset into lines.join("\n") to a position in the source file.
|
|
285
292
|
export function positionMapper(lines, lineLengths) {
|