squiffy-compiler 7.0.0-beta.6 → 7.0.0-beta.8

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.
Files changed (46) hide show
  1. package/dist/analyse.d.ts +12 -1
  2. package/dist/analyse.js +156 -9
  3. package/dist/compiler.d.ts +5 -3
  4. package/dist/compiler.js +3 -2
  5. package/dist/css-urls.d.ts +4 -0
  6. package/dist/css-urls.js +59 -0
  7. package/dist/emit.d.ts +18 -1
  8. package/dist/emit.js +46 -0
  9. package/dist/infix-expressions.d.ts +19 -0
  10. package/dist/infix-expressions.js +713 -0
  11. package/dist/language-service.d.ts +1 -0
  12. package/dist/language-service.js +34 -15
  13. package/dist/node.d.ts +2 -1
  14. package/dist/node.js +1 -0
  15. package/dist/parse.d.ts +4 -0
  16. package/dist/parse.js +29 -0
  17. package/dist/project.d.ts +31 -0
  18. package/dist/project.js +98 -0
  19. package/dist/quest/convert.js +67 -11
  20. package/dist/quest/scripts.js +13 -9
  21. package/dist/quest/text.js +17 -8
  22. package/dist/section-labels.d.ts +4 -0
  23. package/dist/section-labels.js +64 -0
  24. package/dist/setters.d.ts +4 -0
  25. package/dist/setters.js +11 -9
  26. package/dist/story-graph.d.ts +2 -0
  27. package/dist/story-graph.js +61 -16
  28. package/dist/syntax.d.ts +24 -3
  29. package/dist/templates.d.ts +6 -0
  30. package/dist/templates.js +128 -0
  31. package/dist/text.d.ts +3 -1
  32. package/dist/text.js +45 -11
  33. package/dist/twine/audio.d.ts +21 -0
  34. package/dist/twine/audio.js +80 -0
  35. package/dist/twine/convert.d.ts +45 -3
  36. package/dist/twine/convert.js +305 -37
  37. package/dist/twine/expression.d.ts +21 -1
  38. package/dist/twine/expression.js +105 -8
  39. package/dist/twine/harlowe.d.ts +9 -0
  40. package/dist/twine/harlowe.js +461 -86
  41. package/dist/twine/settings.d.ts +9 -0
  42. package/dist/twine/settings.js +322 -0
  43. package/dist/twine/sugarcube.d.ts +3 -0
  44. package/dist/twine/sugarcube.js +438 -114
  45. package/package.json +9 -2
  46. package/syntaxes/squiffy.tmLanguage.json +8 -3
