squiffy-compiler 7.0.0-beta.5 → 7.0.0-beta.7
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 +6 -0
- package/dist/analyse.js +101 -11
- package/dist/compiler.d.ts +9 -2
- package/dist/compiler.js +5 -2
- package/dist/css-urls.d.ts +4 -0
- package/dist/css-urls.js +59 -0
- package/dist/emit.d.ts +13 -0
- package/dist/emit.js +45 -1
- package/dist/language-service.d.ts +1 -0
- package/dist/language-service.js +39 -13
- package/dist/node.d.ts +7 -1
- package/dist/node.js +16 -5
- package/dist/parse.d.ts +3 -0
- package/dist/parse.js +25 -2
- package/dist/project.d.ts +56 -0
- package/dist/project.js +148 -15
- 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 +504 -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 +505 -0
- package/dist/quest/text.d.ts +17 -0
- package/dist/quest/text.js +305 -0
- package/dist/setters.d.ts +4 -0
- package/dist/setters.js +10 -5
- package/dist/story-graph.d.ts +2 -1
- package/dist/story-graph.js +2 -0
- package/dist/syntax.d.ts +27 -2
- package/dist/text.d.ts +2 -0
- package/dist/text.js +24 -15
- package/dist/twine/audio.d.ts +21 -0
- package/dist/twine/audio.js +80 -0
- package/dist/twine/convert.d.ts +15 -1
- package/dist/twine/convert.js +217 -21
- package/dist/twine/expression.d.ts +26 -1
- package/dist/twine/expression.js +111 -6
- package/dist/twine/harlowe.d.ts +10 -0
- package/dist/twine/harlowe.js +417 -58
- package/dist/twine/sugarcube.d.ts +4 -0
- package/dist/twine/sugarcube.js +388 -86
- package/dist/v5.js +2 -1
- package/package.json +3 -2
- package/syntaxes/squiffy.tmLanguage.json +9 -4
package/dist/twine/expression.js
CHANGED
|
@@ -6,13 +6,15 @@ export class Unconvertible extends Error {
|
|
|
6
6
|
}
|
|
7
7
|
const precedence = {
|
|
8
8
|
"or": 1, "and": 2,
|
|
9
|
-
"=": 4, "!=": 4, ">": 4, ">=": 4, "<": 4, "<=": 4,
|
|
9
|
+
"=": 4, "!=": 4, ">": 4, ">=": 4, "<": 4, "<=": 4, "contains": 4,
|
|
10
10
|
"+": 5, "-": 5,
|
|
11
11
|
"*": 6, "/": 6, "%": 6,
|
|
12
12
|
};
|
|
13
13
|
const notPrecedence = 3;
|
|
14
14
|
const atomPrecedence = 8;
|
|
15
15
|
function precedenceOf(e) {
|
|
16
|
+
if (e.k == "cond")
|
|
17
|
+
return 0;
|
|
16
18
|
if (e.k == "bin")
|
|
17
19
|
return precedence[e.op];
|
|
18
20
|
if (e.k == "not")
|
|
@@ -21,6 +23,11 @@ function precedenceOf(e) {
|
|
|
21
23
|
return 7;
|
|
22
24
|
return atomPrecedence;
|
|
23
25
|
}
|
|
26
|
+
// How a loop's item or position is written in Handlebars, e.g. this, ../this or @index
|
|
27
|
+
function loopRef(e) {
|
|
28
|
+
const up = "../".repeat(e.up);
|
|
29
|
+
return e.kind == "item" ? up + "this" : "@" + up + "index";
|
|
30
|
+
}
|
|
24
31
|
// Text as a quoted string. Handlebars and Squiffy's expression tokenizer both take a backslash
|
|
25
32
|
// before the quote character, but Handlebars leaves other backslashes in, so those can't be
|
|
26
33
|
// written exactly.
|
|
@@ -38,12 +45,42 @@ export function quote(text, options = {}) {
|
|
|
38
45
|
return `"${text}"`;
|
|
39
46
|
}
|
|
40
47
|
const reservedWords = new Set(["and", "or", "not", "true", "false", "null", "undefined"]);
|
|
41
|
-
|
|
48
|
+
const word = /^[A-Za-z_]\w*$/;
|
|
49
|
+
// Whether an attribute name is a path to a value inside an object, written with dots, such as
|
|
50
|
+
// hero.hp (#442). The converters only make names of one word, so a dot can only be a path.
|
|
51
|
+
function isPath(name) {
|
|
52
|
+
const parts = name.split(".");
|
|
53
|
+
return parts.length > 1 && parts.every(part => word.test(part)) && !reservedWords.has(parts[0]);
|
|
54
|
+
}
|
|
55
|
+
// An attribute name in an expression: the name, or [name] if it isn't one word. A path to a
|
|
56
|
+
// value inside an object, hero.hp or inventory[i].name, is written as it is.
|
|
42
57
|
export function attributeRef(name) {
|
|
43
|
-
if (
|
|
58
|
+
if ((word.test(name) && !reservedWords.has(name)) || isPath(name) || isDynamicTarget(name))
|
|
44
59
|
return name;
|
|
45
60
|
return `[${name}]`;
|
|
46
61
|
}
|
|
62
|
+
// Whether a statement's target is a value inside an object found as the story runs, such as
|
|
63
|
+
// inventory[i].name, which only a setter can set: {{set}} and the like take a fixed name
|
|
64
|
+
export function isDynamicTarget(name) {
|
|
65
|
+
return /^[A-Za-z_]\w*[.[]/.test(name) && name.includes("[");
|
|
66
|
+
}
|
|
67
|
+
// A value inside an object or list: hero.hp for a fixed word inside an attribute, otherwise
|
|
68
|
+
// inventory[i] (#442)
|
|
69
|
+
export function member(e, key) {
|
|
70
|
+
if (e.k == "var" && key.k == "str" && word.test(key.v) && (word.test(e.name) || isPath(e.name)) && !reservedWords.has(e.name)) {
|
|
71
|
+
return { k: "var", name: `${e.name}.${key.v}` };
|
|
72
|
+
}
|
|
73
|
+
return { k: "member", e, key };
|
|
74
|
+
}
|
|
75
|
+
// The text of a setter's target, e.g. hero.hp or inventory[i].name, or null if it isn't
|
|
76
|
+
// something that can be set
|
|
77
|
+
export function targetName(e) {
|
|
78
|
+
if (e.k == "var")
|
|
79
|
+
return e.name;
|
|
80
|
+
if (e.k == "member" && targetName(e.e) !== null)
|
|
81
|
+
return print(e);
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
47
84
|
export function print(e, options = {}) {
|
|
48
85
|
switch (e.k) {
|
|
49
86
|
case "num": return String(e.v);
|
|
@@ -51,8 +88,34 @@ export function print(e, options = {}) {
|
|
|
51
88
|
case "bool": return String(e.v);
|
|
52
89
|
case "null": return "null";
|
|
53
90
|
case "var": return attributeRef(e.name);
|
|
91
|
+
case "loop":
|
|
92
|
+
if (!options.inLink)
|
|
93
|
+
return loopRef(e);
|
|
94
|
+
// A link fills in its setters with only its own loop's item
|
|
95
|
+
if (e.up)
|
|
96
|
+
throw new Unconvertible("a link in a loop that sets a variable to an outer loop's item");
|
|
97
|
+
// Filled in as text, so quoted, unless it's a number
|
|
98
|
+
return e.kind == "index" || e.numbers ? `{{${loopRef(e)}}}` : `'{{${loopRef(e)}}}'`;
|
|
54
99
|
case "array": return `(array${e.items.map(i => " " + printArgument(i, options)).join("")})`;
|
|
55
|
-
|
|
100
|
+
// Always a space before "}", so nested objects never have the "}}" that would end a tag
|
|
101
|
+
case "object":
|
|
102
|
+
if (!e.entries.length)
|
|
103
|
+
return "{ }";
|
|
104
|
+
return `{ ${e.entries.map(([key, value]) => `${word.test(key) ? key : quote(key, options)}: ${print(value, options)}`).join(", ")} }`;
|
|
105
|
+
case "member": {
|
|
106
|
+
if (e.e.k == "loop" && options.inLink)
|
|
107
|
+
throw new Unconvertible("a link in a loop that sets a variable to a value inside the loop's item");
|
|
108
|
+
const base = printArgument(e.e, options);
|
|
109
|
+
if (e.key.k == "str" && word.test(e.key.v) && (e.e.k == "member" || (e.e.k == "loop" && e.e.kind == "item"))) {
|
|
110
|
+
return `${base}.${e.key.v}`;
|
|
111
|
+
}
|
|
112
|
+
return `${base}[${print(e.key, options)}]`;
|
|
113
|
+
}
|
|
114
|
+
case "call":
|
|
115
|
+
// .indexOf() that isn't checking whether something is there (see sugarcube.ts) (#447)
|
|
116
|
+
if (e.fn == "__indexof")
|
|
117
|
+
return print({ ...e, fn: "indexof" }, options);
|
|
118
|
+
return `(${e.fn}${e.args.map(a => " " + printArgument(a, options)).join("")})`;
|
|
56
119
|
case "neg": {
|
|
57
120
|
const inner = print(e.e, options);
|
|
58
121
|
return precedenceOf(e.e) < atomPrecedence ? `-(${inner})` : `-${inner}`;
|
|
@@ -61,6 +124,12 @@ export function print(e, options = {}) {
|
|
|
61
124
|
const inner = print(e.e, options);
|
|
62
125
|
return `not ${precedenceOf(e.e) < notPrecedence ? `(${inner})` : inner}`;
|
|
63
126
|
}
|
|
127
|
+
case "cond": {
|
|
128
|
+
// Right-associative, so only a condition in the test or the first value needs brackets
|
|
129
|
+
const test = print(e.test, options);
|
|
130
|
+
const then = print(e.then, options);
|
|
131
|
+
return `${e.test.k == "cond" ? `(${test})` : test} ? ${e.then.k == "cond" ? `(${then})` : then} : ${print(e.otherwise, options)}`;
|
|
132
|
+
}
|
|
64
133
|
case "bin": {
|
|
65
134
|
const p = precedence[e.op];
|
|
66
135
|
const comparison = p == 4;
|
|
@@ -79,6 +148,11 @@ function printArgument(e, options) {
|
|
|
79
148
|
const text = print(e, options);
|
|
80
149
|
return precedenceOf(e) < atomPrecedence || e.k == "neg" ? `(${text})` : text;
|
|
81
150
|
}
|
|
151
|
+
// Whether an expression is a call to a helper that makes a new list (#447), which {{#each}} can
|
|
152
|
+
// go through as it can an attribute
|
|
153
|
+
export function isListCall(e) {
|
|
154
|
+
return e.k == "call" && ["range", "sort", "shuffle", "slice"].includes(e.fn);
|
|
155
|
+
}
|
|
82
156
|
// Whether an expression can only be true or false, for Harlowe's "$a is 1 or 2"
|
|
83
157
|
export function isBoolean(e) {
|
|
84
158
|
if (e.k == "bool" || e.k == "not")
|
|
@@ -89,6 +163,21 @@ export function isBoolean(e) {
|
|
|
89
163
|
return e.fn == "seen" || e.fn == "contains" || e.fn == "at";
|
|
90
164
|
return false;
|
|
91
165
|
}
|
|
166
|
+
// The length of a list or text, which Handlebars gives as its length property: name.length (#446)
|
|
167
|
+
export function lengthOf(e) {
|
|
168
|
+
if (e.k == "str")
|
|
169
|
+
return { k: "num", v: e.v.length };
|
|
170
|
+
if (e.k == "array")
|
|
171
|
+
return { k: "num", v: e.items.length };
|
|
172
|
+
return member(e, { k: "str", v: "length" });
|
|
173
|
+
}
|
|
174
|
+
// Harlowe's (cond: test, a, test2, b, otherwise), as a ? b : c
|
|
175
|
+
export function conditional(values, what) {
|
|
176
|
+
if (values.length < 3 || values.length % 2 == 0)
|
|
177
|
+
throw new Unconvertible(`${what} without a condition and two values for each`);
|
|
178
|
+
const [test, then, ...rest] = values;
|
|
179
|
+
return { k: "cond", test, then, otherwise: rest.length == 1 ? rest[0] : conditional(rest, what) };
|
|
180
|
+
}
|
|
92
181
|
// A random whole number from min to max, as Squiffy's {{random}} picks from an array
|
|
93
182
|
export function randomInteger(min, max) {
|
|
94
183
|
if (min.k != "num" || max.k != "num")
|
|
@@ -110,12 +199,28 @@ export function display(e) {
|
|
|
110
199
|
if (e.k == "num" || e.k == "bool")
|
|
111
200
|
return String(e.v);
|
|
112
201
|
if (e.k == "var") {
|
|
113
|
-
return
|
|
202
|
+
return (word.test(e.name) && !reservedWords.has(e.name)) || isPath(e.name) ? `{{${e.name}}}` : `{{get ${quote(e.name)}}}`;
|
|
114
203
|
}
|
|
115
|
-
if (e.k == "
|
|
204
|
+
if (e.k == "loop")
|
|
205
|
+
return `{{${loopRef(e)}}}`;
|
|
206
|
+
// A helper call is written as it is, unless an argument needs an expression, such as
|
|
207
|
+
// (abs (-hp)), which only {{= }} rewrites
|
|
208
|
+
if (e.k == "call" && e.args.every(isPlain))
|
|
116
209
|
return `{{${print(e).slice(1, -1)}}}`;
|
|
117
210
|
return `{{= ${print(e)}}}`;
|
|
118
211
|
}
|
|
212
|
+
// Whether a value is written the same in Handlebars as in an expression
|
|
213
|
+
function isPlain(e) {
|
|
214
|
+
if (e.k == "num" || e.k == "str" || e.k == "bool" || e.k == "null" || e.k == "loop")
|
|
215
|
+
return true;
|
|
216
|
+
if (e.k == "var")
|
|
217
|
+
return !isDynamicTarget(e.name);
|
|
218
|
+
if (e.k == "call")
|
|
219
|
+
return e.args.every(isPlain);
|
|
220
|
+
if (e.k == "array")
|
|
221
|
+
return e.items.every(isPlain);
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
119
224
|
// A token stream for the expression parsers
|
|
120
225
|
export class Tokens {
|
|
121
226
|
constructor(items) {
|
package/dist/twine/harlowe.d.ts
CHANGED
|
@@ -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 function splitArgs(args: string): string[];
|
|
4
5
|
export declare class HarloweExpressions {
|
|
5
6
|
readonly ctx: PassageContext;
|
|
6
7
|
constructor(ctx: PassageContext);
|
|
7
8
|
attribute(name: string, temp: boolean): string;
|
|
9
|
+
variable(name: string, temp: boolean): Expr;
|
|
8
10
|
parse(source: string, it?: Expr): Expr;
|
|
9
11
|
passageName(source: string): {
|
|
10
12
|
name: string;
|
|
@@ -14,4 +16,12 @@ export declare class HarloweExpressions {
|
|
|
14
16
|
macro(name: string, raw: string, it?: Expr): Expr;
|
|
15
17
|
visits(): Expr;
|
|
16
18
|
}
|
|
19
|
+
export declare const harloweAudioPassages: Set<string>;
|
|
20
|
+
export declare function harloweAudio(story: PassageContext["story"]): StoryAudio;
|
|
21
|
+
export declare function harloweMacros(story: PassageContext["story"]): Map<string, {
|
|
22
|
+
section: string;
|
|
23
|
+
params: string[];
|
|
24
|
+
body: string;
|
|
25
|
+
defined: boolean;
|
|
26
|
+
}>;
|
|
17
27
|
export declare function convertHarlowePassage(text: string, ctx: PassageContext): ConvertedPassage;
|