@@ -0,0 +1,9 @@
1
+ import type { PlayerSetting } from "../project.js";
2
+ export interface SettingsProblem {
3
+ text: string;
4
+ reason: string;
5
+ }
6
+ export declare function sugarCubeSettings(script: string): {
7
+ settings: PlayerSetting[];
8
+ problems: SettingsProblem[];
9
+ };
@@ -0,0 +1,322 @@
1
+ // SugarCube's player settings (#452), from the story's JavaScript: Setting.addToggle(),
2
+ // Setting.addList() and Setting.addRange() become squiffy.json's "settings". The story reads them
3
+ // as settings.name, as in SugarCube. Their onInit and onChange functions can't come with them, as
4
+ // they're JavaScript written for SugarCube, but each setting puts classes on the story's element,
5
+ // which the stylesheet can use instead.
6
+ //
7
+ // Only literal values are read: text, numbers, true and false, lists of those, and a list held in a
8
+ // variable the script sets to one, as in `var settingFontSize = ["100%", "125%"]`.
9
+ const isCode = (v) => !!v && typeof v == "object" && !Array.isArray(v) && "code" in v && Object.keys(v).length == 1;
10
+ const isName = (v) => !!v && typeof v == "object" && !Array.isArray(v) && "name" in v && Object.keys(v).length == 1;
11
+ export function sugarCubeSettings(script) {
12
+ const settings = [];
13
+ const problems = [];
14
+ const calls = /\bSetting\s*\.\s*add(Toggle|List|Range|Header|Value)\s*\(/g;
15
+ for (const match of script.matchAll(calls)) {
16
+ const kind = match[1];
17
+ const start = match.index + match[0].length;
18
+ let args;
19
+ try {
20
+ args = new LiteralParser(script, start).arguments();
21
+ }
22
+ catch {
23
+ problems.push({ text: `Setting.add${kind}()`, reason: "This setting couldn't be read, so it isn't in Squiffy's Settings dialog." });
24
+ continue;
25
+ }
26
+ // How the import report names it
27
+ const callText = typeof args[0] == "string" ? `Setting.add${kind}(${JSON.stringify(args[0])})` : `Setting.add${kind}()`;
28
+ if (kind == "Header" || kind == "Value") {
29
+ if (kind == "Header")
30
+ problems.push({ text: callText, reason: "Squiffy's Settings dialog has no headings, so this has been left out." });
31
+ continue;
32
+ }
33
+ const [name, options] = args;
34
+ if (typeof name != "string" || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
35
+ problems.push({ text: callText, reason: "A setting's name in Squiffy is letters, digits and underscores, so this has been left out." });
36
+ continue;
37
+ }
38
+ if (settings.some(s => s.name == name))
39
+ continue;
40
+ const o = options && typeof options == "object" && !Array.isArray(options) && !isCode(options) && !isName(options)
41
+ ? options : {};
42
+ const label = typeof o.label == "string" && o.label.trim() ? stripHtml(o.label) : name;
43
+ const description = typeof o.desc == "string" && o.desc.trim() ? stripHtml(o.desc) : undefined;
44
+ const base = { name, label, ...(description ? { description } : {}) };
45
+ if (kind == "Toggle") {
46
+ settings.push({ ...base, type: "toggle", ...(typeof o.default == "boolean" ? { default: o.default } : {}) });
47
+ }
48
+ else if (kind == "List") {
49
+ const list = isName(o.list) ? arrayNamed(script, o.list.name) : o.list;
50
+ const options = Array.isArray(list) ? list.map(optionOf) : null;
51
+ if (!options?.length || options.some(option => !option)) {
52
+ problems.push({ text: callText, reason: "This setting's list of options couldn't be read, so it isn't in Squiffy's Settings dialog." });
53
+ continue;
54
+ }
55
+ const unique = options.filter((option, i, all) => all.findIndex(o => o.value == option.value) == i);
56
+ const defaultValue = o.default === undefined || o.default === null ? undefined : String(o.default);
57
+ settings.push({
58
+ ...base, type: "list", options: unique,
59
+ ...(defaultValue !== undefined && unique.some(option => option.value == defaultValue) ? { default: defaultValue } : {}),
60
+ });
61
+ }
62
+ else {
63
+ const { min, max, step } = o;
64
+ if (typeof min != "number" || typeof max != "number" || min > max) {
65
+ problems.push({ text: callText, reason: "This setting's min and max couldn't be read, so it isn't in Squiffy's Settings dialog." });
66
+ continue;
67
+ }
68
+ settings.push({
69
+ ...base, type: "range", min, max,
70
+ ...(typeof step == "number" && step > 0 ? { step } : {}),
71
+ ...(typeof o.default == "number" && o.default >= min && o.default <= max ? { default: o.default } : {}),
72
+ });
73
+ }
74
+ if (o.onInit !== undefined || o.onChange !== undefined) {
75
+ const setting = settings[settings.length - 1];
76
+ const example = setting.type == "toggle" ? `setting-${name}`
77
+ : setting.type == "list" ? `setting-${name}-${setting.options[0].value.replace(/[^A-Za-z0-9_-]+/g, "-")}`
78
+ : `setting-${name}-${setting.default ?? setting.min}`;
79
+ problems.push({
80
+ text: callText,
81
+ reason: `This setting's onInit and onChange are SugarCube JavaScript, which Squiffy doesn't run. The setting puts a class on the story's element instead, such as ${example}, which the stylesheet can use.`,
82
+ });
83
+ }
84
+ }
85
+ return { settings, problems };
86
+ }
87
+ function optionOf(item) {
88
+ if (typeof item == "string" && item.trim())
89
+ return { value: item, label: item };
90
+ if (typeof item == "number" || typeof item == "boolean")
91
+ return { value: String(item), label: String(item) };
92
+ return null;
93
+ }
94
+ const stripHtml = (text) => text.replace(/<[^>]*>/g, "").replace(/\s+/g, " ").trim();
95
+ // The list a variable is set to in the script, e.g. var sizes = ["Small", "Large"], or
96
+ // setup.sizes = [...]
97
+ function arrayNamed(script, name) {
98
+ const escaped = name.replace(/[.$]/g, m => "\\" + m);
99
+ const declaration = new RegExp(`(?:\\b(?:var|let|const)\\s+|(?<![\\w.$]))${escaped}\\s*=\\s*(?=\\[)`, "g");
100
+ const match = declaration.exec(script);
101
+ if (!match)
102
+ return undefined;
103
+ try {
104
+ return new LiteralParser(script, match.index + match[0].length).value(false);
105
+ }
106
+ catch {
107
+ return undefined;
108
+ }
109
+ }
110
+ // Reads JavaScript literals from a script. Anything else, such as a function, is skipped over and
111
+ // kept as code, so the arguments around it can still be read.
112
+ class LiteralParser {
113
+ constructor(s, i) {
114
+ Object.defineProperty(this, "s", {
115
+ enumerable: true,
116
+ configurable: true,
117
+ writable: true,
118
+ value: s
119
+ });
120
+ Object.defineProperty(this, "i", {
121
+ enumerable: true,
122
+ configurable: true,
123
+ writable: true,
124
+ value: i
125
+ });
126
+ }
127
+ // A call's arguments, up to its closing bracket
128
+ arguments() {
129
+ const args = [];
130
+ this.space();
131
+ if (this.accept(")"))
132
+ return args;
133
+ do {
134
+ this.space();
135
+ if (this.peek() == ")")
136
+ break;
137
+ args.push(this.value());
138
+ this.space();
139
+ } while (this.accept(","));
140
+ this.expect(")");
141
+ return args;
142
+ }
143
+ // A value, which is code if there's more to it, e.g. a sum. In a statement (not wholeExpression),
144
+ // what comes after it isn't looked at.
145
+ value(wholeExpression = true) {
146
+ this.space();
147
+ const c = this.peek();
148
+ const start = this.i;
149
+ let v;
150
+ if (c == "\"" || c == "'" || c == "`")
151
+ v = this.string();
152
+ else if (c == "[")
153
+ v = this.array();
154
+ else if (c == "{")
155
+ v = this.object();
156
+ else if (/[-+\d.]/.test(c) && /^[-+]?(\d|\.\d)/.test(this.s.substring(this.i, this.i + 3)))
157
+ v = this.number();
158
+ else {
159
+ const word = /^[A-Za-z_$][\w$]*(?:\s*\.\s*[A-Za-z_$][\w$]*)*/.exec(this.s.substring(this.i));
160
+ if (word && !["function", "async", "new"].includes(word[0])) {
161
+ this.i += word[0].length;
162
+ const name = word[0].replace(/\s+/g, "");
163
+ v = name == "true" ? true : name == "false" ? false : name == "null" || name == "undefined" ? null : { name };
164
+ }
165
+ else
166
+ v = { code: "" };
167
+ }
168
+ // Anything more, e.g. an arrow function's body or a sum, makes it code
169
+ if (!wholeExpression && !isCode(v))
170
+ return v;
171
+ this.space();
172
+ if (!this.atEnd()) {
173
+ this.skipExpression();
174
+ return { code: this.s.substring(start, this.i) };
175
+ }
176
+ return v;
177
+ }
178
+ atEnd() {
179
+ const c = this.peek();
180
+ return c == "," || c == ")" || c == "]" || c == "}" || c == "";
181
+ }
182
+ // Skips to the end of the expression: the next comma or closing bracket outside any brackets
183
+ skipExpression() {
184
+ let depth = 0;
185
+ while (this.i < this.s.length) {
186
+ const c = this.peek();
187
+ if (c == "\"" || c == "'" || c == "`") {
188
+ this.string();
189
+ continue;
190
+ }
191
+ if (c == "/" && (this.s[this.i + 1] == "/" || this.s[this.i + 1] == "*")) {
192
+ this.space();
193
+ continue;
194
+ }
195
+ if (c == "(" || c == "[" || c == "{")
196
+ depth++;
197
+ else if (c == ")" || c == "]" || c == "}") {
198
+ if (depth == 0)
199
+ return;
200
+ depth--;
201
+ }
202
+ else if (c == "," && depth == 0)
203
+ return;
204
+ this.i++;
205
+ }
206
+ throw new Error("Unfinished expression");
207
+ }
208
+ string() {
209
+ const quote = this.s[this.i++];
210
+ let out = "";
211
+ let template = false;
212
+ while (this.i < this.s.length) {
213
+ const c = this.s[this.i++];
214
+ if (c == quote)
215
+ return template ? { code: out } : out;
216
+ if (c == "\\") {
217
+ const e = this.s[this.i++];
218
+ out += e == "n" ? "\n" : e == "t" ? "\t" : e;
219
+ }
220
+ else {
221
+ if (quote == "`" && c == "$" && this.s[this.i] == "{")
222
+ template = true;
223
+ out += c;
224
+ }
225
+ }
226
+ throw new Error("Unfinished string");
227
+ }
228
+ number() {
229
+ const match = /^[-+]?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/i.exec(this.s.substring(this.i));
230
+ this.i += match[0].length;
231
+ return Number(match[0]);
232
+ }
233
+ array() {
234
+ this.expect("[");
235
+ const items = [];
236
+ this.space();
237
+ while (!this.accept("]")) {
238
+ items.push(this.value());
239
+ this.space();
240
+ if (!this.accept(",")) {
241
+ this.expect("]");
242
+ break;
243
+ }
244
+ this.space();
245
+ }
246
+ return items;
247
+ }
248
+ object() {
249
+ this.expect("{");
250
+ const result = {};
251
+ this.space();
252
+ while (!this.accept("}")) {
253
+ const c = this.peek();
254
+ let key;
255
+ if (c == "\"" || c == "'") {
256
+ const k = this.string();
257
+ if (typeof k != "string")
258
+ throw new Error("Bad key");
259
+ key = k;
260
+ }
261
+ else {
262
+ const word = /^[A-Za-z_$][\w$]*/.exec(this.s.substring(this.i));
263
+ if (!word)
264
+ throw new Error("Bad key");
265
+ key = word[0];
266
+ this.i += key.length;
267
+ }
268
+ this.space();
269
+ if (this.accept(":")) {
270
+ result[key] = this.value();
271
+ }
272
+ else if (this.peek() == "(") {
273
+ // A method, e.g. onChange() { ... }
274
+ const start = this.i;
275
+ this.skipExpression();
276
+ result[key] = { code: this.s.substring(start, this.i) };
277
+ }
278
+ else {
279
+ // Shorthand, e.g. { list }
280
+ result[key] = { name: key };
281
+ }
282
+ this.space();
283
+ if (!this.accept(",")) {
284
+ this.expect("}");
285
+ break;
286
+ }
287
+ this.space();
288
+ }
289
+ return result;
290
+ }
291
+ // Skips spaces and comments
292
+ space() {
293
+ for (;;) {
294
+ const rest = this.s.substring(this.i, this.i + 2);
295
+ if (/^\s/.test(rest))
296
+ this.i++;
297
+ else if (rest == "//") {
298
+ const end = this.s.indexOf("\n", this.i);
299
+ this.i = end < 0 ? this.s.length : end;
300
+ }
301
+ else if (rest == "/*") {
302
+ const end = this.s.indexOf("*/", this.i + 2);
303
+ this.i = end < 0 ? this.s.length : end + 2;
304
+ }
305
+ else
306
+ return;
307
+ }
308
+ }
309
+ peek() {
310
+ return this.s[this.i] ?? "";
311
+ }
312
+ accept(c) {
313
+ if (this.s[this.i] != c)
314
+ return false;
315
+ this.i++;
316
+ return true;
317
+ }
318
+ expect(c) {
319
+ if (!this.accept(c))
320
+ throw new Error(`Expected ${c}`);
321
+ }
322
+ }
@@ -1,10 +1,12 @@
1
1
  import type { Expr } from "./expression.js";
2
+ import type { StoryAudio } from "./audio.js";
2
3
  import type { ConvertedPassage, PassageContext } from "./convert.js";
3
4
  export declare class SugarCubeExpressions {
4
5
  readonly ctx: PassageContext;
5
6
  constructor(ctx: PassageContext);
6
7
  attribute(name: string, temp: boolean): string;
7
8
  variable(name: string, temp: boolean): Expr;
9
+ inContainerWidget: boolean;
8
10
  parse(source: string): Expr;
9
11
  statements(source: string): Statement[];
10
12
  passageName(source: string): {
@@ -26,6 +28,7 @@ export type Statement = {
26
28
  kind: "unset";
27
29
  name: string;
28
30
  };
31
+ export declare function sugarCubeAudio(story: PassageContext["story"]): StoryAudio;
29
32
  export declare function sugarCubeWidgets(story: PassageContext["story"]): Map<string, string>;
30
33
  export declare function convertSugarCubeWidgets(text: string, ctx: PassageContext): {
31
34
  section: string;